cosmosadapter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

CosmosDB Adapter

CosmosDB Adapter is the cosmosDB adapter for Casbin. With this library, Casbin can load policy from CosmosDB or save policy to it.

Installation

go get github.com/spacycoder/cosmos-adapter

Note

When creating your collection for storing casbin rules you should use "/pType" as your partitionKey

Simple Example

package main

import (
	"github.com/casbin/casbin"
	cosmosadapter "github.com/spacycoder/cosmos-casbin-adapter"
)

func main() {
	// Initialize a CosmosDB adapter and use it in a Casbin enforcer:
	// The first argument is the cosmos connection string.
	// The second argument takes options as its input.
	// if not option is given the default database name is "casbin" and the default collection name is "casbin_rule"
	// The adapter will try to create the database and collection if it does not find them.
	a := cosmosadapter.NewAdapter("connstring")
	e := casbin.NewEnforcer("examples/rbac_model.conf", a)

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

	// Check the permission.
	e.Enforce("alice", "data1", "read")

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

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

With options

package main

import (
	"github.com/casbin/casbin"
	cosmosadapter "github.com/spacycoder/cosmos-casbin-adapter"
)

func main() {
	// Initialize a CosmosDB adapter and use it in a Casbin enforcer:
	// The adapter will try to create the database and collection if it does not find them.
	a := cosmosadapter.NewAdapter("connstring", cosmosadapter.Database("mycasbindb"), cosmosadapter.Collection("mycasbincollection"))
	e := casbin.NewEnforcer("examples/rbac_model.conf", a)

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

	// Check the permission.
	e.Enforce("alice", "data1", "read")

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

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

Filtered Policies

import "github.com/spacycoder/cosmosdb-go-sdk/cosmos"

// This adapter also implements the FilteredAdapter interface. This allows for
// efficent, scalable enforcement of very large policies:

filter := cosmos.Q{Query: "SELECT * FROM root WHERE root.v0 = @v0", Parameters: []cosmos.P{{Name: "@v0", Value: "bob"}}}
e.LoadFilteredPolicy(filter)

// The loaded policy is now a subset of the policy in storage, containing only
// the policy lines that match the provided filter.

Getting Help

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAdapter

func NewAdapter(endpoint string, cred *azidentity.DefaultAzureCredential, options Options) persist.Adapter

NewAdapter is the constructor for Adapter. if no options are given the database name is "casbin" and the containerClient is named casbin_rule if the database or containerClient is not found it is automatically created. the database can be changed by using the Database(db string) option. the containerClient can be changed by using the Collection(coll string) option. see README for example

func NewAdapterFromClient

func NewAdapterFromClient(client *azcosmos.Client, options Options) persist.Adapter

func NewAdapterFromConnectionSting

func NewAdapterFromConnectionSting(connectionString string, options Options) persist.Adapter

Types

type CasbinRule

type CasbinRule struct {
	ID    string `json:"id"`
	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"`
}

CasbinRule represents a rule in Casbin.

type Data

type Data struct {
	Documents interface{} `json:"Documents,omitempty"`
	Count     int         `json:"_count,omitempty"`
}

type Options

type Options struct {
	azcosmos.ClientOptions
	DatabaseName  string
	ContainerName string
}

type P

type P = QueryParam

type QueryParam

type QueryParam struct {
	Name  string      `json:"name"`
	Value interface{} `json:"value"`
}

type SqlQuerySpec

type SqlQuerySpec struct {
	Query      string                    `json:"query"`
	Parameters []azcosmos.QueryParameter `json:"parameters,omitempty"`
}

func Q

func Q(query string, queryParams ...azcosmos.QueryParameter) *SqlQuerySpec

Jump to

Keyboard shortcuts

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