main.yml 5.3 KB

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