Validate user and role names instead of munging binding names
Rancher requires global role binding names to be RFC 1123 labels. The per-user binding name lowercased the key and replaced only "/", so usernames containing ".", "_" or "@" still produced an invalid name and failed at apply, and hyphenated names could collide (foo-bar + baz vs foo + bar-baz) into one object. Add validation blocks on var.users requiring the username and every referenced role name to be RFC 1123 labels, so non-compliant input fails the plan with an actionable message. The binding name is then built from the two parts directly. A precondition rejects the remaining hyphen ambiguity at plan time rather than as a mid-apply conflict. Document the constraint in the users schema section of the README. The group binding path is unchanged.
This commit is contained in:
@@ -51,6 +51,28 @@ variable "users" {
|
||||
global_role_bindings = optional(list(string), [])
|
||||
}))
|
||||
default = {}
|
||||
|
||||
# Rancher names each global role binding as an RFC 1123 label and rejects
|
||||
# anything else at apply time. The binding name is built from the username and
|
||||
# the role name verbatim, so both must already be compliant — fail the plan
|
||||
# with a clear message instead of munging the input and hoping.
|
||||
validation {
|
||||
condition = alltrue([
|
||||
for username in keys(var.users) :
|
||||
can(regex("^[a-z0-9]([a-z0-9-]*[a-z0-9])?$", username))
|
||||
])
|
||||
error_message = "Usernames must be RFC 1123 labels: lowercase alphanumerics and '-', starting and ending alphanumeric (no '.', '_', '@' or uppercase). Rename config/users/<username>.yaml to a compliant username."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = alltrue(flatten([
|
||||
for user in values(var.users) : [
|
||||
for role in user.global_role_bindings :
|
||||
can(regex("^[a-z0-9]([a-z0-9-]*[a-z0-9])?$", role))
|
||||
]
|
||||
]))
|
||||
error_message = "Values in global_role_bindings must be RFC 1123 labels: lowercase alphanumerics and '-', starting and ending alphanumeric."
|
||||
}
|
||||
}
|
||||
|
||||
# Custom Rancher global roles (config/roles/<name>.yaml). Map key is the role
|
||||
|
||||
Reference in New Issue
Block a user