Giter VIP home page Giter VIP logo

udaru-roles-example's Introduction

Udaru Roles Example

This is an example of using Udaru's policy based access control (PBAC) features in a way that should be familiar to developers and administrators with knowledge of role based access control (RBAC).

Quick Start

  • Start PostgreSQL: docker-compose up -d
  • Initialise PostgreSQL: npm run pg:init
  • Start the back end: node server
  • Create Udaru users, policies, etc.: run the ./create-policies script
  • Start the front end: npm start
  • Visit http://localhost:3000 in your browser!

Udaru Configuration

This example uses four users and two teams.

First there is a special root user created when npm run pg:init is run. This user has access to everything by default.

Another user is "Rachel Owner" who is the organization owner. She has certain administrative privileges within Udaru, but does not have any policies that grant her access to the /products resource. Rachel and the root user's accounts are used to authenticate API calls in ./create-policies.

🚨 Warning: in real code you would never authenticate from the front end to Udaru using the root user or organization owner. They are included in the front end to illustrate how access control works for these users.

Finally, there are "Margaret Managerson" and "Sam Staffman". Margaret is assigned to the managers team, while Sam is assigned to the staff team. Members of the managers team are allowed to create and list products, while members of the staff team are allowed to list products only.

While PBAC allows for defining more nuanced access rules than RBAC, in this example Udaru teams are used analogously to RBAC roles.

To see the API calls used to initialize the Udaru policies for this example, along with comments, take a look inside the ./create-policies script.

Back End

The back end is a Hapi server. The server hosts Udaru endpoints added with the Hapi plugin. For this example, two additional endpoints have been added. First, there is the /products endpoint. It allows the DELETE, GET, POST, and PUT methods. There is also a /products/reverse endpoint that allows POST requests.

To secure a route with Udaru, the route code itself does not need to incorporate knowledge of how the system will authorize requests. Instead, configuration values are added that define what action and resource should be checked against the policies for the user authenticated for that request.

Take, for example, the GET /products endpoint. The resource and action names used here were defined in ./create-policies.

UdaruServer.route({
  method: 'GET',
  path: '/products',
  handler: (request, reply) => reply(products),
  config: {
    plugins: {
      auth: {
        action: 'org1:action:list',
        resource: '/products'
      }
    }
  }
})

Front End

This example uses a React app for the front end. Five buttons are displayed. If the user is allowed to use the action on the resource associated with a button, that button will be displayed in green. If the user does not have access, the button will be displayed in red.

Clicking the button will attempt to perform the given action on the /products resource. If the user has permission, the action will succeed. If not, an error from the back end will be displayed that gives some detail of why the request failed.

The batch access endpoint is used to check a number of permissions for a user in one request. For example, this GET request to the batch access endpoint is used in the front end's changeUser function:

const resources = await fetchJSON(`/authorization/access/${userId}`, {
  body: JSON.stringify({
    resourceBatch: [
      { action: 'org1:action:create', resource: '/products' },
      { action: 'org1:action:list', resource: '/products' },
      { action: 'org1:action:delete', resource: '/products' },
      { action: 'org1:action:append', resource: '/products' },
      { action: 'org1:action:reverse', resource: '/products' }
    ]
  }),
  headers: { Authorization: userId },
  method: 'POST'
})

Clean Up

Simply kill the front end and back end processes, then run docker-compose down to stop PostgreSQL and delete its container and related storage.

udaru-roles-example's People

Contributors

william-riley-land avatar mihaidma avatar

Watchers

Jonas Galvez avatar Ivan Frantar avatar Andrea Campolonghi avatar Matteo Collina avatar  avatar Julian Goacher avatar Adrian Rossouw avatar David Gonzalez avatar Tom Andrews avatar Filippo De Santis avatar Marius Voila avatar David Rankin avatar Jeff Simons avatar Luca Maraschi avatar John Kelly avatar Gustavo Caldeira avatar Alex Knol avatar Vladimir Adamić avatar Marco avatar Andrej Stas avatar M Avdi avatar Nicolas Morel avatar Jason Melo avatar James Cloos avatar Jhey Tompkins avatar Donovan Hutchinson avatar  avatar Paul Negrutiu avatar Mihovil Rister avatar Andrea Forni avatar cianomaidin avatar Jiri Spac avatar Ramon Mulia avatar Eleftherios Paraskevas avatar Jack Clark avatar David Spautz avatar Artur Daschevici avatar Brian Mullan avatar Kristin Galvin avatar Clement Le Marc avatar Mircea Alexandru avatar Justin Wolber avatar  avatar Salman Mitha avatar Davide Fiorello avatar Colm Harte avatar Valerio Lanziani avatar Danijel Maksimovic avatar Jhonantans Moraes Rocha avatar Rafael Zeffa avatar  avatar Ivan Jovanovic avatar  avatar Paul Isache avatar Nick Delfino avatar ron litzenberger avatar Sergey avatar Rob Gormley avatar Josiah Bjorgaard avatar Thiago Kroger avatar Michael avatar Siva Boyapati avatar Ruben Bridgewater avatar Jose Bravo avatar Igor Shmukler avatar Ian Read avatar Manuel Spigolon avatar Alessandro Santarini avatar Nathan Power avatar Bruno Amaral avatar  avatar Mark Ireland avatar Tomás Monteiro avatar Rafael Gonzaga avatar  avatar

udaru-roles-example's Issues

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.