diff --git a/roles/authentik/tasks/main.yml b/roles/authentik/tasks/main.yml index 1471836..1947fc4 100644 --- a/roles/authentik/tasks/main.yml +++ b/roles/authentik/tasks/main.yml @@ -49,22 +49,32 @@ until: blueprint_wait_result.rc == 0 when: blueprints_changed -- name: Render LDAP outpost token script +# Pin known tokens on the LDAP outpost and on any proxy outpost that +# declares one, so co-located outposts (e.g. a proxy outpost on another +# host) can authenticate against the authentik server with the token from +# the vault instead of the auto-generated one the blueprint creates. +- name: Set fact whether any outpost needs a pinned token + ansible.builtin.set_fact: + _authentik_outpost_tokens: >- + {{ (authentik_ldap_outpost.name is defined) + or (authentik_proxy_outposts | default([]) | selectattr('token', 'defined') | list | length > 0) }} + +- name: Render outpost token script template: src: set-outpost-token.py.j2 dest: "{{ authentik_docker_volume_dir }}/data/set-outpost-token.py" mode: '0644' - when: authentik_ldap_outpost.name is defined - register: ldap_token_script + when: _authentik_outpost_tokens | bool + register: outpost_token_script -- name: Set known token for LDAP outpost +- name: Set known tokens for outposts community.docker.docker_compose_v2_exec: project_src: "{{ authentik_docker_compose_dir }}" service: server command: ak shell -c "exec(open('/data/set-outpost-token.py').read())" - register: ldap_token_result - changed_when: "'changed' in ldap_token_result.stdout" + register: outpost_token_result + changed_when: "'changed' in outpost_token_result.stdout" retries: 30 delay: 10 - until: ldap_token_result.rc == 0 - when: authentik_ldap_outpost.name is defined and (blueprints_changed or ldap_token_script.changed) \ No newline at end of file + until: outpost_token_result.rc == 0 + when: (_authentik_outpost_tokens | bool) and (blueprints_changed or outpost_token_script.changed) \ No newline at end of file diff --git a/roles/authentik/templates/docker-compose.yml.j2 b/roles/authentik/templates/docker-compose.yml.j2 index cd3ef1e..ce427f2 100644 --- a/roles/authentik/templates/docker-compose.yml.j2 +++ b/roles/authentik/templates/docker-compose.yml.j2 @@ -23,7 +23,9 @@ services: command: server healthcheck: test: ["CMD", "ak", "healthcheck"] - start_period: 30s + # Cold first boot runs DB migrations + app init (~90s observed); + # start_period must cover that or compose --wait trips unhealthy. + start_period: 120s interval: 10s retries: 5 timeout: 5s diff --git a/roles/authentik/templates/set-outpost-token.py.j2 b/roles/authentik/templates/set-outpost-token.py.j2 index 0b61705..ffaa56b 100644 --- a/roles/authentik/templates/set-outpost-token.py.j2 +++ b/roles/authentik/templates/set-outpost-token.py.j2 @@ -1,10 +1,26 @@ from authentik.outposts.models import Outpost from authentik.core.models import Token -o = Outpost.objects.get(name='{{ authentik_ldap_outpost.name }}') -t = Token.objects.get(identifier=o.token_identifier) -if t.key != '{{ authentik_ldap_outpost.token }}': - t.key = '{{ authentik_ldap_outpost.token }}' - t.save(update_fields=['key']) - print('changed') -else: - print('ok') + +# (outpost name, desired token key) for every outpost we pin a known +# token on: the LDAP outpost plus any proxy outpost that declares a +# `token` (co-located proxy outposts authenticate with it against the +# authentik server). +_targets = [ +{% if authentik_ldap_outpost.name is defined %} + ('{{ authentik_ldap_outpost.name }}', '{{ authentik_ldap_outpost.token }}'), +{% endif %} +{% for o in authentik_proxy_outposts | default([]) if o.token is defined %} + ('{{ o.name }}', '{{ o.token }}'), +{% endfor %} +] + +changed = False +for name, key in _targets: + o = Outpost.objects.get(name=name) + t = Token.objects.get(identifier=o.token_identifier) + if t.key != key: + t.key = key + t.save(update_fields=['key']) + changed = True + +print('changed' if changed else 'ok')