Debian Codegraph @

ansible_collections/community/sops/tests/integration/targets/lookup_sops/tasks/main.yml

raw
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 - when: sops_installed
7 block:
8
9 - name: Test lookup with missing file
10 set_fact:
11 sops_file_does_not_exists: "{{ lookup('community.sops.sops', 'file-does-not-exists.sops.yml') }}"
12 ignore_errors: true
13 register: sops_lookup_missing_file
14
15 - assert:
16 that:
17 - "sops_lookup_missing_file is failed"
18 - "'could not locate file in lookup: file-does-not-exists.sops.yml' in sops_lookup_missing_file.msg"
19
20 - name: Test lookup with missing file with empty_on_not_exist
21 set_fact:
22 sops_file_does_not_exists_empty: "{{ lookup('community.sops.sops', 'file-does-not-exists.sops.yml', empty_on_not_exist=true) }}"
23 register: sops_lookup_missing_file_empty_on_not_exist
24
25 - assert:
26 that:
27 - "sops_lookup_missing_file_empty_on_not_exist is success"
28 - "sops_file_does_not_exists_empty == ''"
29
30 - name: Test lookup of non-sops file
31 set_fact:
32 sops_wrong_file: "{{ lookup('community.sops.sops', 'wrong.yaml') }}"
33 ignore_errors: true
34 register: sops_lookup_wrong_file
35
36 - assert:
37 that:
38 - "sops_lookup_wrong_file is failed"
39 - "'sops metadata not found' in sops_lookup_wrong_file.msg"
40
41 - name: Test simple lookup
42 set_fact:
43 sops_success: "{{ lookup('community.sops.sops', 'simple.sops.yaml') }}"
44 sops_success_2: "{{ lookup('community.sops.sops', 'simple.sops.yaml', sops_binary='sops') }}"
45 register: sops_lookup_simple
46
47 - assert:
48 that:
49 - "sops_lookup_simple is success"
50 - "sops_success == 'foo: bar'"
51 - "sops_success_2 == 'foo: bar'"
52
53 - name: Test extract
54 set_fact:
55 simple_yaml_with_extract: >-
56 {{ lookup('community.sops.sops', 'extract.yaml', extract="['foo']") }}
57 simple_json_with_extract: >-
58 {{ lookup('community.sops.sops', 'extract.json', extract="['bar']") }}
59 nested_yaml_with_extract: >-
60 {{ lookup('community.sops.sops', 'extract.yaml', extract="['baz']['bar'][0]") }}
61 nested_json_with_extract: >-
62 {{ lookup('community.sops.sops', 'extract.json', extract="['baz']['bar'][2]") }}
63 register: sops_lookup_extract
64
65 - assert:
66 that:
67 - "sops_lookup_extract is success"
68 - "simple_yaml_with_extract == 'bar'"
69 - "simple_json_with_extract == 'baz'"
70 - "nested_yaml_with_extract == 'zab'"
71 - "nested_json_with_extract == 'oof'"
72
73 - name: Test rstrip
74 set_fact:
75 with_rstrip: "{{ lookup('community.sops.sops', 'rstrip.sops', rstrip=true) }}"
76 without_rstrip: "{{ lookup('community.sops.sops', 'rstrip.sops', rstrip=false) }}"
77 default_rstrip: "{{ lookup('community.sops.sops', 'rstrip.sops') }}"
78
79 - assert:
80 that:
81 - with_rstrip == 'This file has three newlines at the end.'
82 - without_rstrip == 'This file has three newlines at the end.\n\n\n'
83 - default_rstrip == 'This file has three newlines at the end.'
84
85 - name: Test binary
86 set_fact:
87 binary_with_rstrip: "{{ lookup('community.sops.sops', 'binary.sops', rstrip=true, base64=true) }}"
88 binary_without_rstrip: "{{ lookup('community.sops.sops', 'binary.sops', rstrip=false, base64=true) }}"
89
90 - assert:
91 that:
92 - binary_with_rstrip == 'AQIDAAQ='
93 - binary_without_rstrip == 'AQIDAAQgCg=='
94
95 - name: Test hidden binary
96 set_fact:
97 hidden_binary: "{{ lookup('community.sops.sops', 'hidden-binary', output_type='binary') }}"
98 hidden_binary__json: "{{ lookup('community.sops.sops', 'hidden-binary.json', output_type='binary') }}"
99 hidden_binary__yaml: "{{ lookup('community.sops.sops', 'hidden-binary.yaml', output_type='binary') }}"
100 hidden_binary_yaml: "{{ lookup('community.sops.sops', 'hidden-binary-yaml', input_type='yaml', output_type='binary') }}"
101 hidden_binary_yaml__json: "{{ lookup('community.sops.sops', 'hidden-binary-yaml.json', input_type='yaml', output_type='binary') }}"
102 hidden_binary_yaml__yaml: "{{ lookup('community.sops.sops', 'hidden-binary-yaml.yaml', input_type='yaml', output_type='binary') }}"
103 hidden_json: "{{ lookup('community.sops.sops', 'hidden-json', input_type='json', output_type='json') | from_json }}"
104 hidden_json__json: "{{ lookup('community.sops.sops', 'hidden-json.json', input_type='json', output_type='json') | from_json }}"
105 hidden_json__yaml: "{{ lookup('community.sops.sops', 'hidden-json.yaml', input_type='json', output_type='json') | from_json }}"
106 hidden_yaml: "{{ lookup('community.sops.sops', 'hidden-yaml', input_type='yaml', output_type='yaml') }}"
107 hidden_yaml__json: "{{ lookup('community.sops.sops', 'hidden-yaml.json', input_type='yaml', output_type='yaml') }}"
108 hidden_yaml__yaml: "{{ lookup('community.sops.sops', 'hidden-yaml.yaml', input_type='yaml', output_type='yaml') }}"
109 hidden_json__as_yaml: "{{ lookup('community.sops.sops', 'hidden-json', input_type='json', output_type='yaml') }}"
110 hidden_json__json__as_yaml: "{{ lookup('community.sops.sops', 'hidden-json.json', input_type='json', output_type='yaml') }}"
111 hidden_json__yaml__as_yaml: "{{ lookup('community.sops.sops', 'hidden-json.yaml', input_type='json', output_type='yaml') }}"
112 hidden_yaml__as_json: "{{ lookup('community.sops.sops', 'hidden-yaml', input_type='yaml', output_type='json') | from_json }}"
113 hidden_yaml__json__as_json: "{{ lookup('community.sops.sops', 'hidden-yaml.json', input_type='yaml', output_type='json') | from_json }}"
114 hidden_yaml__yaml__as_json: "{{ lookup('community.sops.sops', 'hidden-yaml.yaml', input_type='yaml', output_type='json') | from_json }}"
115
116 - assert:
117 that:
118 - hidden_binary == test_str_abcd
119 - hidden_binary__json == test_str_abcd
120 - hidden_binary__yaml == test_str_abcd
121 - hidden_binary_yaml == test_str_binary_data
122 - hidden_binary_yaml__json == test_str_binary_data
123 - hidden_binary_yaml__yaml == test_str_binary_data
124 - hidden_json == test_dict
125 - hidden_json__json == test_dict
126 - hidden_json__yaml == test_dict
127 - hidden_yaml == test_dict_yaml
128 - hidden_yaml__json == test_dict_yaml
129 - hidden_yaml__yaml == test_dict_yaml
130 - hidden_json__as_yaml == test_dict_yaml
131 - hidden_json__json__as_yaml == test_dict_yaml
132 - hidden_json__yaml__as_yaml == test_dict_yaml
133 - hidden_yaml__as_json == test_dict
134 - hidden_yaml__json__as_json == test_dict
135 - hidden_yaml__yaml__as_json == test_dict
136 vars:
137 test_dict:
138 a: b
139 c: d
140 test_dict_yaml:
141 "a: b\nc: d"
142 test_str_binary_data: This is binary data.
143 test_str_abcd: a is b, and c is d
144
145 - name: Test fake sops binary (lookup parameters)
146 set_fact:
147 fake_sops_output: "{{ lookup('community.sops.sops', 'simple.sops.yaml', sops_binary=role_path ~ '/files/fake-sops.sh', enable_local_keyservice=False, aws_access_key_id='xxx') }}"
148 fake_sops_output_2: "{{ lookup('community.sops.sops', 'simple.sops.yaml', sops_binary=role_path ~ '/files/fake-sops-val.sh', config_path='/path/to/asdf', aws_secret_access_key='yyy') }}"
149 fake_sops_output_3: "{{ lookup('community.sops.sops', 'simple.sops.yaml', sops_binary=role_path ~ '/files/fake-sops-rep.sh', keyservice=['a', 'b'], aws_session_token='zzz') }}"
150
151 - assert:
152 that:
153 - fake_sops_output == 'fake sops output'
154 - fake_sops_output_2 == 'fake sops output 2'
155 - fake_sops_output_3 == 'fake sops output 3'
156
157 - name: Work around Ansible bug for next test
158 # https://github.com/ansible/ansible/issues/73268
159 set_fact:
160 sops_binary: "{{ role_path }}/files/fake-sops.sh"
161 - name: Test fake sops binary (Ansible variables, 1/3)
162 set_fact:
163 fake_sops_output: "{{ lookup('community.sops.sops', 'simple.sops.yaml') }}"
164 vars:
165 # sops_binary: "{{ role_path }}/files/fake-sops.sh"
166 sops_enable_local_keyservice: false
167 sops_aws_access_key_id: xxx
168
169 - name: Work around Ansible bug for next test
170 # https://github.com/ansible/ansible/issues/73268
171 set_fact:
172 sops_binary: "{{ role_path }}/files/fake-sops-val.sh"
173 - name: Test fake sops binary (Ansible variables, 2/3)
174 set_fact:
175 fake_sops_output_2: "{{ lookup('community.sops.sops', 'simple.sops.yaml') }}"
176 vars:
177 # sops_binary: "{{ role_path }}/files/fake-sops-val.sh"
178 sops_config_path: /path/to/asdf
179 sops_aws_secret_access_key: yyy
180
181 - name: Work around Ansible bug for next test
182 # https://github.com/ansible/ansible/issues/73268
183 set_fact:
184 sops_binary: "{{ role_path }}/files/fake-sops-rep.sh"
185 - name: Test fake sops binary (Ansible variables, 3/3)
186 set_fact:
187 fake_sops_output_3: "{{ lookup('community.sops.sops', 'simple.sops.yaml') }}"
188 vars:
189 # sops_binary: "{{ role_path }}/files/fake-sops-rep.sh"
190 sops_keyservice:
191 - a
192 - b
193 sops_session_token: zzz
194
195 - assert:
196 that:
197 - fake_sops_output == 'fake sops output'
198 - fake_sops_output_2 == 'fake sops output 2'
199 - fake_sops_output_3 == 'fake sops output 3'