Matrix Docker Ansible eploy
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

400 linhas
21 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # Proves the logged-out bridge starts, accepts the role-rendered configuration and
  6. # registration, migrates real Postgres, and opens its appservice listener. Slack
  7. # credentials and workspace access are deliberately outside the scenario boundary.
  8. - name: Verify mautrix-slack
  9. hosts: all
  10. become: true
  11. vars_files:
  12. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
  13. - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
  14. vars:
  15. mautrix_slack_config: "{{ mautrix_slack_config_file.content | b64decode | from_yaml }}"
  16. mautrix_slack_registration: "{{ mautrix_slack_registration_file.content | b64decode | from_yaml }}"
  17. mautrix_slack_labels_rendered: "{{ mautrix_slack_labels_file.content | b64decode }}"
  18. mautrix_slack_expected_public_address: http://slack-api.molecule.local/bridges/slack-api
  19. gather_facts: false
  20. tasks:
  21. # A Renovate bump changes this source of truth and therefore the image expectation.
  22. - name: Load the role's defaults under a separate name
  23. ansible.builtin.include_vars:
  24. file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
  25. name: mautrix_slack_role_defaults
  26. - name: Wait for the mautrix-slack service to become active
  27. ansible.builtin.systemd_service:
  28. name: matrix-mautrix-slack.service
  29. register: mautrix_slack_service
  30. until: mautrix_slack_service.status.ActiveState == 'active'
  31. retries: 30
  32. delay: 5
  33. failed_when: false
  34. # Restart=always makes ActiveState insufficient for detecting a crash loop.
  35. - name: Assert the service is active and has not been restarting
  36. ansible.builtin.assert:
  37. that:
  38. - mautrix_slack_service.status.ActiveState == 'active'
  39. - mautrix_slack_service.status.NRestarts is defined
  40. - mautrix_slack_service.status.NRestarts | int == 0
  41. fail_msg: >-
  42. matrix-mautrix-slack.service is
  43. {{ mautrix_slack_service.status.ActiveState | default('unknown') }} after
  44. {{ mautrix_slack_service.status.NRestarts | default('?') }} restart(s)
  45. success_msg: "matrix-mautrix-slack.service is active and has not restarted"
  46. - name: Wait for the bridge liveness endpoint
  47. ansible.builtin.command:
  48. argv:
  49. - docker
  50. - run
  51. - --rm
  52. - --network={{ matrix_bridge_mautrix_slack_container_network }}
  53. - "{{ molecule_shared_image_curl }}"
  54. - --silent
  55. - --output
  56. - /dev/null
  57. - --write-out
  58. - "HTTP_STATUS=%{http_code}"
  59. - http://matrix-mautrix-slack:8080/_matrix/mau/live
  60. register: mautrix_slack_live
  61. changed_when: false
  62. until: mautrix_slack_live.rc == 0 and mautrix_slack_live.stdout == 'HTTP_STATUS=200'
  63. retries: 24
  64. delay: 5
  65. failed_when: false
  66. - name: Wait for the bridge readiness endpoint
  67. ansible.builtin.command:
  68. argv:
  69. - docker
  70. - run
  71. - --rm
  72. - --network={{ matrix_bridge_mautrix_slack_container_network }}
  73. - "{{ molecule_shared_image_curl }}"
  74. - --silent
  75. - --output
  76. - /dev/null
  77. - --write-out
  78. - "HTTP_STATUS=%{http_code}"
  79. - http://matrix-mautrix-slack:8080/_matrix/mau/ready
  80. register: mautrix_slack_ready
  81. changed_when: false
  82. until: mautrix_slack_ready.rc == 0 and mautrix_slack_ready.stdout == 'HTTP_STATUS=200'
  83. retries: 24
  84. delay: 5
  85. failed_when: false
  86. - name: Assert the bridge is live and ready on the configured appservice port
  87. ansible.builtin.assert:
  88. that:
  89. - mautrix_slack_live.rc == 0
  90. - mautrix_slack_live.stdout == 'HTTP_STATUS=200'
  91. - mautrix_slack_ready.rc == 0
  92. - mautrix_slack_ready.stdout == 'HTTP_STATUS=200'
  93. fail_msg: >-
  94. Health probes returned live={{ mautrix_slack_live.stdout | default('none') }}
  95. (rc={{ mautrix_slack_live.rc | default('?') }}) and
  96. ready={{ mautrix_slack_ready.stdout | default('none') }}
  97. (rc={{ mautrix_slack_ready.rc | default('?') }})
  98. success_msg: "The bridge is live and ready on the configured appservice port"
  99. # This authenticated endpoint exercises the provisioning middleware and the network
  100. # connector's login-flow API without starting a Slack login or carrying credentials.
  101. - name: Query the authenticated provisioning login flows
  102. ansible.builtin.command:
  103. argv:
  104. - docker
  105. - run
  106. - --rm
  107. - --network={{ matrix_bridge_mautrix_slack_container_network }}
  108. - "{{ molecule_shared_image_curl }}"
  109. - --silent
  110. - --show-error
  111. - --fail-with-body
  112. - --header
  113. - "Authorization: Bearer {{ matrix_bridge_mautrix_slack_provisioning_shared_secret }}"
  114. - "http://matrix-mautrix-slack:8080/_matrix/provision/v3/login/flows?user_id=%40alice%3Amolecule.local"
  115. register: mautrix_slack_login_flows
  116. changed_when: false
  117. - name: Query the provisioning API with an invalid secret
  118. ansible.builtin.command:
  119. argv:
  120. - docker
  121. - run
  122. - --rm
  123. - --network={{ matrix_bridge_mautrix_slack_container_network }}
  124. - "{{ molecule_shared_image_curl }}"
  125. - --silent
  126. - --output
  127. - /dev/null
  128. - --write-out
  129. - "HTTP_STATUS=%{http_code}"
  130. - --header
  131. - "Authorization: Bearer definitely-wrong-molecule-secret"
  132. - "http://matrix-mautrix-slack:8080/_matrix/provision/v3/login/flows?user_id=%40alice%3Amolecule.local"
  133. register: mautrix_slack_login_flows_unauthorized
  134. changed_when: false
  135. failed_when: false
  136. - name: Assert the live provisioning API enforces auth and advertises login flows
  137. ansible.builtin.assert:
  138. that:
  139. - mautrix_slack_login_flows.rc == 0
  140. - (mautrix_slack_login_flows.stdout | from_json).flows is sequence
  141. - (mautrix_slack_login_flows.stdout | from_json).flows | length > 0
  142. - mautrix_slack_login_flows_unauthorized.rc == 0
  143. - mautrix_slack_login_flows_unauthorized.stdout == 'HTTP_STATUS=401'
  144. fail_msg: "The provisioning API did not enforce its secret or return Slack login flows"
  145. success_msg: "The provisioning API enforces its secret and returns Slack login flows"
  146. - name: Read the configuration the role rendered
  147. ansible.builtin.slurp:
  148. src: "{{ matrix_bridge_mautrix_slack_config_path }}/config.yaml"
  149. register: mautrix_slack_config_file
  150. - name: Assert the rendered configuration carries the appservice identity
  151. ansible.builtin.assert:
  152. that:
  153. - mautrix_slack_config.homeserver.address == matrix_bridge_mautrix_slack_homeserver_address
  154. - mautrix_slack_config.homeserver.domain == matrix_bridge_mautrix_slack_homeserver_domain
  155. - mautrix_slack_config.homeserver.async_media
  156. - mautrix_slack_config.appservice.id == 'slack'
  157. - mautrix_slack_config.appservice.address == 'http://matrix-mautrix-slack:8080'
  158. - mautrix_slack_config.appservice.public_address == mautrix_slack_expected_public_address
  159. - mautrix_slack_config.appservice.bot.username == matrix_bridge_mautrix_slack_appservice_bot_username
  160. - mautrix_slack_config.appservice.as_token == matrix_bridge_mautrix_slack_appservice_token
  161. - mautrix_slack_config.appservice.hs_token == matrix_bridge_mautrix_slack_homeserver_token
  162. fail_msg: "The rendered configuration does not carry the scenario's appservice identity"
  163. success_msg: "The rendered configuration carries the scenario's appservice identity"
  164. - name: Assert the rendered configuration carries non-default Slack behavior
  165. ansible.builtin.assert:
  166. that:
  167. - mautrix_slack_config.network.displayname_template == matrix_bridge_mautrix_slack_network_displayname_template
  168. - mautrix_slack_config.bridge.command_prefix == matrix_bridge_mautrix_slack_command_prefix
  169. - mautrix_slack_config.bridge.permissions['*'] == 'commands'
  170. - mautrix_slack_config.bridge.permissions[matrix_bridge_mautrix_slack_homeserver_domain] == 'admin'
  171. - mautrix_slack_config.bridge.relay.enabled
  172. - not mautrix_slack_config.bridge.relay.admin_only
  173. - mautrix_slack_config.bridge.relay.default_relays == ['molecule-workspace/molecule-relay']
  174. - mautrix_slack_config.logging.min_level == matrix_bridge_mautrix_slack_logging_level
  175. fail_msg: "The rendered configuration does not carry the scenario's Slack behavior"
  176. success_msg: "The rendered configuration carries the scenario's Slack behavior"
  177. - name: Assert the rendered configuration carries integration secrets
  178. ansible.builtin.assert:
  179. that:
  180. - mautrix_slack_config.provisioning.shared_secret == matrix_bridge_mautrix_slack_provisioning_shared_secret
  181. - mautrix_slack_config.public_media.signing_key == matrix_bridge_mautrix_slack_public_media_signing_key
  182. - mautrix_slack_config.double_puppet.secrets['molecule.remote'] == 'molecule_double_puppet_secret_a7d210'
  183. fail_msg: "The rendered configuration does not carry the scenario's integration secrets"
  184. success_msg: "The rendered configuration carries the scenario's integration secrets"
  185. - name: Assert the rendered configuration carries the non-default backfill policy
  186. ansible.builtin.assert:
  187. that:
  188. - not mautrix_slack_config.backfill.enabled
  189. - mautrix_slack_config.backfill.max_initial_messages == 19
  190. - mautrix_slack_config.backfill.max_catchup_messages == 31
  191. - mautrix_slack_config.backfill.unread_hours_threshold == 73
  192. - mautrix_slack_config.backfill.threads.max_initial_messages == 11
  193. fail_msg: "The rendered configuration does not carry the scenario's backfill policy"
  194. success_msg: "The rendered configuration carries the scenario's backfill policy"
  195. - name: Assert the configuration points at the scenario's Postgres database
  196. ansible.builtin.assert:
  197. that:
  198. - mautrix_slack_config.database.type == matrix_bridge_mautrix_slack_database_engine
  199. - matrix_bridge_mautrix_slack_database_username in mautrix_slack_config.database.uri
  200. - matrix_bridge_mautrix_slack_database_hostname in mautrix_slack_config.database.uri
  201. - matrix_bridge_mautrix_slack_database_name in mautrix_slack_config.database.uri
  202. fail_msg: >-
  203. database.uri is {{ mautrix_slack_config.database.uri | default('unset') }}, which
  204. was not composed from the scenario's connection settings
  205. success_msg: "The configuration points at the scenario's Postgres database"
  206. - name: Read the appservice registration the role rendered
  207. ansible.builtin.slurp:
  208. src: "{{ matrix_bridge_mautrix_slack_config_path }}/registration.yaml"
  209. register: mautrix_slack_registration_file
  210. - name: Assert the registration carries the configured handshake values
  211. ansible.builtin.assert:
  212. that:
  213. - mautrix_slack_registration.id == 'slack'
  214. - mautrix_slack_registration.as_token == matrix_bridge_mautrix_slack_appservice_token
  215. - mautrix_slack_registration.hs_token == matrix_bridge_mautrix_slack_homeserver_token
  216. - mautrix_slack_registration.sender_localpart == '_bot_' + matrix_bridge_mautrix_slack_appservice_bot_username
  217. - mautrix_slack_registration.url == 'http://matrix-mautrix-slack:8080'
  218. fail_msg: "The registration does not carry the configured handshake values"
  219. success_msg: "The registration carries the configured handshake values"
  220. - name: Assert the registration namespaces cover only Slack ghosts and the bridge bot
  221. ansible.builtin.assert:
  222. that:
  223. - mautrix_slack_ghost_regex | length > 0
  224. - mautrix_slack_bot_regex | length > 0
  225. - mautrix_slack_ghost_mxid is match(mautrix_slack_ghost_regex)
  226. - mautrix_slack_wrong_ghost_mxid is not match(mautrix_slack_ghost_regex)
  227. - mautrix_slack_bot_mxid is match(mautrix_slack_bot_regex)
  228. fail_msg: "The registration namespaces do not cover only Slack ghosts and the bot"
  229. success_msg: "The registration namespaces cover only Slack ghosts and the bot"
  230. vars:
  231. mautrix_slack_user_regexes: "{{ mautrix_slack_registration.namespaces.users | map(attribute='regex') | list }}"
  232. mautrix_slack_ghost_regex: "{{ mautrix_slack_user_regexes | select('search', 'slack_') | first | default('') }}"
  233. mautrix_slack_bot_regex: "{{ mautrix_slack_user_regexes | reject('search', 'slack_') | first | default('') }}"
  234. mautrix_slack_ghost_mxid: "@slack_T12345-U67890:{{ matrix_bridge_mautrix_slack_homeserver_domain }}"
  235. mautrix_slack_wrong_ghost_mxid: "@discord_T12345-U67890:{{ matrix_bridge_mautrix_slack_homeserver_domain }}"
  236. mautrix_slack_bot_mxid: "@{{ matrix_bridge_mautrix_slack_appservice_bot_username }}:{{ matrix_bridge_mautrix_slack_homeserver_domain }}"
  237. # Tables can appear only after hostname resolution, authentication, and real migrations.
  238. - name: List the tables the bridge created in Postgres
  239. ansible.builtin.command:
  240. argv:
  241. - docker
  242. - exec
  243. - matrix-postgres-molecule
  244. - psql
  245. - --username={{ matrix_bridge_mautrix_slack_database_username }}
  246. - --dbname={{ matrix_bridge_mautrix_slack_database_name }}
  247. - --tuples-only
  248. - --no-align
  249. - "--command=SELECT tablename FROM pg_tables WHERE schemaname = 'public'"
  250. register: mautrix_slack_tables
  251. changed_when: false
  252. - name: Assert the bridge migrated its schema into the configured database
  253. ansible.builtin.assert:
  254. that:
  255. - mautrix_slack_tables.rc == 0
  256. - "'version' in mautrix_slack_table_names"
  257. - "'portal' in mautrix_slack_table_names"
  258. - "'user_login' in mautrix_slack_table_names"
  259. - "'message' in mautrix_slack_table_names"
  260. - "'slack_version' in mautrix_slack_table_names"
  261. - "'emoji' in mautrix_slack_table_names"
  262. - mautrix_slack_table_names | length > 12
  263. fail_msg: >-
  264. The bridge did not migrate its schema into {{ matrix_bridge_mautrix_slack_database_name }}
  265. (found {{ mautrix_slack_table_names | length }} table(s))
  266. success_msg: "The bridge migrated its schema into the configured database"
  267. vars:
  268. mautrix_slack_table_names: "{{ mautrix_slack_tables.stdout_lines | select | list }}"
  269. - name: Read the labels the role rendered
  270. ansible.builtin.slurp:
  271. src: "{{ matrix_bridge_mautrix_slack_base_path }}/labels"
  272. register: mautrix_slack_labels_file
  273. - name: Assert the labels route the public endpoint to the appservice port
  274. ansible.builtin.assert:
  275. that:
  276. - "'traefik.enable=true' in mautrix_slack_labels_rendered"
  277. - "'traefik.docker.network=' + matrix_bridge_mautrix_slack_container_network in mautrix_slack_labels_rendered"
  278. - "'traefik.http.services.matrix-mautrix-slack-exposure.loadbalancer.server.port=8080' in mautrix_slack_labels_rendered"
  279. - "'traefik.http.routers.matrix-mautrix-slack-exposure.rule=Host(`slack-api.molecule.local`) && PathPrefix(`/bridges/slack-api`)' in mautrix_slack_labels_rendered"
  280. - "'traefik.http.middlewares.matrix-mautrix-slack-exposure-strip-prefix.stripprefix.prefixes=/bridges/slack-api' in mautrix_slack_labels_rendered"
  281. - "'traefik.http.routers.matrix-mautrix-slack-exposure.entrypoints=web' in mautrix_slack_labels_rendered"
  282. - "'traefik.http.routers.matrix-mautrix-slack-exposure.tls=false' in mautrix_slack_labels_rendered"
  283. - "'molecule.slack.coverage=enabled' in mautrix_slack_labels_rendered"
  284. fail_msg: "The rendered labels do not carry the scenario's exposure configuration"
  285. success_msg: "The rendered labels carry the scenario's exposure configuration"
  286. - name: Inspect the running bridge container
  287. ansible.builtin.command:
  288. argv:
  289. - docker
  290. - container
  291. - inspect
  292. - matrix-mautrix-slack
  293. register: mautrix_slack_container_inspect
  294. changed_when: false
  295. - name: Parse the running bridge container inspection
  296. ansible.builtin.set_fact:
  297. mautrix_slack_container: "{{ (mautrix_slack_container_inspect.stdout | from_json) | first }}"
  298. - name: Assert the running container uses the exact image pinned by the role
  299. ansible.builtin.assert:
  300. that:
  301. - mautrix_slack_container.Config.Image == mautrix_slack_expected_image
  302. fail_msg: >-
  303. The running container uses {{ mautrix_slack_container.Config.Image }}, expected
  304. {{ mautrix_slack_expected_image }}
  305. success_msg: "The running container uses the exact image pinned by the role"
  306. vars:
  307. mautrix_slack_expected_image: >-
  308. {{ mautrix_slack_role_defaults.matrix_bridge_mautrix_slack_container_image_registry_prefix_upstream_default }}mautrix/slack:{{ mautrix_slack_role_defaults.matrix_bridge_mautrix_slack_version }}
  309. - name: Assert the running container uses the playbook-supplied runtime
  310. ansible.builtin.assert:
  311. that:
  312. - mautrix_slack_container.Config.User == (matrix_user_uid | string) + ':' + (matrix_user_gid | string)
  313. - mautrix_slack_container.Config.WorkingDir == '/data'
  314. - mautrix_slack_container.Config.Cmd == ['/usr/bin/mautrix-slack', '-c', '/config/config.yaml', '-r', '/config/registration.yaml', '--no-update']
  315. - mautrix_slack_container.HostConfig.RestartPolicy.Name == 'no'
  316. - mautrix_slack_container.HostConfig.AutoRemove
  317. - mautrix_slack_container.HostConfig.LogConfig.Type == 'none'
  318. fail_msg: "The running container does not use the role's exact identity and command"
  319. success_msg: "The running container uses the role's exact identity and command"
  320. - name: Assert the running container has the intended privilege isolation
  321. ansible.builtin.assert:
  322. that:
  323. - "'ALL' in mautrix_slack_container.HostConfig.CapDrop"
  324. - not mautrix_slack_container.HostConfig.Privileged
  325. fail_msg: "The running container does not have the intended privilege isolation"
  326. success_msg: "The running container drops all capabilities and is unprivileged"
  327. - name: Assert the running container carries the role's exact bind mounts
  328. ansible.builtin.assert:
  329. that:
  330. - mautrix_slack_config_mount | length > 0
  331. - mautrix_slack_config_mount.Source == matrix_bridge_mautrix_slack_config_path
  332. - not mautrix_slack_config_mount.RW
  333. - mautrix_slack_data_mount | length > 0
  334. - mautrix_slack_data_mount.Source == matrix_bridge_mautrix_slack_data_path
  335. - mautrix_slack_data_mount.RW
  336. - mautrix_slack_container.Mounts | length == 2
  337. fail_msg: "The running container does not carry the role's exact config and data mounts"
  338. success_msg: "The running container carries read-only config and writable data mounts"
  339. vars:
  340. mautrix_slack_config_mount: >-
  341. {{ mautrix_slack_container.Mounts
  342. | selectattr('Destination', 'equalto', '/config')
  343. | first | default({}) }}
  344. mautrix_slack_data_mount: >-
  345. {{ mautrix_slack_container.Mounts
  346. | selectattr('Destination', 'equalto', '/data')
  347. | first | default({}) }}
  348. - name: Assert the rendered labels are attached to the running container
  349. ansible.builtin.assert:
  350. that:
  351. - mautrix_slack_container.Config.Labels['traefik.enable'] == 'true'
  352. - mautrix_slack_container.Config.Labels['traefik.docker.network'] == matrix_bridge_mautrix_slack_container_network
  353. - mautrix_slack_container.Config.Labels['traefik.http.services.matrix-mautrix-slack-exposure.loadbalancer.server.port'] == '8080'
  354. - mautrix_slack_container.Config.Labels['molecule.slack.coverage'] == 'enabled'
  355. fail_msg: "The running container does not carry the labels the role rendered"
  356. success_msg: "The running container carries the labels the role rendered"
  357. - name: Assert the running container has the exact network and port exposure
  358. ansible.builtin.assert:
  359. that:
  360. - mautrix_slack_container.HostConfig.NetworkMode == matrix_bridge_mautrix_slack_container_network
  361. - matrix_bridge_mautrix_slack_container_network in mautrix_slack_container.NetworkSettings.Networks
  362. - mautrix_slack_container.NetworkSettings.Networks | length == 1
  363. - mautrix_slack_container.HostConfig.PortBindings | default({}, true) | length == 0
  364. fail_msg: >-
  365. The container has unexpected networks or host ports:
  366. networks={{ mautrix_slack_container.NetworkSettings.Networks.keys() | list }},
  367. ports={{ mautrix_slack_container.HostConfig.PortBindings | default({}) }}
  368. success_msg: "The container uses only its dedicated network and publishes no host ports"