main.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ---
  2. # Ensures that there are also users, groups, and roles in the test realm.
  3. - name: Check for the KeyCloak resource
  4. k8s_info:
  5. kubeconfig: tmp/kubeconfig-ocp4
  6. validate_certs: no
  7. api_version: keycloak.org/v1alpha1
  8. kind: keycloak
  9. namespace: rhsso
  10. name: rhsso
  11. register: sso_cr
  12. - assert:
  13. that:
  14. - (sso_cr.resources | length) == 1
  15. - sso_cr.resources[0].status.ready
  16. - sso_cr.resources[0].status.phase == "reconciling"
  17. fail_msg: "ERROR: RHSSO instance is missing or not configured correctly."
  18. success_msg: "OK: RHSSO instance is configured correctly."
  19. - name: Store RHSSO URL as a fact
  20. set_fact:
  21. sso_url: "{{ sso_cr.resources[0].status.externalURL }}"
  22. - name: Check for the realm resource
  23. k8s_info:
  24. kubeconfig: tmp/kubeconfig-ocp4
  25. validate_certs: no
  26. api_version: keycloak.org/v1alpha1
  27. kind: keycloakrealm
  28. namespace: rhsso
  29. name: sample-realm
  30. register: sso_realm
  31. - assert:
  32. that:
  33. - (sso_realm.resources | length) == 1
  34. - sso_realm.resources[0].spec.realm.id == "sample"
  35. - sso_realm.resources[0].spec.realm.realm == "sample"
  36. - sso_realm.resources[0].status.ready
  37. - sso_realm.resources[0].status.phase == "reconciling"
  38. fail_msg: "ERROR: RHSSO sample realm is missing or not configured correctly."
  39. success_msg: "OK: RHSSO sample realm is configured correctly."
  40. # Authentication bits from here until we can get group list.
  41. - name: Read the SSO admin pass
  42. k8s_info:
  43. kubeconfig: tmp/kubeconfig-ocp4
  44. validate_certs: no
  45. api_version: v1
  46. kind: secret
  47. namespace: rhsso
  48. name: "{{ sso_cr.resources[0].status.credentialSecret }}"
  49. register: sso_secret
  50. - name: Store RHSSO admin pass as fact
  51. set_fact:
  52. sso_pass: "{{ sso_secret.resources[0].data.ADMIN_PASSWORD }}"
  53. - name: Get an auth token from RHSSO
  54. uri:
  55. method: POST
  56. return_content: true
  57. validate_certs: false
  58. url: "{{ sso_url }}/auth/realms/master/protocol/openid-connect/token"
  59. headers:
  60. Accept: application/json
  61. body: "client_id=admin-cli&username=admin&password={{ sso_pass | string | b64decode }}&grant_type=password"
  62. register: sso_token_rsp
  63. - assert:
  64. that: sso_token_rsp.json is defined and sso_token_rsp.json.access_token is defined
  65. fail_msg: "ERROR: Failed to obtain authentication token from RHSSO."
  66. success_msg: "OK: got authentication token."
  67. - name: Store the token as a fact
  68. set_fact:
  69. sso_token: "{{ sso_token_rsp.json.access_token }}"
  70. # Back to business as usual from here on.
  71. - name: Get existing group list
  72. uri:
  73. method: GET
  74. return_content: true
  75. validate_certs: false
  76. url: "{{ sso_url }}/auth/admin/realms/sample/groups"
  77. headers:
  78. Authorization: Bearer {{ sso_token }}
  79. Accept: application/json
  80. register: sso_groups_raw
  81. tags:
  82. - groups
  83. - name: Store existing groups as a list
  84. set_fact:
  85. sso_groups: "{{ sso_groups_raw.json | items2dict(key_name='name', value_name='id') }}"
  86. tags:
  87. - groups
  88. - name: Create missing groups
  89. uri:
  90. method: POST
  91. return_content: true
  92. validate_certs: false
  93. url: "{{ sso_url }}/auth/admin/realms/sample/groups"
  94. headers:
  95. Authorization: Bearer {{ sso_token }}
  96. Accept: application/json
  97. Content-Type: application/json
  98. body_format: json
  99. body: '{"name": "{{ item | string }}"}'
  100. status_code:
  101. - 200
  102. - 201
  103. loop: "{{ pop_groups }}"
  104. when: item not in sso_groups.keys()
  105. tags:
  106. - groups
  107. # You need offline_access in realmRoles to be able to use OCP OIDC.
  108. - name: Make sure KeycloakUser resources exist
  109. k8s:
  110. kubeconfig: tmp/kubeconfig-ocp4
  111. validate_certs: no
  112. api_version: keycloak.org/v1alpha1
  113. kind: keycloakuser
  114. namespace: rhsso
  115. name: "user-{{ item.username }}"
  116. definition:
  117. metadata:
  118. labels:
  119. app: sso
  120. realm: sample
  121. spec:
  122. realmSelector:
  123. matchLabels:
  124. app: sso
  125. realm: sample
  126. user:
  127. username: "{{ item.username }}"
  128. credentials:
  129. - temporary: False
  130. type: password
  131. value: "{{ item.password }}"
  132. firstName: "{{ item.firstname }}"
  133. lastName: "{{ item.lastname }}"
  134. email: "{{ item.username }}@example.com"
  135. enabled: True
  136. emailVerified: True
  137. groups: "{{ item.groups | list }}"
  138. realmRoles:
  139. - offline_access
  140. loop: "{{ pop_users }}"
  141. tags:
  142. - users
  143. # TODO: assign roles to groups?
  144. # TODO: remove any stale identities / openshift users if keycloakuser resources have been created?
  145. ...