fix(users): merge per-key so two roles can register tokens for one user

The merge across every meta/users.yml compared a field as a whole. For tokens that field is a mapping whose keys are per-app entries, so two roles registering a token for the same user hit the conflict check even though the entries never overlapped: the first role to declare it won and the second aborted the deploy. sys-token-store is the sanctioned way to persist a runtime secret per app on a user, which made it usable by exactly one role.

Dict-valued fields now merge key by key. A real collision, the same sub-key with two different values, still raises, and the message names field.sub_key so a token store holding a dozen apps says which one collided.

Tests cover both paths: two roles registering distinct token keys for one user, and the same key with conflicting values.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-07-29 14:51:05 +02:00
1 parent 04a079a816
commit ffbd2ce0d3
2 files changed
+54 -3

No files matched your search

+38
View File
@@ -90,6 +90,44 @@ class TestLoadUserDefs(unittest.TestCase):
with self.assertRaisesRegex(ValueError, "Conflict for user 'shared'"):
cache_users._load_user_defs(roles)
def test_two_roles_register_a_token_for_the_same_user(self):
with tempfile.TemporaryDirectory() as tmp:
roles = Path(tmp) / "roles"
_write(
roles / "web-app-a" / ROLE_FILE_META_USERS,
"""
administrator: {tokens: {web-app-a: ""}}
""",
)
_write(
roles / "web-app-b" / ROLE_FILE_META_USERS,
"""
administrator: {tokens: {web-app-b: ""}}
""",
)
defs = cache_users._load_user_defs(roles)
self.assertEqual(
sorted(defs["administrator"]["tokens"]), ["web-app-a", "web-app-b"]
)
def test_the_same_token_key_with_two_values_still_conflicts(self):
with tempfile.TemporaryDirectory() as tmp:
roles = Path(tmp) / "roles"
_write(
roles / "web-app-a" / ROLE_FILE_META_USERS,
"""
administrator: {tokens: {shared-app: from-a}}
""",
)
_write(
roles / "web-app-b" / ROLE_FILE_META_USERS,
"""
administrator: {tokens: {shared-app: from-b}}
""",
)
with self.assertRaisesRegex(ValueError, "tokens.shared-app"):
cache_users._load_user_defs(roles)
class TestBuildUsers(unittest.TestCase):
def test_allocates_distinct_uids_for_users_without_explicit_uid(self):
+16 -3
View File
@@ -102,12 +102,25 @@ def _load_user_defs(roles_dir: Path) -> OrderedDict[str, dict[str, Any]]:
existing = merged[key]
for field, value in overrides.items():
if field in existing and existing[field] != value:
if field not in existing:
existing[field] = copy.deepcopy(value)
continue
current = existing[field]
if isinstance(current, dict) and isinstance(value, dict):
for sub_key, sub_value in value.items():
if sub_key in current and current[sub_key] != sub_value:
raise ValueError(
f"Conflict for user '{key}': field '{field}.{sub_key}' has "
f"existing value '{current[sub_key]}', tried to set "
f"'{sub_value}' in {filepath}"
)
current[sub_key] = copy.deepcopy(sub_value)
continue
if current != value:
raise ValueError(
f"Conflict for user '{key}': field '{field}' has existing value "
f"'{existing[field]}', tried to set '{value}' in {filepath}"
f"'{current}', tried to set '{value}' in {filepath}"
)
existing.update(copy.deepcopy(overrides))
return merged