fix(cloudformation): keep stack sets with their instances and operations in their Region (#8987)

This commit is contained in:
mrfelek authored and GitHub committed 2026-11-19 11:41:49 -04:00
1 parent c2721ca938
commit 573cb9e9b7
12 files changed
+657 -50

No files matched your search

@@ -53,6 +53,18 @@ public final class RegionScopedStorage<V> implements StorageBackend<String, V> {
.or(() -> regionlessHere(key) ? delegate.get(key) : Optional.empty());
}
/**
* The value {@code region}'s view holds under {@code key} in {@code accountId}, whatever the request's account and
* Region, read in place as {@link AccountAwareStorageBackend#readInAccount} reads it: for a lookup that must leave
* the store as it is, as IAM's reads of the tags of the resource an ARN names must. A regionless key is read for
* the default Region only, as {@link #get} reads it there.
*/
public Optional<V> readInAccount(String accountId, String region, String key) {
return AccountAwareStorageBackend.readInAccount(delegate, accountId, prefix(region) + key)
.or(() -> regionlessIn(region, key)
? AccountAwareStorageBackend.readInAccount(delegate, accountId, key) : Optional.empty());
}
@Override
public void delete(String key) {
if (regionlessHere(key)) {
@@ -72,6 +84,24 @@ public final class RegionScopedStorage<V> implements StorageBackend<String, V> {
return values;
}
/** A value of {@link #scanEveryRegion}: the Region whose view holds it, and its key there. */
public record RegionEntry<V>(String region, String key, V value) {}
/**
* The request's account's values of every Region, each with the Region whose view holds it, a regionless key's
* being the default Region's: for a worker that acts on every Region's resources, each in its own Region.
*/
public List<RegionEntry<V>> scanEveryRegion() {
List<RegionEntry<V>> entries = new ArrayList<>();
for (String stored : delegate.keys()) {
boolean prefixed = regionPrefixed(stored);
String region = prefixed ? stored.substring(0, stored.indexOf('/')) : regions.getDefaultRegion();
String key = prefixed ? stored.substring(region.length() + 1) : stored;
delegate.get(stored).ifPresent(value -> entries.add(new RegionEntry<>(region, key, value)));
}
return entries;
}
@Override
public Set<String> keys() {
String prefix = prefix();
@@ -128,19 +158,28 @@ public final class RegionScopedStorage<V> implements StorageBackend<String, V> {
}
private String prefix() {
return regions.getRegion() + "/";
return prefix(regions.getRegion());
}
private static String prefix(String region) {
return region + "/";
}
private boolean defaultRegion() {
return regions.getRegion().equals(regions.getDefaultRegion());
}
/**
* Whether {@code key} may also be stored regionless, as it was before this view, and is read here that way:
* only in the default Region, and only when it doesn't start with a Region, as a stored key of this view does.
*/
private boolean regionlessHere(String key) {
return key != null && defaultRegion() && !regionPrefixed(key);
return regionlessIn(regions.getRegion(), key);
}
/**
* Whether {@code key} may also be stored regionless, as it was before this view, and is read that way in
* {@code region}: only in the default Region, and only when it doesn't start with a Region, as a stored key of this
* view does.
*/
private boolean regionlessIn(String region, String key) {
return key != null && region.equals(regions.getDefaultRegion()) && !regionPrefixed(key);
}
/** Whether {@code key} starts with a Region and a slash, as every key this view stores does. */
@@ -7,7 +7,7 @@ import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.common.SmithyValidationErrors;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.RegionScopedStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.cloudformation.model.ChangeSet;
@@ -53,11 +53,15 @@ import java.util.regex.Pattern;
* <p>A StackSet holds a template and configuration; {@code CreateStackInstances} drives the existing
* single-stack engine ({@link CloudFormationService}) once per target {@code (account, region)},
* provisioning the resources into each target account's namespace. The StackSet, instance and
* operation records themselves live in the administration (caller) account's namespace.
* operation records themselves live in the administration (caller) account's namespace, in the Region the stack
* set was created in: a stack set is Regional, listed and reached only there, as on AWS, and so are its instances,
* operations and automatic deployment targets. Trusted access with Organizations is the organization's, in every
* Region.
*
* <p>A service-managed stack set with automatic deployment follows the accounts of the organization: an
* account created in, invited into or moved under one of its targets gets an instance in every Region
* that target was given, and one no target covers any more loses its instances.
* that target was given, and one no target covers any more loses its instances. That runs in each stack set's own
* Region, whatever Region the Organizations call was sent to.
*/
@ApplicationScoped
public class StackSetService implements PassedRoleLookup, AccountPlacementListener {
@@ -75,7 +79,7 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
private static final Pattern REGION_PATTERN = Pattern.compile("[A-Za-z0-9-]{1,128}");
private final CloudFormationService cfnService;
private final StorageBackend<String, StackSet> stackSets;
private final RegionScopedStorage<StackSet> stackSets;
private final StorageBackend<String, StackInstance> instances;
private final StorageBackend<String, StackSetOperation> operations;
private final StorageBackend<String, String> organizationsAccess;
@@ -98,19 +102,25 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
this(cfnService, storageFactory, null, new RegionResolver("us-east-1", "000000000000")); // partition-literal: test-shaped constructor default
}
/**
* A stack set is Regional, as on AWS, so the stores of stack sets, instances, operations and automatic deployment
* targets are the request's Region's view ({@link RegionScopedStorage}); a record stored before, without a
* Region, is the default Region's. Trusted access with Organizations is the organization's, so its store is not.
*/
@Inject
public StackSetService(CloudFormationService cfnService, StorageFactory storageFactory,
OrganizationsService organizationsService, RegionResolver regionResolver) {
this.cfnService = cfnService;
this.stackSets = storageFactory.create("cloudformation", "cloudformation-stacksets.json",
new TypeReference<Map<String, StackSet>>() {});
this.instances = storageFactory.create("cloudformation", "cloudformation-stackset-instances.json",
new TypeReference<Map<String, StackInstance>>() {});
this.operations = storageFactory.create("cloudformation", "cloudformation-stackset-operations.json",
new TypeReference<Map<String, StackSetOperation>>() {});
this.stackSets = new RegionScopedStorage<>(storageFactory, regionResolver, "cloudformation",
"cloudformation-stacksets.json", new TypeReference<Map<String, StackSet>>() {});
this.instances = new RegionScopedStorage<>(storageFactory, regionResolver, "cloudformation",
"cloudformation-stackset-instances.json", new TypeReference<Map<String, StackInstance>>() {});
this.operations = new RegionScopedStorage<>(storageFactory, regionResolver, "cloudformation",
"cloudformation-stackset-operations.json", new TypeReference<Map<String, StackSetOperation>>() {});
this.organizationsAccess = storageFactory.create("cloudformation", "cloudformation-organizations-access.json",
new TypeReference<Map<String, String>>() {});
this.autoDeploymentTargets = storageFactory.create("cloudformation", "cloudformation-stackset-auto-targets.json",
this.autoDeploymentTargets = new RegionScopedStorage<>(storageFactory, regionResolver, "cloudformation",
"cloudformation-stackset-auto-targets.json",
new TypeReference<Map<String, StackSetAutoDeploymentTarget>>() {});
this.organizationsService = organizationsService;
this.regionResolver = regionResolver;
@@ -225,9 +235,9 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
}
/**
* The tags of the stack set stored under {@code arn} in the account the ARN names, a pre-multi-account one of the
* default account read in place, by key, which IAM gives a resource-tag condition; empty when there is none. Reads
* only, matching the stack set's id, {@code <name>:<id>}, on the ARN.
* The tags of the stack set stored under {@code arn} in the account and Region the ARN names, a pre-multi-account
* one of the default account read in place, by key, which IAM gives a resource-tag condition; empty when there is
* none. Reads only, matching the stack set's id, {@code <name>:<id>}, on the ARN.
*/
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "cloudformation")) {
@@ -240,11 +250,8 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
return Map.of();
}
String name = id.substring(0, id.indexOf(':'));
String account = parsed.accountId();
Optional<StackSet> stored = stackSets instanceof AccountAwareStorageBackend<StackSet> aware
? aware.getForAccount(account, name).or(() -> aware.getLegacyForAccount(account, name))
: stackSets.get(name);
return stored.filter(stackSet -> id.equals(stackSet.getStackSetId()))
return stackSets.readInAccount(parsed.accountId(), parsed.region(), name)
.filter(stackSet -> id.equals(stackSet.getStackSetId()))
.<Map<String, String>>map(stackSet -> Map.copyOf(stackSet.getTags()))
.orElse(Map.of());
}
@@ -524,20 +531,42 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
() -> reconcileAccountPlacement(managementAccountId, accountId)));
}
/**
* Deploys to or removes from {@code accountId} the instances of every automatically deploying stack set of the
* management account, in every Region, each in its own Region, where its instances and operations are.
*/
private synchronized void reconcileAccountPlacement(String managementAccountId, String accountId) {
Set<String> ancestors = ancestorsOf(managementAccountId, accountId);
for (StackSet stackSet : stackSets.scan(key -> true)) {
if ("SERVICE_MANAGED".equals(stackSet.getPermissionModel()) && stackSet.isAutoDeploymentEnabled()) {
for (RegionScopedStorage.RegionEntry<StackSet> entry : stackSets.scanEveryRegion()) {
StackSet stackSet = entry.value();
String region = administratorRegion(entry);
if (region != null
&& "SERVICE_MANAGED".equals(stackSet.getPermissionModel()) && stackSet.isAutoDeploymentEnabled()) {
try {
autoDeploy(stackSet, accountId, ancestors);
RequestScopes.runAs(managementAccountId, region, () -> autoDeploy(stackSet, accountId, ancestors));
} catch (RuntimeException e) {
LOG.warnv(e, "Automatic deployment failed for StackSet {0}, account {1}",
stackSet.getStackSetName(), accountId);
LOG.warnv(e, "Automatic deployment failed for StackSet {0} in {1}, account {2}",
stackSet.getStackSetName(), region, accountId);
}
}
}
}
/**
* The Region a stored stack set is reached in, null for a key that isn't one of its own. A stack set of a Region no
* partition publishes, kept with {@code allow-unknown-regions}, reads as the default Region's key
* {@code <Region>/<name>}; a name holds no slash, so that key is the one the store wrote in that Region.
*/
private static String administratorRegion(RegionScopedStorage.RegionEntry<StackSet> entry) {
String name = entry.value().getStackSetName();
if (entry.key().equals(name)) {
return entry.region();
}
String suffix = "/" + name;
return entry.key().endsWith(suffix) && entry.key().length() > suffix.length()
? entry.key().substring(0, entry.key().length() - suffix.length()) : null;
}
@PreDestroy
void stopAccountPlacementExecutor() {
accountPlacementExecutor.shutdownNow();
@@ -998,10 +1027,11 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
}
}
/** The stack set of that name in the request's Region; another Region's is not found, as one that doesn't exist. */
private StackSet getStackSetOrThrow(String name) {
return stackSets.get(name)
.orElseThrow(() -> new AwsException("StackSetNotFoundException",
"StackSet [" + name + "] not found", 404));
"StackSet " + name + " not found", 404));
}
private static String instanceStackName(String stackSetName, String account) {