| 1 |
--- |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
- name: Run tests if GPG is available |
| 7 |
when: has_gnupg |
| 8 |
block: |
| 9 |
- name: Create GPG key |
| 10 |
ansible.builtin.command: |
| 11 |
cmd: gpg --homedir "{{ remote_tmp_dir }}" --batch --generate-key |
| 12 |
stdin: | |
| 13 |
%echo Generating a basic OpenPGP key |
| 14 |
%no-ask-passphrase |
| 15 |
%no-protection |
| 16 |
Key-Type: RSA |
| 17 |
Key-Length: 4096 |
| 18 |
Name-Real: Foo Bar |
| 19 |
Name-Email: [email protected] |
| 20 |
Expire-Date: 0 |
| 21 |
%commit |
| 22 |
%echo done |
| 23 |
register: result |
| 24 |
|
| 25 |
- name: Extract fingerprint |
| 26 |
ansible.builtin.shell: gpg --homedir "{{ remote_tmp_dir }}" --with-colons --fingerprint [email protected] | grep '^fpr:' |
| 27 |
register: fingerprints |
| 28 |
|
| 29 |
- name: Show fingerprints |
| 30 |
ansible.builtin.debug: |
| 31 |
msg: "{{ fingerprints.stdout_lines | map('split', ':') | list }}" |
| 32 |
|
| 33 |
- name: Export public key |
| 34 |
ansible.builtin.command: gpg --homedir "{{ remote_tmp_dir }}" --export --armor [email protected] |
| 35 |
register: public_key |
| 36 |
|
| 37 |
- name: Export private key |
| 38 |
ansible.builtin.command: gpg --homedir "{{ remote_tmp_dir }}" --export-secret-key --armor [email protected] |
| 39 |
register: private_key |
| 40 |
|
| 41 |
- name: Write public key to disk |
| 42 |
ansible.builtin.copy: |
| 43 |
dest: "{{ remote_tmp_dir }}/public-key" |
| 44 |
content: "{{ public_key.stdout }}" |
| 45 |
|
| 46 |
- name: Write private key to disk |
| 47 |
ansible.builtin.copy: |
| 48 |
dest: "{{ remote_tmp_dir }}/private-key" |
| 49 |
content: "{{ private_key.stdout }}" |
| 50 |
|
| 51 |
- name: Gather fingerprints |
| 52 |
ansible.builtin.set_fact: |
| 53 |
public_key_fingerprint: "{{ lookup('community.crypto.gpg_fingerprint', remote_tmp_dir ~ '/public-key') }}" |
| 54 |
private_key_fingerprint: "{{ lookup('community.crypto.gpg_fingerprint', remote_tmp_dir ~ '/private-key') }}" |
| 55 |
|
| 56 |
- name: Check whether fingerprints match |
| 57 |
ansible.builtin.assert: |
| 58 |
that: |
| 59 |
- public_key_fingerprint == (fingerprints.stdout_lines[0] | split(':'))[9] |
| 60 |
- private_key_fingerprint == (fingerprints.stdout_lines[0] | split(':'))[9] |
| 61 |
|
| 62 |
- name: Error scenario - file does not exist |
| 63 |
ansible.builtin.set_fact: |
| 64 |
failing_result: "{{ lookup('community.crypto.gpg_fingerprint', remote_tmp_dir ~ '/does-not-exist') }}" |
| 65 |
register: result |
| 66 |
ignore_errors: true |
| 67 |
|
| 68 |
- name: Check result |
| 69 |
ansible.builtin.assert: |
| 70 |
that: |
| 71 |
- result is failed |
| 72 |
- >- |
| 73 |
(remote_tmp_dir ~ '/does-not-exist does not exist') in result.msg |
| 74 |
|
| 75 |
- name: Write garbage to disk |
| 76 |
ansible.builtin.copy: |
| 77 |
dest: "{{ remote_tmp_dir }}/garbage" |
| 78 |
content: gargabe |
| 79 |
|
| 80 |
- name: Error scenario - file contains garbage |
| 81 |
ansible.builtin.set_fact: |
| 82 |
failing_result: "{{ lookup('community.crypto.gpg_fingerprint', remote_tmp_dir ~ '/garbage') }}" |
| 83 |
register: result |
| 84 |
ignore_errors: true |
| 85 |
|
| 86 |
- name: Check result |
| 87 |
ansible.builtin.assert: |
| 88 |
that: |
| 89 |
- result is failed |
| 90 |
- >- |
| 91 |
'Running ' in result.msg |
| 92 |
- >- |
| 93 |
('/gpg --no-keyring --with-colons --import-options show-only --import ' ~ remote_tmp_dir ~ '/garbage yielded return code ') in result.msg |