mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudmap): give resource-tag conditions the tags of the namespace or service a request names (#8932)
The generated request resources give Cloud Map's namespace and service types the aws:ResourceTag/ key, but no service answered the servicediscovery namespace's stored tags, so a condition on their tags saw none. CloudMapService now answers it: storedTags reads the namespace or the service by the id its ARN ends in, in the request's account, in place, the id the handlers find it by in every Region, so a request sent to another Region is checked with the tags of the resource it acts on; a tag without a value reads as an empty one. TagResource and UntagResource changed the tag map in place with no lock; they now give the resource a new map one call at a time, and the tags fields are volatile.
This commit is contained in:
1 parent
813f89bd8e
commit
26dc6d79ed
7 files changed
+214
-18
No files matched your search
@@ -55,10 +55,10 @@ import java.util.Optional;
|
||||
* Serverless, for the tags of an application, a job run or a session; EMR on EKS, for the tags of a virtual cluster, a
|
||||
* job run, a managed endpoint, a job template or a security configuration; Cost Explorer, for the tags of an anomaly
|
||||
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy; Control Tower, for the tags
|
||||
* of a landing zone or an enabled control; GuardDuty, for the tags of a detector or a Malware Protection plan); looked
|
||||
* up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
* of a landing zone or an enabled control; GuardDuty, for the tags of a detector or a Malware Protection plan; Cloud
|
||||
* Map, for the tags of a namespace or a service); looked up lazily by {@code ResourceArnBuilder} and
|
||||
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
|
||||
* ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>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
|
||||
|
||||
@@ -14,6 +14,7 @@ import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.common.dns.DnsAnswer;
|
||||
import io.floci.aws.core.common.dns.DnsRecord;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
@@ -56,9 +57,12 @@ import java.util.function.Function;
|
||||
* whose status reaches SUCCESS immediately (or after a configurable delay).
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CloudMapService {
|
||||
public class CloudMapService implements StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CloudMapService.class);
|
||||
private static final String SERVICE = "servicediscovery";
|
||||
private static final String NAMESPACE_RESOURCE = "namespace/";
|
||||
private static final String SERVICE_RESOURCE = "service/";
|
||||
private static final String ALNUM = "abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
/** Route 53 answers a service discovery query with at most eight records. */
|
||||
private static final int MAX_DNS_ANSWERS = 8;
|
||||
@@ -161,7 +165,7 @@ public class CloudMapService {
|
||||
if (tags != null) {
|
||||
ns.setTags(tags);
|
||||
}
|
||||
ns.setArn(regionResolver.buildArn("servicediscovery", region, "namespace/" + ns.getId()));
|
||||
ns.setArn(regionResolver.buildArn(SERVICE, region, NAMESPACE_RESOURCE + ns.getId()));
|
||||
return ns;
|
||||
}
|
||||
|
||||
@@ -259,7 +263,7 @@ public class CloudMapService {
|
||||
if (tags != null) {
|
||||
service.setTags(tags);
|
||||
}
|
||||
service.setArn(regionResolver.buildArn("servicediscovery", region, "service/" + service.getId()));
|
||||
service.setArn(regionResolver.buildArn(SERVICE, region, SERVICE_RESOURCE + service.getId()));
|
||||
serviceStore.put(service.getId(), service);
|
||||
if (nsId != null) {
|
||||
namespaceStore.get(nsId).ifPresent(n -> {
|
||||
@@ -860,28 +864,83 @@ public class CloudMapService {
|
||||
return taggable(resourceArn).getTags();
|
||||
}
|
||||
|
||||
public void tagResource(String resourceArn, Map<String, String> tags) {
|
||||
/**
|
||||
* Gives the namespace or the service a new map holding its tags and {@code tags}. Tag calls run one at a time, so
|
||||
* two of them never lose one's tags, and a lookup reads one whole set.
|
||||
*/
|
||||
public synchronized void tagResource(String resourceArn, Map<String, String> tags) {
|
||||
Object resource = taggableRaw(resourceArn);
|
||||
if (resource instanceof Namespace ns) {
|
||||
ns.getTags().putAll(tags);
|
||||
Map<String, String> merged = new LinkedHashMap<>(ns.getTags());
|
||||
merged.putAll(tags);
|
||||
ns.setTags(merged);
|
||||
namespaceStore.put(ns.getId(), ns);
|
||||
} else if (resource instanceof Service s) {
|
||||
s.getTags().putAll(tags);
|
||||
Map<String, String> merged = new LinkedHashMap<>(s.getTags());
|
||||
merged.putAll(tags);
|
||||
s.setTags(merged);
|
||||
serviceStore.put(s.getId(), s);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagResource(String resourceArn, List<String> tagKeys) {
|
||||
/** Gives the namespace or the service a new map without {@code tagKeys}, one tag call at a time. */
|
||||
public synchronized void untagResource(String resourceArn, List<String> tagKeys) {
|
||||
Object resource = taggableRaw(resourceArn);
|
||||
if (resource instanceof Namespace ns) {
|
||||
tagKeys.forEach(ns.getTags()::remove);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(ns.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
ns.setTags(remaining);
|
||||
namespaceStore.put(ns.getId(), ns);
|
||||
} else if (resource instanceof Service s) {
|
||||
tagKeys.forEach(s.getTags()::remove);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(s.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
s.setTags(remaining);
|
||||
serviceStore.put(s.getId(), s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return SERVICE;
|
||||
}
|
||||
|
||||
/** A request names a namespace or a service by the id its ARN ends in: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the namespace or the service whose ARN is {@code arn}, in the request's account, where the tag
|
||||
* handlers read: an ARN of another account names none. Each is read in place by the id its ARN ends in, one stored
|
||||
* before storage was account-aware included. That is the id the handlers find it by, in every Region, so an ARN
|
||||
* naming another Region than the resource's reads its tags too, as a request sent there acts on it. A tag without
|
||||
* a value reads as an empty one. Tag calls give the resource a new map, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, SERVICE)) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = regionResolver.getAccountId();
|
||||
if (!parsed.accountId().equals(account)) {
|
||||
return Map.of();
|
||||
}
|
||||
String resource = parsed.resource();
|
||||
Optional<Map<String, String>> tags = Optional.empty();
|
||||
if (resource.startsWith(NAMESPACE_RESOURCE)) {
|
||||
tags = AccountAwareStorageBackend.readInAccount(namespaceStore, account,
|
||||
resource.substring(NAMESPACE_RESOURCE.length())).map(Namespace::getTags);
|
||||
} else if (resource.startsWith(SERVICE_RESOURCE)) {
|
||||
tags = AccountAwareStorageBackend.readInAccount(serviceStore, account,
|
||||
resource.substring(SERVICE_RESOURCE.length())).map(Service::getTags);
|
||||
}
|
||||
Map<String, String> copy = new LinkedHashMap<>();
|
||||
tags.orElse(Map.of()).forEach((key, value) -> copy.put(key, value == null ? "" : value));
|
||||
return copy;
|
||||
}
|
||||
|
||||
private interface Taggable { Map<String, String> getTags(); }
|
||||
|
||||
private Taggable taggable(String resourceArn) {
|
||||
@@ -896,7 +955,7 @@ public class CloudMapService {
|
||||
if (resourceArn == null) {
|
||||
throw new AwsException("InvalidInput", "ResourceARN is required.", 400);
|
||||
}
|
||||
String id = AwsArnUtils.resourceIfArnFor(resourceArn, "servicediscovery")
|
||||
String id = AwsArnUtils.resourceIfArnFor(resourceArn, SERVICE)
|
||||
.map(resource -> resource.substring(resource.lastIndexOf('/') + 1))
|
||||
.orElse(resourceArn);
|
||||
Optional<Namespace> ns = namespaceStore.get(id);
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Namespace {
|
||||
private String region;
|
||||
private String vpc;
|
||||
private String hostedZoneId;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
|
||||
public Namespace() {}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class Service {
|
||||
private String creatorRequestId;
|
||||
private String region;
|
||||
private long revision;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
|
||||
public Service() {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user