| 1 |
# |
| 2 |
# (c) 2022 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
|
| 8 |
import random |
| 9 |
from unittest.mock import ANY |
| 10 |
from unittest.mock import MagicMock |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
try: |
| 15 |
import botocore |
| 16 |
except ImportError: |
| 17 |
# Handled by HAS_BOTO3 |
| 18 |
pass |
| 19 |
|
| 20 |
from ansible_collections.amazon.aws.plugins.module_utils.acm import ACMServiceManager |
| 21 |
from ansible_collections.amazon.aws.plugins.module_utils.acm import acm_catch_boto_exception |
| 22 |
|
| 23 |
MODULE_NAME = "ansible_collections.amazon.aws.plugins.module_utils.acm" |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(name="acm_service_mgr") |
| 27 |
def fixture_acm_service_mgr(): |
| 28 |
module = MagicMock() |
| 29 |
module.fail_json_aws.side_effect = SystemExit(2) |
| 30 |
module.fail_json.side_effect = SystemExit(1) |
| 31 |
module.client.return_value = MagicMock() |
| 32 |
|
| 33 |
acm_service_mgr_obj = ACMServiceManager(module) |
| 34 |
|
| 35 |
return acm_service_mgr_obj |
| 36 |
|
| 37 |
|
| 38 |
def raise_botocore_error(code="AccessDenied"): |
| 39 |
return botocore.exceptions.ClientError({"Error": {"Code": code}}, "Certificate") |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize("has_module_arg", [True, False]) |
| 43 |
def test_acm_catch_boto_exception_failure(has_module_arg): |
| 44 |
module = MagicMock() |
| 45 |
module.fail_json_aws.side_effect = SystemExit(2) |
| 46 |
|
| 47 |
boto_err = raise_botocore_error() |
| 48 |
|
| 49 |
@acm_catch_boto_exception |
| 50 |
def generate_boto_exception(): |
| 51 |
raise boto_err |
| 52 |
|
| 53 |
if has_module_arg: |
| 54 |
with pytest.raises(SystemExit): |
| 55 |
generate_boto_exception(module=module, error="test") |
| 56 |
module.fail_json_aws.assert_called_with(boto_err, msg="test") |
| 57 |
else: |
| 58 |
with pytest.raises(botocore.exceptions.ClientError): |
| 59 |
generate_boto_exception(error="test") |
| 60 |
module.fail_json_aws.assert_not_called() |
| 61 |
|
| 62 |
|
| 63 |
def test_acm_catch_boto_exception_with_ignore_code(): |
| 64 |
codes = ["this_exception_code_is_ignored", "this_another_exception_code_is_ignored"] |
| 65 |
|
| 66 |
@acm_catch_boto_exception |
| 67 |
def raise_exception_with_ignore_error_code(**kwargs): |
| 68 |
raise raise_botocore_error(code=random.choice(codes)) |
| 69 |
|
| 70 |
assert raise_exception_with_ignore_error_code(ignore_error_codes=codes) is None |
| 71 |
|
| 72 |
|
| 73 |
def test_acm_catch_boto_exception(): |
| 74 |
data = {i: MagicMock() for i in range(10)} |
| 75 |
|
| 76 |
@acm_catch_boto_exception |
| 77 |
def get_data(*args, **kwargs): |
| 78 |
if len(args) > 0: |
| 79 |
return data.get(args[0]) |
| 80 |
return data.get(kwargs.get("id")) |
| 81 |
|
| 82 |
for i in range(10): |
| 83 |
assert data.get(i) == get_data(i) |
| 84 |
assert data.get(i) == get_data(id=i) |
| 85 |
|
| 86 |
|
| 87 |
def test_acm_service_manager_init(): |
| 88 |
module = MagicMock() |
| 89 |
module.client.return_value = {"client": "unit_tests"} |
| 90 |
|
| 91 |
ACMServiceManager(module) |
| 92 |
module.client.assert_called_once_with("acm") |
| 93 |
|
| 94 |
|
| 95 |
def test_acm_service_manager_get_domain_of_cert(acm_service_mgr): |
| 96 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 97 |
|
| 98 |
certificate = {"Certificate": {"DomainName": MagicMock()}, "ResponseMetaData": {"code": 200}} |
| 99 |
acm_service_mgr.client.describe_certificate.return_value = certificate |
| 100 |
assert acm_service_mgr.get_domain_of_cert(arn=arn) == certificate["Certificate"]["DomainName"] |
| 101 |
|
| 102 |
|
| 103 |
def test_acm_service_manager_get_domain_of_cert_missing_arn(acm_service_mgr): |
| 104 |
with pytest.raises(SystemExit): |
| 105 |
acm_service_mgr.get_domain_of_cert(arn=None) |
| 106 |
error = "Internal error with ACM domain fetching, no certificate ARN specified" |
| 107 |
acm_service_mgr.module.fail_json.assert_called_with(msg=error) |
| 108 |
acm_service_mgr.module.fail_json_aws.assert_not_called() |
| 109 |
|
| 110 |
|
| 111 |
def test_acm_service_manager_get_domain_of_cert_failure(acm_service_mgr): |
| 112 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 113 |
boto_err = raise_botocore_error() |
| 114 |
|
| 115 |
acm_service_mgr.client.describe_certificate.side_effect = boto_err |
| 116 |
with pytest.raises(SystemExit): |
| 117 |
acm_service_mgr.get_domain_of_cert(arn=arn) |
| 118 |
|
| 119 |
error = f"Couldn't obtain certificate data for arn {arn}" |
| 120 |
acm_service_mgr.module.fail_json_aws.assert_called_with(boto_err, msg=error) |
| 121 |
acm_service_mgr.module.fail.assert_not_called() |
| 122 |
|
| 123 |
|
| 124 |
def test_acm_service_manager_get_domain_of_cert_with_retry_and_success(maybe_sleep, acm_service_mgr): |
| 125 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 126 |
boto_err = raise_botocore_error(code="ResourceNotFoundException") |
| 127 |
certificate = {"Certificate": {"DomainName": MagicMock()}, "ResponseMetaData": {"code": 200}} |
| 128 |
acm_service_mgr.client.describe_certificate.side_effect = [boto_err, certificate] |
| 129 |
assert acm_service_mgr.get_domain_of_cert(arn=arn) == certificate["Certificate"]["DomainName"] |
| 130 |
|
| 131 |
|
| 132 |
def test_acm_service_manager_get_domain_of_cert_with_retry_and_failure(maybe_sleep, acm_service_mgr): |
| 133 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 134 |
boto_err = raise_botocore_error(code="ResourceNotFoundException") |
| 135 |
|
| 136 |
acm_service_mgr.client.describe_certificate.side_effect = [boto_err for i in range(10)] |
| 137 |
with pytest.raises(SystemExit): |
| 138 |
acm_service_mgr.get_domain_of_cert(arn=arn) |
| 139 |
|
| 140 |
|
| 141 |
def test_acm_service_manager_import_certificate_failure_at_import(acm_service_mgr): |
| 142 |
acm_service_mgr.client.import_certificate.side_effect = raise_botocore_error() |
| 143 |
with pytest.raises(SystemExit): |
| 144 |
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock()) |
| 145 |
|
| 146 |
|
| 147 |
def test_acm_service_manager_import_certificate_failure_at_tagging(acm_service_mgr): |
| 148 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 149 |
acm_service_mgr.client.import_certificate.return_value = {"CertificateArn": arn} |
| 150 |
|
| 151 |
boto_err = raise_botocore_error() |
| 152 |
acm_service_mgr.client.add_tags_to_certificate.side_effect = boto_err |
| 153 |
|
| 154 |
with pytest.raises(SystemExit): |
| 155 |
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock()) |
| 156 |
acm_service_mgr.module.fail_json_aws.assert_called_with(boto_err, msg=f"Couldn't tag certificate {arn}") |
| 157 |
|
| 158 |
|
| 159 |
def test_acm_service_manager_import_certificate_failure_at_deletion(acm_service_mgr): |
| 160 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 161 |
acm_service_mgr.client.import_certificate.return_value = {"CertificateArn": arn} |
| 162 |
|
| 163 |
acm_service_mgr.client.add_tags_to_certificate.side_effect = raise_botocore_error() |
| 164 |
delete_err = raise_botocore_error(code="DeletionError") |
| 165 |
acm_service_mgr.client.delete_certificate.side_effect = delete_err |
| 166 |
|
| 167 |
with pytest.raises(SystemExit): |
| 168 |
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock()) |
| 169 |
acm_service_mgr.module.warn.assert_called_with( |
| 170 |
f"Certificate {arn} exists, and is not tagged. So Ansible will not see it on the next run." |
| 171 |
) |
| 172 |
|
| 173 |
|
| 174 |
def test_acm_service_manager_import_certificate_failure_with_arn_change(acm_service_mgr): |
| 175 |
original_arn = "original_arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 176 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 177 |
|
| 178 |
acm_service_mgr.import_certificate_with_backoff = MagicMock() |
| 179 |
acm_service_mgr.import_certificate_with_backoff.return_value = arn |
| 180 |
|
| 181 |
with pytest.raises(SystemExit): |
| 182 |
acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock(), arn=original_arn) |
| 183 |
acm_service_mgr.module.fail_json.assert_called_with( |
| 184 |
msg=f"ARN changed with ACM update, from {original_arn} to {arn}" |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
def test_acm_service_manager_import_certificate(acm_service_mgr): |
| 189 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 190 |
|
| 191 |
acm_service_mgr.import_certificate_with_backoff = MagicMock() |
| 192 |
acm_service_mgr.import_certificate_with_backoff.return_value = arn |
| 193 |
|
| 194 |
acm_service_mgr.tag_certificate_with_backoff = MagicMock() |
| 195 |
|
| 196 |
assert arn == acm_service_mgr.import_certificate(certificate=MagicMock(), private_key=MagicMock(), arn=arn) |
| 197 |
|
| 198 |
|
| 199 |
def test_acm_service_manager_delete_certificate_keyword_arn(acm_service_mgr): |
| 200 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 201 |
acm_service_mgr.delete_certificate_with_backoff = MagicMock() |
| 202 |
acm_service_mgr.delete_certificate(arn=arn) |
| 203 |
err = f"Couldn't delete certificate {arn}" |
| 204 |
acm_service_mgr.delete_certificate_with_backoff.assert_called_with(arn, module=acm_service_mgr.module, error=err) |
| 205 |
|
| 206 |
|
| 207 |
def test_acm_service_manager_delete_certificate_positional_arn(acm_service_mgr): |
| 208 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 209 |
acm_service_mgr.delete_certificate_with_backoff = MagicMock() |
| 210 |
module = MagicMock() |
| 211 |
client = MagicMock() |
| 212 |
acm_service_mgr.delete_certificate(module, client, arn) |
| 213 |
err = f"Couldn't delete certificate {arn}" |
| 214 |
acm_service_mgr.delete_certificate_with_backoff.assert_called_with(arn, module=acm_service_mgr.module, error=err) |
| 215 |
|
| 216 |
|
| 217 |
def test_acm_service_manager_delete_certificate_missing_arn(acm_service_mgr): |
| 218 |
with pytest.raises(SystemExit): |
| 219 |
acm_service_mgr.delete_certificate() |
| 220 |
acm_service_mgr.module.fail_json.assert_called_with(msg="Missing required certificate arn to delete.") |
| 221 |
|
| 222 |
|
| 223 |
def test_acm_service_manager_delete_certificate_failure(acm_service_mgr): |
| 224 |
arn = "arn:aws:acm:us-west-01:123456789012:certificate/12345678-1234-1234-1234-123456789012" |
| 225 |
acm_service_mgr.client.delete_certificate.side_effect = raise_botocore_error() |
| 226 |
with pytest.raises(SystemExit): |
| 227 |
acm_service_mgr.delete_certificate(arn=arn) |
| 228 |
|
| 229 |
|
| 230 |
@pytest.mark.parametrize( |
| 231 |
"ref,cert,result", |
| 232 |
[ |
| 233 |
(None, ANY, True), |
| 234 |
({"phase": "test"}, {"Phase": "test"}, False), |
| 235 |
({"phase": "test"}, {"phase": "test"}, True), |
| 236 |
({"phase": "test"}, {"phase": "test", "collection": "amazon.aws"}, True), |
| 237 |
({"phase": "test", "collection": "amazon"}, {"phase": "test", "collection": "amazon.aws"}, False), |
| 238 |
({"phase": "test", "collection": "amazon"}, {"phase": "test"}, False), |
| 239 |
], |
| 240 |
) |
| 241 |
def test_acm_service_manager_match_tags(acm_service_mgr, ref, cert, result): |
| 242 |
assert acm_service_mgr._match_tags(ref, cert) == result |
| 243 |
|
| 244 |
|
| 245 |
def test_acm_service_manager_match_tags_failure(acm_service_mgr): |
| 246 |
with pytest.raises(SystemExit): |
| 247 |
acm_service_mgr._match_tags({"Tag": "tag1"}, 10) |
| 248 |
acm_service_mgr.module.fail_json_aws.assert_called_once() |
| 249 |
|
| 250 |
|
| 251 |
def test_acm_service_manager_get_certificates_no_certificates(acm_service_mgr): |
| 252 |
acm_service_mgr.list_certificates_with_backoff = MagicMock() |
| 253 |
acm_service_mgr.list_certificates_with_backoff.return_value = [] |
| 254 |
|
| 255 |
assert acm_service_mgr.get_certificates(domain_name=MagicMock(), statuses=MagicMock(), arn=ANY, only_tags=ANY) == [] |
| 256 |
|
| 257 |
|
| 258 |
@pytest.mark.parametrize( |
| 259 |
"domain_name,arn,tags,expected", |
| 260 |
[ |
| 261 |
(None, None, None, [0, 1, 3]), |
| 262 |
("ansible.com", None, None, [0]), |
| 263 |
("ansible.com", "arn:aws:1", None, [0]), |
| 264 |
(None, "arn:aws:1", None, [0]), |
| 265 |
(None, "arn:aws:4", None, [3]), |
| 266 |
("ansible.com", "arn:aws:3", None, []), |
| 267 |
("ansible.org", None, None, [1, 3]), |
| 268 |
("ansible.org", "arn:aws:2", None, [1]), |
| 269 |
("ansible.org", "arn:aws:4", None, [3]), |
| 270 |
(None, None, {"CertificateArn": "arn:aws:2"}, [1]), |
| 271 |
(None, None, {"CertificateType": "x509"}, [0, 1]), |
| 272 |
(None, None, {"CertificateType": "x509", "CertificateArn": "arn:aws:2"}, [1]), |
| 273 |
], |
| 274 |
) |
| 275 |
def test_acm_service_manager_get_certificates(acm_service_mgr, domain_name, arn, tags, expected): |
| 276 |
all_certificates = [ |
| 277 |
{"CertificateArn": "arn:aws:1", "DomainName": "ansible.com"}, |
| 278 |
{"CertificateArn": "arn:aws:2", "DomainName": "ansible.org"}, |
| 279 |
{"CertificateArn": "arn:aws:3", "DomainName": "ansible.com"}, |
| 280 |
{"CertificateArn": "arn:aws:4", "DomainName": "ansible.org"}, |
| 281 |
] |
| 282 |
|
| 283 |
acm_service_mgr.list_certificates_with_backoff = MagicMock() |
| 284 |
acm_service_mgr.list_certificates_with_backoff.return_value = all_certificates |
| 285 |
|
| 286 |
describe_certificates = { |
| 287 |
"arn:aws:1": {"Status": "VALIDATED", "CertificateArn": "arn:aws:1", "AnotherKey": "some_key_value"}, |
| 288 |
"arn:aws:2": {"Status": "VALIDATION_TIMED_OUT", "CertificateArn": "arn:aws:2"}, |
| 289 |
"arn:aws:3": {"Status": "FAILED", "CertificateArn": "arn:aws:3", "CertificateValidity": "11222022"}, |
| 290 |
"arn:aws:4": {"Status": "PENDING_VALIDATION", "CertificateArn": "arn:aws:4"}, |
| 291 |
} |
| 292 |
|
| 293 |
get_certificates = { |
| 294 |
"arn:aws:1": {"Provider": "Dummy", "Private": True}, |
| 295 |
"arn:aws:2": None, |
| 296 |
"arn:aws:3": {}, |
| 297 |
"arn:aws:4": {}, |
| 298 |
} |
| 299 |
|
| 300 |
certificate_tags = { |
| 301 |
"arn:aws:1": [ |
| 302 |
{"Key": "Validated", "Value": True}, |
| 303 |
{"Key": "CertificateType", "Value": "x509"}, |
| 304 |
{"Key": "CertificateArn", "Value": "arn:aws:1"}, |
| 305 |
], |
| 306 |
"arn:aws:2": [{"Key": "CertificateType", "Value": "x509"}, {"Key": "CertificateArn", "Value": "arn:aws:2"}], |
| 307 |
"arn:aws:3": None, |
| 308 |
"arn:aws:4": {}, |
| 309 |
} |
| 310 |
|
| 311 |
all_results = [ |
| 312 |
{ |
| 313 |
"status": "VALIDATED", |
| 314 |
"certificate_arn": "arn:aws:1", |
| 315 |
"another_key": "some_key_value", |
| 316 |
"provider": "Dummy", |
| 317 |
"private": True, |
| 318 |
"tags": {"Validated": True, "CertificateType": "x509", "CertificateArn": "arn:aws:1"}, |
| 319 |
}, |
| 320 |
{ |
| 321 |
"status": "VALIDATION_TIMED_OUT", |
| 322 |
"certificate_arn": "arn:aws:2", |
| 323 |
"tags": {"CertificateType": "x509", "CertificateArn": "arn:aws:2"}, |
| 324 |
}, |
| 325 |
{"status": "FAILED", "certificate_arn": "arn:aws:3", "certificate_validity": "11222022"}, |
| 326 |
{"status": "PENDING_VALIDATION", "certificate_arn": "arn:aws:4", "tags": {}}, |
| 327 |
] |
| 328 |
|
| 329 |
results = [all_results[i] for i in range(len(all_results)) if i in expected] |
| 330 |
|
| 331 |
acm_service_mgr.describe_certificate_with_backoff = MagicMock() |
| 332 |
acm_service_mgr.describe_certificate_with_backoff.side_effect = lambda *args, **kwargs: describe_certificates.get( |
| 333 |
args[0] |
| 334 |
) |
| 335 |
|
| 336 |
acm_service_mgr.get_certificate_with_backoff = MagicMock() |
| 337 |
acm_service_mgr.get_certificate_with_backoff.side_effect = lambda *args, **kwargs: get_certificates.get(args[0]) |
| 338 |
|
| 339 |
acm_service_mgr.list_certificate_tags_with_backoff = MagicMock() |
| 340 |
acm_service_mgr.list_certificate_tags_with_backoff.side_effect = lambda *args, **kwargs: certificate_tags.get( |
| 341 |
args[0], [] |
| 342 |
) |
| 343 |
|
| 344 |
assert ( |
| 345 |
acm_service_mgr.get_certificates(domain_name=domain_name, statuses=MagicMock(), arn=arn, only_tags=tags) |
| 346 |
== results |
| 347 |
) |