main.yml 6.6 KB

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