| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# (c) 2015, Toshio Kuraotmi <[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 |
from __future__ import annotations |
| 20 |
|
| 21 |
from collections import defaultdict |
| 22 |
|
| 23 |
from unittest import mock |
| 24 |
|
| 25 |
import unittest |
| 26 |
|
| 27 |
from ansible.errors import AnsibleError |
| 28 |
from ansible._internal._datatag._tags import Origin |
| 29 |
from ansible.parsing.vault import EncryptedString |
| 30 |
from ansible.utils.vars import combine_vars, merge_hash, transform_to_native_types |
| 31 |
from units.mock.vault_helper import VaultTestHelper |
| 32 |
|
| 33 |
|
| 34 |
class TestVariableUtils(unittest.TestCase): |
| 35 |
combine_vars_merge_data = ( |
| 36 |
dict( |
| 37 |
a=dict(a=1), |
| 38 |
b=dict(b=2), |
| 39 |
result=dict(a=1, b=2), |
| 40 |
), |
| 41 |
dict( |
| 42 |
a=dict(a=1, c=dict(foo='bar')), |
| 43 |
b=dict(b=2, c=dict(baz='bam')), |
| 44 |
result=dict(a=1, b=2, c=dict(foo='bar', baz='bam')) |
| 45 |
), |
| 46 |
dict( |
| 47 |
a=defaultdict(a=1, c=defaultdict(foo='bar')), |
| 48 |
b=dict(b=2, c=dict(baz='bam')), |
| 49 |
result=defaultdict(a=1, b=2, c=defaultdict(foo='bar', baz='bam')) |
| 50 |
), |
| 51 |
) |
| 52 |
combine_vars_replace_data = ( |
| 53 |
dict( |
| 54 |
a=dict(a=1), |
| 55 |
b=dict(b=2), |
| 56 |
result=dict(a=1, b=2) |
| 57 |
), |
| 58 |
dict( |
| 59 |
a=dict(a=1, c=dict(foo='bar')), |
| 60 |
b=dict(b=2, c=dict(baz='bam')), |
| 61 |
result=dict(a=1, b=2, c=dict(baz='bam')) |
| 62 |
), |
| 63 |
dict( |
| 64 |
a=defaultdict(a=1, c=dict(foo='bar')), |
| 65 |
b=dict(b=2, c=defaultdict(baz='bam')), |
| 66 |
result=defaultdict(a=1, b=2, c=defaultdict(baz='bam')) |
| 67 |
), |
| 68 |
) |
| 69 |
|
| 70 |
def test_combine_vars_improper_args(self): |
| 71 |
with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'): |
| 72 |
with self.assertRaises(AnsibleError): |
| 73 |
combine_vars([1, 2, 3], dict(a=1)) |
| 74 |
with self.assertRaises(AnsibleError): |
| 75 |
combine_vars(dict(a=1), [1, 2, 3]) |
| 76 |
|
| 77 |
with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'merge'): |
| 78 |
with self.assertRaises(AnsibleError): |
| 79 |
combine_vars([1, 2, 3], dict(a=1)) |
| 80 |
with self.assertRaises(AnsibleError): |
| 81 |
combine_vars(dict(a=1), [1, 2, 3]) |
| 82 |
|
| 83 |
def test_combine_vars_replace(self): |
| 84 |
with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'): |
| 85 |
for test in self.combine_vars_replace_data: |
| 86 |
self.assertEqual(combine_vars(test['a'], test['b']), test['result']) |
| 87 |
|
| 88 |
def test_combine_vars_merge(self): |
| 89 |
with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'merge'): |
| 90 |
for test in self.combine_vars_merge_data: |
| 91 |
self.assertEqual(combine_vars(test['a'], test['b']), test['result']) |
| 92 |
|
| 93 |
merge_hash_data = { |
| 94 |
"low_prio": { |
| 95 |
"a": { |
| 96 |
"a'": { |
| 97 |
"x": "low_value", |
| 98 |
"y": "low_value", |
| 99 |
"list": ["low_value"] |
| 100 |
} |
| 101 |
}, |
| 102 |
"b": [1, 1, 2, 3] |
| 103 |
}, |
| 104 |
"high_prio": { |
| 105 |
"a": { |
| 106 |
"a'": { |
| 107 |
"y": "high_value", |
| 108 |
"z": "high_value", |
| 109 |
"list": ["high_value"] |
| 110 |
} |
| 111 |
}, |
| 112 |
"b": [3, 4, 4, {"5": "value"}] |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
def test_merge_hash_simple(self): |
| 117 |
for test in self.combine_vars_merge_data: |
| 118 |
self.assertEqual(merge_hash(test['a'], test['b']), test['result']) |
| 119 |
|
| 120 |
low = self.merge_hash_data['low_prio'] |
| 121 |
high = self.merge_hash_data['high_prio'] |
| 122 |
expected = { |
| 123 |
"a": { |
| 124 |
"a'": { |
| 125 |
"x": "low_value", |
| 126 |
"y": "high_value", |
| 127 |
"z": "high_value", |
| 128 |
"list": ["high_value"] |
| 129 |
} |
| 130 |
}, |
| 131 |
"b": high['b'] |
| 132 |
} |
| 133 |
self.assertEqual(merge_hash(low, high), expected) |
| 134 |
|
| 135 |
def test_merge_hash_non_recursive_and_list_replace(self): |
| 136 |
low = self.merge_hash_data['low_prio'] |
| 137 |
high = self.merge_hash_data['high_prio'] |
| 138 |
expected = high |
| 139 |
self.assertEqual(merge_hash(low, high, False, 'replace'), expected) |
| 140 |
|
| 141 |
def test_merge_hash_non_recursive_and_list_keep(self): |
| 142 |
low = self.merge_hash_data['low_prio'] |
| 143 |
high = self.merge_hash_data['high_prio'] |
| 144 |
expected = { |
| 145 |
"a": high['a'], |
| 146 |
"b": low['b'] |
| 147 |
} |
| 148 |
self.assertEqual(merge_hash(low, high, False, 'keep'), expected) |
| 149 |
|
| 150 |
def test_merge_hash_non_recursive_and_list_append(self): |
| 151 |
low = self.merge_hash_data['low_prio'] |
| 152 |
high = self.merge_hash_data['high_prio'] |
| 153 |
expected = { |
| 154 |
"a": high['a'], |
| 155 |
"b": low['b'] + high['b'] |
| 156 |
} |
| 157 |
self.assertEqual(merge_hash(low, high, False, 'append'), expected) |
| 158 |
|
| 159 |
def test_merge_hash_non_recursive_and_list_prepend(self): |
| 160 |
low = self.merge_hash_data['low_prio'] |
| 161 |
high = self.merge_hash_data['high_prio'] |
| 162 |
expected = { |
| 163 |
"a": high['a'], |
| 164 |
"b": high['b'] + low['b'] |
| 165 |
} |
| 166 |
self.assertEqual(merge_hash(low, high, False, 'prepend'), expected) |
| 167 |
|
| 168 |
def test_merge_hash_non_recursive_and_list_append_rp(self): |
| 169 |
low = self.merge_hash_data['low_prio'] |
| 170 |
high = self.merge_hash_data['high_prio'] |
| 171 |
expected = { |
| 172 |
"a": high['a'], |
| 173 |
"b": [1, 1, 2] + high['b'] |
| 174 |
} |
| 175 |
self.assertEqual(merge_hash(low, high, False, 'append_rp'), expected) |
| 176 |
|
| 177 |
def test_merge_hash_non_recursive_and_list_prepend_rp(self): |
| 178 |
low = self.merge_hash_data['low_prio'] |
| 179 |
high = self.merge_hash_data['high_prio'] |
| 180 |
expected = { |
| 181 |
"a": high['a'], |
| 182 |
"b": high['b'] + [1, 1, 2] |
| 183 |
} |
| 184 |
self.assertEqual(merge_hash(low, high, False, 'prepend_rp'), expected) |
| 185 |
|
| 186 |
def test_merge_hash_recursive_and_list_replace(self): |
| 187 |
low = self.merge_hash_data['low_prio'] |
| 188 |
high = self.merge_hash_data['high_prio'] |
| 189 |
expected = { |
| 190 |
"a": { |
| 191 |
"a'": { |
| 192 |
"x": "low_value", |
| 193 |
"y": "high_value", |
| 194 |
"z": "high_value", |
| 195 |
"list": ["high_value"] |
| 196 |
} |
| 197 |
}, |
| 198 |
"b": high['b'] |
| 199 |
} |
| 200 |
self.assertEqual(merge_hash(low, high, True, 'replace'), expected) |
| 201 |
|
| 202 |
def test_merge_hash_recursive_and_list_keep(self): |
| 203 |
low = self.merge_hash_data['low_prio'] |
| 204 |
high = self.merge_hash_data['high_prio'] |
| 205 |
expected = { |
| 206 |
"a": { |
| 207 |
"a'": { |
| 208 |
"x": "low_value", |
| 209 |
"y": "high_value", |
| 210 |
"z": "high_value", |
| 211 |
"list": ["low_value"] |
| 212 |
} |
| 213 |
}, |
| 214 |
"b": low['b'] |
| 215 |
} |
| 216 |
self.assertEqual(merge_hash(low, high, True, 'keep'), expected) |
| 217 |
|
| 218 |
def test_merge_hash_recursive_and_list_append(self): |
| 219 |
low = self.merge_hash_data['low_prio'] |
| 220 |
high = self.merge_hash_data['high_prio'] |
| 221 |
expected = { |
| 222 |
"a": { |
| 223 |
"a'": { |
| 224 |
"x": "low_value", |
| 225 |
"y": "high_value", |
| 226 |
"z": "high_value", |
| 227 |
"list": ["low_value", "high_value"] |
| 228 |
} |
| 229 |
}, |
| 230 |
"b": low['b'] + high['b'] |
| 231 |
} |
| 232 |
self.assertEqual(merge_hash(low, high, True, 'append'), expected) |
| 233 |
|
| 234 |
def test_merge_hash_recursive_and_list_prepend(self): |
| 235 |
low = self.merge_hash_data['low_prio'] |
| 236 |
high = self.merge_hash_data['high_prio'] |
| 237 |
expected = { |
| 238 |
"a": { |
| 239 |
"a'": { |
| 240 |
"x": "low_value", |
| 241 |
"y": "high_value", |
| 242 |
"z": "high_value", |
| 243 |
"list": ["high_value", "low_value"] |
| 244 |
} |
| 245 |
}, |
| 246 |
"b": high['b'] + low['b'] |
| 247 |
} |
| 248 |
self.assertEqual(merge_hash(low, high, True, 'prepend'), expected) |
| 249 |
|
| 250 |
def test_merge_hash_recursive_and_list_append_rp(self): |
| 251 |
low = self.merge_hash_data['low_prio'] |
| 252 |
high = self.merge_hash_data['high_prio'] |
| 253 |
expected = { |
| 254 |
"a": { |
| 255 |
"a'": { |
| 256 |
"x": "low_value", |
| 257 |
"y": "high_value", |
| 258 |
"z": "high_value", |
| 259 |
"list": ["low_value", "high_value"] |
| 260 |
} |
| 261 |
}, |
| 262 |
"b": [1, 1, 2] + high['b'] |
| 263 |
} |
| 264 |
self.assertEqual(merge_hash(low, high, True, 'append_rp'), expected) |
| 265 |
|
| 266 |
def test_merge_hash_recursive_and_list_prepend_rp(self): |
| 267 |
low = self.merge_hash_data['low_prio'] |
| 268 |
high = self.merge_hash_data['high_prio'] |
| 269 |
expected = { |
| 270 |
"a": { |
| 271 |
"a'": { |
| 272 |
"x": "low_value", |
| 273 |
"y": "high_value", |
| 274 |
"z": "high_value", |
| 275 |
"list": ["high_value", "low_value"] |
| 276 |
} |
| 277 |
}, |
| 278 |
"b": high['b'] + [1, 1, 2] |
| 279 |
} |
| 280 |
self.assertEqual(merge_hash(low, high, True, 'prepend_rp'), expected) |
| 281 |
|
| 282 |
|
| 283 |
def test_transform_to_native_types() -> None: |
| 284 |
"""Verify that transform_to_native_types results in native types for both keys and values, with default redaction.""" |
| 285 |
value = { |
| 286 |
Origin(description="blah").tag("tagged_key"): Origin(description="blah").tag("value with tagged key"), |
| 287 |
# use a bogus EncryptedString instance with no VaultSecretsContext active; ensures that transform with redaction does not attempt decryption |
| 288 |
"redact_this": EncryptedString(ciphertext="bogus") |
| 289 |
} |
| 290 |
|
| 291 |
result = transform_to_native_types(value) |
| 292 |
|
| 293 |
assert result == dict(tagged_key="value with tagged key", redact_this='<redacted>') |
| 294 |
|
| 295 |
assert all(type(key) is str for key in result.keys()) # pylint: disable=unidiomatic-typecheck |
| 296 |
assert all(type(value) is str for value in result.values()) # pylint: disable=unidiomatic-typecheck |
| 297 |
|
| 298 |
|
| 299 |
def test_transform_to_native_types_unredacted(_vault_secrets_context: None) -> None: |
| 300 |
"""Verify that transform with redaction disabled returns a plain string decrypted value.""" |
| 301 |
plaintext = "hello" |
| 302 |
value = dict(enc=VaultTestHelper.make_encrypted_string(plaintext)) |
| 303 |
result = transform_to_native_types(value, redact=False) |
| 304 |
|
| 305 |
assert result == dict(enc=plaintext) |
| 306 |
assert all(type(key) is str for key in result.keys()) # pylint: disable=unidiomatic-typecheck |
| 307 |
assert all(type(value) is str for value in result.values()) # pylint: disable=unidiomatic-typecheck |