| 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 |
|
| 6 |
- name: Create encrypted files |
| 7 |
sops_encrypt: |
| 8 |
path: '{{ remote_tmp_dir }}/{{ item.name }}.sops.yaml' |
| 9 |
age: '{{ item.identities }}' |
| 10 |
age_keyfile: '{{ remote_tmp_dir }}/identities_all' |
| 11 |
content_yaml: '{{ item.data }}' |
| 12 |
loop: '{{ data }}' |
| 13 |
vars: |
| 14 |
data: |
| 15 |
- name: enc-1 |
| 16 |
identities: |
| 17 |
- '{{ identity_1 }}' |
| 18 |
- '{{ identity_2 }}' |
| 19 |
- '{{ identity_3 }}' |
| 20 |
- '{{ identity_4 }}' |
| 21 |
data: |
| 22 |
foo: bar |
| 23 |
baz: this is a secret |
| 24 |
bam: true |
| 25 |
int: 3 |
| 26 |
- name: enc-2 |
| 27 |
identities: |
| 28 |
- '{{ identity_1 }}' |
| 29 |
- '{{ identity_2 }}' |
| 30 |
- '{{ identity_4 }}' |
| 31 |
data: |
| 32 |
foo: 19 |
| 33 |
bar: this is another secret |
| 34 |
- name: enc-3 |
| 35 |
identities: |
| 36 |
- '{{ identity_4 }}' |
| 37 |
data: |
| 38 |
foo: 23 |
| 39 |
|
| 40 |
- name: Copy encrypted files to localhost |
| 41 |
fetch: |
| 42 |
src: '{{ remote_tmp_dir }}/{{ item }}.sops.yaml' |
| 43 |
dest: '{{ local_tmp_dir }}/' |
| 44 |
flat: true |
| 45 |
loop: |
| 46 |
- enc-1 |
| 47 |
- enc-2 |
| 48 |
- enc-3 |
| 49 |
|
| 50 |
- name: Decrypt some data (3.7.0+) |
| 51 |
set_fact: |
| 52 |
decrypt_1: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identities_all') | from_yaml }}" |
| 53 |
decrypt_1_1: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_1') | from_yaml }}" |
| 54 |
decrypt_1_2: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_2') | from_yaml }}" |
| 55 |
decrypt_1_3: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_3') | from_yaml }}" |
| 56 |
decrypt_1_4: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_4') | from_yaml }}" |
| 57 |
decrypt_1_1_2_3: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_keyfile=local_tmp_dir ~ '/identities_1_2_3') | from_yaml }}" |
| 58 |
decrypt_2: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identities_all') | from_yaml }}" |
| 59 |
decrypt_2_1: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_1') | from_yaml }}" |
| 60 |
decrypt_2_2: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_2') | from_yaml }}" |
| 61 |
decrypt_2_4: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_4') | from_yaml }}" |
| 62 |
decrypt_2_1_2_3: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identities_1_2_3') | from_yaml }}" |
| 63 |
decrypt_3: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-3.sops.yaml', age_keyfile=local_tmp_dir ~ '/identities_all') | from_yaml }}" |
| 64 |
decrypt_3_4: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-3.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_4') | from_yaml }}" |
| 65 |
|
| 66 |
- name: Validate decryption |
| 67 |
assert: |
| 68 |
that: |
| 69 |
- decrypt_1.foo == 'bar' |
| 70 |
- decrypt_1.baz == 'this is a secret' |
| 71 |
- decrypt_1.bam == true |
| 72 |
- decrypt_1.int == 3 |
| 73 |
- decrypt_1 == decrypt_1_1 |
| 74 |
- decrypt_1 == decrypt_1_2 |
| 75 |
- decrypt_1 == decrypt_1_3 |
| 76 |
- decrypt_1 == decrypt_1_4 |
| 77 |
- decrypt_1 == decrypt_1_1_2_3 |
| 78 |
- decrypt_2.foo == 19 |
| 79 |
- decrypt_2.bar == 'this is another secret' |
| 80 |
- decrypt_2.bam is undefined |
| 81 |
- decrypt_2.int is undefined |
| 82 |
- decrypt_2 == decrypt_2_1 |
| 83 |
- decrypt_2 == decrypt_2_2 |
| 84 |
- decrypt_2 == decrypt_2_4 |
| 85 |
- decrypt_2 == decrypt_2_1_2_3 |
| 86 |
- decrypt_3.foo == 23 |
| 87 |
- decrypt_3.bar is undefined |
| 88 |
- decrypt_3.bam is undefined |
| 89 |
- decrypt_3.int is undefined |
| 90 |
- decrypt_3 == decrypt_3_4 |
| 91 |
|
| 92 |
- when: >- |
| 93 |
sops_version_controller is version('3.7.1', '>=') |
| 94 |
block: |
| 95 |
- name: Decrypt some data (3.7.1+) |
| 96 |
set_fact: |
| 97 |
decrypt_1b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identities_all')) | from_yaml }}" |
| 98 |
decrypt_1_1b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identity_1')) | from_yaml }}" |
| 99 |
decrypt_1_2b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identity_2')) | from_yaml }}" |
| 100 |
decrypt_1_3b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identity_3')) | from_yaml }}" |
| 101 |
decrypt_1_4b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identity_4')) | from_yaml }}" |
| 102 |
decrypt_1_1_2_3b: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-1.sops.yaml', age_key=lookup('file', local_tmp_dir ~ '/identities_1_2_3')) | from_yaml }}" |
| 103 |
|
| 104 |
- name: Validate decryption |
| 105 |
assert: |
| 106 |
that: |
| 107 |
- decrypt_1 == decrypt_1b |
| 108 |
- decrypt_1 == decrypt_1_1b |
| 109 |
- decrypt_1 == decrypt_1_2b |
| 110 |
- decrypt_1 == decrypt_1_3b |
| 111 |
- decrypt_1 == decrypt_1_4b |
| 112 |
- decrypt_1 == decrypt_1_1_2_3b |
| 113 |
|
| 114 |
- name: Failed encryption 1 |
| 115 |
debug: |
| 116 |
msg: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-2.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_3') | from_yaml }}" |
| 117 |
ignore_errors: true |
| 118 |
register: failure_1 |
| 119 |
|
| 120 |
- name: Failed encryption 2 |
| 121 |
debug: |
| 122 |
msg: "{{ lookup('community.sops.sops', local_tmp_dir ~ '/enc-3.sops.yaml', age_keyfile=local_tmp_dir ~ '/identity_1') | from_yaml }}" |
| 123 |
ignore_errors: true |
| 124 |
register: failure_2 |
| 125 |
|
| 126 |
- name: Validate failed decryption |
| 127 |
assert: |
| 128 |
that: |
| 129 |
- failure_1 is failed |
| 130 |
- "'CouldNotRetrieveKey' in failure_1.msg" |
| 131 |
- failure_2 is failed |
| 132 |
- "'CouldNotRetrieveKey' in failure_2.msg" |