diff --git a/.github/workflows/cfn-schema-drift.yml b/.github/workflows/cfn-schema-drift.yml new file mode 100644 index 0000000000..c60d11c2a2 --- /dev/null +++ b/.github/workflows/cfn-schema-drift.yml @@ -0,0 +1,50 @@ +name: CloudFormation Schema Drift +# Checks the vendored CloudFormation property types against the resource schemas AWS publishes. +# They come from AWS's live schema archive rather than from a pinned dependency, so, as with the +# IAM data in iam-data-drift.yml, this check can fail without anything in the repository having +# changed, and it runs on its own, never as part of a job that ordinary changes trigger. +# +# Two triggers, for two different failures: +# +# - On a pull request that touches the vendored file or its generator, where a hand-edited type +# would enter the repository. +# - Weekly, because the file also goes stale without anyone touching it: AWS changes resource +# schemas, and a property Floci types by an old schema would refuse a template AWS accepts. +# +# The fix for a real failure is `make cfn-property-types-sync` and committing the result. The +# offline shape check is hermetic and runs per pull request in partition-literals.yml instead. +on: + pull_request: + paths: + - 'src/main/resources/aws/cfn-property-types.json' + - 'tools/aws/regen_cfn_property_types.py' + - 'tools/aws/test_regen_cfn_property_types.py' + - '.github/workflows/cfn-schema-drift.yml' + schedule: + # Mondays at 06:10 UTC, beside the IAM data drift check. + - cron: '10 6 * * 1' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + cfn-schema-drift: + name: Vendored CloudFormation property types still match AWS + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Python + uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 + with: + python-version: "3.x" + + # No dependency install: the generator is stdlib-only. + - name: Check the vendored property types still match AWS's schemas + run: make cfn-property-types-verify diff --git a/.github/workflows/partition-literals.yml b/.github/workflows/partition-literals.yml index e91992e8da..16c9059431 100644 --- a/.github/workflows/partition-literals.yml +++ b/.github/workflows/partition-literals.yml @@ -113,6 +113,11 @@ jobs: - name: Check the vendored map of operations to their IAM actions is well-formed run: make iam-authorized-actions-check + # And for the CloudFormation property types, which come from AWS's live resource schema + # archive: shape here, the online comparison in cfn-schema-drift.yml. + - name: Check the vendored CloudFormation property types are well-formed + run: make cfn-property-types-check + # Also offline: the service-linked role table is checked against the vendored managed # policy documents in the repository, never against AWS. The half that comes from a # recorded create cannot be re-derived without the snapshot, which is why every row in diff --git a/Makefile b/Makefile index cff745ceb8..be78aab60d 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ PREFIX ?= $(HOME)/.local iam-namespaces-sync iam-namespaces-check iam-namespaces-verify iam-namespaces-test \ iam-action-resources-sync iam-action-resources-check iam-action-resources-verify \ iam-action-resources-test iam-authorized-actions-sync iam-authorized-actions-check \ + cfn-property-types-sync cfn-property-types-check cfn-property-types-verify cfn-property-types-test \ iam-authorized-actions-verify iam-authorized-actions-test iam-data-verify help: ## List the targets below @@ -379,3 +380,30 @@ iam-authorized-actions-verify: ## CI gate (online): every vendored operation mus iam-authorized-actions-test: ## Run the IAM authorized-action generator's unit tests $(PYTHON) -m pytest tools/aws/test_regen_iam_authorized_actions.py -q + +cfn-property-types-sync: ## Regenerate src/main/resources/aws/cfn-property-types.json from AWS's resource schemas (commit the result) + $(PYTHON) tools/aws/regen_cfn_property_types.py + +cfn-property-types-check: ## CI gate (offline): the vendored CloudFormation property types must be well-formed + @$(PYTHON) tools/aws/regen_cfn_property_types.py --check || { \ + echo ""; \ + echo "error: src/main/resources/aws/cfn-property-types.json is malformed."; \ + echo " Run 'make cfn-property-types-sync' and commit the result."; \ + exit 1; \ + } + +cfn-property-types-verify: ## CI gate (online): the vendored CloudFormation property types must still match AWS's schemas + @$(PYTHON) tools/aws/regen_cfn_property_types.py --verify; status=$$?; \ + if [ $$status -eq 2 ]; then \ + echo ""; \ + echo "error: could not read AWS's CloudFormation resource schemas, so nothing was verified."; \ + exit 1; \ + elif [ $$status -ne 0 ]; then \ + echo ""; \ + echo "error: AWS has changed a resource schema the vendored property types come from."; \ + echo " Run 'make cfn-property-types-sync' and commit the result."; \ + exit 1; \ + fi + +cfn-property-types-test: ## Run the CloudFormation property type generator's unit tests + $(PYTHON) -m pytest tools/aws/test_regen_cfn_property_types.py -q diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationEventsRuleTargetTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationEventsRuleTargetTest.java index c5c00a9e8a..b2b3e73f50 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationEventsRuleTargetTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationEventsRuleTargetTest.java @@ -36,6 +36,7 @@ class CloudFormationEventsRuleTargetTest { private static final String STRING_SUBNETS_STACK = TestFixtures.uniqueName("compat-cfn-rule-subnets"); private static final String FLOAT_STACK = TestFixtures.uniqueName("compat-cfn-rule-float"); private static final String BOOLEAN_STACK = TestFixtures.uniqueName("compat-cfn-rule-boolean"); + private static final String INPUT_STACK = TestFixtures.uniqueName("compat-cfn-rule-input"); private static final String CLUSTER = "{\"Fn::Sub\": \"arn:${AWS::Partition}:ecs:${AWS::Region}:${AWS::AccountId}:cluster/compat-absent\"}"; private static final String ROLE = @@ -56,7 +57,7 @@ class CloudFormationEventsRuleTargetTest { static void cleanup() { if (cfn != null) { // DeleteStack answers for a stack that is gone too, so a failure here is a real one. - for (String stack : List.of(KEPT_STACK, STRING_SUBNETS_STACK, FLOAT_STACK, BOOLEAN_STACK)) { + for (String stack : List.of(KEPT_STACK, STRING_SUBNETS_STACK, FLOAT_STACK, BOOLEAN_STACK, INPUT_STACK)) { cfn.deleteStack(request -> request.stackName(stack)); } cfn.close(); @@ -153,6 +154,20 @@ class CloudFormationEventsRuleTargetTest { + " expected type: Boolean, found: String)")); } + /** Measured on AWS (eu-west-1, 2026-10-19): every target member is typed, Input among them. */ + @Test + @DisplayName("an Input that is an object fails template validation") + void anInputThatIsAnObjectFailsTheStack() throws InterruptedException { + cfn.createStack(request -> request.stackName(INPUT_STACK).templateBody(""" + {"Resources": {"Rule": {"Type": "AWS::Events::Rule", "Properties": { + "EventPattern": {"source": ["compat.rule.targets"]}, + "Targets": [{"Id": "T", "Arn": %s, "RoleArn": %s, "Input": {"a": 1}}]}}}} + """.formatted(CLUSTER, ROLE))); + assertThat(TestFixtures.awaitStackTerminal(cfn, INPUT_STACK, DEPLOY_TIMEOUT)).isEqualTo("ROLLBACK_COMPLETE"); + assertThat(reasons(INPUT_STACK)).anyMatch(reason -> + reason.contains("Property [Input] expected type: String, found: JSONObject")); + } + private static String ecsRule(String parameters, String ecsMembers) { return """ {"Parameters": %s, diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLambdaObjectPropertyTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLambdaObjectPropertyTest.java index d6c30c5f5b..4efa929aaa 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLambdaObjectPropertyTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationLambdaObjectPropertyTest.java @@ -33,6 +33,7 @@ class CloudFormationLambdaObjectPropertyTest { private static final String LAYERS_STACK = TestFixtures.uniqueName("compat-cfn-layers-object"); private static final String DELIMITED_STACK = TestFixtures.uniqueName("compat-cfn-comma-delimited"); private static final String STRING_PARAMETER_STACK = TestFixtures.uniqueName("compat-cfn-string-parameter"); + private static final String NESTED_STACK = TestFixtures.uniqueName("compat-cfn-nested-member"); private static CloudFormationClient cfn; @@ -50,6 +51,7 @@ class CloudFormationLambdaObjectPropertyTest { cfn.deleteStack(request -> request.stackName(LAYERS_STACK)); cfn.deleteStack(request -> request.stackName(DELIMITED_STACK)); cfn.deleteStack(request -> request.stackName(STRING_PARAMETER_STACK)); + cfn.deleteStack(request -> request.stackName(NESTED_STACK)); cfn.close(); } } @@ -144,6 +146,17 @@ class CloudFormationLambdaObjectPropertyTest { reason.contains("Property [Architectures] expected type: JSONArray, found: String")); } + /** Measured on AWS (eu-west-1, 2026-10-19): a member nested in a property is typed too. */ + @Test + @DisplayName("a VpcConfig whose SubnetIds is a string fails the stack") + void aNestedMemberOfTheWrongTypeFailsTheStack() throws InterruptedException { + cfn.createStack(request -> request.stackName(NESTED_STACK).templateBody( + template("\"VpcConfig\": {\"SubnetIds\": \"subnet-0123456789abcdef0\", \"SecurityGroupIds\": []}"))); + assertThat(TestFixtures.awaitStackTerminal(cfn, NESTED_STACK, DEPLOY_TIMEOUT)).isEqualTo("ROLLBACK_COMPLETE"); + assertThat(reasons(NESTED_STACK)).anyMatch(reason -> + reason.contains("Property [SubnetIds] expected type: JSONArray, found: String")); + } + private static List reasons(String stackName) { if (TestFixtures.isRealAws()) { return cfn.describeEvents(request -> request.stackName(stackName)).operationEvents().stream() diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java index 356bd5223e..ea3acfe042 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java @@ -579,6 +579,16 @@ public class CloudFormationTemplateEngine { return resolveListElements(node, true); } + /** + * A list property written as a literal array, as text, a list-valued intrinsic among its elements + * expanded in place and blank entries kept: it reaches the resource as written (measured: + * {@code SecurityGroups ["", "sg-..."]} reaches a rule target as both). See + * {@link #resolveListElements}. + */ + public List resolveLiteralList(JsonNode array) { + return resolveListElements(array); + } + private List resolveListElements(JsonNode node, boolean resolveDynamicReferences) { List out = new ArrayList<>(); for (JsonNode element : node) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AutoScalingGroupCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AutoScalingGroupCfnProvisioner.java index 6ab4161963..22b92b30c9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AutoScalingGroupCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/AutoScalingGroupCfnProvisioner.java @@ -14,7 +14,6 @@ import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; -import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnPropertyTypes.Type; import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -50,11 +49,9 @@ public class AutoScalingGroupCfnProvisioner implements CfnResourceProvisioner { private static final Logger LOG = Logger.getLogger(AutoScalingGroupCfnProvisioner.class); private static final ObjectMapper MAPPER = new ObjectMapper(); - /** The launch configuration's MetadataOptions as its resource schema types them. */ - private static final Type METADATA_OPTIONS = Type.object(Map.of( - "HttpTokens", Type.STRING, - "HttpEndpoint", Type.STRING, - "HttpPutResponseHopLimit", Type.INTEGER)); + /** The shape AWS::AutoScaling::LaunchConfiguration's resource schema declares for MetadataOptions. */ + private static final String METADATA_OPTIONS = + CfnPropertyTypes.propertyShape(LAUNCH_CONFIGURATION, "MetadataOptions"); private final AutoScalingService autoScalingService; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypes.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypes.java index 64e56a5e70..0e960fde49 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypes.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypes.java @@ -9,6 +9,8 @@ import com.fasterxml.jackson.databind.node.JsonNodeType; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RequestShapeModel; +import io.github.hectorvent.floci.core.common.RequestShapeModel.Shape; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; import java.math.BigDecimal; @@ -18,16 +20,19 @@ import java.util.regex.Pattern; /** * CloudFormation's checks of a property value against the type the resource schema declares, with - * the messages they give (measured in eu-west-1, 2026-10-18). + * the messages they give (measured in eu-west-1, 2026-10-18). The types come from the vendored + * {@code aws/cfn-property-types.json}, generated from the published resource schemas by + * {@code tools/aws/regen_cfn_property_types.py}. * *

