main.yml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. ---
  2. # Ensures there is an OIDC identity provider configured in OpenShift, that uses
  3. # a client defined in RHBK deployed by the deploy-rhbk role.
  4. #
  5. # Required variables (some are reused from deploy-rhbk role):
  6. #
  7. # openshift:
  8. # rhbk_client_id: the name of a client above to use for authentication (default "openshift")
  9. # create_groups: whether to create the groups from realm in OpenShift as well (default yes)
  10. #
  11. # rhbk:
  12. # namespace: namespace to deploy to (keycloak)
  13. # name: name of the instance (sso)
  14. # fqdn: fqdn of the route (hostname), detected if omitted
  15. # realm: name of the realm (sample-realm)
  16. # clients:[] a list of clients in the realm, must include openshift.rhbk_client_id
  17. # groups:[] groups to create, this time create them in OpenShift
  18. #
  19. # TODO: prerequisite check:
  20. # - either a fqdn or an existing keycloak resource coordinates
  21. # - admin credentials
  22. #
  23. # TODO: console.spec.authentication.logoutRedirect (config.openshift.io/v1)
  24. # should be patched with a logout URL
  25. #
  26. - name: Check that the ingresscontroller's defaultCertificate is set
  27. kubernetes.core.k8s_info:
  28. kubeconfig: tmp/kubeconfig-ocp4
  29. validate_certs: no
  30. api_version: operator.openshift.io/v1
  31. kind: ingresscontroller
  32. namespace: openshift-ingress-operator
  33. name: default
  34. register: ingress_ca
  35. - name: Get the router's default CA content
  36. kubernetes.core.k8s_info:
  37. kubeconfig: tmp/kubeconfig-ocp4
  38. validate_certs: no
  39. api_version: v1
  40. kind: secret
  41. namespace: openshift-config
  42. name: "{{ ingress_ca.resources[0].spec.defaultCertificate.name }}"
  43. register: ingress_ca
  44. - name: Store the CA cert as an actual fact
  45. ansible.builtin.set_fact:
  46. ingress_ca: "{{ ingress_ca.resources[0].data['tls.crt'] }}"
  47. - name: Check on oauth/cluster
  48. kubernetes.core.k8s_info:
  49. kubeconfig: tmp/kubeconfig-ocp4
  50. validate_certs: no
  51. api_version: config.openshift.io/v1
  52. kind: oauth
  53. name: cluster
  54. register: cluster_auth
  55. - ansible.builtin.assert:
  56. that:
  57. - (cluster_auth.resources | length) == 1
  58. - (cluster_auth.resources[0].spec.identityProviders | length) >= 1
  59. - cluster_auth.resources[0].spec.identityProviders[0].type == "HTPasswd"
  60. fail_msg: "ERROR: OpenShift cluster authentication is not configured correctly."
  61. success_msg: "OK: OpenShift cluster authentication is configured correctly."
  62. - name: If there is no FQDN, check what the default domain of the cluster is.
  63. kubernetes.core.k8s_info:
  64. kubeconfig: tmp/kubeconfig-ocp4
  65. validate_certs: no
  66. api_version: operator.openshift.io/v1
  67. kind: ingresscontroller
  68. namespace: openshift-ingress-operator
  69. name: default
  70. register: default_ingress
  71. when: rhbk.fqdn is not defined
  72. - name: Set a fact that reflects either the FQDN as set, or a composition of vars and default ingress info.
  73. ansible.builtin.set_fact:
  74. rhbk_fqdn: "{{ rhbk.fqdn | default((rhbk.name | default('sso')) + '-' + (rhbk.namespace | default('keycloak')) + '.' + default_ingress.resources[0].status.domain) }}"
  75. - name: Announce what hostname would be used.
  76. ansible.builtin.debug:
  77. msg: Using "https://{{ rhbk_fqdn }}" as the hostname.
  78. - name: Make certain router CA CM exists in openshift-config
  79. kubernetes.core.k8s:
  80. kubeconfig: tmp/kubeconfig-ocp4
  81. validate_certs: no
  82. api_version: v1
  83. kind: configmap
  84. namespace: openshift-config
  85. name: sso-ingress-ca
  86. definition:
  87. metadata:
  88. labels:
  89. app: sso
  90. data:
  91. ca.crt: "{{ ingress_ca | string | b64decode }}"
  92. - name: Make certain client secret exists in openshift-config
  93. kubernetes.core.k8s:
  94. kubeconfig: tmp/kubeconfig-ocp4
  95. validate_certs: no
  96. api_version: v1
  97. kind: secret
  98. namespace: openshift-config
  99. name: sso-client-secret
  100. definition:
  101. metadata:
  102. labels:
  103. app: sso
  104. type: Opaque
  105. data:
  106. clientSecret: "{{ (rhbk | community.general.json_query('clients[?id==`' + (openshift.rhbk_client_id | default('openshift')) + '`].secret'))[0] | b64encode }}"
  107. - name: Figure out what to do with oauth/cluster - option 1
  108. ansible.builtin.set_fact:
  109. oauth_op: add
  110. oauth_path: /spec/identityProviders/-
  111. when: (cluster_auth.resources[0].spec.identityProviders | length) == 1
  112. - name: Figure out what to do with oauth/cluster - option 2
  113. ansible.builtin.set_fact:
  114. oauth_op: replace
  115. oauth_path: /spec/identityProviders/1
  116. when: (cluster_auth.resources[0].spec.identityProviders | length) == 2
  117. - name: Patch oauth/cluster
  118. kubernetes.core.k8s_json_patch:
  119. kubeconfig: tmp/kubeconfig-ocp4
  120. validate_certs: no
  121. api_version: config.openshift.io/v1
  122. kind: oauth
  123. name: cluster
  124. patch:
  125. - op: "{{ oauth_op }}"
  126. path: "{{ oauth_path }}"
  127. value:
  128. name: oidc
  129. mappingMethod: claim
  130. type: OpenID
  131. openID:
  132. clientID: "{{ openshift.rhbk_client_id | default('openshift') }}"
  133. clientSecret:
  134. name: sso-client-secret
  135. ca:
  136. name: sso-ingress-ca
  137. claims:
  138. preferredUsername:
  139. - preferred_username
  140. name:
  141. - name
  142. email:
  143. - email
  144. groups:
  145. - groups
  146. issuer: "https://{{ rhbk_fqdn }}/realms/{{ rhbk.realm | default('sample-realm') }}"
  147. register: patched_oauth
  148. - name: Wait for OAuth to rollout if the resource was patched.
  149. block:
  150. - name: Wait for co/authentication to start progressing.
  151. kubernetes.core.k8s_info:
  152. kubeconfig: tmp/kubeconfig-ocp4
  153. validate_certs: no
  154. api_version: config.openshift.io/v1
  155. kind: clusteroperator
  156. name: authentication
  157. register: co_auth
  158. until:
  159. - co_auth.resources is defined
  160. - co_auth.resources | length == 1
  161. - ((co_auth.resources[0].status | community.general.json_query('conditions[?type==`Progressing`].status'))[0] | bool)
  162. retries: 60
  163. delay: 5
  164. - name: Wait for co/authentication to finish progressing.
  165. kubernetes.core.k8s_info:
  166. kubeconfig: tmp/kubeconfig-ocp4
  167. validate_certs: no
  168. api_version: config.openshift.io/v1
  169. kind: clusteroperator
  170. name: authentication
  171. register: co_auth
  172. until:
  173. - co_auth.resources is defined
  174. - co_auth.resources | length == 1
  175. - not ((co_auth.resources[0].status | community.general.json_query('conditions[?type==`Progressing`].status'))[0] | bool)
  176. retries: 60
  177. delay: 5
  178. when: patched_oauth.changed
  179. - name: Ensure OpenShift groups are there as well.
  180. kubernetes.core.k8s:
  181. kubeconfig: tmp/kubeconfig-ocp4
  182. validate_certs: no
  183. api_version: user.openshift.io/v1
  184. kind: group
  185. name: "{{ item }}"
  186. loop: "{{ rhbk.groups }}"
  187. ...