diff --git a/roles/web-app-gitea/files/sql/auth-source-users.sql b/roles/web-app-gitea/files/sql/auth-source-users.sql new file mode 100644 index 0000000000..a9f11756b1 --- /dev/null +++ b/roles/web-app-gitea/files/sql/auth-source-users.sql @@ -0,0 +1,5 @@ +SELECT s.id, + (SELECT COUNT(*) FROM `user` u WHERE u.login_source = s.id) + + (SELECT COUNT(*) FROM external_login_user e WHERE e.login_source_id = s.id) +FROM login_source s +ORDER BY s.id; diff --git a/roles/web-app-gitea/filter_plugins/gitea_ldap_source_plan.py b/roles/web-app-gitea/filter_plugins/gitea_ldap_source_plan.py new file mode 100644 index 0000000000..916cb95619 --- /dev/null +++ b/roles/web-app-gitea/filter_plugins/gitea_ldap_source_plan.py @@ -0,0 +1,58 @@ +from __future__ import annotations + +from ansible.errors import AnsibleFilterError + + +def gitea_ldap_source_plan(source_ids, user_rows): + """Pick the one LDAP auth source to keep and the duplicates to delete. + + Args: + source_ids: IDs of every auth source of the role's LDAP type, as + ``gitea admin auth list`` prints them. + user_rows: ``[id, linked]`` rows of ``files/sql/auth-source-users.sql``, + where ``linked`` counts the users and external logins Gitea checks + before it deletes a source. Only read when ``source_ids`` holds + more than one ID. + + Returns: + ``{"keep": id, "delete": [ids]}``. ``keep`` is ``""`` when no source + exists, the only source when there is one, else the one source users + are linked to, else the lowest ID. + + Raises: + AnsibleFilterError: a listed source has no row in ``user_rows``, or + users are linked to more than one source. + """ + ids = sorted({str(source_id).strip() for source_id in source_ids} - {""}, key=int) + if len(ids) < 2: + return {"keep": ids[0] if ids else "", "delete": []} + linked = {str(row[0]).strip(): int(row[1]) for row in user_rows} + missing = [source_id for source_id in ids if source_id not in linked] + if missing: + raise AnsibleFilterError( + f"gitea_ldap_source_plan: the database holds no login_source row for " + f"the listed LDAP source id(s) {', '.join(missing)}; the query did not " + f"reach Gitea's database, so no source is deleted" + ) + in_use = [source_id for source_id in ids if linked[source_id] > 0] + if len(in_use) > 1: + counts = ", ".join( + f"id {source_id}: {linked[source_id]} linked" for source_id in in_use + ) + raise AnsibleFilterError( + f"gitea_ldap_source_plan: users are linked to {len(in_use)} LDAP auth " + f"sources ({counts}). Gitea refuses to delete a source in use and the " + f"role keeps exactly one, so move every user of all but one of them to " + f"the remaining source (Site Administration > User Accounts > Edit > " + f"Authentication Source), then deploy again" + ) + keep = in_use[0] if in_use else ids[0] + return { + "keep": keep, + "delete": [source_id for source_id in ids if source_id != keep], + } + + +class FilterModule: + def filters(self): + return {"gitea_ldap_source_plan": gitea_ldap_source_plan} diff --git a/roles/web-app-gitea/tasks/cleanup/ldap.yml b/roles/web-app-gitea/tasks/cleanup/ldap.yml index 35211d8676..0281b59977 100644 --- a/roles/web-app-gitea/tasks/cleanup/ldap.yml +++ b/roles/web-app-gitea/tasks/cleanup/ldap.yml @@ -1,17 +1,17 @@ --- -- name: "🔍 Lookup existing LDAP auth source ID" +- name: "🔍 Lookup existing LDAP auth source IDs" shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated at the include site container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ gitea admin auth list \ - | awk -v name="LDAP ({{ SOFTWARE_NAME }})" '$0 ~ name {print $1; exit}' - register: ldap_source_id_raw + | awk -F '\t+' -v source_type={{ GITEA_LDAP_SOURCE_TYPE | quote }} 'NR > 1 && $3 == source_type {print $1}' + register: ldap_source_ids_raw failed_when: false changed_when: false -- name: "🗑️ Delete existing LDAP auth source if present" +- name: "🗑️ Delete existing LDAP auth sources" shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated at the include site container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ - gitea admin auth delete --id {{ ldap_source_id_raw.stdout }} - when: ldap_source_id_raw.stdout != "" + gitea admin auth delete --id {{ item | quote }} + loop: "{{ ldap_source_ids_raw.stdout_lines }}" register: ldap_delete failed_when: ldap_delete.rc != 0 diff --git a/roles/web-app-gitea/tasks/setup/ldap.yml b/roles/web-app-gitea/tasks/setup/ldap.yml index 2ee7bb1639..d8ff6f5959 100644 --- a/roles/web-app-gitea/tasks/setup/ldap.yml +++ b/roles/web-app-gitea/tasks/setup/ldap.yml @@ -1,37 +1,65 @@ --- -- name: "➕ Add LDAP Authentication Source" +- name: "🔍 Lookup existing LDAP auth source IDs" + shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated to the hosting node + set -o pipefail && container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ + gitea admin auth list \ + | awk -F '\t+' -v source_type={{ GITEA_LDAP_SOURCE_TYPE | quote }} 'NR > 1 && $3 == source_type {print $1}' + delegate_to: "{{ GITEA_HOST_NODE }}" + register: ldap_source_ids_raw + failed_when: ldap_source_ids_raw.rc != 0 + changed_when: false + +- name: "🔍 Count the users linked to each auth source" + database_query: + config: "{{ lookup('database', application_id) }}" + query_file: "{{ role_path }}/files/sql/auth-source-users.sql" + expect_rows: true + mask_values: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + register: ldap_source_users + changed_when: false + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + when: ldap_source_ids_raw.stdout_lines | length > 1 + +- name: "🧭 Pick the LDAP auth source to keep" + set_fact: + ldap_source_plan: "{{ ldap_source_ids_raw.stdout_lines | gitea_ldap_source_plan(ldap_source_users.rows if ldap_source_ids_raw.stdout_lines | length > 1 else []) }}" + +- name: "🗑️ Delete the duplicate LDAP auth sources no user is linked to" shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated to the hosting node container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ - gitea admin auth add-ldap \ - {{ GITEA_LDAP_AUTH_ARGS | join(' ') }} + gitea admin auth delete --id {{ item | quote }} + loop: "{{ ldap_source_plan.delete }}" + delegate_to: "{{ GITEA_HOST_NODE }}" + register: ldap_delete + failed_when: ldap_delete.rc != 0 + +- name: "➕ Add LDAP Authentication Source" + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" + shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated to the hosting node + IFS= read -r GITEA_LDAP_BIND_PASSWORD && export GITEA_LDAP_BIND_PASSWORD && container exec -i -e GITEA_LDAP_BIND_PASSWORD --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} sh -lc ' + : "${GITEA_LDAP_BIND_PASSWORD:?arrived empty on stdin}" && gitea admin auth add-ldap \ + --bind-password "$GITEA_LDAP_BIND_PASSWORD" \ + {{ GITEA_LDAP_AUTH_ARGS | join(' ') }} + ' + args: + stdin: "{{ LDAP.BIND_CREDENTIAL }}" delegate_to: "{{ GITEA_HOST_NODE }}" register: ldap_manage failed_when: ldap_manage.rc != 0 and not (ldap_manage.stderr | gitea_auth_noop) - -- name: "🔍 Lookup existing LDAP auth source ID" - shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated to the hosting node - container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ - gitea admin auth list \ - | tail -n +2 \ - | grep -F "LDAP ({{ DOMAIN_PRIMARY }})" \ - | awk '{print $1; exit}' - delegate_to: "{{ GITEA_HOST_NODE }}" - register: ldap_source_id_raw - failed_when: - - ldap_source_id_raw.rc != 0 - - ldap_source_id_raw.stdout == "" - changed_when: false - -- name: "🔧 Set LDAP source ID fact" - set_fact: - ldap_source_id: "{{ ldap_source_id_raw.stdout }}" + when: ldap_source_plan.keep == "" - name: "🔄 Update LDAP Authentication Source" + no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}" shell: | # nocheck: container-exec-resolver runtime-resolved CID, delegated to the hosting node - container exec -i --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} \ - gitea admin auth update-ldap \ - --id {{ ldap_source_id }} \ - {{ GITEA_LDAP_AUTH_ARGS | join(' ') }} + IFS= read -r GITEA_LDAP_BIND_PASSWORD && export GITEA_LDAP_BIND_PASSWORD && container exec -i -e GITEA_LDAP_BIND_PASSWORD --user {{ GITEA_USER }} {{ GITEA_LOCAL_CID | quote }} sh -lc ' + : "${GITEA_LDAP_BIND_PASSWORD:?arrived empty on stdin}" && gitea admin auth update-ldap \ + --id {{ ldap_source_plan.keep }} \ + --bind-password "$GITEA_LDAP_BIND_PASSWORD" \ + {{ GITEA_LDAP_AUTH_ARGS | join(' ') }} + ' + args: + stdin: "{{ LDAP.BIND_CREDENTIAL }}" delegate_to: "{{ GITEA_HOST_NODE }}" register: ldap_manage failed_when: ldap_manage.rc != 0 + when: ldap_source_plan.keep != "" diff --git a/roles/web-app-gitea/vars/main.yml b/roles/web-app-gitea/vars/main.yml index 2723df3ea3..4016eae3a6 100644 --- a/roles/web-app-gitea/vars/main.yml +++ b/roles/web-app-gitea/vars/main.yml @@ -2,13 +2,14 @@ application_id: "web-app-gitea" entity_name: "{{ application_id | entity_name }}" +GITEA_LDAP_SOURCE_NAME: "LDAP" +GITEA_LDAP_SOURCE_TYPE: "LDAP (via BindDN)" GITEA_LDAP_AUTH_ARGS: - - '--name "LDAP ({{ DOMAIN_PRIMARY }})"' + - '--name "{{ GITEA_LDAP_SOURCE_NAME }}"' - '--host "{{ LDAP.SERVER.DOMAIN }}"' - "--port {{ LDAP.SERVER.PORT }}" - '--security-protocol "{{ LDAP.SERVER.SECURITY | trim or "unencrypted" }}"' - '--bind-dn "{{ LDAP.DN.ADMINISTRATOR.DATA }}"' - - '--bind-password "{{ LDAP.BIND_CREDENTIAL }}"' - '--user-search-base "{{ LDAP.DN.OU.USERS }}"' - '--user-filter "(&(objectClass=inetOrgPerson)(uid=%s))"' - '--admin-filter "(memberOf=cn={{ application_id }}-administrator,{{ LDAP.DN.OU.ROLES }})"' diff --git a/tests/unit/python/roles/web-app-gitea/test_ldap_source.py b/tests/unit/python/roles/web-app-gitea/test_ldap_source.py new file mode 100644 index 0000000000..f5dd9f6231 --- /dev/null +++ b/tests/unit/python/roles/web-app-gitea/test_ldap_source.py @@ -0,0 +1,290 @@ +from __future__ import annotations + +import importlib.util +import re +import shutil +import subprocess +import unittest + +from ansible.errors import AnsibleFilterError + +from utils.cache.files import PROJECT_ROOT, read_text +from utils.cache.yaml import load_yaml_str +from utils.roles.mapping import ROLE_FILE_VARS_MAIN + +APP = "web-app-gitea" +ROLE_DIR = PROJECT_ROOT / "roles" / APP +SETUP_TASKS = ROLE_DIR / "tasks" / "setup" / "ldap.yml" +CLEANUP_TASKS = ROLE_DIR / "tasks" / "cleanup" / "ldap.yml" +USERS_SQL = ROLE_DIR / "files" / "sql" / "auth-source-users.sql" +PLUGIN = ROLE_DIR / "filter_plugins" / "gitea_ldap_source_plan.py" +AWK_LOOKUP = re.compile( + r"awk -F '(?P[^']+)' -v (?P\w+)=\{\{ GITEA_LDAP_SOURCE_TYPE \| quote \}\} " + r"'(?P[^']+)'" +) +PROVIDER_COLUMN_WIDTH = 25 +NAME_COLUMN_WIDTH = 255 +HEADER = "ID\tName\t\t\t\tType\t\t\tEnabled\n" +OAUTH2_ROW = "4\tSSO Login (INFINITO.NEXUS)\tOAuth2\t\t\ttrue\n" +PRODUCTION_LISTING = ( + HEADER + + "3\tLDAP (infinito.nexus)\t\tLDAP (via BindDN)\ttrue\n" + + "1\tLDAP (Infinito.Nexus)\t\tLDAP (via BindDN)\ttrue\n" + + OAUTH2_ROW +) +LOOKALIKE_LISTING = ( + HEADER + + "2\tLDAP (example.org)\t\tLDAP (simple auth)\ttrue\n" + + "5\tLDAP (via BindDN)\t\tOAuth2\t\t\ttrue\n" +) + +_spec = importlib.util.spec_from_file_location("gitea_ldap_source_plan", PLUGIN) +_mod = importlib.util.module_from_spec(_spec) +assert _spec.loader is not None +_spec.loader.exec_module(_mod) +gitea_ldap_source_plan = _mod.gitea_ldap_source_plan + + +def role_vars() -> dict: + return load_yaml_str(read_text(str(ROLE_DIR / ROLE_FILE_VARS_MAIN))) + + +def awk_lookup(task_path) -> re.Match[str]: + match = AWK_LOOKUP.search(read_text(str(task_path))) + if match is None: + raise AssertionError( + f"{task_path} looks the LDAP source up without awk by type" + ) + return match + + +def run_awk(listing: str, *args: str) -> list[str]: + return subprocess.run( + [shutil.which("awk"), *args], + input=listing, + capture_output=True, + text=True, + check=True, + ).stdout.split() + + +def lookup(task_path, listing: str) -> list[str]: + """Run the awk lookup of ``task_path`` on ``listing``. + + Args: + task_path: the task file that carries the lookup. + listing: what ``gitea admin auth list`` prints. + + Returns: + The IDs the lookup prints. + """ + match = awk_lookup(task_path) + source_type = role_vars()["GITEA_LDAP_SOURCE_TYPE"] + return run_awk( + listing, + "-F", + match["sep"], + "-v", + f"{match['var']}={source_type}", + match["program"], + ) + + +def converge(listing: str, linked: dict[str, int]) -> dict[str, str]: + """Apply the setup's lookup, plan, delete and add or update to ``listing``. + + Args: + listing: what ``gitea admin auth list`` prints. + linked: users linked to each source ID of ``listing``. + + Returns: + The LDAP sources after the setup ran, by ID. + """ + source_type = role_vars()["GITEA_LDAP_SOURCE_TYPE"] + name = role_vars()["GITEA_LDAP_SOURCE_NAME"] + rows = [re.split(r"\t+", line) for line in listing.splitlines()[1:]] + ids = lookup(SETUP_TASKS, listing) + user_rows = [[row[0], str(linked[row[0]])] for row in rows] if len(ids) > 1 else [] + plan = gitea_ldap_source_plan(ids, user_rows) + sources = {row[0]: row[1] for row in rows if row[2] == source_type} + for source_id in plan["delete"]: + if linked[source_id]: + raise AssertionError(f"Gitea refuses to delete source {source_id} in use") + del sources[source_id] + if plan["keep"]: + sources[plan["keep"]] = name + else: + sources[str(max([0, *(int(row[0]) for row in rows)]) + 1)] = name + return sources + + +class TestLdapSourceLookup(unittest.TestCase): + def test_one_source_is_found(self) -> None: + listing = HEADER + "1\tLDAP\t\tLDAP (via BindDN)\ttrue\n" + OAUTH2_ROW + self.assertEqual(lookup(SETUP_TASKS, listing), ["1"]) + + def test_both_duplicates_are_found(self) -> None: + self.assertEqual(lookup(SETUP_TASKS, PRODUCTION_LISTING), ["3", "1"]) + + def test_no_source_yields_no_id(self) -> None: + self.assertEqual(lookup(SETUP_TASKS, HEADER + OAUTH2_ROW), []) + self.assertEqual(lookup(SETUP_TASKS, HEADER), []) + + def test_simple_auth_and_a_lookalike_name_are_not_the_role_source(self) -> None: + self.assertEqual(lookup(SETUP_TASKS, LOOKALIKE_LISTING), []) + + def test_cleanup_finds_the_sources_by_type(self) -> None: + self.assertEqual(lookup(CLEANUP_TASKS, PRODUCTION_LISTING), ["3", "1"]) + self.assertEqual(lookup(CLEANUP_TASKS, LOOKALIKE_LISTING), []) + self.assertEqual( + awk_lookup(CLEANUP_TASKS).groupdict(), awk_lookup(SETUP_TASKS).groupdict() + ) + + def test_the_former_name_lookups_miss_the_duplicate(self) -> None: + self.assertEqual( + [ + line.partition("\t")[0] + for line in PRODUCTION_LISTING.splitlines()[1:] + if "LDAP (infinito.nexus)" in line + ], + ["3"], + ) + self.assertEqual( + run_awk( + PRODUCTION_LISTING, + "-v", + "name=LDAP (Infinito.Nexus)", + "$0 ~ name {print $1; exit}", + ), + [], + ) + + +class TestLdapSourcePlan(unittest.TestCase): + def test_no_source_is_added(self) -> None: + self.assertEqual(gitea_ldap_source_plan([], []), {"keep": "", "delete": []}) + + def test_a_single_source_is_updated_without_counting(self) -> None: + self.assertEqual(gitea_ldap_source_plan(["3"], []), {"keep": "3", "delete": []}) + + def test_the_source_users_are_linked_to_is_kept(self) -> None: + rows = [["1", "0"], ["3", "7"], ["4", "2"]] + self.assertEqual( + gitea_ldap_source_plan(["3", "1"], rows), {"keep": "3", "delete": ["1"]} + ) + rows = [["1", "7"], ["3", "0"], ["4", "2"]] + self.assertEqual( + gitea_ldap_source_plan(["3", "1"], rows), {"keep": "1", "delete": ["3"]} + ) + + def test_without_users_the_lowest_id_is_kept(self) -> None: + rows = [["1", "0"], ["3", "0"], ["4", "2"]] + self.assertEqual( + gitea_ldap_source_plan(["3", "1", "10"], [*rows, ["10", "0"]]), + {"keep": "1", "delete": ["3", "10"]}, + ) + + def test_users_on_two_sources_fail_loud(self) -> None: + rows = [["1", "4"], ["3", "1"]] + with self.assertRaisesRegex( + AnsibleFilterError, "id 1: 4 linked, id 3: 1 linked" + ): + gitea_ldap_source_plan(["3", "1"], rows) + with self.assertRaisesRegex(AnsibleFilterError, "move every user"): + gitea_ldap_source_plan(["3", "1"], rows) + + def test_a_source_the_database_does_not_know_fails_loud(self) -> None: + with self.assertRaisesRegex(AnsibleFilterError, "no login_source row"): + gitea_ldap_source_plan(["3", "1"], [["1", "0"]]) + with self.assertRaisesRegex(AnsibleFilterError, "no login_source row"): + gitea_ldap_source_plan(["3", "1"], []) + + +class TestLdapSourceConvergence(unittest.TestCase): + def test_duplicates_with_users_on_one_converge_to_that_source(self) -> None: + self.assertEqual( + converge(PRODUCTION_LISTING, {"1": 12, "3": 0, "4": 3}), + {"1": role_vars()["GITEA_LDAP_SOURCE_NAME"]}, + ) + + def test_duplicates_without_users_converge_to_the_lowest_id(self) -> None: + self.assertEqual( + converge(PRODUCTION_LISTING, {"1": 0, "3": 0, "4": 0}), + {"1": role_vars()["GITEA_LDAP_SOURCE_NAME"]}, + ) + + def test_duplicates_with_users_on_both_are_left_alone(self) -> None: + with self.assertRaises(AnsibleFilterError): + converge(PRODUCTION_LISTING, {"1": 12, "3": 2, "4": 3}) + + def test_a_fresh_instance_gets_one_source(self) -> None: + self.assertEqual( + converge(HEADER + OAUTH2_ROW, {"4": 0}), + {"5": role_vars()["GITEA_LDAP_SOURCE_NAME"]}, + ) + + def test_a_converged_instance_stays_at_one_source(self) -> None: + name = role_vars()["GITEA_LDAP_SOURCE_NAME"] + listing = HEADER + f"1\t{name}\t\tLDAP (via BindDN)\ttrue\n" + OAUTH2_ROW + self.assertEqual(converge(listing, {"1": 12, "4": 3}), {"1": name}) + + +class TestLdapSourceDefinition(unittest.TestCase): + def test_one_fixed_short_name_is_defined_once(self) -> None: + values = role_vars() + name = values["GITEA_LDAP_SOURCE_NAME"] + self.assertNotIn("{{", name) + self.assertLessEqual(len(name), PROVIDER_COLUMN_WIDTH) + self.assertLessEqual(len(name), NAME_COLUMN_WIDTH) + self.assertEqual( + values["GITEA_LDAP_AUTH_ARGS"][0], '--name "{{ GITEA_LDAP_SOURCE_NAME }}"' + ) + + def test_the_count_matches_the_in_use_check_of_gitea(self) -> None: + sql = read_text(str(USERS_SQL)) + self.assertIn("u.login_source = s.id", sql) + self.assertIn("e.login_source_id = s.id", sql) + self.assertTrue(sql.lstrip().upper().startswith("SELECT ")) + + +class TestLdapBindPasswordOffArgv(unittest.TestCase): + def _manage_tasks(self) -> list[dict]: + tasks = load_yaml_str(read_text(str(SETUP_TASKS))) + return [ + task + for task in tasks + if "add-ldap" in task.get("shell", "") + or "update-ldap" in task.get("shell", "") + ] + + def test_the_shared_args_carry_no_password(self) -> None: + self.assertFalse( + any("bind-password" in arg for arg in role_vars()["GITEA_LDAP_AUTH_ARGS"]) + ) + + def test_add_and_update_read_the_password_from_stdin(self) -> None: + tasks = self._manage_tasks() + self.assertEqual(len(tasks), 2) + for task in tasks: + self.assertEqual(task["args"]["stdin"], "{{ LDAP.BIND_CREDENTIAL }}") + self.assertEqual(task["no_log"], "{{ MASK_CREDENTIALS_IN_LOGS | bool }}") + self.assertNotIn("BIND_CREDENTIAL", task["shell"]) + self.assertNotIn("environment", task) + self.assertIn("IFS= read -r GITEA_LDAP_BIND_PASSWORD", task["shell"]) + self.assertIn("-e GITEA_LDAP_BIND_PASSWORD", task["shell"]) + self.assertIn('--bind-password "$GITEA_LDAP_BIND_PASSWORD"', task["shell"]) + + def test_the_password_is_only_expanded_by_the_inner_shell(self) -> None: + for task in self._manage_tasks(): + host, inner = task["shell"].split("sh -lc '", 1) + self.assertNotIn("$GITEA_LDAP_BIND_PASSWORD", host) + self.assertNotIn("${GITEA_LDAP_BIND_PASSWORD", host) + self.assertIn('"$GITEA_LDAP_BIND_PASSWORD"', inner) + self.assertIn('"${GITEA_LDAP_BIND_PASSWORD:?', inner) + + def test_the_shared_args_keep_the_inner_quoting_intact(self) -> None: + self.assertFalse(any("'" in arg for arg in role_vars()["GITEA_LDAP_AUTH_ARGS"])) + + +if __name__ == "__main__": + unittest.main()