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

@@ -34,9 +34,9 @@ import java.util.Optional;
* versions and executions, and an activity; CodeBuild, for the project a build belongs to and the tags of a project or
* a report group; Athena, for the workgroup and the data catalog a request naming none uses and the tags of a
* workgroup or a data catalog; Redshift Serverless, for the tags of a namespace, a workgroup or a snapshot; DynamoDB,
* for the tags of a table and its indexes); 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>
* for the tags of a table and its indexes; ECR, for the tags of a repository); 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
@@ -71,6 +71,7 @@ public class EcrService implements ResourceProvider, Resettable {
"(?:[a-z0-9]+(?:(?:\\.|_|__|-+)[a-z0-9]+)*(?:/[a-z0-9]+"
+ "(?:(?:\\.|_|__|-+)[a-z0-9]+)*)*/?|ROOT)");
private static final Pattern REGISTRY_ID = Pattern.compile("[0-9]{12}");
private static final String REPOSITORY_RESOURCE = "repository/";
private static final Pattern LAYER_DIGEST = Pattern.compile("[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+");
private static final ObjectMapper MAPPER = new ObjectMapper();
/** ECR's imageTag rule, which the service model bounds at 300 characters. */
@@ -106,6 +107,9 @@ public class EcrService implements ResourceProvider, Resettable {
private final EcrLayerUploads layerUploads = new EcrLayerUploads(this::discardUpload);
private final Set<String> inFlightImmutableTags = ConcurrentHashMap.newKeySet();
private final KeyedWriteLocks<String> repositoryWriteLocks = new KeyedWriteLocks<>();
// Held while a repository's tags are copied, changed and put in place of the map it held, which is never changed.
// The copy is put one entry at a time, as the map always was, so ListTagsForResource keeps the order it listed.
private final Object tagLock = new Object();
@Inject
public EcrService(StorageFactory factory,
@@ -1676,21 +1680,29 @@ public class EcrService implements ResourceProvider, Resettable {
}
public void tagResource(String repoName, String registryId, Map<String, String> tags, String region) {
Repository repo = requireRepo(repoName, registryId, region);
if (tags != null) {
repo.getTags().putAll(tags);
synchronized (tagLock) {
Repository repo = requireRepo(repoName, registryId, region);
if (tags != null) {
Map<String, String> updated = new HashMap<>();
repo.getTags().forEach(updated::put);
tags.forEach(updated::put);
repo.setTags(updated);
}
repoStore.put(key(region, repo.getRegistryId(), repoName), repo);
}
repoStore.put(key(region, repo.getRegistryId(), repoName), repo);
}
public void untagResource(String repoName, String registryId, List<String> tagKeys, String region) {
Repository repo = requireRepo(repoName, registryId, region);
if (tagKeys != null) {
for (String k : tagKeys) {
repo.getTags().remove(k);
synchronized (tagLock) {
Repository repo = requireRepo(repoName, registryId, region);
if (tagKeys != null) {
Map<String, String> updated = new HashMap<>();
repo.getTags().forEach(updated::put);
tagKeys.forEach(updated::remove);
repo.setTags(updated);
}
repoStore.put(key(region, repo.getRegistryId(), repoName), repo);
}
repoStore.put(key(region, repo.getRegistryId(), repoName), repo);
}
public Map<String, String> listTagsForResource(String repoName, String registryId, String region) {
@@ -1698,6 +1710,26 @@ public class EcrService implements ResourceProvider, Resettable {
return repo.getTags();
}
/**
* The tags of the repository stored under {@code arn} in the account and Region it names, a pre-multi-account one
* of the default account read in place, matched on the whole ARN; empty for an ARN naming nothing stored.
*/
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "ecr")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String resource = parsed.resource();
if (!resource.startsWith(REPOSITORY_RESOURCE)) {
return Map.of();
}
return AccountAwareStorageBackend.readInAccount(repoStore, parsed.accountId(),
key(parsed.region(), parsed.accountId(), resource.substring(REPOSITORY_RESOURCE.length())))
.filter(repo -> arn.equals(repo.getRepositoryArn()))
.map(Repository::getTags)
.orElse(Map.of());
}
public Repository putLifecyclePolicy(String repoName, String registryId, String policyText, String region) {
validateLifecyclePolicyDocument(policyText);
Repository repo = requireRepo(repoName, registryId, region);
@@ -0,0 +1,37 @@
package io.floci.aws.services.ecr;
import io.floci.aws.core.common.StoredResourceArns;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/** What IAM asks ECR of the resources a request names: the tags of the repository an ARN names. */
@ApplicationScoped
public class EcrStoredResourceArns implements StoredResourceArns {
private final EcrService ecr;
@Inject
public EcrStoredResourceArns(EcrService ecr) {
this.ecr = ecr;
}
@Override
public String namespace() {
return "ecr";
}
/** ECR mints no id into an ARN a request names by its name, so there is nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
@Override
public Map<String, String> storedTags(String arn) {
return ecr.storedTags(arn);
}
}
@@ -25,7 +25,7 @@ public class Repository {
private String kmsKey;
private String lifecyclePolicyText;
private String repositoryPolicyText;
private Map<String, String> tags = new HashMap<>();
private volatile Map<String, String> tags = new HashMap<>();
public Repository() {}