cloudstorageadapter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: MIT Imports: 11 Imported by: 0

README

Casbin Cloud Storage Adapter

Go Report Card Build Status Coverage Status Godoc


Casbin adapter implementation for GCP Cloud Storage. With this library, Casbin can load or save policies from/to Google Cloud Storage buckets.

Installation

go get github.com/qurami/casbin-cloud-storage-adapter/v1

Example Usage

package main

import (
	"context"
	"log"

	"cloud.google.com/go/storage"
	"github.com/casbin/casbin/v2"
	cloudstorageadapter "github.com/qurami/casbin-cloud-storage-adapter"
)

func main() {
	// Initialize a Google Cloud Storage client
	// There are many ways, this is the quickest one.
	// You could need a different one according to your configuration,
	// please see https://pkg.go.dev/cloud.google.com/go/storage
	cloudStorageClient, err := storage.NewClient(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	// Create a new cloudstorageadapter.Adapter
	adapter, err := cloudstorageadapter.NewAdapter(
		cloudStorageClient,
		"myBucketName",
		"path/to/policies.csv",
	)
	if err != nil {
		log.Fatal(err)
	}

	// Use the adapter in the casbin.NewEnforcer constructor
	enforcer, err := casbin.NewEnforcer("rbac_model.conf", adapter)
	if err != nil {
		log.Fatal(err)
	}

	// Use the enforcer as usual
	roles, err := enforcer.GetImplicitRolesForUser("alice")
	if err != nil {
		log.Fatal(err)
	}

	log.Println(roles)
}

The same file with the corresponding RBAC model is available in the examples folder.

Missing Features

This version is missing the autosave features, so please remember to manually execute the enforcer.SavePolicy method when using this adapter.

License

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

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 implements casbin/persist.Adapter storing policy configuration on a Google Cloud Storage Bucket

func NewAdapter

func NewAdapter(client *storage.Client, bucketName string, objectKey string) (*Adapter, error)

NewAdapter creates new Adapter

Parameters:

  • client A cloud.google.com/go/storage.Client object
  • bucketName Name of the bucket where the policy configuration file is stored on
  • objectKey Key (name) of the object that contains policy configuration

func (*Adapter) AddPolicy

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

AddPolicy adds a policy rule to the storage. This is part of the Auto-Save feature.

func (*Adapter) LoadPolicy

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

LoadPolicy loads policy from database.

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. This is 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. This is part of the Auto-Save feature.

func (*Adapter) SavePolicy

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

SavePolicy saves all policy rules to the storage.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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