1
+ ---
2
+ # Copyright (c) Ansible Project
3
+ # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4
+ # SPDX-License-Identifier: GPL-3.0-or-later
5
+ - name : Install required packages
6
+ pip :
7
+ name :
8
+ - jmespath
9
+ - requests
10
+ register : result
11
+ until : result is success
12
+
13
+ - name : Start container
14
+ community.docker.docker_container :
15
+ name : mykeycloak
16
+ image : " quay.io/keycloak/keycloak:{{ keycloak_version }}"
17
+ command : start-dev
18
+ env :
19
+ KC_HTTP_RELATIVE_PATH : /auth
20
+ KEYCLOAK_ADMIN : admin
21
+ KEYCLOAK_ADMIN_PASSWORD : password
22
+ ports :
23
+ - " {{ keycloak_port }}:8080"
24
+ detach : true
25
+ auto_remove : true
26
+ memory : 2200M
27
+
28
+ - name : Wait for Keycloak
29
+ uri :
30
+ url : " {{ url }}/admin/"
31
+ status_code : 200
32
+ validate_certs : no
33
+ register : result
34
+ until : result.status == 200
35
+ retries : 10
36
+ delay : 10
37
+
38
+ - name : Delete realm if exists
39
+ community.general.keycloak_realm :
40
+ auth_keycloak_url : " {{ url }}"
41
+ auth_realm : " {{ admin_realm }}"
42
+ auth_username : " {{ admin_user }}"
43
+ auth_password : " {{ admin_password }}"
44
+ realm : " {{ realm }}"
45
+ state : absent
46
+
47
+ - name : Create realm
48
+ community.general.keycloak_realm :
49
+ auth_keycloak_url : " {{ url }}"
50
+ auth_realm : " {{ admin_realm }}"
51
+ auth_username : " {{ admin_user }}"
52
+ auth_password : " {{ admin_password }}"
53
+ id : " {{ realm }}"
54
+ realm : " {{ realm }}"
55
+ state : present
56
+
57
+ - name : Create an authentication flow from first broker login and add an execution to it.
58
+ community.general.keycloak_authentication :
59
+ auth_keycloak_url : " {{ url }}"
60
+ auth_realm : " {{ admin_realm }}"
61
+ auth_username : " {{ admin_user }}"
62
+ auth_password : " {{ admin_password }}"
63
+ realm : " {{ realm }}"
64
+ alias : " Test first broker login"
65
+ copyFrom : " first broker login"
66
+ authenticationExecutions :
67
+ - providerId : " idp-review-profile"
68
+ requirement : " REQUIRED"
69
+ authenticationConfig :
70
+ alias : " Test review profile config"
71
+ config :
72
+ update.profile.on.first.login : " missing"
73
+
74
+ - name : Create auth flow
75
+ community.general.keycloak_authentication :
76
+ auth_keycloak_url : " {{ url }}"
77
+ auth_realm : " {{ admin_realm }}"
78
+ auth_username : " {{ admin_user }}"
79
+ auth_password : " {{ admin_password }}"
80
+ realm : " {{ realm }}"
81
+ alias : " My conditionnal browser otp"
82
+ description : " browser based authentication with otp"
83
+ providerId : " basic-flow"
84
+ authenticationExecutions :
85
+ - displayName : Cookie
86
+ providerId : auth-cookie
87
+ requirement : ALTERNATIVE
88
+ - displayName : Kerberos
89
+ providerId : auth-spnego
90
+ requirement : DISABLED
91
+ - displayName : Identity Provider Redirector
92
+ providerId : identity-provider-redirector
93
+ requirement : ALTERNATIVE
94
+ - displayName : My browser otp forms
95
+ requirement : ALTERNATIVE
96
+ - displayName : Username Password Form
97
+ flowAlias : My browser otp forms
98
+ providerId : auth-username-password-form
99
+ requirement : REQUIRED
100
+ - displayName : My browser otp Browser - Conditional OTP
101
+ flowAlias : My browser otp forms
102
+ requirement : REQUIRED
103
+ providerId : " auth-conditional-otp-form"
104
+ authenticationConfig :
105
+ alias : my-conditional-otp-config
106
+ config :
107
+ defaultOtpOutcome : " force"
108
+ noOtpRequiredForHeaderPattern : " {{ keycloak_no_otp_required_pattern_orinale }}"
109
+ state : present
110
+
111
+ - name : Modified auth flow with new config
112
+ community.general.keycloak_authentication :
113
+ auth_keycloak_url : " {{ url }}"
114
+ auth_realm : " {{ admin_realm }}"
115
+ auth_username : " {{ admin_user }}"
116
+ auth_password : " {{ admin_password }}"
117
+ realm : " {{ realm }}"
118
+ alias : " My conditionnal browser otp"
119
+ description : " browser based authentication with otp"
120
+ providerId : " basic-flow"
121
+ authenticationExecutions :
122
+ - displayName : Cookie
123
+ providerId : auth-cookie
124
+ requirement : ALTERNATIVE
125
+ - displayName : Kerberos
126
+ providerId : auth-spnego
127
+ requirement : DISABLED
128
+ - displayName : Identity Provider Redirector
129
+ providerId : identity-provider-redirector
130
+ requirement : ALTERNATIVE
131
+ - displayName : My browser otp forms
132
+ requirement : ALTERNATIVE
133
+ - displayName : Username Password Form
134
+ flowAlias : My browser otp forms
135
+ providerId : auth-username-password-form
136
+ requirement : REQUIRED
137
+ - displayName : My browser otp Browser - Conditional OTP
138
+ flowAlias : My browser otp forms
139
+ requirement : REQUIRED
140
+ providerId : " auth-conditional-otp-form"
141
+ authenticationConfig :
142
+ alias : my-conditional-otp-config
143
+ config :
144
+ defaultOtpOutcome : " force"
145
+ noOtpRequiredForHeaderPattern : " {{ keycloak_no_otp_required_pattern_modifed }}"
146
+ state : present
147
+ register : result
148
+
149
+ - name : Retrive access
150
+ ansible.builtin.include_tasks :
151
+ file : access_token.yml
152
+
153
+ - name : Export realm
154
+ ansible.builtin.uri :
155
+ url : " {{ url }}/admin/realms/{{ realm }}/partial-export?exportClients=false&exportGroupsAndRoles=false"
156
+ method : POST
157
+ headers :
158
+ Accept : application/json
159
+ User-agent : Ansible
160
+ Authorization : " Bearer {{ access_token }}"
161
+ body_format : form-urlencoded
162
+ body : {}
163
+ register : exported_realm
164
+ no_log : true
165
+
166
+ - name : Assert `my-conditional-otp-config` exists only once
167
+ ansible.builtin.assert :
168
+ that :
169
+ - exported_realm.json | community.general.json_query('authenticatorConfig[?alias==`my-conditional-otp-config`]') | length == 1
170
+
171
+ - name : Delete auth flow
172
+ community.general.keycloak_authentication :
173
+ auth_keycloak_url : " {{ url }}"
174
+ auth_realm : " {{ admin_realm }}"
175
+ auth_username : " {{ admin_user }}"
176
+ auth_password : " {{ admin_password }}"
177
+ realm : " {{ realm }}"
178
+ alias : " My conditionnal browser otp"
179
+ state : absent
180
+ register : result
181
+
182
+ - name : Remove container
183
+ community.docker.docker_container :
184
+ name : mykeycloak
185
+ state : absent
0 commit comments