Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

KeycloakRoleMapping

The KeycloakRoleMapping resource assigns Keycloak roles to users, groups, or client service accounts.

Overview

This CRD provides a declarative way to:

  • Assign realm roles to users
  • Assign client roles to users
  • Assign realm roles to groups
  • Assign client roles to groups
  • Assign roles to a client’s service account (via serviceAccountRef)

Examples

Realm Role to User

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: admin-role-mapping
spec:
  subject:
    userRef:
      name: admin-user
  roleRef:
    name: admin-role

Client Role to User (using roleRef)

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: client-admin-mapping
spec:
  subject:
    userRef:
      name: service-user
  roleRef:
    name: manage-clients

Inline Client Role to User

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: inline-client-role-mapping
spec:
  subject:
    userRef:
      name: service-user
  role:
    name: manage-clients
    clientRef:
      name: my-client

Inline Role Reference

Instead of referencing a KeycloakRole resource, you can specify the role name directly:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: builtin-role-mapping
spec:
  subject:
    userRef:
      name: my-user
  role:
    name: offline_access

Role to Group

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: group-role-mapping
spec:
  subject:
    groupRef:
      name: developers
  roleRef:
    name: developer-role

Role to a Client’s Service Account

serviceAccountRef assigns roles to the service account user that Keycloak auto-creates for clients with serviceAccountsEnabled: true, without an intermediate KeycloakUser resource:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: my-app-sa-manage-users
spec:
  subject:
    serviceAccountRef:
      name: my-app  # KeycloakClient
  role:
    name: manage-users
    clientRef:
      name: realm-management

Spec

FieldTypeDescriptionRequired
subject.userRefResourceRefReference to KeycloakUserExactly one subject ref
subject.groupRefResourceRefReference to KeycloakGroupExactly one subject ref
subject.serviceAccountRefResourceRefReference to a KeycloakClient whose service account is the subjectExactly one subject ref
roleRefResourceRefReference to KeycloakRole resourceEither roleRef or role
role.namestringKeycloak role name (inline)Either roleRef or role
role.clientRefResourceRefReference to KeycloakClient for client roles (within inline role)No (realm role if omitted)
role.clientIdstringClient ID for client roles (alternative to clientRef)No

Status

FieldTypeDescription
readybooleanWhether the mapping is synced
statusstringCurrent status (Synced, Error, SubjectError, RoleError)
messagestringAdditional status information
resourcePathstringKeycloak API path for this role mapping
subjectTypestringSubject type (“user” or “group”)
subjectIDstringKeycloak ID of the user/group
roleNamestringResolved role name
roleTypestringRole type (“realm” or “client”)
instanceobjectResolved instance reference
realmobjectResolved realm reference
observedGenerationintegerLast observed generation
conditions[]ConditionKubernetes conditions

Behavior

Role Resolution

Using roleRef:

  1. The operator looks up the referenced KeycloakRole resource
  2. It reads status.roleName from that resource (which may differ from the CR name)
  3. If the referenced KeycloakRole has its own spec.clientRef, the mapping is automatically scoped to that client and the client’s UUID is resolved from the KeycloakClient’s status
  4. The referenced role (and the client it points at, if any) must be Ready; otherwise the mapping requeues
  5. This is the recommended approach for roles managed by the operator

Using role.name:

  1. The operator queries Keycloak for a role with the given name
  2. This is useful for built-in roles like offline_access

Mapping Types

SubjectRole sourceResult
userRefinline role without clientRef/clientIdUser realm role mapping
userRefinline role with clientRef or clientIdUser client role mapping
userRefroleRef to a KeycloakRole without clientRefUser realm role mapping
userRefroleRef to a KeycloakRole with clientRefUser client role mapping
groupRefinline role without clientRef/clientIdGroup realm role mapping
groupRefinline role with clientRef or clientIdGroup client role mapping
groupRefroleRef to a KeycloakRole without clientRefGroup realm role mapping
groupRefroleRef to a KeycloakRole with clientRefGroup client role mapping
serviceAccountRefany role sourceRole mapping on the client’s service account user

Cleanup

When the KeycloakRoleMapping is deleted:

  1. The finalizer removes the role mapping from Keycloak
  2. The user/group no longer has the role assigned

Use Cases

RBAC Setup

Set up role-based access control with groups:

# Create a group
apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakGroup
metadata:
  name: admins
spec:
  realmRef:
    name: my-realm
  name: admins
  definition: {}
---
# Create a role
apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRole
metadata:
  name: admin-role
spec:
  realmRef:
    name: my-realm
  name: admin
  definition:
    description: Full admin access
---
# Map role to group
apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: admins-admin-role
spec:
  subject:
    groupRef:
      name: admins
  roleRef:
    name: admin-role

Service Account Roles

Assign specific client roles to service accounts:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: service-manage-users
spec:
  subject:
    userRef:
      name: service-account
  role:
    name: manage-users
    clientRef:
      name: realm-management

Multiple Role Assignments

Assign multiple roles to the same user:

apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: user-role-1
spec:
  subject:
    userRef:
      name: my-user
  roleRef:
    name: role-1
---
apiVersion: keycloak.hostzero.com/v1beta1
kind: KeycloakRoleMapping
metadata:
  name: user-role-2
spec:
  subject:
    userRef:
      name: my-user
  roleRef:
    name: role-2

Notes

  • Exactly one of userRef, groupRef, or serviceAccountRef can be specified
  • Only one of roleRef or role can be specified
  • When using role.clientRef, the role must be a client role, not a realm role
  • Built-in Keycloak roles (like offline_access, uma_authorization) should use inline role.name
  • Do not combine KeycloakRoleMapping with the authoritative spec.realmRoles/spec.clientRoles fields of a KeycloakUser targeting the same user — the user’s reconciler would remove the mappings again