mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
4ac034d645
commit
2ac4554ea8
5 files changed
+124
-7
No files matched your search
@@ -3478,9 +3478,9 @@ floci populates:
|
||||
DB instances, DB clusters, parameter groups and backups, CloudTrail for its trails, Budgets for its budgets and budget
|
||||
actions, AWS FIS for its experiment templates, experiments and actions, CodePipeline for its pipelines, webhooks and
|
||||
custom action types, DataSync for its agents, locations and tasks, Global Accelerator for its accelerators, listeners
|
||||
and endpoint groups, EMR Serverless for its applications, job runs and sessions, and EMR on EKS for its virtual
|
||||
clusters, job runs, managed endpoints, job templates and security configurations. Tag keys differing only in case are
|
||||
one condition key holding every value.
|
||||
and endpoint groups, EMR Serverless for its applications, job runs and sessions, EMR on EKS for its virtual clusters,
|
||||
job runs, managed endpoints, job templates and security configurations, and Cost Explorer for its anomaly monitors and
|
||||
subscriptions. Tag keys differing only in case are one condition key holding every value.
|
||||
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
|
||||
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
|
||||
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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",
|
||||
|
||||
+34
@@ -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
|
||||
|
||||
Reference in new issue
Block a user