Skip to content

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 groups claim, 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=keycloak stops the new operator at startup, with a message pointing here. Remove it, and the KEYCLOAK_* 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
kubectl -n waldur-system get managedrancherprojects -o json > mrp-before.json

# One line per group the old operator managed: CR, scope, group name, Keycloak id
jq -r '.items[] | .metadata.name as $cr
  | ((.status.keycloakRoleBindings // [])[] | [$cr, "project", .groupName, .keycloakGroupId]),
    ((.status.clusterKeycloakRoleBindings // [])[] | [$cr, "cluster", .groupName, .keycloakGroupId])
  | @tsv' mrp-before.json | sort -u > operator-groups.tsv

# The per-cluster parent groups
jq -r '.items[] | [.metadata.name, .spec.keycloak.parentGroupName, .status.keycloakParentGroupId] | @tsv' \
  mrp-before.json | sort -u > operator-parent-groups.tsv

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
# with an admin token for the realm
curl -s -H "Authorization: Bearer $TOKEN" \
  "https://keycloak.example.com/admin/realms/<realm>/groups?q=managed_by:rancher-keycloak-operator&briefRepresentation=false" \
  | jq -r '.. | objects | select(.name?) | [.name, .id] | @tsv'

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
kubectl apply -f examples/waldur-project-admin.roletemplate.yaml
kubectl apply -f examples/waldur-cluster-admin.roletemplate.yaml

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
kubectl -n waldur-system scale deploy/<operator-release> --replicas=0

Then upgrade the rancher-kc-crd site agent to the version without member sync and set its role mapping:

  • project_admin → waldur-project-admin
  • project_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
helm upgrade <release> rancher-keycloak-operator/rancher-keycloak-operator \
  --namespace waldur-system --version 0.7.0 \
  --set config.rancher.url=https://rancher.example.com \
  --set config.rancher.bearerToken=token-xxxxx:yyyyyy

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
kubectl -n waldur-system logs deploy/<operator-release> --tail=50   # no PermanentError
kubectl -n waldur-system get managedrancherprojects                 # Phase: Ready
kubectl -n waldur-system get managedrancherprojects -o json \
  | jq '[.items[].status | has("keycloakParentGroupId", "memberHistory")] | unique'   # [false]

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
kubectl -n waldur-system get managedrancherprojects -o json > mrp-now.json

# ids the operator tracks now
jq -r '.items[].status | (.keycloakRoleBindings // [])[].rancherBindingId,
         (.clusterKeycloakRoleBindings // [])[].rancherClusterBindingId
       | select(. != null and . != "")' mrp-now.json | sort -u > tracked-ids.txt

# group names the operator manages: today's specs plus the old status from step 1
{ jq -r '.items[].spec | (.keycloak.roleBindings // [])[].groupName,
           (.cluster.keycloak.roleBindings // [])[].groupName' mrp-now.json
  cut -f3 operator-groups.tsv; } | sort -u > operator-group-names.txt

# bindings of those groups that no CR tracks
kubectl get projectroletemplatebindings,clusterroletemplatebindings -A -o json \
  | jq -r --rawfile tracked tracked-ids.txt --rawfile names operator-group-names.txt '
      ($tracked | split("\n")) as $t | ($names | split("\n")) as $n
      | .items[] | select(.groupPrincipalName != null)
      | (.groupPrincipalName | sub("^[a-z]+_group://"; "")) as $group
      | (.metadata.namespace + ":" + .metadata.name) as $id
      | select(any($n[]; . == $group) and (any($t[]; . == $id) | not))
      | [.kind, .metadata.namespace, .metadata.name, .groupPrincipalName, .roleTemplateName] | @tsv'

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
kubectl -n <namespace> delete projectroletemplatebinding <name>   # or clusterroletemplatebinding

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

  1. A test user with a resource-project role in Waldur logs in to Rancher and sees the project; their token's roles lists the group, groups lists none of the operator's groups.
  2. Remove the role in Waldur. Within auth-user-info-max-age-seconds the user loses the project, in the UI and through a kubeconfig token.
  3. A user who was only in an old operator group, without a Waldur role, has no access.
  4. As waldur-project-admin, adding a project member and creating a RoleBinding are refused.

Rolling back

Before step 6 a rollback loses nothing:

  1. Scale the new operator to 0.
  2. Roll the site agent back to the version with member sync, so CRs carry members again.
  3. helm rollback (or upgrade to 0.6.x) with the old config.keycloak.* values. The old CRD schema comes back with the chart.
  4. The old operator finds its groups by name, recreates the status fields and syncs the members again. status.memberHistory starts empty: with KEYCLOAK_EXTERNAL_MEMBER_PATTERN set, members added by hand are treated as new for one grace period.
  5. The old operator never removes a project binding whose role changed, so waldur-project-admin PRTBs stay next to the restored project-owner ones: 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 oidc does not disable keycloakoidc. Both stay enabled, and both appear on the login page, until keycloakoidc is 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:

  1. Keycloak. The Rancher client stays as it is (same redirect URI).
  2. Enable oidc. As an admin, configure and enable the oidc provider 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 through oidc.
  3. Switch the operator's prefix:
1
2
3
4
5
6
helm upgrade <release> rancher-keycloak-operator/rancher-keycloak-operator \
  --namespace waldur-system --reuse-values \
  --set config.rancher.groupPrincipalPrefix=oidc_group://
# optional, instead of waiting up to 5 minutes for the resync:
kubectl -n waldur-system annotate managedrancherprojects --all \
  waldur.io/force-reconcile="$(date +%s)" --overwrite

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
kubectl get projectroletemplatebindings,clusterroletemplatebindings -A -o json | jq -r '.items[]
  | select((.groupPrincipalName // "" | startswith("keycloakoidc_group://"))
           or (.userPrincipalName // "" | startswith("keycloakoidc_user://")))
  | [.kind, .metadata.namespace, .metadata.name, .roleTemplateName,
     (.groupPrincipalName // .userPrincipalName)] | @tsv'

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
kubectl get users.management.cattle.io -o json | jq -r '.items[]
  | select(any(.principalIds[]?; startswith("keycloakoidc_user://")))
  | [.metadata.name, .displayName, (.principalIds | join(","))] | @tsv'
# and the binding listing from step 4
  1. Remove the realm default roles. Keycloak → realm settings → User registration → Default roles: remove view-users, query-users and query-groups of realm-management (added by keycloak-bootstrap.sh with RANCHER_PROVIDER=keycloakoidc).
  2. 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.