summaryrefslogtreecommitdiffstats
path: root/awx_collection/tests/integration/targets/credential/tasks/main.yml
blob: 34dd058d97d17cb275aa14cdfabc82731f847882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
---
- name: Generate a random string for test
  set_fact:
    test_id: "{{ lookup('password', '/dev/null chars=ascii_letters length=16') }}"
  when: test_id is not defined

- name: Generate names
  set_fact:
    ssh_cred_name1: "AWX-Collection-tests-credential-ssh-cred1-{{ test_id }}"
    ssh_cred_name2: "AWX-Collection-tests-credential-ssh-cred2-{{ test_id }}"
    ssh_cred_name3: "AWX-Collection-tests-credential-ssh-cred-lookup-source-{{ test_id }}"
    ssh_cred_name4: "AWX-Collection-tests-credential-ssh-cred-file-source-{{ test_id }}"
    vault_cred_name1: "AWX-Collection-tests-credential-vault-cred1-{{ test_id }}"
    vault_cred_name2: "AWX-Collection-tests-credential-vault-ssh-cred1-{{ test_id }}"
    net_cred_name1: "AWX-Collection-tests-credential-net-cred1-{{ test_id }}"
    scm_cred_name1: "AWX-Collection-tests-credential-scm-cred1-{{ test_id }}"
    aws_cred_name1: "AWX-Collection-tests-credential-aws-cred1-{{ test_id }}"
    vmware_cred_name1: "AWX-Collection-tests-credential-vmware-cred1-{{ test_id }}"
    sat6_cred_name1: "AWX-Collection-tests-credential-sat6-cred1-{{ test_id }}"
    gce_cred_name1: "AWX-Collection-tests-credential-gce-cred1-{{ test_id }}"
    azurerm_cred_name1: "AWX-Collection-tests-credential-azurerm-cred1-{{ test_id }}"
    openstack_cred_name1: "AWX-Collection-tests-credential-openstack-cred1-{{ test_id }}"
    rhv_cred_name1: "AWX-Collection-tests-credential-rhv-cred1-{{ test_id }}"
    insights_cred_name1: "AWX-Collection-tests-credential-insights-cred1-{{ test_id }}"
    tower_cred_name1: "AWX-Collection-tests-credential-tower-cred1-{{ test_id }}"

- name: create a tempdir for an SSH key
  local_action: shell mktemp -d
  register: tempdir

- name: Generate a local SSH key
  local_action: "shell ssh-keygen -b 2048 -t rsa -f {{ tempdir.stdout }}/id_rsa -q -N 'passphrase'"

- name: Read the generated key
  set_fact:
    ssh_key_data: "{{ lookup('file', tempdir.stdout + '/id_rsa') }}"

- name: Create an Org-specific credential with an ID
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    credential_type: Machine
    state: present
  register: result

- assert:
    that:
      - "result is changed"

- name: Create an Org-specific credential with an ID with exists
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    credential_type: Machine
    state: exists
  register: result

- assert:
    that:
      - "result is not changed"

- name: Delete an Org-specific credential with an ID
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    credential_type: Machine
    state: absent
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a credential without credential_type
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    state: absent
  register: result
  ignore_errors: true

- assert:
    that:
      - "result is failed"


- name: Create an Org-specific credential with an ID with exists
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    credential_type: Machine
    state: exists
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Org-specific credential
  credential:
    name: "{{ ssh_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

- assert:
    that:
      - "result is changed"

- name: Create the User-specific credential
  credential:
    name: "{{ ssh_cred_name1 }}"
    user: admin
    credential_type: 'Machine'
    state: present
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a User-specific credential
  credential:
    name: "{{ ssh_cred_name1 }}"
    user: admin
    state: absent
    credential_type: 'Machine'
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid SSH credential
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: present
    credential_type: Machine
    description: An example SSH credential
    inputs:
      username: joe
      password: secret
      become_method: sudo
      become_username: superuser
      become_password: supersecret
      ssh_key_data: "{{ ssh_key_data }}"
      ssh_key_unlock: "passphrase"
  register: result

