fix(ecr): give resource-tag conditions the tags of the repository a request names (#8676)

The generated request resources give ECR's repository type the
aws:ResourceTag/ and ecr:ResourceTag/ keys, but ECR had no
StoredResourceArns, so a condition on a repository's tags saw none.
EcrStoredResourceArns now answers from EcrService.storedTags, which
reads the repository stored under the ARN in the account and Region it
names, a pre-multi-account one of the default account in place, matched
on the whole ARN. Tag writes now replace the map whole under one lock,
filled one entry at a time so the listed order holds, and the field is
volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 19:25:37 -04:00
1 parent 895981ac8d
commit b07c49082e
7 files changed
+141 -15

No files matched your search

@@ -338,6 +338,25 @@ class EcrServiceTest {
assertEquals("platform", repo.getTags().get("team"));
}
@Test
void storedTagsAreTheTagsOfTheRepositoryTheArnNamesAndTaggingPutsANewMapInPlaceOfTheHeldOne() {
String arn = service.createRepository(REPO, null, null, null, null, null, Map.of("team", "data"), REGION)
.getRepositoryArn();
Map<String, String> held = service.listTagsForResource(REPO, null, REGION);
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertEquals(Map.of(), service.storedTags(arn + "-other"));
assertEquals(Map.of(), service.storedTags(arn.replace(ACCOUNT, "111111111111")));
assertEquals(Map.of(), service.storedTags(arn.replace(REGION, "us-west-2")));
assertEquals(Map.of(), service.storedTags(arn.replace("arn:aws:", "arn:aws-cn:")));
service.tagResource(REPO, null, Map.of("stage", "prod"), REGION);
service.untagResource(REPO, null, List.of("team"), REGION);
assertEquals(Map.of("stage", "prod"), service.storedTags(arn));
assertEquals(Map.of("team", "data"), held);
}
@Test
void immutableRepositoryIsMarkedForDataPlaneTagProtection() {
service.createRepository(REPO, null, "IMMUTABLE", null, null, null, null, REGION);
@@ -3,6 +3,7 @@ package io.floci.aws.services.iam;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import io.floci.aws.services.ecr.registry.EcrRegistryManager;
import io.floci.aws.services.redshift.model.Endpoint;
import io.floci.aws.services.redshiftserverless.RedshiftServerlessRuntime;
import io.floci.aws.testing.AwsTestClients;
@@ -94,6 +95,10 @@ class IamRequestResourceEnforcementIntegrationTest {
@InjectMock
RedshiftServerlessRuntime runtime;
/** ECR's backing registry is not needed to check a request on a repository's metadata, so none starts. */
@InjectMock
EcrRegistryManager ecrRegistry;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -636,6 +641,39 @@ class IamRequestResourceEnforcementIntegrationTest {
+ "\",\"tags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}").post("/").then(), "ecr:TagResource");
}
/**
* An {@code ecr:ResourceTag} or {@code aws:ResourceTag} condition reads the tags ECR holds on the repository a
* request names, so an allow on one team's tag lets the caller read that team's repository and not another's.
*/
@Test
void anEcrResourceTagConditionReadsTheTagsOfTheRepository() {
String tagged = ecrRepository("res-tag-repo-" + suffix(), "data");
String other = ecrRepository("res-tag-repo-" + suffix(), "web");
String arns = "arn:aws:ecr:" + REGION + ":" + ACCOUNT + ":repository/";
User user = user("res-ecr-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"ecr:DescribeRepositories\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"ecr:ResourceTag/team\":\"data\"}}},"
+ "{\"Effect\":\"Allow\",\"Action\":\"ecr:ListTagsForResource\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
describeRepositories(user, tagged).statusCode(200);
refusedJson(describeRepositories(user, other), "ecr:DescribeRepositories");
send(user, "ecr", "AmazonEC2ContainerRegistry_V20150921.ListTagsForResource", "1.1",
"{\"resourceArn\":\"" + arns + tagged + "\"}").statusCode(200);
refusedJson(send(user, "ecr", "AmazonEC2ContainerRegistry_V20150921.ListTagsForResource", "1.1",
"{\"resourceArn\":\"" + arns + other + "\"}"), "ecr:ListTagsForResource");
}
/** An ECR repository of the name, tagged with the team, deleted afterwards. */
private String ecrRepository(String name, String team) {
adminJson("ecr", "AmazonEC2ContainerRegistry_V20150921.CreateRepository").body("{\"repositoryName\":\""
+ name + "\",\"tags\":[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}").post("/").then()
.statusCode(200);
cleanup.register(() -> adminJson("ecr", "AmazonEC2ContainerRegistry_V20150921.DeleteRepository")
.body("{\"repositoryName\":\"" + name + "\",\"force\":true}").post("/"));
return name;
}
@Test
void aDeliveryStreamIsCheckedOnTheArnItsNameGives() {
String name = "res-stream-" + suffix();