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

@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.PersistentStorage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -17,6 +18,7 @@ import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -152,6 +154,46 @@ class CostAnomalyServiceTest {
assertEquals(0, service.getSubscriptions(mapper.createObjectNode()).path("AnomalySubscriptions").size());
}
/**
* IAM's lookup reads the tags of the monitor or subscription the ARN names, as the tag calls leave them, and
* nothing for another account's, an unknown or another service's ARN.
*/
@Test
void storedTagsAreTheTagsOfTheMonitorOrSubscriptionTheArnNames() {
String monitorArn = service.createMonitor(customMonitor("tagged")).path("MonitorArn").asText();
String subscriptionArn = service.createSubscription(subscriptionRequest(monitorArn))
.path("SubscriptionArn").asText();
service.tagResource(tagRequest(monitorArn, "team", "data"));
service.tagResource(tagRequest(monitorArn, "env", "dev"));
ObjectNode untag = mapper.createObjectNode().put("ResourceArn", monitorArn);
untag.putArray("ResourceTagKeys").add("env");
service.untagResource(untag);
service.tagResource(tagRequest(subscriptionArn, "team", "web"));
assertEquals("ce", service.namespace());
assertEquals(Map.of("team", "data"), service.storedTags(monitorArn));
assertEquals(Map.of("team", "web"), service.storedTags(subscriptionArn));
assertTrue(service.storedTags(monitorArn.replace(ACCOUNT, "222222222222")).isEmpty());
assertTrue(service.storedTags(monitorArn.substring(0, monitorArn.lastIndexOf('/') + 1) + "missing").isEmpty());
assertTrue(service.storedTags("arn:aws:budgets::" + ACCOUNT + ":budget/tagged").isEmpty());
}
/** A monitor stored before storage was account-aware is read in place: the lookup migrates nothing. */
@Test
void storedTagsReadAPreAccountAwareMonitorInPlace() {
InMemoryStorage<String, ObjectNode> raw = new InMemoryStorage<>();
CostAnomalyService legacyState = new CostAnomalyService(new AccountAwareStorageBackend<>(raw, null, ACCOUNT),
AccountAwareStorageBackend.inMemory(ACCOUNT), mapper, new RegionResolver("us-east-1", ACCOUNT));
String arn = "arn:aws:ce::" + ACCOUNT + ":anomalymonitor/legacy";
ObjectNode record = mapper.createObjectNode();
record.putObject("definition").put("MonitorArn", arn);
record.putObject("tags").put("team", "data");
raw.put(arn, record);
assertEquals(Map.of("team", "data"), legacyState.storedTags(arn));
assertEquals(Set.of(arn), raw.keys());
}
@Test
void tagQuotaFailurePreservesExistingTagsAndKeepsThemOutOfDefinitions() {
ObjectNode request = customMonitor("tags");
@@ -230,6 +272,12 @@ class CostAnomalyServiceTest {
new RegionResolver("us-east-1", ACCOUNT));
}
private ObjectNode tagRequest(String arn, String key, String value) {
ObjectNode request = mapper.createObjectNode().put("ResourceArn", arn);
request.putArray("ResourceTags").addObject().put("Key", key).put("Value", value);
return request;
}
private ObjectNode customMonitor(String name) {
return mapper.valueToTree(Map.of("AnomalyMonitor", Map.of("MonitorName", name, "MonitorType", "CUSTOM",
"MonitorSpecification", Map.of("Dimensions", Map.of("Key", "LINKED_ACCOUNT",
@@ -1499,6 +1499,40 @@ class IamRequestResourceEnforcementIntegrationTest {
"{\"SubscriptionArnList\":[\"" + otherSubscription + "\"]}"), "ce:GetAnomalySubscriptions");
}
/**
* An {@code aws:ResourceTag} condition reads the tags Cost Explorer holds on the anomaly monitor a request names,
* as TagResource leaves them, so an allow on one team's tag lets the caller read that team's monitor and not
* another's.
*/
@Test
void aCostExplorerResourceTagConditionReadsTheTagsOfTheAnomalyMonitor() {
String tagged = anomalyMonitor("[{\"Key\":\"team\",\"Value\":\"web\"}]");
String other = anomalyMonitor("[{\"Key\":\"team\",\"Value\":\"web\"}]");
adminJson("ce", "AWSInsightsIndexService.TagResource")
.body("{\"ResourceArn\":\"" + tagged + "\",\"ResourceTags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}")
.post("/").then().statusCode(200);
User user = user("res-monitor-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"ce:GetAnomalyMonitors\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
send(user, "ce", "AWSInsightsIndexService.GetAnomalyMonitors", "1.1",
"{\"MonitorArnList\":[\"" + tagged + "\"]}").statusCode(200);
refusedJson(send(user, "ce", "AWSInsightsIndexService.GetAnomalyMonitors", "1.1",
"{\"MonitorArnList\":[\"" + other + "\"]}"), "ce:GetAnomalyMonitors");
}
/** The ARN of a custom anomaly monitor an administrator creates with the {@code tags} list, deleted afterwards. */
private String anomalyMonitor(String tags) {
String arn = adminJson("ce", "AWSInsightsIndexService.CreateAnomalyMonitor").body("{\"AnomalyMonitor\":"
+ "{\"MonitorName\":\"res-monitor-" + suffix() + "\",\"MonitorType\":\"CUSTOM\","
+ "\"MonitorSpecification\":{\"Dimensions\":{\"Key\":\"LINKED_ACCOUNT\",\"Values\":[\"" + ACCOUNT
+ "\"]}}},\"ResourceTags\":" + tags + "}")
.post("/").then().statusCode(200).extract().path("MonitorArn");
cleanup.register(() -> adminJson("ce", "AWSInsightsIndexService.DeleteAnomalyMonitor")
.body("{\"MonitorArn\":\"" + arn + "\"}").post("/"));
return arn;
}
/**
* A Cost Explorer cost read is checked on the billing view its {@code BillingViewArn} gives, and Floci answers the
* costs of none but the caller's primary view: another is refused, so a grant on it does not reach the account's