[WIP] Initial work on matrix-static-files role

This commit is contained in:
Slavi Pantaleev
2024-01-03 13:05:59 +02:00
parent 128a7b82d5
commit 065b70203d
21 changed files with 675 additions and 57 deletions

View File

@ -0,0 +1,87 @@
---
- name: Ensure matrix-static-files paths exist
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0750
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
with_items:
- "{{ matrix_static_files_base_path }}"
- "{{ matrix_static_files_config_path }}"
- "{{ matrix_static_files_public_path }}"
- "{{ matrix_static_files_public_well_known_path }}"
- "{{ matrix_static_files_public_well_known_matrix_path }}"
- name: Ensure matrix-static-files is configured
ansible.builtin.template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
mode: 0644
with_items:
- src: "{{ role_path }}/templates/config.toml.j2"
dest: "{{ matrix_static_files_config_path }}/config.toml"
- src: "{{ role_path }}/templates/env.j2"
dest: "{{ matrix_static_files_base_path }}/env"
- src: "{{ role_path }}/templates/labels.j2"
dest: "{{ matrix_static_files_base_path }}/labels"
- name: Ensure matrix-static-files files are installed
ansible.builtin.copy:
content: "{{ item.content | to_nice_json }}"
dest: "{{ item.dest }}"
mode: 0644
owner: "{{ matrix_user_username }}"
group: "{{ matrix_user_groupname }}"
when: item.when | bool
with_items:
- content: "{{ matrix_static_files_file_matrix_client_configuration }}"
dest: "{{ matrix_static_files_public_well_known_matrix_path }}/client"
when: true
- content: "{{ matrix_static_files_file_matrix_server_configuration }}"
dest: "{{ matrix_static_files_public_well_known_matrix_path }}/server"
when: "{{ matrix_static_files_file_matrix_server_enabled }}"
- content: "{{ matrix_static_files_file_matrix_support_configuration }}"
dest: "{{ matrix_static_files_public_well_known_matrix_path }}/support"
when: "{{ matrix_static_files_file_matrix_support_enabled }}"
- name: Ensure /.well-known/matrix/server file deleted if not enabled
ansible.builtin.file:
path: "{{ matrix_static_files_public_well_known_matrix_path }}/server"
state: absent
when: "not matrix_static_files_file_matrix_server_enabled | bool"
- name: Ensure /.well-known/matrix/support file deleted if not enabled
ansible.builtin.file:
path: "{{ matrix_static_files_public_well_known_matrix_path }}/support"
state: absent
when: "not matrix_static_files_file_matrix_support_enabled | bool"
- name: Ensure matrix-static-files container image is pulled
community.docker.docker_image:
name: "{{ matrix_static_files_container_image }}"
source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}"
force_source: "{{ matrix_static_files_container_image_force_pull if ansible_version.major > 2 or ansible_version.minor >= 8 else omit }}"
force: "{{ omit if ansible_version.major > 2 or ansible_version.minor >= 8 else matrix_static_files_container_image_force_pull }}"
register: result
retries: "{{ devture_playbook_help_container_retries_count }}"
delay: "{{ devture_playbook_help_container_retries_delay }}"
until: result is not failed
- name: Ensure matrix-static-files container network is created
community.general.docker_network:
name: "{{ matrix_static_files_container_network }}"
driver: bridge
- name: Ensure matrix-static-files systemd service is installed
ansible.builtin.template:
src: "{{ role_path }}/templates/systemd/matrix-static-files.service.j2"
dest: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_static_files_ident }}.service"
mode: 0644

View File

@ -0,0 +1,25 @@
---
- tags:
- setup-all
- setup-matrix-static-files
- install-all
- install-matrix-static-files
block:
- when: matrix_static_files_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/validate_config.yml"
- when: matrix_static_files_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/install.yml"
- tags:
- setup-all
- setup-matrix-static-files
block:
- when: not matrix_static_files_enabled | bool
ansible.builtin.include_tasks: "{{ role_path }}/tasks/uninstall.yml"
- tags:
- self-check
block:
- ansible.builtin.include_tasks: "{{ role_path }}/tasks/self_check_well_known.yml"

View File

@ -0,0 +1,32 @@
---
# TODO - migrate these variables and deprecate the old ones
- name: Determine well-known files to check (Matrix)
ansible.builtin.set_fact:
well_known_file_checks:
- path: /.well-known/matrix/client
purpose: Client Discovery
cors: true
follow_redirects: "{{ matrix_nginx_proxy_self_check_well_known_matrix_client_follow_redirects }}"
validate_certs: "{{ matrix_nginx_proxy_self_check_validate_certificates }}"
- when: matrix_well_known_matrix_server_enabled | bool
block:
- ansible.builtin.set_fact:
well_known_file_check_matrix_server:
path: /.well-known/matrix/server
purpose: Server Discovery
cors: false
follow_redirects: safe
validate_certs: "{{ matrix_nginx_proxy_self_check_validate_certificates }}"
- name: Determine domains that we require certificates for (ma1sd)
ansible.builtin.set_fact:
well_known_file_checks: "{{ well_known_file_checks + [well_known_file_check_matrix_server] }}"
- name: Perform well-known checks
ansible.builtin.include_tasks: "{{ role_path }}/tasks/self_check_well_known_file.yml"
with_items: "{{ well_known_file_checks }}"
loop_control:
loop_var: well_known_file_check

View File

