Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

319 lines
17 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Element Call is a static browser application. Its live JSON configuration and
  6. # a browser bundle are the weight-bearing probes; no Matrix or LiveKit account is needed.
  7. - name: Verify matrix-element-call
  8. hosts: all
  9. become: true
  10. vars_files:
  11. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  12. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  13. vars:
  14. matrix_element_call_root_body: "{{ matrix_element_call_root_http.stdout_lines[:-1] | join('\n') }}"
  15. matrix_element_call_runtime: "{{ (matrix_element_call_container_inspect.stdout | from_json) | first }}"
  16. gather_facts: false
  17. tasks:
  18. # A Renovate bump changes this source of truth and therefore the image expectation.
  19. - name: Load the role's defaults under a separate name
  20. ansible.builtin.include_vars:
  21. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  22. name: matrix_element_call_role_defaults
  23. - name: Wait for the matrix-element-call service to become active
  24. ansible.builtin.systemd_service:
  25. name: matrix-element-call.service
  26. register: matrix_element_call_service
  27. until: matrix_element_call_service.status.ActiveState == 'active'
  28. retries: 30
  29. delay: 5
  30. failed_when: false
  31. # Restart=always makes ActiveState insufficient for detecting a crash loop.
  32. - name: Assert the service is active and has not restarted
  33. ansible.builtin.assert:
  34. that:
  35. - matrix_element_call_service.status.ActiveState == 'active'
  36. - matrix_element_call_service.status.NRestarts is defined
  37. - matrix_element_call_service.status.NRestarts | int == 0
  38. fail_msg: >-
  39. matrix-element-call.service is
  40. {{ matrix_element_call_service.status.ActiveState | default('unknown') }} after
  41. {{ matrix_element_call_service.status.NRestarts | default('?') }} restart(s)
  42. success_msg: "matrix-element-call.service is active and has not restarted"
  43. # Probe through the private container network, matching the deployment path from Traefik.
  44. - name: Wait for Element Call to serve its application root
  45. ansible.builtin.command:
  46. argv:
  47. - docker
  48. - run
  49. - --rm
  50. - --network={{ matrix_element_call_container_network }}
  51. - "{{ molecule_shared_image_curl }}"
  52. - --silent
  53. - --show-error
  54. - --write-out
  55. - "\nHTTP_STATUS=%{http_code}"
  56. - http://matrix-element-call:8080/
  57. register: matrix_element_call_root_http
  58. changed_when: false
  59. until: "'HTTP_STATUS=200' in matrix_element_call_root_http.stdout"
  60. retries: 24
  61. delay: 5
  62. failed_when: false
  63. - name: Assert the application root is real Element Call HTML
  64. ansible.builtin.assert:
  65. that:
  66. - matrix_element_call_root_http.rc == 0
  67. - matrix_element_call_root_http.stdout_lines[-1] == 'HTTP_STATUS=200'
  68. - matrix_element_call_root_body | trim | length > 500
  69. - "'<!doctype html' in matrix_element_call_root_body | lower"
  70. - "'element call' in matrix_element_call_root_body | lower"
  71. fail_msg: "Element Call did not return its real application HTML over the container network"
  72. success_msg: "Element Call serves its real application root over HTTP"
  73. - name: Fetch Element Call's live browser configuration
  74. ansible.builtin.command:
  75. argv:
  76. - docker
  77. - run
  78. - --rm
  79. - --network={{ matrix_element_call_container_network }}
  80. - "{{ molecule_shared_image_curl }}"
  81. - --silent
  82. - --show-error
  83. - --write-out
  84. - "\nHTTP_STATUS=%{http_code}\nCONTENT_TYPE=%{content_type}"
  85. - http://matrix-element-call:8080/config.json
  86. register: matrix_element_call_config_http
  87. changed_when: false
  88. failed_when: false
  89. - name: Assert the live browser configuration is served as JSON
  90. ansible.builtin.assert:
  91. that:
  92. - matrix_element_call_config_http.rc == 0
  93. - "'HTTP_STATUS=200' in matrix_element_call_config_http.stdout_lines"
  94. - matrix_element_call_config_http.stdout | regex_search('(?m)^CONTENT_TYPE=application/json(?:;|$)') is not none
  95. fail_msg: "Element Call did not serve its live browser configuration as JSON"
  96. success_msg: "Element Call serves its live browser configuration as JSON"
  97. - name: Parse the live browser configuration
  98. ansible.builtin.set_fact:
  99. matrix_element_call_live_config: >-
  100. {{ matrix_element_call_config_http.stdout | regex_replace('\nHTTP_STATUS=[0-9]+\nCONTENT_TYPE=.*$', '') | from_json }}
  101. - name: Assert the live configuration carries the configured homeserver and LiveKit service
  102. ansible.builtin.assert:
  103. that:
  104. - matrix_element_call_live_config is mapping
  105. - matrix_element_call_live_config.default_server_config['m.homeserver'].base_url == matrix_element_call_config_default_server_config_m_homeserver_base_url
  106. - matrix_element_call_live_config.default_server_config['m.homeserver'].server_name == matrix_element_call_config_default_server_config_m_homeserver_server_name
  107. - matrix_element_call_live_config.livekit.livekit_service_url == matrix_element_call_config_livekit_livekit_service_url
  108. fail_msg: "The live config.json does not carry the non-default values rendered by the role"
  109. success_msg: "The live config.json carries the configured homeserver and LiveKit service"
  110. - name: Read the configuration file the role rendered
  111. ansible.builtin.slurp:
  112. src: "{{ matrix_element_call_base_path }}/config.json"
  113. register: matrix_element_call_rendered_config_file
  114. - name: Assert the served and rendered structured configurations are identical
  115. ansible.builtin.assert:
  116. that:
  117. - matrix_element_call_rendered_config == matrix_element_call_live_config
  118. fail_msg: "The live configuration differs from the parsed file the role rendered"
  119. success_msg: "The running service exposes the exact structured configuration the role rendered"
  120. vars:
  121. matrix_element_call_rendered_config: >-
  122. {{ matrix_element_call_rendered_config_file.content | b64decode | from_json }}
  123. # Fetch a browser bundle named by the live HTML, proving the image contains the application.
  124. - name: Extract a live Element Call JavaScript asset path
  125. ansible.builtin.set_fact:
  126. matrix_element_call_application_asset_path: >-
  127. {{ matrix_element_call_root_body | regex_search('(?<=src=")[^"]+[.]js(?=")') }}
  128. - name: Fetch the live Element Call application asset
  129. ansible.builtin.command:
  130. argv:
  131. - docker
  132. - run
  133. - --rm
  134. - --network={{ matrix_element_call_container_network }}
  135. - "{{ molecule_shared_image_curl }}"
  136. - --silent
  137. - --show-error
  138. - --output
  139. - /dev/null
  140. - --write-out
  141. - "HTTP_STATUS=%{http_code}\nCONTENT_TYPE=%{content_type}\nSIZE=%{size_download}"
  142. - "http://matrix-element-call:8080/{{ matrix_element_call_application_asset_path | regex_replace('^/', '') }}"
  143. register: matrix_element_call_asset_http
  144. changed_when: false
  145. failed_when: false
  146. - name: Assert a real Element Call application asset is served
  147. ansible.builtin.assert:
  148. that:
  149. - matrix_element_call_application_asset_path | length > 0
  150. - matrix_element_call_asset_http.rc == 0
  151. - "'HTTP_STATUS=200' in matrix_element_call_asset_http.stdout_lines"
  152. - matrix_element_call_asset_http.stdout | regex_search('(?m)^CONTENT_TYPE=(?:application|text)/javascript(?:;|$)') is not none
  153. - matrix_element_call_asset_http.stdout | regex_search('(?m)^SIZE=([1-9][0-9]{4,})$') is not none
  154. fail_msg: "Element Call did not serve the substantial JavaScript asset referenced by its live HTML"
  155. success_msg: "Element Call serves a substantial live application asset"
  156. - name: Read the labels file the role rendered
  157. ansible.builtin.slurp:
  158. src: "{{ matrix_element_call_base_path }}/labels"
  159. register: matrix_element_call_labels_file
  160. - name: Initialize the parsed labels
  161. ansible.builtin.set_fact:
  162. matrix_element_call_labels_parsed: {}
  163. - name: Parse the rendered labels
  164. ansible.builtin.set_fact:
  165. matrix_element_call_labels_parsed: >-
  166. {{ matrix_element_call_labels_parsed | combine({item.split('=', 1)[0]: item.split('=', 1)[1]}) }}
  167. loop: "{{ (matrix_element_call_labels_file.content | b64decode).splitlines() | reject('equalto', '') }}"
  168. when: "'=' in item"
  169. no_log: true
  170. - name: Assert the rendered labels carry the public routing contract
  171. ansible.builtin.assert:
  172. that:
  173. - matrix_element_call_labels_parsed['traefik.enable'] == 'true'
  174. - matrix_element_call_labels_parsed['traefik.docker.network'] == matrix_element_call_container_network
  175. - matrix_element_call_labels_parsed['traefik.http.services.matrix-element-call.loadbalancer.server.port'] == '8080'
  176. - matrix_element_call_labels_parsed['traefik.http.routers.matrix-element-call.rule'] == 'Host(`call.molecule.local`)'
  177. - matrix_element_call_labels_parsed['traefik.http.routers.matrix-element-call.priority'] == '827'
  178. - matrix_element_call_labels_parsed['traefik.http.routers.matrix-element-call.entrypoints'] == 'web'
  179. - matrix_element_call_labels_parsed['traefik.http.routers.matrix-element-call.tls'] == 'false'
  180. - matrix_element_call_labels_parsed['traefik.http.routers.matrix-element-call.middlewares'] == 'matrix-element-call-add-headers'
  181. - matrix_element_call_labels_parsed['molecule.element-call.coverage'] == 'enabled'
  182. fail_msg: "The parsed labels do not carry the scenario's routing contract"
  183. success_msg: "The parsed labels carry the scenario's routing contract"
  184. - name: Assert the rendered labels carry the configured response headers
  185. ansible.builtin.assert:
  186. that:
  187. - matrix_element_call_labels_parsed['traefik.http.middlewares.matrix-element-call-add-headers.headers.customresponseheaders.X-XSS-Protection'] == '0'
  188. - matrix_element_call_labels_parsed['traefik.http.middlewares.matrix-element-call-add-headers.headers.customresponseheaders.X-Content-Type-Options'] == 'molecule-nosniff'
  189. - matrix_element_call_labels_parsed['traefik.http.middlewares.matrix-element-call-add-headers.headers.customresponseheaders.Content-Security-Policy'] == 'frame-ancestors https://embed.call.molecule.local'
  190. - matrix_element_call_labels_parsed['traefik.http.middlewares.matrix-element-call-add-headers.headers.customresponseheaders.Permission-Policy'] == 'camera=(self), microphone=(self)'
  191. - "'traefik.http.middlewares.matrix-element-call-add-headers.headers.customresponseheaders.Strict-Transport-Security' not in matrix_element_call_labels_parsed"
  192. fail_msg: "The parsed labels lost response headers or incorrectly enable HSTS without TLS"
  193. success_msg: "The parsed labels carry the configured response-header contract"
  194. - name: Inspect the running Element Call container
  195. ansible.builtin.command:
  196. argv:
  197. - docker
  198. - container
  199. - inspect
  200. - matrix-element-call
  201. register: matrix_element_call_container_inspect
  202. changed_when: false
  203. - name: Assert the running container uses the exact image pinned by the role
  204. ansible.builtin.assert:
  205. that:
  206. - matrix_element_call_runtime.Config.Image == matrix_element_call_expected_image
  207. fail_msg: >-
  208. The running container uses {{ matrix_element_call_runtime.Config.Image }}, expected
  209. {{ matrix_element_call_expected_image }}
  210. success_msg: "The running container uses the exact image pinned by the role"
  211. vars:
  212. matrix_element_call_expected_image: >-
  213. {{ matrix_element_call_role_defaults.matrix_element_call_container_image_registry_prefix_upstream_default }}element-hq/element-call:{{ matrix_element_call_role_defaults.matrix_element_call_version }}
  214. - name: Assert the running container uses the playbook-supplied runtime
  215. ansible.builtin.assert:
  216. that:
  217. - matrix_element_call_runtime.Config.User == (matrix_user_uid | string) ~ ':' ~ (matrix_user_gid | string)
  218. - matrix_element_call_runtime.HostConfig.RestartPolicy.Name == 'no'
  219. - matrix_element_call_runtime.HostConfig.AutoRemove is sameas true
  220. - matrix_element_call_runtime.HostConfig.LogConfig.Type == 'none'
  221. fail_msg: "The running container does not use the role's identity and Docker lifecycle contract"
  222. success_msg: "The running container uses the role's identity and Docker lifecycle contract"
  223. - name: Assert the running container has the intended privilege isolation
  224. ansible.builtin.assert:
  225. that:
  226. - matrix_element_call_runtime.HostConfig.Privileged is sameas false
  227. - matrix_element_call_runtime.HostConfig.CapAdd | default([], true) | length == 0
  228. - matrix_element_call_runtime.HostConfig.CapDrop == ['ALL']
  229. fail_msg: "The running container does not have the intended privilege isolation"
  230. success_msg: "The running container drops all capabilities and is unprivileged"
  231. - name: Assert the role's configuration is the sole read-only bind mount
  232. ansible.builtin.assert:
  233. that:
  234. - matrix_element_call_runtime.Mounts | length == 1
  235. - matrix_element_call_runtime.Mounts[0].Type == 'bind'
  236. - matrix_element_call_runtime.Mounts[0].Source == matrix_element_call_base_path ~ '/config.json'
  237. - matrix_element_call_runtime.Mounts[0].Destination == '/app/config.json'
  238. - matrix_element_call_runtime.Mounts[0].RW is sameas false
  239. fail_msg: "The role-rendered config.json is not the container's sole read-only bind mount"
  240. success_msg: "The role-rendered config.json is mounted read-only at the image's live path"
  241. - name: Assert the rendered and extra labels reached the running container
  242. ansible.builtin.assert:
  243. that:
  244. - matrix_element_call_runtime.Config.Labels['traefik.enable'] == 'true'
  245. - matrix_element_call_runtime.Config.Labels['traefik.docker.network'] == matrix_element_call_container_network
  246. - matrix_element_call_runtime.Config.Labels['traefik.http.services.matrix-element-call.loadbalancer.server.port'] == '8080'
  247. - matrix_element_call_runtime.Config.Labels['traefik.http.routers.matrix-element-call.priority'] == '827'
  248. - matrix_element_call_runtime.Config.Labels['molecule.element-call.coverage'] == 'enabled'
  249. - matrix_element_call_runtime.Config.Labels['molecule.element-call.extra-argument'] == 'reached'
  250. fail_msg: "The running container does not carry the labels the role rendered and passed"
  251. success_msg: "The configured labels reached the running container"
  252. - name: Assert the running container is attached only to its dedicated network
  253. ansible.builtin.assert:
  254. that:
  255. - matrix_element_call_runtime.HostConfig.NetworkMode == matrix_element_call_container_network
  256. - matrix_element_call_runtime.NetworkSettings.Networks is mapping
  257. - matrix_element_call_runtime.NetworkSettings.Networks | length == 1
  258. - matrix_element_call_container_network in matrix_element_call_runtime.NetworkSettings.Networks
  259. fail_msg: >-
  260. Element Call has unexpected network attachments:
  261. {{ matrix_element_call_runtime.NetworkSettings.Networks.keys() | list }}
  262. success_msg: "The running container is attached only to its dedicated network"
  263. - name: Assert the observable extra runtime argument reached Docker
  264. ansible.builtin.assert:
  265. that:
  266. - matrix_element_call_runtime.Config.Hostname == 'element-call-molecule-runtime'
  267. fail_msg: "The configured container hostname extra argument did not reach Docker"
  268. success_msg: "The configured container extra argument reached Docker"
  269. - name: Ask Docker for Element Call's published ports
  270. ansible.builtin.command:
  271. argv:
  272. - docker
  273. - container
  274. - port
  275. - matrix-element-call
  276. register: matrix_element_call_published_ports
  277. changed_when: false
  278. failed_when: false
  279. - name: Assert the role publishes no host ports
  280. ansible.builtin.assert:
  281. that:
  282. - matrix_element_call_runtime.HostConfig.PortBindings | default({}, true) | length == 0
  283. - matrix_element_call_published_ports.rc == 0
  284. - matrix_element_call_published_ports.stdout | trim | length == 0
  285. fail_msg: >-
  286. Element Call unexpectedly publishes a host port:
  287. {{ matrix_element_call_published_ports.stdout | default('unknown') }}
  288. success_msg: "The role leaves Element Call's HTTP port unpublished"