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 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Fable 5.1 committed 2026-09-03 02:15:53 +02:00
1 parent 32ee8992b5
commit eab8f5fc90
2 files changed
+46 -15

No files matched your search

@@ -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__":