mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(budgets): give resource-tag conditions the tags of the budget or budget action a request names (#8823)
The generated request resources give Budgets' budget and budgetAction types the aws:ResourceTag/ key, but no service answered the budgets namespace's stored tags, so a condition on a budget's or an action's tags saw none. BudgetsService now answers it: a budget records no ARN, so storedTags takes the ARN only when it is the one the budget's account and name give in the request's partition, an action's adding its id, then reads the record under the key the handlers read, in the request's account, in place. Tag calls already run one at a time and give the record a new tag list; the tag fields are now volatile.
This commit is contained in:
1 parent
72055e2690
commit
bb8a92b8fd
8 files changed
+166
-25
No files matched your search
@@ -4,12 +4,17 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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.services.budgets.model.BudgetActionRecord;
|
||||
import io.floci.aws.services.budgets.model.BudgetRecord;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -22,7 +27,8 @@ class BudgetsServiceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new BudgetsService(AccountAwareStorageBackend.inMemory(ACCOUNT),
|
||||
AccountAwareStorageBackend.<BudgetActionRecord>inMemory(ACCOUNT), mapper);
|
||||
AccountAwareStorageBackend.<BudgetActionRecord>inMemory(ACCOUNT), mapper,
|
||||
new RegionResolver("us-east-1", ACCOUNT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,6 +114,55 @@ class BudgetsServiceTest {
|
||||
assertEquals("EXECUTION_SUCCESS", history.path("Status").asText());
|
||||
}
|
||||
|
||||
/**
|
||||
* IAM's lookup reads the tags of the budget or the action the ARN names, as the tag calls leave them, and nothing
|
||||
* for another account's, another partition's, a Regional or an unknown ARN.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheBudgetOrTheActionTheArnNames() {
|
||||
ObjectNode create = createBudget("tagged");
|
||||
create.putArray("ResourceTags").addObject().put("Key", "team").put("Value", "web");
|
||||
service.createBudget(create);
|
||||
String arn = "arn:aws:budgets::" + ACCOUNT + ":budget/tagged";
|
||||
ObjectNode tag = mapper.createObjectNode().put("ResourceARN", arn);
|
||||
tag.putArray("ResourceTags").add(mapper.createObjectNode().put("Key", "team").put("Value", "data"))
|
||||
.add(mapper.createObjectNode().put("Key", "env").put("Value", "dev"));
|
||||
service.tagResource(tag);
|
||||
ObjectNode untag = mapper.createObjectNode().put("ResourceARN", arn);
|
||||
untag.putArray("ResourceTagKeys").add("env");
|
||||
service.untagResource(untag);
|
||||
ObjectNode action = actionRequest("tagged", "APPLY_IAM_POLICY");
|
||||
action.putArray("ResourceTags").addObject().put("Key", "team").put("Value", "ops");
|
||||
String actionArn = arn + "/action/" + service.createBudgetAction(action).getActionId();
|
||||
|
||||
assertEquals("budgets", service.namespace());
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn));
|
||||
assertEquals(Map.of("team", "ops"), service.storedTags(actionArn));
|
||||
assertTrue(service.storedTags(arn.replace(ACCOUNT, "210987654321")).isEmpty());
|
||||
assertTrue(service.storedTags(arn.replace("arn:aws:", "arn:aws-cn:")).isEmpty());
|
||||
assertTrue(service.storedTags(arn.replace("budgets::", "budgets:us-east-1:")).isEmpty());
|
||||
assertTrue(service.storedTags(arn.replace("budget/tagged", "budget/missing")).isEmpty());
|
||||
assertTrue(service.storedTags("arn:aws:ce::" + ACCOUNT + ":anomalymonitor/tagged").isEmpty());
|
||||
}
|
||||
|
||||
/** A budget stored before storage was account-aware is read in place: the lookup migrates nothing. */
|
||||
@Test
|
||||
void storedTagsReadAPreAccountAwareBudgetInPlace() {
|
||||
InMemoryStorage<String, BudgetRecord> raw = new InMemoryStorage<>();
|
||||
BudgetsService legacyState = new BudgetsService(new AccountAwareStorageBackend<>(raw, null, ACCOUNT),
|
||||
AccountAwareStorageBackend.<BudgetActionRecord>inMemory(ACCOUNT), mapper,
|
||||
new RegionResolver("us-east-1", ACCOUNT));
|
||||
BudgetRecord legacy = new BudgetRecord();
|
||||
legacy.setAccountId(ACCOUNT);
|
||||
legacy.setBudget(createBudget("legacy").get("Budget"));
|
||||
legacy.setResourceTags(mapper.createArrayNode().add(mapper.createObjectNode().put("Key", "team")
|
||||
.put("Value", "data")));
|
||||
raw.put(ACCOUNT + "::legacy", legacy);
|
||||
|
||||
assertEquals(Map.of("team", "data"), legacyState.storedTags("arn:aws:budgets::" + ACCOUNT + ":budget/legacy"));
|
||||
assertEquals(Set.of(ACCOUNT + "::legacy"), raw.keys());
|
||||
}
|
||||
|
||||
private ObjectNode createBudget(String name) {
|
||||
ObjectNode request = mapper.createObjectNode().put("AccountId", ACCOUNT);
|
||||
ObjectNode budget = request.putObject("Budget");
|
||||
|
||||
+36
-8
@@ -1680,14 +1680,8 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
void aBudgetIsCheckedOnTheArnItsNameGives() {
|
||||
String name = "res-budget-" + suffix();
|
||||
String other = name + "-other";
|
||||
for (String budget : List.of(name, other)) {
|
||||
adminJson("budgets", "AWSBudgetServiceGateway.CreateBudget")
|
||||
.body("{\"AccountId\":\"" + ACCOUNT + "\",\"Budget\":" + budget(budget, null) + "}").post("/")
|
||||
.then().statusCode(200);
|
||||
cleanup.register(() -> adminJson("budgets", "AWSBudgetServiceGateway.DeleteBudget")
|
||||
.body("{\"AccountId\":\"" + ACCOUNT + "\",\"BudgetName\":\"" + budget + "\"}").post("/"));
|
||||
}
|
||||
String arn = "arn:aws:budgets::" + ACCOUNT + ":budget/" + name;
|
||||
String arn = createdBudget(name, null);
|
||||
createdBudget(other, null);
|
||||
adminJson("budgets", "AWSBudgetServiceGateway.TagResource").body("{\"ResourceARN\":\"" + arn
|
||||
+ "\",\"ResourceTags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}").post("/").then().statusCode(200);
|
||||
|
||||
@@ -1703,6 +1697,40 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags Budgets holds on the budget a request names, as TagResource
|
||||
* leaves them, so an allow on one team's tag lets the caller read that team's budget and not another's.
|
||||
*/
|
||||
@Test
|
||||
void aBudgetsResourceTagConditionReadsTheTagsOfTheBudget() {
|
||||
String web = "[{\"Key\":\"team\",\"Value\":\"web\"}]";
|
||||
String tagged = "res-budget-tagged-" + suffix();
|
||||
String other = "res-budget-untagged-" + suffix();
|
||||
String taggedArn = createdBudget(tagged, web);
|
||||
createdBudget(other, web);
|
||||
adminJson("budgets", "AWSBudgetServiceGateway.TagResource").body("{\"ResourceARN\":\"" + taggedArn
|
||||
+ "\",\"ResourceTags\":[{\"Key\":\"team\",\"Value\":\"data\"}]}").post("/").then().statusCode(200);
|
||||
|
||||
User user = user("res-budget-tags");
|
||||
user.policy("{\"Effect\":\"Allow\",\"Action\":\"budgets:ViewBudget\",\"Resource\":\"*\","
|
||||
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
|
||||
budgets(user, "DescribeBudget", "{\"AccountId\":\"" + ACCOUNT + "\",\"BudgetName\":\"" + tagged + "\"}")
|
||||
.statusCode(200);
|
||||
refusedJson(budgets(user, "DescribeBudget", "{\"AccountId\":\"" + ACCOUNT + "\",\"BudgetName\":\"" + other
|
||||
+ "\"}"), "budgets:ViewBudget");
|
||||
}
|
||||
|
||||
/** The ARN of the budget {@code name} an administrator creates, with the {@code tags} list if any. */
|
||||
private String createdBudget(String name, String tags) {
|
||||
adminJson("budgets", "AWSBudgetServiceGateway.CreateBudget")
|
||||
.body("{\"AccountId\":\"" + ACCOUNT + "\",\"Budget\":" + budget(name, null)
|
||||
+ (tags == null ? "" : ",\"ResourceTags\":" + tags) + "}")
|
||||
.post("/").then().statusCode(200);
|
||||
cleanup.register(() -> adminJson("budgets", "AWSBudgetServiceGateway.DeleteBudget")
|
||||
.body("{\"AccountId\":\"" + ACCOUNT + "\",\"BudgetName\":\"" + name + "\"}").post("/"));
|
||||
return "arn:aws:budgets::" + ACCOUNT + ":budget/" + name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Cost and Usage Report definition is checked on {@code definition/<name>}, a put on the one its
|
||||
* {@code ReportDefinition.ReportName} names and a modify on the one its {@code ReportName} names, neither on one
|
||||
|
||||
Reference in new issue
Block a user