| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2022 Brian Scholer (@briantist) |
| 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 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import pytest |
| 10 |
import re |
| 11 |
import json |
| 12 |
|
| 13 |
from .....plugins.modules import vault_write |
| 14 |
from .....plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 15 |
|
| 16 |
|
| 17 |
hvac = pytest.importorskip('hvac') |
| 18 |
|
| 19 |
|
| 20 |
pytestmark = pytest.mark.usefixtures( |
| 21 |
'patch_ansible_module', |
| 22 |
'patch_authenticator', |
| 23 |
'patch_get_vault_client', |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def _connection_options(): |
| 28 |
return { |
| 29 |
'auth_method': 'token', |
| 30 |
'url': 'http://myvault', |
| 31 |
'token': 'beep-boop', |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
def _sample_options(): |
| 36 |
return { |
| 37 |
'path': 'endpoint', |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def _combined_options(**kwargs): |
| 42 |
opt = _connection_options() |
| 43 |
opt.update(_sample_options()) |
| 44 |
opt.update(kwargs) |
| 45 |
return opt |
| 46 |
|
| 47 |
|
| 48 |
@pytest.fixture |
| 49 |
def approle_secret_id_write_response(fixture_loader): |
| 50 |
return fixture_loader('approle_secret_id_write_response.json') |
| 51 |
|
| 52 |
|
| 53 |
class TestModuleVaultWrite(): |
| 54 |
|
| 55 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 56 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 57 |
def test_vault_write_authentication_error(self, authenticator, exc, capfd): |
| 58 |
authenticator.authenticate.side_effect = exc |
| 59 |
|
| 60 |
with pytest.raises(SystemExit) as e: |
| 61 |
vault_write.main() |
| 62 |
|
| 63 |
out, err = capfd.readouterr() |
| 64 |
result = json.loads(out) |
| 65 |
|
| 66 |
assert e.value.code != 0, "result: %r" % (result,) |
| 67 |
assert result['msg'] == 'throwaway msg', "result: %r" % result |
| 68 |
|
| 69 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 70 |
@pytest.mark.parametrize('exc', [HashiVaultValueError('throwaway msg'), NotImplementedError('throwaway msg')]) |
| 71 |
def test_vault_write_auth_validation_error(self, authenticator, exc, capfd): |
| 72 |
authenticator.validate.side_effect = exc |
| 73 |
|
| 74 |
with pytest.raises(SystemExit) as e: |
| 75 |
vault_write.main() |
| 76 |
|
| 77 |
out, err = capfd.readouterr() |
| 78 |
result = json.loads(out) |
| 79 |
|
| 80 |
assert e.value.code != 0, "result: %r" % (result,) |
| 81 |
assert result['msg'] == 'throwaway msg' |
| 82 |
|
| 83 |
@pytest.mark.parametrize('opt_data', [{}, {'thing': 'one', 'thang': 'two'}]) |
| 84 |
@pytest.mark.parametrize('opt_wrap_ttl', [None, '5m']) |
| 85 |
@pytest.mark.parametrize('patch_ansible_module', [[_combined_options(), 'data', 'wrap_ttl']], indirect=True) |
| 86 |
def test_vault_write_return_data(self, patch_ansible_module, approle_secret_id_write_response, vault_client, opt_wrap_ttl, opt_data, capfd): |
| 87 |
client = vault_client |
| 88 |
client.write_data.return_value = approle_secret_id_write_response |
| 89 |
|
| 90 |
with pytest.raises(SystemExit) as e: |
| 91 |
vault_write.main() |
| 92 |
|
| 93 |
out, err = capfd.readouterr() |
| 94 |
result = json.loads(out) |
| 95 |
|
| 96 |
assert e.value.code == 0, "result: %r" % (result,) |
| 97 |
|
| 98 |
client.write_data.assert_called_once_with(path=patch_ansible_module['path'], wrap_ttl=opt_wrap_ttl, data=opt_data) |
| 99 |
|
| 100 |
assert result['data'] == approle_secret_id_write_response, ( |
| 101 |
"module result did not match expected result:\nmodule: %r\nexpected: %r" % (result['data'], approle_secret_id_write_response) |
| 102 |
) |
| 103 |
|
| 104 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 105 |
def test_vault_write_empty_response(self, vault_client, requests_unparseable_response, capfd): |
| 106 |
client = vault_client |
| 107 |
|
| 108 |
requests_unparseable_response.status_code = 204 |
| 109 |
|
| 110 |
client.write_data.return_value = requests_unparseable_response |
| 111 |
|
| 112 |
with pytest.raises(SystemExit) as e: |
| 113 |
vault_write.main() |
| 114 |
|
| 115 |
out, err = capfd.readouterr() |
| 116 |
result = json.loads(out) |
| 117 |
|
| 118 |
assert e.value.code == 0, "result: %r" % (result,) |
| 119 |
|
| 120 |
assert result['data'] == {} |
| 121 |
|
| 122 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 123 |
def test_vault_write_unparseable_response(self, vault_client, requests_unparseable_response, module_warn, capfd): |
| 124 |
client = vault_client |
| 125 |
|
| 126 |
requests_unparseable_response.status_code = 200 |
| 127 |
requests_unparseable_response.content = '﷽' |
| 128 |
|
| 129 |
client.write_data.return_value = requests_unparseable_response |
| 130 |
|
| 131 |
with pytest.raises(SystemExit) as e: |
| 132 |
vault_write.main() |
| 133 |
|
| 134 |
out, err = capfd.readouterr() |
| 135 |
result = json.loads(out) |
| 136 |
|
| 137 |
assert e.value.code == 0, "result: %r" % (result,) |
| 138 |
assert result['data'] == '﷽' |
| 139 |
|
| 140 |
module_warn.assert_called_once_with('Vault returned status code 200 and an unparsable body.') |
| 141 |
|
| 142 |
@pytest.mark.parametrize( |
| 143 |
'exc', |
| 144 |
[ |
| 145 |
(hvac.exceptions.Forbidden, r'^Forbidden: Permission Denied to path'), |
| 146 |
(hvac.exceptions.InvalidPath, r"^The path '[^']+' doesn't seem to exist"), |
| 147 |
(hvac.exceptions.InternalServerError, r'^Internal Server Error:'), |
| 148 |
] |
| 149 |
) |
| 150 |
@pytest.mark.parametrize('patch_ansible_module', [_combined_options()], indirect=True) |
| 151 |
def test_vault_write_vault_exception(self, vault_client, exc, capfd): |
| 152 |
|
| 153 |
client = vault_client |
| 154 |
client.write_data.side_effect = exc[0] |
| 155 |
|
| 156 |
with pytest.raises(SystemExit) as e: |
| 157 |
vault_write.main() |
| 158 |
|
| 159 |
out, err = capfd.readouterr() |
| 160 |
result = json.loads(out) |
| 161 |
|
| 162 |
assert e.value.code != 0, "result: %r" % (result,) |
| 163 |
assert re.search(exc[1], result['msg']) is not None |
| 164 |
|
| 165 |
@pytest.mark.parametrize( |
| 166 |
'opt_data', |
| 167 |
[ |
| 168 |
{"path": 'path_value'}, |
| 169 |
{"wrap_ttl": 'wrap_ttl_value'}, |
| 170 |
{"path": 'data_value', "wrap_ttl": 'write_ttl_value'}, |
| 171 |
], |
| 172 |
) |
| 173 |
@pytest.mark.parametrize('patch_ansible_module', [[_combined_options(), 'data']], indirect=True) |
| 174 |
def test_vault_write_data_fallback_bad_params(self, vault_client, opt_data, capfd): |
| 175 |
client = vault_client |
| 176 |
client.mock_add_spec(['write']) |
| 177 |
|
| 178 |
with pytest.raises(SystemExit) as e: |
| 179 |
vault_write.main() |
| 180 |
|
| 181 |
out, err = capfd.readouterr() |
| 182 |
result = json.loads(out) |
| 183 |
|
| 184 |
assert e.value.code != 0, "result: %r" % (result,) |
| 185 |
assert re.search(r"To use 'path' or 'wrap_ttl' as data keys, use hvac >= 1\.2", result['msg']) is not None |
| 186 |
|
| 187 |
client.write.assert_not_called() |
| 188 |
|
| 189 |
@pytest.mark.parametrize( |
| 190 |
'opt_data', |
| 191 |
[ |
| 192 |
{"item1": 'item1_value'}, |
| 193 |
{"item2": 'item2_value'}, |
| 194 |
{"item1": 'item1_value', "item2": 'item2_value'}, |
| 195 |
], |
| 196 |
) |
| 197 |
@pytest.mark.parametrize('patch_ansible_module', [[_combined_options(), 'data']], indirect=True) |
| 198 |
def test_vault_write_data_fallback_write(self, vault_client, opt_data, patch_ansible_module, approle_secret_id_write_response, capfd): |
| 199 |
client = vault_client |
| 200 |
client.mock_add_spec(['write']) |
| 201 |
client.write.return_value = approle_secret_id_write_response |
| 202 |
|
| 203 |
with pytest.raises(SystemExit) as e: |
| 204 |
vault_write.main() |
| 205 |
|
| 206 |
out, err = capfd.readouterr() |
| 207 |
result = json.loads(out) |
| 208 |
|
| 209 |
assert e.value.code == 0, "result: %r" % (result,) |
| 210 |
|
| 211 |
client.write.assert_called_once_with(path=patch_ansible_module['path'], wrap_ttl=None, **opt_data) |