|
| 1 | +--- |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +title: "OpenShift Identity Federation" |
| 5 | +sidebar-title: "Identity Federation" |
| 6 | +slug: "kubernetes/openshift/identity-federation" |
| 7 | +description: "Configure Keycloak to delegate authentication to the OpenShift OAuth server so cluster users can log in with their existing credentials." |
| 8 | +keywords: "Generative AI, Cybersecurity, Kubernetes, OpenShift, Keycloak, Identity Federation, OAuth, SSO" |
| 9 | +position: 4 |
| 10 | +--- |
| 11 | + |
| 12 | +Keycloak can delegate authentication to the OpenShift OAuth server so users log in with their cluster credentials instead of separate Keycloak accounts. Users are auto-imported into the Keycloak realm on first login. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- [OIDC with Keycloak](/kubernetes/openshift/oidc-keycloak) configured and working |
| 17 | +- `cluster-admin` access to create `OAuthClient` resources |
| 18 | + |
| 19 | +## Create an OAuthClient in OpenShift |
| 20 | + |
| 21 | +Generate a client secret and create the `OAuthClient` that Keycloak uses to initiate the OAuth flow: |
| 22 | + |
| 23 | +```shell |
| 24 | +OCP_SECRET=$(openssl rand -hex 32) |
| 25 | +KC_URL=https://keycloak.example.com |
| 26 | + |
| 27 | +cat <<EOF | oc apply -f - |
| 28 | +apiVersion: oauth.openshift.io/v1 |
| 29 | +kind: OAuthClient |
| 30 | +metadata: |
| 31 | + name: keycloak-openshell |
| 32 | +secret: "${OCP_SECRET}" |
| 33 | +grantMethod: auto |
| 34 | +redirectURIs: |
| 35 | + - "${KC_URL}/realms/openshell/broker/openshift/endpoint" |
| 36 | +EOF |
| 37 | +``` |
| 38 | + |
| 39 | +## Add OpenShift as a Keycloak identity provider |
| 40 | + |
| 41 | +<Tabs> |
| 42 | +<Tab title="kcadm.sh"> |
| 43 | + |
| 44 | +```shell |
| 45 | +KC_POD="oc exec keycloak-0 -n keycloak --" |
| 46 | +KC_ADM="/opt/keycloak/bin/kcadm.sh" |
| 47 | + |
| 48 | +$KC_POD $KC_ADM create identity-provider/instances -r openshell \ |
| 49 | + -s alias=openshift \ |
| 50 | + -s displayName=OpenShift \ |
| 51 | + -s providerId=openshift-v4 \ |
| 52 | + -s enabled=true \ |
| 53 | + -s trustEmail=true \ |
| 54 | + -s "firstBrokerLoginFlowAlias=first broker login" \ |
| 55 | + -s "config.clientId=keycloak-openshell" \ |
| 56 | + -s "config.clientSecret=${OCP_SECRET}" \ |
| 57 | + -s "config.baseUrl=$(oc whoami --show-server)" \ |
| 58 | + -s "config.defaultScope=user:info" \ |
| 59 | + -s "config.syncMode=IMPORT" |
| 60 | +``` |
| 61 | + |
| 62 | +</Tab> |
| 63 | +<Tab title="REST API"> |
| 64 | + |
| 65 | +```shell |
| 66 | +TOKEN=$(curl -sk "${KC_URL}/realms/master/protocol/openid-connect/token" \ |
| 67 | + -d "client_id=admin-cli" \ |
| 68 | + -d "username=<admin-user>" \ |
| 69 | + -d "password=<admin-password>" \ |
| 70 | + -d "grant_type=password" | jq -r .access_token) |
| 71 | + |
| 72 | +curl -sk -X POST "${KC_URL}/admin/realms/openshell/identity-provider/instances" \ |
| 73 | + -H "Authorization: Bearer $TOKEN" \ |
| 74 | + -H "Content-Type: application/json" \ |
| 75 | + -d "{ |
| 76 | + \"alias\": \"openshift\", |
| 77 | + \"displayName\": \"OpenShift\", |
| 78 | + \"providerId\": \"openshift-v4\", |
| 79 | + \"enabled\": true, |
| 80 | + \"trustEmail\": true, |
| 81 | + \"firstBrokerLoginFlowAlias\": \"first broker login\", |
| 82 | + \"config\": { |
| 83 | + \"clientId\": \"keycloak-openshell\", |
| 84 | + \"clientSecret\": \"${OCP_SECRET}\", |
| 85 | + \"baseUrl\": \"$(oc whoami --show-server)\", |
| 86 | + \"defaultScope\": \"user:info\", |
| 87 | + \"syncMode\": \"IMPORT\" |
| 88 | + } |
| 89 | + }" |
| 90 | +``` |
| 91 | + |
| 92 | +</Tab> |
| 93 | +</Tabs> |
| 94 | + |
| 95 | +<Note> |
| 96 | +**ROSA HCP**: set `baseUrl` to the API server URL (`https://api.<cluster>:443`), not the OAuth route (`oauth.<cluster>`). On ROSA HCP, the OAuth metadata endpoint (`/.well-known/oauth-authorization-server`) is served by the API server. The OAuth route returns 404. |
| 97 | +</Note> |
| 98 | + |
| 99 | +## Assign roles to federated users |
| 100 | + |
| 101 | +Users imported from OpenShift do not receive the `openshell-user` or `openshell-admin` realm roles automatically. After a user's first login through the "OpenShift" button, assign the appropriate role through the Keycloak admin console: |
| 102 | + |
| 103 | +**Keycloak Admin Console → Users → select user → Role Mappings → Assign role → openshell-user** |
| 104 | + |
| 105 | +Or via the CLI/API: |
| 106 | + |
| 107 | +<Tabs> |
| 108 | +<Tab title="kcadm.sh"> |
| 109 | + |
| 110 | +```shell |
| 111 | +$KC_POD $KC_ADM add-roles -r openshell \ |
| 112 | + --uusername <openshift-username> \ |
| 113 | + --rolename openshell-user |
| 114 | +``` |
| 115 | + |
| 116 | +</Tab> |
| 117 | +<Tab title="REST API"> |
| 118 | + |
| 119 | +```shell |
| 120 | +USER_ID=$(curl -sk "${KC_URL}/admin/realms/openshell/users?username=<openshift-username>&exact=true" \ |
| 121 | + -H "Authorization: Bearer $TOKEN" | jq -r '.[0].id') |
| 122 | + |
| 123 | +ROLE_ID=$(curl -sk "${KC_URL}/admin/realms/openshell/roles/openshell-user" \ |
| 124 | + -H "Authorization: Bearer $TOKEN" | jq -r '.id') |
| 125 | + |
| 126 | +curl -sk -X POST "${KC_URL}/admin/realms/openshell/users/${USER_ID}/role-mappings/realm" \ |
| 127 | + -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \ |
| 128 | + -d "[{\"id\":\"${ROLE_ID}\",\"name\":\"openshell-user\"}]" |
| 129 | +``` |
| 130 | + |
| 131 | +</Tab> |
| 132 | +</Tabs> |
| 133 | + |
| 134 | +## Result |
| 135 | + |
| 136 | +The Keycloak login page shows a "Login with OpenShift" button below the standard username/password form. Clicking it redirects to the OpenShift login page. After authentication, Keycloak issues an OIDC token with the user's identity and assigned roles, and the CLI stores the token. |
0 commit comments