Giter VIP home page Giter VIP logo

goslo.policy's Introduction

Build Status

A go implementation of OpenStack's oslo.policy

This repository provides a reimplementation of the original oslo.policy library written in python. It is meant to provide the same RBAC semantics for OpenStack enabled applications written in go.

You can view the API docs here: https://pkg.go.dev/github.com/databus23/goslo.policy

Usage

package main

import (
	"log"

	policy "github.com/databus23/goslo.policy"
)

func main() {
	rules := map[string]string{
		"admin_required": "role:admin",
		"cloud_admin":    "rule:admin_required and domain_id:default",
		"owner":          "user_id:%(user_id)s",
	}
	//Load and parse policy
	enforcer, err := policy.NewEnforcer(rules)
	if err != nil {
		log.Fatal("Failed to parse policy ", err)
	}
	//Context provides the current token & request information needed for enforcement
	ctx := policy.Context{
		Auth: map[string]string{
			"user_id":   "u-1",
			"domain_id": "default",
		},
		Roles: []string{"admin"},
		Request: map[string]string{
			"user_id": "u-1",
		},
	}

	if enforcer.Enforce("cloud_admin", ctx) {
		log.Println("user is a cloud admin")
	}
	if enforcer.Enforce("owner", ctx) {
		log.Println("user is owner")
	}
}

The package includes optional debug logging that can be enabled per context:

if os.Getenv("DEBUG") == "1" {
    ctx.Logger = log.Printf //or any other function with the same signature
}

goslo.policy's People

Contributors

databus23 avatar majewsky avatar supersandro2000 avatar

Watchers

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