From 61216d51cc70eb13bcb1ac8a97689eb9319b311d Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 9 Jan 2024 11:26:46 +0200 Subject: [PATCH] Move matrix-ldap-registration-proxy to its own container network and add native Traefik support This also makes it handle the `/_matrix/client/v3/register` endpoint, not just `/_matrix/client/r0/register` --- group_vars/matrix_servers | 25 +++++++++ .../defaults/main.yml | 56 ++++++++++++++----- .../tasks/inject_into_nginx_proxy.yml | 44 --------------- .../tasks/main.yml | 9 --- .../tasks/setup_install.yml | 15 +++++ .../tasks/validate_config.yml | 16 +++++- .../templates/labels.j2 | 39 +++++++++++++ .../templates/ldap-registration-proxy.env.j2 | 2 +- .../matrix-ldap-registration-proxy.service.j2 | 15 ++++- 9 files changed, 150 insertions(+), 71 deletions(-) delete mode 100644 roles/custom/matrix-ldap-registration-proxy/tasks/inject_into_nginx_proxy.yml create mode 100644 roles/custom/matrix-ldap-registration-proxy/templates/labels.j2 diff --git a/group_vars/matrix_servers b/group_vars/matrix_servers index d5ec0dabd..254d70ee0 100755 --- a/group_vars/matrix_servers +++ b/group_vars/matrix_servers @@ -3052,6 +3052,31 @@ jitsi_disable_gravatar: true # This is only for users with a specific LDAP setup matrix_ldap_registration_proxy_enabled: false +matrix_ldap_registration_proxy_hostname: "{{ matrix_server_fqn_matrix }}" + +matrix_ldap_registration_proxy_matrix_server_url: "{{ matrix_addons_homeserver_client_api_url }}" + +matrix_ldap_registration_proxy_systemd_required_services_list_auto: | + {{ + matrix_addons_homeserver_systemd_services_list + }} + +matrix_ldap_registration_proxy_container_network: "{{ matrix_addons_container_network }}" + +matrix_ldap_registration_proxy_container_additional_networks_auto: |- + {{ + ( + ([] if matrix_addons_homeserver_container_network == '' else [matrix_addons_homeserver_container_network]) + + + ([matrix_playbook_reverse_proxyable_services_additional_network] if (matrix_playbook_reverse_proxyable_services_additional_network and matrix_ldap_registration_proxy_container_labels_traefik_enabled) else []) + ) | unique + }} + +matrix_ldap_registration_proxy_container_labels_traefik_enabled: "{{ matrix_playbook_reverse_proxy_type in ['playbook-managed-traefik', 'other-traefik-container'] }}" +matrix_ldap_registration_proxy_container_labels_traefik_docker_network: "{{ matrix_playbook_reverse_proxyable_services_additional_network }}" +matrix_ldap_registration_proxy_container_labels_traefik_entrypoints: "{{ devture_traefik_entrypoint_primary }}" +matrix_ldap_registration_proxy_container_labels_traefik_tls_certResolver: "{{ devture_traefik_certResolver_primary }}" + ###################################################################### # # /matrix-ldap-registration-proxy diff --git a/roles/custom/matrix-ldap-registration-proxy/defaults/main.yml b/roles/custom/matrix-ldap-registration-proxy/defaults/main.yml index 7ee5a947b..8aced9ce0 100644 --- a/roles/custom/matrix-ldap-registration-proxy/defaults/main.yml +++ b/roles/custom/matrix-ldap-registration-proxy/defaults/main.yml @@ -4,6 +4,8 @@ matrix_ldap_registration_proxy_enabled: true +matrix_ldap_registration_proxy_hostname: '' + matrix_ldap_registration_proxy_docker_image: matrix_ldap_registration_proxy matrix_ldap_registration_proxy_container_image_self_build_repo: "https://gitlab.com/activism.international/matrix_ldap_registration_proxy.git" matrix_ldap_registration_proxy_container_image_self_build_branch: "{{ matrix_ldap_registration_proxy_version }}" @@ -20,34 +22,62 @@ matrix_ldap_registration_proxy_ldap_base_dn: "" matrix_ldap_registration_proxy_ldap_user: "" matrix_ldap_registration_proxy_ldap_password: "" matrix_ldap_registration_proxy_matrix_server_name: "{{ matrix_domain }}" -matrix_ldap_registration_proxy_matrix_server_url: "https://{{ matrix_server_fqn_matrix }}" - -matrix_ldap_registration_proxy_registration_endpoint: "/_matrix/client/r0/register" +matrix_ldap_registration_proxy_matrix_server_url: "" # Controls whether the self-check feature should validate SSL certificates. matrix_matrix_ldap_registration_proxy_self_check_validate_certificates: true -matrix_ldap_registration_proxy_container_port: 8080 -# Controls whether the matrix_ldap_registration_proxy container exposes its HTTP port (tcp/{{ matrix_ldap_registration_proxy_container_port }} in the container). +matrix_ldap_registration_listen_port: 8080 + +# Controls whether the matrix_ldap_registration_proxy container exposes its HTTP port (tcp/{{ matrix_ldap_registration_listen_port }} in the container). # # Takes an ":" or "" value (e.g. "127.0.0.1:8080"), or empty string to not expose. matrix_ldap_registration_proxy_container_http_host_bind_port: '' -# `matrix_ldap_registration_proxy_container_http_host_bind_port_number_raw` contains the raw port number extracted from `matrix_ldap_registration_proxy_container_http_host_bind_port`, -# which can contain values like this: ('1234', '127.0.0.1:1234', '0.0.0.0:1234') -matrix_ldap_registration_proxy_container_http_host_bind_port_number_raw: "{{ '' if matrix_ldap_registration_proxy_container_http_host_bind_port == '' else (matrix_ldap_registration_proxy_container_http_host_bind_port.split(':')[1] if ':' in matrix_ldap_registration_proxy_container_http_host_bind_port else matrix_ldap_registration_proxy_container_http_host_bind_port) }}" +matrix_ldap_registration_proxy_container_network: "" -matrix_ldap_registration_proxy_registration_addr_with_container: "matrix-ldap_registration-proxy:{{ matrix_ldap_registration_proxy_container_http_host_bind_port_number_raw }}" -matrix_ldap_registration_proxy_registration_addr_sans_container: "127.0.0.1:{{ matrix_ldap_registration_proxy_container_http_host_bind_port_number_raw }}" +matrix_ldap_registration_proxy_container_additional_networks: "{{ matrix_ldap_registration_proxy_container_additional_networks_auto + matrix_ldap_registration_proxy_container_additional_networks_custom }}" +matrix_ldap_registration_proxy_container_additional_networks_auto: [] +matrix_ldap_registration_proxy_container_additional_networks_custom: [] +# matrix_ldap_registration_proxy_container_labels_traefik_enabled controls whether labels to assist a Traefik reverse-proxy will be attached to the container. +# See `../templates/labels.j2` for details. +# +# To inject your own other container labels, see `matrix_ldap_registration_proxy_container_labels_additional_labels`. +matrix_ldap_registration_proxy_container_labels_traefik_enabled: true +matrix_ldap_registration_proxy_container_labels_traefik_docker_network: "{{ matrix_ldap_registration_proxy_container_network }}" +matrix_ldap_registration_proxy_container_labels_traefik_entrypoints: web-secure +matrix_ldap_registration_proxy_container_labels_traefik_tls_certResolver: default # noqa var-naming + +# Controls whether labels will be added that expose ldap-registration-proxy's registration endpoint (matrix_ldap_registration_proxy_container_labels_registration_endpoint_path_prefix) +matrix_ldap_registration_proxy_container_labels_registration_endpoint_enabled: true +matrix_ldap_registration_proxy_container_labels_registration_endpoint_hostname: "{{ matrix_ldap_registration_proxy_hostname }}" +matrix_ldap_registration_proxy_container_labels_registration_endpoint_path_prefix: "/_matrix/client/(r0|v3)/register" +matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_rule: "Host(`{{ matrix_ldap_registration_proxy_container_labels_registration_endpoint_hostname }}`) && PathPrefix(`{{ matrix_ldap_registration_proxy_container_labels_registration_endpoint_path_prefix }}`)" +matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_priority: 0 +matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_entrypoints: "{{ matrix_ldap_registration_proxy_container_labels_traefik_entrypoints }}" +matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_tls: "{{ matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_entrypoints != 'web' }}" +matrix_ldap_registration_proxy_container_labels_registration_endpoint_traefik_tls_certResolver: "{{ matrix_ldap_registration_proxy_container_labels_traefik_tls_certResolver }}" # noqa var-naming + +# matrix_ldap_registration_proxy_container_labels_additional_labels contains a multiline string with additional labels to add to the container label file. +# See `../templates/labels.j2` for details. +# +# Example: +# matrix_ldap_registration_proxy_container_labels_additional_labels: | +# my.label=1 +# another.label="here" +matrix_ldap_registration_proxy_container_labels_additional_labels: '' # A list of extra arguments to pass to the container matrix_ldap_registration_proxy_container_extra_arguments: [] -# List of systemd services that matrix_ldap_registration_proxy.service depends on -matrix_ldap_registration_proxy_systemd_required_services_list: ['docker.service'] +# List of systemd services that matrix-ldap-registration-proxy.service depends on. +matrix_ldap_registration_proxy_systemd_required_services_list: "{{ matrix_ldap_registration_proxy_systemd_required_services_list_default + matrix_ldap_registration_proxy_systemd_required_services_list_auto + matrix_ldap_registration_proxy_systemd_required_services_list_custom }}" +matrix_ldap_registration_proxy_systemd_required_services_list_default: ['docker.service'] +matrix_ldap_registration_proxy_systemd_required_services_list_auto: [] +matrix_ldap_registration_proxy_systemd_required_services_list_custom: [] -# List of systemd services that matrix_ldap_registration_proxy.service wants +# List of systemd services that matrix-ldap-registration-proxy.service wants matrix_ldap_registration_proxy_systemd_wanted_services_list: [] # Additional environment variables to pass to the LDAP proxy environment variables. diff --git a/roles/custom/matrix-ldap-registration-proxy/tasks/inject_into_nginx_proxy.yml b/roles/custom/matrix-ldap-registration-proxy/tasks/inject_into_nginx_proxy.yml deleted file mode 100644 index 472503c9a..000000000 --- a/roles/custom/matrix-ldap-registration-proxy/tasks/inject_into_nginx_proxy.yml +++ /dev/null @@ -1,44 +0,0 @@ ---- - -- name: Fail if matrix-nginx-proxy role already executed - ansible.builtin.fail: - msg: >- - Trying to append Matrix LDAP registration proxy's reverse-proxying configuration to matrix-nginx-proxy, - but it's pointless since the matrix-nginx-proxy role had already executed. - To fix this, please change the order of roles in your playbook, - so that the matrix-nginx-proxy role would run after the matrix-bridge-mautrix-telegram role. - when: matrix_nginx_proxy_role_executed | default(False) | bool - -- name: Generate Matrix LDAP registration proxy proxying configuration for matrix-nginx-proxy - ansible.builtin.set_fact: - matrix_ldap_registration_proxy_matrix_nginx_proxy_configuration: | - location {{ matrix_ldap_registration_proxy_registration_endpoint }} { - {% if matrix_nginx_proxy_enabled | default(False) %} - {# Use the embedded DNS resolver in Docker containers to discover the service #} - resolver 127.0.0.11 valid=5s; - set $backend "{{ matrix_ldap_registration_proxy_registration_addr_with_container }}"; - proxy_pass http://$backend/register; - {% else %} - {# Generic configuration for use outside of our container setup #} - proxy_pass http://{{ matrix_ldap_registration_proxy_registration_addr_sans_container }}/register; - {% endif %} - } - -- name: Register Matrix LDAP registration proxy proxying configuration with matrix-nginx-proxy - ansible.builtin.set_fact: - matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks: | - {{ - matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks | default([]) - + - [matrix_ldap_registration_proxy_matrix_nginx_proxy_configuration] - }} - -- name: Warn about reverse-proxying if matrix-nginx-proxy not used - ansible.builtin.debug: - msg: >- - NOTE: You've enabled the Matrix LDAP registration proxy bridge but are not using the matrix-nginx-proxy - reverse proxy. - Please make sure that you're proxying the `{{ matrix_ldap_registration_proxy_public_endpoint }}` - URL endpoint to the matrix-ldap-proxy container. - You can expose the container's port using the `matrix_ldap_registration_proxy_container_http_host_bind_port` variable. - when: "not matrix_nginx_proxy_enabled | default(False) | bool" diff --git a/roles/custom/matrix-ldap-registration-proxy/tasks/main.yml b/roles/custom/matrix-ldap-registration-proxy/tasks/main.yml index cc71ae82c..9a7f24d2d 100644 --- a/roles/custom/matrix-ldap-registration-proxy/tasks/main.yml +++ b/roles/custom/matrix-ldap-registration-proxy/tasks/main.yml @@ -1,14 +1,5 @@ --- -- tags: - - setup-all - - setup-nginx-proxy - - install-all - - install-nginx-proxy - block: - - when: matrix_ldap_registration_proxy_enabled | bool - ansible.builtin.include_tasks: "{{ role_path }}/tasks/inject_into_nginx_proxy.yml" - - tags: - setup-all - setup-matrix-ldap-registration-proxy diff --git a/roles/custom/matrix-ldap-registration-proxy/tasks/setup_install.yml b/roles/custom/matrix-ldap-registration-proxy/tasks/setup_install.yml index 3814b1391..c54a2f65e 100644 --- a/roles/custom/matrix-ldap-registration-proxy/tasks/setup_install.yml +++ b/roles/custom/matrix-ldap-registration-proxy/tasks/setup_install.yml @@ -41,6 +41,21 @@ owner: "{{ matrix_user_username }}" group: "{{ matrix_user_groupname }}" +- name: Ensure matrix-ldap-registration-proxy support files installed + ansible.builtin.template: + src: "{{ role_path }}/templates/{{ item }}.j2" + dest: "{{ matrix_ldap_registration_proxy_base_path }}/{{ item }}" + mode: 0640 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_groupname }}" + with_items: + - labels + +- name: Ensure matrix-ldap-registration-proxy container network is created + community.general.docker_network: + name: "{{ matrix_ldap_registration_proxy_container_network }}" + driver: bridge + - name: Ensure matrix-ldap-registration-proxy.service installed ansible.builtin.template: src: "{{ role_path }}/templates/systemd/matrix-ldap-registration-proxy.service.j2" diff --git a/roles/custom/matrix-ldap-registration-proxy/tasks/validate_config.yml b/roles/custom/matrix-ldap-registration-proxy/tasks/validate_config.yml index 6b52af9c8..a0e0a97fd 100644 --- a/roles/custom/matrix-ldap-registration-proxy/tasks/validate_config.yml +++ b/roles/custom/matrix-ldap-registration-proxy/tasks/validate_config.yml @@ -1,12 +1,26 @@ --- -- name: Fail if required settings not defined +- name: Fail if required matrix-ldap-registration-proxy settings not defined ansible.builtin.fail: msg: >- You need to define a required configuration setting (`{{ item }}`). when: "vars[item] == ''" with_items: + - "matrix_ldap_registration_proxy_hostname" - "matrix_ldap_registration_proxy_ldap_uri" - "matrix_ldap_registration_proxy_ldap_base_dn" - "matrix_ldap_registration_proxy_ldap_user" - "matrix_ldap_registration_proxy_ldap_password" + - "matrix_ldap_registration_proxy_container_network" + +- name: (Deprecation) Catch and report renamed matrix-ldap-registration-proxy settings + ansible.builtin.fail: + msg: >- + Your configuration contains a variable, which now has a different name. + Please change your configuration to rename the variable (`{{ item.old }}` -> `{{ item.new }}`). + when: "item.old in vars" + with_items: + - {'old': 'matrix_ldap_registration_proxy_registration_addr_with_container', 'new': ''} + - {'old': 'matrix_ldap_registration_proxy_registration_addr_sans_container', 'new': ''} + - {'old': 'matrix_ldap_registration_proxy_container_port', 'new': 'matrix_ldap_registration_listen_port'} + - {'old': 'matrix_ldap_registration_proxy_registration_endpoint', 'new': 'matrix_ldap_registration_proxy_container_labels_registration_endpoint_path_prefix'} diff --git a/roles/custom/matrix-ldap-registration-proxy/templates/labels.j2 b/roles/custom/matrix-ldap-registration-proxy/templates/labels.j2 new file mode 100644 index 000000000..fa4b3e130 --- /dev/null +++ b/roles/custom/matrix-ldap-registration-proxy/templates/labels.j2 @@ -0,0 +1,39 @@ +{% if matrix_ldap_registration_proxy_container_labels_traefik_enabled %} +traefik.enable=true + +{% if matrix_ldap_registration_proxy_container_labels_traefik_docker_network %} +traefik.docker.network={{ matrix_ldap_registration_proxy_container_labels_traefik_docker_network }} +{% endif %} + +{# + Registration +#} +{% if matrix_ldap_registration_proxy_container_labels_public_endpoint_enabled %} + +traefik.http.middlewares.matrix-ldap-registration-proxy-registration-endpoint-replacepath.replacepath.path=/register + +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.rule={{ matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_rule }} + +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.middlewares=matrix-ldap-registration-proxy-registration-endpoint-replacepath + +{% if matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_priority | int > 0 %} +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.priority={{ matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_priority }} +{% endif %} + +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.service=matrix-ldap-registration-proxy +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.entrypoints={{ matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_entrypoints }} + +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.tls={{ matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_tls | to_json }} +{% if matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_tls %} +traefik.http.routers.matrix-ldap-registration-proxy-registration-endpoint.tls.certResolver={{ matrix_ldap_registration_proxy_container_labels_public_endpoint_traefik_tls_certResolver }} +{% endif %} + +traefik.http.services.matrix-ldap-registration-proxy.loadbalancer.server.port={{ matrix_ldap_registration_listen_port }} +{% endif %} +{# + /Registration +#} + +{% endif %} + +{{ matrix_ldap_registration_proxy_container_labels_additional_labels }} diff --git a/roles/custom/matrix-ldap-registration-proxy/templates/ldap-registration-proxy.env.j2 b/roles/custom/matrix-ldap-registration-proxy/templates/ldap-registration-proxy.env.j2 index 581a0b0d8..0def38932 100644 --- a/roles/custom/matrix-ldap-registration-proxy/templates/ldap-registration-proxy.env.j2 +++ b/roles/custom/matrix-ldap-registration-proxy/templates/ldap-registration-proxy.env.j2 @@ -29,7 +29,7 @@ MATRIX_SERVER_URL={{ matrix_ldap_registration_proxy_matrix_server_url }} # these settings are optional: # Specify the port to listen on. Default to 8080 -LISTEN_PORT={{ matrix_ldap_registration_proxy_container_port }} +LISTEN_PORT={{ matrix_ldap_registration_listen_port }} # Use this to extend the configuration with custom variables {{ matrix_ldap_registration_proxy_env_variables_extension }} diff --git a/roles/custom/matrix-ldap-registration-proxy/templates/systemd/matrix-ldap-registration-proxy.service.j2 b/roles/custom/matrix-ldap-registration-proxy/templates/systemd/matrix-ldap-registration-proxy.service.j2 index d1f666407..5d3ef7f47 100644 --- a/roles/custom/matrix-ldap-registration-proxy/templates/systemd/matrix-ldap-registration-proxy.service.j2 +++ b/roles/custom/matrix-ldap-registration-proxy/templates/systemd/matrix-ldap-registration-proxy.service.j2 @@ -18,21 +18,30 @@ ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_s # matrix_ldap_registration_proxy writes an SQLite shared library (libsqlitejdbc.so) to /tmp and executes it from there, # so /tmp needs to be mounted with an exec option. -ExecStart={{ devture_systemd_docker_base_host_command_docker }} run --rm --name matrix-ldap-registration-proxy \ +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \ + --rm \ + --name=matrix-ldap-registration-proxy \ --log-driver=none \ --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ --cap-drop=ALL \ --read-only \ - --network={{ matrix_docker_network }} \ + --network={{ matrix_ldap_registration_proxy_container_network }} \ {% if matrix_ldap_registration_proxy_container_http_host_bind_port %} - -p {{ matrix_ldap_registration_proxy_container_http_host_bind_port }}:{{ matrix_ldap_registration_proxy_container_port }} \ + -p {{ matrix_ldap_registration_proxy_container_http_host_bind_port }}:{{ matrix_ldap_registration_listen_port }} \ {% endif %} --env-file {{ matrix_ldap_registration_proxy_config_path }}/ldap-registration-proxy.env \ + --label-file={{ matrix_ldap_registration_proxy_base_path }}/labels \ {% for arg in matrix_ldap_registration_proxy_container_extra_arguments %} {{ arg }} \ {% endfor %} {{ matrix_ldap_registration_proxy_docker_image }} +{% for network in matrix_ldap_registration_proxy_container_additional_networks %} +ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-ldap-registration-proxy +{% endfor %} + +ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-ldap-registration-proxy + ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} stop --time={{ devture_systemd_docker_base_container_stop_grace_time_seconds }} matrix-ldap-registration-proxy 2>/dev/null || true' ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-ldap-registration-proxy 2>/dev/null || true' Restart=always