GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - embesozzi/keycloak-cedar-authorization: A Keycloak extension that adds Cedar policy support to Keycloak Authorization Services, enabling AuthZEN-based authorization for users and AI agents.

BranchesTags
Open more actions menu

Latest commit

 

History

5 Commits
5 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
src
src
 
 
tests
tests
 
 
tools/cedar-cli
tools/cedar-cli
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

Repository files navigation

Keycloak Cedar Authorization

A Keycloak extension that integrates Cedar as a policy type in Keycloak Authorization Services, enabling Keycloak's AuthZEN endpoint to evaluate authorization requests for users and AI agents using Cedar policies.

For the background and design, see Keycloak Cedar Policy Engine, a research article by TwoGenIdentity, along with the reference article Cedar Policy Engine in Keycloak for FGA for Users & AI Agents.

Overview

Keycloak exposes an AuthZEN endpoint: a PEP, such as an API gateway or an MCP server, asks "may this subject do this action on this resource?" and Keycloak answers with a decision. Keycloak takes that decision with the policies of its Authorization Services: role, group, user, time or JavaScript policies, combined in permissions.

These policies work well for simple rules, but fine grained rules (on resource attributes, on how the user logged in, on which agent is calling) end up in JavaScript policies that are hard to review and test.

This extension adds Cedar, a policy language designed for authorization, as a new policy type. Keycloak Authorization Services then acts as a Policy Decision Point (PDP) compatible with AuthZEN, powered by Cedar. Cedar runs inside Keycloak through cedar-java; no external PDP is required.

Features

  • Cedar policy type: a Cedar policy is used by Keycloak permissions like any other policy.
  • Your own model: each resource server describes its resource types, actions and context in a Cedar schema, so policies use your names (Documents::Document, Mcp::Tool).
  • Validation on write: a policy that does not match the schema is rejected by the Admin API, with Cedar's error, before it can affect any decision.
  • One policy set per resource server: all its Cedar policies decide together, so a forbid in any of them overrides every permit.
  • Principal from Keycloak: the subject's roles, groups and attributes are read from Keycloak, never taken from the request.
  • Typed request context: facts only the PEP knows, such as acr, amr or token scopes, are passed in the AuthZEN context and checked by Cedar with their declared types.
  • Users and AI agents: both are Cedar principals (Keycloak::User, Keycloak::Client).
  • Fail closed: any error results in a deny, never in an allow.

How It Works

sequenceDiagram
    participant PEP as PEP <br/> (API Gateway, Agent Gateway <br/> or MCP Server)
    participant KC as Keycloak <br/> (AuthZEN endpoint)
    participant AS as Authorization Services
    participant CP as Cedar policy provider

    PEP->>KC: POST /realms/{realm}/authzen/access/v1/evaluation
    KC->>AS: Evaluate resource + scope for the subject
    AS->>CP: Evaluate the permission's Cedar policy
    CP->>CP: Build the Cedar request from Keycloak's data and the schema
    CP-->>AS: grant / deny
    KC-->>PEP: { "decision": true | false }
Loading
  • The PEP is the component that enforces the decision: an API gateway, an agent gateway or an MCP server.
  • The resource server is the client of the PEP's access token (its azp). Its schema and policies decide.
  • Each part of the AuthZEN request becomes part of the Cedar request:
AuthZEN Keycloak Cedar
subject User, or the service account of a client Keycloak::User / Keycloak::Client
resource Resource (type and name) Documents::Document::"document-1"
action Authorization scope Documents::Action::"read"
context Evaluation context context, declared keys only

Configuration

In Keycloak

Keycloak's AuthZEN endpoint evaluates a request only when these exist in the resource server:

  1. A resource with the requested name and type (document-1, type Document).
  2. A scope with the requested action name (read).
  3. A scope permission that links them to a Cedar policy.

Without them, the decision is false before any Cedar policy runs. See tests/bootstrap.sh.

Resource Server Schema

Each resource server describes its own resources in a Cedar schema, stored in the client attribute cedar.schema. Names in the schema must match names in Keycloak:

In Keycloak In the schema In a policy
Resource type Document entity Document resource is Documents::Document
Resource name document-1 resource == Documents::Document::"document-1"
Scope read action read action == Documents::Action::"read"
Resource attributes entity attributes, or tags resource.getTag("classification")
AuthZEN context keys the action's context context.acr
namespace Documents {
  entity Document = { name: String, type: String } tags Set<String>;

  action read, write appliesTo {
    principal: [Keycloak::User, Keycloak::Client],
    resource: [Document],
    context: { acr?: String, amr?: Set<String> },
  };
}
  • The schema declares exactly one namespace, any name except Keycloak.
  • Users and clients are always Keycloak::User and Keycloak::Client, defined by the provider in keycloak.cedarschema.
  • A resource type or scope missing from the schema is denied.