- assert:
    that:
      - result is changed

- name: Create a valid SSH credential
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: present
    credential_type: Machine
    description: An example SSH credential
    inputs:
      username: joe
      become_method: sudo
      become_username: superuser
  register: result

- assert:
    that:
      - result is changed

- name: Check for inputs idempotency (when "inputs" is blank)
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: present
    credential_type: Machine
    description: An example SSH credential
  register: result

- assert:
    that:
      - result is not changed

- name: Copy ssh Credential
  credential:
    name: "copy_{{ ssh_cred_name2 }}"
    copy_from: "{{ ssh_cred_name2 }}"
    credential_type: Machine
  register: result

- assert:
    that:
      - result.copied

- name: Delete an SSH credential
  credential:
    name: "copy_{{ ssh_cred_name2 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid SSH credential from lookup source
  credential:
    name: "{{ ssh_cred_name3 }}"
    organization: Default
    state: present
    credential_type: Machine
    description: An example SSH credential from lookup source
    inputs:
      username: joe
      password: secret
      become_method: sudo
      become_username: superuser
      become_password: supersecret
      ssh_key_data: "{{ lookup('file', tempdir.stdout + '/id_rsa') }}"
      ssh_key_unlock: "passphrase"
  register: result

- assert:
    that:
      - result is changed

- name: Delete an SSH credential
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

- assert:
    that:
      - "result is changed"

- name: Ensure existence of SSH credential
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: exists
    credential_type: Machine
    description: An example SSH awx.awx.credential
    inputs:
      username: joe
      password: secret
      become_method: sudo
      become_username: superuser
      become_password: supersecret
      ssh_key_data: "{{ ssh_key_data }}"
      ssh_key_unlock: "passphrase"
  register: result

- assert:
    that:
      - result is changed

- name: Ensure existence of SSH credential, not updating any inputs
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: exists
    credential_type: Machine
    description: An example SSH awx.awx.credential
    inputs:
      username: joe
      password: no-update-secret
      become_method: sudo
      become_username: some-other-superuser
      become_password: some-other-secret
      ssh_key_data: "{{ ssh_key_data }}"
      ssh_key_unlock: "another-pass-phrase"
  register: result

- assert:
    that:
      - result is not changed

- name: Create an invalid SSH credential (passphrase required)
  credential:
    name: SSH Credential
    organization: Default
    state: present
    credential_type: Machine
    inputs:
      username: joe
      ssh_key_data: "{{ ssh_key_data }}"
  ignore_errors: true
  register: result

- assert:
    that:
      - "result is failed"
      - "'must be set when SSH key is encrypted' in result.msg"

- name: Create an invalid SSH credential (Organization not found)
  credential:
    name: SSH Credential
    organization: Missing_Organization
    state: present
    credential_type: Machine
    inputs:
      username: joe
  ignore_errors: true
  register: result

- assert:
    that:
      - "result is failed"
      - "result is not changed"
      - "'Missing_Organization' in result.msg"
      - "result.total_results == 0"

- name: Delete an SSH credential
  credential:
    name: "{{ ssh_cred_name2 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an SSH credential
  credential:
    name: "{{ ssh_cred_name3 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an SSH credential
  credential:
    name: "{{ ssh_cred_name4 }}"
    organization: Default
    state: absent
    credential_type: Machine
  register: result

# This one was never really created so it shouldn't be deleted
- assert:
    that:
      - "result is not changed"

- name: Create a valid Vault credential
  credential:
    name: "{{ vault_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Vault
    description: An example Vault credential
    inputs:
      vault_id: bar
      vault_password: secret-vault
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Vault credential
  credential:
    name: "{{ vault_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Vault
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Vault credential
  credential:
    name: "{{ vault_cred_name2 }}"
    organization: Default
    state: absent
    credential_type: Vault
  register: result

# The creation of vault_cred_name2 never worked so we shouldn't actually need to delete it
- assert:
    that:
      - "result is not changed"

