diff --git a/docs/services/iam.md b/docs/services/iam.md index 565000cc77..484db06a55 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -4016,8 +4016,8 @@ floci populates: controls, GuardDuty for its detectors and Malware Protection plans, Cloud Map for its namespaces and services, Elastic Load Balancing for its load balancers, target groups, listeners and rules and its Classic Load Balancers, SQS for its queues, SNS for its topics, Kinesis Data Streams for its streams and consumers, EventBridge Pipes for its pipes, - Aurora DSQL for its clusters, EKS for its clusters, node groups, Fargate profiles, add-ons, Pod Identity - associations and access entries, Resource Groups for its groups, CloudWatch Logs for its log groups, + Aurora DSQL for its clusters, MemoryDB for its clusters, users and ACLs, EKS for its clusters, node groups, + Fargate profiles, add-ons, Pod Identity associations and access entries, Resource Groups for its groups, CloudWatch Logs for its log groups, destinations, delivery sources, delivery destinations and deliveries, Pricing Calculator for its workload estimates, and Amazon Managed Service for Prometheus for its workspaces and rule groups namespaces. Tag keys differing only in case are one condition key holding every value. diff --git a/src/main/java/io/floci/aws/services/memorydb/MemoryDbService.java b/src/main/java/io/floci/aws/services/memorydb/MemoryDbService.java index f25209fc6e..2fa2c471f5 100644 --- a/src/main/java/io/floci/aws/services/memorydb/MemoryDbService.java +++ b/src/main/java/io/floci/aws/services/memorydb/MemoryDbService.java @@ -29,6 +29,7 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -708,22 +709,39 @@ public class MemoryDbService { // ──────────────────────────── Tags ──────────────────────────── - public Map listTags(String resourceArn) { - return taggedResource(resourceArn).tags(); + /** + * The resource's tags, as a copy taken under the monitor its tag changes hold, so a concurrent change, IAM's read + * of them for an {@code aws:ResourceTag} condition included ({@link #storedTags}), sees them whole or not at all. + */ + public synchronized Map listTags(String resourceArn) { + return new LinkedHashMap<>(taggedResource(resourceArn).tags()); } - public Map tagResource(String resourceArn, Map tags) { + /** + * The tags of the caller's cluster, user or ACL the ARN names, by key, as {@link #listTags} reads them; empty for + * an ARN of another account or Region, or one naming no resource MemoryDB holds. + */ + public synchronized Map storedTags(String resourceArn) { + try { + return new LinkedHashMap<>(taggedResource(resourceArn).tags()); + } catch (AwsException expected) { + // A resource the service does not hold has no stored tags for IAM to read. + return Map.of(); + } + } + + public synchronized Map tagResource(String resourceArn, Map tags) { TaggedResource resource = taggedResource(resourceArn); resource.tags().putAll(tags); resource.save().run(); - return resource.tags(); + return new LinkedHashMap<>(resource.tags()); } - public Map untagResource(String resourceArn, List tagKeys) { + public synchronized Map untagResource(String resourceArn, List tagKeys) { TaggedResource resource = taggedResource(resourceArn); tagKeys.forEach(resource.tags()::remove); resource.save().run(); - return resource.tags(); + return new LinkedHashMap<>(resource.tags()); } // ──────────────────────────── Authentication ──────────────────────────── diff --git a/src/main/java/io/floci/aws/services/memorydb/MemoryDbStoredResourceArns.java b/src/main/java/io/floci/aws/services/memorydb/MemoryDbStoredResourceArns.java new file mode 100644 index 0000000000..692407e42e --- /dev/null +++ b/src/main/java/io/floci/aws/services/memorydb/MemoryDbStoredResourceArns.java @@ -0,0 +1,50 @@ +package io.floci.aws.services.memorydb; + +import io.floci.aws.core.common.AwsArnUtils; +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 MemoryDB of the resources a request names: the tags it keeps on a cluster, a user or an ACL, which the + * request-resources table's resource-tag keys for those types take as their values ({@code aws:ResourceTag/}). + */ +@ApplicationScoped +public class MemoryDbStoredResourceArns implements StoredResourceArns { + + private final MemoryDbService memoryDb; + + @Inject + public MemoryDbStoredResourceArns(MemoryDbService memoryDb) { + this.memoryDb = memoryDb; + } + + @Override + public String namespace() { + return "memorydb"; + } + + /** A request names a cluster, a user or an ACL by the name its ARN ends in: nothing to find. */ + @Override + public Optional find(String type, List names, String region) { + return Optional.empty(); + } + + /** + * {@inheritDoc} + * + *

The tags of the caller's cluster, user or ACL the ARN names, as {@code ListTags} reads them, under the monitor + * MemoryDB's tag changes hold; none for an ARN of another account or Region, or one naming no resource it holds. + */ + @Override + public Map storedTags(String arn) { + if (!AwsArnUtils.isArnFor(arn, "memorydb")) { + return Map.of(); + } + return StoredResourceArns.withEmptyValues(memoryDb.storedTags(arn)); + } +} diff --git a/src/test/java/io/floci/aws/services/memorydb/MemoryDbResourceTagIamIntegrationTest.java b/src/test/java/io/floci/aws/services/memorydb/MemoryDbResourceTagIamIntegrationTest.java new file mode 100644 index 0000000000..73989288c8 --- /dev/null +++ b/src/test/java/io/floci/aws/services/memorydb/MemoryDbResourceTagIamIntegrationTest.java @@ -0,0 +1,87 @@ +package io.floci.aws.services.memorydb; + +import io.floci.aws.testing.EnforcementFixtures; +import io.floci.aws.testing.EnforcementFixtures.UserCredentials; +import io.floci.aws.testing.IamEnforcementProfile; +import io.floci.aws.testing.PartitionCleanup; +import io.floci.aws.testing.RestAssuredJsonUtils; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.net.URI; +import java.util.UUID; + +import static io.floci.aws.testing.EnforcementFixtures.auth; +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; + +/** + * A condition on {@code aws:ResourceTag} reads the tags MemoryDB keeps on a user, a type the request-resources table + * gives resource-tag keys: a caller allowed {@code memorydb:DeleteUser} only on users tagged {@code team=blue} deletes + * the blue user and is refused the red one. + */ +@QuarkusTest +@TestProfile(IamEnforcementProfile.class) +class MemoryDbResourceTagIamIntegrationTest { + + private static final String ACCOUNT = "000000000000"; + private static final String REGION = "us-east-1"; + + @TestHTTPResource("/") + URI endpoint; + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void aUserIsCheckedWithItsTags() { + String admin = auth(ACCOUNT, REGION, "memorydb"); + String blue = user(admin, "blue"); + String red = user(admin, "red"); + String name = "tags-" + suffix(); + String iam = auth(ACCOUNT, REGION, "iam"); + UserCredentials caller = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), iam, name); + EnforcementFixtures.putUserPolicy(endpoint.getPort(), iam, name, "blue", """ + {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"memorydb:DeleteUser","Resource":"*", + "Condition":{"StringEquals":{"aws:ResourceTag/team":"blue"}}}]}"""); + String signed = auth(caller.accessKeyId(), REGION, "memorydb"); + + memorydb("DeleteUser", "{\"UserName\":\"" + red + "\"}", signed).statusCode(400) + .body(containsString("AccessDenied")) + .body(containsString("not authorized to perform: memorydb:DeleteUser")); + memorydb("DeleteUser", "{\"UserName\":\"" + blue + "\"}", signed).statusCode(200); + } + + private String user(String authorization, String team) { + String name = team + "-" + suffix(); + memorydb("CreateUser", """ + {"UserName":"%s","AccessString":"on ~* +@all", + "AuthenticationMode":{"Type":"password","Passwords":["a-long-enough-password-1"]}, + "Tags":[{"Key":"team","Value":"%s"}]}""".formatted(name, team), authorization).statusCode(200); + cleanup.register(() -> memorydb("DeleteUser", "{\"UserName\":\"" + name + "\"}", authorization)); + return name; + } + + private static ValidatableResponse memorydb(String action, String body, String authorization) { + return given().contentType("application/x-amz-json-1.1") + .header("X-Amz-Target", "AmazonMemoryDB." + action) + .header("Authorization", authorization) + .body(body) + .when().post("/") + .then(); + } + + private static String suffix() { + return UUID.randomUUID().toString().substring(0, 8); + } +}