mirror of
https://github.com/floci-io/floci.git
synced 2026-11-25 14:57:54 -05:00
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:
1 parent
be2998f2a2
commit
a95f4cee3d
2 files changed
+80
-12
No files matched your search
+23
-12
@@ -163,17 +163,17 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
ctx, r.getLogicalId());
|
ctx, r.getLogicalId());
|
||||||
String protocol = ctx.resolveOptional(props, "Protocol");
|
String protocol = ctx.resolveOptional(props, "Protocol");
|
||||||
String protocolVersion = ctx.resolveOptional(props, "ProtocolVersion");
|
String protocolVersion = ctx.resolveOptional(props, "ProtocolVersion");
|
||||||
Integer port = parseIntOrNull(ctx.resolveOptional(props, "Port"));
|
Integer port = optionalInt(props, ctx, "Port");
|
||||||
String vpcId = ctx.resolveOptional(props, "VpcId");
|
String vpcId = ctx.resolveOptional(props, "VpcId");
|
||||||
String targetType = ctx.resolveOptional(props, "TargetType");
|
String targetType = ctx.resolveOptional(props, "TargetType");
|
||||||
String hcProtocol = ctx.resolveOptional(props, "HealthCheckProtocol");
|
String hcProtocol = ctx.resolveOptional(props, "HealthCheckProtocol");
|
||||||
String hcPort = ctx.resolveOptional(props, "HealthCheckPort");
|
String hcPort = ctx.resolveOptional(props, "HealthCheckPort");
|
||||||
Boolean hcEnabled = parseBooleanOrNull(ctx.resolveOptional(props, "HealthCheckEnabled"));
|
Boolean hcEnabled = parseBooleanOrNull(ctx.resolveOptional(props, "HealthCheckEnabled"));
|
||||||
String hcPath = ctx.resolveOptional(props, "HealthCheckPath");
|
String hcPath = ctx.resolveOptional(props, "HealthCheckPath");
|
||||||
Integer hcInterval = parseIntOrNull(ctx.resolveOptional(props, "HealthCheckIntervalSeconds"));
|
Integer hcInterval = optionalInt(props, ctx, "HealthCheckIntervalSeconds");
|
||||||
Integer hcTimeout = parseIntOrNull(ctx.resolveOptional(props, "HealthCheckTimeoutSeconds"));
|
Integer hcTimeout = optionalInt(props, ctx, "HealthCheckTimeoutSeconds");
|
||||||
Integer healthyThreshold = parseIntOrNull(ctx.resolveOptional(props, "HealthyThresholdCount"));
|
Integer healthyThreshold = optionalInt(props, ctx, "HealthyThresholdCount");
|
||||||
Integer unhealthyThreshold = parseIntOrNull(ctx.resolveOptional(props, "UnhealthyThresholdCount"));
|
Integer unhealthyThreshold = optionalInt(props, ctx, "UnhealthyThresholdCount");
|
||||||
String matcher = parseMatcher(props, ctx);
|
String matcher = parseMatcher(props, ctx);
|
||||||
String ipAddressType = ctx.resolveOptional(props, "IpAddressType");
|
String ipAddressType = ctx.resolveOptional(props, "IpAddressType");
|
||||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||||
@@ -471,23 +471,34 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
try {
|
try {
|
||||||
return Integer.parseInt(value.trim());
|
return Integer.parseInt(value.trim());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
throw new AwsException("ValidationError", "Value of property " + property + " must be an integer.", 400);
|
throw notAnInteger(property);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The switch treated an unparsable optional integer as unset; kept so a stray value does not fail a stack. */
|
/**
|
||||||
private static Integer parseIntOrNull(String value) {
|
* An omitted property stays unset for the service to default, and so does one an intrinsic resolves
|
||||||
if (value == null || value.isBlank()) {
|
* to nothing: the engine resolves {@code AWS::NoValue} to blank, which is how the common
|
||||||
|
* {@code Fn::If [cond, value, AWS::NoValue]} drops a property. A value written into the template
|
||||||
|
* has to be an integer, a literal blank included, as CloudFormation's schema type check requires.
|
||||||
|
* A {@code Ref} to a parameter whose value is blank cannot be told apart from {@code AWS::NoValue}
|
||||||
|
* here, so it stays unset too.
|
||||||
|
*/
|
||||||
|
private static Integer optionalInt(JsonNode props, ProvisionContext ctx, String property) {
|
||||||
|
String value = ctx.resolveOptional(props, property);
|
||||||
|
if (value == null || (value.isBlank() && !props.get(property).isTextual())) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return Integer.valueOf(value.trim());
|
return Integer.parseInt(value.trim());
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
LOG.debugv("Ignoring non-integer value {0}", value);
|
throw notAnInteger(property);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static AwsException notAnInteger(String property) {
|
||||||
|
return new AwsException("ValidationError", "Value of property " + property + " must be an integer.", 400);
|
||||||
|
}
|
||||||
|
|
||||||
private static Boolean parseBooleanOrNull(String value) {
|
private static Boolean parseBooleanOrNull(String value) {
|
||||||
return value == null || value.isBlank() ? null : Boolean.valueOf(value);
|
return value == null || value.isBlank() ? null : Boolean.valueOf(value);
|
||||||
}
|
}
|
||||||
|
|||||||
+57
@@ -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.RuleCondition;
|
||||||
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
|
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
|
||||||
import org.junit.jupiter.api.Test;
|
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.ArgumentCaptor;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
||||||
|
|
||||||
@@ -602,6 +604,61 @@ class ElbV2CfnProvisionerTest {
|
|||||||
mapper.createObjectNode().put("ListenerArn", LISTENER_ARN).put("Priority", "high"), ctx()));
|
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
|
@Test
|
||||||
void deletesCallTheServiceAndPropagateItsRefusals() {
|
void deletesCallTheServiceAndPropagateItsRefusals() {
|
||||||
provisioner.delete("AWS::ElasticLoadBalancingV2::LoadBalancer", LB_ARN, REGION);
|
provisioner.delete("AWS::ElasticLoadBalancingV2::LoadBalancer", LB_ARN, REGION);
|
||||||
|
|||||||
Reference in new issue
Block a user