|
8 | 8 | import pytest |
9 | 9 | from botocore.config import Config |
10 | 10 | from botocore.exceptions import ClientError |
| 11 | +from cryptography import x509 |
| 12 | +from cryptography.hazmat.backends import default_backend |
| 13 | +from cryptography.hazmat.primitives import hashes, serialization |
| 14 | +from cryptography.hazmat.primitives.asymmetric import rsa |
11 | 15 | from cryptography.x509 import ( |
| 16 | + DNSName, |
12 | 17 | IPAddress, |
| 18 | + NameOID, |
13 | 19 | SubjectAlternativeName, |
14 | 20 | load_pem_x509_certificate, |
15 | 21 | ) |
16 | 22 | from freezegun import freeze_time |
17 | 23 |
|
18 | 24 | from moto import mock_aws, settings |
19 | 25 | from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID |
| 26 | +from moto.core.utils import utcnow |
20 | 27 | from tests.test_elbv2.test_elbv2 import create_load_balancer |
21 | 28 |
|
22 | 29 | RESOURCE_FOLDER = os.path.join(os.path.dirname(__file__), "resources") |
@@ -76,6 +83,51 @@ def test_import_certificate(): |
76 | 83 | assert "CertificateChain" in resp |
77 | 84 |
|
78 | 85 |
|
| 86 | +@mock_aws |
| 87 | +def test_import_certificate_without_cn(): |
| 88 | + """CN is optional per CAB Forum baseline requirements since 2017. |
| 89 | + import_certificate should succeed and DomainName should fall back to the first SAN.""" |
| 90 | + # Generate a cert with SANs but no CN in the subject |
| 91 | + key = rsa.generate_private_key( |
| 92 | + public_exponent=65537, key_size=2048, backend=default_backend() |
| 93 | + ) |
| 94 | + subject = x509.Name([]) # empty subject — no CN |
| 95 | + cert = ( |
| 96 | + x509.CertificateBuilder() |
| 97 | + .subject_name(subject) |
| 98 | + .issuer_name( |
| 99 | + x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Test Issuer")]) |
| 100 | + ) |
| 101 | + .public_key(key.public_key()) |
| 102 | + .serial_number(x509.random_serial_number()) |
| 103 | + .not_valid_before(utcnow()) |
| 104 | + .not_valid_after(utcnow() + datetime.timedelta(days=365)) |
| 105 | + .add_extension( |
| 106 | + x509.SubjectAlternativeName( |
| 107 | + [DNSName("app.test.example.com"), DNSName("app2.test.example.com")] |
| 108 | + ), |
| 109 | + critical=False, |
| 110 | + ) |
| 111 | + .sign(key, hashes.SHA256(), default_backend()) |
| 112 | + ) |
| 113 | + cert_pem = cert.public_bytes(serialization.Encoding.PEM) |
| 114 | + key_pem = key.private_bytes( |
| 115 | + encoding=serialization.Encoding.PEM, |
| 116 | + format=serialization.PrivateFormat.TraditionalOpenSSL, |
| 117 | + encryption_algorithm=serialization.NoEncryption(), |
| 118 | + ) |
| 119 | + |
| 120 | + client = boto3.client("acm", region_name="us-east-1") |
| 121 | + resp = client.import_certificate(Certificate=cert_pem, PrivateKey=key_pem) |
| 122 | + arn = resp["CertificateArn"] |
| 123 | + |
| 124 | + desc = client.describe_certificate(CertificateArn=arn)["Certificate"] |
| 125 | + # DomainName should fall back to the first SAN when CN is absent |
| 126 | + assert desc["DomainName"] == "app.test.example.com" |
| 127 | + assert "app.test.example.com" in desc["SubjectAlternativeNames"] |
| 128 | + assert "app2.test.example.com" in desc["SubjectAlternativeNames"] |
| 129 | + |
| 130 | + |
79 | 131 | @mock_aws |
80 | 132 | def test_import_certificate_with_tags(): |
81 | 133 | client = boto3.client("acm", region_name="eu-central-1") |
@@ -556,14 +608,12 @@ def test_list_certificates_with_key_types_filter(): |
556 | 608 | certs = client.list_certificates( |
557 | 609 | Includes={"keyTypes": ["RSA_2048", "RSA_4096", "EC_prime256v1"]} |
558 | 610 | )["CertificateSummaryList"] |
559 | | - |
560 | | - assert len(certs) == 1 |
561 | | - assert certs[0]["CertificateArn"] == arn |
| 611 | + assert any(cert["CertificateArn"] == arn for cert in certs) |
562 | 612 |
|
563 | 613 | certs = client.list_certificates(Includes={"keyTypes": ["RSA_1024"]})[ |
564 | 614 | "CertificateSummaryList" |
565 | 615 | ] |
566 | | - assert len(certs) == 0 |
| 616 | + assert not any(cert["CertificateArn"] == arn for cert in certs) |
567 | 617 |
|
568 | 618 |
|
569 | 619 | @mock_aws |
|
0 commit comments