fix(elbv2): preserve default listener certificates (#5692)

* fix(elbv2): preserve the default listener certificate

* fix(elbv2): keep certificate checks independent of other VPCs
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-14 23:13:33 -04:00
1 parent 4c3d9cad2d
commit a55352bf9d
5 files changed
+236 -1

No files matched your search

@@ -345,4 +345,51 @@ class ElbV2ListenerCertificateIntegrationTest {
assertTrue(listenerCertificates(listenerArn).containsAll(List.of(first, second)),
"both certificates should be on the listener");
}
@Test
void removingTheOnlyDefaultCertificateReturnsOperationNotPermitted() {
String certificate = uploadCertificate();
String listener = listenerServing(certificate);
assertEquals(List.of(certificate), listenerCertificates(listener));
given().header("Authorization", ELB_AUTH)
.formParam("Action", "RemoveListenerCertificates").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", certificate)
.when().post("/").then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("OperationNotPermitted"));
assertEquals(List.of(certificate), listenerCertificates(listener));
}
@Test
void defaultRemovalKeepsAdditionalCertificatesAndModifyCanReplaceTheDefault() {
String first = uploadCertificate();
String additional = uploadCertificate();
String listener = listenerServing(first);
given().header("Authorization", ELB_AUTH)
.formParam("Action", "AddListenerCertificates").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", additional)
.when().post("/").then().statusCode(200);
assertEquals(List.of(first, additional), listenerCertificates(listener));
given().header("Authorization", ELB_AUTH)
.formParam("Action", "RemoveListenerCertificates").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", first)
.when().post("/").then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("OperationNotPermitted"));
assertEquals(List.of(first, additional), listenerCertificates(listener));
given().header("Authorization", ELB_AUTH)
.formParam("Action", "RemoveListenerCertificates").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", additional)
.when().post("/").then().statusCode(200);
assertEquals(List.of(first), listenerCertificates(listener));
given().header("Authorization", ELB_AUTH)
.formParam("Action", "ModifyListener").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", additional)
.when().post("/").then().statusCode(200);
assertEquals(List.of(additional), listenerCertificates(listener));
given().header("Authorization", ELB_AUTH)
.formParam("Action", "RemoveListenerCertificates").formParam("ListenerArn", listener)
.formParam("Certificates.member.1.CertificateArn", additional)
.when().post("/").then().statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("OperationNotPermitted"));
assertEquals(List.of(additional), listenerCertificates(listener));
}
}
@@ -22,14 +22,22 @@ import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
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.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -82,6 +90,86 @@ class ElbV2ServiceTest {
stubAlbSubnets(ec2Service);
}
@ParameterizedTest
@ValueSource(strings = {"HTTPS", "TLS"})
void defaultCertificateRemovalIsRefusedBeforeChangingTheList(String protocol) {
String lbArn = service.createLoadBalancer(REGION, "default-cert-lb", "internal",
"TLS".equals(protocol) ? "network" : "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String first = "arn:aws:iam::000000000000:server-certificate/first";
String additional = "arn:aws:iam::000000000000:server-certificate/additional";
String listenerArn = service.createListener(REGION, lbArn, protocol, 9443, null,
List.of(first, additional), List.of(), List.of(), Map.of()).getListenerArn();
clearInvocations(dataPlane);
AwsException rejected = assertThrows(AwsException.class,
() -> service.removeListenerCertificates(REGION, listenerArn, List.of(additional, first)));
assertEquals("OperationNotPermitted", rejected.getErrorCode());
assertEquals(List.of(first, additional), service.describeListenerCertificates(REGION, listenerArn));
verifyNoInteractions(dataPlane);
service.removeListenerCertificates(REGION, listenerArn, List.of(additional));
assertEquals(List.of(first), service.describeListenerCertificates(REGION, listenerArn));
assertThrows(AwsException.class,
() -> service.removeListenerCertificates(REGION, listenerArn, List.of(first)));
assertEquals(List.of(first), service.describeListenerCertificates(REGION, listenerArn));
}
@Test
void removalCannotDeleteADefaultPublishedByConcurrentModification() throws Exception {
String lbArn = service.createLoadBalancer(REGION, "default-replacement-lb", "internal", "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String first = "arn:aws:iam::000000000000:server-certificate/first";
String additional = "arn:aws:iam::000000000000:server-certificate/additional";
String listener = service.createListener(REGION, lbArn, "HTTPS", 9443, null,
List.of(first, additional), List.of(), List.of(), Map.of()).getListenerArn();
CountDownLatch checkedOriginalDefault = new CountDownLatch(1);
CountDownLatch releaseRemoval = new CountDownLatch(1);
List<String> requested = new ArrayList<>(List.of(additional)) {
@Override
public boolean contains(Object value) {
boolean contains = super.contains(value);
checkedOriginalDefault.countDown();
try {
assertTrue(releaseRemoval.await(5, TimeUnit.SECONDS), "removal was not released");
} catch (InterruptedException failure) {
Thread.currentThread().interrupt();
throw new AssertionError("removal interrupted", failure);
}
return contains;
}
};
ExecutorService worker = Executors.newSingleThreadExecutor();
try {
Future<?> removal = worker.submit(() -> service.removeListenerCertificates(REGION, listener, requested));
assertTrue(checkedOriginalDefault.await(5, TimeUnit.SECONDS), "removal did not check the original default");
service.modifyListener(REGION, listener, null, null, null, List.of(additional), null, null);
assertEquals(List.of(additional), service.describeListenerCertificates(REGION, listener));
releaseRemoval.countDown();
removal.get(5, TimeUnit.SECONDS);
assertEquals(List.of(additional), service.describeListenerCertificates(REGION, listener));
AwsException rejected = assertThrows(AwsException.class,
() -> service.removeListenerCertificates(REGION, listener, List.of(additional)));
assertEquals("OperationNotPermitted", rejected.getErrorCode());
assertEquals(List.of(additional), service.describeListenerCertificates(REGION, listener));
} finally {
releaseRemoval.countDown();
worker.shutdownNow();
assertTrue(worker.awaitTermination(5, TimeUnit.SECONDS));
}
}
@Test
void certificateMetadataOnAnHttpListenerKeepsItsExistingRemovalBehavior() {
String lbArn = service.createLoadBalancer(REGION, "http-cert-lb", "internal", "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String certificate = "arn:aws:iam::000000000000:server-certificate/metadata";
String listenerArn = service.createListener(REGION, lbArn, "HTTP", 8080, null,
List.of(certificate), List.of(), List.of(), Map.of()).getListenerArn();
service.removeListenerCertificates(REGION, listenerArn, List.of(certificate));
assertEquals(List.of(), service.describeListenerCertificates(REGION, listenerArn));
}
@Test
void modifyListenerDefaultActionsRecompilesRulesWithoutRestartingListener() {
String lbArn = service.createLoadBalancer(