Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(elbv2): scope SetRulePriorities conflicts to the rule's listener (#4507)

Abhigyab830ade6cdce
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2Test.java
index b87a5e9457..1286454ca6 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2Test.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElbV2Test.java
@@ -501,6 +501,91 @@ class ElbV2Test {
.isInstanceOf(OperationNotPermittedException.class);
}
+ @Test
+ @Order(36)
+ @DisplayName("SetRulePriorities - priority held on another listener is not a conflict")
+ void setRulePrioritiesIgnoresOtherListeners() {
+ String otherLbArn = elb.createLoadBalancer(CreateLoadBalancerRequest.builder()
+ .name(TestFixtures.uniqueName("sdk-lb2"))
+ .type(LoadBalancerTypeEnum.APPLICATION)
+ .scheme(LoadBalancerSchemeEnum.INTERNAL)
+ .ipAddressType(IpAddressType.IPV4)
+ .build()).loadBalancers().get(0).loadBalancerArn();
+ String otherListenerArn = null;
+ String otherRuleArn = null;
+ try {
+ otherListenerArn = elb.createListener(CreateListenerRequest.builder()
+ .loadBalancerArn(otherLbArn)
+ .protocol(ProtocolEnum.HTTP)
+ .port(81)
+ .defaultActions(Action.builder()
+ .type(ActionTypeEnum.FORWARD)
+ .targetGroupArn(tgArn)
+ .build())
+ .build()).listeners().get(0).listenerArn();
+ otherRuleArn = elb.createRule(CreateRuleRequest.builder()
+ .listenerArn(otherListenerArn)
+ .priority(100)
+ .conditions(RuleCondition.builder()
+ .field("path-pattern")
+ .values("/signup/*")
+ .build())
+ .actions(Action.builder()
+ .type(ActionTypeEnum.FORWARD)
+ .targetGroupArn(tgArn)
+ .build())
+ .build()).rules().get(0).ruleArn();
+
+ elb.setRulePriorities(SetRulePrioritiesRequest.builder()
+ .rulePriorities(RulePriorityPair.builder()
+ .ruleArn(ruleArn)
+ .priority(100)
+ .build())
+ .build());
+
+ DescribeRulesResponse resp = elb.describeRules(
+ DescribeRulesRequest.builder().ruleArns(ruleArn, otherRuleArn).build());
+ assertThat(resp.rules()).extracting(Rule::priority).containsOnly("100");
+ } finally {
+ if (otherRuleArn != null) {
+ elb.deleteRule(DeleteRuleRequest.builder().ruleArn(otherRuleArn).build());
+ }
+ if (otherListenerArn != null) {
+ elb.deleteListener(DeleteListenerRequest.builder().listenerArn(otherListenerArn).build());
+ }
+ elb.deleteLoadBalancer(DeleteLoadBalancerRequest.builder().loadBalancerArn(otherLbArn).build());
+ }
+ }
+
+ @Test
+ @Order(37)
+ @DisplayName("SetRulePriorities - priority held on the same listener throws PriorityInUseException")
+ void setRulePrioritiesSameListenerConflict() {
+ String conflictingRuleArn = elb.createRule(CreateRuleRequest.builder()
+ .listenerArn(listenerArn)
+ .priority(30)
+ .conditions(RuleCondition.builder()
+ .field("path-pattern")
+ .values("/conflict/*")
+ .build())
+ .actions(Action.builder()
+ .type(ActionTypeEnum.FORWARD)
+ .targetGroupArn(tgArn)
+ .build())
+ .build()).rules().get(0).ruleArn();
+ try {
+ assertThatThrownBy(() -> elb.setRulePriorities(SetRulePrioritiesRequest.builder()
+ .rulePriorities(RulePriorityPair.builder()
+ .ruleArn(ruleArn)
+ .priority(30)
+ .build())
+ .build()))
+ .isInstanceOf(PriorityInUseException.class);
+ } finally {
+ elb.deleteRule(DeleteRuleRequest.builder().ruleArn(conflictingRuleArn).build());
+ }
+ }
+
// ─── Tags ────────────────────────────────────────────────────────────────
@Test
diff --git a/docs/services/elb.md b/docs/services/elb.md
index 4f541352c0..e9334660e6 100644
--- a/docs/services/elb.md
+++ b/docs/services/elb.md
@@ -91,7 +91,7 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB)
- Target health starts in `initial` state with reason `Elb.RegistrationInProgress` and is updated by Floci's health checker when monitoring is active.
- Health checks probe the target group's `HealthCheckPort` when it is a port number, and each target's own port when it is `traffic-port` (the default).
- 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 validated for uniqueness. `SetRulePriorities` is atomic: all priority assignments are validated before any change is committed.
+- 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.
- `DeleteRule` is rejected with `OperationNotPermitted` for the default rule.
- `DescribeSSLPolicies` returns a pre-seeded list of standard AWS SSL policies (`ELBSecurityPolicy-*`).
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 9e1a0ddadf..14796884a2 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
@@ -752,18 +752,28 @@ public class ElbV2Service implements ResourceProvider {
}
}
- // check for collisions with rules NOT in the update set
- Set<String> updatingArns = arnToPriority.keySet();
- Set<Integer> newPriorities = new HashSet<>(arnToPriority.values());
+ // priorities are unique per listener, so collisions are only checked among rules of the same listener
+ Map<String, Set<Integer>> newPrioritiesByListener = new HashMap<>();
+ for (Map.Entry<String, Integer> e : arnToPriority.entrySet()) {
+ String listenerArn = regionRules.get(e.getKey()).getListenerArn();
+ if (!newPrioritiesByListener.computeIfAbsent(listenerArn, k -> new HashSet<>()).add(e.getValue())) {
+ throw new AwsException("PriorityInUse",
+ "Priority " + e.getValue() + " is already in use.", 400);
+ }
+ }
+ // iterate the concurrent region map, not the listener index lists, which CreateRule/DeleteRule mutate
for (Rule existing : regionRules.values()) {
- if (!updatingArns.contains(existing.getRuleArn()) && !existing.isDefault()) {
- try {
- int existingPriority = Integer.parseInt(existing.getPriority());
- if (newPriorities.contains(existingPriority)) {
- throw new AwsException("PriorityInUse",
- "Priority " + existingPriority + " is already in use.", 400);
- }
- } catch (NumberFormatException ignored) { /* default rule */ }
+ if (existing.isDefault() || arnToPriority.containsKey(existing.getRuleArn())) {
+ continue;
+ }
+ Set<Integer> newPriorities = newPrioritiesByListener.get(existing.getListenerArn());
+ if (newPriorities == null) {
+ continue;
+ }
+ int existingPriority = Integer.parseInt(existing.getPriority());
+ if (newPriorities.contains(existingPriority)) {
+ throw new AwsException("PriorityInUse",
+ "Priority " + existingPriority + " is already in use.", 400);
}
}
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 2041783118..0f469fee28 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
@@ -125,6 +125,87 @@ class ElbV2ServiceTest {
verify(dataPlane, never()).startListener(any(Listener.class), anyString(), anyList());
}
+ @Test
+ void setRulePrioritiesIgnoresPrioritiesHeldOnOtherListeners() {
+ String tgArn = createTargetGroup("sample-tg");
+ String publicListener = createListener("public-lb", tgArn);
+ String internalListener = createListener("internal-lb", tgArn);
+ service.createRule(REGION, publicListener, List.of(pathPattern("/signup/*")),
+ 100, List.of(forwardAction(tgArn)), Map.of());
+ Rule moved = service.createRule(REGION, internalListener, List.of(pathPattern("/documents/*")),
+ 230, List.of(forwardAction(tgArn)), Map.of());
+
+ service.setRulePriorities(REGION, Map.of(moved.getRuleArn(), 100));
+
+ assertEquals("100", service.describeRules(REGION, null, List.of(moved.getRuleArn())).getFirst().getPriority());
+ }
+
+ @Test
+ void setRulePrioritiesChecksEachListenerOnlyAgainstItsOwnRules() {
+ String tgArn = createTargetGroup("sample-tg");
+ String firstListener = createListener("first-lb", tgArn);
+ String secondListener = createListener("second-lb", tgArn);
+ Rule first = service.createRule(REGION, firstListener, List.of(pathPattern("/a/*")),
+ 10, List.of(forwardAction(tgArn)), Map.of());
+ service.createRule(REGION, secondListener, List.of(pathPattern("/b/*")),
+ 100, List.of(forwardAction(tgArn)), Map.of());
+ Rule second = service.createRule(REGION, secondListener, List.of(pathPattern("/c/*")),
+ 20, List.of(forwardAction(tgArn)), Map.of());
+
+ service.setRulePriorities(REGION, Map.of(first.getRuleArn(), 100, second.getRuleArn(), 200));
+
+ assertEquals("100", service.describeRules(REGION, null, List.of(first.getRuleArn())).getFirst().getPriority());
+ assertEquals("200", service.describeRules(REGION, null, List.of(second.getRuleArn())).getFirst().getPriority());
+ }
+
+ @Test
+ void setRulePrioritiesRejectsPriorityHeldOnSameListener() {
+ String tgArn = createTargetGroup("sample-tg");
+ String listenerArn = createListener("sample-lb", tgArn);
+ service.createRule(REGION, listenerArn, List.of(pathPattern("/a/*")),
+ 100, List.of(forwardAction(tgArn)), Map.of());
+ Rule moved = service.createRule(REGION, listenerArn, List.of(pathPattern("/b/*")),
+ 230, List.of(forwardAction(tgArn)), Map.of());
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> service.setRulePriorities(REGION, Map.of(moved.getRuleArn(), 100)));
+
+ assertEquals("PriorityInUse", e.getErrorCode());
+ assertEquals("230", service.describeRules(REGION, null, List.of(moved.getRuleArn())).getFirst().getPriority());
+ }
+
+ @Test
+ void setRulePrioritiesRejectsDuplicatePrioritiesForSameListener() {
+ String tgArn = createTargetGroup("sample-tg");
+ String listenerArn = createListener("sample-lb", tgArn);
+ Rule first = service.createRule(REGION, listenerArn, List.of(pathPattern("/a/*")),
+ 10, List.of(forwardAction(tgArn)), Map.of());
+ Rule second = service.createRule(REGION, listenerArn, List.of(pathPattern("/b/*")),
+ 20, List.of(forwardAction(tgArn)), Map.of());
+
+ AwsException e = assertThrows(AwsException.class,
+ () -> service.setRulePriorities(REGION, Map.of(first.getRuleArn(), 50, second.getRuleArn(), 50)));
+
+ assertEquals("PriorityInUse", e.getErrorCode());
+ assertEquals("10", service.describeRules(REGION, null, List.of(first.getRuleArn())).getFirst().getPriority());
+ assertEquals("20", service.describeRules(REGION, null, List.of(second.getRuleArn())).getFirst().getPriority());
+ }
+
+ @Test
+ void setRulePrioritiesAllowsSwappingPrioritiesOnSameListener() {
+ String tgArn = createTargetGroup("sample-tg");
+ String listenerArn = createListener("sample-lb", tgArn);
+ Rule first = service.createRule(REGION, listenerArn, List.of(pathPattern("/a/*")),
+ 10, List.of(forwardAction(tgArn)), Map.of());
+ Rule second = service.createRule(REGION, listenerArn, List.of(pathPattern("/b/*")),
+ 20, List.of(forwardAction(tgArn)), Map.of());
+
+ service.setRulePriorities(REGION, Map.of(first.getRuleArn(), 20, second.getRuleArn(), 10));
+
+ assertEquals("20", service.describeRules(REGION, null, List.of(first.getRuleArn())).getFirst().getPriority());
+ assertEquals("10", service.describeRules(REGION, null, List.of(second.getRuleArn())).getFirst().getPriority());
+ }
+
@Test
void createLoadBalancerUsesConfiguredHostnameForDnsSuffix() {
EmulatorConfig config = mock(EmulatorConfig.class);
@@ -321,6 +402,15 @@ class ElbV2ServiceTest {
"ipv4", Map.of()).getTargetGroupArn();
}
+ private String createListener(String loadBalancerName, String targetGroupArn) {
+ String lbArn = service.createLoadBalancer(
+ REGION, loadBalancerName, "internal", "application", "ipv4",
+ ALB_SUBNETS, List.of("sg-a"), Map.of()).getLoadBalancerArn();
+ return service.createListener(
+ REGION, lbArn, "HTTP", 80, null, List.of(),
+ List.of(forwardAction(targetGroupArn)), List.of(), Map.of()).getListenerArn();
+ }
+
private static Action forwardAction(String targetGroupArn) {
Action action = new Action();
action.setType("forward");