fix(elbv2): keep a refused ModifyListener from moving the listener (#5491)

The certificate check on ModifyListener runs twice: once outside IAM's
certificate lock, where it keeps the error precedence AWS has, and once under
the lock, where it is atomic with the write. The second one sat behind the port,
protocol and SSL policy setters, so a DeleteServerCertificate landing between
the two answered CertificateNotFound on a listener that had already moved to its
new port. The comment above the first check says no refusal leaves the listener
changed, which only held for the check it sits on.

Every setter now runs inside the guard, behind the re-check, so a refusal either
lands before anything is written or does not land at all. The guard is still
taken for the certificates the request records, which are the only ones a delete
can race: one the listener already holds is a reference DeleteServerCertificate
finds, and it refuses to delete it. The re-check covers the effective list,
which is what a protocol flip putting the stored certificates into service
needs, so that path is checked under the lock too rather than once on the way
in.

The restartDataPlane local is gone, because a lambda cannot assign a local and
portChanges already says the same thing at the one place it was read.

Two unit tests, both of which fail without the change: a refused modify leaves
the port, protocol, policy and certificates alone and does not restart the data
plane, and a protocol flip refused the same way leaves the protocol alone. The
resolver they wire finds the certificate once and not again, which is what a
delete landing between the two checks looks like from inside the call.
docs/services/elb.md records the guarantee next to the lock it comes from.

Follow-up to the review note on #5474.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-12 20:51:32 -05:00
1 parent b318794fa2
commit ee274b9229
3 files changed
+97 -16

No files matched your search

@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsRegionFacts;
import io.github.hectorvent.floci.core.common.CertificateReferenceResolver;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
@@ -39,6 +40,7 @@ import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -54,6 +56,11 @@ class ElbV2ServiceTest {
// Application Load Balancers require subnets in at least two Availability Zones.
private static final List<String> ALB_SUBNETS = List.of("subnet-a", "subnet-b");
private static final String HELD_CERTIFICATE =
"arn:aws:iam::000000000000:server-certificate/held";
private static final String OTHER_CERTIFICATE =
"arn:aws:iam::000000000000:server-certificate/other";
@Mock
ElbV2DataPlane dataPlane;
@@ -127,6 +134,66 @@ class ElbV2ServiceTest {
verify(dataPlane, never()).startListener(any(Listener.class), anyString(), anyList());
}
@Test
void modifyListenerRefusedUnderTheCertificateGuardLeavesTheListenerWhereItWas() {
String lbArn = service.createLoadBalancer(
REGION, "sample-lb", "internal", "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String tgArn = createTargetGroup("sample-tg");
String listenerArn = service.createListener(
REGION, lbArn, "HTTPS", 443, "ELBSecurityPolicy-2016-08", List.of(HELD_CERTIFICATE),
List.of(forwardAction(tgArn)), List.of(), Map.of()).getListenerArn();
clearInvocations(dataPlane);
service.certificateResolver = deletedOnTheSecondLook();
AwsException e = assertThrows(AwsException.class, () -> service.modifyListener(
REGION, listenerArn, "TLS", 8443, "ELBSecurityPolicy-TLS13-1-2-2021-06",
List.of(OTHER_CERTIFICATE), null, null));
assertEquals("CertificateNotFound", e.getErrorCode());
Listener listener = service.describeListeners(REGION, lbArn, List.of(listenerArn)).getFirst();
assertEquals(Integer.valueOf(443), listener.getPort());
assertEquals("HTTPS", listener.getProtocol());
assertEquals("ELBSecurityPolicy-2016-08", listener.getSslPolicy());
assertEquals(List.of(HELD_CERTIFICATE), listener.getCertificates());
verify(dataPlane, never()).restartListener(any(Listener.class), anyString(), anyList());
}
@Test
void modifyListenerProtocolAloneTakesTheGuardForTheCertificatesItPutsIntoService() {
String lbArn = service.createLoadBalancer(
REGION, "sample-lb", "internal", "application", "ipv4",
ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
String tgArn = createTargetGroup("sample-tg");
String listenerArn = service.createListener(
REGION, lbArn, "HTTP", 80, null, List.of(HELD_CERTIFICATE),
List.of(forwardAction(tgArn)), List.of(), Map.of()).getListenerArn();
clearInvocations(dataPlane);
service.certificateResolver = deletedOnTheSecondLook();
AwsException e = assertThrows(AwsException.class, () -> service.modifyListener(
REGION, listenerArn, "HTTPS", null, null, null, null, null));
assertEquals("CertificateNotFound", e.getErrorCode());
assertEquals("HTTP", service.describeListeners(REGION, lbArn, List.of(listenerArn))
.getFirst().getProtocol());
}
/**
* A resolver that finds the certificate once and not again, which is what a
* {@code DeleteServerCertificate} landing between the check outside the guard and the one
* under it looks like from inside the call.
*/
private static CertificateReferenceResolver deletedOnTheSecondLook() {
CertificateReferenceResolver resolver = mock(CertificateReferenceResolver.class);
when(resolver.exists(anyString(), anyString())).thenReturn(true, false);
doAnswer(invocation -> {
invocation.getArgument(1, Runnable.class).run();
return null;
}).when(resolver).runWhileHeld(any(), any());
return resolver;
}
@Test
void setRulePrioritiesIgnoresPrioritiesHeldOnOtherListeners() {
String tgArn = createTargetGroup("sample-tg");