fix(ce): give resource-tag conditions the tags of the anomaly monitor or subscription a request names (#8881)

The generated request resources give Cost Explorer's anomalymonitor and
anomalysubscription types the aws:ResourceTag/ key, but no service
answered the ce namespace's stored tags, so a condition on a monitor's
or a subscription's tags saw none. CostAnomalyService now answers it:
storedTags reads the monitor or subscription stored under its whole ARN,
in the request's account, where the handlers read, in place. Every write
already puts a new record under the service's lock, so nothing to lock.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 13:43:11 -04:00
1 parent 4ac034d645
commit 2ac4554ea8
5 files changed
+124 -7

No files matched your search

@@ -53,9 +53,10 @@ import java.util.Optional;
* CodePipeline, for the tags of a pipeline, a webhook or a custom action type; DataSync, for the tags of an agent, a
* location or a task; Global Accelerator, for the tags of an accelerator, a listener or an endpoint group; EMR
* 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); 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>
* job run, a managed endpoint, a job template or a security configuration; Cost Explorer, for the tags of an anomaly
* monitor or subscription); 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
@@ -11,6 +11,7 @@ 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.Resettable;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import jakarta.enterprise.context.ApplicationScoped;
@@ -21,16 +22,18 @@ import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
/** Stores Cost Anomaly Detection configuration without generating anomalies or notifications. */
@ApplicationScoped
public class CostAnomalyService implements Resettable {
public class CostAnomalyService implements Resettable, StoredResourceArns {
private static final BigDecimal MAX_THRESHOLD = new BigDecimal("10000000000");
private static final Pattern TAG_TEXT = Pattern.compile("[\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*");
@@ -205,6 +208,37 @@ public class CostAnomalyService implements Resettable {
storage.putForAccount(account, arn, resource);
}
@Override
public String namespace() {
return "ce";
}
/** A request names a monitor or a subscription by its ARN: nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* The tags of the anomaly monitor or subscription whose ARN is {@code arn}, in the request's account, where the
* handlers read: an ARN of another account names none. Both are stored under their whole ARN, so the read is in
* place under that key, a record stored before storage was account-aware included. Every write puts a new record,
* under this service's lock, so the read is of one whole set.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "ce") || !AwsArnUtils.parse(arn).accountId().equals(monitors.accountId())) {
return Map.of();
}
String account = monitors.accountId();
Map<String, String> tags = new LinkedHashMap<>();
AccountAwareStorageBackend.readInAccount(monitors, account, arn)
.or(() -> AccountAwareStorageBackend.readInAccount(subscriptions, account, arn))
.ifPresent(resource -> resource.path("tags").properties()
.forEach(tag -> tags.put(tag.getKey(), tag.getValue().asText(""))));
return tags;
}
@Override
public synchronized void clear() {
monitors.clear();