mirror of
https://github.com/floci-io/floci.git
synced 2026-11-26 23:29:28 -05:00
fix(cloudformation): apply an AWS::EC2::FlowLog's Tags (#7490)
This commit is contained in:
1 parent
7b4e568ce4
commit
dbbf22957f
10 files changed
+620
-19
No files matched your search
+8
@@ -166,6 +166,14 @@ public final class CfnRollback {
|
||||
*/
|
||||
public static final String LAMBDA_WEB_UPDATE_SNAPSHOT_ATTR = "__FlociLambdaWebUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds what an update did to an EC2 flow log, so a failed stack update can undo it: the tag
|
||||
* keys it touched on the reused log with the values they had, or the log it created because
|
||||
* the stack's own was gone. Written by {@code Ec2FlowLogCfnProvisioner} before its tag write or
|
||||
* right after that creation, and spent by its {@code rollbackUpdate}.
|
||||
*/
|
||||
public static final String FLOW_LOG_UPDATE_SNAPSHOT_ATTR = "__FlociEc2FlowLogUpdateSnapshot";
|
||||
|
||||
/**
|
||||
* Holds the function ARN, status, caching flag, token settings and tags an IoT authorizer
|
||||
* carried before an in-place update changed them, so a failed stack update can put them back.
|
||||
|
||||
+170
-6
@@ -1,20 +1,33 @@
|
||||
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.ArrayNode;
|
||||
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.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.FlowLogService;
|
||||
import io.github.hectorvent.floci.services.ec2.model.FlowLog;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* CloudFormation provisioning for {@code AWS::EC2::FlowLog}, backed by {@link FlowLogService}.
|
||||
* Ref and {@code Fn::GetAtt [Flow, Id]} both yield the {@code fl-} id, matching AWS.
|
||||
* Ref and {@code Fn::GetAtt [Flow, Id]} both yield the {@code fl-} id, matching AWS. {@code Tags}
|
||||
* live in the EC2 tag store, as a {@code CreateFlowLogs} TagSpecification's do, and an update
|
||||
* reconciles the keys the template owns on the reused log, keeping tags added outside the stack, as
|
||||
* AWS does. What an update changes is recorded in {@link CfnRollback#FLOW_LOG_UPDATE_SNAPSHOT_ATTR},
|
||||
* so a stack update that fails on a later resource puts the touched keys back, or deletes a log the
|
||||
* update recreated because the stack's own was gone.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
@@ -25,12 +38,23 @@ public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
/** The defaults createFlowLog applies when the template omits these, kept in step with it. */
|
||||
private static final String DEFAULT_RESOURCE_TYPE = "VPC";
|
||||
private static final String DEFAULT_TRAFFIC_TYPE = "ALL";
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String SNAPSHOT_ATTR = CfnRollback.FLOW_LOG_UPDATE_SNAPSHOT_ATTR;
|
||||
|
||||
/**
|
||||
* The tag keys the last deployment resolved and wrote, as a JSON array: the keys the stack owns.
|
||||
* Recorded rather than re-resolved from the previous template, because a key taken from a
|
||||
* parameter resolves to the new value on the next update.
|
||||
*/
|
||||
static final String TAG_KEYS_ATTR = "__FlociEc2FlowLogTagKeys";
|
||||
|
||||
private final FlowLogService flowLogService;
|
||||
private final Ec2Service ec2Service;
|
||||
|
||||
@Inject
|
||||
public Ec2FlowLogCfnProvisioner(FlowLogService flowLogService) {
|
||||
public Ec2FlowLogCfnProvisioner(FlowLogService flowLogService, Ec2Service ec2Service) {
|
||||
this.flowLogService = flowLogService;
|
||||
this.ec2Service = ec2Service;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,16 +67,31 @@ public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
// An update re-invokes provision with the prior physical id. Creating unconditionally
|
||||
// would leave a second flow log on every update and orphan the first, which then outlives
|
||||
// the stack, since delete only knows the id recorded last.
|
||||
String region = ctx.region();
|
||||
Map<String, String> desiredTags = ctx.resolveTags(props, "Tags");
|
||||
// Every tag rule CreateTags applies (reserved keys, lengths, the count limit) is checked
|
||||
// before anything is created, so a bad tag fails the resource before a log exists.
|
||||
Ec2Service.validateNewResourceTags(Ec2Tags.validatedTags(desiredTags));
|
||||
// The ownership record this deployment replaces, kept in the rollback snapshot, since the
|
||||
// rollback walker keeps the attempted resource's attributes.
|
||||
String priorTagKeys = r.getAttributes().get(TAG_KEYS_ATTR);
|
||||
String existingId = r.getPhysicalId();
|
||||
if (existingId != null && !existingId.isBlank()) {
|
||||
FlowLog existing = flowLogService.describeFlowLogs(ctx.region(), List.of(existingId))
|
||||
.stream().findFirst().orElse(null);
|
||||
if (existing != null) {
|
||||
// Every property except Tags is createOnly, so there is nothing to modify on the
|
||||
// reused log and a change to any of them is a replacement. Reusing regardless would
|
||||
// Every property except Tags is createOnly, so only the tags change on the reused
|
||||
// log and a change to any other property is a replacement. Reusing regardless would
|
||||
// report the stack complete while DescribeFlowLogs kept serving the old config.
|
||||
requireUnchanged(existing, props, ctx);
|
||||
r.getAttributes().put("Id", existingId);
|
||||
// Only the keys the last deployment wrote are the stack's to remove.
|
||||
Set<String> owned = ownedTagKeys(r, ctx);
|
||||
CfnRollback.guarded(r, ctx, false, () -> { }, () -> undoInPlace(r), () -> {
|
||||
snapshotTags(r, region, existingId, owned, desiredTags, priorTagKeys);
|
||||
Ec2Tags.reconcileOwned(ec2Service, region, existingId, owned, desiredTags);
|
||||
});
|
||||
recordTagKeys(r, desiredTags);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -67,8 +106,133 @@ public class Ec2FlowLogCfnProvisioner implements CfnResourceProvisioner {
|
||||
props != null && props.hasNonNull("MaxAggregationInterval")
|
||||
? props.get("MaxAggregationInterval").asInt()
|
||||
: DEFAULT_MAX_AGGREGATION_INTERVAL);
|
||||
r.setPhysicalId(fl.getFlowLogId());
|
||||
r.getAttributes().put("Id", fl.getFlowLogId());
|
||||
String flowLogId = fl.getFlowLogId();
|
||||
r.setPhysicalId(flowLogId);
|
||||
r.getAttributes().put("Id", flowLogId);
|
||||
// A tag write that still fails deletes the new log rather than leaving it outside the stack.
|
||||
CfnRollback.guarded(r, ctx, true, () -> flowLogService.deleteFlowLogs(region, List.of(flowLogId)),
|
||||
() -> { }, () -> Ec2Tags.reconcile(ec2Service, region, flowLogId, desiredTags));
|
||||
recordTagKeys(r, desiredTags);
|
||||
if (ctx.isUpdate()) {
|
||||
// The stack's log was removed outside it, so a rollback deletes this one and names the
|
||||
// prior id again rather than keeping the failed update's log.
|
||||
CfnRollback.recordCreatedByUpdate(r, ctx, SNAPSHOT_ATTR);
|
||||
ObjectNode created = (ObjectNode) CfnRollback.updateSnapshot(r, SNAPSHOT_ATTR);
|
||||
created.put("tagKeys", priorTagKeys);
|
||||
r.getAttributes().put(SNAPSHOT_ATTR, created.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(SNAPSHOT_ATTR);
|
||||
return UpdateCleanupResult.notApplicable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearUpdate(StackResource resource) {
|
||||
resource.getAttributes().remove(SNAPSHOT_ATTR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undoes what the failed update recorded: deletes a log it recreated, or puts back the tag keys
|
||||
* it touched on the reused one. Without a snapshot the update changed nothing.
|
||||
*/
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
JsonNode snapshot = CfnRollback.updateSnapshot(resource, SNAPSHOT_ATTR);
|
||||
if (snapshot != null && CfnRollback.undoCreatedByUpdate(resource, snapshot, SNAPSHOT_ATTR, "Id",
|
||||
(id, region) -> flowLogService.deleteFlowLogs(region, List.of(id)))) {
|
||||
restoreTagKeys(resource, snapshot);
|
||||
return true;
|
||||
}
|
||||
undoInPlace(resource);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Puts back the ownership record the snapshot carries, or its absence. */
|
||||
private static void restoreTagKeys(StackResource r, JsonNode snapshot) {
|
||||
String tagKeys = CfnRollback.snapshotText(snapshot, "tagKeys");
|
||||
if (tagKeys == null) {
|
||||
r.getAttributes().remove(TAG_KEYS_ATTR);
|
||||
} else {
|
||||
r.getAttributes().put(TAG_KEYS_ATTR, tagKeys);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys the last deployment recorded in {@link #TAG_KEYS_ATTR}. A log provisioned before
|
||||
* that was recorded falls back to the previous template's keys, resolved as well as they still
|
||||
* can be.
|
||||
*/
|
||||
private Set<String> ownedTagKeys(StackResource r, ProvisionContext ctx) {
|
||||
String recorded = r.getAttributes().get(TAG_KEYS_ATTR);
|
||||
if (recorded == null) {
|
||||
return ctx.resolveTags(ctx.priorProperties(), "Tags").keySet();
|
||||
}
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
try {
|
||||
MAPPER.readTree(recorded).forEach(key -> keys.add(key.asText()));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Unreadable tag keys on " + r.getLogicalId(), e);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
private static void recordTagKeys(StackResource r, Map<String, String> desiredTags) {
|
||||
ArrayNode keys = MAPPER.createArrayNode();
|
||||
desiredTags.keySet().forEach(keys::add);
|
||||
r.getAttributes().put(TAG_KEYS_ATTR, keys.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Records the values the touched keys (the owned ones and the declared ones) had before the
|
||||
* update changes them, with the ownership record it replaces, only when a value or the owned
|
||||
* key set is about to change, so an unchanged reuse leaves no snapshot. Tags outside that set
|
||||
* are never written, so they are not recorded either.
|
||||
*/
|
||||
private void snapshotTags(StackResource r, String region, String flowLogId, Set<String> owned,
|
||||
Map<String, String> desiredTags, String priorTagKeys) {
|
||||
Map<String, String> currentTags = Ec2Tags.current(ec2Service, region, flowLogId);
|
||||
Set<String> touched = new LinkedHashSet<>(owned);
|
||||
touched.addAll(desiredTags.keySet());
|
||||
// A change of ownership alone (taking over a key that already holds the declared value)
|
||||
// still needs the record put back, so it counts as a change too.
|
||||
boolean changes = !owned.equals(desiredTags.keySet())
|
||||
|| touched.stream().anyMatch(key -> desiredTags.containsKey(key)
|
||||
? !desiredTags.get(key).equals(currentTags.get(key))
|
||||
: currentTags.containsKey(key));
|
||||
if (!changes) {
|
||||
return;
|
||||
}
|
||||
ObjectNode prior = MAPPER.createObjectNode();
|
||||
prior.put("region", region);
|
||||
prior.put("flowLogId", flowLogId);
|
||||
prior.put("tagKeys", priorTagKeys);
|
||||
touched.forEach(prior.putArray("touched")::add);
|
||||
ObjectNode tags = prior.putObject("tags");
|
||||
touched.stream().filter(currentTags::containsKey).forEach(key -> tags.put(key, currentTags.get(key)));
|
||||
r.getAttributes().put(SNAPSHOT_ATTR, prior.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the touched keys of a reused log back from the snapshot: a key it had gets its value
|
||||
* again, and a key the update added is removed. The ownership record goes back too. The snapshot is removed only after the restore
|
||||
* succeeds, so a failed one keeps it for the rollback walker's retry.
|
||||
*/
|
||||
private void undoInPlace(StackResource r) {
|
||||
JsonNode prior = CfnRollback.updateSnapshot(r, SNAPSHOT_ATTR);
|
||||
if (prior == null) {
|
||||
return;
|
||||
}
|
||||
Set<String> touched = new LinkedHashSet<>();
|
||||
prior.path("touched").forEach(key -> touched.add(key.asText()));
|
||||
Map<String, String> priorTags = new LinkedHashMap<>();
|
||||
prior.path("tags").properties().forEach(f -> priorTags.put(f.getKey(), f.getValue().asText()));
|
||||
Ec2Tags.reconcileOwned(ec2Service, prior.path("region").asText(), prior.path("flowLogId").asText(),
|
||||
touched, priorTags);
|
||||
restoreTagKeys(r, prior);
|
||||
r.getAttributes().remove(SNAPSHOT_ATTR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-2
@@ -8,11 +8,13 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Tag reconciliation shared by the EC2 provisioners ({@code Ec2InstanceCfnProvisioner},
|
||||
* {@code Ec2SecurityGroupCfnProvisioner} and {@code Ec2VpcCfnProvisioner}). They drive it from the
|
||||
* desired tags resolved via {@link ProvisionContext#resolveTags}; the reconcile itself is identical.
|
||||
* {@code Ec2SecurityGroupCfnProvisioner}, {@code Ec2VpcCfnProvisioner} and
|
||||
* {@code Ec2FlowLogCfnProvisioner}). They drive it from the desired tags resolved via
|
||||
* {@link ProvisionContext#resolveTags}; the reconcile itself is identical.
|
||||
*/
|
||||
final class Ec2Tags {
|
||||
|
||||
@@ -66,4 +68,28 @@ final class Ec2Tags {
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Brings the keys a template owns to {@code desired} and leaves every other tag alone, as AWS
|
||||
* keeps a tag added outside the stack: removes the {@code owned} keys {@code desired} no longer
|
||||
* declares, then creates or overwrites the declared ones. {@code owned} is the key set the
|
||||
* previous template declared, or the keys an update touched when a rollback restores them.
|
||||
*/
|
||||
static void reconcileOwned(Ec2Service ec2Service, String region, String resourceId, Set<String> owned,
|
||||
Map<String, String> desired) {
|
||||
List<Tag> add = validatedTags(desired);
|
||||
Map<String, String> current = current(ec2Service, region, resourceId);
|
||||
List<Tag> remove = new ArrayList<>();
|
||||
for (String key : owned) {
|
||||
if (!desired.containsKey(key) && current.containsKey(key)) {
|
||||
remove.add(new Tag(key, current.get(key)));
|
||||
}
|
||||
}
|
||||
if (!remove.isEmpty()) {
|
||||
ec2Service.deleteTags(region, List.of(resourceId), remove);
|
||||
}
|
||||
if (!add.isEmpty()) {
|
||||
ec2Service.createTags(region, List.of(resourceId), add);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user