fix(applicationautoscaling): give resource-tag conditions the tags of the target a request names (#8726)

The generated request resources give Application Auto Scaling's
ScalableTarget type the aws:ResourceTag/ key, but its service answered
no tags, so a condition on a scalable target's tags saw none. Its
storedTags now reads the target whose ARN is the ARN, among the targets
of the account and Region it names, a pre-multi-account one of the
default account included, as the handlers find it. Tag changes already
store a copy of the target under its lock; the tags field is now
volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 06:53:47 -04:00
1 parent 151cb586f9
commit 65c6440b8f
6 files changed
+114 -8

No files matched your search

+2 -2
View File
@@ -3201,8 +3201,8 @@ floci populates:
snapshot schedules, event subscriptions and integrations, SSM for its parameters, documents and associations,
EMR for its clusters, CloudFront for every resource it tags, by its ARN, IAM Identity Center for its instances,
permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares,
Payment Cryptography for its keys, and Resource Explorer for its indexes and views. Tag keys differing only in
case are one condition key holding every value.
Payment Cryptography for its keys, Resource Explorer for its indexes and views, and Application Auto Scaling for
its scalable targets. Tag keys differing only in case are one condition key holding every value.
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
@@ -20,11 +20,11 @@ import java.util.Optional;
* points at.
*
* <p>Implemented by the service that owns such resources or whose requests name them (EventBridge, for its
* connections and API destinations; Application Auto Scaling, for its scalable targets; Redshift Data, for the
* Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for
* the key an alias names and the tags of a key; EMR, for the clusters of its instance groups and a tag call's
* resource and the tags of a cluster; IAM Identity Center, for the accounts its permission sets are provisioned to
* and the tags of an instance,
* connections and API destinations; Application Auto Scaling, for its scalable targets and their tags; Redshift
* Data, for the Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment
* Cryptography, for the key an alias names and the tags of a key; EMR, for the clusters of its instance groups and a
* tag call's resource and the tags of a cluster; IAM Identity Center, for the accounts its permission sets are
* provisioned to and the tags of an instance,
* a permission set, an application or a trusted token issuer; Resource Explorer, for its default view and the tags
* of an index or a view; OAM and RAM, for the sinks, invitations and shares of other accounts they act on, OAM for
* the tags of a sink or a link, and RAM for the tags of a resource share;
@@ -1,6 +1,7 @@
package io.floci.aws.services.applicationautoscaling;
import com.fasterxml.jackson.core.type.TypeReference;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
@@ -38,6 +39,7 @@ import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -1004,6 +1006,42 @@ public class ApplicationAutoScalingService implements StoredResourceArns {
return findTarget(region, names.get(0), names.get(1), names.get(2)).map(ScalableTarget::getScalableTargetArn);
}
/**
* The tags held on the scalable target whose ARN is {@code arn}, read in place where its handlers find it: among
* the targets of the account and Region the ARN names, a pre-multi-account one of the default account included,
* matched on the whole ARN. Tagging stores a copy of the target, so the read is of one whole set. Empty for an ARN
* naming no target.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "application-autoscaling")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String account = parsed.accountId();
if (account.isEmpty()) {
return Map.of();
}
String prefix = parsed.region() + "::";
List<ScalableTarget> held;
if (targets instanceof AccountAwareStorageBackend<ScalableTarget> aware) {
String scoped = AccountAwareStorageBackend.rawKey(account, prefix);
boolean legacyToo = account.equals(aware.defaultAccountId());
held = aware.scanRaw(key -> key.startsWith(scoped)
|| (legacyToo && !AccountAwareStorageBackend.hasAccountPrefix(key) && key.startsWith(prefix)));
} else {
held = targets.scan(key -> key.startsWith(prefix));
}
Map<String, String> tags = new LinkedHashMap<>();
held.stream().filter(target -> arn.equals(target.getScalableTargetArn())).findFirst()
.ifPresent(target -> target.getTags().forEach((key, value) -> {
if (key != null && value != null) {
tags.put(key, value);
}
}));
return tags;
}
/**
* Looked up by {@link ScalingPolicyAlarmActionHandler} when an alarm's AlarmActions references
* a scaling-policy ARN, and by the CloudFormation provisioner, whose physical id for a policy
@@ -29,7 +29,7 @@ public class ScalableTarget {
private String scalableTargetArn;
private double creationTime;
private SuspendedState suspendedState = new SuspendedState();
private Map<String, String> tags = new HashMap<>();
private volatile Map<String, String> tags = new HashMap<>();
public ScalableTarget() {}
@@ -3,6 +3,7 @@ package io.floci.aws.services.applicationautoscaling;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.applicationautoscaling.model.PredefinedMetricSpecification;
import io.floci.aws.services.applicationautoscaling.model.ScalableTarget;
@@ -79,6 +80,48 @@ class ApplicationAutoScalingServiceTest {
null, null, Map.of("Environment", "dev"), REGION);
}
/**
* IAM's tag lookup reads the tags held on the scalable target whose ARN it is given, in that ARN's Region, as
* tagging and an update's tags leave them; nothing for another Region's ARN or an unknown target's.
*/
@Test
void storedTagsAreTheTagsOfTheScalableTargetTheArnNames() {
String arn = register().getScalableTargetArn();
service.tagResource(arn, Map.of("team", "data"), REGION);
service.untagResource(arn, List.of("Environment"), REGION);
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertEquals(Map.of(), service.storedTags(arn.replace(":" + REGION + ":", ":eu-west-1:")));
assertEquals(Map.of(), service.storedTags(arn + "0"));
assertEquals(Map.of(), service.storedTags("arn:aws:s3:::bucket"));
}
/**
* A target stored before storage was account-aware is read in place for the default account, its ECS resource id
* slashes and all, and never migrated; another account's ARN reads nothing.
*/
@Test
void storedTagsReadsAPreMultiAccountTargetOfTheDefaultAccountInPlace() {
InMemoryStorage<String, ScalableTarget> raw = new InMemoryStorage<>();
ApplicationAutoScalingService legacy = new ApplicationAutoScalingService(
new AccountAwareStorageBackend<>(raw, null, "000000000000"), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"),
cloudWatch, mock(EcsCapacityAdjuster.class));
String arn = "arn:aws:application-autoscaling:" + REGION + ":000000000000:scalable-target/0123456789abcdef";
ScalableTarget target = new ScalableTarget();
target.setServiceNamespace(NAMESPACE);
target.setResourceId(RESOURCE_ID);
target.setScalableDimension(DIMENSION);
target.setScalableTargetArn(arn);
target.setTags(Map.of("team", "data"));
String key = REGION + "::" + NAMESPACE + "::" + DIMENSION + "::" + RESOURCE_ID;
raw.put(key, target);
assertEquals(Map.of("team", "data"), legacy.storedTags(arn));
assertEquals(Map.of(), legacy.storedTags(arn.replace(":000000000000:", ":111122223333:")));
assertTrue(raw.get(key).isPresent());
}
private TargetTrackingConfiguration targetTracking(double value, String metricType, String resourceLabel) {
TargetTrackingConfiguration config = new TargetTrackingConfiguration();
config.setTargetValue(value);
@@ -1589,6 +1589,31 @@ class IamRequestResourceEnforcementIntegrationTest {
}
}
/**
* An {@code aws:ResourceTag} condition reads the tags Application Auto Scaling holds on the scalable target a
* request names, so an allow on one team's tag lets the caller list that team's target's tags and not another's.
*/
@Test
void anApplicationAutoScalingResourceTagConditionReadsTheTagsOfTheScalableTarget() {
String tagged = taggedScalableTarget("data");
String other = taggedScalableTarget("web");
User user = user("res-target-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"application-autoscaling:ListTagsForResource\","
+ "\"Resource\":\"*\",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
scaling(user, "ListTagsForResource", "{\"ResourceARN\":\"" + tagged + "\"}").statusCode(200);
refusedJson(scaling(user, "ListTagsForResource", "{\"ResourceARN\":\"" + other + "\"}"),
"application-autoscaling:ListTagsForResource");
}
/** A scalable target the admin registers and tags with the team; its ARN. */
private String taggedScalableTarget(String team) {
String arn = registerScalableTarget(scalableTarget("service/res-" + suffix() + "/web"));
adminJson("application-autoscaling", "AnyScaleFrontendService.TagResource").body("{\"ResourceARN\":\"" + arn
+ "\",\"Tags\":{\"team\":\"" + team + "\"}}").post("/").then().statusCode(200);
return arn;
}
/** The members naming an ECS service's desired count as a scalable target, an open JSON object. */
private static String scalableTarget(String resourceId) {
return "{\"ServiceNamespace\":\"ecs\",\"ResourceId\":\"" + resourceId + "\","