diff --git a/group_vars/all/00_general.yml b/group_vars/all/00_general.yml index e044287449..01986efab9 100644 --- a/group_vars/all/00_general.yml +++ b/group_vars/all/00_general.yml @@ -15,6 +15,8 @@ STORAGE_CONSTRAINED: false # Storage constraint means that Infinito.Nexus prefer MASK_CREDENTIALS_IN_LOGS: true +IAM_USERNAME_MAX_LENGTH: 20 # Discourse refuses a longer one with ActiveRecord::RecordInvalid, and it is the tightest consumer + HOST_CURRENCY: "EUR" HOST_TIMEZONE: "UTC" HOST_TIMEZONE_LABEL: "United Kingdom" diff --git a/roles/svc-db-openldap/templates/ldif/configuration/05_constraint_module.ldif.j2 b/roles/svc-db-openldap/templates/ldif/configuration/05_constraint_module.ldif.j2 new file mode 100644 index 0000000000..0eff186ce8 --- /dev/null +++ b/roles/svc-db-openldap/templates/ldif/configuration/05_constraint_module.ldif.j2 @@ -0,0 +1,5 @@ +# Load constraint module (cn=config) +dn: {{ OPENLDAP_MODULE_DN }} +changetype: modify +add: olcModuleLoad +olcModuleLoad: constraint diff --git a/roles/svc-db-openldap/templates/ldif/configuration/06_constraint_username_length.ldif.j2 b/roles/svc-db-openldap/templates/ldif/configuration/06_constraint_username_length.ldif.j2 new file mode 100644 index 0000000000..bf93640707 --- /dev/null +++ b/roles/svc-db-openldap/templates/ldif/configuration/06_constraint_username_length.ldif.j2 @@ -0,0 +1,6 @@ +# Add constraint overlay to data backend (cn=config): cap the uid length +dn: olcOverlay={2}constraint,{{ OPENLDAP_DATA_BACKEND_DN }} +objectClass: olcOverlayConfig +objectClass: olcConstraintConfig +olcOverlay: constraint +olcConstraintAttribute: uid size {{ IAM_USERNAME_MAX_LENGTH }} diff --git a/roles/web-app-homeassistant/meta/users.yml b/roles/web-app-homeassistant/meta/users.yml index 28e8230ada..8716e9c234 100644 --- a/roles/web-app-homeassistant/meta/users.yml +++ b/roles/web-app-homeassistant/meta/users.yml @@ -1,5 +1,6 @@ --- mcp-web-app-homeassistant: + username: mcp-homeassistant firstname: Infinito lastname: MCP accounts: [] diff --git a/roles/web-app-keycloak/templates/import/components/org.keycloak.userprofile.UserProfileProvider.json.j2 b/roles/web-app-keycloak/templates/import/components/org.keycloak.userprofile.UserProfileProvider.json.j2 index 24f0391ed5..165eef7f8f 100644 --- a/roles/web-app-keycloak/templates/import/components/org.keycloak.userprofile.UserProfileProvider.json.j2 +++ b/roles/web-app-keycloak/templates/import/components/org.keycloak.userprofile.UserProfileProvider.json.j2 @@ -26,7 +26,7 @@ "name": "username", "displayName": "${username}", "validations": { - "length": { "min": 3, "max": 255 }, + "length": { "min": 3, "max": {{ IAM_USERNAME_MAX_LENGTH }} }, "pattern": { "pattern": "^(?!(?:" ~ KEYCLOAK_RESERVED_USERNAMES_REGEX | replace('\\', '\\\\') ~ ")$)[a-z0-9]+$", "error-message": "Username is reserved or contains invalid characters. Only lowercase letters (a–z) and digits (0–9) are allowed." diff --git a/roles/web-app-nextcloud/meta/users.yml b/roles/web-app-nextcloud/meta/users.yml index db72a09373..59733a984e 100644 --- a/roles/web-app-nextcloud/meta/users.yml +++ b/roles/web-app-nextcloud/meta/users.yml @@ -8,6 +8,7 @@ no-reply: rate: 60 / 1min burst: 3600 mcp-web-app-nextcloud: + username: mcp-nextcloud firstname: Infinito lastname: MCP accounts: [] diff --git a/roles/web-app-openproject/meta/users.yml b/roles/web-app-openproject/meta/users.yml index 8199cbbdd8..051b5b7734 100644 --- a/roles/web-app-openproject/meta/users.yml +++ b/roles/web-app-openproject/meta/users.yml @@ -1,5 +1,6 @@ --- gitlab-web-app-openproject: + username: gitlab-openproject firstname: Infinito lastname: GitLab accounts: [] diff --git a/roles/web-app-semaphore/meta/users.yml b/roles/web-app-semaphore/meta/users.yml index f3d89eccb9..10b0cfceb4 100644 --- a/roles/web-app-semaphore/meta/users.yml +++ b/roles/web-app-semaphore/meta/users.yml @@ -1,6 +1,6 @@ --- web-app-semaphore-breakglass: - username: breakglass-administrator + username: breakglass-admin accounts: [] forward: platform_administrator description: Semaphore local break-glass admin; not provisioned to IAM, mail forwards to the platform administrator mailbox diff --git a/roles/web-app-wordpress/meta/users.yml b/roles/web-app-wordpress/meta/users.yml index 38f23ec80e..b86809d8e8 100644 --- a/roles/web-app-wordpress/meta/users.yml +++ b/roles/web-app-wordpress/meta/users.yml @@ -1,5 +1,6 @@ --- mcp-web-app-wordpress: + username: mcp-wordpress firstname: Infinito lastname: MCP accounts: [] diff --git a/tests/lint/ansible/roles/meta/test_identity_username_charset.py b/tests/lint/ansible/roles/meta/test_identity_username_charset.py index 98ea25d70e..541ae49c12 100644 --- a/tests/lint/ansible/roles/meta/test_identity_username_charset.py +++ b/tests/lint/ansible/roles/meta/test_identity_username_charset.py @@ -4,11 +4,22 @@ import re import unittest from utils.cache.users import get_user_defaults +from utils.cache.yaml import load_yaml from . import PROJECT_ROOT IDENTITY_USERNAME = re.compile(r"^[a-z0-9]+$") -IDENTITY_USERNAME_MAX = 20 +_GENERAL_VARS = PROJECT_ROOT / "group_vars" / "all" / "00_general.yml" + + +def username_max_length() -> int: + """The username ceiling every IAM consumer is configured against. + + Returns: + ``IAM_USERNAME_MAX_LENGTH`` from the global variables, which the + Keycloak user profile and the OpenLDAP constraint overlay both render. + """ + return int(load_yaml(str(_GENERAL_VARS))["IAM_USERNAME_MAX_LENGTH"]) class TestIdentityUsernameCharset(unittest.TestCase): @@ -38,27 +49,27 @@ class TestIdentityUsernameCharset(unittest.TestCase): was too long stays too long, and the second consumer only speaks up once the first is satisfied. """ + maximum = username_max_length() users = get_user_defaults(roles_dir=str(PROJECT_ROOT / "roles")) offenders: list[str] = [] for key, entry in sorted(users.items()): - if "identity" not in (entry.get("accounts") or []): - continue username = str(entry.get("username", "")) - if not IDENTITY_USERNAME.match(username): - offenders.append( - f"{key}: {username!r} is not lowercase alphanumeric" - ) - elif len(username) > IDENTITY_USERNAME_MAX: + if len(username) > maximum: offenders.append( f"{key}: {username!r} is {len(username)} characters, " - f"over the {IDENTITY_USERNAME_MAX} Discourse allows" + f"over the {maximum} of IAM_USERNAME_MAX_LENGTH" + ) + if "identity" in (entry.get("accounts") or []) and not ( + IDENTITY_USERNAME.match(username) + ): + offenders.append( + f"{key}: {username!r} is not lowercase alphanumeric" ) if offenders: self.fail( - "These users are registered as an identity but carry a " - "username one of Keycloak, Mastodon or Discourse refuses:\n" - " - " + "\n - ".join(offenders) + "These declared users carry a username an IAM consumer " + "refuses:\n - " + "\n - ".join(offenders) )