mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): read the stored tags of MemoryDB clusters, users and ACLs (#9799)
The request-resources table gives MemoryDB's clusters, users and ACLs the aws:ResourceTag keys, which the Service Reference lists for them, but MemoryDB answered no storedTags, so a condition on a resource tag never matched: a caller allowed memorydb:DeleteUser only on users tagged team=blue was refused it on the blue user as on any other. MemoryDbStoredResourceArns reads the tags MemoryDB's ListTags reads, for the caller's cluster, user or ACL the ARN names. Its tag changes mutated the stored map in place under no lock, and ListTags handed out that live map, which an IAM check reading it while another request tags the resource could see half changed or fail on; the tag read, tag and untag are now synchronized, and each returns a copy.
This commit is contained in:
1 parent
d623162594
commit
91480010b7
4 files changed
+163
-8
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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<String, String> 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<String, String> listTags(String resourceArn) {
|
||||
return new LinkedHashMap<>(taggedResource(resourceArn).tags());
|
||||
}
|
||||
|
||||
public Map<String, String> tagResource(String resourceArn, Map<String, String> 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<String, String> 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<String, String> tagResource(String resourceArn, Map<String, String> tags) {
|
||||
TaggedResource resource = taggedResource(resourceArn);
|
||||
resource.tags().putAll(tags);
|
||||
resource.save().run();
|
||||
return resource.tags();
|
||||
return new LinkedHashMap<>(resource.tags());
|
||||
}
|
||||
|
||||
public Map<String, String> untagResource(String resourceArn, List<String> tagKeys) {
|
||||
public synchronized Map<String, String> untagResource(String resourceArn, List<String> tagKeys) {
|
||||
TaggedResource resource = taggedResource(resourceArn);
|
||||
tagKeys.forEach(resource.tags()::remove);
|
||||
resource.save().run();
|
||||
return resource.tags();
|
||||
return new LinkedHashMap<>(resource.tags());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Authentication ────────────────────────────
|
||||
|
||||
@@ -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/<key>}).
|
||||
*/
|
||||
@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<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>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<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "memorydb")) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(memoryDb.storedTags(arn));
|
||||
}
|
||||
}
|
||||
+87
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user