diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2ListenerLookupTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2ListenerLookupTest.java new file mode 100644 index 0000000000..f5aee8c3cc --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2ListenerLookupTest.java @@ -0,0 +1,92 @@ +package com.floci.test; + +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.ListenerNotFoundException; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancerNotFoundException; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@DisplayName("ELBv2 listener lookup errors") +class ElbV2ListenerLookupTest { + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + @DisplayName("Deleting a listener or its balancer makes explicit lookup fail without losing another listener") + void deletedListenerIsNotAnEmptyOrPartialSuccess(boolean deleteBalancer) { + Assumptions.assumeFalse(TestFixtures.isRealAws(), "Uses the emulator's default VPC fixtures"); + try (ElasticLoadBalancingV2Client client = TestFixtures.elbV2Client()) { + String first = null; + String second = null; + try { + first = createBalancer(client, "sdk-listener-a"); + second = createBalancer(client, "sdk-listener-b"); + String firstArn = first; + String secondArn = second; + assertThat(client.describeListeners(r -> r.loadBalancerArn(firstArn)).listeners()).isEmpty(); + String removed = createListener(client, first); + String retained = createListener(client, second); + assertThat(client.describeListeners(r -> r.listenerArns(removed, retained)).listeners()) + .extracting(listener -> listener.listenerArn()).containsExactlyInAnyOrder(removed, retained); + + if (deleteBalancer) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(firstArn)); + } else { + client.deleteListener(r -> r.listenerArn(removed)); + } + for (List requested : List.of(List.of(removed), + List.of(retained, removed), List.of(removed, retained))) { + ListenerNotFoundException error = assertThrows(ListenerNotFoundException.class, + () -> client.describeListeners(r -> r.listenerArns(requested))); + assertThat(error.statusCode()).isEqualTo(400); + assertThat(error.awsErrorDetails().errorCode()).isEqualTo("ListenerNotFound"); + assertThat(error.requestId()).isNotBlank(); + } + if (deleteBalancer) { + LoadBalancerNotFoundException error = assertThrows(LoadBalancerNotFoundException.class, + () -> client.describeListeners(r -> r.loadBalancerArn(firstArn))); + assertThat(error.statusCode()).isEqualTo(400); + assertThat(error.awsErrorDetails().errorCode()).isEqualTo("LoadBalancerNotFound"); + } else { + assertThat(client.describeListeners(r -> r.loadBalancerArn(firstArn)).listeners()).isEmpty(); + } + assertThat(client.describeListeners(r -> r.loadBalancerArn(secondArn)).listeners()) + .singleElement().satisfies(listener -> assertThat(listener.listenerArn()).isEqualTo(retained)); + } finally { + String ownedFirst = first; + String ownedSecond = second; + assertAll("Remove both owned balancers and their listeners", + () -> { + if (ownedFirst != null) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(ownedFirst)); + } + }, + () -> { + if (ownedSecond != null) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(ownedSecond)); + } + }); + } + } + } + + private static String createBalancer(ElasticLoadBalancingV2Client client, String prefix) { + return client.createLoadBalancer(r -> r.name(TestFixtures.uniqueName(prefix)) + .type("application").subnets(TestFixtures.defaultVpcSubnets())) + .loadBalancers().get(0).loadBalancerArn(); + } + + private static String createListener(ElasticLoadBalancingV2Client client, String balancer) { + return client.createListener(r -> r.loadBalancerArn(balancer).port(8080).protocol("HTTP") + .defaultActions(a -> a.type("fixed-response").fixedResponseConfig(f -> + f.statusCode("200").contentType("text/plain").messageBody("listener")))) + .listeners().get(0).listenerArn(); + } +} diff --git a/pom.xml b/pom.xml index 06e2bf692c..4cda8702e8 100644 --- a/pom.xml +++ b/pom.xml @@ -516,6 +516,12 @@ ${aws.sdk.version} test + + software.amazon.awssdk + elasticloadbalancingv2 + ${aws.sdk.version} + test + software.amazon.awssdk ec2 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 cac1b3c940..4e1143ac9d 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 @@ -684,12 +684,16 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc List result = new ArrayList<>(regionListeners.values()); if (lbArn != null && !lbArn.isEmpty()) { + requireLoadBalancer(region, lbArn); result = result.stream() .filter(l -> l.getLoadBalancerArn().equals(lbArn)) .collect(Collectors.toList()); } if (listenerArns != null && !listenerArns.isEmpty()) { Set arnSet = new HashSet<>(listenerArns); + for (String arn : arnSet) { + requireListener(region, arn); + } result = result.stream().filter(l -> arnSet.contains(l.getListenerArn())).collect(Collectors.toList()); } return result; diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationElbV2ReplacementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationElbV2ReplacementIntegrationTest.java index fce4e16b36..ba0edf4235 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationElbV2ReplacementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationElbV2ReplacementIntegrationTest.java @@ -315,11 +315,13 @@ class CloudFormationElbV2ReplacementIntegrationTest { return m.group(1); } - /** DescribeListeners filters by ARN and answers an empty list for one that is gone. */ + /** An explicit ARN for a deleted listener reports the modeled not-found error. */ private static void assertListenerMissing(String listenerArn) { given().formParam("Action", "DescribeListeners").formParam("Version", "2015-12-01") .formParam("ListenerArns.member.1", listenerArn) - .when().post("/").then().statusCode(200).body(not(containsString(listenerArn))); + .when().post("/").then().statusCode(400) + .body(containsString("ListenerNotFound")) + .body(not(containsString("" + listenerArn + ""))); } private static void assertListenerPresent(String listenerArn) { diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificatePublicationServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificatePublicationServiceTest.java index 791570960f..44b5fa1395 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificatePublicationServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2CertificatePublicationServiceTest.java @@ -202,7 +202,10 @@ class ElbV2CertificatePublicationServiceTest { } ExecutionException refused = assertThrows(ExecutionException.class, () -> creation.get(5, TimeUnit.SECONDS)); assertEquals("LoadBalancerNotFound", ((AwsException) refused.getCause()).getErrorCode()); - assertTrue(service.describeListeners(REGION, lb.getLoadBalancerArn(), List.of()).isEmpty()); + AwsException missing = assertThrows(AwsException.class, + () -> service.describeListeners(REGION, lb.getLoadBalancerArn(), List.of())); + assertEquals("LoadBalancerNotFound", missing.getErrorCode()); + assertTrue(service.describeListeners(REGION, null, List.of()).isEmpty()); assertTrue(service.describeRules(REGION, null, List.of()).isEmpty()); } finally { worker.shutdownNow(); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerLookupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerLookupIntegrationTest.java new file mode 100644 index 0000000000..c9626bef79 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2ListenerLookupIntegrationTest.java @@ -0,0 +1,100 @@ +package io.github.hectorvent.floci.services.elbv2; + +import io.github.hectorvent.floci.testing.AwsTestClients; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.ListenerNotFoundException; +import software.amazon.awssdk.services.elasticloadbalancingv2.model.LoadBalancerNotFoundException; + +import java.util.List; +import java.util.UUID; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.empty; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@QuarkusTest +class ElbV2ListenerLookupIntegrationTest { + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void deletedListenerIsNotAnEmptyOrPartialSuccess(boolean deleteBalancer) { + try (ElasticLoadBalancingV2Client client = AwsTestClients.create( + ElasticLoadBalancingV2Client.builder(), "000000000000", Region.US_EAST_1)) { + String first = null; + String second = null; + try { + first = createBalancer(client, "sdk-listener-a"); + second = createBalancer(client, "sdk-listener-b"); + String firstArn = first; + String secondArn = second; + assertThat(client.describeListeners(r -> r.loadBalancerArn(firstArn)).listeners(), empty()); + String removed = createListener(client, first); + String retained = createListener(client, second); + assertThat(client.describeListeners(r -> r.listenerArns(removed, retained)).listeners().stream() + .map(listener -> listener.listenerArn()).toList(), containsInAnyOrder(removed, retained)); + + if (deleteBalancer) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(firstArn)); + } else { + client.deleteListener(r -> r.listenerArn(removed)); + } + for (List requested : List.of(List.of(removed), + List.of(retained, removed), List.of(removed, retained))) { + ListenerNotFoundException error = assertThrows(ListenerNotFoundException.class, + () -> client.describeListeners(r -> r.listenerArns(requested))); + assertEquals(400, error.statusCode()); + assertEquals("ListenerNotFound", error.awsErrorDetails().errorCode()); + assertNotNull(error.requestId()); + assertFalse(error.requestId().isBlank()); + } + if (deleteBalancer) { + LoadBalancerNotFoundException error = assertThrows(LoadBalancerNotFoundException.class, + () -> client.describeListeners(r -> r.loadBalancerArn(firstArn))); + assertEquals(400, error.statusCode()); + assertEquals("LoadBalancerNotFound", error.awsErrorDetails().errorCode()); + } else { + assertThat(client.describeListeners(r -> r.loadBalancerArn(firstArn)).listeners(), empty()); + } + List retainedArns = client.describeListeners(r -> r.loadBalancerArn(secondArn)).listeners() + .stream().map(listener -> listener.listenerArn()).toList(); + assertEquals(List.of(retained), retainedArns); + } finally { + String ownedFirst = first; + String ownedSecond = second; + assertAll("Remove both owned balancers and their listeners", + () -> { + if (ownedFirst != null) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(ownedFirst)); + } + }, + () -> { + if (ownedSecond != null) { + client.deleteLoadBalancer(r -> r.loadBalancerArn(ownedSecond)); + } + }); + } + } + } + + private static String createBalancer(ElasticLoadBalancingV2Client client, String prefix) { + return client.createLoadBalancer(r -> r.name(prefix + "-" + UUID.randomUUID().toString().substring(0, 8)) + .type("application").subnets(List.of("subnet-default-us-east-1-a", "subnet-default-us-east-1-b"))) + .loadBalancers().get(0).loadBalancerArn(); + } + + private static String createListener(ElasticLoadBalancingV2Client client, String balancer) { + return client.createListener(r -> r.loadBalancerArn(balancer).port(8080).protocol("HTTP") + .defaultActions(a -> a.type("fixed-response").fixedResponseConfig(f -> + f.statusCode("200").contentType("text/plain").messageBody("listener")))) + .listeners().get(0).listenerArn(); + } +} 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 4e346267e6..30e91e8faa 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 @@ -104,6 +104,51 @@ class ElbV2ServiceTest { stubAlbSubnets(ec2Service); } + @Test + void describeListenersRequiresEveryExplicitArn() { + String lb = service.createLoadBalancer(REGION, "listener-lookup", "internal", "application", "ipv4", + ALB_SUBNETS, List.of(), Map.of()).getLoadBalancerArn(); + String removed = service.createListener(REGION, lb, "HTTP", 8080, null, + List.of(), List.of(), List.of(), Map.of()).getListenerArn(); + Listener retained = service.createListener(REGION, lb, "HTTP", 8081, null, + List.of(), List.of(), List.of(), Map.of()); + service.deleteListener(REGION, removed); + + for (List requested : List.of(List.of(removed), + List.of(retained.getListenerArn(), removed), List.of(removed, retained.getListenerArn()))) { + AwsException error = assertThrows(AwsException.class, + () -> service.describeListeners(REGION, null, requested)); + assertEquals("ListenerNotFound", error.getErrorCode()); + assertEquals(400, error.getHttpStatus()); + } + AwsException otherRegion = assertThrows(AwsException.class, + () -> service.describeListeners("us-east-1", null, List.of(retained.getListenerArn()))); + assertEquals("ListenerNotFound", otherRegion.getErrorCode()); + assertEquals(List.of(retained), service.describeListeners(REGION, lb, List.of())); + assertEquals(List.of(retained), + service.describeListeners(REGION, null, List.of(retained.getListenerArn()))); + } + + @Test + void describeListenersRejectsADeletedBalancerAndItsListeners() { + String lb = service.createLoadBalancer(REGION, "deleted-listeners", "internal", "application", "ipv4", + ALB_SUBNETS, List.of(), Map.of()).getLoadBalancerArn(); + assertTrue(service.describeListeners(REGION, lb, List.of()).isEmpty()); + String listener = service.createListener(REGION, lb, "HTTP", 8080, null, + List.of(), List.of(), List.of(), Map.of()).getListenerArn(); + service.deleteLoadBalancer(REGION, lb); + + AwsException missingListener = assertThrows(AwsException.class, + () -> service.describeListeners(REGION, null, List.of(listener))); + assertEquals("ListenerNotFound", missingListener.getErrorCode()); + assertEquals(400, missingListener.getHttpStatus()); + AwsException missingBalancer = assertThrows(AwsException.class, + () -> service.describeListeners(REGION, lb, List.of())); + assertEquals("LoadBalancerNotFound", missingBalancer.getErrorCode()); + assertEquals(400, missingBalancer.getHttpStatus()); + assertTrue(service.describeListeners(REGION, null, List.of()).isEmpty()); + } + /** * Every type takes Subnets or SubnetMappings, so a balancer with neither is refused before a * subnet is looked up or anything is stored. The messages are the ones AWS gives an Application