Coder Social home page Coder Social logo

casbin-blob-adapter's Introduction

casbin-blob-adapter

Go Reference

Azure Blob Storage adapter for casbin.

Casbin adapter implementation for Azure Blob Storage.

Installation

go get github.com/RedeployAB/casbin-blob-adapter 

Example usage

This example uses azcore.TokenCredential as credentials for the adapter. See Constructor functions below for other options.

package main

import (
    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    blobadapter "github.com/RedeployAB/casbin-blob-adapter"
    "github.com/casbin/casbin/v2"
)

func main() {
    // Create credentials for Azure Blob Storage (service principal, managed identity, az cli).
    cred, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        // Handle error.
    }

    // Create the adapter for Azure Blob Storage. Provide account (storage account name),
    // container name, blob name and credentials. If the container and blob does not exist,
    // they will be created.
    a, err := blobadapter.NewAdapter("account", "container", "policy.csv", cred)
    if err != nil {
        // Handle error.
    }

    e, err := casbin.NewEnforcer("rbac_with_domains_model.conf", a)
    if err != nil {
        // Handle error.
    }

    // Load the policy from the specified blob in Azure Blob Storage manually.
    // NOTE: Like all implicit and explicit adapters the policies is loaded
    // automatically when calling NewEnforcer. This method can be used at
    // runtime to reload policy.
    if err := e.LoadPolicy(); err != nil {
        // Handle error.
    }

    // Check the permission.
    ok, err := e.Enforce("alice", "domain1", "data1", "read")
    if err != nil {
        // Handle error.
    }

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

    // Save policy back to the blob in Azure Blob Storage.
    if err := e.SavePolicy(); err != nil {
        // Handle error.
    }
}

Constructor functions

NewAdapter(account string, container string, blob string, cred azcore.TokenCredential, options ...Option) (*Adapter, error)

Uses azcore.TokenCredential. See azidentity for more options on creating credentials.

cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
    // Handle error.
}

a, err := blobadapter.NewAdapter("account", "container", "policy.csv", cred)
if err != nil {
    // Handle error.
}

NewAdapterFromConnectionString(connectionString string, container string, blob string, options ...Option) (*Adapter, error)

Uses a connection string for an Azure Storage account.

a, err := blobadapter.NewAdapterFromConnectionString("connectionstring", "container", "policy.csv")
if err != nil {
    // Handle error.
}

NewAdapterFromSharedKeyCredential(account string, key string, container string, blob string, options ...Option) (*Adapter, error)

Uses storage account name and key for an Azure Storage account.

a, err := blobadapter.NewAdapterFromSharedKeyCredential("account", "key", "container", "policy.csv")
if err != nil {
    // Handle error.
}

casbin-blob-adapter's People

Contributors

dependabot[bot] avatar karlgw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.