arangodbadapter

package module
v0.0.0-...-a9ec999 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

ArangoDB Adapter

GoDoc Go Report Card Build Status

ArangoDB Adapter is the Arango DB adapter for Casbin.

TODO

  • Adapter cleanup (closing connections). See this issue.
  • Remove hardcoded db & collection names.
  • Indexes.
  • Filtered policies.
  • Policy removal.
  • Add partial policy removal.
  • Unit tests.
  • Better README (examples of use).

Example

Following snippet of code shows how to initialize adapter and use with casbin enforcer. See documentation for list of all available options.

a, err := arango.NewAdapter(
    arango.OpCollectionName("casbinrules"),
    arango.OpFieldMapping("p", "sub", "obj", "act"))
if err != nil {
    ...
}

e, err := casbin.NewEnforcer("model.conf", a)

...

Contributing

Documentation

Currently this README and examples folder are best source of documentation for this project and of course - source code itself.

Reporting issues

Raise an issue for bugs, enhancements and general discussions/questions about adapter.

Running tests

It would make very little sense to perform an isolated unit tests for code like that. Therefore tests connects to real database instance. To test fully all options two instances must be run: with and without authorization. CI setup may be helpful to establish working testing rig:

docker run -e ARANGO_NO_AUTH=1 -p 127.0.0.1:8529:8529 -d --name arangodb-instance-no-auth arangodb:3.7.2
docker run -e ARANGO_ROOT_PASSWORD=password -p 127.0.0.1:8530:8529 -d --name arangodb-instance-auth arangodb:3.7.2

Then, running tests is as simple as:

go test .
ok  	github.com/adamwasila/arangodb-adapter	0.847s
Pull requests

If possible each PR should be linked to some issue (except trivial ones like typo fixes). Avoid unrelated changes. Redundant commits should be squashed together before merge.

Getting Help

  • Casbin - main library this adapter is extending

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrTooManyArguments      error = errors.New("policy has too many arguments")
	ErrInvalidPolicyDocument error = errors.New("db document does not match valid policy")
	ErrTooManyFields         error = errors.New("unmaped values in remove request")
)

Functions

func NewAdapter

func NewAdapter(options ...adapterOption) (persist.Adapter, error)

NewAdapter creates new instance of adapter. If called with no argument default options are applied. Options may reconfigure all or some parameters to different values. See description of each Option for details.

Example
a, err := NewAdapter(
	OpCollectionName("casbinrules_example"),
	OpFieldMapping("p", "sub", "obj", "act"))

if err != nil {
	fmt.Printf("Adapter creation error! %s\n", err)
	return
}

m, err := model.NewModelFromString(`
	[request_definition]
	r = sub, obj, act
	
	[policy_definition]
	p = sub, obj, act
	
	[policy_effect]
	e = some(where (p.eft == allow))
	
	[matchers]
	m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
	`)
if err != nil {
	fmt.Printf("Enforcer creation error! %s\n", err)
	return
}

e, err := casbin.NewEnforcer(m, a)
if err != nil {
	fmt.Printf("Enforcer creation error! %s\n", err)
	return
}
err = e.LoadPolicy()
if err != nil {
	fmt.Printf("Load policy error! %s\n", err)
	return
}

sub, obj, act := "adam", "data1", "read"

_, _ = e.AddPolicy("adam", "data1", "read")
_ = e.SavePolicy()

r, err := e.Enforce(sub, obj, act)
if err != nil {
	fmt.Printf("Failed to enforce! %s\n", err)
	return
}
if !r {
	fmt.Printf("%s %s %s: Forbidden!\n", sub, obj, act)
} else {
	fmt.Printf("%s %s %s: Access granted\n", sub, obj, act)
}
Output:

adam data1 read: Access granted

func OpAutocreate

func OpAutocreate(autocreate bool) func(*adapter)

OpAutocreate enables autocreate mode - both database and collection will be created by adapter if not exist. Should be used with care as each structure is created with driver default options set.

func OpBasicAuthCredentials

func OpBasicAuthCredentials(user, passwd string) func(*adapter)

OpBasicAuthCredentials configures username and password of database used; default is ""

func OpCollectionName

func OpCollectionName(collectionName string) func(*adapter)

OpCollectionName configures name of collection used; default is "casbin_rules"

func OpDatabaseName

func OpDatabaseName(dbName string) func(*adapter)

OpDatabaseName configures name of database used; default is "casbin"

func OpEndpoints

func OpEndpoints(endpoints ...string) func(*adapter)

OpEndpoints configures list of endpoints used to connect to ArangoDB; default is: http://127.0.0.1:8529

func OpFieldMapping

func OpFieldMapping(mapping ...string) func(*adapter)

OpFieldMapping configures mapping to fields used by adapter; default is same used by MongoDB (for eaasy migration): "PType", "V0", "V1", ..., "V6"

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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