fix(ecs): give resource-tag conditions the tags of the resource a request names (#8732)

The generated request resources give ECS's cluster, task-definition,
service, capacity-provider, task and container-instance types the
aws:ResourceTag/ and ecs:ResourceTag/ keys, but EcsService answered no
tags, so a condition on an ECS resource's tags saw none. Its storedTags
now reads the resource the ARN names in the request's account, where
every ECS handler reads: a stored one in place under the key its ARN
gives, a record saved before storage was account-aware or before a task
definition's key named its Region included, matched on the whole ARN,
and a task or a container instance from memory. TagResource and
UntagResource changed the resource's map in place without a lock; they
now give it a new map under the service's lock, and the tags fields are
volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 08:22:39 -04:00
1 parent 50276fa533
commit 83c64f5d91
11 files changed
+210 -28

No files matched your search

@@ -106,6 +106,71 @@ class EcsServicePersistenceTest {
"deleted attribute must not reappear after restart");
}
/**
* IAM's tag lookup reads the tags of the cluster, task definition or container instance whose ARN it is given, as
* tag calls leave them and a restart keeps them. A tag call gives the resource a new map rather than changing the
* one a reader already holds, and an ARN of another account or of no resource reads nothing.
*/
@Test
void storedTagsAreTheTagsOfTheResourceTheArnNamesAndTaggingPutsANewMap() {
SharedStorageFactory storage = new SharedStorageFactory();
EcsService first = serviceWithStorage(storage);
String cluster = first.createCluster("tagged", Map.of("team", "web"), REGION).getClusterArn();
String td = first.registerTaskDefinition("tagged", List.of(container("app", "nginx:latest")),
NetworkMode.awsvpc, "256", "512", null, null, List.of("FARGATE"), Map.of("tier", "web"), REGION)
.getTaskDefinitionArn();
String instance = first.registerContainerInstance("tagged", null, List.of(), REGION).getContainerInstanceArn();
Map<String, String> held = first.listTagsForResource(cluster);
first.tagResource(cluster, Map.of("team", "data"));
first.untagResource(td, List.of("tier"));
first.tagResource(instance, Map.of("rack", "a"));
assertEquals(Map.of("team", "web"), held);
assertEquals(Map.of("team", "data"), first.storedTags(cluster));
assertEquals(Map.of(), first.storedTags(td));
assertEquals(Map.of("rack", "a"), first.storedTags(instance));
assertEquals(Map.of("team", "data"), serviceWithStorage(storage).storedTags(cluster));
assertEquals(Map.of(), first.storedTags(cluster.replace(":000000000000:", ":111122223333:")));
assertEquals(Map.of(), first.storedTags(cluster + "0"));
assertEquals(Map.of(), first.storedTags("arn:aws:s3:::tagged"));
}
/**
* A cluster saved before storage was account-aware, and a task definition saved before that and before its key
* named its Region, are read in place for the default account and never migrated; another account's ARN reads
* nothing.
*/
@Test
void storedTagsReadsPreAccountAwareResourcesOfTheDefaultAccountInPlace() {
InMemoryStorage<String, EcsCluster> rawClusters = new InMemoryStorage<>();
InMemoryStorage<String, TaskDefinition> rawDefinitions = new InMemoryStorage<>();
SharedStorageFactory storage = new SharedStorageFactory();
storage.stores.put("ecs-clusters.json", new AccountAwareStorageBackend<>(rawClusters, null, "000000000000"));
storage.stores.put("ecs-task-definitions.json",
new AccountAwareStorageBackend<>(rawDefinitions, null, "000000000000"));
String clusterArn = "arn:aws:ecs:" + REGION + ":000000000000:cluster/legacy";
EcsCluster cluster = new EcsCluster();
cluster.setClusterName("legacy");
cluster.setClusterArn(clusterArn);
cluster.setTags(Map.of("team", "data"));
rawClusters.put(REGION + "::legacy", cluster);
String definitionArn = "arn:aws:ecs:" + REGION + ":000000000000:task-definition/legacy:1";
TaskDefinition definition = new TaskDefinition();
definition.setFamily("legacy");
definition.setRevision(1);
definition.setTaskDefinitionArn(definitionArn);
definition.setTags(Map.of("tier", "web"));
rawDefinitions.put("legacy:1", definition);
EcsService service = serviceWithStorage(storage);
assertEquals(Map.of("team", "data"), service.storedTags(clusterArn));
assertEquals(Map.of("tier", "web"), service.storedTags(definitionArn));
assertEquals(Map.of(), service.storedTags(clusterArn.replace(":000000000000:", ":111122223333:")));
assertTrue(rawClusters.get(REGION + "::legacy").isPresent());
assertTrue(rawDefinitions.get("legacy:1").isPresent());
}
@Test
void persistedServiceGetsItsTasksBackAfterRestartWithoutAnEcsRequest() {
SharedStorageFactory storage = new SharedStorageFactory();
@@ -65,6 +65,8 @@ class IamRequestResourceEnforcementIntegrationTest {
private static final String EVENTS = "arn:aws:events:" + REGION + ":" + ACCOUNT + ":";
private static final String TARGETS = "arn:aws:application-autoscaling:" + REGION + ":" + ACCOUNT + ":scalable-target/";
private static final String REDSHIFT = "arn:aws:redshift:" + REGION + ":" + ACCOUNT + ":";
/** The X-Amz-Target prefix of ECS operations. */
private static final String ECS_API = "AmazonEC2ContainerServiceV20141113.";
private static final String WORKGROUPS = "arn:aws:redshift-serverless:" + REGION + ":" + ACCOUNT + ":workgroup/";
/** The AWS Load Balancer Controller's create, tag-on-create and listener statements (its policy v2.14.0). */
private static final String CONTROLLER_CREATE = "{\"Effect\":\"Allow\",\"Action\":[\"elasticloadbalancing:CreateLoadBalancer\","
@@ -690,6 +692,33 @@ class IamRequestResourceEnforcementIntegrationTest {
"{\"resourceArn\":\"" + arns + other + "\"}"), "ecr:ListTagsForResource");
}
/**
* An {@code ecs:ResourceTag} or {@code aws:ResourceTag} condition reads the tags ECS holds on the cluster a request
* names, by its name or by its ARN, as a tag call leaves them, so an allow on one team's tag lets the caller read
* that team's cluster and not another's.
*/
@Test
void anEcsResourceTagConditionReadsTheTagsOfTheCluster() {
String tagged = ecsCluster("res-tag-cluster-" + suffix(), "web");
String other = ecsCluster("res-tag-cluster-" + suffix(), "web");
String arns = "arn:aws:ecs:" + REGION + ":" + ACCOUNT + ":cluster/";
adminJson("ecs", ECS_API + "TagResource").body("{\"resourceArn\":\"" + arns + tagged
+ "\",\"tags\":[{\"key\":\"team\",\"value\":\"data\"}]}").post("/").then().statusCode(200);
User user = user("res-ecs-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"ecs:DescribeClusters\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"ecs:ResourceTag/team\":\"data\"}}},"
+ "{\"Effect\":\"Allow\",\"Action\":\"ecs:ListTagsForResource\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
send(user, "ecs", ECS_API + "DescribeClusters", "1.1", "{\"clusters\":[\"" + tagged + "\"]}").statusCode(200);
refusedJson(send(user, "ecs", ECS_API + "DescribeClusters", "1.1", "{\"clusters\":[\"" + other + "\"]}"),
"ecs:DescribeClusters");
send(user, "ecs", ECS_API + "ListTagsForResource", "1.1", "{\"resourceArn\":\"" + arns + tagged + "\"}")
.statusCode(200);
refusedJson(send(user, "ecs", ECS_API + "ListTagsForResource", "1.1", "{\"resourceArn\":\"" + arns + other
+ "\"}"), "ecs:ListTagsForResource");
}
/**
* An {@code ssm:resourceTag} or {@code aws:ResourceTag} condition reads the tags SSM holds on the parameter or the
* document a request names, so an allow on one team's tag lets the caller read that team's and not another's.
@@ -736,6 +765,13 @@ class IamRequestResourceEnforcementIntegrationTest {
"{\"repositoryName\":\"" + name + "\",\"force\":true}", name);
}
/** An ECS cluster of the name, tagged with the team, deleted afterwards. */
private String ecsCluster(String name, String team) {
return created("ecs", ECS_API + "CreateCluster", "{\"clusterName\":\"" + name + "\",\"tags\":[{\"key\":"
+ "\"team\",\"value\":\"" + team + "\"}]}", ECS_API + "DeleteCluster", "{\"cluster\":\"" + name + "\"}",
name);
}
/**
* {@code name}, after an administrator creates the resource with the JSON 1.1 {@code create} call and registers
* the {@code delete} call that removes it after the test.