Debian Codegraph @

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

raw
1 # test code for the known_hosts module
2 # (c) 2017, Marius Gedminas <[email protected]>
3
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
18
19 - name: copy an existing file in place
20 copy:
21 src: existing_known_hosts
22 dest: "{{ remote_tmp_dir }}/known_hosts"
23
24 # test addition
25
26 - name: add a new host in check mode
27 check_mode: yes
28 known_hosts:
29 name: example.org
30 key: "{{ example_org_rsa_key }}"
31 state: present
32 path: "{{remote_tmp_dir}}/known_hosts"
33 register: diff
34
35 - name: assert that the diff looks as expected (the key was added at the end)
36 assert:
37 that:
38 - 'diff is changed'
39 - 'diff.diff.before_header == diff.diff.after_header == remote_tmp_dir|expanduser + "/known_hosts"'
40 - 'diff.diff.after.splitlines()[:-1] == diff.diff.before.splitlines()'
41 - 'diff.diff.after.splitlines()[-1] == example_org_rsa_key.strip()'
42
43 - name: add a new host
44 known_hosts:
45 name: example.org
46 key: "{{ example_org_rsa_key }}"
47 state: present
48 path: "{{remote_tmp_dir}}/known_hosts"
49 register: result
50
51 - name: get the file content
52 command: "cat {{remote_tmp_dir}}/known_hosts"
53 register: known_hosts
54
55 - name: assert that the key was added and ordering preserved
56 assert:
57 that:
58 - 'result is changed'
59 - 'known_hosts.stdout_lines[0].startswith("example.com")'
60 - 'known_hosts.stdout_lines[4].startswith("@cert-authority")'
61 - 'known_hosts.stdout_lines[5].startswith("# example.net")'
62 - 'known_hosts.stdout_lines[-1].strip() == example_org_rsa_key.strip()'
63
64 # test idempotence of addition
65
66 - name: add the same host in check mode
67 check_mode: yes
68 known_hosts:
69 name: example.org
70 key: "{{ example_org_rsa_key }}"
71 state: present
72 path: "{{remote_tmp_dir}}/known_hosts"
73 register: check
74
75 - name: assert that no changes were expected
76 assert:
77 that:
78 - 'check is not changed'
79 - 'check.diff.before == check.diff.after'
80
81 - name: add the same host
82 known_hosts:
83 name: example.org
84 key: "{{ example_org_rsa_key }}"
85 state: present
86 path: "{{remote_tmp_dir}}/known_hosts"
87 register: result
88
89 - name: get the file content
90 command: "cat {{remote_tmp_dir}}/known_hosts"
91 register: known_hosts_v2
92
93 - name: assert that no changes happened
94 assert:
95 that:
96 - 'result is not changed'
97 - 'result.diff.before == result.diff.after'
98 - 'known_hosts.stdout == known_hosts_v2.stdout'
99
100 # https://github.com/ansible/ansible/issues/78598
101 # test removing nonexistent host key when the other keys exist for the host
102 - name: remove different key
103 known_hosts:
104 name: example.org
105 key: "{{ example_org_ed25519_key }}"
106 state: absent
107 path: "{{remote_tmp_dir}}/known_hosts"
108 register: result
109
110 - name: remove nonexistent key with check mode
111 known_hosts:
112 name: example.org
113 key: "{{ example_org_ed25519_key }}"
114 state: absent
115 path: "{{remote_tmp_dir}}/known_hosts"
116 check_mode: yes
117 register: check_mode_result
118
119 - name: get the file content
120 command: "cat {{remote_tmp_dir}}/known_hosts"
121 register: known_hosts_different_key_removal
122
123 - name: assert that no changes happened
124 assert:
125 that:
126 - 'result is not changed'
127 - 'check_mode_result is not changed'
128 - 'result.diff.before == result.diff.after'
129 - 'known_hosts_v2.stdout == known_hosts_different_key_removal.stdout'
130
131 # test removal
132
133 - name: remove the host in check mode
134 check_mode: yes
135 known_hosts:
136 name: example.org
137 key: "{{ example_org_rsa_key }}"
138 state: absent
139 path: "{{remote_tmp_dir}}/known_hosts"
140 register: diff
141
142 - name: assert that the diff looks as expected (the key was removed)
143 assert:
144 that:
145 - 'diff.diff.before_header == diff.diff.after_header == remote_tmp_dir|expanduser + "/known_hosts"'
146 - 'diff.diff.before.splitlines()[-1] == example_org_rsa_key.strip()'
147 - 'diff.diff.after.splitlines() == diff.diff.before.splitlines()[:-1]'
148
149 - name: remove the host
150 known_hosts:
151 name: example.org
152 key: "{{ example_org_rsa_key }}"
153 state: absent
154 path: "{{remote_tmp_dir}}/known_hosts"
155 register: result
156
157 - name: get the file content
158 command: "cat {{remote_tmp_dir}}/known_hosts"
159 register: known_hosts_v3
160
161 - name: assert that the key was removed and ordering preserved
162 assert:
163 that:
164 - 'diff is changed'
165 - 'result is changed'
166 - '"example.org" not in known_hosts_v3.stdout'
167 - 'known_hosts_v3.stdout_lines[0].startswith("example.com")'
168 - 'known_hosts_v3.stdout_lines[-1].startswith("# example.net")'
169
170 # test idempotence of removal
171
172 - name: remove the same host in check mode
173 check_mode: yes
174 known_hosts:
175 name: example.org
176 key: "{{ example_org_rsa_key }}"
177 state: absent
178 path: "{{remote_tmp_dir}}/known_hosts"
179 register: check
180
181 - name: assert that no changes were expected
182 assert:
183 that:
184 - 'check is not changed'
185 - 'check.diff.before == check.diff.after'
186
187 - name: remove the same host
188 known_hosts:
189 name: example.org
190 key: "{{ example_org_rsa_key }}"
191 state: absent
192 path: "{{remote_tmp_dir}}/known_hosts"
193 register: result
194
195 - name: get the file content
196 command: "cat {{remote_tmp_dir}}/known_hosts"
197 register: known_hosts_v4
198
199 - name: assert that no changes happened
200 assert:
201 that:
202 - 'result is not changed'
203 - 'result.diff.before == result.diff.after'
204 - 'known_hosts_v3.stdout == known_hosts_v4.stdout'
205
206 # test addition as hashed_host
207
208 - name: add a new hashed host
209 known_hosts:
210 name: example.org
211 key: "{{ example_org_rsa_key }}"
212 state: present
213 path: "{{remote_tmp_dir}}/known_hosts"
214 hash_host: yes
215 register: result
216
217 - name: get the file content
218 command: "cat {{remote_tmp_dir}}/known_hosts"
219 register: known_hosts_v5
220
221 - name: assert that the key was added and ordering preserved
222 assert:
223 that:
224 - 'result is changed'
225 - 'known_hosts_v5.stdout_lines[0].startswith("example.com")'
226 - 'known_hosts_v5.stdout_lines[5].startswith("# example.net")'
227 - 'known_hosts_v5.stdout_lines[-1].strip().startswith("|1|")'
228 - 'known_hosts_v5.stdout_lines[-1].strip().endswith(example_org_rsa_key.strip().split()[-1])'
229
230 # test idempotence of hashed addition
231
232 - name: add the same host hashed
233 known_hosts:
234 name: example.org
235 key: "{{ example_org_rsa_key }}"
236 state: present
237 path: "{{remote_tmp_dir}}/known_hosts"
238 hash_host: yes
239 register: result
240
241 - name: get the file content
242 command: "cat {{remote_tmp_dir}}/known_hosts"
243 register: known_hosts_v6
244
245 - name: assert that no changes happened
246 assert:
247 that:
248 - 'result is not changed'
249 - 'result.diff.before == result.diff.after'
250 - 'known_hosts_v5.stdout == known_hosts_v6.stdout'
251
252 # test hashed removal
253
254 - name: remove the hashed host
255 known_hosts:
256 name: example.org
257 key: "{{ example_org_rsa_key }}"
258 state: absent
259 path: "{{remote_tmp_dir}}/known_hosts"
260 register: result
261
262 - name: get the file content
263 command: "cat {{remote_tmp_dir}}/known_hosts"
264 register: known_hosts_v7
265
266 - name: assert that the key was removed and ordering preserved
267 assert:
268 that:
269 - 'result is changed'
270 - 'example_org_rsa_key.strip().split()[-1] not in known_hosts_v7.stdout'
271 - 'known_hosts_v7.stdout_lines[0].startswith("example.com")'
272 - 'known_hosts_v7.stdout_lines[-1].startswith("# example.net")'
273
274 # test idempotence of removal
275
276 - name: remove the same hashed host
277 known_hosts:
278 name: example.org
279 key: "{{ example_org_rsa_key }}"
280 state: absent
281 path: "{{remote_tmp_dir}}/known_hosts"
282 register: result
283
284 - name: get the file content
285 command: "cat {{remote_tmp_dir}}/known_hosts"
286 register: known_hosts_v8
287
288 - name: assert that no changes happened
289 assert:
290 that:
291 - 'result is not changed'
292 - 'result.diff.before == result.diff.after'
293 - 'known_hosts_v7.stdout == known_hosts_v8.stdout'
294
295 # test roundtrip plaintext => hashed => plaintext
296 # The assertions are rather relaxed, because most of this hash been tested previously
297
298 - name: add a new host
299 known_hosts:
300 name: example.org
301 key: "{{ example_org_rsa_key }}"
302 state: present
303 path: "{{remote_tmp_dir}}/known_hosts"
304
305 - name: get the file content
306 command: "cat {{remote_tmp_dir}}/known_hosts"
307 register: known_hosts_v8
308
309 - name: assert the plaintext host is there
310 assert:
311 that:
312 - 'known_hosts_v8.stdout_lines[-1].strip() == example_org_rsa_key.strip()'
313
314 - name: update the host to hashed mode
315 known_hosts:
316 name: example.org
317 key: "{{ example_org_rsa_key }}"
318 state: present
319 path: "{{remote_tmp_dir}}/known_hosts"
320 hash_host: true
321
322 - name: get the file content
323 command: "cat {{remote_tmp_dir}}/known_hosts"
324 register: known_hosts_v9
325
326 - name: assert the hashed host is there
327 assert:
328 that:
329 - 'known_hosts_v9.stdout_lines[-1].strip().startswith("|1|")'
330 - 'known_hosts_v9.stdout_lines[-1].strip().endswith(example_org_rsa_key.strip().split()[-1])'
331
332 - name: downgrade the host to plaintext mode
333 known_hosts:
334 name: example.org
335 key: "{{ example_org_rsa_key }}"
336 state: present
337 path: "{{remote_tmp_dir}}/known_hosts"
338
339 - name: get the file content
340 command: "cat {{remote_tmp_dir}}/known_hosts"
341 register: known_hosts_v10
342
343 - name: assert the plaintext host is there
344 assert:
345 that:
346 - 'known_hosts_v10.stdout_lines[6].strip() == example_org_rsa_key.strip()'
347
348 # ... and remove the host again for the next test
349
350 - name: copy an existing file in place
351 copy:
352 src: existing_known_hosts
353 dest: "{{ remote_tmp_dir }}/known_hosts"
354
355 # Test key changes
356
357 - name: add a hashed host
358 known_hosts:
359 name: example.org
360 key: "{{ example_org_rsa_key }}"
361 state: present
362 path: "{{remote_tmp_dir}}/known_hosts"
363 hash_host: true
364
365 - name: change the key of a hashed host
366 known_hosts:
367 name: example.org
368 key: "{{ example_org_rsa_key.strip()[:-7] + 'RANDOM=' }}"
369 state: present
370 path: "{{remote_tmp_dir}}/known_hosts"
371 hash_host: true
372
373 - name: get the file content
374 command: "cat {{remote_tmp_dir}}/known_hosts"
375 register: known_hosts_v11
376
377 - name: assert the change took place and the key got modified
378 assert:
379 that:
380 - 'known_hosts_v11.stdout_lines[-1].strip().endswith("RANDOM=")'
381
382 - name: add the ed25519 host key
383 known_hosts:
384 name: host.example.com
385 key: "{{ host_example_com_ed25519_key }}"
386 state: present
387 path: "{{remote_tmp_dir}}/known_hosts"
388 register: result
389
390 - name: get the file content
391 command: "cat {{remote_tmp_dir}}/known_hosts"
392 register: known_hosts_v12
393
394 - name: assert that the key was added and ordering preserved
395 assert:
396 that:
397 - 'result is changed'
398 - 'known_hosts_v12.stdout_lines[0].startswith("example.com")'
399 - 'known_hosts_v12.stdout_lines[4].startswith("@cert-authority")'
400 - 'known_hosts_v12.stdout_lines[5].startswith("# example.net")'
401 - 'known_hosts_v12.stdout_lines[-1].strip() == host_example_com_ed25519_key.strip()'
402
403 - name: add the ed25519 ca key
404 known_hosts:
405 name: '*.example.com'
406 key: "{{ example_com_ed25519_ca }}"
407 state: present
408 path: "{{remote_tmp_dir}}/known_hosts"
409 register: result
410
411 - name: get the file content
412 command: "cat {{remote_tmp_dir}}/known_hosts"
413 register: known_hosts_v13
414
415 - name: assert that the key was added and ordering preserved
416 assert:
417 that:
418 - 'result is changed'
419 - 'known_hosts_v13.stdout_lines[0].startswith("example.com")'
420 - 'known_hosts_v13.stdout_lines[4].startswith("@cert-authority")'
421 - 'known_hosts_v13.stdout_lines[5].startswith("# example.net")'
422 - 'known_hosts_v13.stdout_lines[-1].strip() == example_com_ed25519_ca.strip()'
423
424 - name: Remove the ed25519 ca key
425 known_hosts:
426 name: '*.example.com'
427 key: "{{ example_com_ed25519_ca }}"
428 state: absent
429 path: "{{remote_tmp_dir}}/known_hosts"
430 register: result
431
432 - name: get the file content
433 command: "cat {{remote_tmp_dir}}/known_hosts"
434 register: known_hosts_v14
435
436 - name: assert that the key was removed and ordering preserved
437 assert:
438 that:
439 - 'result is changed'
440 - 'known_hosts_v12.stdout == known_hosts_v14.stdout'
441
442 - name: add the revoked ed25519 host key
443 known_hosts:
444 name: 'host.example.com'
445 key: "@revoked {{ host_example_com_ed25519_signedhost }}"
446 state: present
447 path: "{{remote_tmp_dir}}/known_hosts"
448 register: result
449
450 - name: get the file content
451 command: "cat {{remote_tmp_dir}}/known_hosts"
452 register: known_hosts_v15
453
454 - name: assert that the key was added and ordering preserved
455 assert:
456 that:
457 - 'result is changed'
458 - 'known_hosts_v15.stdout_lines[0].startswith("example.com")'
459 - 'known_hosts_v15.stdout_lines[4].startswith("@cert-authority")'
460 - 'known_hosts_v15.stdout_lines[5].startswith("# example.net")'
461 - 'known_hosts_v15.stdout_lines[-1].strip() == "@revoked " ~ host_example_com_ed25519_signedhost.strip()'
462
463 - name: remove the revoked ed25519 host key
464 known_hosts:
465 name: 'host.example.com'
466 key: "@revoked {{ host_example_com_ed25519_signedhost }}"
467 state: absent
468 path: "{{remote_tmp_dir}}/known_hosts"
469 register: result
470
471 - name: get the file content
472 command: "cat {{remote_tmp_dir}}/known_hosts"
473 register: known_hosts_v16
474
475 - name: assert that the key was removed and ordering preserved
476 assert:
477 that:
478 - 'result is changed'
479 - 'known_hosts_v12.stdout == known_hosts_v16.stdout'
480
481 # test errors
482
483 - name: Try using a comma separated list of hosts
484 known_hosts:
485 name: example.org,acme.com
486 key: "{{ example_org_rsa_key }}"
487 path: "{{remote_tmp_dir}}/known_hosts"
488 ignore_errors: yes
489 register: result
490
491 - name: Assert that error message was displayed
492 assert:
493 that:
494 - result is failed
495 - result.msg == 'Comma separated list of names is not supported. Please pass a single name to lookup in the known_hosts file.'
496
497 - name: Try using a name that does not match the key
498 known_hosts:
499 name: example.com
500 key: "{{ example_org_rsa_key }}"
501 path: "{{remote_tmp_dir}}/known_hosts"
502 ignore_errors: yes
503 register: result
504
505 - name: Assert that name checking failed with error message
506 assert:
507 that:
508 - result is failed
509 - result.msg == 'Host parameter does not match hashed host field in supplied key'
510 - result.rc is defined