main.yml 6.5 KB

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