@ -0,0 +1,73 @@
---
- ansible.builtin.set_fact:
well_known_url_matrix: "https://{{ matrix_server_fqn_matrix }}{{ well_known_file_check.path }}"
well_known_url_identity: "https://{{ matrix_domain }}{{ well_known_file_check.path }}"
# These well-known files may be served without a `Content-Type: application/json` header,
# so we can't rely on the uri module's automatic parsing of JSON.
- name: Check .well-known on the matrix hostname
ansible.builtin.uri:
url: "{{ well_known_url_matrix }}"
follow_redirects: none
return_content: true
validate_certs: "{{ well_known_file_check.validate_certs }}"
headers:
Origin: example.com
check_mode: false
register: result_well_known_matrix
ignore_errors: true
- name: Fail if .well-known not working on the matrix hostname
ansible.builtin.fail:
msg: "Failed checking that the well-known file for {{ well_known_file_check.purpose }} is configured at `{{ matrix_server_fqn_matrix }}` (checked endpoint: `{{ well_known_url_matrix }}`). Is port 443 open in your firewall? Full error: {{ result_well_known_matrix }}"
when: "result_well_known_matrix.failed"
- name: Parse JSON for well-known payload at the matrix hostname
ansible.builtin.set_fact:
well_known_matrix_payload: "{{ result_well_known_matrix.content | from_json }}"
- name: Fail if .well-known not CORS-aware on the matrix hostname
ansible.builtin.fail:
msg: "The well-known file for {{ well_known_file_check.purpose }} on `{{ matrix_server_fqn_matrix }}` (checked endpoint: `{{ well_known_url_matrix }}`) is not CORS-aware. The file needs to be served with an Access-Control-Allow-Origin header set."
when: "well_known_file_check.cors and 'access_control_allow_origin' not in result_well_known_matrix"
- name: Report working .well-known on the matrix hostname
ansible.builtin.debug:
msg: "well-known for {{ well_known_file_check.purpose }} is configured correctly for `{{ matrix_server_fqn_matrix }}` (checked endpoint: `{{ well_known_url_matrix }}`)"
- name: Check .well-known on the identity hostname
ansible.builtin.uri:
url: "{{ well_known_url_identity }}"
follow_redirects: "{{ well_known_file_check.follow_redirects }}"
return_content: true
validate_certs: "{{ well_known_file_check.validate_certs }}"
headers:
Origin: example.com
check_mode: false
register: result_well_known_identity
ignore_errors: true
- name: Fail if .well-known not working on the identity hostname
ansible.builtin.fail:
msg: "Failed checking that the well-known file for {{ well_known_file_check.purpose }} is configured at `{{ matrix_domain }}` (checked endpoint: `{{ well_known_url_identity }}`). Is port 443 open in your firewall? Full error: {{ result_well_known_identity }}"
when: "result_well_known_identity.failed"
- name: Parse JSON for well-known payload at the identity hostname
ansible.builtin.set_fact:
well_known_identity_payload: "{{ result_well_known_identity.content | from_json }}"
- name: Fail if .well-known not CORS-aware on the identity hostname
ansible.builtin.fail:
msg: "The well-known file for {{ well_known_file_check.purpose }} on `{{ matrix_domain }}` (checked endpoint: `{{ well_known_url_identity }}`) is not CORS-aware. The file needs to be served with an Access-Control-Allow-Origin header set. See docs/configuring-well-known.md"
when: "well_known_file_check.cors and 'access_control_allow_origin' not in result_well_known_identity"
# For people who manually copy the well-known file, try to detect if it's outdated
- name: Fail if well-known is different on matrix hostname and identity hostname
ansible.builtin.fail:
msg: "The well-known files for {{ well_known_file_check.purpose }} at `{{ matrix_server_fqn_matrix }}` and `{{ matrix_domain }}` are different. Perhaps you copied the file ({{ well_known_file_check.path }}) manually before and now it's outdated?"
when: "well_known_matrix_payload != well_known_identity_payload"
- name: Report working .well-known on the identity hostname
ansible.builtin.debug:
msg: "well-known for {{ well_known_file_check.purpose }} ({{ well_known_file_check.path }}) is configured correctly for `{{ matrix_domain }}` (checked endpoint: `{{ well_known_url_identity }}`)"

View File

@ -0,0 +1,25 @@
---
- name: Check existence of matrix-static-files systemd service
ansible.builtin.stat:
path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_static_files_ident }}.service"
register: matrix_static_files_service_stat
- when: matrix_static_files_service_stat.stat.exists | bool
block:
- name: Ensure matrix-static-files systemd service is stopped
ansible.builtin.service:
name: "{{ matrix_static_files_ident }}"
state: stopped
enabled: false
daemon_reload: true
- name: Ensure matrix-static-files systemd service doesn't exist
ansible.builtin.file:
path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_static_files_ident }}.service"
state: absent
- name: Ensure matrix-static-files directory is deleted
ansible.builtin.file:
path: "{{ matrix_static_files_base_path }}"
state: absent

View File

@ -0,0 +1,11 @@
---
- name: Fail if required matrix-static-files settings not defined
ansible.builtin.fail:
msg: >-
You need to define a required configuration setting (`{{ item.name }}`).
when: "item.when | bool and vars[item.name] == ''"
with_items:
- {'name': 'matrix_static_files_container_labels_well_known_matrix_endpoint_traefik_hostname', when: "{{ matrix_static_files_container_labels_well_known_matrix_endpoint_enabled }}"}
- {'name': 'matrix_static_files_container_labels_well_known_matrix_endpoint_traefik_path_prefix', when: "{{ matrix_static_files_container_labels_well_known_matrix_endpoint_enabled }}"}