mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(memorydb): keep user and ACL tags and let them be tagged (#8082)
CreateUser and CreateACL never read their Tags member, and ListTags, TagResource and UntagResource resolved every ARN as a cluster, so a user or ACL ARN answered ClusterNotFoundFault and neither resource could be tagged at all. User and Acl now keep a tag map, set from Tags on create as CreateCluster already does. The three tag operations pick the resource from the ARN: user/ and acl/ look the name up in the current Region and require the stored ARN to match, answering UserNotFoundFault or ACLNotFoundFault, and any other ARN goes through clusterByArn as before. The built-in default user and open-access ACL are not stored, so they still answer the not-found fault. Users and ACLs saved before this change have no tags field and load with an empty map. Fixes #7956
This commit is contained in:
1 parent
f6b95fda16
commit
33968bca39
7 files changed
+111
-12
No files matched your search
@@ -25,9 +25,9 @@ Set `FLOCI_SERVICES_MEMORYDB_MOCK=true` to manage clusters as control-plane-only
|
||||
| `CreateACL` | - |
|
||||
| `DescribeACLs` | - |
|
||||
| `DeleteACL` | - |
|
||||
| `ListTags` | List tags for a cluster |
|
||||
| `TagResource` | Add tags to a cluster |
|
||||
| `UntagResource` | Remove tags from a cluster |
|
||||
| `ListTags` | List tags for a cluster, user or ACL |
|
||||
| `TagResource` | Add tags to a cluster, user or ACL |
|
||||
| `UntagResource` | Remove tags from a cluster, user or ACL |
|
||||
<!-- floci:actions:end -->
|
||||
|
||||
## Configuration
|
||||
|
||||
@@ -122,6 +122,7 @@ public class MemoryDbHandler {
|
||||
JsonNode authNode = request.path("AuthenticationMode");
|
||||
spec.setAuthMode(parseAuthMode(authNode));
|
||||
spec.setPasswords(parsePasswords(authNode.path("Passwords")));
|
||||
spec.setTags(parseTags(request.path("Tags")));
|
||||
User created = service.createUser(spec, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("User", userNode(created));
|
||||
@@ -148,6 +149,7 @@ public class MemoryDbHandler {
|
||||
Acl spec = new Acl();
|
||||
spec.setName(text(request, "ACLName"));
|
||||
spec.setUserNames(parseStringList(request.path("UserNames")));
|
||||
spec.setTags(parseTags(request.path("Tags")));
|
||||
Acl created = service.createAcl(spec, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("ACL", aclNode(created));
|
||||
|
||||
@@ -556,6 +556,7 @@ public class MemoryDbService {
|
||||
user.setMinimumEngineVersion(DEFAULT_ENGINE_VERSION);
|
||||
user.setArn(buildArn(region, "user", name));
|
||||
user.setCreatedAt(Instant.now());
|
||||
user.setTags(spec.getTags());
|
||||
|
||||
users.put(resourceKey, user);
|
||||
LOG.infov("MemoryDB user {0} created with authMode={1}", name, user.getAuthMode());
|
||||
@@ -631,6 +632,7 @@ public class MemoryDbService {
|
||||
acl.setMinimumEngineVersion(DEFAULT_ENGINE_VERSION);
|
||||
acl.setArn(buildArn(region, "acl", name));
|
||||
acl.setCreatedAt(Instant.now());
|
||||
acl.setTags(spec.getTags());
|
||||
|
||||
acls.put(resourceKey, acl);
|
||||
LOG.infov("MemoryDB ACL {0} created with users={1}", name, acl.getUserNames());
|
||||
@@ -707,21 +709,21 @@ public class MemoryDbService {
|
||||
// ──────────────────────────── Tags ────────────────────────────
|
||||
|
||||
public Map<String, String> listTags(String resourceArn) {
|
||||
return clusterByArn(resourceArn).getTags();
|
||||
return taggedResource(resourceArn).tags();
|
||||
}
|
||||
|
||||
public Map<String, String> tagResource(String resourceArn, Map<String, String> tags) {
|
||||
Cluster cluster = clusterByArn(resourceArn);
|
||||
cluster.getTags().putAll(tags);
|
||||
clusters.put(key(cluster.getRegion(), cluster.getName()), cluster);
|
||||
return cluster.getTags();
|
||||
TaggedResource resource = taggedResource(resourceArn);
|
||||
resource.tags().putAll(tags);
|
||||
resource.save().run();
|
||||
return resource.tags();
|
||||
}
|
||||
|
||||
public Map<String, String> untagResource(String resourceArn, List<String> tagKeys) {
|
||||
Cluster cluster = clusterByArn(resourceArn);
|
||||
tagKeys.forEach(cluster.getTags()::remove);
|
||||
clusters.put(key(cluster.getRegion(), cluster.getName()), cluster);
|
||||
return cluster.getTags();
|
||||
TaggedResource resource = taggedResource(resourceArn);
|
||||
tagKeys.forEach(resource.tags()::remove);
|
||||
resource.save().run();
|
||||
return resource.tags();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Authentication ────────────────────────────
|
||||
@@ -841,6 +843,28 @@ public class MemoryDbService {
|
||||
|
||||
// ──────────────────────────── Internals ────────────────────────────
|
||||
|
||||
private record TaggedResource(Map<String, String> tags, Runnable save) {}
|
||||
|
||||
private TaggedResource taggedResource(String resourceArn) {
|
||||
String resource = AwsArnUtils.resourceIfArnFor(resourceArn, "memorydb").orElse("");
|
||||
String region = currentRegion();
|
||||
if (resource.startsWith("user/")) {
|
||||
User user = resourceGet(users, resource.substring("user/".length()), region)
|
||||
.filter(u -> resourceArn.equals(u.getArn()))
|
||||
.orElseThrow(() -> new AwsException("UserNotFoundFault", "User not found.", 404));
|
||||
return new TaggedResource(user.getTags(), () -> users.put(key(region, user.getName()), user));
|
||||
}
|
||||
if (resource.startsWith("acl/")) {
|
||||
Acl acl = resourceGet(acls, resource.substring("acl/".length()), region)
|
||||
.filter(a -> resourceArn.equals(a.getArn()))
|
||||
.orElseThrow(() -> new AwsException("ACLNotFoundFault", "ACL not found.", 404));
|
||||
return new TaggedResource(acl.getTags(), () -> acls.put(key(region, acl.getName()), acl));
|
||||
}
|
||||
Cluster cluster = clusterByArn(resourceArn);
|
||||
return new TaggedResource(cluster.getTags(),
|
||||
() -> clusters.put(key(cluster.getRegion(), cluster.getName()), cluster));
|
||||
}
|
||||
|
||||
private Cluster clusterByArn(String resourceArn) {
|
||||
if (resourceArn == null) {
|
||||
throw new AwsException("InvalidParameterValueException", "ResourceArn is required.", 400);
|
||||
|
||||
@@ -5,7 +5,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A MemoryDB Access Control List (ACL): a named collection of {@link User}s that a
|
||||
@@ -26,6 +28,7 @@ public class Acl {
|
||||
private String minimumEngineVersion;
|
||||
private String arn;
|
||||
private Instant createdAt;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public Acl() {}
|
||||
|
||||
@@ -56,4 +59,9 @@ public class Acl {
|
||||
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags != null ? tags : new LinkedHashMap<>();
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A MemoryDB user. Users authenticate either with one or more passwords or with IAM,
|
||||
@@ -26,6 +28,7 @@ public class User {
|
||||
private String minimumEngineVersion;
|
||||
private String arn;
|
||||
private Instant createdAt;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public User() {}
|
||||
|
||||
@@ -62,4 +65,9 @@ public class User {
|
||||
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags != null ? tags : new LinkedHashMap<>();
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -89,6 +90,26 @@ class MemoryDbHandlerTest {
|
||||
assertEquals(List.of("app-user"), captor.getValue().getUserNames());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserAndAclReadTags() throws Exception {
|
||||
when(service.createUser(any(), any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(service.createAcl(any(), any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
String tags = "\"Tags\":[{\"Key\":\"team\",\"Value\":\"red\"}]";
|
||||
|
||||
handler.handle("CreateUser", objectMapper.readTree(
|
||||
"{\"UserName\":\"tag-user\",\"AccessString\":\"on ~* +@all\","
|
||||
+ "\"AuthenticationMode\":{\"Type\":\"iam\"}," + tags + "}"), "us-east-1");
|
||||
handler.handle("CreateACL", objectMapper.readTree(
|
||||
"{\"ACLName\":\"tag-acl\",\"UserNames\":[\"default\"]," + tags + "}"), "us-east-1");
|
||||
|
||||
ArgumentCaptor<User> user = ArgumentCaptor.forClass(User.class);
|
||||
verify(service).createUser(user.capture(), eq("us-east-1"));
|
||||
assertEquals(Map.of("team", "red"), user.getValue().getTags());
|
||||
ArgumentCaptor<Acl> acl = ArgumentCaptor.forClass(Acl.class);
|
||||
verify(service).createAcl(acl.capture(), eq("us-east-1"));
|
||||
assertEquals(Map.of("team", "red"), acl.getValue().getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownOperationReturns400() throws Exception {
|
||||
JsonNode request = objectMapper.readTree("{}");
|
||||
|
||||
@@ -18,6 +18,7 @@ import io.floci.aws.services.memorydb.proxy.MemoryDbProxyManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -170,6 +171,41 @@ class MemoryDbServiceTest {
|
||||
assertFalse(service.listTags(arn).containsKey("env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void usersAndAclsKeepCreateTagsAndCanBeTagged() {
|
||||
User userSpec = new User();
|
||||
userSpec.setName("tag-user");
|
||||
userSpec.setAuthMode(AuthMode.PASSWORD);
|
||||
userSpec.setPasswords(List.of("passwordpassword1"));
|
||||
userSpec.setAccessString("on ~* +@all");
|
||||
userSpec.setTags(new LinkedHashMap<>(Map.of("team", "red")));
|
||||
String userArn = service.createUser(userSpec, "us-east-1").getArn();
|
||||
|
||||
Acl aclSpec = new Acl();
|
||||
aclSpec.setName("tag-acl");
|
||||
aclSpec.setUserNames(List.of("default", "tag-user"));
|
||||
aclSpec.setTags(new LinkedHashMap<>(Map.of("team", "red")));
|
||||
String aclArn = service.createAcl(aclSpec, "us-east-1").getArn();
|
||||
|
||||
assertEquals(Map.of("team", "red"), service.listTags(userArn));
|
||||
assertEquals(Map.of("team", "red"), service.listTags(aclArn));
|
||||
|
||||
service.tagResource(userArn, Map.of("env", "dev"));
|
||||
service.untagResource(aclArn, List.of("team"));
|
||||
assertEquals(Map.of("team", "red", "env", "dev"), service.listTags(userArn));
|
||||
assertEquals(Map.of(), service.listTags(aclArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownUserOrAclArnRaisesItsNotFoundFault() {
|
||||
AwsException user = assertThrows(AwsException.class,
|
||||
() -> service.listTags("arn:aws:memorydb:us-east-1:000000000000:user/missing"));
|
||||
assertEquals("UserNotFoundFault", user.jsonType());
|
||||
AwsException acl = assertThrows(AwsException.class,
|
||||
() -> service.tagResource("arn:aws:memorydb:us-east-1:000000000000:acl/missing", Map.of("env", "dev")));
|
||||
assertEquals("ACLNotFoundFault", acl.jsonType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openAccessClusterRequiresNoAuth() {
|
||||
Cluster spec = new Cluster();
|
||||
|
||||
Reference in new issue
Block a user