Template validation runs over the whole template before any resource is touched and names the * property, or the list index, alone: "Property [Layers] expected type: JSONArray, found: String". - * Where an object or a list is expected, a number or a boolean reads as a String. Where text is - * expected, any scalar is taken as its text. Where an integer is expected, a whole number or text + * Where an object, a map or a list is expected, a number or a boolean reads as a String. Where text + * is expected, any scalar is taken as its text. Where an integer is expected, a whole number or text * that spells one is taken, and anything else is named by what it is: Float (4.5 or "4.5"), * Boolean, String, JSONObject, JSONArray. Where a boolean is expected, only an object or a list is * refused there. A list of strings also takes every intrinsic that yields a list, as - * {@link #readStringList} says. + * {@link #readStringList} says. A number, and a value the schema leaves free (a free-form object, + * several types at once), are taken as they are. * *

The resource handler's model validation runs after it, on the values CloudFormation converted, * and names the whole path: a boolean that is not true or false in any case, and an integer beyond @@ -36,46 +41,37 @@ import java.util.regex.Pattern; */ final class CfnPropertyTypes { - /** A type a resource schema declares for a property. */ - enum Kind { - STRING("String"), INTEGER("Integer"), BOOLEAN("Boolean"), OBJECT("JSONObject"), LIST("JSONArray"); - - private final String schemaName; - - Kind(String schemaName) { - this.schemaName = schemaName; - } - } - - /** A property's type: a scalar, an object with the members it declares, or a list of {@code items}. */ - record Type(Kind kind, Map members, Type items) { - static final Type STRING = new Type(Kind.STRING, null, null); - static final Type INTEGER = new Type(Kind.INTEGER, null, null); - static final Type BOOLEAN = new Type(Kind.BOOLEAN, null, null); - - static Type object(Map members) { - return new Type(Kind.OBJECT, members, null); - } - - static Type list(Type items) { - return new Type(Kind.LIST, null, items); - } - } + static final String RESOURCE_NAME = "aws/cfn-property-types.json"; private static final Pattern WHOLE_NUMBER = Pattern.compile("-?\\d+"); private static final String INTEGER_RANGE_HINT = ". This may occur when a numeric value exceeds 32-bit" + " integer max (2147483647). Consider using 'number' type or ensure value fits in integer range."; + private static final class Holder { + static final RequestShapeModel MODEL = RequestShapeModel.load(RESOURCE_NAME, "CloudFormation property types"); + } + private CfnPropertyTypes() { } + /** The shape the resource schema of {@code resourceType} declares for its property {@code property}. */ + static String propertyShape(String resourceType, String property) { + String properties = Holder.MODEL.operations().get(resourceType); + String shape = properties == null ? null : Holder.MODEL.shapes().get(properties).members().get(property); + if (shape == null) { + throw new IllegalStateException(resourceType + "." + property + " is not in " + RESOURCE_NAME + + "; add the type to tools/aws/regen_cfn_property_types.py"); + } + return shape; + } + /** * Refuses a value of another type than {@code expected}, an object or a list, with template * validation's message, where a number or a boolean reads as a String. */ static void requireType(String property, JsonNode value, JsonNodeType expected) { if (value.getNodeType() != expected) { - throw templateMismatch(property, expected == JsonNodeType.OBJECT ? Kind.OBJECT : Kind.LIST, + throw templateMismatch(property, expected == JsonNodeType.OBJECT ? "JSONObject" : "JSONArray", containerName(value)); } } @@ -106,28 +102,35 @@ final class CfnPropertyTypes { } /** - * Reads template values against their declared types and resolves them, converting them as - * CloudFormation does before the handler sees them: an integer or a boolean given as text - * becomes one, and a number or a boolean where text is expected becomes text. Members a type - * does not declare are resolved as they are. What {@code AWS::NoValue} stands for is left out. - * Template validation failures are thrown at once; the first model validation failure waits for - * {@link #finish}, so every value read has passed template validation first, as on AWS. + * Reads template values against the shapes the resource schemas declare and resolves them, + * converting them as CloudFormation does before the handler sees them: an integer or a boolean + * given as text becomes one, and a number or a boolean where text is expected becomes text. + * Members a shape does not declare are resolved as they are. What {@code AWS::NoValue} stands for + * is left out. Template validation failures are thrown at once; the first model validation + * failure waits for {@link #finish}, so every value read has passed template validation first, + * as on AWS. */ static final class Reader { private final CloudFormationTemplateEngine engine; + private final RequestShapeModel model; private AwsException modelFailure; Reader(CloudFormationTemplateEngine engine) { + this(engine, Holder.MODEL); + } + + Reader(CloudFormationTemplateEngine engine, RequestShapeModel model) { this.engine = engine; + this.model = model; } /** - * The template value {@code raw}, at {@code path} in the resource's properties, read as - * {@code type}; null when it is absent or {@code AWS::NoValue}. + * The template value {@code raw}, at {@code path} in the resource's properties, read as the + * shape {@code shapeName}; null when it is absent or {@code AWS::NoValue}. */ - JsonNode read(String path, JsonNode raw, Type type) { - return read(path, path.substring(path.lastIndexOf('/') + 1), engine.omitNoValue(raw), type); + JsonNode read(String path, JsonNode raw, String shapeName) { + return read(path, path.substring(path.lastIndexOf('/') + 1), engine.omitNoValue(raw), shapeName); } /** Throws the first model validation failure found by {@link #read}, if any. */ @@ -137,11 +140,12 @@ final class CfnPropertyTypes { } } - private JsonNode read(String path, String property, JsonNode raw, Type type) { + private JsonNode read(String path, String property, JsonNode raw, String shapeName) { if (raw == null || raw.isNull() || raw.isMissingNode()) { return null; } - if (type.kind() == Kind.LIST && type.items().kind() == Kind.STRING) { + Shape shape = model.shapes().get(shapeName); + if ("list".equals(shape.type()) && "string".equals(model.shapes().get(shape.element()).type())) { return readStringList(property, raw); } JsonNode value = raw.isContainerNode() && !CloudFormationTemplateEngine.callsIntrinsic(raw) @@ -149,22 +153,24 @@ final class CfnPropertyTypes { if (value == null || value.isNull() || value.isMissingNode()) { return null; } - return switch (type.kind()) { - case OBJECT -> readObject(path, property, value, type.members()); - case LIST -> readList(path, property, value, type.items()); - case STRING -> readString(property, value); - case INTEGER -> readInteger(path, property, value); - case BOOLEAN -> readBoolean(path, property, value); + return switch (shape.type()) { + case "structure" -> readObject(path, property, value, shape.members()); + case "map" -> readMap(path, property, value, shape.element()); + case "list" -> readList(path, property, value, shape.element()); + case "string" -> readString(property, value); + case "integer" -> readInteger(path, property, value); + case "boolean" -> readBoolean(path, property, value); + default -> value == raw ? engine.resolveNode(raw) : value; }; } - private JsonNode readObject(String path, String property, JsonNode value, Map members) { + private JsonNode readObject(String path, String property, JsonNode value, Map members) { requireType(property, value, JsonNodeType.OBJECT); ObjectNode out = JsonNodeFactory.instance.objectNode(); for (Map.Entry member : value.properties()) { - Type memberType = members.get(member.getKey()); - JsonNode read = memberType == null ? engine.resolveNode(member.getValue()) - : read(path + "/" + member.getKey(), member.getKey(), member.getValue(), memberType); + String memberShape = members.get(member.getKey()); + JsonNode read = memberShape == null ? engine.resolveNode(member.getValue()) + : read(path + "/" + member.getKey(), member.getKey(), member.getValue(), memberShape); if (read != null) { out.set(member.getKey(), read); } @@ -172,11 +178,23 @@ final class CfnPropertyTypes { return out; } - private JsonNode readList(String path, String property, JsonNode value, Type items) { + private JsonNode readMap(String path, String property, JsonNode value, String valueShape) { + requireType(property, value, JsonNodeType.OBJECT); + ObjectNode out = JsonNodeFactory.instance.objectNode(); + for (Map.Entry entry : value.properties()) { + JsonNode read = read(path + "/" + entry.getKey(), entry.getKey(), entry.getValue(), valueShape); + if (read != null) { + out.set(entry.getKey(), read); + } + } + return out; + } + + private JsonNode readList(String path, String property, JsonNode value, String itemShape) { requireType(property, value, JsonNodeType.ARRAY); ArrayNode out = JsonNodeFactory.instance.arrayNode(); for (int i = 0; i < value.size(); i++) { - JsonNode read = read(path + "/" + i, String.valueOf(i), value.get(i), items); + JsonNode read = read(path + "/" + i, String.valueOf(i), value.get(i), itemShape); if (read != null) { out.add(read); } @@ -184,17 +202,24 @@ final class CfnPropertyTypes { return out; } - /** A literal item that is an object or a list is refused by its index, as for any string. */ + /** + * A literal list keeps its blank entries where the template put them, as CloudFormation + * passes it on, and an item that is an object or a list is refused by its index, as for any + * string; any other value is read as {@link CfnPropertyTypes#readStringList} says. + */ private JsonNode readStringList(String property, JsonNode raw) { + List values; if (raw.isArray()) { for (int i = 0; i < raw.size(); i++) { JsonNode item = raw.get(i); if (item.isContainerNode() && !CloudFormationTemplateEngine.callsIntrinsic(item)) { - throw templateMismatch(String.valueOf(i), Kind.STRING, containerName(item)); + throw templateMismatch(String.valueOf(i), "String", containerName(item)); } } + values = engine.resolveLiteralList(raw); + } else { + values = CfnPropertyTypes.readStringList(property, raw, engine); } - List values = CfnPropertyTypes.readStringList(property, raw, engine); if (values == null) { return null; } @@ -205,7 +230,7 @@ final class CfnPropertyTypes { private static JsonNode readString(String property, JsonNode value) { if (value.isContainerNode()) { - throw templateMismatch(property, Kind.STRING, containerName(value)); + throw templateMismatch(property, "String", containerName(value)); } return value.isTextual() ? value : TextNode.valueOf(value.asText()); } @@ -213,12 +238,12 @@ final class CfnPropertyTypes { private JsonNode readInteger(String path, String property, JsonNode value) { String text = value.isIntegralNumber() ? value.bigIntegerValue().toString() : value.asText(); if (value.isContainerNode() || value.isBoolean() || !WHOLE_NUMBER.matcher(text).matches()) { - throw templateMismatch(property, Kind.INTEGER, scalarName(value)); + throw templateMismatch(property, "Integer", scalarName(value)); } BigDecimal number = new BigDecimal(text); if (number.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0 || number.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) { - recordModelFailure(path, Kind.INTEGER, INTEGER_RANGE_HINT); + recordModelFailure(path, "Integer", INTEGER_RANGE_HINT); return value; } return IntNode.valueOf(number.intValue()); @@ -226,27 +251,27 @@ final class CfnPropertyTypes { private JsonNode readBoolean(String path, String property, JsonNode value) { if (value.isContainerNode()) { - throw templateMismatch(property, Kind.BOOLEAN, containerName(value)); + throw templateMismatch(property, "Boolean", containerName(value)); } String text = value.asText(); if ("true".equalsIgnoreCase(text) || "false".equalsIgnoreCase(text)) { return BooleanNode.valueOf(Boolean.parseBoolean(text)); } - recordModelFailure(path, Kind.BOOLEAN, ""); + recordModelFailure(path, "Boolean", ""); return value; } - private void recordModelFailure(String path, Kind expected, String hint) { + private void recordModelFailure(String path, String expected, String hint) { if (modelFailure == null) { modelFailure = new AwsException("InvalidRequest", "Model validation failed (#/" + path - + ": expected type: " + expected.schemaName + ", found: String" + hint + ")", 400); + + ": expected type: " + expected + ", found: String" + hint + ")", 400); } } } - private static AwsException templateMismatch(String property, Kind expected, String found) { + private static AwsException templateMismatch(String property, String expected, String found) { return new AwsException("ValidationError", "Property [" + property + "] expected type: " - + expected.schemaName + ", found: " + found, 400); + + expected + ", found: " + found, 400); } private static String containerName(JsonNode value) { diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java index f062cee693..8b93c47b70 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisioner.java @@ -9,7 +9,6 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; -import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnPropertyTypes.Type; import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; import io.github.hectorvent.floci.services.eventbridge.model.ApiDestination; import io.github.hectorvent.floci.services.eventbridge.model.BatchParameters; @@ -53,37 +52,8 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner { private static final Logger LOG = Logger.getLogger(EventsCfnProvisioner.class); private static final ObjectMapper MAPPER = new ObjectMapper(); - /** - * The types AWS::Events::Rule's resource schema gives the target members this provisioner reads - * as typed values (describe-type, eu-west-1, 2026-10-18), in the template's spelling. - */ - private static final Type ECS_PARAMETERS = Type.object(Map.ofEntries( - Map.entry("CapacityProviderStrategy", Type.list(Type.object(Map.of( - "CapacityProvider", Type.STRING, "Base", Type.INTEGER, "Weight", Type.INTEGER)))), - Map.entry("EnableECSManagedTags", Type.BOOLEAN), - Map.entry("EnableExecuteCommand", Type.BOOLEAN), - Map.entry("Group", Type.STRING), - Map.entry("LaunchType", Type.STRING), - Map.entry("NetworkConfiguration", Type.object(Map.of("AwsVpcConfiguration", Type.object(Map.of( - "AssignPublicIp", Type.STRING, - "SecurityGroups", Type.list(Type.STRING), - "Subnets", Type.list(Type.STRING)))))), - Map.entry("PlacementConstraints", Type.list(Type.object(Map.of( - "Expression", Type.STRING, "Type", Type.STRING)))), - Map.entry("PlacementStrategies", Type.list(Type.object(Map.of( - "Field", Type.STRING, "Type", Type.STRING)))), - Map.entry("PlatformVersion", Type.STRING), - Map.entry("PropagateTags", Type.STRING), - Map.entry("ReferenceId", Type.STRING), - Map.entry("TagList", Type.list(Type.object(Map.of("Key", Type.STRING, "Value", Type.STRING)))), - Map.entry("TaskCount", Type.INTEGER), - Map.entry("TaskDefinitionArn", Type.STRING))); - private static final Type TARGETS = Type.list(Type.object(Map.of( - "RoleArn", Type.STRING, - "RetryPolicy", Type.object(Map.of( - "MaximumRetryAttempts", Type.INTEGER, "MaximumEventAgeInSeconds", Type.INTEGER)), - "DeadLetterConfig", Type.object(Map.of("Arn", Type.STRING)), - "EcsParameters", ECS_PARAMETERS))); + /** The shape AWS::Events::Rule's resource schema declares for its targets. */ + private static final String TARGETS = CfnPropertyTypes.propertyShape("AWS::Events::Rule", "Targets"); private static final String EVENT_BUS_CREATED_TIME_ATTR = "FlociEventBusCreatedTime"; private static final String EVENT_BUS_MANAGED_TAG_KEYS_ATTR = "FlociEventBusManagedTagKeys"; @@ -210,8 +180,7 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner { * The rule's {@code Targets}, each carrying what PutTargets would set. An Fn::If that chooses the * whole list works, and AWS::NoValue is left out wherever it stands for a member, a list item or * an Fn::If branch, as CloudFormation leaves them out of the PutTargets call, while a literal "" - * stays. The members typed in {@link #TARGETS} are read against the resource schema's types; the - * others are resolved as they come. + * stays. Every member is read against the type the resource schema declares for it. */ private List declaredTargets(JsonNode props, ProvisionContext ctx) { List targets = new ArrayList<>(); diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java index 04525f3603..7ed44da1f4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaCfnProvisioner.java @@ -1,7 +1,6 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.JsonNodeType; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; @@ -161,6 +160,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { private LambdaDesiredState buildLambdaDesiredState(StackResource r, JsonNode props, ProvisionContext ctx, LambdaFunction existing) { CloudFormationTemplateEngine engine = ctx.engine(); + CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(engine); String stackName = ctx.stackName(); String explicitName = ctx.resolveOptional(props, "FunctionName"); boolean hasExplicitName = explicitName != null && !explicitName.isBlank(); @@ -252,22 +252,21 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { LAMBDA_DEFAULT_MEMORY_MB)); configRequest.put("Description", ctx.resolveOptional(props, "Description")); configRequest.put("KMSKeyArn", ctx.resolveOptional(props, "KMSKeyArn")); - configRequest.put("Environment", Map.of("Variables", resolveLambdaEnvironment(props, engine))); - putStringListIfPresent(configRequest, props, "Architectures", "Architectures", engine); - configRequest.put("Layers", resolveStringListOrEmpty(props, "Layers", engine)); - configRequest.put("EphemeralStorage", resolveMapOrDefault(props, "EphemeralStorage", engine, + configRequest.put("Environment", Map.of("Variables", resolveLambdaEnvironment(types, props))); + putStringListIfPresent(configRequest, types, props, "Architectures"); + configRequest.put("Layers", resolveStringListOrEmpty(types, FUNCTION, props, "Layers")); + configRequest.put("EphemeralStorage", resolveMapOrDefault(types, props, "EphemeralStorage", Map.of("Size", LAMBDA_DEFAULT_EPHEMERAL_STORAGE_MB))); - configRequest.put("TracingConfig", resolveMapOrDefault(props, "TracingConfig", engine, + configRequest.put("TracingConfig", resolveMapOrDefault(types, props, "TracingConfig", Map.of("Mode", LAMBDA_DEFAULT_TRACING_MODE))); - configRequest.put("DeadLetterConfig", resolveMapOrDefault(props, "DeadLetterConfig", engine, + configRequest.put("DeadLetterConfig", resolveMapOrDefault(types, props, "DeadLetterConfig", mapWithNullValue("TargetArn"))); - configRequest.put("VpcConfig", resolveMapOrDefault(props, "VpcConfig", engine, Map.of())); + configRequest.put("VpcConfig", resolveMapOrDefault(types, props, "VpcConfig", Map.of())); // Empty when the template has none, so that removing it puts the function back on its // defaults (Text, or JSON for a durable function, and /aws/lambda/), as on AWS. - configRequest.put("LoggingConfig", resolveMapOrDefault(props, "LoggingConfig", engine, Map.of())); - configRequest.put("FileSystemConfigs", - resolveObjectListOrEmpty(props, "FileSystemConfigs", engine)); - putResolvedMapIfPresent(configRequest, props, "ImageConfig", "ImageConfig", engine); + configRequest.put("LoggingConfig", resolveMapOrDefault(types, props, "LoggingConfig", Map.of())); + configRequest.put("FileSystemConfigs", resolveObjectListOrEmpty(types, props, "FileSystemConfigs")); + putResolvedMapIfPresent(configRequest, types, props, "ImageConfig"); if (durableConfig != null) { configRequest.put("DurableConfig", durableConfig); } @@ -293,6 +292,7 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { } } + types.finish(); return new LambdaDesiredState(functionName, hasExplicitName, packageType, durableReplacement, createRequest, code, configRequest, tags, props != null && props.has("ReservedConcurrentExecutions"), reservedConcurrentExecutions); @@ -774,40 +774,31 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { return jsonNodeToValue(value); } - private Map resolveLambdaEnvironment(JsonNode props, CloudFormationTemplateEngine engine) { - JsonNode envNode = resolveTyped(props, "Environment", JsonNodeType.OBJECT, engine); + private Map resolveLambdaEnvironment(CfnPropertyTypes.Reader types, JsonNode props) { + JsonNode envNode = readTyped(types, FUNCTION, props, "Environment"); JsonNode variables = envNode == null ? null : envNode.get("Variables"); - if (variables == null || variables.isNull() || variables.isMissingNode()) { + if (variables == null) { return Map.of(); } - CfnPropertyTypes.requireType("Variables", variables, JsonNodeType.OBJECT); Map vars = new HashMap<>(); variables.fields().forEachRemaining(e -> vars.put(e.getKey(), e.getValue().asText())); return vars; } - private List resolveStringListOrEmpty(JsonNode props, String source, - CloudFormationTemplateEngine engine) { - List values = resolveStringList(props, source, engine); - return values == null ? List.of() : values; + private static List resolveStringListOrEmpty(CfnPropertyTypes.Reader types, String resourceType, + JsonNode props, String source) { + JsonNode values = readTyped(types, resourceType, props, source); + return values == null ? List.of() : texts(values); } - /** - * The list-of-strings property {@code source}, or null when the template leaves it out, - * {@code Ref: AWS::NoValue} directly or through {@code Fn::If} included; see - * {@link CfnPropertyTypes#readStringList}. - */ - private static List resolveStringList(JsonNode props, String source, - CloudFormationTemplateEngine engine) { - if (props == null || !props.has(source) || props.get(source).isNull()) { - return null; - } - return CfnPropertyTypes.readStringList(source, props.get(source), engine); + private static List texts(JsonNode values) { + List out = new ArrayList<>(); + values.forEach(value -> out.add(value.asText())); + return out; } - private List resolveObjectListOrEmpty(JsonNode props, String source, - CloudFormationTemplateEngine engine) { - JsonNode resolved = resolveTyped(props, source, JsonNodeType.ARRAY, engine); + private List resolveObjectListOrEmpty(CfnPropertyTypes.Reader types, JsonNode props, String source) { + JsonNode resolved = readTyped(types, FUNCTION, props, source); if (resolved == null) { return List.of(); } @@ -817,29 +808,24 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { } /** The object property {@code source}, or {@code defaultValue} when the template leaves it out. */ - private Map resolveMapOrDefault(JsonNode props, String source, - CloudFormationTemplateEngine engine, + private Map resolveMapOrDefault(CfnPropertyTypes.Reader types, JsonNode props, String source, Map defaultValue) { - JsonNode resolved = resolveTyped(props, source, JsonNodeType.OBJECT, engine); + JsonNode resolved = readTyped(types, FUNCTION, props, source); return resolved == null ? defaultValue : jsonObjectToMap(resolved); } /** - * The property {@code source}, or null when the template leaves it out, {@code Ref: AWS::NoValue} - * directly or through {@code Fn::If} included. A value of another type than {@code expected} is - * refused before the function is touched; see {@link CfnPropertyTypes#requireType}. + * The property {@code source} of {@code resourceType} read against the type its resource schema + * declares, or null when the template leaves it out, {@code Ref: AWS::NoValue} directly or + * through {@code Fn::If} included. A value of another type is refused before the function is + * touched; see {@link CfnPropertyTypes}. */ - private static JsonNode resolveTyped(JsonNode props, String source, JsonNodeType expected, - CloudFormationTemplateEngine engine) { - if (props == null || !props.has(source) || props.get(source).isNull()) { + private static JsonNode readTyped(CfnPropertyTypes.Reader types, String resourceType, JsonNode props, + String source) { + if (props == null || !props.has(source)) { return null; } - JsonNode resolved = engine.resolveNodeOmittingNoValue(props.get(source)); - if (resolved == null || resolved.isNull() || resolved.isMissingNode()) { - return null; - } - CfnPropertyTypes.requireType(source, resolved, expected); - return resolved; + return types.read(source, props.get(source), CfnPropertyTypes.propertyShape(resourceType, source)); } private static Map mapWithNullValue(String key) { @@ -848,19 +834,19 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { return map; } - private void putStringListIfPresent(Map request, JsonNode props, String source, - String target, CloudFormationTemplateEngine engine) { - List values = resolveStringList(props, source, engine); + private void putStringListIfPresent(Map request, CfnPropertyTypes.Reader types, + JsonNode props, String source) { + JsonNode values = readTyped(types, FUNCTION, props, source); if (values != null) { - request.put(target, values); + request.put(source, texts(values)); } } - private void putResolvedMapIfPresent(Map request, JsonNode props, String source, - String target, CloudFormationTemplateEngine engine) { - JsonNode resolved = resolveTyped(props, source, JsonNodeType.OBJECT, engine); + private void putResolvedMapIfPresent(Map request, CfnPropertyTypes.Reader types, + JsonNode props, String source) { + JsonNode resolved = readTyped(types, FUNCTION, props, source); if (resolved != null) { - request.put(target, jsonObjectToMap(resolved)); + request.put(source, jsonObjectToMap(resolved)); } } @@ -916,11 +902,13 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner { if (licenseInfo != null) { request.put("LicenseInfo", licenseInfo); } - List runtimes = resolveStringListOrEmpty(props, "CompatibleRuntimes", engine); + CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(engine); + List runtimes = resolveStringListOrEmpty(types, LAYER_VERSION, props, "CompatibleRuntimes"); if (!runtimes.isEmpty()) { request.put("CompatibleRuntimes", runtimes); } - List architectures = resolveStringListOrEmpty(props, "CompatibleArchitectures", engine); + List architectures = resolveStringListOrEmpty(types, LAYER_VERSION, props, "CompatibleArchitectures"); + types.finish(); if (!architectures.isEmpty()) { request.put("CompatibleArchitectures", architectures); } diff --git a/src/main/resources/aws/cfn-property-types.json b/src/main/resources/aws/cfn-property-types.json new file mode 100644 index 0000000000..4ecea6c7c5 --- /dev/null +++ b/src/main/resources/aws/cfn-property-types.json @@ -0,0 +1,608 @@ +{ + "_source": { + "generator": "tools/aws/regen_cfn_property_types.py", + "schemas": "https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip", + "retrieved": "2026-10-19", + "note": "make cfn-property-types-verify checks the file against AWS." + }, + "operations": { + "AWS::AutoScaling::LaunchConfiguration": "AWS::AutoScaling::LaunchConfiguration", + "AWS::Events::Rule": "AWS::Events::Rule", + "AWS::Lambda::Function": "AWS::Lambda::Function", + "AWS::Lambda::LayerVersion": "AWS::Lambda::LayerVersion" + }, + "shapes": { + "AWS::AutoScaling::LaunchConfiguration": { + "type": "structure", + "members": { + "PlacementTenancy": "String", + "SecurityGroups": "AWS::AutoScaling::LaunchConfiguration.SecurityGroups", + "LaunchConfigurationName": "String", + "MetadataOptions": "AWS::AutoScaling::LaunchConfiguration/MetadataOptions", + "InstanceId": "String", + "UserData": "String", + "ClassicLinkVPCSecurityGroups": "AWS::AutoScaling::LaunchConfiguration.ClassicLinkVPCSecurityGroups", + "BlockDeviceMappings": "AWS::AutoScaling::LaunchConfiguration.BlockDeviceMappings", + "IamInstanceProfile": "String", + "KernelId": "String", + "AssociatePublicIpAddress": "Boolean", + "ClassicLinkVPCId": "String", + "EbsOptimized": "Boolean", + "KeyName": "String", + "SpotPrice": "String", + "ImageId": "String", + "InstanceType": "String", + "RamDiskId": "String", + "InstanceMonitoring": "Boolean" + } + }, + "AWS::AutoScaling::LaunchConfiguration.BlockDeviceMappings": { + "type": "list", + "member": "AWS::AutoScaling::LaunchConfiguration/BlockDeviceMapping" + }, + "AWS::AutoScaling::LaunchConfiguration.ClassicLinkVPCSecurityGroups": { + "type": "list", + "member": "String" + }, + "AWS::AutoScaling::LaunchConfiguration.SecurityGroups": { + "type": "list", + "member": "String" + }, + "AWS::AutoScaling::LaunchConfiguration/BlockDevice": { + "type": "structure", + "members": { + "SnapshotId": "String", + "VolumeType": "String", + "Encrypted": "Boolean", + "Throughput": "Integer", + "Iops": "Integer", + "VolumeSize": "Integer", + "DeleteOnTermination": "Boolean" + } + }, + "AWS::AutoScaling::LaunchConfiguration/BlockDeviceMapping": { + "type": "structure", + "members": { + "Ebs": "AWS::AutoScaling::LaunchConfiguration/BlockDevice", + "NoDevice": "Boolean", + "VirtualName": "String", + "DeviceName": "String" + } + }, + "AWS::AutoScaling::LaunchConfiguration/MetadataOptions": { + "type": "structure", + "members": { + "HttpPutResponseHopLimit": "Integer", + "HttpTokens": "String", + "HttpEndpoint": "String" + } + }, + "AWS::Events::Rule": { + "type": "structure", + "members": { + "EventBusName": "String", + "EventPattern": "Json", + "ScheduleExpression": "String", + "Description": "String", + "State": "String", + "Targets": "AWS::Events::Rule.Targets", + "Arn": "String", + "RoleArn": "String", + "RuleName": "String", + "Tags": "AWS::Events::Rule.Tags", + "Name": "String" + } + }, + "AWS::Events::Rule.Tags": { + "type": "list", + "member": "AWS::Events::Rule/Tag" + }, + "AWS::Events::Rule.Targets": { + "type": "list", + "member": "AWS::Events::Rule/Target" + }, + "AWS::Events::Rule/AppSyncParameters": { + "type": "structure", + "members": { + "GraphQLOperation": "String" + } + }, + "AWS::Events::Rule/AwsVpcConfiguration": { + "type": "structure", + "members": { + "SecurityGroups": "AWS::Events::Rule/AwsVpcConfiguration.SecurityGroups", + "Subnets": "AWS::Events::Rule/AwsVpcConfiguration.Subnets", + "AssignPublicIp": "String" + } + }, + "AWS::Events::Rule/AwsVpcConfiguration.SecurityGroups": { + "type": "list", + "member": "String" + }, + "AWS::Events::Rule/AwsVpcConfiguration.Subnets": { + "type": "list", + "member": "String" + }, + "AWS::Events::Rule/BatchArrayProperties": { + "type": "structure", + "members": { + "Size": "Integer" + } + }, + "AWS::Events::Rule/BatchParameters": { + "type": "structure", + "members": { + "ArrayProperties": "AWS::Events::Rule/BatchArrayProperties", + "JobName": "String", + "RetryStrategy": "AWS::Events::Rule/BatchRetryStrategy", + "JobDefinition": "String" + } + }, + "AWS::Events::Rule/BatchRetryStrategy": { + "type": "structure", + "members": { + "Attempts": "Integer" + } + }, + "AWS::Events::Rule/CapacityProviderStrategyItem": { + "type": "structure", + "members": { + "CapacityProvider": "String", + "Base": "Integer", + "Weight": "Integer" + } + }, + "AWS::Events::Rule/DeadLetterConfig": { + "type": "structure", + "members": { + "Arn": "String" + } + }, + "AWS::Events::Rule/EcsParameters": { + "type": "structure", + "members": { + "PlatformVersion": "String", + "Group": "String", + "EnableECSManagedTags": "Boolean", + "EnableExecuteCommand": "Boolean", + "PlacementConstraints": "AWS::Events::Rule/EcsParameters.PlacementConstraints", + "PropagateTags": "String", + "TaskCount": "Integer", + "PlacementStrategies": "AWS::Events::Rule/EcsParameters.PlacementStrategies", + "CapacityProviderStrategy": "AWS::Events::Rule/EcsParameters.CapacityProviderStrategy", + "LaunchType": "String", + "ReferenceId": "String", + "TagList": "AWS::Events::Rule/EcsParameters.TagList", + "NetworkConfiguration": "AWS::Events::Rule/NetworkConfiguration", + "TaskDefinitionArn": "String" + } + }, + "AWS::Events::Rule/EcsParameters.CapacityProviderStrategy": { + "type": "list", + "member": "AWS::Events::Rule/CapacityProviderStrategyItem" + }, + "AWS::Events::Rule/EcsParameters.PlacementConstraints": { + "type": "list", + "member": "AWS::Events::Rule/PlacementConstraint" + }, + "AWS::Events::Rule/EcsParameters.PlacementStrategies": { + "type": "list", + "member": "AWS::Events::Rule/PlacementStrategy" + }, + "AWS::Events::Rule/EcsParameters.TagList": { + "type": "list", + "member": "AWS::Events::Rule/Tag" + }, + "AWS::Events::Rule/HttpParameters": { + "type": "structure", + "members": { + "PathParameterValues": "AWS::Events::Rule/HttpParameters.PathParameterValues", + "HeaderParameters": "AWS::Events::Rule/HttpParameters.HeaderParameters", + "QueryStringParameters": "AWS::Events::Rule/HttpParameters.QueryStringParameters" + } + }, + "AWS::Events::Rule/HttpParameters.HeaderParameters": { + "type": "map", + "value": "String" + }, + "AWS::Events::Rule/HttpParameters.PathParameterValues": { + "type": "list", + "member": "String" + }, + "AWS::Events::Rule/HttpParameters.QueryStringParameters": { + "type": "map", + "value": "String" + }, + "AWS::Events::Rule/InputTransformer": { + "type": "structure", + "members": { + "InputPathsMap": "AWS::Events::Rule/InputTransformer.InputPathsMap", + "InputTemplate": "String" + } + }, + "AWS::Events::Rule/InputTransformer.InputPathsMap": { + "type": "map", + "value": "String" + }, + "AWS::Events::Rule/KinesisParameters": { + "type": "structure", + "members": { + "PartitionKeyPath": "String" + } + }, + "AWS::Events::Rule/NetworkConfiguration": { + "type": "structure", + "members": { + "AwsVpcConfiguration": "AWS::Events::Rule/AwsVpcConfiguration" + } + }, + "AWS::Events::Rule/PlacementConstraint": { + "type": "structure", + "members": { + "Type": "String", + "Expression": "String" + } + }, + "AWS::Events::Rule/PlacementStrategy": { + "type": "structure", + "members": { + "Field": "String", + "Type": "String" + } + }, + "AWS::Events::Rule/RedshiftDataParameters": { + "type": "structure", + "members": { + "StatementName": "String", + "Sqls": "AWS::Events::Rule/RedshiftDataParameters.Sqls", + "Database": "String", + "SecretManagerArn": "String", + "DbUser": "String", + "Sql": "String", + "WithEvent": "Boolean" + } + }, + "AWS::Events::Rule/RedshiftDataParameters.Sqls": { + "type": "list", + "member": "String" + }, + "AWS::Events::Rule/RetryPolicy": { + "type": "structure", + "members": { + "MaximumRetryAttempts": "Integer", + "MaximumEventAgeInSeconds": "Integer" + } + }, + "AWS::Events::Rule/RunCommandParameters": { + "type": "structure", + "members": { + "RunCommandTargets": "AWS::Events::Rule/RunCommandParameters.RunCommandTargets" + } + }, + "AWS::Events::Rule/RunCommandParameters.RunCommandTargets": { + "type": "list", + "member": "AWS::Events::Rule/RunCommandTarget" + }, + "AWS::Events::Rule/RunCommandTarget": { + "type": "structure", + "members": { + "Values": "AWS::Events::Rule/RunCommandTarget.Values", + "Key": "String" + } + }, + "AWS::Events::Rule/RunCommandTarget.Values": { + "type": "list", + "member": "String" + }, + "AWS::Events::Rule/SageMakerPipelineParameter": { + "type": "structure", + "members": { + "Value": "String", + "Name": "String" + } + }, + "AWS::Events::Rule/SageMakerPipelineParameters": { + "type": "structure", + "members": { + "PipelineParameterList": "AWS::Events::Rule/SageMakerPipelineParameters.PipelineParameterList" + } + }, + "AWS::Events::Rule/SageMakerPipelineParameters.PipelineParameterList": { + "type": "list", + "member": "AWS::Events::Rule/SageMakerPipelineParameter" + }, + "AWS::Events::Rule/SqsParameters": { + "type": "structure", + "members": { + "MessageGroupId": "String" + } + }, + "AWS::Events::Rule/Tag": { + "type": "structure", + "members": { + "Value": "String", + "Key": "String" + } + }, + "AWS::Events::Rule/Target": { + "type": "structure", + "members": { + "InputPath": "String", + "HttpParameters": "AWS::Events::Rule/HttpParameters", + "DeadLetterConfig": "AWS::Events::Rule/DeadLetterConfig", + "RunCommandParameters": "AWS::Events::Rule/RunCommandParameters", + "InputTransformer": "AWS::Events::Rule/InputTransformer", + "KinesisParameters": "AWS::Events::Rule/KinesisParameters", + "RoleArn": "String", + "RedshiftDataParameters": "AWS::Events::Rule/RedshiftDataParameters", + "AppSyncParameters": "AWS::Events::Rule/AppSyncParameters", + "Input": "String", + "SqsParameters": "AWS::Events::Rule/SqsParameters", + "EcsParameters": "AWS::Events::Rule/EcsParameters", + "BatchParameters": "AWS::Events::Rule/BatchParameters", + "Id": "String", + "Arn": "String", + "SageMakerPipelineParameters": "AWS::Events::Rule/SageMakerPipelineParameters", + "RetryPolicy": "AWS::Events::Rule/RetryPolicy" + } + }, + "AWS::Lambda::Function": { + "type": "structure", + "members": { + "FunctionScalingConfig": "AWS::Lambda::Function/FunctionScalingConfig", + "Description": "String", + "TracingConfig": "AWS::Lambda::Function/TracingConfig", + "VpcConfig": "AWS::Lambda::Function/VpcConfig", + "RuntimeManagementConfig": "AWS::Lambda::Function/RuntimeManagementConfig", + "DurableConfig": "AWS::Lambda::Function/DurableConfig", + "ReservedConcurrentExecutions": "Integer", + "SnapStart": "AWS::Lambda::Function/SnapStart", + "FileSystemConfigs": "AWS::Lambda::Function.FileSystemConfigs", + "FunctionName": "String", + "Runtime": "String", + "KmsKeyArn": "String", + "PublishToLatestPublished": "Boolean", + "PackageType": "String", + "CodeSigningConfigArn": "String", + "Layers": "AWS::Lambda::Function.Layers", + "TenancyConfig": "AWS::Lambda::Function/TenancyConfig", + "Tags": "AWS::Lambda::Function.Tags", + "ImageConfig": "AWS::Lambda::Function/ImageConfig", + "MemorySize": "Integer", + "DeadLetterConfig": "AWS::Lambda::Function/DeadLetterConfig", + "Timeout": "Integer", + "CapacityProviderConfig": "AWS::Lambda::Function/CapacityProviderConfig", + "Handler": "String", + "SnapStartResponse": "AWS::Lambda::Function/SnapStartResponse", + "Code": "AWS::Lambda::Function/Code", + "Role": "String", + "LoggingConfig": "AWS::Lambda::Function/LoggingConfig", + "RecursiveLoop": "AWS::Lambda::Function/RecursiveLoop", + "Environment": "AWS::Lambda::Function/Environment", + "Arn": "String", + "EphemeralStorage": "AWS::Lambda::Function/EphemeralStorage", + "Architectures": "AWS::Lambda::Function.Architectures" + } + }, + "AWS::Lambda::Function.Architectures": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::Function.FileSystemConfigs": { + "type": "list", + "member": "AWS::Lambda::Function/FileSystemConfig" + }, + "AWS::Lambda::Function.Layers": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::Function.Tags": { + "type": "list", + "member": "AWS::Lambda::Function/Tag" + }, + "AWS::Lambda::Function/CapacityProviderConfig": { + "type": "structure", + "members": { + "LambdaManagedInstancesCapacityProviderConfig": "AWS::Lambda::Function/LambdaManagedInstancesCapacityProviderConfig" + } + }, + "AWS::Lambda::Function/Code": { + "type": "structure", + "members": { + "SourceKMSKeyArn": "String", + "S3ObjectVersion": "String", + "S3Bucket": "String", + "ZipFile": "String", + "S3Key": "String", + "S3ObjectStorageMode": "String", + "ImageUri": "String" + } + }, + "AWS::Lambda::Function/DeadLetterConfig": { + "type": "structure", + "members": { + "TargetArn": "String" + } + }, + "AWS::Lambda::Function/DurableConfig": { + "type": "structure", + "members": { + "KMSKeyArn": "String", + "ExecutionTimeout": "Integer", + "RetentionPeriodInDays": "Integer" + } + }, + "AWS::Lambda::Function/Environment": { + "type": "structure", + "members": { + "Variables": "AWS::Lambda::Function/Environment.Variables" + } + }, + "AWS::Lambda::Function/Environment.Variables": { + "type": "map", + "value": "String" + }, + "AWS::Lambda::Function/EphemeralStorage": { + "type": "structure", + "members": { + "Size": "Integer" + } + }, + "AWS::Lambda::Function/FileSystemConfig": { + "type": "structure", + "members": { + "S3FilesConfig": "AWS::Lambda::Function/S3FilesConfig", + "Arn": "String", + "LocalMountPath": "String" + } + }, + "AWS::Lambda::Function/FunctionScalingConfig": { + "type": "structure", + "members": { + "MinExecutionEnvironments": "Integer", + "MaxExecutionEnvironments": "Integer" + } + }, + "AWS::Lambda::Function/ImageConfig": { + "type": "structure", + "members": { + "WorkingDirectory": "String", + "Command": "AWS::Lambda::Function/ImageConfig.Command", + "EntryPoint": "AWS::Lambda::Function/ImageConfig.EntryPoint" + } + }, + "AWS::Lambda::Function/ImageConfig.Command": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::Function/ImageConfig.EntryPoint": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::Function/LambdaManagedInstancesCapacityProviderConfig": { + "type": "structure", + "members": { + "ExecutionEnvironmentMemoryGiBPerVCpu": "Double", + "CapacityProviderArn": "String", + "PerExecutionEnvironmentMaxConcurrency": "Integer" + } + }, + "AWS::Lambda::Function/LoggingConfig": { + "type": "structure", + "members": { + "LogFormat": "String", + "ApplicationLogLevel": "String", + "LogGroup": "String", + "SystemLogLevel": "String" + } + }, + "AWS::Lambda::Function/RecursiveLoop": { + "type": "string" + }, + "AWS::Lambda::Function/RuntimeManagementConfig": { + "type": "structure", + "members": { + "UpdateRuntimeOn": "String", + "RuntimeVersionArn": "String" + } + }, + "AWS::Lambda::Function/S3FilesConfig": { + "type": "structure", + "members": { + "DirectS3Read": "String" + } + }, + "AWS::Lambda::Function/SnapStart": { + "type": "structure", + "members": { + "ApplyOn": "String" + } + }, + "AWS::Lambda::Function/SnapStartResponse": { + "type": "structure", + "members": { + "OptimizationStatus": "String", + "ApplyOn": "String" + } + }, + "AWS::Lambda::Function/Tag": { + "type": "structure", + "members": { + "Value": "String", + "Key": "String" + } + }, + "AWS::Lambda::Function/TenancyConfig": { + "type": "structure", + "members": { + "TenantIsolationMode": "String" + } + }, + "AWS::Lambda::Function/TracingConfig": { + "type": "structure", + "members": { + "Mode": "String" + } + }, + "AWS::Lambda::Function/VpcConfig": { + "type": "structure", + "members": { + "Ipv6AllowedForDualStack": "Boolean", + "SecurityGroupIds": "AWS::Lambda::Function/VpcConfig.SecurityGroupIds", + "SubnetIds": "AWS::Lambda::Function/VpcConfig.SubnetIds" + } + }, + "AWS::Lambda::Function/VpcConfig.SecurityGroupIds": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::Function/VpcConfig.SubnetIds": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::LayerVersion": { + "type": "structure", + "members": { + "CompatibleRuntimes": "AWS::Lambda::LayerVersion.CompatibleRuntimes", + "LicenseInfo": "String", + "Description": "String", + "LayerName": "String", + "Content": "AWS::Lambda::LayerVersion/Content", + "LayerVersionArn": "String", + "CompatibleArchitectures": "AWS::Lambda::LayerVersion.CompatibleArchitectures" + } + }, + "AWS::Lambda::LayerVersion.CompatibleArchitectures": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::LayerVersion.CompatibleRuntimes": { + "type": "list", + "member": "String" + }, + "AWS::Lambda::LayerVersion/Content": { + "type": "structure", + "members": { + "S3ObjectVersion": "String", + "S3Bucket": "String", + "S3Key": "String", + "S3ObjectStorageMode": "String" + } + }, + "Boolean": { + "type": "boolean" + }, + "Double": { + "type": "double" + }, + "Integer": { + "type": "integer" + }, + "Json": { + "type": "json" + }, + "String": { + "type": "string" + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java index 46a9e6bb24..8665ac79b4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationLambdaObjectPropertyIntegrationTest.java @@ -207,7 +207,8 @@ class CloudFormationLambdaObjectPropertyIntegrationTest { /** * The other readers of the function and of its layers refuse a value of the wrong type the same - * way, each with the message measured for it on AWS (eu-west-1, 2026-10-18). + * way, nested members included, each with the message measured for it on AWS (eu-west-1, + * 2026-10-18 and 2026-10-19). */ @Test void everyOtherPropertyOfTheWrongTypeFailsTheCreate() { @@ -226,6 +227,18 @@ class CloudFormationLambdaObjectPropertyIntegrationTest { "Property [Architectures] expected type: JSONArray, found: String"); functionProperties.put(",\"FileSystemConfigs\": \"x\"", "Property [FileSystemConfigs] expected type: JSONArray, found: String"); + functionProperties.put(",\"VpcConfig\": {\"SubnetIds\": \"subnet-0123456789abcdef0\", \"SecurityGroupIds\": []}", + "Property [SubnetIds] expected type: JSONArray, found: String"); + functionProperties.put(",\"EphemeralStorage\": {\"Size\": \"abc\"}", + "Property [Size] expected type: Integer, found: String"); + functionProperties.put(",\"ImageConfig\": {\"Command\": \"x\"}", + "Property [Command] expected type: JSONArray, found: String"); + functionProperties.put(",\"TracingConfig\": {\"Mode\": {\"a\": 1}}", + "Property [Mode] expected type: String, found: JSONObject"); + functionProperties.put(",\"Environment\": {\"Variables\": {\"AB\": {\"b\": 1}}}", + "Property [AB] expected type: String, found: JSONObject"); + functionProperties.put(",\"Environment\": {\"Variables\": {\"AB\": [\"x\"]}}", + "Property [AB] expected type: String, found: JSONArray"); functionProperties.forEach((property, reason) -> { String stack = stackName("type"); submit("CreateStack", stack, template(property)); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypesTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypesTest.java new file mode 100644 index 0000000000..c677312a0d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnPropertyTypesTest.java @@ -0,0 +1,116 @@ +package io.github.hectorvent.floci.services.cloudformation.provisioners; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RequestShapeModel; +import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The property types vendored from the resource schemas, and the reader's walk over the shapes the + * generator writes: structures, lists, maps, scalars and values the schema leaves free. + */ +class CfnPropertyTypesTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static final RequestShapeModel MODEL = model(""" + { + "operations": {"AWS::Test::Thing": "AWS::Test::Thing"}, + "shapes": { + "AWS::Test::Thing": {"type": "structure", "members": { + "Config": "AWS::Test::Thing/Config", "Pattern": "Json", "Ratio": "Double"}}, + "AWS::Test::Thing/Config": {"type": "structure", "members": { + "Headers": "AWS::Test::Thing/Config.Headers", "Count": "Integer", + "Children": "AWS::Test::Thing/Config.Children"}}, + "AWS::Test::Thing/Config.Headers": {"type": "map", "value": "String"}, + "AWS::Test::Thing/Config.Children": {"type": "list", "member": "AWS::Test::Thing/Config"}, + "String": {"type": "string"}, + "Integer": {"type": "integer"}, + "Boolean": {"type": "boolean"}, + "Double": {"type": "double"}, + "Json": {"type": "json"} + } + } + """); + + @Test + void eachPropertyTheProvisionersReadHasItsSchemaShape() { + assertEquals("AWS::Events::Rule.Targets", CfnPropertyTypes.propertyShape("AWS::Events::Rule", "Targets")); + assertEquals("AWS::Lambda::Function/Environment", + CfnPropertyTypes.propertyShape("AWS::Lambda::Function", "Environment")); + assertEquals("AWS::Lambda::LayerVersion.CompatibleRuntimes", + CfnPropertyTypes.propertyShape("AWS::Lambda::LayerVersion", "CompatibleRuntimes")); + assertEquals("AWS::AutoScaling::LaunchConfiguration/MetadataOptions", + CfnPropertyTypes.propertyShape("AWS::AutoScaling::LaunchConfiguration", "MetadataOptions")); + } + + @Test + void aPropertyTheVendoredFileLacksIsAnErrorOfTheBuild() { + IllegalStateException e = assertThrows(IllegalStateException.class, + () -> CfnPropertyTypes.propertyShape("AWS::Events::Rule", "NoSuchProperty")); + assertTrue(e.getMessage().contains("regen_cfn_property_types.py"), e.getMessage()); + assertThrows(IllegalStateException.class, () -> CfnPropertyTypes.propertyShape("AWS::No::Type", "Name")); + } + + @Test + void aMapReadsEachValueAsItsValueShape() { + JsonNode read = read("{\"Headers\": {\"a\": \"x\", \"b\": 5}, \"Count\": \"3\"}", "AWS::Test::Thing/Config"); + + assertEquals(json("{\"Headers\": {\"a\": \"x\", \"b\": \"5\"}, \"Count\": 3}"), read); + AwsException e = assertThrows(AwsException.class, + () -> read("{\"Headers\": \"x\"}", "AWS::Test::Thing/Config")); + assertEquals("Property [Headers] expected type: JSONObject, found: String", e.getMessage()); + } + + @Test + void aStructureNestedInAListIsReadAtEachLevel() { + AwsException e = assertThrows(AwsException.class, + () -> read("{\"Children\": [{\"Count\": 4.5}]}", "AWS::Test::Thing/Config")); + assertEquals("Property [Count] expected type: Integer, found: Float", e.getMessage()); + } + + @Test + void aValueTheSchemaLeavesFreeIsResolvedAsItIs() { + JsonNode read = read("{\"Pattern\": {\"source\": [{\"Fn::Sub\": \"app\"}]}, \"Ratio\": \"half\"}", + "AWS::Test::Thing"); + + assertEquals(json("{\"Pattern\": {\"source\": [\"app\"]}, \"Ratio\": \"half\"}"), read); + } + + @Test + void aValueLeftOutByNoValueIsAbsent() { + assertNull(new CfnPropertyTypes.Reader(engine(), MODEL) + .read("Config", json("{\"Ref\": \"AWS::NoValue\"}"), "AWS::Test::Thing/Config")); + } + + private static JsonNode read(String value, String shape) { + CfnPropertyTypes.Reader reader = new CfnPropertyTypes.Reader(engine(), MODEL); + JsonNode read = reader.read("Config", json(value), shape); + reader.finish(); + return read; + } + + private static CloudFormationTemplateEngine engine() { + return CloudFormationTemplateEngine.standalone("000000000000", "us-east-1", "my-stack", MAPPER, null); + } + + private static JsonNode json(String text) { + try { + return MAPPER.readTree(text); + } catch (JsonProcessingException e) { + throw new IllegalArgumentException(text, e); + } + } + + private static RequestShapeModel model(String text) { + return RequestShapeModel.parse(json(text)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisionerTest.java index b36dfac116..ee7f3cd839 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EventsCfnProvisionerTest.java @@ -65,6 +65,11 @@ class EventsCfnProvisionerTest { ((JsonNode) inv.getArgument(0)).forEach(value -> values.add(value.asText())); return values; }); + when(engine.resolveLiteralList(any())).thenAnswer(inv -> { + List values = new ArrayList<>(); + ((JsonNode) inv.getArgument(0)).forEach(value -> values.add(value.asText())); + return values; + }); return new ProvisionContext(engine, REGION, "000000000000", "my-stack", priorPhysicalId); } @@ -276,9 +281,9 @@ class EventsCfnProvisionerTest { } /** - * Measured on CloudFormation (eu-west-1, 2026-10-18): a member the resource schema types fails - * template validation, naming the member or the list index, or the handler's model validation, - * naming the whole path, before the rule exists. + * Measured on CloudFormation (eu-west-1, 2026-10-18 and 2026-10-19): a member the resource schema + * types fails template validation, naming the member, the list index or the map key, or the + * handler's model validation, naming the whole path, before the rule exists. */ @ParameterizedTest(name = "{0}") @MethodSource("mistypedMembers") @@ -348,6 +353,21 @@ class EventsCfnProvisionerTest { "Property [Group] expected type: String, found: JSONObject"), Arguments.of("{\"EcsParameters\": {\"Group\": [\"a\"]}}", template, "Property [Group] expected type: String, found: JSONArray"), + Arguments.of("{\"Input\": {\"a\": 1}}", template, + "Property [Input] expected type: String, found: JSONObject"), + Arguments.of("{\"InputPath\": [\"$.a\"]}", template, + "Property [InputPath] expected type: String, found: JSONArray"), + Arguments.of("{\"InputTransformer\": {\"InputTemplate\": \"x\", \"InputPathsMap\": \"x\"}}", template, + "Property [InputPathsMap] expected type: JSONObject, found: String"), + Arguments.of("{\"InputTransformer\": {\"InputTemplate\": \"x\", \"InputPathsMap\": {\"a\": {\"b\": 1}}}}", + template, "Property [a] expected type: String, found: JSONObject"), + Arguments.of("{\"BatchParameters\": {\"JobDefinition\": \"d\", \"JobName\": \"n\"," + + " \"ArrayProperties\": {\"Size\": \"abc\"}}}", template, + "Property [Size] expected type: Integer, found: String"), + Arguments.of("{\"HttpParameters\": {\"HeaderParameters\": {\"h\": {\"x\": 1}}}}", template, + "Property [h] expected type: String, found: JSONObject"), + Arguments.of("{\"SqsParameters\": \"x\"}", template, + "Property [SqsParameters] expected type: JSONObject, found: String"), Arguments.of("{\"RetryPolicy\": {\"MaximumRetryAttempts\": 3000000000}}", model, "Model validation failed (#/Targets/0/RetryPolicy/MaximumRetryAttempts:" + " expected type: Integer, found: String" + range + ")"), @@ -404,6 +424,29 @@ class EventsCfnProvisionerTest { assertEquals(List.of("5"), ecs.getNetworkConfiguration().getAwsvpcConfiguration().getSubnets()); } + @Test + void aLiteralListKeepsItsBlankEntriesWhereTheTemplatePutThem() throws Exception { + // Measured: SecurityGroups ["", "sg-..."] reaches the target as both. Path parameter values + // are positional, so a blank one dropped would move the next into its wildcard. + stubPutRule(); + ObjectNode props = mapper.createObjectNode().put("Name", "orders"); + props.putArray("Targets").add(mapper.readTree(""" + {"Id": "Run", "Arn": "arn:aws:ecs:us-east-1:000000000000:cluster/jobs", + "HttpParameters": {"PathParameterValues": ["", "b"]}, + "EcsParameters": {"TaskDefinitionArn": "arn:aws:ecs:us-east-1:000000000000:task-definition/job:1", + "NetworkConfiguration": {"AwsVpcConfiguration": {"Subnets": ["subnet-1"], "SecurityGroups": ["", "sg-1"]}}}} + """)); + + provisioner.provision(resource("AWS::Events::Rule", "Rule"), props, new ProvisionContext( + CloudFormationTemplateEngine.standalone("000000000000", REGION, "my-stack", mapper, null), + REGION, "000000000000", "my-stack", null)); + + Target put = onlyPutTarget(); + assertEquals(List.of("", "b"), put.getHttpParameters().getPathParameterValues()); + assertEquals(List.of("", "sg-1"), + put.getEcsParameters().getNetworkConfiguration().getAwsvpcConfiguration().getSecurityGroups()); + } + private Target onlyPutTarget() { ArgumentCaptor> put = ArgumentCaptor.forClass(List.class); verify(events).putTargets(eq("orders"), any(), put.capture(), anyString()); diff --git a/tools/aws/regen_cfn_property_types.py b/tools/aws/regen_cfn_property_types.py new file mode 100644 index 0000000000..741ef47fa7 --- /dev/null +++ b/tools/aws/regen_cfn_property_types.py @@ -0,0 +1,254 @@ +#!/usr/bin/env python3 +"""Regenerate src/main/resources/aws/cfn-property-types.json from CloudFormation's resource schemas. + +CloudFormation checks every resource property against the type the resource schema declares +before it touches any resource. `CfnPropertyTypes` does the same for the resource types whose +provisioners read their properties through it, and needs only the types: for each resource type +its properties, and for every type reachable from one, whether it is an object and its members, +a list and its items, a map and its values, or a scalar. + +The schemas are the ones AWS publishes for us-east-1, the same `describe-type` returns: + + https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip + +cfn-lint ships copies of them, but widens some types (a Lambda environment variable's value takes +any JSON type there, a string in the published schema), so they are not used. + +The output has the request shapes' layout, so `RequestShapeModel` reads it: `operations` names each +resource type's properties shape, and `shapes` holds a `structure` for an object with properties, +a `list`, a `map` for an object keyed by pattern, `string`, `integer`, `boolean` or `double`, and +`json` for anything else (a free-form object, a value of several types), which is not checked. + +The archive is live rather than a pinned dependency and keeps no older copy, so, as with +`regen_iam_authorized_actions.py`: + +- `--check` is offline and structural: every resource type listed here is present, every shape a + shape names exists, and the anchor cases still hold; +- `--verify` is online: a fresh generation must equal the vendored file, so a schema AWS has + changed since is reported; it exits 2 when the archive cannot be read. + +Run from anywhere in the repo: + python3 tools/aws/regen_cfn_property_types.py # fetch and rewrite the vendored file + python3 tools/aws/regen_cfn_property_types.py --check # offline: the vendored file is well-formed + python3 tools/aws/regen_cfn_property_types.py --verify # online: the vendored file matches AWS +""" +from __future__ import annotations + +import argparse +import io +import json +import sys +import urllib.request +import zipfile +from datetime import datetime, timezone +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] +OUTPUT = REPO_ROOT / "src/main/resources/aws/cfn-property-types.json" +ARCHIVE_URL = "https://schema.cloudformation.us-east-1.amazonaws.com/CloudformationSchema.zip" +FETCH_TIMEOUT_SECONDS = 60 + +# The resource types whose provisioners read their properties through CfnPropertyTypes. +RESOURCE_TYPES = ["AWS::AutoScaling::LaunchConfiguration", "AWS::Events::Rule", "AWS::Lambda::Function", + "AWS::Lambda::LayerVersion"] + +# The shared shape each scalar body is written as, inline; a definition keeps its own name. +SCALAR_SHAPES = {"string": "String", "integer": "Integer", "boolean": "Boolean", "double": "Double", "json": "Json"} +SCHEMA_SCALARS = {"string": "string", "integer": "integer", "boolean": "boolean", "number": "double"} +SHAPE_TYPES = {"structure", "list", "map", *SCALAR_SHAPES} + +# Types the provisioners rely on, each measured on CloudFormation: (shape, member) -> member shape, +# and for a list or a map the shape of its items or values. +ANCHORS = { + ("AWS::Events::Rule/RetryPolicy", "MaximumRetryAttempts"): "Integer", + ("AWS::Events::Rule/EcsParameters", "EnableECSManagedTags"): "Boolean", + ("AWS::Events::Rule/AwsVpcConfiguration", "Subnets"): ["String"], + ("AWS::Lambda::Function/Environment", "Variables"): {"String"}, + ("AWS::Lambda::Function/EphemeralStorage", "Size"): "Integer", + ("AWS::AutoScaling::LaunchConfiguration/MetadataOptions", "HttpPutResponseHopLimit"): "Integer", +} + + +def file_name(resource_type: str) -> str: + """The archive's file for a resource type: AWS::Events::Rule is aws-events-rule.json.""" + return resource_type.lower().replace("::", "-") + ".json" + + +def fetch_schemas(source_dir: Path | None) -> tuple[dict[str, dict], str]: + """The schema of every resource type listed here and a provenance label for where they came from.""" + if source_dir is not None: + return ({resource_type: json.loads((source_dir / file_name(resource_type)).read_text(encoding="utf-8")) + for resource_type in RESOURCE_TYPES}, f"--source-dir {source_dir}") + with urllib.request.urlopen(ARCHIVE_URL, timeout=FETCH_TIMEOUT_SECONDS) as response: + archive = zipfile.ZipFile(io.BytesIO(response.read())) + schemas = {} + for resource_type in RESOURCE_TYPES: + try: + schemas[resource_type] = json.loads(archive.read(file_name(resource_type)).decode("utf-8")) + except KeyError as missing: + raise ValueError(f"{resource_type} has no {file_name(resource_type)} in {ARCHIVE_URL}") from missing + return schemas, ARCHIVE_URL + + +def build(schemas: dict[str, dict], provenance: str) -> dict: + shapes: dict[str, dict] = {name: {"type": kind} for kind, name in SCALAR_SHAPES.items()} + operations = {} + for resource_type in RESOURCE_TYPES: + schema = schemas[resource_type] + properties = {"type": "object", "properties": schema["properties"]} + operations[resource_type] = shape_for(schema, resource_type, resource_type, properties, shapes) + return { + "_source": { + "generator": "tools/aws/regen_cfn_property_types.py", + "schemas": provenance, + "retrieved": datetime.now(timezone.utc).strftime("%Y-%m-%d"), + "note": "make cfn-property-types-verify checks the file against AWS.", + }, + "operations": operations, + "shapes": dict(sorted(shapes.items())), + } + + +def shape_for(schema: dict, resource_type: str, name: str, node: dict, shapes: dict) -> str: + """The name of the shape for `node`, adding it, and every shape it reaches, to `shapes`. A + definition is named `/`, an inline object or list by its path, and an + inline scalar is the shared shape of its type.""" + if "$ref" in node: + definition = node["$ref"].removeprefix("#/definitions/") + target = f"{resource_type}/{definition}" + if target not in shapes: + shapes[target] = {"type": "json"} # until it is built, so a definition that refers to itself ends + shapes[target] = body_of(schema, resource_type, target, schema["definitions"][definition], shapes) + return target + body = body_of(schema, resource_type, name, node, shapes) + if body["type"] in SCALAR_SHAPES: + return SCALAR_SHAPES[body["type"]] + shapes[name] = body + return name + + +def body_of(schema: dict, resource_type: str, name: str, node: dict, shapes: dict) -> dict: + if "$ref" in node: + return dict(shapes[shape_for(schema, resource_type, name, node, shapes)]) + kind = node.get("type") + if isinstance(kind, list): + kind = kind[0] if len(kind) == 1 else None + if kind is None and "properties" in node: + kind = "object" + if kind == "object": + if "properties" in node: + return {"type": "structure", "members": { + member: shape_for(schema, resource_type, f"{name}.{member}", spec, shapes) + for member, spec in node["properties"].items()}} + values = list(node.get("patternProperties", {}).values()) + if isinstance(node.get("additionalProperties"), dict): + values.append(node["additionalProperties"]) + if len(values) == 1: + return {"type": "map", "value": shape_for(schema, resource_type, f"{name}.*", values[0], shapes)} + return {"type": "json"} + if kind == "array": + items = node.get("items") + member = shape_for(schema, resource_type, f"{name}[]", items, shapes) if isinstance(items, dict) else "Json" + return {"type": "list", "member": member} + return {"type": SCHEMA_SCALARS.get(kind, "json")} + + +def render(document: dict) -> str: + return json.dumps(document, indent=2) + "\n" + + +def validate(document: dict) -> list[str]: + """Problems that make the vendored file unusable, rather than merely out of date.""" + problems: list[str] = [] + operations, shapes = document.get("operations"), document.get("shapes") + if not isinstance(operations, dict) or not isinstance(shapes, dict): + return ["operations or shapes is missing"] + for resource_type in RESOURCE_TYPES: + if operations.get(resource_type) not in shapes: + problems.append(f"{resource_type} has no properties shape") + for name, shape in shapes.items(): + kind = shape.get("type") + if kind not in SHAPE_TYPES: + problems.append(f"{name} has the unknown type {kind!r}") + named = list(shape.get("members", {}).values()) + [shape[key] for key in ("member", "value") if key in shape] + for target in named: + if target not in shapes: + problems.append(f"{name} names the missing shape {target!r}") + for (shape, member), expected in ANCHORS.items(): + target = shapes.get(shape, {}).get("members", {}).get(member) + actual = shapes.get(target, {}) if target else {} + if isinstance(expected, list): + holds = actual.get("type") == "list" and actual.get("member") == expected[0] + elif isinstance(expected, set): + holds = actual.get("type") == "map" and actual.get("value") in expected + else: + holds = target == expected + if not holds: + problems.append(f"expected {shape}.{member} to be {expected}, not {target} {actual}") + return problems + + +def _shown(path: Path) -> Path: + try: + return path.resolve().relative_to(REPO_ROOT) + except ValueError: + return path + + +def _without_source(document: dict) -> dict: + return {key: value for key, value in document.items() if key != "_source"} + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--check", action="store_true", help="offline: validate the shape of the vendored file") + parser.add_argument("--verify", action="store_true", help="online: the vendored file matches AWS's schemas") + parser.add_argument("--source-dir", type=Path, default=None, + help="read saved aws--.json schemas instead of fetching the archive") + parser.add_argument("--output", type=Path, default=OUTPUT, help=argparse.SUPPRESS) + args = parser.parse_args(argv) + + if args.check or args.verify: + if not args.output.exists(): + print(f"error: {args.output} does not exist", file=sys.stderr) + return 1 + vendored = json.loads(args.output.read_text(encoding="utf-8")) + + if args.check: + problems = validate(vendored) + for problem in problems: + print(f"error: {problem}", file=sys.stderr) + if problems: + return 1 + print(f"{_shown(args.output)} is well-formed ({len(vendored['shapes'])} shapes over " + f"{len(vendored['operations'])} resource types)") + return 0 + + try: + schemas, provenance = fetch_schemas(args.source_dir) + document = build(schemas, provenance) + except (OSError, ValueError, zipfile.BadZipFile) as e: + # The gate could not run, which CI has to tell apart from having found a difference. + print(f"error: could not read the CloudFormation resource schemas ({e})", file=sys.stderr) + return 2 + + if args.verify: + if _without_source(vendored) != _without_source(document): + print(f"error: {_shown(args.output)} no longer matches {provenance}; run " + f"'make cfn-property-types-sync' and commit the result.", file=sys.stderr) + return 1 + print(f"{_shown(args.output)} matches {provenance}.") + return 0 + + problems = validate(document) + for problem in problems: + print(f"error: {problem}", file=sys.stderr) + if problems: + return 1 + args.output.write_text(render(document), encoding="utf-8") + print(f"wrote {_shown(args.output)} ({len(document['shapes'])} shapes, from {provenance})") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/aws/test_regen_cfn_property_types.py b/tools/aws/test_regen_cfn_property_types.py new file mode 100644 index 0000000000..6e75291160 --- /dev/null +++ b/tools/aws/test_regen_cfn_property_types.py @@ -0,0 +1,102 @@ +"""Tests for regen_cfn_property_types. + +Run with: pytest tools/aws -q (or: make aws-data-test) +""" +from __future__ import annotations + +import json + +import regen_cfn_property_types as r + +SCHEMA = { + "typeName": "AWS::Test::Thing", + "definitions": { + "Target": {"type": "object", "properties": { + "Id": {"type": "string"}, + "Retry": {"$ref": "#/definitions/Retry"}, + "Labels": {"type": "array", "items": {"type": "string"}}, + "Headers": {"type": "object", "patternProperties": {"^.+$": {"type": "string"}}}, + "Body": {"type": ["string", "object"]}, + "Children": {"type": "array", "items": {"$ref": "#/definitions/Target"}}, + }}, + "Retry": {"type": "object", "properties": { + "Attempts": {"type": "integer"}, "Enabled": {"type": "boolean"}, "Ratio": {"type": "number"}}}, + }, + "properties": { + "Name": {"type": "string"}, + "Targets": {"type": "array", "items": {"$ref": "#/definitions/Target"}}, + "Pattern": {"type": "object"}, + "Tags": {"type": "array", "items": {"type": "object", "properties": {"Key": {"type": "string"}}}}, + }, +} + + +def test_the_archive_names_a_schema_after_its_type(): + assert r.file_name("AWS::Events::Rule") == "aws-events-rule.json" + assert r.file_name("AWS::Lambda::LayerVersion") == "aws-lambda-layerversion.json" + + +def test_each_schema_type_becomes_a_shape(monkeypatch): + monkeypatch.setattr(r, "RESOURCE_TYPES", ["AWS::Test::Thing"]) + + doc = r.build({"AWS::Test::Thing": SCHEMA}, "schemas test") + + assert doc["_source"]["schemas"] == "schemas test" + assert doc["operations"] == {"AWS::Test::Thing": "AWS::Test::Thing"} + shapes = doc["shapes"] + assert shapes["AWS::Test::Thing"] == {"type": "structure", "members": { + "Name": "String", "Targets": "AWS::Test::Thing.Targets", "Pattern": "Json", "Tags": "AWS::Test::Thing.Tags"}} + assert shapes["AWS::Test::Thing.Targets"] == {"type": "list", "member": "AWS::Test::Thing/Target"} + assert shapes["AWS::Test::Thing/Target"] == {"type": "structure", "members": { + "Id": "String", "Retry": "AWS::Test::Thing/Retry", "Labels": "AWS::Test::Thing/Target.Labels", + "Headers": "AWS::Test::Thing/Target.Headers", "Body": "Json", "Children": "AWS::Test::Thing/Target.Children"}} + assert shapes["AWS::Test::Thing/Target.Labels"] == {"type": "list", "member": "String"} + assert shapes["AWS::Test::Thing/Target.Headers"] == {"type": "map", "value": "String"} + assert shapes["AWS::Test::Thing/Target.Children"] == {"type": "list", "member": "AWS::Test::Thing/Target"} + assert shapes["AWS::Test::Thing/Retry"] == {"type": "structure", "members": { + "Attempts": "Integer", "Enabled": "Boolean", "Ratio": "Double"}} + assert shapes["AWS::Test::Thing.Tags[]"] == {"type": "structure", "members": {"Key": "String"}} + for name, kind in [("String", "string"), ("Integer", "integer"), ("Boolean", "boolean"), ("Double", "double"), + ("Json", "json")]: + assert shapes[name] == {"type": kind} + + +def test_the_vendored_file_is_well_formed(): + assert r.validate(json.loads(r.OUTPUT.read_text(encoding="utf-8"))) == [] + + +def test_validate_names_a_missing_shape_and_a_broken_anchor(): + doc = json.loads(r.OUTPUT.read_text(encoding="utf-8")) + doc["shapes"]["AWS::Events::Rule/RetryPolicy"]["members"]["MaximumRetryAttempts"] = "String" + doc["shapes"]["AWS::Events::Rule/Target"]["members"]["Gone"] = "AWS::Events::Rule/Gone" + + problems = r.validate(doc) + + assert any("missing shape 'AWS::Events::Rule/Gone'" in problem for problem in problems) + assert any("AWS::Events::Rule/RetryPolicy.MaximumRetryAttempts" in problem for problem in problems) + + +def test_verify_reports_a_schema_aws_has_changed(tmp_path, monkeypatch): + monkeypatch.setattr(r, "RESOURCE_TYPES", ["AWS::Test::Thing"]) + monkeypatch.setattr(r, "ANCHORS", {}) + source = tmp_path / "schemas" + source.mkdir() + (source / "aws-test-thing.json").write_text(json.dumps(SCHEMA), encoding="utf-8") + output = tmp_path / "types.json" + output.write_text(r.render(r.build({"AWS::Test::Thing": SCHEMA}, "earlier")), encoding="utf-8") + + assert r.main(["--verify", "--source-dir", str(source), "--output", str(output)]) == 0 + + changed = json.loads(json.dumps(SCHEMA)) + changed["definitions"]["Retry"]["properties"]["Attempts"]["type"] = "string" + (source / "aws-test-thing.json").write_text(json.dumps(changed), encoding="utf-8") + + assert r.main(["--verify", "--source-dir", str(source), "--output", str(output)]) == 1 + + +def test_verify_exits_2_when_the_schemas_cannot_be_read(tmp_path, monkeypatch): + monkeypatch.setattr(r, "RESOURCE_TYPES", ["AWS::Test::Thing"]) + output = tmp_path / "types.json" + output.write_text(r.render(r.build({"AWS::Test::Thing": SCHEMA}, "earlier")), encoding="utf-8") + + assert r.main(["--verify", "--source-dir", str(tmp_path / "absent"), "--output", str(output)]) == 2