fix(elbv2): report the attribute defaults, not only what was modified

DescribeLoadBalancerAttributes and DescribeTargetGroupAttributes answered with
an empty list for a resource nobody had modified: a key appeared only once
something had explicitly set it. AWS always returns the full set, and a key is
absent only when the resource type does not have it.

The Terraform provider reads these on every refresh, into aws_lb's idle_timeout,
enable_http2, enable_deletion_protection and access_logs, and into
aws_lb_target_group's stickiness, deregistration_delay and
load_balancing_algorithm_type, so an empty list left every one of them a
permanent diff on every ALB and NLB.

Which keys apply is decided the way AWS decides it, by load balancer type and
target group protocol rather than one flat list: an NLB has no idle timeout, an
ALB has no proxy protocol, a Lambda target group has neither. Stored values win
over defaults, so a modified key still reports what it was set to.

An existing test pinned the old behaviour by asserting the modified key was the
only one returned; it now asserts that key is among the set, which is what it
was really trying to establish.
This commit is contained in:
Allen Sanborn committed 2026-09-01 14:17:14 -05:00
1 parent 10192f05d5
commit 910e64e726
4 files changed
+307 -3

No files matched your search

@@ -0,0 +1,98 @@
package io.github.hectorvent.floci.services.elbv2;
import io.github.hectorvent.floci.services.elbv2.model.LoadBalancer;
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* The attribute defaults AWS reports for a load balancer or target group that has never been
* modified. DescribeLoadBalancerAttributes and DescribeTargetGroupAttributes always answer with
* the full set: a key is absent only because the resource type does not have it, never because
* nobody set it. That matters beyond tidiness, because the Terraform provider reads these on
* every refresh to populate aws_lb's idle_timeout, enable_http2, enable_deletion_protection and
* access_logs, and aws_lb_target_group's stickiness, deregistration_delay and
* load_balancing_algorithm_type. An empty list leaves each of them a permanent diff.
*
* <p>Which keys apply is decided by the load balancer's type and the target group's protocol,
* the same way AWS decides it: an NLB has no idle timeout, an ALB has no proxy protocol.
*/
final class ElbV2DefaultAttributes {
/** Target group protocols that belong to an Application Load Balancer. */
private static final Set<String> APPLICATION_PROTOCOLS = Set.of("HTTP", "HTTPS");
private ElbV2DefaultAttributes() {}
static Map<String, String> forLoadBalancer(LoadBalancer lb) {
String type = lb.getType() == null ? "application" : lb.getType().toLowerCase();
Map<String, String> defaults = new LinkedHashMap<>();
defaults.put("deletion_protection.enabled", "false");
defaults.put("access_logs.s3.enabled", "false");
defaults.put("access_logs.s3.bucket", "");
defaults.put("access_logs.s3.prefix", "");
// Cross-zone load balancing is on for an ALB and off for the others, which is the one
// default that differs by type rather than the key simply not existing.
defaults.put("load_balancing.cross_zone.enabled", "application".equals(type) ? "true" : "false");
if ("application".equals(type)) {
defaults.put("idle_timeout.timeout_seconds", "60");
defaults.put("client_keep_alive.seconds", "3600");
defaults.put("routing.http2.enabled", "true");
defaults.put("routing.http.desync_mitigation_mode", "defensive");
defaults.put("routing.http.drop_invalid_header_fields.enabled", "false");
defaults.put("routing.http.preserve_host_header.enabled", "false");
defaults.put("routing.http.x_amzn_tls_version_and_cipher_suite.enabled", "false");
defaults.put("routing.http.xff_client_port.enabled", "false");
defaults.put("routing.http.xff_header_processing.mode", "append");
defaults.put("waf.fail_open.enabled", "false");
defaults.put("connection_logs.s3.enabled", "false");
defaults.put("connection_logs.s3.bucket", "");
defaults.put("connection_logs.s3.prefix", "");
} else if ("network".equals(type)) {
defaults.put("dns_record.client_routing_policy", "any_availability_zone");
}
return defaults;
}
static Map<String, String> forTargetGroup(TargetGroup tg) {
Map<String, String> defaults = new LinkedHashMap<>();
if ("lambda".equalsIgnoreCase(tg.getTargetType())) {
// A Lambda target group has one attribute and none of the connection-oriented ones:
// there is no instance to drain, no cookie to pin and no algorithm to choose.
defaults.put("lambda.multi_value_headers.enabled", "false");
return defaults;
}
boolean application = tg.getProtocol() != null
&& APPLICATION_PROTOCOLS.contains(tg.getProtocol().toUpperCase());
defaults.put("deregistration_delay.timeout_seconds", "300");
defaults.put("stickiness.enabled", "false");
defaults.put("load_balancing.cross_zone.enabled", "use_load_balancer_configuration");
if (application) {
defaults.put("stickiness.type", "lb_cookie");
defaults.put("stickiness.lb_cookie.duration_seconds", "86400");
defaults.put("stickiness.app_cookie.duration_seconds", "86400");
defaults.put("stickiness.app_cookie.cookie_name", "");
defaults.put("load_balancing.algorithm.type", "round_robin");
defaults.put("slow_start.duration_seconds", "0");
} else {
defaults.put("stickiness.type", "source_ip");
defaults.put("proxy_protocol_v2.enabled", "false");
}
return defaults;
}
/**
* The stored values win over the defaults, so a key that was modified reports what it was set
* to and every other key still reports the value AWS would.
*/
static Map<String, String> merge(Map<String, String> defaults, Map<String, String> stored) {
Map<String, String> merged = new LinkedHashMap<>(defaults);
merged.putAll(stored);
return merged;
}
}
@@ -277,7 +277,7 @@ public class ElbV2Service implements ResourceProvider {
public Map<String, String> describeLoadBalancerAttributes(String region, String arn) {
LoadBalancer lb = requireLoadBalancer(region, arn);
return new LinkedHashMap<>(lb.getAttributes());
return ElbV2DefaultAttributes.merge(ElbV2DefaultAttributes.forLoadBalancer(lb), lb.getAttributes());
}
public void modifyLoadBalancerAttributes(String region, String arn, Map<String, String> newAttrs) {
@@ -446,7 +446,7 @@ public class ElbV2Service implements ResourceProvider {
public Map<String, String> describeTargetGroupAttributes(String region, String arn) {
TargetGroup tg = requireTargetGroup(region, arn);
return new LinkedHashMap<>(tg.getAttributes());
return ElbV2DefaultAttributes.merge(ElbV2DefaultAttributes.forTargetGroup(tg), tg.getAttributes());
}
public void modifyTargetGroupAttributes(String region, String arn, Map<String, String> newAttrs) {