Configuring CEL expressions for username and groups claim mapping
You can use Common Expression Language (CEL) expressions to construct usernames and groups from JWT token claims. This provides flexible claim mapping, including fallback logic when specific claims are not present.
-
You have configured an external OIDC identity provider for direct authentication.
-
You have access to the cluster as a user with the
cluster-adminrole. -
You have access to a long-lived authentication method, such as a certificate-based kubeconfig file.
-
You are familiar with CEL expression syntax.
-
Create a YAML file named
authentication-cel-mapping.yamlwith your CEL expression configuration:When using
expression, do not set theclaimfield. You must use eitherclaimorexpression, but not both. Setting both will result in a validation error. Additionally, when usingexpression, do not setprefixPolicytoPrefix. Prefix policies are only compatible withclaim-based mappings.When using the
emailclaim in CEL expressions, you must also validateemail_verifiedto ensure the email address has been verified by the identity provider.apiVersion: config.openshift.io/v1 kind: Authentication metadata: name: cluster spec: type: OIDC oidcProviders: - name: my-oidc-provider issuer: issuerURL: https://idp.example.com audiences: - my-audience claimMappings: username: expression: 'claims.?upn.orValue(claims.?oid.orValue(claims.sub))' groups: expression: 'claims.?groups.orValue([])' # ...where:
username.expression-
Specifies the fallback logic for username:
upnif present, elseoid, elsesub. groups.expression-
Specifies that the
groupsclaim is used if present, else an empty array.Replace the placeholder values (
my-oidc-provider,https://idp.example.com,my-audience) with your actual OIDC provider configuration.
-
Apply the configuration:
$ oc apply -f authentication-cel-mapping.yaml
-
Verify that the authentication configuration is applied successfully:
$ oc get authentication.config.openshift.io/cluster -o yaml -
Authenticate with a user account and verify the username is constructed correctly:
$ oc whoami -
Monitor the cluster authentication Operator status:
$ oc get clusteroperator authenticationThe Operator should report
Available=TrueandDegraded=False.
|
|
CEL expressions have access to standard CEL string functions ( |
You can use the following CEL expression patterns for claim mapping:
- Use the optional chaining Operator
?to safely access claims that might not exist -
username: expression: 'claims.email_verified ? claims.email : claims.sub'Uses
emailif verified, otherwisesub. When using theemailclaim, you must also checkemail_verified. - Concatenate multiple claims
-
username: expression: 'claims.givenname + "." + claims.surname'Combines given name and surname claims.
- Transform claim values
-
username: expression: 'claims.email.lowerAscii()'Converts email to lowercase.
- Conditional logic for different user types
-
username: expression: 'has(claims.upn) ? claims.upn : claims.oid'Uses
upnfor regular users,oidfor service principals. - Extract domain from email
-
groups: expression: 'claims.?email.orValue("").split("@").size() > 1 ? [claims.email.split("@")[1]] : []'Safely extracts domain from email address for group assignment, returning an empty array if email is missing or malformed.
- Combine group sources
-
groups: expression: 'claims.?groups.orValue([]) + claims.?roles.orValue([])'Combines
groupsandrolesclaims.