mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(scheduler): check EventBridge Scheduler requests from the generated request resources (#8406)
EventBridge Scheduler was built by hand in ResourceArnBuilder. It now moves
out of HAND_BUILT into NAMESPACES, so every operation's resources come from
iam-request-resources.json, and the hand builder and the enforcement filter's
routing to it are gone.
A schedule a request names with no group is in the default group, as
botocore documents GroupName: "If you omit this, the default schedule group
is used" (CreateSchedule, DeleteSchedule), and "EventBridge Scheduler
assumes that the schedule is associated with the default group"
(GetSchedule, UpdateSchedule). The rule that reads ECS's "If you do not
specify a cluster, the default cluster is assumed" now reads this phrasing
too, and SchedulerService answers the default group's ARN, reading only. A
resource already chosen by whether its member names the default (an
EventBridge rule on the default bus or on another) takes no default of its
own, so EventBridge's entries stay as they are.
Tagging is checked on the group Scheduler's modeled /tags/{ResourceArn}
carries, the only tagging route AWS gives it. The shared tags controller
now serves a namespace the generated table reads whole only on the routes
its model gives: the bare /tags, or an ARN whose slash is not encoded, is
refused for Scheduler, EMR Serverless, GuardDuty and Security Hub with the
controller's existing "Invalid resource ARN", and tags nothing, rather than
being checked on * where a Deny on the resource would not apply.
This commit is contained in:
1 parent
1b8e4fa503
commit
4a70910c53
24 files changed
+511
-176
No files matched your search
@@ -35,7 +35,8 @@ public abstract class AbstractTagsController {
|
||||
|
||||
@GET
|
||||
@Path("/{arn: .+}")
|
||||
public Response listTags(@Context HttpHeaders headers, @PathParam("arn") String arn) {
|
||||
public Response listTags(@Context HttpHeaders headers, @Context UriInfo uriInfo, @PathParam("arn") String arn) {
|
||||
dispatcher.resolveHandler(arn, uriInfo);
|
||||
return dispatcher.listTagsForArn(headers, arn);
|
||||
}
|
||||
|
||||
@@ -43,9 +44,10 @@ public abstract class AbstractTagsController {
|
||||
@Path("/{arn: .+}")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response tagResourcePost(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@PathParam("arn") String arn,
|
||||
String body) {
|
||||
TagHandler handler = dispatcher.resolveHandler(arn);
|
||||
TagHandler handler = dispatcher.resolveHandler(arn, uriInfo);
|
||||
return dispatcher.tagResourcePost(headers, arn, body,
|
||||
Response.status(handler.tagResourceSuccessStatus()).build());
|
||||
}
|
||||
@@ -54,9 +56,10 @@ public abstract class AbstractTagsController {
|
||||
@Path("/{arn: .+}")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response tagResourcePut(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@PathParam("arn") String arn,
|
||||
String body) {
|
||||
TagHandler handler = dispatcher.resolveHandler(arn);
|
||||
TagHandler handler = dispatcher.resolveHandler(arn, uriInfo);
|
||||
return dispatcher.tagResourcePut(headers, arn, body,
|
||||
Response.status(handler.tagResourceSuccessStatus()).build());
|
||||
}
|
||||
@@ -66,7 +69,7 @@ public abstract class AbstractTagsController {
|
||||
public Response untagResource(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@PathParam("arn") String arn) {
|
||||
TagHandler handler = dispatcher.resolveHandler(arn);
|
||||
TagHandler handler = dispatcher.resolveHandler(arn, uriInfo);
|
||||
return dispatcher.untagResourceForArn(headers, uriInfo, arn,
|
||||
Response.status(handler.untagResourceSuccessStatus()).build(), handler);
|
||||
}
|
||||
|
||||
@@ -1839,7 +1839,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
/**
|
||||
* Logs, Secrets Manager and KMS targets depend on the serving operation and the request's partition, STS
|
||||
* targets on the serving operation;
|
||||
* Scheduler and Pipes targets on the request's partition, which those services mint their ARNs in, as are
|
||||
* Pipes and DSQL targets on the request's partition, which those services mint their ARNs in, as are
|
||||
* API Gateway's path ARNs; and the
|
||||
* targets of a namespace the table of request resources reads on {@code operation}, the one the request is
|
||||
* dispatched as (see {@link RequestResources}).
|
||||
@@ -1855,8 +1855,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if ("sts".equals(credentialScope)) {
|
||||
return arnBuilder.buildStsResources(action, ctx);
|
||||
}
|
||||
if ("scheduler".equals(credentialScope) || "pipes".equals(credentialScope) || "dsql".equals(credentialScope)
|
||||
|| "apigateway".equals(credentialScope)) {
|
||||
if ("pipes".equals(credentialScope) || "dsql".equals(credentialScope) || "apigateway".equals(credentialScope)) {
|
||||
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
|
||||
}
|
||||
// ARNs the table renders take the request's partition, which a Region Floci does not know cannot be mapped to.
|
||||
|
||||
@@ -72,6 +72,15 @@ public final class RequestResources {
|
||||
return namespace != null && RequestResourceMembers.table().handBuilt().contains(namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the table reads this namespace whole, every operation's resources from its request as modeled: one it
|
||||
* covers that {@code ResourceArnBuilder} does not build by hand, which a few of its operations may be read for.
|
||||
*/
|
||||
public static boolean readsNamespace(String namespace) {
|
||||
return namespace != null && RequestResourceMembers.table().namespaces().contains(namespace)
|
||||
&& !builtByHand(namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARNs of the resources a request names, in the request's partition, Region and account: empty when the
|
||||
* table does not read the operation or its request names none, so it is checked on {@code *}. The operation,
|
||||
|
||||
@@ -26,6 +26,10 @@ import jakarta.ws.rs.core.UriInfo;
|
||||
* ({@code arn:aws:<service>:<region>:<account>:<resource>}) and dispatches to the
|
||||
* matching {@link TagHandler}.
|
||||
*
|
||||
* <p>A service whose IAM resources the generated table reads whole is served here only on the routes its AWS model
|
||||
* gives ({@link TagDispatcher#resolveHandler(String, UriInfo)}): the bare {@code /tags}, or an ARN whose
|
||||
* {@code /} is not encoded, is refused for it, as AWS has no such route for that service.
|
||||
*
|
||||
* <p>The resolution and wire-shape logic lives in {@link TagDispatcher}, shared with
|
||||
* {@link V1TagsController}, which does the same job for the services AWS puts on
|
||||
* {@code /v1/tags/{resourceArn}} instead.
|
||||
@@ -43,15 +47,17 @@ public class SharedTagsController extends AbstractTagsController {
|
||||
|
||||
@GET
|
||||
public Response listTagsByQuery(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@QueryParam("resourceArn") String arn) {
|
||||
dispatcher().resolveHandler(arn, uriInfo);
|
||||
return dispatcher().listTagsForArn(headers, arn);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response tagResourceByBody(@Context HttpHeaders headers, String body) {
|
||||
public Response tagResourceByBody(@Context HttpHeaders headers, @Context UriInfo uriInfo, String body) {
|
||||
String arn = dispatcher().readResourceArn(body);
|
||||
TagHandler handler = dispatcher().resolveHandler(arn);
|
||||
TagHandler handler = dispatcher().resolveHandler(arn, uriInfo);
|
||||
return dispatcher().doTagResource(headers, handler, arn, body,
|
||||
Response.ok(dispatcher().emptyObject()).build());
|
||||
}
|
||||
@@ -60,7 +66,8 @@ public class SharedTagsController extends AbstractTagsController {
|
||||
public Response untagResourceByQuery(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@QueryParam("resourceArn") String arn) {
|
||||
TagHandler handler = dispatcher().resolveHandler(arn, uriInfo);
|
||||
return dispatcher().untagResourceForArn(headers, uriInfo, arn,
|
||||
Response.ok(dispatcher().emptyObject()).build());
|
||||
Response.ok(dispatcher().emptyObject()).build(), handler);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.services.iam.IamActionRegistry;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
@@ -209,16 +210,36 @@ public class TagDispatcher {
|
||||
try {
|
||||
serviceKey = AwsArnUtils.parse(arn).service();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Invalid resource ARN: " + arn, 400);
|
||||
throw invalidArn(arn);
|
||||
}
|
||||
TagHandler handler = handlersByServiceKey.get(serviceKey);
|
||||
if (handler == null) {
|
||||
// Surface an unregistered service as an invalid-ARN error so floci's
|
||||
// internal routing isn't leaked to the client.
|
||||
throw new AwsException("BadRequestException",
|
||||
"Invalid resource ARN: " + arn, 400);
|
||||
throw invalidArn(arn);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* The handler of the service the ARN names, for a request on a path that service's AWS model gives. AWS serves
|
||||
* a service's tagging on its own routes only, telling the services apart by hostname, so a service whose IAM
|
||||
* resources the generated table reads from its modeled routes ({@link RequestResources#readsNamespace}) is
|
||||
* served on those paths alone: the bare {@code /tags}, or a path whose ARN's {@code /} is not encoded, is
|
||||
* refused as an ARN this path does not serve when its model has no such path
|
||||
* ({@link IamActionRegistry#modelRoutes}). A method its model does not give on the path is the handler's to
|
||||
* answer.
|
||||
*/
|
||||
public TagHandler resolveHandler(String arn, UriInfo uriInfo) {
|
||||
TagHandler handler = resolveHandler(arn);
|
||||
String service = AwsArnUtils.parse(arn).service();
|
||||
if (RequestResources.readsNamespace(service) && !IamActionRegistry.modelRoutes(service, uriInfo)) {
|
||||
throw invalidArn(arn);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
private static AwsException invalidArn(String arn) {
|
||||
return new AwsException("BadRequestException", "Invalid resource ARN: " + arn, 400);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
|
||||
/**
|
||||
* Dispatcher for the AWS services that put their tag endpoints on {@code /v1/tags/{resourceArn}}
|
||||
@@ -46,7 +47,9 @@ public class V1TagsController extends AbstractTagsController {
|
||||
@POST
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response listTagsForResourceByQuery(@Context HttpHeaders headers,
|
||||
@Context UriInfo uriInfo,
|
||||
@QueryParam("resourceArn") String resourceArn) {
|
||||
dispatcher().resolveHandler(resourceArn, uriInfo);
|
||||
return dispatcher().listTagsForArn(headers, resourceArn);
|
||||
}
|
||||
}
|
||||
@@ -743,6 +743,18 @@ public class IamActionRegistry {
|
||||
.map(IamRestRoutes.Match::labels).orElse(Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the path of a request sent to the URI {@code uriInfo} gives is one of the credential scope's vendored
|
||||
* model, whatever method it is sent with: matched still percent-encoded, so an encoded {@code /} stays inside
|
||||
* its label and an unencoded one ends it. True for a scope whose routes are not vendored, which the model
|
||||
* cannot answer for.
|
||||
*/
|
||||
public static boolean modelRoutes(String credentialScope, UriInfo uriInfo) {
|
||||
IamRestRoutes routes = IamRestRoutes.vendored();
|
||||
return !routes.routes(credentialScope)
|
||||
|| routes.takesPath(credentialScope, sentPath(uriInfo), uriInfo.getQueryParameters());
|
||||
}
|
||||
|
||||
/** Keep the modeled operation when AWS authorizes its route under a different action name. */
|
||||
public String operationForRoute(String credentialScope, String action, ContainerRequestContext ctx) {
|
||||
if (credentialScope == null || action == null) {
|
||||
|
||||
@@ -185,6 +185,15 @@ final class IamRestRoutes {
|
||||
return routes.containsKey(scope);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a route of the credential scope, of any method, takes the path and query: the path is one of its
|
||||
* model's, whatever method a request sends to it.
|
||||
*/
|
||||
boolean takesPath(String scope, String path, MultivaluedMap<String, String> query) {
|
||||
return routes.getOrDefault(scope, Map.of()).keySet().stream()
|
||||
.anyMatch(method -> match(scope, method, path, query).isPresent());
|
||||
}
|
||||
|
||||
static IamRestRoutes parse(InputStream in) throws IOException {
|
||||
JsonNode document = MAPPER.readTree(in);
|
||||
JsonNode handRouted = document == null ? null : document.path("handRouted");
|
||||
|
||||
@@ -177,7 +177,7 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* The resources a request is checked against. Scheduler, Pipes and DSQL ARNs are minted in
|
||||
* The resources a request is checked against. Pipes and DSQL ARNs are minted in
|
||||
* {@code partition}, the request's, as those services mint theirs, so a Region no partition publishes gets
|
||||
* the deployment's partition rather than {@code aws}.
|
||||
*/
|
||||
@@ -210,7 +210,6 @@ public class ResourceArnBuilder {
|
||||
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));
|
||||
case "scheduler" -> List.of(buildSchedulerArn(path, ctx, region, accountId, partition));
|
||||
case "pipes" -> List.of(buildPipesArn(path, ctx, region, accountId, partition));
|
||||
case "dsql" -> List.of(buildDsqlArn(path, ctx, region, accountId, partition));
|
||||
case "eks" -> List.of(buildEksArn(path, ctx, region, accountId));
|
||||
@@ -1227,37 +1226,6 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an EventBridge Scheduler route: {@code schedule/<group>/<name>}, the group read where
|
||||
* Scheduler reads it, the body's {@code GroupName} on a create or an update and {@code groupName}
|
||||
* otherwise, and {@code default} when the request names none; {@code schedule-group/<name>}; or the
|
||||
* ARN a tagging route names. The list routes name no resource.
|
||||
*/
|
||||
private String buildSchedulerArn(String path, ContainerRequestContext ctx, String region, String accountId,
|
||||
String partition) {
|
||||
String schedule = segmentAfterPrefix(path, "/schedules/");
|
||||
if (schedule != null) {
|
||||
String group;
|
||||
if ("POST".equalsIgnoreCase(ctx.getMethod()) || "PUT".equalsIgnoreCase(ctx.getMethod())) {
|
||||
JsonNode body = readJsonBody(ctx);
|
||||
group = body == null ? null : body.path("GroupName").asText(null);
|
||||
} else {
|
||||
group = ctx.getUriInfo().getQueryParameters().getFirst("groupName");
|
||||
}
|
||||
if (group == null || group.isBlank()) {
|
||||
group = "default";
|
||||
}
|
||||
return new AwsArnUtils.Arn(partition, "scheduler", region, accountId,
|
||||
"schedule/" + group + "/" + schedule).toString();
|
||||
}
|
||||
String scheduleGroup = segmentAfterPrefix(path, "/schedule-groups/");
|
||||
if (scheduleGroup != null) {
|
||||
return new AwsArnUtils.Arn(partition, "scheduler", region, accountId,
|
||||
"schedule-group/" + scheduleGroup).toString();
|
||||
}
|
||||
return taggedResource(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an Aurora DSQL route, as the Service Authorization Reference names it: the cluster
|
||||
* a cluster route names, the stream a stream route names (its cluster, to create or list streams), or
|
||||
|
||||
@@ -12,6 +12,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.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -26,10 +27,11 @@ import java.time.DateTimeException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SchedulerService {
|
||||
public class SchedulerService implements StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SchedulerService.class);
|
||||
|
||||
@@ -71,6 +73,28 @@ public class SchedulerService {
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
// ──────────────────────────── IAM lookups ────────────────────────────
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "scheduler";
|
||||
}
|
||||
|
||||
/** Scheduler mints no part of an ARN a request names, so there is nothing to look up by name. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The schedule group Scheduler acts on for a request naming none, {@code default}, which every account has in
|
||||
* every Region. Reads only: the group is not stored until a request uses it.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> findDefault(String type, String region) {
|
||||
return "schedule-group".equals(type) ? Optional.of(buildGroupArn(region, DEFAULT_GROUP)) : Optional.empty();
|
||||
}
|
||||
|
||||
// ──────────────────────────── Schedule Groups ────────────────────────────
|
||||
|
||||
public ScheduleGroup getOrCreateDefaultGroup(String region) {
|
||||
|
||||
Reference in new issue
Block a user