mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(cloudformation): read property types from the vendored resource schemas (#5988)
* refactor(cloudformation): read property types from the vendored resource schemas Follow-up asked on #5958: the types EventsCfnProvisioner checked a rule target's members against were written by hand from describe-type, and LambdaCfnProvisioner named the JSON type of each property it reads. They now come from src/main/resources/aws/cfn-property-types.json, which tools/aws/regen_cfn_property_types.py generates from the resource schemas AWS publishes for us-east-1, in the request shapes' layout so RequestShapeModel reads it: a structure, list, map or scalar per shape, and "json" for what a schema leaves free. The schemas come from AWS's live archive rather than a pinned package. cfn-lint ships copies, but widens some types: a Lambda environment variable's value takes any JSON type there and a string in the published schema, where CloudFormation refuses an object or a list with "Property [AB] expected type: String, found: JSONObject" (measured). The archive keeps no older copy, so the generator follows the IAM data's split: --check is offline and structural and runs on every pull request, --verify compares a fresh generation with the vendored file and runs in cfn-schema-drift.yml, weekly and on changes to the file or its generator. CfnPropertyTypes.Reader walks the vendored shapes instead of type trees, maps included: a map's values are read as its value shape, and a mismatch names the key, as CloudFormation does ("Property [h] expected type: String, found: JSONObject", measured). Every member of a rule target is now read against its schema type, not only the four #5958 typed, and Lambda reads the properties it already typed through the same reader, nested members included (VpcConfig.SubnetIds, EphemeralStorage. Size, ImageConfig.Command, TracingConfig.Mode, Environment.Variables). The messages for the newly checked members were measured on CloudFormation (eu-west-1) and are in the tests. * fix(cloudformation): keep a literal list's blank entries where the template put them Review of #5988: the reader read every list of strings through the engine's resolveStringList, which drops blank entries, so a rule target's HttpParameters.PathParameterValues ["", "b"] became ["b"] and moved "b" into the first wildcard. CloudFormation passes a literal list on as written (measured: SecurityGroups ["", "sg-..."] reaches the target as both). A literal list is now read through the engine's new resolveLiteralList, which keeps blanks and still expands a list-valued intrinsic in place; a list an intrinsic yields keeps the engine's reading. Lambda's literal lists, read through the same reader, keep their blanks too, for Lambda to judge as on AWS. resolveLiteralList delegates to the private resolveListElements rather than opening it: tests that mock the engine and call resolveStringList's real code would otherwise get the mock's empty list from it, as IamUserCfnProvisionerTest did. * refactor(cloudformation): read a launch configuration's MetadataOptions by its schema shape Review of #5988: #5964, merged after this branch opened, read AWS::AutoScaling::LaunchConfiguration's MetadataOptions through the type trees this PR removes. The generator now vendors that resource type too, and AutoScalingGroupCfnProvisioner reads the block by the shape its published schema declares (HttpTokens and HttpEndpoint strings, HttpPutResponseHopLimit an integer, the same types #5964 wrote by hand), so its tests pass unchanged. The hop limit's type is an anchor of the generator's offline check.
This commit is contained in:
1 parent
83147f204e
commit
e5a206554f
16 files changed
+1406
-170
No files matched your search
+14
-1
@@ -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));
|
||||
|
||||
+116
@@ -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));
|
||||
}
|
||||
}
|
||||
+46
-3
@@ -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<String> 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<List<Target>> put = ArgumentCaptor.forClass(List.class);
|
||||
verify(events).putTargets(eq("orders"), any(), put.capture(), anyString());
|
||||
|
||||
Reference in new issue
Block a user