mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-30 14:56:11 +00:00
fix(web-app-stalwart): recognise existing v1.0 accounts and patch only their password
Stalwart v1.0 refuses an existing account with primaryKeyViolation; the batched provisioning from 1fbd4758e3 only knew alreadyExists, so every update deploy skipped the password sync and the listed-id fallback hid the refusal (msmtp then failed with 535). An account now counts as existing when the refusal's objectId is the listed account of that name, any other refusal fails the assert, and the update patches credentials/<k>/secret instead of replacing the whole credentials list, which would have dropped every app password, API key and second factor. sys-token-store gains write_many.yml, so all bot tokens land in one store write and one applications re-render; write.yml delegates a one-entry batch to it.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
06a899df0d
commit
86e7c844f3
10 files changed
+479
-110
No files matched your search
@@ -0,0 +1,164 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from ansible.parsing.dataloader import DataLoader
|
||||
from ansible.template import Templar
|
||||
|
||||
from utils.cache.files import PROJECT_ROOT
|
||||
from utils.cache.yaml import load_yaml_any
|
||||
from utils.templating.ansible import _trust_as_template
|
||||
|
||||
TASKS = PROJECT_ROOT / "roles" / "sys-token-store" / "tasks"
|
||||
MERGE = "🔨 Build merged structure"
|
||||
EMPTY = "🛑 Abort if a token is empty (do not persist empties)"
|
||||
PERSIST = "💾 Persist store (idempotent, single-writer via STACK_HOST)"
|
||||
MIRROR = "🪞 Mirror the store onto the controller, where every users lookup reads"
|
||||
HAND_OVER = "🔄 Hand the written store to the lookups of the rest of the play"
|
||||
RERENDER = "🧊 Re-render the applications SPOT the new tokens invalidated"
|
||||
|
||||
USERS = {
|
||||
"alice": {"tokens": {"web-app-a": "old-a"}, "note": "kept"},
|
||||
"carol": {"tokens": {"web-app-c": "token-c"}},
|
||||
}
|
||||
SKIPPED = {"changed": False, "skipped": True}
|
||||
|
||||
|
||||
def _tasks(name: str) -> list[dict]:
|
||||
return load_yaml_any(str(TASKS / name), default_if_missing=[])
|
||||
|
||||
|
||||
def _task(name: str) -> dict:
|
||||
return next(t for t in _tasks("write_many.yml") if t.get("name") == name)
|
||||
|
||||
|
||||
def _render(template, **variables):
|
||||
templar = Templar(loader=DataLoader(), variables=variables)
|
||||
return templar.template(_trust_as_template(template))
|
||||
|
||||
|
||||
def _merged(entries: list[dict], users: dict = USERS) -> dict:
|
||||
return _render(
|
||||
_task(MERGE)["ansible.builtin.set_fact"]["sys_token_store_users_merged"],
|
||||
sys_token_store_entries=entries,
|
||||
sys_token_store_users=users,
|
||||
)
|
||||
|
||||
|
||||
def _when(name: str, **variables) -> bool:
|
||||
when = _task(name)["when"]
|
||||
conditions = when if isinstance(when, list) else [when]
|
||||
return _render(
|
||||
"{{ ((" + ") and (".join(conditions) + ")) | bool }}",
|
||||
DEPLOYMENT_MODE="swarm",
|
||||
**variables,
|
||||
)
|
||||
|
||||
|
||||
def _entry(user_key: str, app: str, token: str) -> dict:
|
||||
return {"user_key": user_key, "app": app, "token": token}
|
||||
|
||||
|
||||
class TestWriteMany(unittest.TestCase):
|
||||
def test_every_entry_lands_in_one_merge(self):
|
||||
merged = _merged(
|
||||
[
|
||||
_entry("alice", "web-app-b", " new-b "),
|
||||
_entry("alice", "web-app-a", "new-a"),
|
||||
_entry("bob", "web-app-b", "token-b"),
|
||||
]
|
||||
)
|
||||
self.assertEqual(
|
||||
merged,
|
||||
{
|
||||
"alice": {
|
||||
"tokens": {"web-app-a": "new-a", "web-app-b": "new-b"},
|
||||
"note": "kept",
|
||||
},
|
||||
"bob": {"tokens": {"web-app-b": "token-b"}},
|
||||
"carol": {"tokens": {"web-app-c": "token-c"}},
|
||||
},
|
||||
)
|
||||
|
||||
def test_the_later_entry_of_one_user_and_app_wins(self):
|
||||
merged = _merged(
|
||||
[_entry("bob", "web-app-b", "first"), _entry("bob", "web-app-b", "second")]
|
||||
)
|
||||
self.assertEqual(merged["bob"], {"tokens": {"web-app-b": "second"}})
|
||||
|
||||
def test_an_unchanged_store_skips_the_write_and_the_rerender(self):
|
||||
merged = _merged(
|
||||
[
|
||||
_entry("alice", "web-app-a", "old-a"),
|
||||
_entry("carol", "web-app-c", "token-c"),
|
||||
]
|
||||
)
|
||||
self.assertEqual(merged, USERS)
|
||||
state = {"sys_token_store_users_merged": merged, "sys_token_store_users": USERS}
|
||||
self.assertFalse(_when(PERSIST, **state))
|
||||
self.assertFalse(_when(MIRROR, **state))
|
||||
self.assertFalse(_when(HAND_OVER, **state))
|
||||
self.assertFalse(
|
||||
_when(
|
||||
RERENDER,
|
||||
sys_token_store_persisted=SKIPPED,
|
||||
sys_token_store_mirrored=SKIPPED,
|
||||
)
|
||||
)
|
||||
|
||||
def test_a_changed_store_is_written_and_rerendered_once(self):
|
||||
merged = _merged([_entry("bob", "web-app-b", "token-b")])
|
||||
state = {"sys_token_store_users_merged": merged, "sys_token_store_users": USERS}
|
||||
self.assertTrue(_when(PERSIST, **state))
|
||||
self.assertTrue(_when(HAND_OVER, **state))
|
||||
self.assertTrue(
|
||||
_when(
|
||||
RERENDER,
|
||||
sys_token_store_persisted={"changed": True},
|
||||
sys_token_store_mirrored=SKIPPED,
|
||||
)
|
||||
)
|
||||
rerenders = [t for t in _tasks("write_many.yml") if "applications_carrier" in t]
|
||||
self.assertEqual(len(rerenders), 1)
|
||||
self.assertNotIn("loop", rerenders[0])
|
||||
|
||||
def test_the_store_has_one_writer(self):
|
||||
persist = _task(PERSIST)
|
||||
self.assertEqual(persist["delegate_to"], "{{ STACK_HOST }}")
|
||||
self.assertIs(persist["run_once"], True)
|
||||
mirror = _task(MIRROR)
|
||||
self.assertEqual(mirror["delegate_to"], "localhost")
|
||||
self.assertIs(mirror["run_once"], True)
|
||||
|
||||
def test_an_empty_token_is_refused_by_name(self):
|
||||
empty = _render(
|
||||
_task(EMPTY)["vars"]["sys_token_store_empty"],
|
||||
sys_token_store_entries=[
|
||||
_entry("alice", "web-app-a", "set"),
|
||||
_entry("bob", "web-app-b", " "),
|
||||
],
|
||||
)
|
||||
self.assertEqual(empty, ["bob/web-app-b"])
|
||||
|
||||
|
||||
class TestWriteIsAOneEntryBatch(unittest.TestCase):
|
||||
def test_the_single_token_contract_feeds_write_many(self):
|
||||
tasks = _tasks("write.yml")
|
||||
self.assertEqual(len(tasks), 1)
|
||||
self.assertEqual(tasks[0]["ansible.builtin.import_tasks"], "write_many.yml")
|
||||
single = {
|
||||
"sys_token_store_user_key": "administrator",
|
||||
"sys_token_store_app": "web-app-matomo",
|
||||
"sys_token_store_token": "secret-token",
|
||||
}
|
||||
entries = [
|
||||
{field: _render(value, **single) for field, value in entry.items()}
|
||||
for entry in tasks[0]["vars"]["sys_token_store_entries"]
|
||||
]
|
||||
self.assertEqual(
|
||||
entries, [_entry("administrator", "web-app-matomo", "secret-token")]
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -82,19 +82,42 @@ BASE = {
|
||||
"stalwart_app_password_stale": {},
|
||||
}
|
||||
|
||||
PASSWORD = {"@type": "Password", "secret": "********"}
|
||||
APP_PASSWORD = {"@type": "AppPassword", "description": DESCRIPTION}
|
||||
|
||||
|
||||
def _violation(object_id: str, object_type: str = "Account") -> dict:
|
||||
return {
|
||||
"type": "primaryKeyViolation",
|
||||
"properties": ["email"],
|
||||
"objectId": {"object": object_type, "id": object_id},
|
||||
}
|
||||
|
||||
|
||||
def _held(object_id: str, name: str, credentials: dict | None = None) -> dict:
|
||||
return {
|
||||
"id": object_id,
|
||||
"name": name,
|
||||
"domainId": "d1",
|
||||
"credentials": {"0": PASSWORD} if credentials is None else credentials,
|
||||
}
|
||||
|
||||
|
||||
CREATE_RESPONSE = [
|
||||
[
|
||||
"x:Account/set",
|
||||
{
|
||||
"created": {"u0": {"id": "a"}},
|
||||
"notCreated": {
|
||||
"u1": {"type": "alreadyExists", "objectId": {"id": "b"}},
|
||||
"u2": {"type": "alreadyExists", "objectId": {"id": "c"}},
|
||||
},
|
||||
"notCreated": {"u1": _violation("b"), "u2": _violation("c")},
|
||||
},
|
||||
"c0",
|
||||
]
|
||||
]
|
||||
HELD = (
|
||||
_held("a", "administrator"),
|
||||
_held("b", "no-reply"),
|
||||
_held("c", "contact"),
|
||||
)
|
||||
|
||||
|
||||
def _resolved() -> list[dict]:
|
||||
@@ -106,7 +129,7 @@ def _resolved() -> list[dict]:
|
||||
)
|
||||
|
||||
|
||||
def _matched(response: list = CREATE_RESPONSE, listed: tuple = ()) -> list[dict]:
|
||||
def _matched(response: list = CREATE_RESPONSE, listed: tuple = HELD) -> list[dict]:
|
||||
return _fact(
|
||||
"04_manage_user.yml",
|
||||
"🔍 Match every account to its Stalwart id",
|
||||
@@ -209,10 +232,73 @@ class TestAccountPhases(unittest.TestCase):
|
||||
|
||||
def test_an_existing_account_without_an_id_fails(self):
|
||||
response = [
|
||||
["x:Account/set", {"notCreated": {"u0": {"type": "alreadyExists"}}}, "c0"]
|
||||
[
|
||||
"x:Account/set",
|
||||
{"notCreated": {"u0": {"type": "primaryKeyViolation"}}},
|
||||
"c0",
|
||||
]
|
||||
]
|
||||
refused = _refused(_matched(response, listed=()))
|
||||
self.assertIn("administrator@infinito.test", refused)
|
||||
|
||||
def test_a_primary_key_violation_on_the_listed_account_is_existing(self):
|
||||
accounts = {a["key"]: a for a in _matched()}
|
||||
self.assertEqual(
|
||||
{key: (a["id"], a["existing"]) for key, a in accounts.items()},
|
||||
{
|
||||
"administrator": ("a", False),
|
||||
"no-reply": ("b", True),
|
||||
"contact": ("c", True),
|
||||
},
|
||||
)
|
||||
self.assertEqual(_refused(list(accounts.values())), {})
|
||||
|
||||
def test_a_violation_against_another_object_is_refused(self):
|
||||
alias = _violation("a")
|
||||
mailing_list = _violation("c", "MailingList")
|
||||
response = [
|
||||
[
|
||||
"x:Account/set",
|
||||
{
|
||||
"created": {"u0": {"id": "a"}},
|
||||
"notCreated": {"u1": alias, "u2": mailing_list},
|
||||
},
|
||||
"c0",
|
||||
]
|
||||
]
|
||||
accounts = _matched(response)
|
||||
self.assertEqual([a["existing"] for a in accounts], [False, False, False])
|
||||
self.assertEqual(
|
||||
_refused(accounts),
|
||||
{"no-reply@infinito.test": alias, "contact@infinito.test": mailing_list},
|
||||
)
|
||||
|
||||
def test_an_unknown_refusal_of_a_listed_bot_fails_without_sso(self):
|
||||
refusal = {"type": "invalidProperties", "properties": ["description"]}
|
||||
response = [
|
||||
[
|
||||
"x:Account/set",
|
||||
{"notCreated": {"u0": _violation("a"), "u1": refusal, "u2": refusal}},
|
||||
"c0",
|
||||
]
|
||||
]
|
||||
refused = _refused(_matched(response))
|
||||
self.assertIn("administrator@infinito.test", refused)
|
||||
self.assertEqual(
|
||||
refused,
|
||||
{"no-reply@infinito.test": refusal, "contact@infinito.test": refusal},
|
||||
)
|
||||
|
||||
def test_an_unknown_refusal_of_a_listed_bot_fails_under_sso(self):
|
||||
refusal = {"type": "invalidProperties", "properties": ["description"]}
|
||||
response = [
|
||||
[
|
||||
"x:Account/set",
|
||||
{"notCreated": {"u0": EXTERNAL, "u1": refusal, "u2": EXTERNAL}},
|
||||
"c0",
|
||||
]
|
||||
]
|
||||
refused = _refused(_matched(response), sso=True)
|
||||
self.assertEqual(refused, {"no-reply@infinito.test": refusal})
|
||||
|
||||
def test_accounts_of_an_external_directory_take_their_listed_ids(self):
|
||||
listed = (
|
||||
@@ -271,9 +357,65 @@ class TestAccountPhases(unittest.TestCase):
|
||||
)
|
||||
method, arguments, _ = body["methodCalls"][0]
|
||||
self.assertEqual(method, "x:Account/set")
|
||||
self.assertEqual(sorted(arguments["update"]), ["b", "c"])
|
||||
self.assertEqual(
|
||||
arguments["update"]["c"]["credentials"]["0"]["secret"], _sha("contact-pw")
|
||||
arguments["update"],
|
||||
{
|
||||
"b": {"credentials/0/secret": _sha("no-reply-pw")},
|
||||
"c": {"credentials/0/secret": _sha("contact-pw")},
|
||||
},
|
||||
)
|
||||
|
||||
def _update_of_contact(self, credentials: dict) -> dict:
|
||||
listed = (*HELD[:2], _held("c", "contact", credentials))
|
||||
body = _body(
|
||||
"04_manage_user.yml",
|
||||
"🔄 Send the passwords of the pre-existing accounts",
|
||||
**BASE,
|
||||
stalwart_accounts=_matched(listed=listed),
|
||||
)
|
||||
return body["methodCalls"][0][1]["update"]["c"]
|
||||
|
||||
def test_the_patch_keeps_the_other_credentials_of_the_account(self):
|
||||
update = self._update_of_contact({"0": APP_PASSWORD, "3": PASSWORD})
|
||||
self.assertEqual(update, {"credentials/3/secret": _sha("contact-pw")})
|
||||
|
||||
def test_an_account_without_a_password_gets_one_at_a_free_key(self):
|
||||
self.assertEqual(
|
||||
self._update_of_contact({"0": APP_PASSWORD, "1": APP_PASSWORD}),
|
||||
{"credentials/2/secret": _sha("contact-pw")},
|
||||
)
|
||||
self.assertEqual(
|
||||
self._update_of_contact({}), {"credentials/0/secret": _sha("contact-pw")}
|
||||
)
|
||||
|
||||
def test_one_store_write_carries_every_bot(self):
|
||||
task = _task(
|
||||
"04_manage_user.yml",
|
||||
"💾 Persist the Stalwart submission credential of every bot",
|
||||
)
|
||||
self.assertEqual(
|
||||
task["ansible.builtin.include_role"]["tasks_from"], "write_many.yml"
|
||||
)
|
||||
self.assertNotIn("loop", task)
|
||||
entries = _render(
|
||||
task["vars"]["sys_token_store_entries"],
|
||||
**BASE,
|
||||
stalwart_accounts=_matched(),
|
||||
)
|
||||
self.assertEqual(
|
||||
entries,
|
||||
[
|
||||
{
|
||||
"user_key": "no-reply",
|
||||
"app": "web-app-stalwart",
|
||||
"token": _sha("no-reply-pw"),
|
||||
},
|
||||
{
|
||||
"user_key": "contact",
|
||||
"app": "web-app-stalwart",
|
||||
"token": _sha("contact-pw"),
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
def test_a_refused_update_fails_naming_the_account(self):
|
||||
@@ -493,6 +635,31 @@ class TestAppPasswordPhases(unittest.TestCase):
|
||||
],
|
||||
)
|
||||
|
||||
def test_the_minted_passwords_land_in_one_store_write(self):
|
||||
task = _task(
|
||||
"04_manage_user/app_password.yml", "💾 Persist the new app passwords"
|
||||
)
|
||||
self.assertEqual(
|
||||
task["ansible.builtin.include_role"]["tasks_from"], "write_many.yml"
|
||||
)
|
||||
self.assertNotIn("loop", task)
|
||||
minted = [
|
||||
{"key": "no-reply", "address": "no-reply@infinito.test", "secret": "p1"},
|
||||
{"key": "contact", "address": "contact@infinito.test", "secret": "p2"},
|
||||
]
|
||||
entries = _render(
|
||||
task["vars"]["sys_token_store_entries"],
|
||||
**BASE,
|
||||
stalwart_app_password_minted=minted,
|
||||
)
|
||||
self.assertEqual(
|
||||
entries,
|
||||
[
|
||||
{"user_key": "no-reply", "app": "web-app-stalwart", "token": "p1"},
|
||||
{"user_key": "contact", "app": "web-app-stalwart", "token": "p2"},
|
||||
],
|
||||
)
|
||||
|
||||
def test_the_record_keeps_the_owners_of_an_earlier_chunk(self):
|
||||
stale = _fact(
|
||||
"04_manage_user/app_password.yml",
|
||||
|
||||
Reference in new issue
Block a user