mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(rds): resolve tags by ARN resource type, not only DB instances (#1649)
* fix(rds): resolve tags by ARN resource type, not only DB instances ListTagsForResource, AddTagsToResource and RemoveTagsFromResource unconditionally resolved every ResourceName to a DB instance, so any non-instance ARN (e.g. a DB subnet group) returned 404 DBInstanceNotFound. This broke tools such as the Crossplane Upbound AWS provider, which calls ListTagsForResource on every observe of a DB subnet group and never sees it become ready. Parse the ARN's resource-type segment and dispatch to the matching store (db, cluster, subgrp): return the resource's tags, the resource-specific NotFound fault when it is missing, and InvalidParameterValue for unsupported resource types. Add tag storage to DbCluster and DbSubnetGroup mirroring DbInstance. A bare (non-ARN) resource name still resolves to a DB instance for backwards compatibility. * fix(rds): return InvalidParameterValue for malformed or non-RDS tag ARNs Address PR review: only fall back to a DB-instance lookup for bare, non-ARN resource names. Strings that start with arn: are now parsed strictly and must be RDS ARNs; malformed ARNs and ARNs for other services return 400 InvalidParameterValue instead of a misleading DBInstanceNotFound/DBClusterNotFoundFault. * fix(rds): preserve subnet group tags across modifyDbSubnetGroup Address PR review: buildSubnetGroup constructs a fresh DbSubnetGroup with an empty tag map, so modifying a subnet group's subnets dropped any tags added via AddTagsToResource. Carry the existing group's tags onto the rebuilt object before persisting, matching AWS ModifyDBSubnetGroup. * fix(rds): reject type-less RDS ARNs in tag ops and clarify unsupported-type message Review feedback from #1649: - An RDS ARN whose resource part is not <type>:<id> now returns 400 InvalidParameterValue (matching real AWS) instead of falling back to a DB-instance lookup; bare non-ARN names keep resolving as DB instances. - The unsupported-resource-type message now states the Floci scope limitation instead of implying those types are untaggable on AWS.
This commit is contained in:
1 parent
c99dd75b87
commit
98464b4433
4 files changed
+166
-17
No files matched your search
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
|
||||
@@ -320,37 +321,89 @@ public class RdsService implements Resettable {
|
||||
}
|
||||
|
||||
public Map<String, String> listTagsForResource(String resourceName) {
|
||||
DbInstance instance = getDbInstance(dbInstanceIdentifierFromResourceName(resourceName));
|
||||
return Map.copyOf(instance.getTags());
|
||||
return Map.copyOf(resolveTagHandle(resourceName).tags());
|
||||
}
|
||||
|
||||
public void addTagsToResource(String resourceName, Map<String, String> tags) {
|
||||
String id = dbInstanceIdentifierFromResourceName(resourceName);
|
||||
DbInstance instance = getDbInstance(id);
|
||||
Map<String, String> updated = new java.util.LinkedHashMap<>(instance.getTags());
|
||||
TagHandle handle = resolveTagHandle(resourceName);
|
||||
Map<String, String> updated = new java.util.LinkedHashMap<>(handle.tags());
|
||||
updated.putAll(tags);
|
||||
instance.setTags(updated);
|
||||
instances.put(id, instance);
|
||||
handle.save().accept(updated);
|
||||
}
|
||||
|
||||
public void removeTagsFromResource(String resourceName, Collection<String> tagKeys) {
|
||||
String id = dbInstanceIdentifierFromResourceName(resourceName);
|
||||
DbInstance instance = getDbInstance(id);
|
||||
Map<String, String> updated = new java.util.LinkedHashMap<>(instance.getTags());
|
||||
TagHandle handle = resolveTagHandle(resourceName);
|
||||
Map<String, String> updated = new java.util.LinkedHashMap<>(handle.tags());
|
||||
tagKeys.forEach(updated::remove);
|
||||
instance.setTags(updated);
|
||||
instances.put(id, instance);
|
||||
handle.save().accept(updated);
|
||||
}
|
||||
|
||||
private static String dbInstanceIdentifierFromResourceName(String resourceName) {
|
||||
/** A resolved tag target: its current tags plus a sink that persists an updated map. */
|
||||
private record TagHandle(Map<String, String> tags, java.util.function.Consumer<Map<String, String>> save) {}
|
||||
|
||||
/**
|
||||
* Resolves a tagging ResourceName to its backing resource.
|
||||
*
|
||||
* RDS tags can be attached to many resource types (DB instances, clusters, subnet groups, ...),
|
||||
* each identified by an ARN of the form {@code arn:aws:rds:<region>:<account>:<type>:<id>}.
|
||||
* A bare resource name (no ARN) is treated as a DB instance identifier for backwards compatibility.
|
||||
*/
|
||||
private TagHandle resolveTagHandle(String resourceName) {
|
||||
if (resourceName == null || resourceName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400);
|
||||
}
|
||||
int marker = resourceName.lastIndexOf(":db:");
|
||||
if (marker >= 0) {
|
||||
return resourceName.substring(marker + 4);
|
||||
|
||||
String type = "db";
|
||||
String id = resourceName;
|
||||
if (resourceName.startsWith("arn:")) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(resourceName);
|
||||
} catch (IllegalArgumentException malformed) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400);
|
||||
}
|
||||
if (!"rds".equals(arn.service())) {
|
||||
throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400);
|
||||
}
|
||||
String resource = arn.resource();
|
||||
int sep = resource.indexOf(':');
|
||||
if (sep < 0) {
|
||||
// Real AWS requires the resource part of an RDS ARN to be <type>:<id>.
|
||||
throw new AwsException("InvalidParameterValue", "Invalid resource name: " + resourceName, 400);
|
||||
}
|
||||
type = resource.substring(0, sep);
|
||||
id = resource.substring(sep + 1);
|
||||
}
|
||||
return resourceName;
|
||||
// A bare (non-ARN) resource name is treated as a DB instance identifier for backwards compatibility.
|
||||
|
||||
String resourceId = id;
|
||||
return switch (type) {
|
||||
case "db" -> {
|
||||
DbInstance instance = getDbInstance(resourceId);
|
||||
yield new TagHandle(instance.getTags(), updated -> {
|
||||
instance.setTags(updated);
|
||||
instances.put(resourceId, instance);
|
||||
});
|
||||
}
|
||||
case "cluster" -> {
|
||||
DbCluster cluster = getDbCluster(resourceId);
|
||||
yield new TagHandle(cluster.getTags(), updated -> {
|
||||
cluster.setTags(updated);
|
||||
clusters.put(resourceId, cluster);
|
||||
});
|
||||
}
|
||||
case "subgrp" -> {
|
||||
DbSubnetGroup group = getDbSubnetGroup(resourceId);
|
||||
yield new TagHandle(group.getTags(), updated -> {
|
||||
group.setTags(updated);
|
||||
subnetGroups.put(resourceId, group);
|
||||
});
|
||||
}
|
||||
// Valid RDS resource types Floci does not model yet (og, pg, snapshot, ...) — taggable
|
||||
// on real AWS, so the message states the Floci limitation rather than AWS semantics.
|
||||
default -> throw new AwsException("InvalidParameterValue",
|
||||
"Tagging for resource type '" + type + "' is not yet implemented by Floci: " + resourceName, 400);
|
||||
};
|
||||
}
|
||||
|
||||
private void attachManagedMasterUserSecret(DbInstance instance, String region, String kmsKeyId) {
|
||||
@@ -750,6 +803,7 @@ public class RdsService implements Resettable {
|
||||
"SubnetIds must contain at least one subnet.", 400);
|
||||
}
|
||||
DbSubnetGroup group = buildSubnetGroup(name, existing.getDescription(), subnetIds);
|
||||
group.setTags(existing.getTags());
|
||||
subnetGroups.put(name, group);
|
||||
return group;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class DbCluster {
|
||||
private String dbClusterArn;
|
||||
private Instant createdAt;
|
||||
private int proxyPort;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
private String dockerVolumeName;
|
||||
private String volumeId;
|
||||
@@ -133,6 +134,9 @@ public class DbCluster {
|
||||
public int getProxyPort() { return proxyPort; }
|
||||
public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>(); }
|
||||
|
||||
public String getDockerVolumeName() { return dockerVolumeName; }
|
||||
public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; }
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ public class DbSubnetGroup {
|
||||
private String subnetGroupStatus = "Complete";
|
||||
private List<String> subnetIds = new ArrayList<>();
|
||||
private Map<String, String> subnetAvailabilityZones = new LinkedHashMap<>();
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public DbSubnetGroup() {}
|
||||
|
||||
@@ -61,4 +62,7 @@ public class DbSubnetGroup {
|
||||
? new LinkedHashMap<>(subnetAvailabilityZones)
|
||||
: new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags != null ? new LinkedHashMap<>(tags) : new LinkedHashMap<>(); }
|
||||
}
|
||||
Reference in new issue
Block a user