mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): check Application Auto Scaling requests on the scalable target they name (#7193)
Under IAM enforcement, Application Auto Scaling requests were checked on
`*`, so a grant scoped to a scalable target was refused and a deny
scoped to one never applied.
A scalable target's ARN ends in a unique id the service assigns,
`scalable-target/<id>` ("The unique ID ... is a value assigned by
Application Auto Scaling to the scalable target"), which the Service
Reference names ${ResourceId}, while requests name the target by
ServiceNamespace, ResourceId and ScalableDimension. Read as the table
reads other services, ResourceId (`service/<cluster>/<service>`) would
have filled that id, so the namespace was left out until the table could
look the ARN up.
The generator's new FOUND_BY gives a type whose ARN is all an id its
service mints, with the members a request names it by. An operation
whose request has them all names the type by them (`find`), generated
rather than corrected, and the reader asks the service for the ARN they
find through StoredResourceArns, the lookup EventBridge's connections
already use, or else checks the ARN with its id open, as for a create.
StoredResourceArns.find now takes the values a request names the
resource by, in the table's order: one name for EventBridge, the three
members for Application Auto Scaling, whose service now implements it
from the store its handlers read. A member left out or given a JSON
null names none, as the handler reads it.
Application Auto Scaling joins the table (14 operations, 1036 over 25
namespaces): RegisterScalableTarget, DeregisterScalableTarget,
PutScalingPolicy, DeleteScalingPolicy, PutScheduledAction and
DeleteScheduledAction are checked on the target's stored ARN, and a
RegisterScalableTarget registering a new one, its tags included, on
`scalable-target/*`; TagResource, UntagResource and
ListTagsForResource on the ARN their ResourceARN gives; the Describe
operations and GetPredictiveScalingForecast on `*`, as AWS checks them.
This commit is contained in:
1 parent
d39f06e733
commit
beef71e8eb
15 files changed
+372
-44
No files matched your search
+33
-3
@@ -79,6 +79,35 @@ class RequestResourceMembersTest {
|
||||
assertFalse(events.get("event-bus").minted());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aScalableTargetIsFoundByTheMembersItsServiceReadsNotFilledByItsResourceId() {
|
||||
List<Member> triple = List.of(new Member("ServiceNamespace", false), new Member("ResourceId", false),
|
||||
new Member("ScalableDimension", false));
|
||||
for (String operation : List.of("RegisterScalableTarget", "DeregisterScalableTarget", "PutScalingPolicy",
|
||||
"DeleteScalingPolicy", "PutScheduledAction", "DeleteScheduledAction")) {
|
||||
Resource target = RequestResourceMembers.table().operations().get("application-autoscaling:" + operation)
|
||||
.stream().filter(resource -> resource.arn() == null).findFirst().orElseThrow();
|
||||
assertEquals(triple, target.find(), operation);
|
||||
assertTrue(target.fill().isEmpty(), operation);
|
||||
}
|
||||
String formats = "\"arnFormats\":{\"a\":{\"target\":\"arn:${Partition}:a:${Region}:${Account}:target/${Id}\"}}";
|
||||
assertEquals(List.of(new Member("Namespace", false), new Member("Dimension", false)), parseUnchecked(document(
|
||||
formats, "{\"type\":\"target\",\"fill\":{},\"find\":[\"Namespace\",\"Dimension\"]}"))
|
||||
.operations().get("a:Op").getFirst().find());
|
||||
// No members, a list, members beside one filling it, no list of them, or beside members reading it otherwise.
|
||||
for (String bad : List.of("\"fill\":{},\"find\":[]", "\"fill\":{},\"find\":[\"Ids[]\"]",
|
||||
"\"fill\":{\"Id\":\"Id\"},\"find\":[\"Namespace\"]", "\"fill\":{},\"find\":\"Namespace\"",
|
||||
"\"fill\":{},\"find\":{\"a\":\"Namespace\"}",
|
||||
"\"fill\":{},\"find\":[\"Namespace\"],\"account\":\"registryId\"",
|
||||
"\"fill\":{},\"find\":[\"Namespace\"],\"absent\":{\"Namespace\":\"ecs\"}")) {
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> parse(document(formats, "{\"type\":\"target\"," + bad + "}")), bad);
|
||||
}
|
||||
// Nor beside an ARN member, which names the resource itself.
|
||||
assertThrows(IllegalStateException.class, () -> parse(document(formats,
|
||||
"{\"types\":[\"target\"],\"arn\":\"TargetArn\",\"find\":[\"Namespace\"]}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedTableFailsToLoad() {
|
||||
assertThrows(IllegalStateException.class, () -> parse("{\"operations\":{}}"));
|
||||
@@ -218,7 +247,8 @@ class RequestResourceMembersTest {
|
||||
ObjectNode body = MAPPER.createObjectNode();
|
||||
StringBuilder form = new StringBuilder("Action=" + name);
|
||||
Map<Member, String> values = resource.arn() != null ? Map.of(resource.arn(), arn)
|
||||
: resource.fill().values().stream().distinct().collect(Collectors.toMap(m -> m, m -> "x"));
|
||||
: Stream.concat(resource.fill().values().stream(), resource.find().stream()).distinct()
|
||||
.collect(Collectors.toMap(m -> m, m -> "x"));
|
||||
Map<Member, String> given = new LinkedHashMap<>(values);
|
||||
if (resource.account() != null) {
|
||||
given.put(resource.account(), account);
|
||||
@@ -273,8 +303,8 @@ class RequestResourceMembersTest {
|
||||
|
||||
/** Whether every request of its operation names the resource: no member names it and no member's value selects it. */
|
||||
private static boolean named(Resource resource) {
|
||||
return resource.arn() == null && resource.fill().isEmpty() && resource.when().isEmpty()
|
||||
&& resource.unless().isEmpty();
|
||||
return resource.arn() == null && resource.fill().isEmpty() && resource.find().isEmpty()
|
||||
&& resource.when().isEmpty() && resource.unless().isEmpty();
|
||||
}
|
||||
|
||||
/** The value at the member's path in a body: in its list's first element, and in its field's structure. */
|
||||
|
||||
@@ -338,9 +338,9 @@ class RequestResourcesTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, String name, String region) {
|
||||
return "web".equals(name) && "us-east-1".equals(region) ? Optional.of(events + type + "/web/1234")
|
||||
: Optional.empty();
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return List.of("web").equals(names) && "us-east-1".equals(region)
|
||||
? Optional.of(events + type + "/web/1234") : Optional.empty();
|
||||
}
|
||||
};
|
||||
assertEquals(List.of(events + "connection/web/1234"), RequestResources.of("events:DescribeConnection",
|
||||
@@ -356,6 +356,44 @@ class RequestResourcesTest {
|
||||
+ "connection/web/1234\"}"), "aws", "us-east-1", "000000000000", stored));
|
||||
}
|
||||
|
||||
/**
|
||||
* A scalable target's ARN is all the id Application Auto Scaling mints, while its requests name it by its service
|
||||
* namespace, resource id and scalable dimension: it is the one stored under those, or else has its id open, as a
|
||||
* new one has; a request leaving one out names none, and one naming it by its ARN names it as given.
|
||||
*/
|
||||
@Test
|
||||
void aResourceWhoseArnIsAllAMintedIdIsTheOneStoredUnderTheMembersNamingIt() {
|
||||
String targets = "arn:aws:application-autoscaling:us-east-1:000000000000:scalable-target/";
|
||||
StoredResourceArns stored = new StoredResourceArns() {
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "application-autoscaling";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return "ScalableTarget".equals(type) && "us-east-1".equals(region)
|
||||
&& List.of("ecs", "service/default/web", "ecs:service:DesiredCount").equals(names)
|
||||
? Optional.of(targets + "0123abcd") : Optional.empty();
|
||||
}
|
||||
};
|
||||
String web = "\"ServiceNamespace\":\"ecs\",\"ResourceId\":\"service/default/web\","
|
||||
+ "\"ScalableDimension\":\"ecs:service:DesiredCount\"";
|
||||
assertEquals(List.of(targets + "0123abcd"), RequestResources.of("application-autoscaling:PutScalingPolicy",
|
||||
json("{\"PolicyName\":\"p\"," + web + "}"), "aws", "us-east-1", "000000000000", stored));
|
||||
assertEquals(List.of(targets + "*"), RequestResources.of("application-autoscaling:RegisterScalableTarget",
|
||||
json("{" + web.replace("/web", "/api") + "}"), "aws", "us-east-1", "000000000000", stored));
|
||||
assertEquals(List.of(targets + "*"), of("application-autoscaling:DeregisterScalableTarget", json("{" + web + "}")));
|
||||
assertEquals(List.of(), RequestResources.of("application-autoscaling:DeregisterScalableTarget",
|
||||
json("{\"ServiceNamespace\":\"ecs\",\"ResourceId\":\"service/default/web\"}"), "aws", "us-east-1",
|
||||
"000000000000", stored));
|
||||
assertEquals(List.of(), RequestResources.of("application-autoscaling:DeregisterScalableTarget",
|
||||
json("{" + web.replace("\"ecs\",", "null,") + "}"), "aws", "us-east-1", "000000000000", stored));
|
||||
assertEquals(List.of(targets + "0123abcd"), RequestResources.of("application-autoscaling:TagResource",
|
||||
json("{\"ResourceARN\":\"" + targets + "0123abcd\",\"Tags\":{}}"), "aws", "us-east-1", "000000000000",
|
||||
stored));
|
||||
}
|
||||
|
||||
/** A request value is a name, so a {@code ${...}} in it is kept, not read as a variable of the format. */
|
||||
@Test
|
||||
void aValueIsTakenLiterally() {
|
||||
|
||||
+15
@@ -21,6 +21,7 @@ import java.util.Base64;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -619,6 +620,20 @@ class ApplicationAutoScalingServiceTest {
|
||||
assertEquals(0, service.describeScalableTargets(NAMESPACE, null, null, "eu-west-1").size());
|
||||
}
|
||||
|
||||
/** IAM finds a target's ARN, which ends in the id it was registered with, by the three members naming it. */
|
||||
@Test
|
||||
void aTargetIsFoundByItsNamespaceResourceAndDimension() {
|
||||
String arn = register().getScalableTargetArn();
|
||||
assertEquals("application-autoscaling", service.namespace());
|
||||
assertEquals(Optional.of(arn), service.find("ScalableTarget", List.of(NAMESPACE, RESOURCE_ID, DIMENSION), REGION));
|
||||
assertEquals(Optional.empty(), service.find("ScalableTarget",
|
||||
List.of(NAMESPACE, RESOURCE_ID, "ecs:service:Other"), REGION));
|
||||
assertEquals(Optional.empty(), service.find("ScalableTarget", List.of(NAMESPACE, RESOURCE_ID, DIMENSION),
|
||||
"eu-west-1"));
|
||||
assertEquals(Optional.empty(), service.find("ScalableTarget", List.of(RESOURCE_ID), REGION));
|
||||
assertEquals(Optional.empty(), service.find("other", List.of(NAMESPACE, RESOURCE_ID, DIMENSION), REGION));
|
||||
}
|
||||
|
||||
private TargetTrackingConfiguration withCooldowns(TargetTrackingConfiguration config) {
|
||||
config.setScaleInCooldown(240);
|
||||
config.setScaleOutCooldown(60);
|
||||
|
||||
+67
@@ -47,6 +47,7 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ELB = "arn:aws:elasticloadbalancing:" + REGION + ":" + ACCOUNT + ":";
|
||||
private static final String EVENTS = "arn:aws:events:" + REGION + ":" + ACCOUNT + ":";
|
||||
private static final String TARGETS = "arn:aws:application-autoscaling:" + REGION + ":" + ACCOUNT + ":scalable-target/";
|
||||
/** The AWS Load Balancer Controller's create, tag-on-create and listener statements (its policy v2.14.0). */
|
||||
private static final String CONTROLLER_CREATE = "{\"Effect\":\"Allow\",\"Action\":[\"elasticloadbalancing:CreateLoadBalancer\","
|
||||
+ "\"elasticloadbalancing:CreateTargetGroup\"],\"Resource\":\"*\","
|
||||
@@ -1128,6 +1129,72 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
+ "-other\"]}"), "translate:TranslateDocument");
|
||||
}
|
||||
|
||||
/**
|
||||
* A scalable target, named by its service namespace, resource id and scalable dimension, is checked on the ARN
|
||||
* Application Auto Scaling registered it under, which ends in the id it assigned, so a grant on that ARN allows
|
||||
* it, one on another target's does not, and a deny on it holds; registering a new one is checked with its id
|
||||
* open, and its tags by its ARN. Before, these were checked on {@code *}.
|
||||
*/
|
||||
@Test
|
||||
void aScalableTargetIsCheckedOnTheArnItWasRegisteredUnder() {
|
||||
String target = scalableTarget("service/res-" + suffix() + "/web");
|
||||
String arn = registerScalableTarget(target);
|
||||
assertTrue(arn.startsWith(TARGETS), arn);
|
||||
|
||||
User user = user("res-target");
|
||||
user.policy(allowOn("application-autoscaling:RegisterScalableTarget", arn));
|
||||
scaling(user, "RegisterScalableTarget", target + ",\"MaxCapacity\":3}").statusCode(200);
|
||||
user.policy(allowOn("application-autoscaling:RegisterScalableTarget", TARGETS + "other"));
|
||||
refusedJson(scaling(user, "RegisterScalableTarget", target + ",\"MaxCapacity\":3}"),
|
||||
"application-autoscaling:RegisterScalableTarget");
|
||||
// Its tags on an update are checked on the target too.
|
||||
user.policy(allowOn("application-autoscaling:RegisterScalableTarget", arn) + ","
|
||||
+ allowOn("application-autoscaling:TagResource", TARGETS + "other"));
|
||||
refusedJson(scaling(user, "RegisterScalableTarget", target + ",\"Tags\":{\"team\":\"a\"}}"),
|
||||
"application-autoscaling:TagResource");
|
||||
user.policy(allowOn("application-autoscaling:RegisterScalableTarget", arn) + ","
|
||||
+ allowOn("application-autoscaling:TagResource", arn));
|
||||
scaling(user, "RegisterScalableTarget", target + ",\"Tags\":{\"team\":\"a\"}}").statusCode(200);
|
||||
// A new target has no id yet, so it is checked on scalable-target/*, which a grant on another does not cover.
|
||||
String other = scalableTarget("service/res-" + suffix() + "/api");
|
||||
refusedJson(scaling(user, "RegisterScalableTarget", other + ",\"MinCapacity\":1,\"MaxCapacity\":2}"),
|
||||
"application-autoscaling:RegisterScalableTarget");
|
||||
user.policy(allowOn("application-autoscaling:RegisterScalableTarget", TARGETS + "*"));
|
||||
cleanup.register(() -> adminJson("application-autoscaling", "AnyScaleFrontendService.DeregisterScalableTarget")
|
||||
.body(other + "}").post("/"));
|
||||
scaling(user, "RegisterScalableTarget", other + ",\"MinCapacity\":1,\"MaxCapacity\":2}").statusCode(200);
|
||||
// A deny on the target holds, on a policy put on it and on its tags read by its ARN, and on no other target.
|
||||
user.policy(allowOn("application-autoscaling:*", "*") + ",{\"Effect\":\"Deny\",\"Action\":["
|
||||
+ "\"application-autoscaling:PutScalingPolicy\",\"application-autoscaling:ListTagsForResource\"],"
|
||||
+ "\"Resource\":\"" + arn + "\"}");
|
||||
refusedJson(scaling(user, "PutScalingPolicy", target.replace("{", "{\"PolicyName\":\"p\","
|
||||
+ "\"PolicyType\":\"StepScaling\",") + "}"), "application-autoscaling:PutScalingPolicy");
|
||||
refusedJson(scaling(user, "ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}"),
|
||||
"application-autoscaling:ListTagsForResource");
|
||||
scaling(user, "DeregisterScalableTarget", other + "}").statusCode(200);
|
||||
}
|
||||
|
||||
/** The members naming an ECS service's desired count as a scalable target, an open JSON object. */
|
||||
private static String scalableTarget(String resourceId) {
|
||||
return "{\"ServiceNamespace\":\"ecs\",\"ResourceId\":\"" + resourceId + "\","
|
||||
+ "\"ScalableDimension\":\"ecs:service:DesiredCount\"";
|
||||
}
|
||||
|
||||
/** Registers the scalable target as the admin and gives its ARN. */
|
||||
private String registerScalableTarget(String target) {
|
||||
String arn = adminJson("application-autoscaling", "AnyScaleFrontendService.RegisterScalableTarget")
|
||||
.body(target + ",\"MinCapacity\":1,\"MaxCapacity\":2}").post("/").then().statusCode(200)
|
||||
.extract().path("ScalableTargetARN");
|
||||
cleanup.register(() -> adminJson("application-autoscaling", "AnyScaleFrontendService.DeregisterScalableTarget")
|
||||
.body(target + "}").post("/"));
|
||||
return arn;
|
||||
}
|
||||
|
||||
private static ValidatableResponse scaling(User user, String operation, String body) {
|
||||
return user.json("application-autoscaling", "AnyScaleFrontendService." + operation, "1.1").body(body).post("/")
|
||||
.then();
|
||||
}
|
||||
|
||||
/** A monthly cost budget of the name, with a billing view ARN when one is given. */
|
||||
private static String budget(String name, String billingViewArn) {
|
||||
return "{\"BudgetName\":\"" + name + "\",\"BudgetType\":\"COST\",\"TimeUnit\":\"MONTHLY\",\"BudgetLimit\":"
|
||||
|
||||
Reference in new issue
Block a user