fix(cloudformation): give resource-tag conditions the tags of the stack, stack set or change set a request names (#8527)

The generated request resources give CloudFormation's stack, stackset and
changeset types aws:ResourceTag/ keys, but CloudFormationStoredResourceArns
did not answer storedTags, so a condition on their tags saw none. It now
answers from CloudFormationService.storedTags, which reads the live stack, or
the change set, under the ARN in the account and Region it names, and
StackSetService.storedTags, which reads the stack set under the ARN in the
account it names, a pre-multi-account one of the default account in place;
each matched on the whole ARN, reading only.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 20:12:12 -04:00
1 parent bcca1bb8d3
commit b46507dbec
8 files changed
+148 -8

No files matched your search

@@ -26,10 +26,11 @@ import java.util.Optional;
* permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks,
* invitations and shares of other accounts they act on; Secrets Manager, for the secret a name or an ARN names,
* random suffix included, and its tags; EventBridge Scheduler, for its default schedule group and its groups' tags;
* CloudFormation, for the stack or the stack set a name names, whose id it minted; S3 Tables, for the table a request
* names by its table bucket, namespace and name, and the tags of a table bucket or a table); looked up lazily by
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
* CloudFormation, for the stack or the stack set a name names, whose id it minted, and the tags of a stack, a stack set
* or a change set; S3 Tables, for the table a request names by its table bucket, namespace and name, and the tags of a
* table bucket or a table); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver}
* through {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on
* that service directly.</p>
*
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
@@ -425,6 +425,34 @@ public class CloudFormationService implements ResourceProvider {
return Optional.ofNullable(resolveStack(stackNameOrArn, region, currentAccount())).map(Stack::getStackId);
}
/**
* The tags of the live stack, or of the change set, stored under {@code arn} in the account and Region the ARN
* names, by key, which IAM gives a resource-tag condition; empty when there is none. Reads only, matching the
* stack's id or the change set's on the whole ARN.
*/
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "cloudformation")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String resource = parsed.resource();
if (resource.startsWith("stack/")) {
Stack stack = resolveStack(arn, parsed.region(), parsed.accountId());
return stack != null && arn.equals(stack.getStackId()) ? stack.tagsSnapshot() : Map.of();
}
if (!resource.startsWith("changeSet/")) {
return Map.of();
}
try {
Stack stack = findStackForChangeSetArn(arn, parsed.region(), parsed.accountId());
ChangeSet changeSet = stack == null ? null
: stack.getChangeSets().get(resolveChangeSetName(arn, parsed.region(), parsed.accountId()));
return changeSet == null || changeSet.getTags() == null ? Map.of() : Map.copyOf(changeSet.getTags());
} catch (AwsException expected) {
return Map.of();
}
}
/**
* The stack's current parameter values, or an empty map if it does not exist (yet). Used to
* resolve {@code UsePreviousValue} on an update before the stack lookup that
@@ -5,11 +5,13 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* What IAM asks CloudFormation of the resources a request names: the ARN of the stack or the stack set a name names,
* whose id CloudFormation minted, so a request naming one by its name is checked on the ARN AWS checks.
* whose id CloudFormation minted, so a request naming one by its name is checked on the ARN AWS checks; and the tags
* of a stack, a stack set or a change set, which a resource-tag condition reads.
*/
@ApplicationScoped
public class CloudFormationStoredResourceArns implements StoredResourceArns {
@@ -39,4 +41,10 @@ public class CloudFormationStoredResourceArns implements StoredResourceArns {
default -> Optional.empty();
};
}
/** The tags of the stack, stack set or change set {@code arn} names, which a resource-tag condition reads. */
@Override
public Map<String, String> storedTags(String arn) {
return arn.contains(":stackset/") ? stackSets.storedTags(arn) : stacks.storedTags(arn);
}
}
@@ -7,6 +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.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.cloudformation.model.ChangeSet;
@@ -66,6 +67,7 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
private static final String INSTANCE_CHANGE_SET = "stackset-instance";
private static final String UPDATE_CHANGE_SET = "stackset-update";
private static final String ORGANIZATIONS_ACCESS_KEY = "organizations-access";
private static final String STACK_SET_RESOURCE = "stackset/";
private static final int STACK_SET_LIMIT = 1000;
private static final Pattern STACK_SET_NAME = Pattern.compile("[A-Za-z][A-Za-z0-9-]{0,127}");
private static final Pattern OPERATION_ID = Pattern.compile("[A-Za-z0-9][-A-Za-z0-9]{0,127}");
@@ -211,7 +213,7 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
/** The stack set's ARN, {@code stackset/<name>:<id>} in the request's Region, as AWS gives its StackSetARN. */
/** The stack set's ARN in the Region given, the one the request naming it is in, whatever context runs it. */
public String stackSetArn(StackSet stackSet, String region) {
return regionResolver.buildArn("cloudformation", region, "stackset/" + stackSet.getStackSetId());
return regionResolver.buildArn("cloudformation", region, STACK_SET_RESOURCE + stackSet.getStackSetId());
}
/**
@@ -222,6 +224,31 @@ public class StackSetService implements PassedRoleLookup, AccountPlacementListen
return name == null ? Optional.empty() : stackSets.get(name).map(stackSet -> stackSetArn(stackSet, region));
}
/**
* 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.
*/
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "cloudformation")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String id = parsed.resource().startsWith(STACK_SET_RESOURCE)
? parsed.resource().substring(STACK_SET_RESOURCE.length()) : "";
if (id.indexOf(':') < 1) {
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()))
.<Map<String, String>>map(stackSet -> Map.copyOf(stackSet.getTags()))
.orElse(Map.of());
}
/** The StackSet's permission model, which says whether an update naming no administration role passes the default one. */
@Override
public Optional<String> stackSetPermissionModel(String stackSetName) {