Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Priamo Germosenee274b922995
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<String> certificates,
List<Action> defaultActions, List<String> 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());
-
- 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);
+ List<String> effectiveCertificates =
+ certificates != null ? certificates : listener.getCertificates();
+ requireCertificates(region, effectiveProtocol, effectiveCertificates);
+
+ // 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<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");