mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -47,6 +47,8 @@ final class RequestResourceMembers {
|
||||
private static final Pattern ONE_VARIABLE = Pattern.compile("[^$]*\\$\\{\\w+}");
|
||||
/** The one id a child type's format appends to its parent's. */
|
||||
private static final Pattern APPENDED_ID = Pattern.compile("[/:]\\$\\{\\w+}");
|
||||
/** The keys a resource found by members does not take: it is read no other way, in the caller's account. */
|
||||
private static final List<String> FOUND_ONLY = List.of("absent", "arnOf", "account", "fixed", "before");
|
||||
|
||||
/**
|
||||
* A top-level request member that holds a string, or a list of them when {@code list}, or, with a
|
||||
@@ -124,11 +126,20 @@ final class RequestResourceMembers {
|
||||
* {@code before}, when there is one: Step Functions Local's handler drops a test case after {@code #}, and a
|
||||
* CodeBuild build id is {@code <project>:<id>}, a build being checked on its project. A create naming other
|
||||
* resources beside the one it makes marks that one {@code made}, where its tags go (a webhook, not the pipeline
|
||||
* it connects to).
|
||||
* it connects to). A type whose ARN is all an id its service mints is named instead by the members its service
|
||||
* finds it by, {@code find}, each one value, in the order its service reads them, filling none of its variables
|
||||
* and read no other way, in the caller's account (a scalable target by its service namespace, resource id and
|
||||
* scalable dimension).
|
||||
*/
|
||||
record Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
|
||||
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
|
||||
Member account, String before, Map<String, String> fixed, boolean made) {
|
||||
Member account, String before, Map<String, String> fixed, boolean made, List<Member> find) {
|
||||
|
||||
Resource(List<String> types, Member arn, Map<String, Member> fill, Map<String, List<String>> when,
|
||||
Map<String, List<String>> unless, Map<String, String> absent, Map<String, String> arnOf,
|
||||
Member account, String before, Map<String, String> fixed, boolean made) {
|
||||
this(types, arn, fill, when, unless, absent, arnOf, account, before, fixed, made, List.of());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,6 +258,10 @@ final class RequestResourceMembers {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " reads an ARN for " + operation
|
||||
+ " of types with no format: " + types);
|
||||
}
|
||||
if (resource.has("find")) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " reads an ARN for " + operation
|
||||
+ " and finds its resource by " + resource.path("find") + " too");
|
||||
}
|
||||
return new Resource(types, arnMember(operation, resource.path("arn").asText("")), Map.of(), Map.of(),
|
||||
Map.of(), Map.of(), Map.of(), null, before, Map.of(), made);
|
||||
}
|
||||
@@ -274,6 +289,15 @@ final class RequestResourceMembers {
|
||||
}
|
||||
fixed.put(variable.getKey(), variable.getValue().asText());
|
||||
});
|
||||
List<Member> find = new ArrayList<>();
|
||||
for (JsonNode member : resource.path("find")) {
|
||||
find.add(member(operation, member.asText("")));
|
||||
}
|
||||
if (resource.has("find") && (!resource.path("find").isArray() || find.isEmpty() || !fill.isEmpty()
|
||||
|| find.stream().anyMatch(Member::list) || FOUND_ONLY.stream().anyMatch(resource::has))) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " finds a " + type + " for " + operation + " by "
|
||||
+ resource.path("find") + ", no members of one value each, or beside members reading it otherwise");
|
||||
}
|
||||
if (fill.values().stream().filter(Member::list).count() > 1) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " fills " + type + " for " + operation
|
||||
+ " from more than one list");
|
||||
@@ -306,7 +330,7 @@ final class RequestResourceMembers {
|
||||
+ " from " + account.name() + ", no member of one account or a type of none");
|
||||
}
|
||||
return new Resource(List.of(type), null, Map.copyOf(fill), when, unless, Map.copyOf(absent), Map.copyOf(arnOf),
|
||||
account, before, Map.copyOf(fixed), made);
|
||||
account, before, Map.copyOf(fixed), made, List.copyOf(find));
|
||||
}
|
||||
|
||||
/** The values each member named in {@code when} or {@code unless} may have, an empty one for the member absent. */
|
||||
|
||||
@@ -23,7 +23,9 @@ import java.util.regex.Pattern;
|
||||
* resource type the action acts on, or by members filling the variables of a type's format, the others open as
|
||||
* {@code *} (a resource a request creates has the id its service mints). A type whose service follows the name a
|
||||
* request gives with an id it mints ({@code connection/<name>/<id>}) is the resource its service stores under that
|
||||
* name, or else has that id open. An ARN with a qualifier its service drops
|
||||
* name, or else has that id open, and so is a type whose ARN is all an id its service mints, which a request names
|
||||
* by members its ARN does not hold (a scalable target, {@code scalable-target/<id>}, by its service namespace,
|
||||
* resource id and scalable dimension), under those members' values. An ARN with a qualifier its service drops
|
||||
* to check it is checked without one (a state machine's version or alias, on the state machine). An ARN naming
|
||||
* another account or Region than the request's is checked as given and as each resource of that name a handler may
|
||||
* act on instead, in the ARN's account or the caller's and in the ARN's Region or the request's, and the request is
|
||||
@@ -61,8 +63,8 @@ public final class RequestResources {
|
||||
|
||||
/**
|
||||
* As {@link #of(String, ContainerRequestContext, String, String, String)}, a resource of a type whose service
|
||||
* mints an id after its name being the one {@code stored} finds for the caller under the name the request gives,
|
||||
* or else its ARN with that id open, as it is with no {@code stored}.
|
||||
* mints an id after its name, or as all of its ARN, being the one {@code stored} finds for the caller under the
|
||||
* name or the members the request gives, or else its ARN with that id open, as it is with no {@code stored}.
|
||||
*/
|
||||
public static List<String> of(String operation, ContainerRequestContext ctx, String partition, String region,
|
||||
String accountId, StoredResourceArns stored) {
|
||||
@@ -93,6 +95,8 @@ public final class RequestResources {
|
||||
.findFirst().ifPresent(format -> addNamed(arns, format.checked(named), partition, region,
|
||||
accountId));
|
||||
}
|
||||
} else if (!resource.find().isEmpty()) {
|
||||
found(arns, resource, formats, form, body, partition, region, accountId, stored);
|
||||
} else {
|
||||
fill(arns, resource, formats, form, body, partition, region, accountId, stored);
|
||||
}
|
||||
@@ -323,6 +327,29 @@ public final class RequestResources {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of a type whose ARN is all an id its service mints, named by the members the table gives it
|
||||
* ({@code find}), when the request gives each one value: the caller's resource {@code stored} finds under those
|
||||
* values in the Region, or else its ARN with that id open, as for a create; none when it leaves one out or gives
|
||||
* it a JSON null, which its handler reads as none.
|
||||
*/
|
||||
private static void found(Set<String> arns, RequestResourceMembers.Resource resource,
|
||||
Map<String, RequestResourceMembers.ArnFormat> formats, Map<String, String> form,
|
||||
JsonNode body, String partition, String region, String accountId,
|
||||
StoredResourceArns stored) {
|
||||
List<String> names = new ArrayList<>();
|
||||
for (RequestResourceMembers.Member member : resource.find()) {
|
||||
List<String> values = values(form, body, member);
|
||||
if (isNull(body, member) || values.size() != 1) {
|
||||
return;
|
||||
}
|
||||
names.add(values.getFirst());
|
||||
}
|
||||
String type = resource.types().getFirst();
|
||||
Optional<String> found = stored == null ? Optional.empty() : stored.find(type, names, region);
|
||||
arns.add(found.orElseGet(() -> render(formats.get(type).format(), Map.of(), partition, region, accountId)));
|
||||
}
|
||||
|
||||
/** Whether a member that may give an ARN of a type instead of its name gives one. */
|
||||
private static boolean isArnOf(RequestResourceMembers.ArnFormat named, String value) {
|
||||
return named != null && named.pattern().matcher(value).matches();
|
||||
@@ -339,7 +366,7 @@ public final class RequestResources {
|
||||
return arn;
|
||||
}
|
||||
if (stored != null && filling.size() == 1) {
|
||||
Optional<String> found = stored.find(type, filling.values().iterator().next(), region);
|
||||
Optional<String> found = stored.find(type, List.of(filling.values().iterator().next()), region);
|
||||
if (found.isPresent()) {
|
||||
return found.get();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Finds the ARN a service stores a resource under, for a type of the request-resources table whose service follows
|
||||
* the name a request gives with an id it mints ({@code connection/<name>/<id>}), which only the service knows.
|
||||
* Finds the ARN a service stores a resource under, for a type of the request-resources table whose ARN carries an id
|
||||
* the service mints, which only the service knows: after the name a request gives ({@code connection/<name>/<id>}),
|
||||
* or as all of it, a request naming the resource by members its ARN does not hold
|
||||
* ({@code scalable-target/<id>}, named by its service namespace, resource id and scalable dimension).
|
||||
*
|
||||
* <p>Implemented by the service that owns such resources (EventBridge, for its connections and API destinations);
|
||||
* looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>} for the namespace a
|
||||
* request is of, so IAM never depends on that service directly.</p>
|
||||
* <p>Implemented by the service that owns such resources (EventBridge, for its connections and API destinations;
|
||||
* Application Auto Scaling, for its scalable targets); looked up lazily by {@code ResourceArnBuilder} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request is of, so IAM never depends on that service
|
||||
* directly.</p>
|
||||
*/
|
||||
public interface StoredResourceArns {
|
||||
|
||||
@@ -16,8 +20,9 @@ public interface StoredResourceArns {
|
||||
String namespace();
|
||||
|
||||
/**
|
||||
* The ARN the caller's resource of the type, named {@code name} in the Region, is stored under; empty when there
|
||||
* is none, or the type is not one it mints an id for.
|
||||
* The ARN the caller's resource of the type in the Region is stored under, named by {@code names}, the values the
|
||||
* request names it by in the order the table reads them: a connection's name, or a scalable target's service
|
||||
* namespace, resource id and scalable dimension; empty when there is none, or the type is not one it finds.
|
||||
*/
|
||||
Optional<String> find(String type, String name, String region);
|
||||
Optional<String> find(String type, List<String> names, String region);
|
||||
}
|
||||
+19
-1
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.common.SignedMarkers;
|
||||
import io.github.hectorvent.floci.core.common.StoredResourceArns;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.applicationautoscaling.model.Alarm;
|
||||
@@ -46,7 +47,7 @@ import java.util.UUID;
|
||||
* @see <a href="https://docs.aws.amazon.com/autoscaling/application/APIReference/Welcome.html">Application Auto Scaling API Reference</a>
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class ApplicationAutoScalingService {
|
||||
public class ApplicationAutoScalingService implements StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ApplicationAutoScalingService.class);
|
||||
private static final int MAX_TARGET_PAGE_SIZE = 50;
|
||||
@@ -704,6 +705,23 @@ public class ApplicationAutoScalingService {
|
||||
return targets.get(targetKey(region, serviceNamespace, resourceId, scalableDimension));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "application-autoscaling";
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of the caller's scalable target in the Region named by its service namespace, resource id and scalable
|
||||
* dimension, in that order, which ends in the id it was registered with, as its handlers look it up.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
if (!"ScalableTarget".equals(type) || names.size() != 3) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return findTarget(region, names.get(0), names.get(1), names.get(2)).map(ScalableTarget::getScalableTargetArn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looked up by {@link ScalingPolicyAlarmActionHandler} when an alarm's AlarmActions references
|
||||
* a scaling-policy ARN, and by the CloudFormation provisioner, whose physical id for a policy
|
||||
|
||||
+7
-3
@@ -1625,11 +1625,15 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of the caller's connection or API destination of the name in the Region, which follows the name with
|
||||
* the id it was minted with, as its handlers look it up.
|
||||
* The ARN of the caller's connection or API destination of the one name in the Region, which follows the name
|
||||
* with the id it was minted with, as its handlers look it up.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> find(String type, String name, String region) {
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
if (names.size() != 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String name = names.getFirst();
|
||||
return switch (type) {
|
||||
case "connection" -> connectionStore.get(connectionKey(region, name)).map(Connection::getConnectionArn);
|
||||
case "api-destination" -> apiDestinationStore.get(apiDestinationKey(region, name)).map(ApiDestination::getArn);
|
||||
|
||||
@@ -101,7 +101,7 @@ public class ResourceArnBuilder {
|
||||
private final Instance<KmsService> kmsService;
|
||||
/**
|
||||
* Looked up lazily for the same reason, to read the ARN a resource the request-resources table names by a name
|
||||
* its service follows with a minted id is stored under.
|
||||
* its service follows with a minted id, or by members its minted ARN does not hold, is stored under.
|
||||
*/
|
||||
private final Instance<StoredResourceArns> storedResourceArns;
|
||||
|
||||
@@ -219,8 +219,8 @@ public class ResourceArnBuilder {
|
||||
/**
|
||||
* The resources a request of a namespace this builder has no branch for names, from the vendored table of
|
||||
* request resources (see {@link RequestResources}); {@code *} when it names none, or the operation is not one
|
||||
* of the namespace. A resource whose service mints an id after its name is the one the namespace's
|
||||
* {@link StoredResourceArns} finds under that name.
|
||||
* of the namespace. A resource whose service mints an id after its name, or as all of its ARN, is the one the
|
||||
* namespace's {@link StoredResourceArns} finds under that name or the members naming it.
|
||||
*/
|
||||
private List<String> namedResources(String credentialScope, String operation, ContainerRequestContext ctx,
|
||||
String region, String accountId, String partition) {
|
||||
|
||||
Reference in new issue
Block a user