事件发布器支持 Keycloak 和 OpenFGA 之间的事件集成。
Event Publisher enables event integration between Keycloak and OpenFGA

原始链接: https://github.com/embesozzi/keycloak-openfga-event-publisher

## Keycloak & OpenFGA 集成:事件发布者摘要 此 Keycloak 扩展将 Keycloak 的身份验证与 OpenFGA 的细粒度授权(ReBAC)连接起来。它充当事件监听器提供程序,监控 Keycloak 事件,例如用户/角色分配和组 membership。 在检测到相关事件时,扩展会使用 OpenFGA Java SDK 将其转换为 OpenFGA Tuple key,并通过 HTTP 发布到 OpenFGA 服务器。这种同步允许应用程序利用 OpenFGA 作为策略决策点 (PDP) 来强制执行细粒度的访问控制。 该架构通过让 Keycloak 管理身份验证和基于角色的基本访问,同时让 OpenFGA 处理复杂的基于关系的权限,从而简化了授权。扩展通过环境变量配置 OpenFGA API URL、Store ID 和 Authorization Model ID。 安装涉及将扩展的 JAR 文件复制到 Keycloak 的 providers 目录并重建 Keycloak 实例。它主要在基于 Quarkus 的 Keycloak 发行版上进行测试。

一个新的开源项目“Event Publisher”(github.com/embesozzi)旨在简化Keycloak(身份提供者)与OpenFGA(授权服务器)的集成。它解决的核心问题是同步这两个系统之间的授权相关数据——特别是将Keycloak中的身份信息同步到OpenFGA。 发布者强调这是一个有价值的工具,但也承认它不是一个完整的解决方案。虽然它可以处理身份同步,但保持应用程序/领域数据与授权服务器同步仍然是一个挑战,通常需要使用OpenFGA的SDK。 作者澄清他们尚未亲自使用该软件,并指出潜在的维护问题,同时也披露了与Keycloak和OpenFGA领域内竞争产品的专业关系。
相关文章

原文

This is a Keycloak extension that implements an Event Listener Provider to detect Identity events and publish them to the OpenFGA server over HTTP, thanks to the OpenFGA Java SDK.
This extension allows for direct integration between Keycloak and OpenFGA. OpenFGA is an open source solution for Fine-Grained Authorization that applies the concept of ReBAC (created by the Auth0 inspired by Zanzibar). The extension follows these steps:

  1. Listens to the following Keycloak events based on his own Identity, Role and Group model (e.g., User Role Assignment, Role to Role Assignment, etc)

  2. Converts these event into an OpenFGA Tuple key based on the OpenFGA Authorization Schema:

  1. Publishes the Tuple keys to the OpenFGA solution using the OpenFGA Java SDK. Apps and APIs can then use OpenFGA as a PDP (Policy Decision Endpoint) to enforce the authorization policies.

Solution Architecture Overview (New)

This extension improves the Authorization Architecture described in the article Keycloak integration with OpenFGA (based on Zanzibar) for Fine-Grained Authorization at Scale (ReBAC) by enabling direct event synchronization between the Access Manager Platform and the OpenFGA Server.

A brief introduction of the new simplified Authorization Architecture is as follows:

  • Core:
    • Keycloak is responsible for handling the authentication with the standard OpenID Connect and manages user access with its Role Model.
    • Keycloak is configured with a new custom extension 🚀 keycloak-openfga-event-publisher which listens to the Keycloak events (User Role Assignment, Role to Role Assignment, etc), parses this event into an OpenFGA tuple based on the Keycloak Authz Schema and publishes them to OpenFGA over HTTP.
    • OpenFGA is responsible for applying fine-grained access control. The OpenFGA service answers authorization checks by determining whether a relationship exists between an object and a user.
  • Other components
    • Store Web Application is integrated with Keycloak by OpenID Connect
    • Store API is protected by OAuth 2.0 and it utilizes the OpenFGA SDK for FGA

The main purpose of this SPI is to listen to the Keycloak events and publish these events to an OpenFGA solution.

Here is a high level overview of the extension:

In this case, the extension listens to the Admin Events related to operation in Keycloak Identity, Role and Group model. So far, the extension proceeds with the following steps:

  1. Parses and enriches the default Keycloak events in the following cases:
Keycloak Event (Friendly Name) Description
User Role Assignment User is assigned to a Keycloak Role
Role To Role Assignment Role is assigned to a parent Keycloak Role
Group To Role Assignment Group is assigned to a Keycloak Role
User Group Membership User is assigned to a Group
  1. Transforms the Keycloak event into an OpenFGA ClientWriteRequest object, thanks to the OpenFGA Java SDK.
Keycloak Event (Friendly Name) OpenFGA (Tuple Key)
User Role Assignment User related to the object Role as assignee
Role To Role Assignment Role related to the object Role as parent
Group To Role Assignment Group related to the object Role as parent group
User Group Membership User related to a Group as assignee

These are all the OpenFGA events handled by the provided keycloak-openfga-authorization-model. You can edit the authorization model to handle the desired events.

  1. Publishes the event to OpenFGA solution

Publishes the ClientWriteRequest object to the OpenFGA server over an HTTP request fgaClient.write(request) with the OpenFGA SDK client.

Download a release (*.jar file) that works with your Keycloak version from the list of releases. Or you can build with bash mvn clean package

Follow the below instructions depending on your distribution and runtime environment.

Quarkus-based distro (Keycloak.X)

Copy the jar to the providers folder and execute the following command:

${kc.home.dir}/bin/kc.sh build

For Docker-based setups mount or copy the jar to

  • /opt/keycloak/providers for Keycloak.X from version 15.1.0

Warning:

With the release of Keycloak 17 the Quarkus-based distribution is now fully supported by the Keycloak team. Therefore, I have not tested this extension in Wildfly-based distro ❗ ️

The following properties can be set via environment variables following the Keycloak specs, thus each variable MUST use the prefix KC_SPI_EVENTS_LISTENER_OPENFGA_EVENTS_PUBLISHER.

  • KC_SPI_EVENTS_LISTENER_OPENFGA_EVENTS_PUBLISHER_API_URL: The openfgaApiUrl is the URI of the OpenFGA Server. If this variable is empty, the extension will use the default value http://openfga:8080 for demo purposes only.

  • Optional KC_SPI_EVENTS_LISTENER_OPENFGA_EVENTS_PUBLISHER__STORE_ID and KC_SPI_EVENTS_LISTENER_OPENFGA_EVENTS_PUBLISHER_AUTHORIZATION_MODEL_ID : The openfgaStoreId and the openfgaAuthorizationModelId are the store and authorization model identifiers in the OpenFGA server. If not provided, the extension will attempt to discovery them.

You may want to check docker-compose.yml as an example.

Enable OpenFGA Event Publisher extension in Keycloak

Enable the Keycloak OpenFGA Event Listener extension in Keycloak:

  • Open administration console
  • Choose realm
  • Realm settings
  • Select Events tab and add openfga-events-publisher to Event Listeners.

The test cases are available in the workshop:

联系我们 contact @ memedata.com