etcdadapter

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

etcd-adapter

Build Status Coverage Status Godoc

ETCD adapter is the policy storage adapter for Casbin. With this library, Casbin can load policy from ETCD and save policy to it. ETCD adapter support the Auto-Save feature for Casbin policy. This means it can support adding a single policy rule to the storage, or removing a single policy rule from the storage.

Installation

go get -u github.com/trinhdaiphuc/etcd-adapter

Sample Example

package main

import (
	"fmt"
	"github.com/casbin/casbin/v2"
	"github.com/trinhdaiphuc/etcd-adapter"
)

func main() {
	// Initialize a casbin etcd adapter and use it in a Casbin enforcer:
	// The adapter will use the ETCD and a named path with the key you give.
	// If not provided, the adapter will use the default value casbin_policy.
	a := etcdadapter.NewAdapter(
		etcdadapter.WithEndpoints([]string{"http://localhost:2379"}),
		etcdadapter.WithEtcdAuth("root", "password"),
		etcdadapter.WithKey("casbin_policy_test"),
	) // Your etcd endpoints and the path key.

	e, _ := casbin.NewEnforcer("rbac_model.conf", a)

	// Load the policy from ETCD.
	e.LoadPolicy()

	e.EnableAutoSave(true)

	// Check the permission.
	result, err := e.Enforce("alice", "data1", "read")

	fmt.Printf("result %v, error %v\n", result, err)

	// Modify the policy.
	// e.AddPolicy(...)
	// e.RemovePolicy(...)

	// Save the policy back to DB.
	e.SavePolicy()
}

Documentation

Index

Constants

View Source
const (
	// DIALTIMEOUT is the timeout for failing to establish a connection.
	DIALTIMEOUT = 5 * time.Second

	// DIALKEEPALIVETIME is the time after which client pings the server to see if
	// transport is alive.
	DIALKEEPALIVETIME = 5 * time.Second

	REQUESTTIMEOUT = 5 * time.Second

	// DIALKEEPALIVETIMEOUT is the time that the client waits for a response for the
	// keep-alive probe. If the response is not received in this time, the connection is closed.
	DIALKEEPALIVETIMEOUT = 10 * time.Second

	// PLACEHOLDER represent the NULL value in the Casbin Rule.
	PLACEHOLDER = "_"

	// DEFAULT_KEY is the root path in ETCD, if not provided.
	DEFAULT_KEY = "casbin_policy"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter represents the ETCD adapter for policy storage.

func NewAdapter

func NewAdapter(opts ...AdapterOption) *Adapter

func (*Adapter) AddPolicies

func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error

AddPolicies adds policy rules to the storage. This is part of the Auto-Save feature.

func (*Adapter) AddPolicy

func (a *Adapter) AddPolicy(sec string, ptype string, line []string) error

AddPolicy adds a policy rule to the storage. Part of the Auto-Save feature.

func (*Adapter) LoadPolicy

func (a *Adapter) LoadPolicy(model model.Model) error

LoadPolicy loads all of policys from ETCD

func (*Adapter) RemoveFilteredPolicy

func (a *Adapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error

RemoveFilteredPolicy removes policy rules that match the filter from the storage. Part of the Auto-Save feature.

func (*Adapter) RemovePolicies

func (a *Adapter) RemovePolicies(sec string, ptype string, rules [][]string) error

RemovePolicies removes policy rules from the storage. This is part of the Auto-Save feature.

func (*Adapter) RemovePolicy

func (a *Adapter) RemovePolicy(sec string, ptype string, line []string) error

RemovePolicy removes a policy rule from the storage. Part of the Auto-Save feature.

func (*Adapter) SavePolicy

func (a *Adapter) SavePolicy(model model.Model) error

This will rewrite all of policies in ETCD with the current data in Casbin

type AdapterOption

type AdapterOption func(*Adapter)

func WithEndpoints

func WithEndpoints(etcdEndpoints []string) AdapterOption

func WithEtcdAuth

func WithEtcdAuth(username, password string) AdapterOption

func WithKey

func WithKey(key string) AdapterOption

type CasbinRule

type CasbinRule struct {
	Key   string `json:"key"`
	PType string `json:"ptype"`
	V0    string `json:"v0"`
	V1    string `json:"v1"`
	V2    string `json:"v2"`
	V3    string `json:"v3"`
	V4    string `json:"v4"`
	V5    string `json:"v5"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL