mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudformation): identify Lambda microVM images and network connectors by ARN (#4971)
* fix(cloudformation): identify Lambda microVM images and network connectors by ARN Both registry schemas name the ARN as the primaryIdentifier, but Ref returned the image name and the connector id. The physical id is now the ARN, and the prior name is read from the Name attribute recorded at create time. A network connector update created a new connector with a random client token and leaked the old one. An update that keeps Name, the only createOnly property, now goes through updateConnector; a changed Name creates a replacement, and ReplacementCleanup deletes the displaced image or connector once the update commits or restores it on rollback. A stack created when the physical id was still the name or id records its cleanup before the id moves to the ARN, so the entity it reuses is not marked displaced. Deletes tolerate an entity that is already gone. * fix(cloudformation): update every connector setting in place, roll it back, and replace on a dropped Name Greptile on #4971 found three gaps in the network connector update path: - An update that kept Name applied only subnets and security groups, so a changed OperatorRole, NetworkProtocol or AssociatedComputeResourceTypes reported success and changed nothing. UpdateNetworkConnector accepts all of them, so LambdaMicrovmsService.updateConnector now applies them (null leaves a value as it is), and both the REST controller and the provisioner pass them. - The in-place update could not be rolled back: no replacement was recorded, so a later failure left the stack in UPDATE_ROLLBACK_FAILED with the attempted settings. The provisioner now snapshots the connector's settings before the update and restores them in rollbackUpdate, spending the snapshot only once the restore succeeded. - Dropping an explicit Name reused the old named entity, while Name is createOnly and its removal replaces the resource. A prior name is now kept only when it has the shape generatePhysicalName gives this resource; a declared name that the template drops gets a generated one and the old entity is replaced. The check reads the name itself, so it holds across rollbacks and for stacks created before this change.
This commit is contained in:
1 parent
e25f488bf3
commit
be2998f2a2
8 files changed
+598
-63
No files matched your search
+7
@@ -140,6 +140,13 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String LAUNCH_TEMPLATE_UPDATE_SNAPSHOT_ATTR = "__FlociLaunchTemplateUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the settings a Lambda MicroVMs network connector had before an in-place update changed
|
||||
* them, so a failed stack update can put them back. Written by {@code LambdaMicrovmsCfnProvisioner}
|
||||
* before its update call and spent by its {@code rollbackUpdate}.
|
||||
*/
|
||||
public static final String NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR = "__FlociNetworkConnectorUpdateSnapshot";
|
||||
|
||||
private CfnRollback() {
|
||||
}
|
||||
|
||||
|
||||
+173
-22
@@ -1,14 +1,22 @@
|
||||
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 com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* CloudFormation provisioning for AWS Lambda MicroVMs:
|
||||
@@ -20,10 +28,22 @@ import java.util.Set;
|
||||
* group's {@code GroupId} — each array element resolves through the engine
|
||||
* individually. An image's {@code EgressNetworkConnectors} likewise arrive as
|
||||
* references to connector ARNs.</p>
|
||||
*
|
||||
* <p>Both types are identified by their ARN, the registry schemas' primaryIdentifier, so {@code Ref}
|
||||
* returns it. {@code Name} is the only createOnly property of either: an update that keeps it
|
||||
* reconciles the existing entity in place, and one that changes it creates a replacement and the
|
||||
* displaced entity is deleted once the update commits, through {@link ReplacementCleanup}. Removing
|
||||
* an explicit {@code Name} is such a change: the resource is replaced under a generated name. An
|
||||
* in-place connector update is snapshotted first, so a failed stack update puts it back.</p>
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String NETWORK_CONNECTOR = "AWS::Lambda::NetworkConnector";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
/** The shape {@link ProvisionContext#generatePhysicalName} gives a name: a stack-and-id prefix, a hex token. */
|
||||
private static final Pattern GENERATED_NAME = Pattern.compile("(?:(.*)-)?[0-9a-f]{12}");
|
||||
|
||||
private final LambdaMicrovmsService microvmsService;
|
||||
|
||||
@Inject
|
||||
@@ -49,16 +69,71 @@ public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
switch (resourceType) {
|
||||
case "AWS::Lambda::MicrovmImage" -> microvmsService.deleteImage(region, physicalId);
|
||||
case "AWS::Lambda::NetworkConnector" -> microvmsService.deleteConnector(region, physicalId);
|
||||
case "AWS::Lambda::MicrovmImage" -> CfnDeletes.safeDelete("MicroVM image", physicalId,
|
||||
() -> microvmsService.deleteImage(region, physicalId), "ResourceNotFoundException");
|
||||
case "AWS::Lambda::NetworkConnector" -> CfnDeletes.safeDelete("network connector", physicalId,
|
||||
() -> microvmsService.deleteConnector(region, physicalId), "ResourceNotFoundException");
|
||||
default -> { }
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasReplacementUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.hasReplacement(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String updateCleanupPhysicalId(StackResource resource) {
|
||||
return ReplacementCleanup.cleanupPhysicalId(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
return ReplacementCleanup.complete(resource, this::delete);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(CfnRollback.NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR);
|
||||
ReplacementCleanup.clear(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* A replacement is undone through the cleanup record, and an in-place connector update from the
|
||||
* snapshot taken before it; a connector with neither changed nothing. An image updated in place
|
||||
* keeps no snapshot and is still reported as not rolled back.
|
||||
*/
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
if (ReplacementCleanup.rollback(resource, this::delete)) {
|
||||
return true;
|
||||
}
|
||||
if (!NETWORK_CONNECTOR.equals(resource.getResourceType())) {
|
||||
return false;
|
||||
}
|
||||
// Spent only once the restore succeeded, so a restore that throws can be retried.
|
||||
String raw = resource.getAttributes().get(CfnRollback.NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR);
|
||||
if (raw == null) {
|
||||
return true;
|
||||
}
|
||||
JsonNode snapshot;
|
||||
try {
|
||||
snapshot = MAPPER.readTree(raw);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Could not read the network connector update snapshot for "
|
||||
+ resource.getLogicalId(), e);
|
||||
}
|
||||
microvmsService.updateConnector(snapshot.path("region").asText(), snapshot.path("arn").asText(),
|
||||
textList(snapshot.path("subnetIds")), textList(snapshot.path("securityGroupIds")),
|
||||
snapshot.path("operatorRole").asText(null), snapshot.path("networkProtocol").asText(null),
|
||||
textList(snapshot.path("associatedComputeResourceTypes")));
|
||||
resource.getAttributes().remove(CfnRollback.NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void provisionImage(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
// Name is a createOnlyProperty and the physical id, so an unnamed image keeps the name it
|
||||
// already had across updates instead of a fresh one each time provision runs.
|
||||
String name = ctx.stablePhysicalName(ctx.resolveOptional(props, "Name"), r.getLogicalId(), 64, false);
|
||||
String priorName = r.getAttributes().get("Name");
|
||||
String name = nameOrPrior(ctx.resolveOptional(props, "Name"), priorName, r, ctx);
|
||||
String codeArtifactUri = null;
|
||||
if (props != null && props.has("CodeArtifact") && props.get("CodeArtifact").has("Uri")) {
|
||||
codeArtifactUri = ctx.engine().resolve(props.get("CodeArtifact").get("Uri"));
|
||||
@@ -70,12 +145,14 @@ public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
// the service with an image that exists; the registry schema's update handler is
|
||||
// UpdateMicrovmImage, so reconcile through updateImage rather than mint a version through
|
||||
// createImage. A replacing update derives a different name and still creates.
|
||||
LambdaMicrovmsService.MicrovmImage image = ctx.reusesPriorEntity(name)
|
||||
boolean reused = ctx.isUpdate() && name.equals(priorName);
|
||||
Map<String, String> attributesBefore = new HashMap<>(r.getAttributes());
|
||||
LambdaMicrovmsService.MicrovmImage image = reused
|
||||
? microvmsService.updateImage(ctx.region(), name, baseImageArn, buildRoleArn,
|
||||
codeArtifactUri, description)
|
||||
: microvmsService.createImage(ctx.region(), ctx.accountId(), name, baseImageArn,
|
||||
buildRoleArn, codeArtifactUri, description);
|
||||
r.setPhysicalId(image.name);
|
||||
settle(r, ctx, attributesBefore, reused, image.imageArn);
|
||||
r.getAttributes().put("ImageArn", image.imageArn);
|
||||
r.getAttributes().put("Arn", image.imageArn);
|
||||
r.getAttributes().put("Name", image.name);
|
||||
@@ -83,10 +160,10 @@ public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
|
||||
private void provisionConnector(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "Name");
|
||||
if (name == null || name.isBlank()) {
|
||||
name = ctx.generatePhysicalName(r.getLogicalId(), 64, false);
|
||||
}
|
||||
// A snapshot describes the update in flight; one an earlier update left behind is stale.
|
||||
r.getAttributes().remove(CfnRollback.NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR);
|
||||
String priorName = r.getAttributes().get("Name");
|
||||
String name = nameOrPrior(ctx.resolveOptional(props, "Name"), priorName, r, ctx);
|
||||
JsonNode vpc = props == null
|
||||
? null
|
||||
: props.path("Configuration").path("VpcEgressConfiguration");
|
||||
@@ -94,22 +171,96 @@ public class LambdaMicrovmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (vpc != null && vpc.has("NetworkProtocol")) {
|
||||
networkProtocol = ctx.engine().resolve(vpc.get("NetworkProtocol"));
|
||||
}
|
||||
LambdaMicrovmsService.NetworkConnector connector = microvmsService.createConnector(
|
||||
ctx.region(),
|
||||
ctx.accountId(),
|
||||
name,
|
||||
resolveList(vpc == null ? null : vpc.get("SubnetIds"), ctx),
|
||||
resolveList(vpc == null ? null : vpc.get("SecurityGroupIds"), ctx),
|
||||
ctx.resolveOptional(props, "OperatorRole"),
|
||||
java.util.UUID.randomUUID().toString(),
|
||||
resolveList(vpc == null ? null : vpc.get("AssociatedComputeResourceTypes"), ctx),
|
||||
networkProtocol);
|
||||
r.setPhysicalId(connector.id);
|
||||
List<String> subnetIds = resolveList(vpc == null ? null : vpc.get("SubnetIds"), ctx);
|
||||
List<String> securityGroupIds = resolveList(vpc == null ? null : vpc.get("SecurityGroupIds"), ctx);
|
||||
String operatorRole = ctx.resolveOptional(props, "OperatorRole");
|
||||
List<String> computeResourceTypes =
|
||||
resolveList(vpc == null ? null : vpc.get("AssociatedComputeResourceTypes"), ctx);
|
||||
// The schema's update handler is UpdateNetworkConnector, so an update that keeps the name
|
||||
// reconciles the connector it already has instead of creating a second one.
|
||||
boolean reused = ctx.isUpdate() && name.equals(priorName);
|
||||
Map<String, String> attributesBefore = new HashMap<>(r.getAttributes());
|
||||
LambdaMicrovmsService.NetworkConnector connector;
|
||||
if (reused) {
|
||||
snapshotConnector(r, ctx);
|
||||
connector = microvmsService.updateConnector(ctx.region(), ctx.priorPhysicalId(), subnetIds,
|
||||
securityGroupIds, operatorRole, networkProtocol, computeResourceTypes);
|
||||
} else {
|
||||
connector = microvmsService.createConnector(ctx.region(), ctx.accountId(), name, subnetIds,
|
||||
securityGroupIds, operatorRole, UUID.randomUUID().toString(), computeResourceTypes,
|
||||
networkProtocol);
|
||||
}
|
||||
settle(r, ctx, attributesBefore, reused, connector.arn);
|
||||
r.getAttributes().put("Arn", connector.arn);
|
||||
r.getAttributes().put("Id", connector.id);
|
||||
r.getAttributes().put("Name", connector.name);
|
||||
}
|
||||
|
||||
/** Records the connector's settings before an in-place update, for {@link #rollbackUpdate}. */
|
||||
private void snapshotConnector(StackResource r, ProvisionContext ctx) {
|
||||
LambdaMicrovmsService.NetworkConnector current =
|
||||
microvmsService.getConnector(ctx.region(), ctx.priorPhysicalId());
|
||||
ObjectNode snapshot = MAPPER.createObjectNode();
|
||||
snapshot.put("region", ctx.region());
|
||||
snapshot.put("arn", current.arn);
|
||||
current.subnetIds.forEach(snapshot.putArray("subnetIds")::add);
|
||||
current.securityGroupIds.forEach(snapshot.putArray("securityGroupIds")::add);
|
||||
snapshot.put("operatorRole", current.operatorRole);
|
||||
snapshot.put("networkProtocol", current.networkProtocol);
|
||||
current.associatedComputeResourceTypes.forEach(snapshot.putArray("associatedComputeResourceTypes")::add);
|
||||
r.getAttributes().put(CfnRollback.NETWORK_CONNECTOR_UPDATE_SNAPSHOT_ATTR, snapshot.toString());
|
||||
}
|
||||
|
||||
private static List<String> textList(JsonNode node) {
|
||||
List<String> out = new ArrayList<>();
|
||||
node.forEach(item -> out.add(item.asText()));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The template's name when it gives one, otherwise the generated name this resource already had,
|
||||
* and only failing both a fresh generated one. The physical id is the ARN, so the prior name comes
|
||||
* from the {@code Name} attribute recorded at create time. A prior name that was declared, not
|
||||
* generated, is not kept when the template drops it: {@code Name} is createOnly, so removing it
|
||||
* replaces the resource, as CloudFormation does.
|
||||
*/
|
||||
private static String nameOrPrior(String declared, String prior, StackResource r, ProvisionContext ctx) {
|
||||
if (declared != null && !declared.isBlank()) {
|
||||
return declared;
|
||||
}
|
||||
if (ctx.isUpdate() && prior != null && wasGenerated(prior, ctx.stackName(), r.getLogicalId())) {
|
||||
return prior;
|
||||
}
|
||||
return ctx.generatePhysicalName(r.getLogicalId(), 64, false);
|
||||
}
|
||||
|
||||
/** Whether {@code name} has the shape this resource's generated names have, truncation included. */
|
||||
static boolean wasGenerated(String name, String stackName, String logicalId) {
|
||||
Matcher matcher = GENERATED_NAME.matcher(name);
|
||||
if (!matcher.matches()) {
|
||||
return false;
|
||||
}
|
||||
String prefix = matcher.group(1);
|
||||
return prefix == null || (stackName + "-" + logicalId).startsWith(prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the ARN as the physical id and records any replacement. A reuse records before the id
|
||||
* changes: a stack created when the physical id was still the image name or connector id names
|
||||
* the entity it reuses by that, and recording after the switch would mark it displaced and
|
||||
* delete it once the update commits.
|
||||
*/
|
||||
private static void settle(StackResource r, ProvisionContext ctx, Map<String, String> attributesBefore,
|
||||
boolean reused, String arn) {
|
||||
if (reused) {
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
r.setPhysicalId(arn);
|
||||
return;
|
||||
}
|
||||
r.setPhysicalId(arn);
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
}
|
||||
|
||||
/** Resolves each array element through the engine so intrinsic refs work per entry. */
|
||||
private List<String> resolveList(JsonNode node, ProvisionContext ctx) {
|
||||
if (node == null || !node.isArray()) {
|
||||
|
||||
+20
-1
@@ -486,8 +486,15 @@ public class LambdaMicrovmsService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateNetworkConnector: every member is optional and a null one leaves the connector's value
|
||||
* as it is. The model lets an update change the operator role, the network protocol and the
|
||||
* associated compute resource types as well as the subnets and security groups.
|
||||
*/
|
||||
public NetworkConnector updateConnector(String region, String id,
|
||||
List<String> subnetIds, List<String> securityGroupIds) {
|
||||
List<String> subnetIds, List<String> securityGroupIds,
|
||||
String operatorRole, String networkProtocol,
|
||||
List<String> computeResourceTypes) {
|
||||
NetworkConnector connector = getConnector(region, id);
|
||||
if (subnetIds != null && !subnetIds.isEmpty()) {
|
||||
if (subnetIds.size() > MAX_CONNECTOR_SUBNETS) {
|
||||
@@ -502,6 +509,18 @@ public class LambdaMicrovmsService {
|
||||
if (securityGroupIds != null) {
|
||||
connector.securityGroupIds = List.copyOf(securityGroupIds);
|
||||
}
|
||||
if (operatorRole != null && !operatorRole.isBlank()) {
|
||||
connector.operatorRole = operatorRole;
|
||||
connector.lastModified = Instant.now();
|
||||
}
|
||||
if (networkProtocol != null && !networkProtocol.isBlank()) {
|
||||
connector.networkProtocol = networkProtocol;
|
||||
connector.lastModified = Instant.now();
|
||||
}
|
||||
if (computeResourceTypes != null && !computeResourceTypes.isEmpty()) {
|
||||
connector.associatedComputeResourceTypes = List.copyOf(computeResourceTypes);
|
||||
connector.lastModified = Instant.now();
|
||||
}
|
||||
persist(connectors, region, connector.id, connector);
|
||||
return connector;
|
||||
}
|
||||
|
||||
+3
-1
@@ -99,7 +99,9 @@ public class LambdaNetworkConnectorsController {
|
||||
JsonNode request = parse(body);
|
||||
JsonNode vpc = request.path("Configuration").path("VpcEgressConfiguration");
|
||||
NetworkConnector connector = service.updateConnector(
|
||||
region, identifier, stringList(vpc.path("SubnetIds")), stringList(vpc.path("SecurityGroupIds")));
|
||||
region, identifier, stringList(vpc.path("SubnetIds")), stringList(vpc.path("SecurityGroupIds")),
|
||||
text(request, "OperatorRole"), text(vpc, "NetworkProtocol"),
|
||||
stringList(vpc.path("AssociatedComputeResourceTypes")));
|
||||
return Response.status(202).entity(connectorNode(connector, ConnectorShape.UPDATE)).build();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user