fix(elbv2): probe the target group's HealthCheckPort instead of always the traffic port (#4432)

* fix(elbv2): probe the target group's HealthCheckPort instead of always the traffic port

The health checker always probed each target on its traffic port, so a target
group whose HealthCheckPort names a separate port (an app serving traffic on
one port and its health endpoint on another) was marked unhealthy even though
its health endpoint answered. AWS probes HealthCheckPort when it is a port
number and the target's own port when it is traffic-port, the default.
Health state stays keyed by the traffic port either way.

* fix(elbv2): reject a HealthCheckPort that is not traffic-port or a port from 1 to 65535
This commit is contained in:
Clement authored and GitHub committed 2026-09-26 16:44:04 -05:00
1 parent b4d8483348
commit 16a6c24e15
5 files changed
+115 -1

No files matched your search

@@ -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();
@@ -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() {