33-clair-deploy.yml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. ---
  2. # Tasks required by 15-clair-deploy.adoc.
  3. - name: Prepare registry VM to run Clair services.
  4. hosts: registry.ocp4.example.com
  5. gather_subset: min
  6. remote_user: quay
  7. tasks:
  8. - name: Ensure the podman network is there.
  9. containers.podman.podman_network_info:
  10. name: quay
  11. register: quay_net
  12. ignore_errors: yes
  13. - ansible.builtin.assert:
  14. that:
  15. - not quay_net.failed
  16. - quay_net.networks is defined
  17. - quay_net.networks is iterable
  18. - quay_net.networks | length == 1
  19. fail_msg: "FATAL: Podman network 'quay' does not exist for 'quay' user. Ensure you deployed Quay before running this playbook."
  20. success_msg: "OK, network 'quay' found."
  21. - name: Ensure the quay service is defined.
  22. ansible.builtin.stat:
  23. path: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/quay.service"
  24. get_attributes: no
  25. get_checksum: no
  26. get_mime: no
  27. register: quay_svc_unit
  28. - ansible.builtin.assert:
  29. that:
  30. - not quay_svc_unit.failed
  31. - quay_svc_unit.stat.exists
  32. fail_msg: "FATAL: User service 'quay.service' not found for 'quay' user. Ensure you deployed Quay before running this playbook."
  33. success_msg: "OK, service 'quay.service' found."
  34. - name: Ensure the quay-pg service is defined.
  35. ansible.builtin.stat:
  36. path: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/quay-pg.service"
  37. get_attributes: no
  38. get_checksum: no
  39. get_mime: no
  40. register: quay_pg_svc_unit
  41. - ansible.builtin.assert:
  42. that:
  43. - not quay_pg_svc_unit.failed
  44. - quay_pg_svc_unit.stat.exists
  45. fail_msg: "FATAL: User service 'quay-pg.service' not found for 'quay' user. Ensure you deployed Quay before running this playbook."
  46. success_msg: "OK, service 'quay-pg.service' found."
  47. - name: Ensure Quay PostgreSQL is running.
  48. ansible.builtin.systemd_service:
  49. name: quay-pg
  50. scope: user
  51. state: started
  52. - name: Check whether the clair database exists.
  53. containers.podman.podman_container_exec:
  54. name: postgresql
  55. command: psql -d postgres -U postgres -t -A -c "SELECT datname FROM pg_database WHERE datname = 'clair'"
  56. register: pg_clair
  57. changed_when: no
  58. - name: Create the clair database if necessary.
  59. containers.podman.podman_container_exec:
  60. name: postgresql
  61. command: 'psql -d postgres -U postgres -c "CREATE DATABASE clair OWNER quay"'
  62. when:
  63. - pg_clair is defined
  64. - pg_clair.stdout_lines | length == 0
  65. - name: Create the uuid-ossp extension if necessary.
  66. containers.podman.podman_container_exec:
  67. name: postgresql
  68. command: psql -d clair -U postgres -c 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp"'
  69. register: pg_ext
  70. changed_when:
  71. - not "already exists" in pg_ext.stderr
  72. - name: Pull all the images if necessary.
  73. containers.podman.podman_image:
  74. name: "{{ registry_host }}/quay/clair-rhel8:v{{ quay_version }}"
  75. pull: yes
  76. state: present
  77. # TODO: Make loop labels nicer.
  78. - name: Patch Quay config if necessary.
  79. ansible.builtin.lineinfile:
  80. path: "{{ ansible_facts['user_dir'] }}/config/config.yaml"
  81. insertafter: "{{ item.after }}"
  82. regexp: "{{ item.fixre }}"
  83. line: "{{ item.value }}"
  84. loop:
  85. - after: "^FEATURE_MAILING: false$"
  86. fixre: "^FEATURE_SECURITY_SCANNER: .*$"
  87. value: "FEATURE_SECURITY_SCANNER: true"
  88. - after: "^SECRET_KEY: .*$"
  89. fixre: "^SECURITY_SCANNER_INDEXING_INTERVAL: .*$"
  90. value: "SECURITY_SCANNER_INDEXING_INTERVAL: 30"
  91. - after: "^SECURITY_SCANNER_INDEXING_INTERVAL: .*$"
  92. fixre: "^SECURITY_SCANNER_V4_PSK: .*$"
  93. value: "SECURITY_SCANNER_V4_PSK: NjA1aWhnNWk4MWhqNw=="
  94. - after: "^SECURITY_SCANNER_V4_PSK: .*$"
  95. fixre: "^SECURITY_SCANNER_V4_ENDPOINT: .*$"
  96. value: "SECURITY_SCANNER_V4_ENDPOINT: http://clair:8081"
  97. notify:
  98. - restart quay and wait for ready
  99. - name: Create Clair config directory if necessary.
  100. ansible.builtin.file:
  101. path: "{{ ansible_facts['user_dir'] }}/clair"
  102. state: directory
  103. mode: 0775
  104. - name: Publish Clair config if necessary.
  105. ansible.builtin.copy:
  106. dest: "{{ ansible_facts['user_dir'] }}/clair/config.yaml"
  107. content: |
  108. http_listen_addr: :8081
  109. introspection_addr: :8088
  110. #log_level: debug # too noisy
  111. log_level: info
  112. indexer:
  113. connstring: host=postgresql port=5432 dbname=clair user=quay password=secret sslmode=disable
  114. scanlock_retry: 10
  115. layer_scan_concurrency: 5
  116. migrations: true
  117. scanner:
  118. repo:
  119. rhel-repository-scanner:
  120. repo2cpe_mapping_file: /data/repository-to-cpe.json
  121. package:
  122. rhel_containerscanner:
  123. name2repos_mapping_file: /data/container-name-repos-map.json
  124. airgap: true
  125. matcher:
  126. connstring: host=postgresql port=5432 dbname=clair user=quay password=secret sslmode=disable
  127. max_conn_pool: 100
  128. migrations: true
  129. indexer_addr: clair-indexer
  130. disable_updaters: true
  131. notifier:
  132. connstring: host=postgresql port=5432 dbname=clair user=quay password=secret sslmode=disable
  133. delivery_interval: 1m
  134. poll_interval: 5m
  135. migrations: true
  136. updaters:
  137. config:
  138. rhel:
  139. ignore_unpatched: false
  140. auth:
  141. psk:
  142. key: "NjA1aWhnNWk4MWhqNw=="
  143. iss: ["quay"]
  144. metrics:
  145. name: "prometheus"
  146. mode: 0664
  147. notify:
  148. - restart quay and wait for ready
  149. - restart clair
  150. - name: Ensure same TLS trust will be used for Clair as for workstation.
  151. ansible.builtin.copy:
  152. src: /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
  153. dest: "{{ ansible_facts['user_dir'] }}/tls-ca-bundle.pem"
  154. mode: 0664
  155. notify:
  156. - restart clair
  157. - name: Ensure Clair service unit is there.
  158. ansible.builtin.template:
  159. dest: "{{ ansible_facts['user_dir'] }}/.config/systemd/user/clair.service"
  160. src: "templates/clair.service.j2"
  161. - name: Reload systemd.
  162. ansible.builtin.systemd_service:
  163. daemon_reload: yes
  164. scope: user
  165. - name: Verify that the vulnerability definitions exist.
  166. containers.podman.podman_container_exec:
  167. name: postgresql
  168. command: psql -d clair -U postgres -t -c 'SELECT COUNT(id) FROM vuln'
  169. ignore_errors: yes
  170. register: clair_vuln
  171. - name: Remember the number of vulnerability definitions in clair.
  172. ansible.builtin.set_fact:
  173. clair_nvuln: "{{ clair_vuln.stdout_lines[0] | default(0) | trim | int }}"
  174. - name: Import clair backup if vulnerabilities seem to be missing.
  175. block:
  176. - name: Ensure clair service is stopped.
  177. ansible.builtin.systemd_service:
  178. name: clair
  179. scope: user
  180. state: stopped
  181. - name: Ensure clair container is stopped.
  182. containers.podman.podman_container:
  183. name: clair
  184. image: "{{ registry_host }}/quay/clair-rhel8:v{{ quay_version }}"
  185. state: stopped
  186. # NOTE: This is required due to SELinux context changes for the container task below.
  187. - name: Ensure backup file is owned by quay user.
  188. remote_user: lab
  189. become: yes
  190. ansible.builtin.file:
  191. path: /local/backups/clair.backup
  192. owner: quay
  193. group: quay
  194. mode: 0644
  195. - name: Create a temporary pgpass file
  196. ansible.builtin.copy:
  197. dest: /tmp/pgpass
  198. owner: quay
  199. group: quay
  200. mode: 0600
  201. content: |
  202. postgresql:5432:clair:postgres:verysecret
  203. - name: Ensure the pgpass file is owned by postgres user of the container.
  204. become_method: containers.podman.podman_unshare
  205. become: yes
  206. ansible.builtin.file:
  207. path: /tmp/pgpass
  208. state: file
  209. owner: 26
  210. - name: Run pg_restore in a clair_import container (should take about 5-10 min).
  211. containers.podman.podman_container:
  212. name: clair_import
  213. image: "{{ registry_host }}/rhel9/postgresql-15:latest"
  214. rm: yes
  215. detach: no
  216. network:
  217. - quay
  218. volumes:
  219. - /local/backups/clair.backup:/clair.backup:Z
  220. - /tmp/pgpass:/var/lib/pgsql/.pgpass:Z
  221. command:
  222. - pg_restore
  223. - -dclair
  224. - -Upostgres
  225. - -hpostgresql
  226. - -c
  227. - /clair.backup
  228. state: started
  229. register: clair_import
  230. ignore_errors: yes
  231. failed_when:
  232. - (clair_import.stderr | regex_search('FATAL')) is defined
  233. - (clair_import.stderr | regex_search('FATAL')) != ""
  234. - debug: var=clair_import
  235. - name: Restore the ownership of the file.
  236. become_method: containers.podman.podman_unshare
  237. become: yes
  238. ansible.builtin.file:
  239. path: /tmp/pgpass
  240. state: file
  241. owner: 0
  242. - name: Remove the pgpass file
  243. ansible.builtin.file:
  244. path: /tmp/pgpass
  245. state: absent
  246. when:
  247. - clair_nvuln < 5000000
  248. - name: Enable services and start them.
  249. ansible.builtin.systemd_service:
  250. name: clair
  251. scope: user
  252. state: started
  253. enabled: yes
  254. handlers:
  255. - name: restart quay
  256. listen: restart quay and wait for ready
  257. ansible.builtin.systemd_service:
  258. name: quay
  259. scope: user
  260. state: restarted
  261. - name: wait for quay to become ready again
  262. listen: restart quay and wait for ready
  263. ansible.builtin.uri:
  264. method: GET
  265. url: https://registry.ocp4.example.com/
  266. headers:
  267. Accept: application/json
  268. Content-Type: application/json
  269. validate_certs: no
  270. status_code:
  271. - 200
  272. - 404
  273. - 502
  274. register: startup_wait
  275. until: startup_wait.status == 200
  276. retries: 30
  277. delay: 5
  278. - name: restart clair
  279. ansible.builtin.systemd_service:
  280. name: clair
  281. scope: user
  282. state: restarted
  283. ...