From eab8f5fc90cb7615076a2187579dd2664af05e1f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 3 Sep 2026 02:15:53 +0200 Subject: [PATCH] fix(inventory): give pinned user passwords the mark the realm policy demands The shell-safe default drew from letters and digits only, and every user without declared accounts is an identity account, so kcadm set-password rejected hermes and openclaw with invalidPasswordMinSpecialCharsMessage as soon as keycloak entered the GitLab#1 round without LDAP (4164f37981) and the extras file stopped replacing the users map (03e42bae6d; run 33689701895). The generator now draws from letters, digits and -_. and guarantees upper case, lower case, a digit and one mark, never in the leading position. Co-Authored-By: Claude Fable 5.1 --- .../inventory/provision/passwords.py | 29 ++++++++++++----- .../provision/test_users_generator.py | 32 +++++++++++++++---- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/cli/administration/inventory/provision/passwords.py b/cli/administration/inventory/provision/passwords.py index 9382bc33ea..251c466b03 100644 --- a/cli/administration/inventory/provision/passwords.py +++ b/cli/administration/inventory/provision/passwords.py @@ -1,11 +1,18 @@ from __future__ import annotations import re +import secrets +import string from utils.manager.value_generator import ValueGenerator USER_PASSWORD_LENGTH = 64 DECLARED_PASSWORD_ATTEMPTS = 64 +SHELL_SAFE_MARKS = "-_." +_USER_PASSWORD_ALPHABET = string.ascii_letters + string.digits + SHELL_SAFE_MARKS +_USER_PASSWORD_RE = re.compile( + r"^[A-Za-z0-9](?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[-_.])[A-Za-z0-9._-]{15,}$" +) def generate_random_password(length: int = 64) -> str: @@ -16,16 +23,22 @@ def generate_user_password(length: int = USER_PASSWORD_LENGTH) -> str: """Return a shell-safe password for a declared user. Args: - length: characters to generate. + length: characters to generate, at least 16. - A user password reaches its application through Ansible, a shell, a - ``container exec -e`` and finally the runtime's own environment reader. The - punctuation in ``generate_strong_password`` does not survive that chain - intact, which is why roles used to declare a separate alphanumeric - credential rather than use the user's own password. At this length the - alphabet costs nothing worth measuring. + Returns: + letters, digits and the marks ``-_.``, never a leading ``-``, with one + of each class the Keycloak realm policy ``specialChars(1)`` counts. """ - return ValueGenerator().generate_secure_alphanumeric(length) + if length < 16: + raise ValueError("A user password must be at least 16 characters") + for _ in range(DECLARED_PASSWORD_ATTEMPTS): + body = [secrets.choice(_USER_PASSWORD_ALPHABET) for _ in range(length)] + body[0] = secrets.choice(string.ascii_letters + string.digits) + body[secrets.randbelow(length - 1) + 1] = secrets.choice(SHELL_SAFE_MARKS) + password = "".join(body) + if _USER_PASSWORD_RE.match(password): + return password + raise RuntimeError("Failed to generate a user password after many attempts") def generate_declared_user_password(username, algorithm, validation): diff --git a/tests/unit/python/cli/administration/inventory/provision/test_users_generator.py b/tests/unit/python/cli/administration/inventory/provision/test_users_generator.py index b1865c43ce..d503658a5c 100644 --- a/tests/unit/python/cli/administration/inventory/provision/test_users_generator.py +++ b/tests/unit/python/cli/administration/inventory/provision/test_users_generator.py @@ -1,3 +1,4 @@ +import re import shutil import tempfile import unittest @@ -247,19 +248,34 @@ class TestGenerateUserPasswords(unittest.TestCase): class TestGeneratedPasswordIsShellSafe(unittest.TestCase): """The value travels Ansible -> shell -> container exec -> runtime env.""" - def test_no_punctuation_survives_into_a_user_password(self): + SHELL_SAFE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$") + REALM_POLICY = re.compile( + r"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{16,}$" + ) + + def test_only_unreserved_marks_survive_into_a_user_password(self): for _ in range(200): password = generate_user_password() - self.assertTrue( - password.isalnum(), - f"{password!r} carries punctuation; roles used to declare their " - "own alphanumeric credential precisely because the exec chain " - "mangles it", + self.assertRegex( + password, + self.SHELL_SAFE, + f"{password!r} carries punctuation the exec chain mangles; only " + "the URL-unreserved marks - _ . are known to survive it", ) + def test_every_password_satisfies_the_realm_policy(self): + for _ in range(200): + password = generate_user_password() + self.assertRegex(password, self.REALM_POLICY) + self.assertRegex(password, r"[-_.]") + def test_the_password_is_long_enough_to_afford_the_smaller_alphabet(self): self.assertGreaterEqual(len(generate_user_password()), 64) + def test_a_password_shorter_than_the_realm_minimum_is_refused(self): + with self.assertRaises(ValueError): + generate_user_password(8) + class TestRealisticInventoryRoundTrip(unittest.TestCase): """The generator rewrites the whole host_vars, so nothing else may shift.""" @@ -509,7 +525,9 @@ class TestDeclaredPasswordPolicy(unittest.TestCase): generate_declared_user_password("bot", "alphanumeric", "[^A-Za-z0-9]") def test_no_declaration_keeps_the_shell_safe_default(self): - self.assertTrue(generate_declared_user_password("bot", None, None).isalnum()) + password = generate_declared_user_password("bot", None, None) + self.assertRegex(password, r"^[A-Za-z0-9][A-Za-z0-9._-]*$") + self.assertRegex(password, r"[-_.]") if __name__ == "__main__":