Coder Social home page Coder Social logo

abac's Introduction

abac

Simple implementation for Attribute-Based Access Control (ABAC).

Install

go get github.com/yuriiiz/abac

Example

policy := Policy{
    Name:        "Name",
    Description: "Description",
    Statements: []Statement{
        {
            Effect:  Allow,
            Actions: []string{"read", "write", "audit"},
            Resources: Resources{
                ResourceType:  "resource_type_1",
                ResourceNames: []string{"*"},
                CompareFunc:   StrGlob,
            },
        },
        {
            Effect:  Allow,
            Actions: []string{"read", "write", "audit"},
            Resources: Resources{
                ResourceType:  "resource_type_2",
                ResourceNames: []string{"prefix_.*"},
                CompareFunc:   StrRegex,
            },
        },
        {
            Effect:  Deny,
            Actions: []string{"write"},
            Resources: Resources{
                ResourceType:  "resource_type_1",
                ResourceNames: []string{"resource_name_1", "resource_name_2", "resource_name_3"},
                CompareFunc:   StrEqual,
            },
            Conditions: Conditions{
                Items: []Condition{
                    {
                        Operator:      "gt",
                        AttributeType: AttributeTypeEnv,
                        AttributeKey:  "current_timestamp",
                        Value:         1600000000,
                    },
                    {
                        Operator:      "lt",
                        AttributeType: AttributeTypeEnv,
                        AttributeKey:  "current_timestamp",
                        Value:         1700000000,
                    },
                    {
                        Operator:      "eq",
                        AttributeType: AttributeTypeResource,
                        AttributeKey:  "environment",
                        Value:         "production",
                    },
                },
                Formula: "$0 and $1 and $2", // TODO: to be supported
            },
        },
    },
}

var resource Resource
var attributes Attributes

resource = Resource{
    ResourceType: "resource_type_1",
    ResourceName: "resource_name",
}
fmt.Println(policy.Validate("read", resource, attributes)) // -> nil

resource = Resource{
    ResourceType: "resource_type_2",
    ResourceName: "prefix_resource_name",
}
fmt.Println(policy.Validate("read", resource, attributes)) // -> nil

resource = Resource{
    ResourceType: "resource_type_2",
    ResourceName: "resource_name",
}
fmt.Println(policy.Validate("read", resource, attributes)) // -> ErrStatementNotMatch

resource = Resource{
    ResourceType: "resource_type_1",
    ResourceName: "resource_name_1",
}
attributes = []Attribute{
    {
        Type:      AttributeTypeResource,
        Key:       "environment",
        ValueType: "string",
        Value:     "production",
    },
    {
        Type:      AttributeTypeEnv,
        Key:       "current_timestamp",
        ValueType: "number",
        Value:     1650000000,
    },
}
fmt.Println(policy.Validate("write", resource, attributes)) // -> ErrDeniedByStatement

abac's People

Contributors

yuriiiz 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.