Manage Rancher users, global roles, tokens and settings #8
Reference in New Issue
Block a user
Delete Branch "benvin/users-roles-tokens-settings"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Why
The repo managed only the Keycloak(OIDC) auth provider and group role bindings; the rest of Rancher's config (users, custom global roles, API tokens, settings) was unmanaged clickops.
server-urlin particular was never asserted. This expands the same yaml-discovery convention the estate uses everywhere to four more rancher2 kinds.How
config/config.hcldiscoversusers/,roles/,tokens/andsettings/with the existingstartswith-guarded comprehension; absent/empty dirs yield empty maps.terragrunt.hclpasses the four new maps as inputs.modules/rancher/variables.tfdeclares precise object schemas per kind (optional-with-default attributes, same style askeycloakoidc).rancher2_user— filename is the username.passwordis required by the provider, so each entry carries apassword_vault: {mount, path, key}ref and the module readsdata.vault_kv_secret_v2. Optionalglobal_role_bindings: [role]becomesrancher2_global_role_bindingwithuser_id, keyed"<username>/<role>"so it never collides with the existing group-binding map.rancher2_global_role— filename is the role name;new_user_default,inherited_cluster_roles, andrules(dynamic blocks:api_groups/resources/verbs/non_resource_urls/resource_names).rancher2_token—description,ttl,renew,cluster_id.rancher2_setting— filename is the setting name; seedsconfig/settings/server-url.yaml=https://rancher.k8s.syd1.au.unkin.net, matching the environment.Tokens caveat
Verified against the rancher2 14.1.1 provider schema (
tofu providers schema -json):rancher2_token.user_idis computed, not settable — there is no user selector. Every token declared underconfig/tokens/is minted for the identity the provider authenticates as (the admin token Terraform runs with), not for any user inconfig/users/. A token for another user must be created by that user. Token/secret values also land in Terraform state. This is called out in a comment on the resource and in the README.Vault seeding
data.vault_kv_secret_v2resolves at plan time, so aconfig/users/*.yamlentry whose Vault secret does not exist yet fails the plan. This PR therefore ships zero user and token yamls — only the empty directories (.gitkeep) and the oneserver-urlsetting — so plan needs no new KV seed. Entries land in follow-ups once their secrets are seeded.Validation
uvx pre-commit run --all-filesclean (tofu fmt, tofu validate, tflint, terragrunt hcl fmt, yamllint).terragrunt render --jsonconfirms discovery:settings = {server-url},users/roles/tokensempty, existing inputs unchanged.Addressed both review findings on the per-user global role binding name (
187e29d):validationblocks onvar.usersrequiring the username and every entry inglobal_role_bindingsto match^[a-z0-9]([a-z0-9-]*[a-z0-9])?$. Non-compliant input (.,_,@, uppercase) now fails the plan with a message telling the operator to renameconfig/users/<username>.yaml, instead of silently munging and failing Rancher's RFC 1123 validation at apply.akuser-${each.value.username}-${each.value.global_role_id}— noreplace()/lower(). Since hyphens remain legal inside both parts, alifecycle.preconditionon the resource rejects the residual ambiguity (foo-bar+bazvsfoo+bar-baz) at plan time rather than as a duplicate-object conflict mid-apply.README users schema section documents the constraint. The group binding path is untouched.
pre-commit run --all-filesgreen;terragrunt render --jsonstill shows an empty users map and otherwise unchanged inputs.