From ee274b92299552e1775f43693dae25a4b649e4dc Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Mon, 12 Oct 2026 21:51:32 -0400 Subject: [PATCH] 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. --- docs/services/elb.md | 2 +- .../floci/services/elbv2/ElbV2Service.java | 44 +++++++----- .../services/elbv2/ElbV2ServiceTest.java | 67 +++++++++++++++++++ 3 files changed, 97 insertions(+), 16 deletions(-) diff --git a/docs/services/elb.md b/docs/services/elb.md index 60be5cfec4..4ecfc2bf03 100644 --- a/docs/services/elb.md +++ b/docs/services/elb.md @@ -93,7 +93,7 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB) - Each `CreateListener` automatically creates an immutable default rule (`priority=default`, `isDefault=true`). This rule cannot be deleted; use `ModifyListener` to change its action. - Rule priorities are unique per listener: `CreateRule` and `SetRulePriorities` only reject a priority held by another rule on the same listener. `SetRulePriorities` is atomic: all priority assignments are validated before any change is committed. - `DeleteTargetGroup` is rejected with `ResourceInUse` while the target group is referenced by any listener or rule. -- An HTTPS or TLS listener's certificates must name existing certificates: an IAM server certificate ARN or an ACM certificate ARN. `CreateListener`, `ModifyListener` and `AddListenerCertificates` reject a missing or empty one with `CertificateNotFound`, the three operations the API reference models it on, and a `ModifyListener` that turns a listener into an HTTPS or TLS one is held to the same check over the certificates it already carried. An IAM server certificate a listener references cannot be deleted: `DeleteServerCertificate` returns `DeleteConflict` until the certificate is removed from the listener. A write that records one runs while IAM holds its certificate lock, so a certificate cannot be deleted between the check and the write. +- An HTTPS or TLS listener's certificates must name existing certificates: an IAM server certificate ARN or an ACM certificate ARN. `CreateListener`, `ModifyListener` and `AddListenerCertificates` reject a missing or empty one with `CertificateNotFound`, the three operations the API reference models it on, and a `ModifyListener` that turns a listener into an HTTPS or TLS one is held to the same check over the certificates it already carried. An IAM server certificate a listener references cannot be deleted: `DeleteServerCertificate` returns `DeleteConflict` until the certificate is removed from the listener. A write that records one runs while IAM holds its certificate lock, so a certificate cannot be deleted between the check and the write, and a `ModifyListener` refused by that check leaves the listener on the port, protocol, policy and certificates it already had. - `DeleteRule` is rejected with `OperationNotPermitted` for the default rule. - `DescribeSSLPolicies` returns a pre-seeded list of standard AWS SSL policies (`ELBSecurityPolicy-*`). - `DescribeAccountLimits` returns standard default limits (e.g., 50 load balancers per region, 100 target groups, etc.). 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 270813936f..a2d8d2d270 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 @@ -607,7 +607,6 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc String sslPolicy, List certificates, List defaultActions, List alpnPolicy) { Listener listener = requireListener(region, listenerArn); - boolean restartDataPlane = false; boolean recompileRules = false; boolean portChanges = port != null && !Objects.equals(listener.getPort(), port); @@ -632,22 +631,37 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc // protocol needs, and one that changes the protocol alone puts the certificates the // listener already holds into service, where they have to be certificates the account has. String effectiveProtocol = protocol != null ? protocol : listener.getProtocol(); - requireCertificates(region, effectiveProtocol, - certificates != null ? certificates : listener.getCertificates()); + List effectiveCertificates = + certificates != null ? certificates : listener.getCertificates(); + requireCertificates(region, effectiveProtocol, effectiveCertificates); - if (portChanges) { - listener.setPort(port); - restartDataPlane = true; - } - if (protocol != null) listener.setProtocol(protocol); - if (sslPolicy != null) listener.setSslPolicy(sslPolicy); - if (certificates != null) { - whileCertificatesHeld(certificates, () -> { - requireCertificates(region, effectiveProtocol, certificates); + // 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 + // about the moment of the write, so a delete landing between the two has to answer 400 on + // a listener that has not moved rather than one already on its new port. + // + // The guard is taken for the certificates the request records, which are the only ones a + // delete can race: a certificate the listener already holds is one + // 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); + if (portChanges) { + listener.setPort(port); + } + if (protocol != null) { + listener.setProtocol(protocol); + } + if (sslPolicy != null) { + listener.setSslPolicy(sslPolicy); + } + if (certificates != null) { listener.setCertificates(new ArrayList<>(certificates)); - }); + } + }); + if (alpnPolicy != null) { + listener.setAlpnPolicy(new ArrayList<>(alpnPolicy)); } - if (alpnPolicy != null) listener.setAlpnPolicy(new ArrayList<>(alpnPolicy)); if (defaultActions != null) { listener.setDefaultActions(new ArrayList<>(defaultActions)); // update the default rule's actions @@ -663,7 +677,7 @@ public class ElbV2Service implements ResourceProvider, ServerCertificateReferenc } persistRegion(listeners, region); persistRegion(rules, region); - if (restartDataPlane) { + if (portChanges) { dataPlane.restartListener(requireListener(region, listenerArn), region, getListenerRules(region, listenerArn)); } else if (recompileRules) { dataPlane.recompileRules(listenerArn, getListenerRules(region, 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 2f6c3ae105..d89e6b6df6 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 @@ -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 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");