Skip to content

qurami/casbin-cloud-storage-adapter

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

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.