diff --git a/docs/services/elb.md b/docs/services/elb.md index c04243bd92..4f541352c0 100644 --- a/docs/services/elb.md +++ b/docs/services/elb.md @@ -89,6 +89,7 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB) - HTTP listener sockets are preserved when listener actions change and are restarted only when socket-level settings such as port change. - Instance targets are resolved through EC2 instance private addresses so local load balancer traffic can reach containers. - 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. - `DeleteTargetGroup` is rejected with `ResourceInUse` while the target group is referenced by any listener or rule. diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthChecker.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthChecker.java index 348981762f..d40db1d3a6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthChecker.java +++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthChecker.java @@ -151,9 +151,10 @@ public class ElbV2HealthChecker implements Resettable { int timeout = tg.getHealthCheckTimeoutSeconds() != null ? tg.getHealthCheckTimeoutSeconds() : 5; int healthyThreshold = tg.getHealthyThresholdCount() != null ? tg.getHealthyThresholdCount() : 5; int unhealthyThreshold = tg.getUnhealthyThresholdCount() != null ? tg.getUnhealthyThresholdCount() : 2; + int probePort = healthCheckPort(tg, port); vertx.executeBlocking(() -> { - return probe(host, port, path, timeout); + return probe(host, probePort, path, timeout); }).onSuccess(statusCode -> { boolean success = matchesStatusCode(statusCode, matcher); if (success) { @@ -209,6 +210,37 @@ public class ElbV2HealthChecker implements Resettable { } } + /** + * AWS probes {@code HealthCheckPort} when it is a number and the target's own port when it is + * {@code traffic-port} or unset. Health state stays keyed by the traffic port either way. + * A stored value that is not a valid port, which the API accepted before it validated the + * field, keeps probing the traffic port as before. + */ + static int healthCheckPort(TargetGroup tg, int trafficPort) { + String configured = tg.getHealthCheckPort(); + if (configured == null || configured.isBlank() || "traffic-port".equals(configured)) { + return trafficPort; + } + Integer port = parsePort(configured); + if (port == null) { + LOG.debugv("Target group {0} has invalid HealthCheckPort {1}; probing traffic port {2}", + tg.getTargetGroupArn(), configured, trafficPort); + return trafficPort; + } + return port; + } + + /** Returns the port number {@code value} names, or null when it is not a port from 1 to 65535. */ + static Integer parsePort(String value) { + try { + int port = Integer.parseInt(value.trim()); + return port >= 1 && port <= 65535 ? port : null; + } catch (NumberFormatException ignored) { + // Not a number: the caller treats it the same as an out-of-range port. + return null; + } + } + static boolean matchesStatusCode(int code, String matcher) { String codeStr = String.valueOf(code); if (matcher.contains("-")) { 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 3e5fa55ab2..9e1a0ddadf 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 @@ -357,6 +357,7 @@ public class ElbV2Service implements ResourceProvider { String matcher, String ipAddressType, Map initialTags) { validateName(name, "target group"); + validateHealthCheckPort(healthCheckPort); Map regionTgs = targetGroups.computeIfAbsent(region, k -> new ConcurrentHashMap<>()); boolean duplicate = regionTgs.values().stream() .anyMatch(tg -> tg.getTargetGroupName().equals(name)); @@ -450,6 +451,7 @@ public class ElbV2Service implements ResourceProvider { Integer healthCheckTimeout, Integer healthyThreshold, Integer unhealthyThreshold, String matcher) { TargetGroup tg = requireTargetGroup(region, arn); + validateHealthCheckPort(healthCheckPort); if (healthCheckProtocol != null) tg.setHealthCheckProtocol(healthCheckProtocol); if (healthCheckPort != null) tg.setHealthCheckPort(healthCheckPort); if (healthCheckEnabled != null) tg.setHealthCheckEnabled(healthCheckEnabled); @@ -1056,6 +1058,17 @@ public class ElbV2Service implements ResourceProvider { .collect(Collectors.toList()); } + /** {@code HealthCheckPort} is either {@code traffic-port} or a port number from 1 to 65535. */ + private static void validateHealthCheckPort(String healthCheckPort) { + if (healthCheckPort == null || "traffic-port".equals(healthCheckPort)) { + return; + } + if (ElbV2HealthChecker.parsePort(healthCheckPort) == null) { + throw new AwsException("ValidationError", + "Health check port '" + healthCheckPort + "' must be 'traffic-port' or a port number from 1 to 65535.", 400); + } + } + private static void validateName(String name, String resource) { if (name == null || name.isEmpty()) { throw new AwsException("ValidationError", "Name is required for " + resource + ".", 400); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthCheckerTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthCheckerTest.java index 8d1684dafd..bc3a3abd50 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthCheckerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2HealthCheckerTest.java @@ -157,6 +157,42 @@ class ElbV2HealthCheckerTest { } } + @Test + void numericHealthCheckPortIsProbedInsteadOfTrafficPort() throws Exception { + HttpServer traffic = startServer(404); + HttpServer health = startServer(200); + try { + ElbV2HealthChecker checker = healthChecker(); + TargetGroup targetGroup = targetGroup(traffic.actualPort(), "200", 1, 1); + targetGroup.setHealthCheckPort(String.valueOf(health.actualPort())); + TargetDescription target = target("127.0.0.1", traffic.actualPort()); + + checker.addTargets(targetGroup.getTargetGroupArn(), List.of(target), targetGroup); + + await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> assertEquals( + "healthy", + checker.getHealth(targetGroup.getTargetGroupArn(), target.getId(), traffic.actualPort()).state())); + } finally { + close(traffic); + close(health); + } + } + + @Test + void trafficPortKeywordOrInvalidPortProbesTargetPort() { + TargetGroup targetGroup = new TargetGroup(); + targetGroup.setHealthCheckPort("traffic-port"); + assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051)); + targetGroup.setHealthCheckPort(null); + assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051)); + targetGroup.setHealthCheckPort("8080"); + assertEquals(8080, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051)); + targetGroup.setHealthCheckPort("70000"); + assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051)); + targetGroup.setHealthCheckPort("not-a-port"); + assertEquals(50051, ElbV2HealthChecker.healthCheckPort(targetGroup, 50051)); + } + @Test void registrationStateCarriesAwsReasonUntilFirstProbeCompletes() { ElbV2HealthChecker checker = healthChecker(); diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java index 0d690344f8..2d569d9940 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2IntegrationTest.java @@ -599,6 +599,38 @@ class ElbV2IntegrationTest { .body("ErrorResponse.Error.Code", equalTo("DuplicateTargetGroupName")); } + @Test + @Order(22) + void createTargetGroupRejectsOutOfRangeHealthCheckPort() { + given() + .formParam("Action", "CreateTargetGroup") + .formParam("Name", "bad-hc-port-tg") + .formParam("Protocol", "HTTP") + .formParam("Port", "80") + .formParam("HealthCheckPort", "70000") + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")); + } + + @Test + @Order(22) + void modifyTargetGroupRejectsNonNumericHealthCheckPort() { + given() + .formParam("Action", "ModifyTargetGroup") + .formParam("TargetGroupArn", tgArn) + .formParam("HealthCheckPort", "not-a-port") + .header("Authorization", AUTH) + .when() + .post("/") + .then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")); + } + @Test @Order(23) void modifyTargetGroupAttributes() {