fix(acm): give resource-tag conditions the tags of the certificate a request names (#8737)

The generated request resources give ACM's certificate type the
aws:ResourceTag/ key, but no service answered the acm namespace's
stored tags, so a condition on a certificate's tags saw none. AcmService
now answers it: storedTags reads the certificate stored under the key of
the Region the ARN names, in the request's account, where the handlers
read, in place, and matched on the whole ARN. Tag changes already store
a new map under the service's lock; the tags field is now volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 09:26:43 -04:00
1 parent c6faa73eb7
commit 2fdcbecdf0
6 files changed
+104 -12

No files matched your search

+3 -2
View File
@@ -3202,8 +3202,9 @@ floci populates:
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, Resource Explorer for its indexes and views, Application Auto Scaling for its
scalable targets, and ECS for its clusters, task definitions, services, capacity providers, tasks and container
instances. Tag keys differing only in case are one condition key holding every value.
scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container
instances, and ACM for its certificates. 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
@@ -44,9 +44,9 @@ import java.util.Optional;
* another account an association runs and the tags of a parameter, a document or an association; CloudFront, for the
* key value store a name names and the tags of any resource it tags; ECS, for the revision a task definition
* reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity
* provider, a task or a container instance); 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>
* provider, a task or a container instance; ACM, for the tags of a certificate); 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
@@ -9,9 +9,11 @@ import io.floci.aws.core.common.Digests;
import io.floci.aws.core.common.DomainNames;
import io.floci.aws.core.common.Pem;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.resource.ResourceProvider;
import io.floci.aws.core.resource.SupportedResourceType;
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.acm.model.Certificate;
@@ -64,7 +66,7 @@ import java.util.stream.Collectors;
* @see <a href="https://docs.aws.amazon.com/acm/latest/APIReference/Welcome.html">AWS ACM API Reference</a>
*/
@ApplicationScoped
public class AcmService implements ResourceProvider {
public class AcmService implements ResourceProvider, StoredResourceArns {
private static final Logger LOG = Logger.getLogger(AcmService.class);
private static final int MAX_TAGS = 50;
@@ -572,6 +574,40 @@ public class AcmService implements ResourceProvider {
store.put(regionKey(region, cert.extractCertificateId()), cert);
}
@Override
public String namespace() {
return "acm";
}
/** A request names a certificate by its ARN, whose id ACM minted, so there is nothing to find by name. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* The tags of the certificate whose ARN is {@code arn}, read in place under its key in the Region the ARN names,
* in the request's account, where the handlers read: an ARN of another account names none. Tagging stores a new
* map under the service's lock, so the read is of one whole set.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "acm")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
if (!parsed.accountId().equals(regionResolver.getAccountId())
|| !parsed.resource().startsWith("certificate/")) {
return Map.of();
}
Map<String, String> tags = AccountAwareStorageBackend.readInAccount(store, parsed.accountId(),
regionKey(parsed.region(), Certificate.idFromArn(arn)))
.filter(cert -> arn.equals(cert.getArn()))
.map(Certificate::getTags)
.orElse(Map.of());
return new LinkedHashMap<>(tags);
}
// ============ Account Configuration ============
public int getAccountDaysBeforeExpiry() {
@@ -36,7 +36,7 @@ public class Certificate {
private KeyAlgorithm keyAlgorithm;
private String signatureAlgorithm;
private List<String> inUseBy;
private Map<String, String> tags;
private volatile Map<String, String> tags;
private String certificateBody;
private String privateKey;
private String certificateChain;
@@ -69,6 +69,26 @@ class AcmServicePersistenceTest {
assertEquals(cert.getCertificateChain(), loaded.getCertificateChain());
}
/**
* IAM's tag lookup reads the tags of the certificate whose ARN it is given, as tag calls leave them and a restart
* keeps them; nothing for an ARN of another account or Region, or of no certificate.
*/
@Test
void storedTagsAreTheTagsOfTheCertificateTheArnNames(@TempDir Path dir) {
AcmService first = newService(dir);
String arn = first.requestCertificate("example.com", List.of(), ValidationMethod.DNS, null,
KeyAlgorithm.RSA_2048, null, null, Map.of("Env", "test"), REGION).getArn();
first.addTagsToCertificate(arn, Map.of("team", "data"), REGION);
first.removeTagsFromCertificate(arn, List.of(Map.of("Key", "Env")), REGION);
assertEquals(Map.of("team", "data"), first.storedTags(arn));
assertEquals(Map.of("team", "data"), newService(dir).storedTags(arn));
assertEquals(Map.of(), first.storedTags(arn.replace(":000000000000:", ":111122223333:")));
assertEquals(Map.of(), first.storedTags(arn.replace(":" + REGION + ":", ":eu-west-1:")));
assertEquals(Map.of(), first.storedTags(arn + "0"));
assertEquals(Map.of(), first.storedTags("arn:aws:s3:::example"));
}
private AcmService newService(Path dir) {
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn("000000000000");
@@ -847,11 +847,7 @@ class IamRequestResourceEnforcementIntegrationTest {
@Test
void aCertificateIsCheckedOnItsArn() {
String arn = adminJson("acm", "CertificateManager.RequestCertificate")
.body("{\"DomainName\":\"res-" + suffix() + ".example.com\"}").post("/").then().statusCode(200)
.extract().path("CertificateArn");
cleanup.register(() -> adminJson("acm", "CertificateManager.DeleteCertificate")
.body("{\"CertificateArn\":\"" + arn + "\"}").post("/"));
String arn = certificate(null);
assertTrue(arn.startsWith("arn:aws:acm:" + REGION + ":" + ACCOUNT + ":certificate/"), arn);
User user = user("res-certificate");
@@ -861,6 +857,45 @@ class IamRequestResourceEnforcementIntegrationTest {
"acm:DescribeCertificate");
}
/**
* An {@code aws:ResourceTag} condition reads the tags ACM holds on the certificate a request names, as a tag call
* leaves them, so an allow on one team's tag lets the caller read that team's certificate and not another's, and
* a deny on it holds, also for the certificate's id under another Region's ARN, which is checked in the request's
* Region too, where the handler reads it.
*/
@Test
void anAcmResourceTagConditionReadsTheTagsOfTheCertificate() {
String tagged = certificate(teamTag("web"));
String other = certificate(teamTag("web"));
adminJson("acm", "CertificateManager.AddTagsToCertificate").body("{\"CertificateArn\":\"" + tagged
+ "\",\"Tags\":" + teamTag("data") + "}").post("/").then().statusCode(200);
User user = user("res-acm-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"acm:DescribeCertificate\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
describeCertificate(user, tagged).statusCode(200);
refusedJson(describeCertificate(user, other), "acm:DescribeCertificate");
User denied = user("res-acm-tag-deny");
denied.policy(allowOn("acm:DescribeCertificate", "*") + ",{\"Effect\":\"Deny\",\"Action\":"
+ "\"acm:DescribeCertificate\",\"Resource\":\"*\",\"Condition\":{\"StringEquals\":"
+ "{\"aws:ResourceTag/team\":\"data\"}}}");
refusedJson(describeCertificate(denied, tagged), "acm:DescribeCertificate");
describeCertificate(denied, other).statusCode(200);
refusedJson(describeCertificate(denied, tagged.replace(":" + REGION + ":", ":eu-west-1:")),
"acm:DescribeCertificate");
}
/** The ARN of a certificate an administrator requests, with the {@code tags} list if any, deleted afterwards. */
private String certificate(String tags) {
String arn = adminJson("acm", "CertificateManager.RequestCertificate")
.body("{\"DomainName\":\"res-" + suffix() + ".example.com\"" + (tags == null ? "" : ",\"Tags\":" + tags)
+ "}").post("/").then().statusCode(200).extract().path("CertificateArn");
cleanup.register(() -> adminJson("acm", "CertificateManager.DeleteCertificate")
.body("{\"CertificateArn\":\"" + arn + "\"}").post("/"));
return arn;
}
/**
* A pipeline is checked on the ARN its name gives, a request naming it by a bare {@code name} too, and a webhook
* on {@code webhook:<name>}, the ARN CodePipeline gives it.