Home

Awesome

Keycloak integration with OpenFGA (based on Zanzibar) for Fine-Grained Authorization at Scale (ReBAC)

This repository contains a PoC implemented with Keycloak integrated with OpenFGA on demostrating how to apply fine-grained access (FGA) control in a high performance and flexible authorization.

In this version of the PoC we have a direct integration between the Access Manager platform and the OpenFGA solution thanks to the use of the OpenFGA Java SDK to publish the events.

This workshop is based the following article Keycloak integration with OpenFGA (based on Zanzibar) for Fine-Grained Authorization at Scale (ReBAC). You will find there full details about the authorization architecture guidelines and involved components.

In the new version 🔥, I aimed to continue improving the authorization architecture and provide an agnostic approach for exposing and protecting APIs following best practices such as Policy as Code (PaC), decoupling authorization, and low-code authorization. Therefore, I added an identity-aware API gateway, Apache APISIX, as an API sidecar to the authorization architecture to enforce authorization and decouple it from the backend. The gateway uses a plugin I developed called authz-openfga that supports Relationship-Based Access Control (ReBAC) policies because it's integrated with OpenFGA platform. The details are explained in the following article Mastering Access Control: Implementing Low-Code Authorization Based on ReBAC and Decoupling Pattern.

Authorization Framework (New)

The following diagram illustrates the solution architecture of this workshop:

<p align="center"> <img width="90%" height="90%" src="doc/images/workshop-docker-environment.png"> </p>

You can also have Store OpenFGA API is protected by OAuth 2.0 and it utilizes the OpenFGA SDK for FGA as example (Protection at API Level).

Another cool feature of custom extension is its capability to discover the OpenFGA authorization model and determine which events are handled. This gives you the flexibility to choose your authorization model, whether it’s RBAC, GBAC, or both 🙌.

How to install?

Prerequisites

Deploy the PoC

  1. Clone this repository

    git clone https://github.com/embesozzi/keycloak-openfga-workshop
    cd keycloak-openfga-workshop
    
  2. Execute following Docker Compose command to start the deployment

    docker-compose -f docker-compose.yml -f docker-compose-apps.yml -f docker-compose-openfga.yml -f docker-compose-import.yml up
    
  3. To be able to use this environment, you need to add this line to your local HOSTS file:

    127.0.0.1  keycloak openfga store store-openfga-api store-authz-gateway
    
  4. Access the following web UIs using URLs bellow via a web browser.

    ComponentURICredentialImage
    Keycloak Consolehttp://keycloak:8081admin / passwordquay.io/keycloak/keycloak:25.0.1
    OpenFGA Playgroundhttp://localhost:3000/playgroundopenfga/openfga:v1.5.5
    Store Portalhttp://store:9090Custom image
    Store Authorization Gatewayhttp://store-authz-gateway:9080Custom image based Apache APISIX Gateway
    Store OpenFGA APIhttp://store-openfga:9091Custom image

In the latest version, the store portal calls the API exposed by the Store Authorization Gateway (store-authz-gateway:9080). If you want to call the Store OpenFGA API (store-openfga:9091), just adjust the store portal configuration

Review configuration (optional)

In this new version of the workshop, we automatically import all the required configurations into the Keycloak and OpenFGA platforms 😄 🪄

Keycloak Platform

OpenFGA Platform

Test cases

As an example, we will implement an Product Catalog web application that has the following requirements:

You can follow the test cases described in the Keycloak integration with OpenFGA (based on Zanzibar) for Fine-Grained Authorization at Scale (ReBAC).

And the article Mastering Access Control: Implementing Low-Code Authorization Based on ReBAC and Decoupling Pattern.

Nevertheless, the use cases are detailed below:

Use case 1: Access to the Store for managing products as an Analyst (Paula)

  1. Access to the store web application store web application and proceed to login with Paula (paula / demo1234!) in Keycloak.

    <img src="doc/images/tc-store-login.png" width="80%" height="80%">
  2. Keycloak will return the id_token and the access token to the application

  3. The Store web application will show the product catalog

    3.1. The app will call the product API sending the access token in the Authorization header

    3.2. The API will apply the following steps:

    3.3. It will validate the token following the OAuth 2.0 standard and it will extract the claim sub to identify the user

    3.4. Then it will call the OpenFGA API to check if the user has the view-product role with the relationship assignee

    3.5. OpenFGA will return the response “allowed”

    3.6. The API will return the product information and the Store web app will show the product catalog to the user

    <img src="doc/images/tc1-view-products-success.png" width="80%" height="80%">
  4. Try to publish a product by clicking the button "Publish" but you will see that Paula is not allowed

    4.1. The app will call the product API sending the access token in the Authorization header

    4.2. The API will apply the following steps:

    4.3. It will validate the token following the OAuth 2.0 standard and it will extract the claim sub to identify the user

    4.4. Then it will call the OpenFGA API to check if the user has the edit-product role with the relationship assignee

    4.5. OpenFGA will return the response “denied”

    <img src="doc/images/tc1-publish-product-denied.png" width="80%" height="80%">

Use case 2: Access to the Store for managing products as an Admin (Richard)

  1. Access to the store web application store web application and proceed to login with Richard (richard / demo1234!) in Keycloak.

    <img src="doc/images/tc-store-login.png" width="80%" height="80%">
  2. Keycloak will return the id_token and the access token to the application

  3. The store web application will show the product catalog

    <img src="doc/images/tc-2-view-products-success.png" width="80%" height="80%">
  4. Try to publish a product by clicking the button "Publish" but you will see that Richard is allowed

    <img src="doc/images/tc-2-publish-product-success.png" width="80%" height="80%">

Use case 3: Access to the Store for managing products as an Regular User (Peter)

  1. Access to the store web application store web application and proceed to login with Peter (peter / demo1234!) in Keycloak.

    <img src="doc/images/tc-store-login.png" width="80%" height="80%">
  2. Keycloak will return the id_token and the access token to the application

  3. The store web application will try to the products show but the user is not allowed

    <img src="doc/images/tc-3-view-products-denied.png" width="80%" height="80%">