Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudformation): reject non-integer target group ports and health check counts (#4970)
Hector Venturaa95f4cee3de6
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.javaindex dd2c4c9662..e34341c44a 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisioner.java@@ -163,17 +163,17 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {ctx, r.getLogicalId());String protocol = ctx.resolveOptional(props, "Protocol");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 targetType = ctx.resolveOptional(props, "TargetType");String hcProtocol = ctx.resolveOptional(props, "HealthCheckProtocol");String hcPort = ctx.resolveOptional(props, "HealthCheckPort");Boolean hcEnabled = parseBooleanOrNull(ctx.resolveOptional(props, "HealthCheckEnabled"));String hcPath = ctx.resolveOptional(props, "HealthCheckPath");- Integer hcInterval = parseIntOrNull(ctx.resolveOptional(props, "HealthCheckIntervalSeconds"));- Integer hcTimeout = parseIntOrNull(ctx.resolveOptional(props, "HealthCheckTimeoutSeconds"));- Integer healthyThreshold = parseIntOrNull(ctx.resolveOptional(props, "HealthyThresholdCount"));- Integer unhealthyThreshold = parseIntOrNull(ctx.resolveOptional(props, "UnhealthyThresholdCount"));+ Integer hcInterval = optionalInt(props, ctx, "HealthCheckIntervalSeconds");+ Integer hcTimeout = optionalInt(props, ctx, "HealthCheckTimeoutSeconds");+ Integer healthyThreshold = optionalInt(props, ctx, "HealthyThresholdCount");+ Integer unhealthyThreshold = optionalInt(props, ctx, "UnhealthyThresholdCount");String matcher = parseMatcher(props, ctx);String ipAddressType = ctx.resolveOptional(props, "IpAddressType");Map<String, String> tags = ctx.resolveTags(props, "Tags");@@ -471,23 +471,34 @@ public class ElbV2CfnProvisioner implements CfnResourceProvisioner {try {return Integer.parseInt(value.trim());} 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) {- if (value == null || value.isBlank()) {+ /**+ * An omitted property stays unset for the service to default, and so does one an intrinsic resolves+ * 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;}try {- return Integer.valueOf(value.trim());+ return Integer.parseInt(value.trim());} catch (NumberFormatException e) {- LOG.debugv("Ignoring non-integer value {0}", value);- return null;+ throw notAnInteger(property);}}+ 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) {return value == null || value.isBlank() ? null : Boolean.valueOf(value);}diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisionerTest.javaindex 4fe3e5e177..a38d16c372 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisionerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ElbV2CfnProvisionerTest.java@@ -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());+ }+@Testvoid deletesCallTheServiceAndPropagateItsRefusals() {provisioner.delete("AWS::ElasticLoadBalancingV2::LoadBalancer", LB_ARN, REGION);