fix(elbv2): reject missing listeners in describe requests (#7152)

Promote the complete retained fork #235 listener lookup validation and affected SDK and CloudFormation regressions on the qualified main base. Preserve the original compatibility fixture and JUnit SDK adapter.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-31 20:15:05 -04:00
1 parent 8f5548687f
commit faa9ff2365
7 files changed
+255 -3

No files matched your search

@@ -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<String> 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();
}
}
+6
View File
@@ -516,6 +516,12 @@
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>elasticloadbalancingv2</artifactId>
<version>${aws.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ec2</artifactId>
@@ -684,12 +684,16 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc
List<Listener> 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<String> 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;
@@ -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("<Code>ListenerNotFound</Code>"))
.body(not(containsString("<ListenerArn>" + listenerArn + "</ListenerArn>")));
}
private static void assertListenerPresent(String listenerArn) {
@@ -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();
@@ -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<String> 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<String> 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();
}
}
@@ -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<String> 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