mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
A listener stored whatever certificate ARN it was given. CreateListener, ModifyListener and AddListenerCertificates each model CertificateNotFound, and all three took an ARN naming nothing and answered 200, so a listener could be left serving a certificate that was never uploaded. Only the other direction worked: ELBv2 reports its references to IAM, so DeleteServerCertificate already refused with DeleteConflict while a listener held one. The error is the one AWS publishes on those three operations and no others: botocore gives CertificateNotFoundException the code CertificateNotFound, 400, senderFault, and the API reference lists it under AddListenerCertificates, CreateListener and ModifyListener with one sentence, "The specified certificate does not exist." That sentence is the message rather than one naming the ARN, because it is the one the reference publishes. The check covers the listener types the member belongs to and no more. The reference marks Certificates "[HTTPS and TLS listeners]" on CreateListener and ModifyListener, and describes AddListenerCertificates as adding to the list "for the specified HTTPS or TLS listener", so a certificate on a listener of any other protocol is a configuration it does not describe. What AWS answers there is not CertificateNotFound on any evidence to hand, so such a request keeps the behaviour it had rather than being refused with a code read off the wrong case. A test pins each side of that line. The line follows the protocol the listener ends the call with, not the one it started with: ModifyListener takes a protocol without taking certificates, so when none arrive the certificates checked are the ones already on file. An HTTP listener holding an ARN that resolves to nothing is left alone, and the request that turns it into an HTTPS one, which is where those certificates start being served, is refused. A blank entry is refused the same way, rather than skipped. Certificates is the list of certificates the listener serves, an empty value in it names one no better than a value that is no ARN at all, and the Query parser turns an empty CertificateArn parameter into exactly that entry, so skipping it stored a certificate the listener cannot serve. ELB Classic already answered this question in full, resolving an ARN to an IAM server certificate or an ACM one and holding IAM's certificate lock across the write. This is the second caller, so the three private methods it kept, certificateExists, iamServerCertificate and acmCertificateExists, move to CertificateReferenceResolver in core.common and Classic reads them from there. Neither referrer owns the lookup: IAM holds server certificates, ACM holds the rest, and a listener carries only the ARN. Classic's public constructors are untouched, so its unit tests still build it the same way. The check alone is not enough, which is why the lock moves with it. A certificate can be deleted between the check and the write, and the delete walks the reference providers under IAM's own lock, so every write that records an IAM certificate runs inside that lock and re-checks there, while the fail-fast check outside it keeps the error precedence. That covers CreateListener as much as the two writes to a listener already published: until the new listener is in the map, nothing the delete walks can see that it holds the certificate. A race test drives each of them against DeleteServerCertificate over the wire, and with the lock removed they leave a listener holding a deleted certificate in 1 of 30 trials for AddListenerCertificates and 4 of 30 for CreateListener, so the guarantee is measured rather than asserted. Where the check sits in ModifyListener is part of the fix. The listener it modifies is the stored object, and the port, protocol and SSL policy were set before anything could refuse the request, so a refusal answered 400 over a listener already moved to the new port: no later read put it back, and the data plane was never restarted to match. Every refusal now lands before the first setter. An absent resolver leaves the old behaviour in place. ElbV2Service is wired by hand in its unit tests, with no IAM and no ACM, and a listener write there is the write it was before any certificate existed to check. The dependency is a field rather than a constructor parameter because that is how this class takes all six of its others, and converting it would be a refactor this change does not need. docs/services/elb.md said listener certificate ARNs are not validated when they are added, which was true and is the behaviour this removes, so it now documents the three operations, the protocols they cover, the empty value, the protocol change, the error and the lock, as the Classic page already does. Sixteen integration tests cover a missing IAM certificate, a missing ACM one, a blank entry, a value that is no ARN at all and an ARN of a service that serves no certificates, an uploaded certificate and an ACM certificate being accepted, a listener carrying no certificate, a TLS listener held to the same check as an HTTPS one and a listener of a protocol with no certificates left alone, a protocol change refused over the certificate it already held and allowed over one that exists, a second certificate being added, a refused change leaving the port and protocol as they were, and the two writes that must refuse without changing what the listener already serves. Every certificate and load balancer they make is registered for cleanup: the account's certificate quota is 20 and the Quarkus instance is shared, so one left behind fails an unrelated class's upload instead of anything here. Three things the reference states about these operations stay open, each wanting an error code no source here gives: CreateListener and ModifyListener take "exactly one certificate" and AddListenerCertificates "one certificate per call", where Floci accepts a list; TooManyCertificates is modeled on all three and no quota is enforced; and DescribeListenerCertificates is documented to report the default certificate alongside the list, with IsDefault telling them apart, which Floci does not model. Driven through the standard hashicorp/aws provider against a local instance as well, which is how the change was checked against a real client rather than only against the suite: an aws_iam_server_certificate and an aws_lb_listener serving it apply clean, and a listener naming a certificate nobody uploaded fails with CertificateNotFound and the reference's sentence. That failure arrives at the end of the provider's create timeout rather than at once, because the provider retries this error on all three calls, the AddListenerCertificates one against a constant named iamPropagationTimeout: real AWS is eventually consistent about a certificate just imported, so a client waits it out. Floci's storage is immediately consistent and nothing here models a propagation delay, so the retry simply ends in the refusal. The same contract is pinned at the SDK boundary too, as eight boto3 tests in compatibility-tests: the refusal on each of the three operations, the accepted cases, and both protocol edges. They run in under a second, where driving the same thing through Terraform in CI costs about a minute for the accepted case and nearly two for the refusal, almost all of it the provider's waiter and the retry above rather than anything Floci does. The suite had no ELB coverage at all before this. Each test takes its load balancer back out before its certificates, which is the order that frees them, and test_acm.py's private certificate builder moves to a conftest fixture taking the common name rather than being copied, since the new file needs the same pair.
328 lines
13 KiB
Python
328 lines
13 KiB
Python
"""ACM integration tests."""
|
|
|
|
import re
|
|
|
|
import pytest
|
|
from botocore.exceptions import ClientError
|
|
from cryptography import x509
|
|
from cryptography.hazmat.primitives import serialization
|
|
from cryptography.hazmat.primitives.asymmetric import ec, rsa
|
|
|
|
FAKE_ARN = "arn:aws:acm:us-east-1:000000000000:certificate/00000000-0000-0000-0000-000000000000"
|
|
|
|
|
|
class TestACMCertificateLifecycle:
|
|
"""Test ACM certificate lifecycle operations."""
|
|
|
|
def test_request_certificate(self, acm_client):
|
|
"""Test RequestCertificate creates a certificate and returns a valid ARN."""
|
|
response = acm_client.request_certificate(DomainName="test.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
assert arn
|
|
assert re.match(r"arn:aws:acm:.*:.*:certificate/.*", arn)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
@pytest.mark.parametrize(
|
|
"key_algorithm,key_type",
|
|
[("RSA_2048", rsa.RSAPublicKey), ("EC_prime256v1", ec.EllipticCurvePublicKey)],
|
|
)
|
|
def test_request_certificate_key_algorithms(
|
|
self, acm_client, key_algorithm, key_type
|
|
):
|
|
"""Test RequestCertificate issues a parseable certificate for each key algorithm."""
|
|
response = acm_client.request_certificate(
|
|
DomainName=f"{key_algorithm.lower().replace('_', '-')}.example.com",
|
|
KeyAlgorithm=key_algorithm,
|
|
)
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.get_certificate(CertificateArn=arn)
|
|
certificate = x509.load_pem_x509_certificate(response["Certificate"].encode())
|
|
assert isinstance(certificate.public_key(), key_type)
|
|
# As on AWS, the certificate is signed by the issuing CA, returned first in
|
|
# CertificateChain, not by its own key: check that signature, not just the PEM.
|
|
issuer = x509.load_pem_x509_certificate(response["CertificateChain"].encode())
|
|
assert certificate.issuer == issuer.subject
|
|
certificate.verify_directly_issued_by(issuer)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_describe_certificate(self, acm_client):
|
|
"""Test DescribeCertificate returns certificate details."""
|
|
response = acm_client.request_certificate(DomainName="describe.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.describe_certificate(CertificateArn=arn)
|
|
cert = response["Certificate"]
|
|
assert cert["DomainName"] == "describe.example.com"
|
|
assert cert["Status"] == "ISSUED"
|
|
assert cert["Type"] == "AMAZON_ISSUED"
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_get_certificate(self, acm_client):
|
|
"""Test GetCertificate returns certificate body in PEM format."""
|
|
response = acm_client.request_certificate(DomainName="get.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.get_certificate(CertificateArn=arn)
|
|
assert response["Certificate"]
|
|
assert "BEGIN CERTIFICATE" in response["Certificate"]
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_list_certificates(self, acm_client):
|
|
"""Test ListCertificates includes created certificate."""
|
|
response = acm_client.request_certificate(DomainName="list.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.list_certificates()
|
|
arns = [
|
|
c["CertificateArn"]
|
|
for c in response["CertificateSummaryList"]
|
|
]
|
|
assert arn in arns
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_delete_certificate(self, acm_client):
|
|
"""Test DeleteCertificate removes certificate."""
|
|
response = acm_client.request_certificate(DomainName="delete.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
with pytest.raises(ClientError) as exc_info:
|
|
acm_client.describe_certificate(CertificateArn=arn)
|
|
assert "ResourceNotFoundException" in str(exc_info.value)
|
|
|
|
|
|
class TestACMImportExport:
|
|
"""Test ACM import and export operations."""
|
|
|
|
def test_import_certificate(self, acm_client, make_self_signed_certificate):
|
|
"""Test ImportCertificate with self-signed cert returns ARN."""
|
|
cert_pem, key_pem = make_self_signed_certificate()
|
|
|
|
response = acm_client.import_certificate(
|
|
Certificate=cert_pem, PrivateKey=key_pem
|
|
)
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
assert arn
|
|
assert re.match(r"arn:aws:acm:.*:.*:certificate/.*", arn)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_import_certificate_with_chain(self, acm_client, make_self_signed_certificate):
|
|
"""Test ImportCertificate with certificate chain returns ARN."""
|
|
cert_pem, key_pem = make_self_signed_certificate()
|
|
|
|
response = acm_client.import_certificate(
|
|
Certificate=cert_pem,
|
|
PrivateKey=key_pem,
|
|
CertificateChain=cert_pem,
|
|
)
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
assert arn
|
|
assert re.match(r"arn:aws:acm:.*:.*:certificate/.*", arn)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_get_imported_certificate(self, acm_client, make_self_signed_certificate):
|
|
"""Test GetCertificate on imported cert returns matching body."""
|
|
cert_pem, key_pem = make_self_signed_certificate()
|
|
|
|
import_response = acm_client.import_certificate(
|
|
Certificate=cert_pem, PrivateKey=key_pem
|
|
)
|
|
arn = import_response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.get_certificate(CertificateArn=arn)
|
|
assert response["Certificate"]
|
|
assert "BEGIN CERTIFICATE" in response["Certificate"]
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_export_certificate(self, acm_client, make_self_signed_certificate):
|
|
"""Test ExportCertificate returns the imported key, encrypted under the passphrase."""
|
|
cert_pem, key_pem = make_self_signed_certificate()
|
|
passphrase = b"test-passphrase"
|
|
|
|
import_response = acm_client.import_certificate(
|
|
Certificate=cert_pem, PrivateKey=key_pem
|
|
)
|
|
arn = import_response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.export_certificate(
|
|
CertificateArn=arn, Passphrase=passphrase
|
|
)
|
|
assert response["Certificate"]
|
|
assert response["PrivateKey"]
|
|
|
|
# Asserting on the PEM header alone would pass for a key no client can
|
|
# open. Decrypt it with the passphrase and check it is the key that was
|
|
# imported, which is the whole point of the export.
|
|
exported_key = serialization.load_pem_private_key(
|
|
response["PrivateKey"].encode(), password=passphrase
|
|
)
|
|
imported_key = serialization.load_pem_private_key(key_pem, password=None)
|
|
assert (
|
|
exported_key.private_numbers() == imported_key.private_numbers()
|
|
)
|
|
|
|
# The passphrase has to actually be doing the work.
|
|
with pytest.raises(ValueError):
|
|
serialization.load_pem_private_key(
|
|
response["PrivateKey"].encode(), password=b"wrong-passphrase"
|
|
)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_export_requested_certificate_fails(self, acm_client):
|
|
"""Test ExportCertificate on a requested (non-imported) cert raises error."""
|
|
response = acm_client.request_certificate(DomainName="export.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
with pytest.raises(ClientError):
|
|
acm_client.export_certificate(
|
|
CertificateArn=arn, Passphrase=b"test-passphrase"
|
|
)
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
|
|
class TestACMTagging:
|
|
"""Test ACM tagging operations."""
|
|
|
|
def test_add_tags(self, acm_client):
|
|
"""Test AddTagsToCertificate and ListTagsForCertificate."""
|
|
response = acm_client.request_certificate(DomainName="tags.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
acm_client.add_tags_to_certificate(
|
|
CertificateArn=arn,
|
|
Tags=[
|
|
{"Key": "Env", "Value": "test"},
|
|
{"Key": "Project", "Value": "floci"},
|
|
],
|
|
)
|
|
|
|
response = acm_client.list_tags_for_certificate(CertificateArn=arn)
|
|
tags = response["Tags"]
|
|
tag_map = {t["Key"]: t["Value"] for t in tags}
|
|
assert tag_map["Env"] == "test"
|
|
assert tag_map["Project"] == "floci"
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_remove_tags(self, acm_client):
|
|
"""Test RemoveTagsFromCertificate removes specified tags."""
|
|
response = acm_client.request_certificate(DomainName="rmtags.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
acm_client.add_tags_to_certificate(
|
|
CertificateArn=arn,
|
|
Tags=[
|
|
{"Key": "Env", "Value": "test"},
|
|
{"Key": "Project", "Value": "floci"},
|
|
],
|
|
)
|
|
|
|
acm_client.remove_tags_from_certificate(
|
|
CertificateArn=arn,
|
|
Tags=[{"Key": "Env", "Value": "test"}],
|
|
)
|
|
|
|
response = acm_client.list_tags_for_certificate(CertificateArn=arn)
|
|
tags = response.get("Tags", [])
|
|
tag_keys = [t["Key"] for t in tags]
|
|
assert "Env" not in tag_keys
|
|
assert "Project" in tag_keys
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_list_tags_empty(self, acm_client):
|
|
"""Test ListTagsForCertificate on fresh cert returns empty tags."""
|
|
response = acm_client.request_certificate(DomainName="notags.example.com")
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.list_tags_for_certificate(CertificateArn=arn)
|
|
tags = response.get("Tags", [])
|
|
assert len(tags) == 0
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
|
|
class TestACMAccountConfiguration:
|
|
"""Test ACM account configuration operations."""
|
|
|
|
def test_put_and_get_account_configuration(self, acm_client, unique_name):
|
|
"""Test PutAccountConfiguration and GetAccountConfiguration."""
|
|
acm_client.put_account_configuration(
|
|
ExpiryEvents={"DaysBeforeExpiry": 45},
|
|
IdempotencyToken=unique_name,
|
|
)
|
|
|
|
response = acm_client.get_account_configuration()
|
|
assert response["ExpiryEvents"]["DaysBeforeExpiry"] == 45
|
|
|
|
|
|
class TestACMErrorHandling:
|
|
"""Test ACM error handling."""
|
|
|
|
def test_describe_nonexistent_certificate(self, acm_client):
|
|
"""Test DescribeCertificate with fake ARN raises error."""
|
|
with pytest.raises(ClientError):
|
|
acm_client.describe_certificate(CertificateArn=FAKE_ARN)
|
|
|
|
def test_delete_nonexistent_certificate(self, acm_client):
|
|
"""Test DeleteCertificate with fake ARN raises error."""
|
|
with pytest.raises(ClientError):
|
|
acm_client.delete_certificate(CertificateArn=FAKE_ARN)
|
|
|
|
def test_request_certificate_with_sans(self, acm_client):
|
|
"""Test RequestCertificate with SubjectAlternativeNames."""
|
|
response = acm_client.request_certificate(
|
|
DomainName="sans.example.com",
|
|
SubjectAlternativeNames=[
|
|
"sans.example.com",
|
|
"www.sans.example.com",
|
|
"api.sans.example.com",
|
|
],
|
|
)
|
|
arn = response["CertificateArn"]
|
|
|
|
try:
|
|
response = acm_client.describe_certificate(CertificateArn=arn)
|
|
sans = response["Certificate"]["SubjectAlternativeNames"]
|
|
assert "sans.example.com" in sans
|
|
assert "www.sans.example.com" in sans
|
|
assert "api.sans.example.com" in sans
|
|
finally:
|
|
acm_client.delete_certificate(CertificateArn=arn)
|
|
|
|
def test_import_invalid_pem(self, acm_client):
|
|
"""Test ImportCertificate with invalid PEM raises error."""
|
|
with pytest.raises(ClientError):
|
|
acm_client.import_certificate(
|
|
Certificate=b"not-a-valid-certificate",
|
|
PrivateKey=b"not-a-valid-key",
|
|
)
|