diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2DefaultCertificateTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2DefaultCertificateTest.java index f8126eb60a..787eac329a 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2DefaultCertificateTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2DefaultCertificateTest.java @@ -6,16 +6,71 @@ import org.junit.jupiter.api.Test; import software.amazon.awssdk.services.acm.AcmClient; import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client; import software.amazon.awssdk.services.elasticloadbalancingv2.model.Certificate; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.InvalidConfigurationRequestException; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.Listener; import software.amazon.awssdk.services.elasticloadbalancingv2.model.OperationNotPermittedException; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; @DisplayName("ELBv2 default listener certificates") class ElbV2DefaultCertificateTest { + @Test + @DisplayName("HTTPS needs a default before listener creation or protocol replacement") + void missingHttpsDefaultDoesNotPublishOrChangeAListener() { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Local certificate and listener fixtures"); + try (AcmClient acm = TestFixtures.acmClient(); + ElasticLoadBalancingV2Client elb = TestFixtures.elbV2Client()) { + String certificate = acm.requestCertificate(b -> b.domainName("required.example.test").validationMethod("DNS")) + .certificateArn(); + String loadBalancer = null; + try { + loadBalancer = elb.createLoadBalancer(b -> b.name(TestFixtures.uniqueName("sdk-https-default")) + .type("application")).loadBalancers().get(0).loadBalancerArn(); + String lbArn = loadBalancer; + InvalidConfigurationRequestException creation = assertThrows(InvalidConfigurationRequestException.class, + () -> createListener(elb, lbArn, "HTTPS", 443, null)); + assertThat(creation.statusCode()).isEqualTo(400); + assertThat(elb.describeListeners(b -> b.loadBalancerArn(lbArn)).listeners()).isEmpty(); + String https = createListener(elb, lbArn, "HTTPS", 443, certificate); + elb.modifyListener(b -> b.listenerArn(https).port(444).certificates(List.of())); + assertThat(certificates(elb, https)).containsExactly(certificate); + String http = createListener(elb, lbArn, "HTTP", 8080, null); + InvalidConfigurationRequestException modification = assertThrows(InvalidConfigurationRequestException.class, + () -> elb.modifyListener(b -> b.listenerArn(http).protocol("HTTPS").port(8443))); + assertThat(modification.statusCode()).isEqualTo(400); + Listener unchanged = elb.describeListeners(b -> b.listenerArns(http)).listeners().get(0); + assertThat(unchanged.protocolAsString()).isEqualTo("HTTP"); + assertThat(unchanged.port()).isEqualTo(8080); + assertThat(unchanged.certificates()).isEmpty(); + elb.modifyListener(b -> b.listenerArn(http).protocol("HTTPS").port(8443) + .certificates(Certificate.builder().certificateArn(certificate).build())); + assertThat(certificates(elb, http)).containsExactly(certificate); + } finally { + String ownedLoadBalancer = loadBalancer; + assertAll("Delete listeners before their certificate", + () -> { if (ownedLoadBalancer != null) { elb.deleteLoadBalancer(b -> b.loadBalancerArn(ownedLoadBalancer)); } }, + () -> acm.deleteCertificate(b -> b.certificateArn(certificate))); + } + } + } + + private static String createListener(ElasticLoadBalancingV2Client elb, String lb, String protocol, + int port, String certificate) { + return elb.createListener(b -> { + b.loadBalancerArn(lb).protocol(protocol).port(port).defaultActions(a -> a.type("fixed-response") + .fixedResponseConfig(f -> f.statusCode("200").contentType("text/plain").messageBody("ok"))); + if (certificate != null) { + b.certificates(Certificate.builder().certificateArn(certificate).build()); + } + }).listeners().get(0).listenerArn(); + } + @Test @DisplayName("default removal is refused and ModifyListener can replace it") void defaultRemovalIsRefusedAndModifyCanReplaceIt() { diff --git a/compatibility-tests/sdk-test-python/tests/test_elbv2_listener_certificates.py b/compatibility-tests/sdk-test-python/tests/test_elbv2_listener_certificates.py index b0b71f512d..6b1afa4964 100644 --- a/compatibility-tests/sdk-test-python/tests/test_elbv2_listener_certificates.py +++ b/compatibility-tests/sdk-test-python/tests/test_elbv2_listener_certificates.py @@ -135,9 +135,18 @@ class TestELBv2ListenerCertificates: assert listeners.certificates_on(listener_arn) == {certificate_arn} - def test_create_listener_without_certificates_is_untouched(self, listeners): - """Test CreateListener with no certificates at all is accepted.""" - assert listeners.create_listener() + def test_create_listener_requires_an_https_default(self, listeners, elbv2_client): + """A rejected HTTPS creation publishes nothing and can retry with a certificate.""" + with pytest.raises(ClientError) as exc_info: + listeners.create_listener() + assert exc_info.value.response["Error"]["Code"] == "InvalidConfigurationRequest" + assert exc_info.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400 + assert elbv2_client.describe_listeners( + LoadBalancerArn=listeners.load_balancer_arn + )["Listeners"] == [] + certificate_arn = listeners.upload_certificate() + listener_arn = listeners.create_listener(certificate_arn) + assert listeners.certificates_on(listener_arn) == {certificate_arn} def test_http_listener_is_left_alone(self, listeners): """Test the check covers the protocols the Certificates member is bracketed for. diff --git a/docs/services/elb.md b/docs/services/elb.md index ea5051190a..d493277c07 100644 --- a/docs/services/elb.md +++ b/docs/services/elb.md @@ -45,6 +45,8 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB) ### Listeners +HTTPS listeners require a default certificate. Supply one when creating a listener or changing an HTTP listener to HTTPS; an omitted certificate list on an existing HTTPS listener keeps its default. + | Action | Description | |--------|-------------| | CreateListener | Creates a listener and its non-deletable default rule. | diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java index 72f25e7c96..b65a32a5a3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2Service.java @@ -506,7 +506,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc // Fail fast, then again under the guard the listener is published inside: this one keeps // the error precedence, and only the one holding the lock is atomic with the write. - requireCertificates(region, protocol, certificates); + requireListenerCertificates(region, protocol, certificates); LoadBalancer lb = requireLoadBalancer(region, lbArn); String lbType = lb.getType() != null ? lb.getType() : "application"; @@ -530,7 +530,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc // certificate, so a delete landing in that window would take the certificate out from // under a listener that is about to serve it. whileCertificatesHeld(certificates, () -> { - requireCertificates(region, protocol, certificates); + requireListenerCertificates(region, protocol, certificates); regionListeners.put(listenerArn, listener); listeners.put(region, regionListeners); }); @@ -633,7 +633,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc String effectiveProtocol = protocol != null ? protocol : listener.getProtocol(); List effectiveCertificates = certificates != null ? certificates : listener.getCertificates(); - requireCertificates(region, effectiveProtocol, effectiveCertificates); + requireListenerCertificates(region, effectiveProtocol, effectiveCertificates); // Every setter runs inside the guard, behind the re-check, which is what makes the // comment above hold for that refusal too: the check made outside the guard says nothing @@ -645,7 +645,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc // DeleteServerCertificate finds among the references and refuses to delete. The re-check // covers the effective list all the same, so the refusal is the same one either way. whileCertificatesHeld(certificates, () -> { - requireCertificates(region, effectiveProtocol, effectiveCertificates); + requireListenerCertificates(region, effectiveProtocol, effectiveCertificates); if (portChanges) { listener.setPort(port); } @@ -1027,6 +1027,14 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc } } + private void requireListenerCertificates(String region, String protocol, List certificateArns) { + requireCertificates(region, protocol, certificateArns); + if ("HTTPS".equalsIgnoreCase(protocol) && (certificateArns == null || certificateArns.isEmpty())) { + throw new AwsException("InvalidConfigurationRequest", + "An HTTPS listener requires a default certificate.", 400); + } + } + /** The listener protocols a certificate belongs to, per the reference's own bracket. */ private static boolean servesCertificates(String protocol) { return "HTTPS".equalsIgnoreCase(protocol) || "TLS".equalsIgnoreCase(protocol); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2AuthenticateActionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2AuthenticateActionIntegrationTest.java index 81f9ceac03..de3ffcf44f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2AuthenticateActionIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2AuthenticateActionIntegrationTest.java @@ -2,6 +2,9 @@ package io.github.hectorvent.floci.services.elbv2; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.config.EncoderConfig; +import io.restassured.http.ContentType; import io.restassured.path.xml.XmlPath; import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.MethodOrderer; @@ -9,8 +12,10 @@ import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -29,6 +34,10 @@ class ElbV2AuthenticateActionIntegrationTest { private static final String AUTH = "AWS4-HMAC-SHA256 Credential=test/20260427/us-east-1/elasticloadbalancing/aws4_request"; + private static final String ACM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260427/us-east-1/acm/aws4_request"; + + private static String certificateArn; private static String lbArn; private static String tgArn; private static String listenerArn; @@ -41,6 +50,13 @@ class ElbV2AuthenticateActionIntegrationTest { @Test @Order(1) void seedLoadBalancerAndTargetGroup() { + certificateArn = given().contentType("application/x-amz-json-1.1") + .config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig() + .encodeContentTypeAs("application/x-amz-json-1.1", ContentType.TEXT))) + .header("Authorization", ACM_AUTH) + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .body("{\"DomainName\":\"auth-action.example.test\",\"ValidationMethod\":\"DNS\"}") + .when().post("/").then().statusCode(200).extract().path("CertificateArn"); lbArn = XmlPath.from(xml(given() .formParam("Action", "CreateLoadBalancer") .formParam("Name", "auth-action-lb") @@ -68,6 +84,7 @@ class ElbV2AuthenticateActionIntegrationTest { .formParam("Action", "CreateListener") .formParam("LoadBalancerArn", lbArn) .formParam("Protocol", "HTTPS") + .formParam("Certificates.member.1.CertificateArn", certificateArn) .formParam("Port", "443") .formParam("DefaultActions.member.1.Type", "authenticate-oidc") .formParam("DefaultActions.member.1.Order", "1") @@ -166,6 +183,7 @@ class ElbV2AuthenticateActionIntegrationTest { .formParam("Action", "CreateListener") .formParam("LoadBalancerArn", lbArn) .formParam("Protocol", "HTTPS") + .formParam("Certificates.member.1.CertificateArn", certificateArn) .formParam("Port", "9443") .formParam("DefaultActions.member.1.Type", "authenticate-oidc") .formParam(base + "Issuer", "https://idp.example.com") @@ -252,6 +270,7 @@ class ElbV2AuthenticateActionIntegrationTest { .formParam("Action", "CreateListener") .formParam("LoadBalancerArn", lbArn) .formParam("Protocol", "HTTPS") + .formParam("Certificates.member.1.CertificateArn", certificateArn) .formParam("Port", "8443") .formParam("DefaultActions.member.1.Type", "authenticate-oidc") .formParam("DefaultActions.member.1.AuthenticateOidcConfig.Issuer", @@ -261,4 +280,34 @@ class ElbV2AuthenticateActionIntegrationTest { .then().statusCode(400) .body("ErrorResponse.Error.Code", equalTo("ValidationError")); } + + @Test + @Order(8) + void cleanupAuthenticatedListenerFixture() { + assertAll("Delete the authenticated listener before its certificate", + () -> { + if (lbArn != null) { + given().header("Authorization", AUTH).formParam("Action", "DeleteLoadBalancer") + .formParam("LoadBalancerArn", lbArn).when().post("/").then().statusCode(200); + } + }, + () -> { + if (tgArn != null) { + given().header("Authorization", AUTH).formParam("Action", "DeleteTargetGroup") + .formParam("TargetGroupArn", tgArn).when().post("/").then().statusCode(200); + } + }, + () -> { + if (certificateArn != null) { + given().contentType("application/x-amz-json-1.1") + .config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig() + .encodeContentTypeAs("application/x-amz-json-1.1", ContentType.TEXT))) + .header("Authorization", ACM_AUTH) + .header("X-Amz-Target", "CertificateManager.DeleteCertificate") + .body("{\"CertificateArn\":\"" + certificateArn + "\"}") + .when().post("/").then().statusCode(200); + } + }); + } + } diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificateDeleteRaceTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificateDeleteRaceTest.java index 6eb3eeb79d..5381e8f3bd 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificateDeleteRaceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificateDeleteRaceTest.java @@ -4,6 +4,9 @@ import io.github.hectorvent.floci.services.acm.CertificateGenerator; import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.RestAssured; +import io.restassured.config.EncoderConfig; +import io.restassured.http.ContentType; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -36,9 +39,33 @@ class ElbV2CertificateDeleteRaceTest { "AWS4-HMAC-SHA256 Credential=test/20260427/us-east-1/elasticloadbalancing/aws4_request"; private static final String IAM_AUTH = "AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request"; + private static final String ACM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260427/us-east-1/acm/aws4_request"; private static final CertificateGenerator GENERATOR = new CertificateGenerator(); private static final int TRIALS = 30; + private static String createDefaultCertificate() { + return given().contentType("application/x-amz-json-1.1") + .config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig() + .encodeContentTypeAs("application/x-amz-json-1.1", ContentType.TEXT))) + .header("Authorization", ACM_AUTH) + .header("X-Amz-Target", "CertificateManager.RequestCertificate") + .body("{\"DomainName\":\"elbv2.race.test.local\",\"ValidationMethod\":\"DNS\"}") + .when().post("/").then().statusCode(200).extract().path("CertificateArn"); + } + + private static void deleteDefaultCertificate(String defaultCertificateArn) { + if (defaultCertificateArn != null) { + given().contentType("application/x-amz-json-1.1") + .config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig() + .encodeContentTypeAs("application/x-amz-json-1.1", ContentType.TEXT))) + .header("Authorization", ACM_AUTH) + .header("X-Amz-Target", "CertificateManager.DeleteCertificate") + .body("{\"CertificateArn\":\"" + defaultCertificateArn + "\"}") + .when().post("/").then().statusCode(200); + } + } + private static String suffix() { return Long.toString(System.nanoTime(), 36); } @@ -68,12 +95,13 @@ class ElbV2CertificateDeleteRaceTest { + ".LoadBalancers.member.LoadBalancerArn"); } - /** A listener carrying no certificate yet, so the race is only about the one being added. */ - private static String listener(String lbArn) { + /** The stable ACM default is distinct from the IAM certificate raced by AddListenerCertificates. */ + private static String listener(String lbArn, String defaultCertificateArn) { return given().header("Authorization", ELB_AUTH) .formParam("Action", "CreateListener") .formParam("LoadBalancerArn", lbArn) .formParam("Protocol", "HTTPS") + .formParam("Certificates.member.1.CertificateArn", defaultCertificateArn) .formParam("Port", "443") .formParam("DefaultActions.member.1.Type", "fixed-response") .formParam("DefaultActions.member.1.FixedResponseConfig.StatusCode", "200") @@ -125,8 +153,13 @@ class ElbV2CertificateDeleteRaceTest { @Test void addingACertificateNeverLeavesAListenerHoldingADeletedOne() throws Exception { - race("the listener it was added to", ElbV2CertificateDeleteRaceTest::listener, - ElbV2CertificateDeleteRaceTest::addListenerCertificate); + String defaultCertificateArn = createDefaultCertificate(); + try { + race("the listener it was added to", lbArn -> listener(lbArn, defaultCertificateArn), + ElbV2CertificateDeleteRaceTest::addListenerCertificate); + } finally { + deleteDefaultCertificate(defaultCertificateArn); + } } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerCertificateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerCertificateIntegrationTest.java index 795e379fe9..bbe5a1a466 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerCertificateIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerCertificateIntegrationTest.java @@ -8,6 +8,7 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; import io.restassured.config.EncoderConfig; import io.restassured.http.ContentType; +import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.Test; @@ -17,6 +18,7 @@ import java.util.List; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -196,9 +198,47 @@ class ElbV2ListenerCertificateIntegrationTest { } @Test - void createListenerWithNoCertificateIsUntouched() { - // Nothing to resolve, so the check has nothing to say: a listener may carry none. - createListener(null).statusCode(200); + void missingHttpsDefaultIsRefusedWithoutPublishingAListenerAndCanRetry() { + String certificate = uploadCertificate(); + String lb = loadBalancer(); + Response refused = given().header("Authorization", ELB_AUTH).formParam("Action", "CreateListener") + .formParam("LoadBalancerArn", lb).formParam("Protocol", "HTTPS").formParam("Port", "443") + .formParam("DefaultActions.member.1.Type", "fixed-response") + .formParam("DefaultActions.member.1.FixedResponseConfig.StatusCode", "200") + .when().post("/"); + List listeners = given().header("Authorization", ELB_AUTH).formParam("Action", "DescribeListeners") + .formParam("LoadBalancerArn", lb).when().post("/").then().statusCode(200) + .extract().xmlPath().getList("DescribeListenersResponse.DescribeListenersResult.Listeners.member.ListenerArn"); + assertAll(() -> assertEquals(400, refused.statusCode()), + () -> assertEquals("InvalidConfigurationRequest", refused.xmlPath().getString("ErrorResponse.Error.Code")), + () -> assertTrue(listeners.isEmpty(), "rejected request must leave no listener")); + given().header("Authorization", ELB_AUTH).formParam("Action", "CreateListener") + .formParam("LoadBalancerArn", lb).formParam("Protocol", "HTTPS").formParam("Port", "443") + .formParam("Certificates.member.1.CertificateArn", certificate) + .formParam("DefaultActions.member.1.Type", "fixed-response") + .formParam("DefaultActions.member.1.FixedResponseConfig.StatusCode", "200") + .when().post("/").then().statusCode(200); + } + + @Test + void changingHttpToHttpsWithoutADefaultPreservesTheListenerAndCanRetry() { + String certificate = uploadCertificate(); + String listener = listenerServing("HTTP", null); + Response refused = given().header("Authorization", ELB_AUTH).formParam("Action", "ModifyListener") + .formParam("ListenerArn", listener).formParam("Protocol", "HTTPS").formParam("Port", "444") + .when().post("/"); + String protocol = listenerField(listener, "Protocol"); + String port = listenerField(listener, "Port"); + assertAll(() -> assertEquals(400, refused.statusCode()), + () -> assertEquals("InvalidConfigurationRequest", refused.xmlPath().getString("ErrorResponse.Error.Code")), + () -> assertEquals("HTTP", protocol), () -> assertEquals("443", port), + () -> assertTrue(listenerCertificates(listener).isEmpty())); + given().header("Authorization", ELB_AUTH).formParam("Action", "ModifyListener") + .formParam("ListenerArn", listener).formParam("Protocol", "HTTPS").formParam("Port", "444") + .formParam("Certificates.member.1.CertificateArn", certificate) + .when().post("/").then().statusCode(200); + assertEquals("HTTPS", listenerField(listener, "Protocol")); + assertEquals(List.of(certificate), listenerCertificates(listener)); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java index fda821154e..cbfbdc234b 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ServiceTest.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.Mock; @@ -90,6 +91,39 @@ class ElbV2ServiceTest { stubAlbSubnets(ec2Service); } + @ParameterizedTest + @NullAndEmptySource + void httpsCreateNeedsADefaultBeforeAllocatingAListener(List certificates) { + String lb = service.createLoadBalancer(REGION, "https-default", "internal", "application", "ipv4", + ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn(); + AwsException refusal = assertThrows(AwsException.class, () -> service.createListener(REGION, lb, + "HTTPS", 9443, null, certificates, List.of(), List.of(), Map.of("scope", "refused"))); + assertEquals("InvalidConfigurationRequest", refusal.getErrorCode()); + assertTrue(service.describeListeners(REGION, lb, List.of()).isEmpty()); + assertTrue(service.describeRules(REGION, null, List.of()).isEmpty()); + Listener retry = service.createListener(REGION, lb, "HTTPS", 9443, null, + List.of(HELD_CERTIFICATE), List.of(), List.of(), Map.of()); + assertEquals(List.of(HELD_CERTIFICATE), retry.getCertificates()); + } + + @ParameterizedTest + @NullAndEmptySource + void httpsModifyNeedsAnEffectiveDefaultBeforeChangingTheListener(List certificates) { + String lb = service.createLoadBalancer(REGION, "https-change", "internal", "application", "ipv4", + ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn(); + Listener listener = service.createListener(REGION, lb, "HTTP", 9080, null, List.of(), List.of(), List.of(), Map.of()); + AwsException refusal = assertThrows(AwsException.class, () -> service.modifyListener(REGION, + listener.getListenerArn(), "HTTPS", 9443, "ELBSecurityPolicy-2016-08", certificates, null, null)); + assertEquals("InvalidConfigurationRequest", refusal.getErrorCode()); + assertEquals("HTTP", listener.getProtocol()); + assertEquals(9080, listener.getPort()); + assertEquals(List.of(), listener.getCertificates()); + service.modifyListener(REGION, listener.getListenerArn(), "HTTPS", 9443, null, + List.of(HELD_CERTIFICATE), null, null); + assertEquals("HTTPS", listener.getProtocol()); + assertEquals(List.of(HELD_CERTIFICATE), listener.getCertificates()); + } + @ParameterizedTest @ValueSource(strings = {"HTTPS", "TLS"}) void defaultCertificateRemovalIsRefusedBeforeChangingTheList(String protocol) {