mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
+10
@@ -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<String> resolveLiteralList(JsonNode array) {
|
||||
return resolveListElements(array);
|
||||
}
|
||||
|
||||
private List<String> resolveListElements(JsonNode node, boolean resolveDynamicReferences) {
|
||||
List<String> out = new ArrayList<>();
|
||||
for (JsonNode element : node) {
|
||||
|
||||
+3
-6
@@ -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;
|
||||
|
||||
|
||||
+91
-66
@@ -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}.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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<String, Type> 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<String, Type> 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<String, Type> members) {
|
||||
private JsonNode readObject(String path, String property, JsonNode value, Map<String, String> members) {
|
||||
requireType(property, value, JsonNodeType.OBJECT);
|
||||
ObjectNode out = JsonNodeFactory.instance.objectNode();
|
||||
for (Map.Entry<String, JsonNode> 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<String, JsonNode> 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<String> 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<String> 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) {
|
||||
|
||||
+3
-34
@@ -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<Target> declaredTargets(JsonNode props, ProvisionContext ctx) {
|
||||
List<Target> targets = new ArrayList<>();
|
||||
|
||||
+47
-59
@@ -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/<name>), 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<String, String> resolveLambdaEnvironment(JsonNode props, CloudFormationTemplateEngine engine) {
|
||||
JsonNode envNode = resolveTyped(props, "Environment", JsonNodeType.OBJECT, engine);
|
||||
private Map<String, String> 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<String, String> vars = new HashMap<>();
|
||||
variables.fields().forEachRemaining(e -> vars.put(e.getKey(), e.getValue().asText()));
|
||||
return vars;
|
||||
}
|
||||
|
||||
private List<String> resolveStringListOrEmpty(JsonNode props, String source,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
List<String> values = resolveStringList(props, source, engine);
|
||||
return values == null ? List.of() : values;
|
||||
private static List<String> 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<String> 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<String> texts(JsonNode values) {
|
||||
List<String> out = new ArrayList<>();
|
||||
values.forEach(value -> out.add(value.asText()));
|
||||
return out;
|
||||
}
|
||||
|
||||
private List<Object> resolveObjectListOrEmpty(JsonNode props, String source,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
JsonNode resolved = resolveTyped(props, source, JsonNodeType.ARRAY, engine);
|
||||
private List<Object> 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<String, Object> resolveMapOrDefault(JsonNode props, String source,
|
||||
CloudFormationTemplateEngine engine,
|
||||
private Map<String, Object> resolveMapOrDefault(CfnPropertyTypes.Reader types, JsonNode props, String source,
|
||||
Map<String, Object> 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<String, Object> mapWithNullValue(String key) {
|
||||
@@ -848,19 +834,19 @@ public class LambdaCfnProvisioner implements CfnResourceProvisioner {
|
||||
return map;
|
||||
}
|
||||
|
||||
private void putStringListIfPresent(Map<String, Object> request, JsonNode props, String source,
|
||||
String target, CloudFormationTemplateEngine engine) {
|
||||
List<String> values = resolveStringList(props, source, engine);
|
||||
private void putStringListIfPresent(Map<String, Object> 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<String, Object> request, JsonNode props, String source,
|
||||
String target, CloudFormationTemplateEngine engine) {
|
||||
JsonNode resolved = resolveTyped(props, source, JsonNodeType.OBJECT, engine);
|
||||
private void putResolvedMapIfPresent(Map<String, Object> 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<String> runtimes = resolveStringListOrEmpty(props, "CompatibleRuntimes", engine);
|
||||
CfnPropertyTypes.Reader types = new CfnPropertyTypes.Reader(engine);
|
||||
List<String> runtimes = resolveStringListOrEmpty(types, LAYER_VERSION, props, "CompatibleRuntimes");
|
||||
if (!runtimes.isEmpty()) {
|
||||
request.put("CompatibleRuntimes", runtimes);
|
||||
}
|
||||
List<String> architectures = resolveStringListOrEmpty(props, "CompatibleArchitectures", engine);
|
||||
List<String> architectures = resolveStringListOrEmpty(types, LAYER_VERSION, props, "CompatibleArchitectures");
|
||||
types.finish();
|
||||
if (!architectures.isEmpty()) {
|
||||
request.put("CompatibleArchitectures", architectures);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user