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

@@ -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("-")) {
@@ -357,6 +357,7 @@ public class ElbV2Service implements ResourceProvider {
String matcher, String ipAddressType,
Map<String, String> initialTags) {
validateName(name, "target group");
validateHealthCheckPort(healthCheckPort);
Map<String, TargetGroup> 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);