diff --git a/docs/services/iam.md b/docs/services/iam.md index f7fe908c9e..1336c0e2ae 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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 diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java index 91dcb86ed1..4e986d8ee2 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -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} for the namespace a request or an - * ARN is of, so IAM never depends on that service directly.

+ * 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} for + * the namespace a request or an ARN is of, so IAM never depends on that service directly.

* *

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 diff --git a/src/main/java/io/floci/aws/services/acm/AcmService.java b/src/main/java/io/floci/aws/services/acm/AcmService.java index cb4790cd0b..346ea3da98 100644 --- a/src/main/java/io/floci/aws/services/acm/AcmService.java +++ b/src/main/java/io/floci/aws/services/acm/AcmService.java @@ -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 AWS ACM API Reference */ @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 find(String type, List 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 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 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() { diff --git a/src/main/java/io/floci/aws/services/acm/model/Certificate.java b/src/main/java/io/floci/aws/services/acm/model/Certificate.java index 3b2cbea9a4..719f76b9c6 100644 --- a/src/main/java/io/floci/aws/services/acm/model/Certificate.java +++ b/src/main/java/io/floci/aws/services/acm/model/Certificate.java @@ -36,7 +36,7 @@ public class Certificate { private KeyAlgorithm keyAlgorithm; private String signatureAlgorithm; private List inUseBy; - private Map tags; + private volatile Map tags; private String certificateBody; private String privateKey; private String certificateChain; diff --git a/src/test/java/io/floci/aws/services/acm/AcmServicePersistenceTest.java b/src/test/java/io/floci/aws/services/acm/AcmServicePersistenceTest.java index f8b06dc7f2..8b3db08681 100644 --- a/src/test/java/io/floci/aws/services/acm/AcmServicePersistenceTest.java +++ b/src/test/java/io/floci/aws/services/acm/AcmServicePersistenceTest.java @@ -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"); diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java index da580e214f..bc1a724bab 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java @@ -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:}, the ARN CodePipeline gives it.