Migrating from the Keycloak membership mode
Up to 0.6 the operator filled Keycloak groups with the members each
ManagedRancherProject listed, and Rancher read those groups at login.
From 0.7 the operator manages Rancher bindings only. Access comes from the
user's token: a Keycloak protocol mapper asks Waldur which roles the
person holds and writes the matching group names into a roles claim.
The group names do not change for project bindings, so the switch itself does not move anyone's access. What does need care:
- The groups the old operator filled keep granting access. Rancher
also reads the
groupsclaim, so a member of a Keycloak group named like a Waldur group keeps that group's access whatever Waldur says. The new operator does not touch Keycloak, so these groups must be emptied or deleted by hand (step 6). - The old status is cleared. The new CRD drops the Keycloak fields
from
status, and the new operator clears them on its first reconcile. Record them first (step 1); they are the list of groups to clean up. MEMBERSHIP_MODE=keycloakstops the new operator at startup, with a message pointing here. Remove it, and theKEYCLOAK_*settings, when deploying.
Do the steps in order. Steps 1 to 3 change nothing for users.
1. Record each CR's Keycloak groups
Before anything else is upgraded:
1 2 3 4 5 6 7 8 9 10 11 | |
Keep both files until step 6 is done. With the site agent's default
templates the names look like c_<cluster>_<rp-uuid>_<role> (project),
c_<cluster>_cluster_<role> (cluster) and c_<cluster> (parent). The old
operator also tagged every group it created with the attribute
managed_by=rancher-keycloak-operator, which finds groups of CRs that
were deleted earlier or never reported a status:
1 2 3 4 | |
Also note which Rancher projects the CRs track
(jq -r '.items[] | [.metadata.name, .status.rancherProjectId] | @tsv' mrp-before.json).
The new operator writes the CR's resourceProjectUuid into each tracked
project's waldur/resource_project_uuid annotation on its first
reconcile, and from then on deletes the project with the CR. The old
operator adopted existing projects by name; if any tracked project is not
really Waldur's, take it out of the CR's status before step 5.
2. Set up token-based access
Configure Keycloak, the Waldur roles mapper and Rancher as described in
keycloak-setup.md: offline_access, the realm default
roles for Rancher's refresh (needed while Rancher uses the keycloakoidc
provider), the mapper with the same group-name templates as the site
agent, and rancherApiHost. Keep the provider Rancher uses today for this
migration; moving to the plain oidc provider is a separate step
(below). Then check that a test user's
token carries their Waldur group names in roles
(verify the claim).
From here on the same names arrive twice, through groups and roles;
access does not change.
3. Apply the role templates
On the Rancher management cluster:
1 2 | |
They must exist before any CR references them, otherwise the binding fails
with UnknownRoleTemplate (and the old binding is kept).
4. Switch the site agent
Stop the old operator first, so it does not act on the CRs the new site agent writes (they carry no members, and the old operator would empty or fail on the groups):
1 | |
Then upgrade the rancher-kc-crd site agent to the version without member
sync and set its role mapping:
project_admin→waldur-project-adminproject_member→project-member- the cluster admin role →
waldur-cluster-admin
CR changes queue up in the CRs until the new operator starts.
5. Deploy the new operator
1 2 3 4 | |
Do not use --reuse-values: it carries the old config.keycloak.* values
along (the chart ignores them, but they stay in the release). With
existingSecret, remove KEYCLOAK_* and MEMBERSHIP_MODE from that
secret; the operator ignores KEYCLOAK_* with a warning but refuses to
start with MEMBERSHIP_MODE=keycloak.
The upgrade replaces the CRD schema in place (still v1alpha1). Check:
1 2 3 4 | |
Project bindings whose role changed (project-owner →
waldur-project-admin) are replaced: the new PRTB is created first, the
old one deleted once the new one exists. Cluster bindings whose group name
changed with the site agent's new cluster template are replaced the same
way.
Remove bindings the old operator left untracked
The Keycloak-mode operator could leave bindings in Rancher that no CR
status refers to, for example a second cluster-owner CRTB for the same
group principal after a status was lost. The new operator only deletes
bindings it tracks, so these stay and keep granting access. A binding is
tracked when its id (<namespace>:<name>, as in
status.keycloakRoleBindings[].rancherBindingId and
status.clusterKeycloakRoleBindings[].rancherClusterBindingId) appears in
some CR's status; an operator-named group's binding whose id appears in no
status is an untracked leftover.
Once every CR is Ready, on the Rancher management cluster:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Each line is an untracked binding of an operator-managed group. Check it is not one a person made on purpose, then delete it:
1 | |
6. Empty or delete the old groups in Keycloak
In the Keycloak admin console, realm → Groups, for every group in
operator-groups.tsv (search by name, or open it by the id in the last
column):
- Members tab → select all → Leave, or
- the group's menu → Delete.
Deleting is cleaner; emptying keeps the group for audit. Then delete the
parent groups from operator-parent-groups.tsv once they have no
subgroups left (deleting a parent deletes its subgroups with it, so check
that only operator groups were under it).
Do not touch groups people maintain by hand. Leave users the old operator
created (createMissingUsers) alone; they are ordinary users now. The
operator's own Keycloak admin user (rancher-operator by default) is no
longer used: disable or delete it and drop its credential.
7. Verify with a test user
- A test user with a resource-project role in Waldur logs in to Rancher
and sees the project; their token's
roleslists the group,groupslists none of the operator's groups. - Remove the role in Waldur. Within
auth-user-info-max-age-secondsthe user loses the project, in the UI and through a kubeconfig token. - A user who was only in an old operator group, without a Waldur role, has no access.
- As
waldur-project-admin, adding a project member and creating a RoleBinding are refused.
Rolling back
Before step 6 a rollback loses nothing:
- Scale the new operator to 0.
- Roll the site agent back to the version with member sync, so CRs carry members again.
helm rollback(or upgrade to 0.6.x) with the oldconfig.keycloak.*values. The old CRD schema comes back with the chart.- The old operator finds its groups by name, recreates the status fields
and syncs the members again.
status.memberHistorystarts empty: withKEYCLOAK_EXTERNAL_MEMBER_PATTERNset, members added by hand are treated as new for one grace period. - The old operator never removes a project binding whose role changed,
so
waldur-project-adminPRTBs stay next to the restoredproject-ownerones: delete them in Rancher. Cluster bindings tracked in status are replaced by its own clean-up.
After step 6, the same works, but the groups are recreated empty and refilled by the first reconcile of each CR; members have no access through Keycloak groups until then.
Switching the Rancher auth provider from keycloakoidc to oidc
Optional, and independent of the steps above. The plain oidc provider
refreshes groups from the token and userinfo, so the realm no longer needs
the default roles that let every user read the realm's users and groups.
Two things about Rancher 2.14 shape the order (verified on 2.14.3):
- Enabling
oidcdoes not disablekeycloakoidc. Both stay enabled, and both appear on the login page, untilkeycloakoidcis disabled explicitly. - Disabling a provider deletes its users, with their tokens, and every PRTB and CRTB on its principals. Whatever must survive has to exist for the new provider's principals first.
Steps:
- Keycloak. The Rancher client stays as it is (same redirect URI).
- Enable
oidc. As an admin, configure and enable theoidcprovider with the values in keycloak-setup.md. The admin who enables it logs in through it, and Rancher links that admin's user to the new principal; check that the admin can log in throughoidc. - Switch the operator's prefix:
1 2 3 4 5 6 | |
Every CR gets bindings for oidc_group://<name>; the tracked
keycloakoidc_group:// bindings are deleted once the new ones exist.
From here on, Waldur-granted access works only for people who log in
through oidc: tell users to pick it on the login page.
4. Recreate hand-made bindings for the new principals. Everything still
on a keycloakoidc principal now was made by hand, and goes when the
provider is disabled:
1 2 3 4 5 | |
Recreate each one still wanted for its oidc_group://<name> principal,
or for the person's oidc_user://<sub> principal, which exists once
they have logged in through oidc.
5. Disable keycloakoidc. Rancher deletes its users, their kubeconfig
and API tokens, and the bindings listed in step 4. People log in through
oidc and create new tokens.
6. Check that nothing is left on the old provider. Both listings should
be empty:
1 2 3 4 | |
- Remove the realm default roles. Keycloak → realm settings → User
registration → Default roles: remove
view-users,query-usersandquery-groupsofrealm-management(added bykeycloak-bootstrap.shwithRANCHER_PROVIDER=keycloakoidc). - Verify as in step 7 above, with a user who logs in through
oidc.
On Rancher 2.14.x and up to 2.15.4, after a group refresh with the oidc
provider, the impersonation extra principalid in Kubernetes audit logs
shows oidc_group://<sub> instead of the user principal. Groups and RBAC
are not affected; it is fixed in Rancher 2.15.6 and 2.16.0.
Rolling back. Before step 5, disable nothing: set
config.rancher.groupPrincipalPrefix=keycloakoidc_group:// again and the
operator moves its bindings back; keycloakoidc users are still there.
After step 5 there are no old users to return to: re-enable
keycloakoidc, switch the prefix back, restore the realm default roles,
and recreate hand-made bindings for the keycloakoidc principals. Users
log in again and get new Rancher users; the operator recreates its
bindings after the prefix switch. Disabling oidc afterwards deletes the
oidc users and bindings in the same way.