|
28 | 28 | command: vault auth enable jwt |
29 | 29 | when: not vault_auth_jwt |
30 | 30 |
|
31 | | -- name: Get router CA certificate |
32 | | - kubernetes.core.k8s_info: |
33 | | - kind: Secret |
34 | | - namespace: openshift-ingress-operator |
35 | | - name: router-ca |
36 | | - api_version: v1 |
37 | | - register: router_ca_cert |
38 | | - when: not vault_auth_jwt |
| 31 | +- name: Split url into host and port |
| 32 | + ansible.builtin.set_fact: |
| 33 | + oidc_discovery_host: "{{ oidc_discovery_url | urlsplit('hostname') }}" |
| 34 | + oidc_discovery_port: "{{ oidc_discovery_url | urlsplit('port') | default('443', true) }}" |
39 | 35 |
|
40 | | -- name: Copy router CA certificate to vault |
41 | | - kubernetes.core.k8s_cp: |
| 36 | +- name: Check if OIDC endpoint is reachable |
| 37 | + kubernetes.core.k8s_exec: |
42 | 38 | namespace: "{{ vault_ns }}" |
43 | 39 | pod: "{{ vault_pod }}" |
44 | | - content: "{{ router_ca_cert.resources[0].data['tls.crt'] | b64decode }}" |
45 | | - remote_path: /tmp/router-ca.crt |
46 | | - when: not vault_auth_jwt |
| 40 | + command: > |
| 41 | + curl -fsk -o /dev/null -w "%{http_code}" {{ oidc_discovery_url }}/.well-known/openid-configuration |
| 42 | + register: oidc_discovery_reachable |
| 43 | + until: oidc_discovery_reachable.rc == 0 and oidc_discovery_reachable.stdout | int == 200 |
| 44 | + retries: 20 |
| 45 | + delay: 45 |
| 46 | + changed_when: false |
| 47 | + failed_when: oidc_discovery_reachable.rc != 0 or oidc_discovery_reachable.stdout | int != 200 |
| 48 | + |
| 49 | +- name: Check JWT discovery configuration |
| 50 | + kubernetes.core.k8s_exec: |
| 51 | + namespace: "{{ vault_ns }}" |
| 52 | + pod: "{{ vault_pod }}" |
| 53 | + command: > |
| 54 | + vault read auth/jwt/config -format=json |
| 55 | + register: jwt_discovery_config_json |
| 56 | + changed_when: false |
| 57 | + failed_when: false |
| 58 | + |
| 59 | +- name: Set jwt_discovery fact |
| 60 | + ansible.builtin.set_fact: |
| 61 | + jwt_discovery: "{{ true if jwt_discovery_config_json.stdout_lines | length > 0 else false }}" |
| 62 | + |
| 63 | +- name: Set JWT discovery configuration fact |
| 64 | + ansible.builtin.set_fact: |
| 65 | + jwt_discovery_config: "{{ jwt_discovery_config_json.stdout | from_json }}" |
| 66 | + when: jwt_discovery |
| 67 | + |
| 68 | +- name: Set JWT discovery configuration facts |
| 69 | + ansible.builtin.set_fact: |
| 70 | + jwt_config_oidc_discovery_url: "{{ jwt_discovery_config.data.oidc_discovery_url }}" |
| 71 | + jwt_config_default_role: "{{ jwt_discovery_config.data.default_role }}" |
| 72 | + when: jwt_discovery |
| 73 | + |
| 74 | +- name: Get OIDC discovery certificate |
| 75 | + kubernetes.core.k8s_exec: |
| 76 | + namespace: "{{ vault_ns }}" |
| 77 | + pod: "{{ vault_pod }}" |
| 78 | + command: > |
| 79 | + bash -e -c |
| 80 | + "echo -n | openssl s_client -connect {{ oidc_discovery_host }}:{{ oidc_discovery_port }} -servername {{ oidc_discovery_host }} |
| 81 | + | openssl x509 -outform PEM > /tmp/oidc-discovery-certificate.pem" |
| 82 | + when: not vault_auth_jwt or |
| 83 | + not jwt_discovery or |
| 84 | + not jwt_config_oidc_discovery_url == oidc_discovery_url or |
| 85 | + not jwt_config_default_role == default_role | default('default') |
47 | 86 |
|
48 | 87 | - name: Write JWT configuration |
49 | 88 | kubernetes.core.k8s_exec: |
|
53 | 92 | vault write auth/jwt/config |
54 | 93 | oidc_discovery_url={{ oidc_discovery_url }} |
55 | 94 | default_role={{ default_role | default('default') }} |
56 | | - oidc_discovery_ca_pem=@/tmp/router-ca.crt |
57 | | - when: not vault_auth_jwt |
| 95 | + oidc_discovery_ca_pem=@/tmp/oidc-discovery-certificate.pem |
| 96 | + when: not vault_auth_jwt or |
| 97 | + not jwt_discovery or |
| 98 | + not jwt_config_oidc_discovery_url == oidc_discovery_url or |
| 99 | + not jwt_config_default_role == default_role | default('default') |
| 100 | + |
| 101 | +- name: Get JWT role configuration |
| 102 | + kubernetes.core.k8s_exec: |
| 103 | + namespace: "{{ vault_ns }}" |
| 104 | + pod: "{{ vault_pod }}" |
| 105 | + command: > |
| 106 | + vault read auth/jwt/role/{{ default_role | default('default') }} -format=json |
| 107 | + register: jwt_role_config_json |
| 108 | + changed_when: false |
| 109 | + failed_when: false |
| 110 | + |
| 111 | +- name: Set jwt_role fact |
| 112 | + ansible.builtin.set_fact: |
| 113 | + jwt_role: "{{ true if jwt_role_config_json.stdout_lines | length > 0 else false }}" |
| 114 | + |
| 115 | +- name: Set JWT role configuration fact |
| 116 | + ansible.builtin.set_fact: |
| 117 | + jwt_role_config: "{{ jwt_role_config_json.stdout | from_json }}" |
| 118 | + when: jwt_role |
| 119 | + |
| 120 | +- name: Set JWT role configuration facts |
| 121 | + ansible.builtin.set_fact: |
| 122 | + jwt_role_config_bound_audiences: "{{ jwt_role_config.data.bound_audiences[0] | default('') }}" |
| 123 | + jwt_role_config_bound_subject: "{{ jwt_role_config.data.bound_subject }}" |
| 124 | + jwt_role_config_token_ttl: "{{ jwt_role_config.data.token_ttl }}" |
| 125 | + jwt_role_config_token_policies: "{{ jwt_role_config.data.token_policies[0] | default('') }}" |
| 126 | + when: jwt_role |
58 | 127 |
|
59 | 128 | - name: Write JWT role |
60 | 129 | kubernetes.core.k8s_exec: |
|
66 | 135 | user_claim=sub |
67 | 136 | bound_audiences={{ spiffe_audience }} |
68 | 137 | bound_subject={{ spiffe_subject }} |
69 | | - token_ttl={{ token_ttl | default('24h') }} |
70 | | - token_policies={{ vault_global_policy }}-secret |
71 | | - when: not vault_auth_jwt |
| 138 | + token_ttl={{ token_ttl | default('86400') }} |
| 139 | + token_policies={{ role_policy | default('{}-secret'.format(vault_global_policy)) }} |
| 140 | + when: not vault_auth_jwt or |
| 141 | + not jwt_role or |
| 142 | + not jwt_role_config_bound_audiences == spiffe_audience or |
| 143 | + not jwt_role_config_bound_subject == spiffe_subject or |
| 144 | + not jwt_role_config_token_ttl == token_ttl | default('86400') or |
| 145 | + not jwt_role_config_token_policies == role_policy | default('{}-secret'.format(vault_global_policy)) |
72 | 146 |
|
73 | 147 | - name: Delete router CA certificate |
74 | 148 | kubernetes.core.k8s_exec: |
75 | 149 | namespace: "{{ vault_ns }}" |
76 | 150 | pod: "{{ vault_pod }}" |
77 | | - command: rm -f /tmp/router-ca.crt |
| 151 | + command: rm -f /tmp/oidc-discovery-certificate.pem |
78 | 152 | when: not vault_auth_jwt |
0 commit comments