fix(cloudformation): reject non-integer target group ports and health check counts (#4970)

* fix(cloudformation): reject non-integer target group ports and health check counts

ElbV2CfnProvisioner turned a non-integer Port, HealthCheckIntervalSeconds,
HealthCheckTimeoutSeconds, HealthyThresholdCount or UnhealthyThresholdCount
into an unset value and created the target group anyway. The registry schema
types all five as integers and CloudFormation fails such a resource, so they
now fail with the same ValidationError the listener's Port and the rule's
Priority already raise. An absent value still reaches the service unset.

* fix(cloudformation): reject a literal blank target group integer, keep AWS::NoValue unset

Review on #4970: optionalInt treated a present blank value as absent, so
HealthCheckIntervalSeconds: "" still created the target group with the
service default, while CloudFormation's schema type check rejects a blank
string for an integer property. A literal blank in the template now fails
with the same ValidationError. A blank that an intrinsic resolves to stays
unset, because the engine resolves AWS::NoValue to blank and
Fn::If [cond, value, AWS::NoValue] is how templates drop the property.

The javadoc now says exactly that, including the one case still lenient (a
Ref to a parameter whose value is blank), and optionalInt parses directly
instead of passing parseInt a fallback it could never use. Both integer
checks share one error so the message cannot drift.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-02 20:19:28 -04:00
1 parent be2998f2a2
commit a95f4cee3d
2 files changed
+80 -12

No files matched your search

@@ -15,6 +15,8 @@ import io.github.hectorvent.floci.services.elbv2.model.Rule;
import io.github.hectorvent.floci.services.elbv2.model.RuleCondition;
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
@@ -602,6 +604,61 @@ class ElbV2CfnProvisionerTest {
mapper.createObjectNode().put("ListenerArn", LISTENER_ARN).put("Priority", "high"), ctx()));
}
@ParameterizedTest
@ValueSource(strings = {"Port", "HealthCheckIntervalSeconds", "HealthCheckTimeoutSeconds",
"HealthyThresholdCount", "UnhealthyThresholdCount"})
void aNonIntegerTargetGroupIntegerIsAValidationError(String property) {
assertRejected(property, "ten");
}
@ParameterizedTest
@ValueSource(strings = {"Port", "HealthCheckIntervalSeconds", "HealthCheckTimeoutSeconds",
"HealthyThresholdCount", "UnhealthyThresholdCount"})
void aLiteralBlankTargetGroupIntegerIsAValidationError(String property) {
assertRejected(property, "");
assertRejected(property, " ");
}
private void assertRejected(String property, String value) {
StackResource r = resource("AWS::ElasticLoadBalancingV2::TargetGroup", "Tg");
ObjectNode props = mapper.createObjectNode().put("Name", "web-tg").put(property, value);
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", e.getErrorCode());
assertEquals("Value of property " + property + " must be an integer.", e.getMessage());
assertNull(r.getPhysicalId());
verify(elb, never()).createTargetGroup(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void anIntegerAnIntrinsicResolvesToNothingReachesTheServiceUnset() {
// Fn::If [cond, 30, AWS::NoValue] drops the property; the engine resolves AWS::NoValue to blank.
when(elb.createTargetGroup(eq(REGION), eq("fn-tg"), isNull(), isNull(), isNull(), isNull(), eq("lambda"),
isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
eq(Map.of()))).thenReturn(targetGroup("fn-tg"));
ObjectNode props = mapper.createObjectNode().put("Name", "fn-tg").put("TargetType", "lambda");
props.putObject("HealthCheckIntervalSeconds").put("Ref", "AWS::NoValue");
StackResource r = resource("AWS::ElasticLoadBalancingV2::TargetGroup", "Tg");
provisioner.provision(r, props, ctx());
assertEquals(TG_ARN, r.getPhysicalId());
}
@Test
void absentTargetGroupIntegersReachTheServiceUnset() {
when(elb.createTargetGroup(eq(REGION), eq("fn-tg"), isNull(), isNull(), isNull(), isNull(), eq("lambda"),
isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
eq(Map.of()))).thenReturn(targetGroup("fn-tg"));
StackResource r = resource("AWS::ElasticLoadBalancingV2::TargetGroup", "Tg");
provisioner.provision(r, mapper.createObjectNode().put("Name", "fn-tg").put("TargetType", "lambda"), ctx());
assertEquals(TG_ARN, r.getPhysicalId());
}
@Test
void deletesCallTheServiceAndPropagateItsRefusals() {
provisioner.delete("AWS::ElasticLoadBalancingV2::LoadBalancer", LB_ARN, REGION);