- name: Create a valid Network credential
  credential:
    name: "{{ net_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Network
    inputs:
      username: joe
      password: secret
      authorize: true
      authorize_password: authorize-me
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Network credential
  credential:
    name: "{{ net_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Network
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid SCM credential
  credential:
    name: "{{ scm_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Source Control
    inputs:
      username: joe
      password: secret
      ssh_key_data: "{{ ssh_key_data }}"
      ssh_key_unlock: "passphrase"
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an SCM credential
  credential:
    name: "{{ scm_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Source Control
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid AWS credential
  credential:
    name: "{{ aws_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Amazon Web Services
    inputs:
      username: joe
      password: secret
      security_token: aws-token
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an AWS credential
  credential:
    name: "{{ aws_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Amazon Web Services
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid VMWare credential
  credential:
    name: "{{ vmware_cred_name1 }}"
    organization: Default
    state: present
    credential_type: VMware vCenter
    inputs:
      host: https://example.org
      username: joe
      password: secret
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an VMWare credential
  credential:
    name: "{{ vmware_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: VMware vCenter
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid Satellite6 credential
  credential:
    name: "{{ sat6_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Red Hat Satellite 6
    inputs:
      host: https://example.org
      username: joe
      password: secret
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Satellite6 credential
  credential:
    name: "{{ sat6_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Red Hat Satellite 6
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid GCE credential
  credential:
    name: "{{ gce_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Google Compute Engine
    inputs:
      username: joe
      project: ABC123
      ssh_key_data: "{{ ssh_key_data }}"
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a GCE credential
  credential:
    name: "{{ gce_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Google Compute Engine
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid AzureRM credential
  credential:
    name: "{{ azurerm_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Microsoft Azure Resource Manager
    inputs:
      username: joe
      password: secret
      subscription: some-subscription
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid AzureRM credential with a tenant
  credential:
    name: "{{ azurerm_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Microsoft Azure Resource Manager
    inputs:
      client: some-client
      secret: some-secret
      tenant: some-tenant
      subscription: some-subscription
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an AzureRM credential
  credential:
    name: "{{ azurerm_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Microsoft Azure Resource Manager
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid OpenStack credential
  credential:
    name: "{{ openstack_cred_name1 }}"
    organization: Default
    state: present
    credential_type: OpenStack
    inputs:
      host: https://keystone.example.org
      username: joe
      password: secret
      project: tenant123
      domain: some-domain
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a OpenStack credential
  credential:
    name: "{{ openstack_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: OpenStack
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid RHV credential
  credential:
    name: "{{ rhv_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Red Hat Virtualization
    inputs:
      host: https://example.org
      username: joe
      password: secret
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an RHV credential
  credential:
    name: "{{ rhv_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Red Hat Virtualization
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid Insights credential
  credential:
    name: "{{ insights_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Insights
    inputs:
      username: joe
      password: secret
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete an Insights credential
  credential:
    name: "{{ insights_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Insights
  register: result

- assert:
    that:
      - "result is changed"

- name: Create a valid Tower-to-Tower credential
  credential:
    name: "{{ tower_cred_name1 }}"
    organization: Default
    state: present
    credential_type: Red Hat Ansible Automation Platform
    inputs:
      host: https://controller.example.org
      username: joe
      password: secret
  register: result

- assert:
    that:
      - "result is changed"

- name: Delete a Tower-to-Tower credential
  credential:
    name: "{{ tower_cred_name1 }}"
    organization: Default
    state: absent
    credential_type: Red Hat Ansible Automation Platform
  register: result

- assert:
    that:
      - "result is changed"

- name: Check module fails with correct msg
  credential:
    name: test-credential
    description: Credential Description
    credential_type: Machine
    organization: test-non-existing-org
    state: present
  register: result
  ignore_errors: true

- assert:
    that:
      - "result is failed"
      - "result is not changed"
      - "'test-non-existing-org' in result.msg"
      - "result.total_results == 0"