Debian Codegraph @

test/integration/targets/win_become/tasks/main.yml

raw
1 - set_fact:
2 become_test_username: ansible_become_test
3 become_test_admin_username: ansible_become_admin
4 gen_pw: "{{ 'password123!' + lookup('password', '/dev/null chars=ascii_letters,digits length=8') }}"
5
6 - name: create unprivileged user
7 win_user:
8 name: "{{ become_test_username }}"
9 password: "{{ gen_pw }}"
10 update_password: always
11 groups: Users
12 register: user_limited_result
13
14 - name: create a privileged user
15 win_user:
16 name: "{{ become_test_admin_username }}"
17 password: "{{ gen_pw }}"
18 update_password: always
19 groups: Administrators
20 register: user_admin_result
21
22 - name: add requisite logon rights for test user
23 win_user_right:
24 name: '{{item}}'
25 users: '{{become_test_username}}'
26 action: add
27 with_items:
28 - SeNetworkLogonRight
29 - SeInteractiveLogonRight
30 - SeBatchLogonRight
31
32 - name: fetch current target date/time for log filtering
33 raw: '[datetime]::now | Out-String'
34 register: test_starttime
35
36 - name: execute tests and ensure that test user is deleted regardless of success/failure
37 block:
38 - name: ensure current user is not the become user
39 win_whoami:
40 register: whoami_out
41 failed_when: whoami_out.account.sid == user_limited_result.sid or whoami_out.account.sid == user_admin_result.sid
42
43 - name: get become user profile dir so we can clean it up later
44 vars: &become_vars
45 ansible_become_user: "{{ become_test_username }}"
46 ansible_become_password: "{{ gen_pw }}"
47 ansible_become_method: runas
48 ansible_become: yes
49 win_shell: $env:USERPROFILE
50 register: profile_dir_out
51
52 - name: ensure profile dir contains test username (eg, if become fails silently, prevent deletion of real user profile)
53 assert:
54 that:
55 - become_test_username in profile_dir_out.stdout_lines[0]
56
57 - name: get become admin user profile dir so we can clean it up later
58 vars: &admin_become_vars
59 ansible_become_user: "{{ become_test_admin_username }}"
60 ansible_become_password: "{{ gen_pw }}"
61 ansible_become_method: runas
62 ansible_become: yes
63 win_shell: $env:USERPROFILE
64 register: admin_profile_dir_out
65
66 - name: ensure profile dir contains admin test username
67 assert:
68 that:
69 - become_test_admin_username in admin_profile_dir_out.stdout_lines[0]
70
71 - name: test become runas via task vars (underprivileged user)
72 vars: *become_vars
73 win_whoami:
74 register: whoami_out
75
76 - name: verify output
77 assert:
78 that:
79 - whoami_out.account.sid == user_limited_result.sid
80 - whoami_out.account.account_name == become_test_username
81 - whoami_out.label.account_name == 'Medium Mandatory Level'
82 - whoami_out.label.sid == 'S-1-16-8192'
83 - whoami_out.logon_type == 'Interactive'
84
85 - name: test become runas via task vars (privileged user)
86 vars: *admin_become_vars
87 win_whoami:
88 register: whoami_out
89
90 - name: verify output
91 assert:
92 that:
93 - whoami_out.account.sid == user_admin_result.sid
94 - whoami_out.account.account_name == become_test_admin_username
95 - whoami_out.label.account_name == 'High Mandatory Level'
96 - whoami_out.label.sid == 'S-1-16-12288'
97 - whoami_out.logon_type == 'Interactive'
98
99 - name: test become runas via task keywords
100 vars:
101 ansible_become_password: "{{ gen_pw }}"
102 become: yes
103 become_method: runas
104 become_user: "{{ become_test_username }}"
105 win_shell: whoami
106 register: whoami_out
107
108 - name: verify output
109 assert:
110 that:
111 - whoami_out.stdout_lines[0].endswith(become_test_username)
112
113 - name: test become via block vars
114 vars: *become_vars
115 block:
116 - name: ask who the current user is
117 win_whoami:
118 register: whoami_out
119
120 - name: verify output
121 assert:
122 that:
123 - whoami_out.account.sid == user_limited_result.sid
124 - whoami_out.account.account_name == become_test_username
125 - whoami_out.label.account_name == 'Medium Mandatory Level'
126 - whoami_out.label.sid == 'S-1-16-8192'
127 - whoami_out.logon_type == 'Interactive'
128
129 - name: test with module that will return non-zero exit code (https://github.com/ansible/ansible/issues/30468)
130 vars: *become_vars
131 setup:
132
133 - name: test become with invalid password
134 win_whoami:
135 vars:
136 ansible_become_pass: '{{ gen_pw }}abc'
137 become: yes
138 become_method: runas
139 become_user: '{{ become_test_username }}'
140 register: become_invalid_pass
141 failed_when:
142 - '"Failed to become user " + become_test_username not in become_invalid_pass.msg'
143 - '"LogonUser failed" not in become_invalid_pass.msg'
144 - '"Win32ErrorCode 1326 - 0x0000052E)" not in become_invalid_pass.msg'
145
146 - name: test become password precedence
147 win_whoami:
148 become: yes
149 become_method: runas
150 become_user: '{{ become_test_username }}'
151 vars:
152 ansible_become_pass: broken
153 ansible_runas_pass: '{{ gen_pw }}' # should have a higher precedence than ansible_become_pass
154
155 - name: test become + async
156 vars: *become_vars
157 win_command: whoami
158 async: 10
159 register: whoami_out
160
161 - name: verify become + async worked
162 assert:
163 that:
164 - whoami_out is successful
165 - become_test_username in whoami_out.stdout
166
167 - name: test failure with string become invalid key
168 vars: *become_vars
169 win_whoami:
170 become_flags: logon_type=batch invalid_flags=a
171 become_method: runas
172 register: failed_flags_invalid_key
173 ignore_errors: true
174
175 - name: test failure with invalid logon_type
176 vars: *become_vars
177 win_whoami:
178 become_flags: logon_type=invalid
179 register: failed_flags_invalid_type
180 ignore_errors: true
181
182 - name: test failure with invalid logon_flag
183 vars: *become_vars
184 win_whoami:
185 become_flags: logon_flags=with_profile,invalid
186 register: failed_flags_invalid_flag
187 ignore_errors: true
188
189 - name: assert test failures with invalid runas options
190 assert:
191 that:
192 - failed_flags_invalid_key.msg is contains "become_flags key 'invalid_flags' is not a valid runas flag, must be 'logon_type' or 'logon_flags'"
193 - >-
194 failed_flags_invalid_type.msg is contains "become_flags logon_type value 'invalid' is not valid, valid values are: interactive, network, batch, service, unlock, network_cleartext, new_credentials"
195 - >-
196 failed_flags_invalid_flag.msg is contains "become_flags logon_flags value 'invalid' is not valid, valid values are: none, with_profile, netcredentials_only"
197
198 - name: echo some non ascii characters
199 win_command: cmd.exe /c echo über den Fußgängerübergang gehen
200 vars: *become_vars
201 register: nonascii_output
202
203 - name: assert echo some non ascii characters
204 assert:
205 that:
206 - nonascii_output is changed
207 - nonascii_output.rc == 0
208 - nonascii_output.stdout_lines|count == 1
209 - nonascii_output.stdout_lines[0] == 'über den Fußgängerübergang gehen'
210 - nonascii_output.stderr == ''
211
212 - name: get PS events containing password or module args created since test start
213 raw: |
214 $dt=[datetime]"{{ test_starttime.stdout|trim }}"
215 (Get-WinEvent -LogName Microsoft-Windows-Powershell/Operational |
216 ? { $_.TimeCreated -ge $dt -and $_.Message -match "{{ gen_pw }}" }).Count
217 register: ps_log_count
218
219 - name: assert no PS events contain password or module args
220 assert:
221 that:
222 - ps_log_count.stdout | int == 0
223
224 # FUTURE: test raw + script become behavior once they're running under the exec wrapper again
225 # FUTURE: add standalone playbook tests to include password prompting and play become keywords
226
227 always:
228 - name: remove explicit logon rights for test user
229 win_user_right:
230 name: '{{item}}'
231 users: '{{become_test_username}}'
232 action: remove
233 with_items:
234 - SeNetworkLogonRight
235 - SeInteractiveLogonRight
236 - SeBatchLogonRight
237
238 - name: ensure underprivileged test user is deleted
239 win_user:
240 name: "{{ become_test_username }}"
241 state: absent
242
243 - name: ensure privileged test user is deleted
244 win_user:
245 name: "{{ become_test_admin_username }}"
246 state: absent
247
248 - name: ensure underprivileged test user profile is deleted
249 # NB: have to work around powershell limitation of long filenames until win_file fixes it
250 win_shell: rmdir /S /Q {{ profile_dir_out.stdout_lines[0] }}
251 args:
252 executable: cmd.exe
253 when: become_test_username in profile_dir_out.stdout_lines[0] | default([])
254
255 - name: ensure privileged test user profile is deleted
256 # NB: have to work around powershell limitation of long filenames until win_file fixes it
257 win_shell: rmdir /S /Q {{ admin_profile_dir_out.stdout_lines[0] }}
258 args:
259 executable: cmd.exe
260 when: become_test_admin_username in admin_profile_dir_out.stdout_lines[0] | default([])