Cedar Policy

Create a policy of type cedar in the resource server's Authorization Services and reference it from a permission:

POST /admin/realms/{realm}/clients/{client-uuid}/authz/resource-server/policy/cedar
{ "name": "documents", "code": "@id(\"document-readers-read\") permit (...);" }
Field Description
name Keycloak policy name; decision logs refer to statements as <name>#<@id>
code Cedar policy statements; templates are not supported
  • One resource goes in the policy scope (resource == Mcp::Tool::"delete_customer"); a list goes in the condition (resource in [...]). Cedar does not accept a list in the scope.
  • To group resources without listing them, give them an attribute such as category and test it as a tag: resource.getTag("category").contains("customer").

Provider Options

Option Default Description
--spi-policy--cedar--user-attributes (none) User attributes exposed as principal tags, separated by commas
CEDAR_JAVA_FFI_LIB (environment) Path to the cedar-java native library

Installation

Requirement Version
Keycloak 26.7.0
cedar-java (bundled) 4.10.0
Java 17
Platform Linux (x86_64, aarch64)
  1. Download from the latest release the provider jar and the native library for your platform (libcedar_java_ffi-linux-x86_64.so or libcedar_java_ffi-linux-aarch64.so), and verify them with sha256sum -c SHA256SUMS --ignore-missing.
  2. Copy keycloak-cedar-authorization-<version>.jar to /opt/keycloak/providers/.
  3. Copy the native library into the image and set CEDAR_JAVA_FFI_LIB to its path.
  4. Enable the AuthZEN feature: --features=authzen.

To build from source instead, run mvn package: the jar is target/keycloak-cedar-authorization.jar and the native libraries are in target/native/jne/linux/<arch>/.

For a working setup, see the demo in tests/README.md: its Docker Compose file shows the provider jar, the native library and the features in place.

Demo and Use Cases

The repository includes a demo: Keycloak with the provider, a realm with users and an AI agent, and four use cases (roles, authentication context, AI agent identity, MCP tools), each with its policy, AuthZEN request and expected decisions.

See tests/README.md to run it.

Policies as Code

Keep each resource server's schema and policies in Git, one folder per resource server. The demo in tests/policies/ uses this layout:

tests/policies/
  documents-api/                     # folder = resource server clientId
    schema.cedarschema               # stored in the client attribute cedar.schema
    documents.cedar                  # stored as the Keycloak Cedar policy "documents"
    planner-agent-read.cedar         # stored as the Keycloak Cedar policy "planner-agent-read"
    document-write-passkey.cedar     # stored as the Keycloak Cedar policy "document-write-passkey"
  mcp-server/
    schema.cedarschema
    tools.cedar                      # stored as the Keycloak Cedar policy "tools"

Each .cedar file becomes one Keycloak policy of type cedar, named after the file, so a decision in the logs (documents#document-readers-read) points to its file and statement.

Check them with the official Cedar CLI, in a container:

make policies-check

Testing

make test            # unit tests
make policies-check  # Cedar CLI format check and strict validation
make test-e2e        # start the demo and run the AuthZEN checks (make down to stop it)

Considerations

Caution

Experimental Version: This software is currently experimental and is provided AS IS without any warranties.

  • Not recommended for production use
  • Issues and feedback should be reported via the GitHub issue tracker
  • Maintenance and response times are not guaranteed

By using this experimental software, you acknowledge and accept these conditions.

  • Callers: the AuthZEN endpoint accepts any valid access token of the realm. Only authorized clients should be able to obtain tokens for resource servers with Cedar policies.
  • Context: context values are trusted as sent by the PEP. The PEP must ensure they cannot be manipulated by the user or agent.
  • AI agents: send an agent as the sub of its own token, with subject type user. Keycloak accepts a subject of type client only for the caller itself. See tests/README.md.

References

  • Keycloak Cedar Policy Engine (TwoGenIdentity research)
  • Cedar Policy Language
  • cedar-java
  • AuthZEN Authorization API
  • Keycloak Authorization Services

Commercial Support

For professional services, architecture consulting, implementation, and commercial support around Keycloak and Cedar authorization, visit TwoGenIdentity.

License

Licensed under the Apache License, Version 2.0.

Originally designed and implemented by Martin Besozzi, as part of the TwoGenIdentity research on authorization for AI agents.

About

A Keycloak extension that adds Cedar policy support to Keycloak Authorization Services, enabling AuthZEN-based authorization for users and AI agents.

embesozzi.github.io/posts/martin-besozzi/keycloak-cedar-authorization-authzen/

Topics

ai-agentsauthorizationauthzenawscedarfine-grained-authorizationkeycloak-spimcppolicy-as-codezero-trust

Resources

Readme
Apache-2.0 license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.