spanneradapter

package module
v0.2.3 Latest Latest
Warning

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

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

README

main

This library implements a Casbin adapter for Cloud Spanner.

To install:

$ go get github.com/flowerinthenight/casbin-spanner-adapter

Example usage:

package main

import (
    "flag"
    "log"
    "time"

    "github.com/casbin/casbin/v2"
    spanneradapter "github.com/flowerinthenight/casbin-spanner-adapter"
)

func main() {
    a, _ := spanneradapter.NewAdapter(
        "projects/{v}/instances/{v}/databases/{v}",
        spanneradapter.WithSkipDatabaseCreation(true),
    )

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

    // Load stored policy from database.
    e.LoadPolicy()

    // Do permission checking.
    e.Enforce("alice", "data1", "write")

    // Do some mutations.
    e.AddPolicy("alice", "data2", "write")
    e.RemovePolicy("alice", "data1", "write")

    // Persist policy to database.
    e.SavePolicy()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

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

Adapter represents a Cloud Spanner-based adapter for policy storage.

func NewAdapter

func NewAdapter(db string, opts ...Option) (*Adapter, error)

NewAdapter creates an Adapter instance. Use the "projects/{project}/instances/{instance}/databases/{db}" format for 'db'. Instance creation is not supported. If database creation is not skipped, it will attempt to create the database. If table creation is not skipped, it will attempt to create the table as well.

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage. Part of the auto-save feature.

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database. Implements casbin Adapter interface.

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) RemovePolicy

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

RemovePolicy removes a policy rule from the storage. Part of the auto-save feature.

func (*Adapter) SavePolicy

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

SavePolicy saves policy to database. Implements casbin Adapter interface.

type CasbinRule

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

func (CasbinRule) ToString

func (c CasbinRule) ToString() string

type Option added in v0.2.0

type Option interface {
	Apply(*Adapter)
}

func WithDatabaseAdminClient added in v0.2.0

func WithDatabaseAdminClient(c *dbv1.DatabaseAdminClient) Option

WithDatabaseAdminClient sets the adapter's database client. If not provided, an internal client is created using the environment's default credentials.

func WithSkipDatabaseCreation added in v0.2.0

func WithSkipDatabaseCreation(v bool) Option

WithSkipDatabaseCreation allows caller to skip the database creation.

func WithSkipTableCreation added in v0.2.0

func WithSkipTableCreation(v bool) Option

WithSkipTableCreation allows caller to skip the table creation.

func WithSpannerClient added in v0.2.0

func WithSpannerClient(c *spanner.Client) Option

WithSpannerClient sets the adapter's Spanner client. If not provided, an internal client is created using the environment's default credentials.

func WithTableName added in v0.2.0

func WithTableName(v string) Option

WithTableName sets adapter's internal table name. Default is 'casbin_rule'.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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