mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3tables): check S3 Tables requests under IAM enforcement (#8424)
Under IAM enforcement no S3 Tables request was checked: its model was not among the vendored REST routes and its namespace's request resources were not read, so no request mapped to an action and every call went through, DeleteTableBucket and the policy calls included. s3tables is now vendored by regen_rest_routes.py and read by the request-resources, tag-on-create and passed-role generators. A table bucket call is checked on the bucket its path names. A table's ARN ends in an id the service mints, which no request carries: a request names the table by its table bucket's ARN, its namespace and its name. A new generic rule in the request-resources generator reads such a type, found by its service by those path labels (or GetTable's query string members, when tableArn is absent), instead of filling the bucket's name from the table's name or reading the bucket's ARN as the table's. S3TablesStoredResourceArns answers it from the stored table, read only.
This commit is contained in:
1 parent
0a40ab728c
commit
92a61c2b86
22 files changed
+651
-44
No files matched your search
@@ -55,6 +55,8 @@ final class RequestResourceMembers {
|
||||
private static final Pattern LOCATION = Pattern.compile("\\{[A-Za-z0-9_]+}|\\?[A-Za-z0-9_.-]+");
|
||||
/** The one id a child type's format appends to its parent's. */
|
||||
private static final Pattern APPENDED_ID = Pattern.compile("[/:]\\$\\{\\w+}");
|
||||
/** A word of a child type's own and one id, which its format appends to its parent's whole resource. */
|
||||
private static final Pattern NESTED_ID = Pattern.compile("[/:][A-Za-z][A-Za-z0-9-]*[/:]\\$\\{\\w+}");
|
||||
/** The keys a resource found by members does not take: it is read no other way. */
|
||||
private static final List<String> FOUND_ONLY = List.of("arnOf", "fixed");
|
||||
/** The keys of a resource read from a member holding its ARN. */
|
||||
@@ -273,9 +275,11 @@ final class RequestResourceMembers {
|
||||
/**
|
||||
* A resource a create makes under a parent its action acts on, from a member holding the parent's ARN: the
|
||||
* parent's resource with its leading type word {@code parentWord} replaced by {@code childWord} and an open
|
||||
* id appended after {@code separator} ({@code listener/app/<lb>/<id>/*} under {@code loadbalancer/app/...}).
|
||||
* id appended after {@code infix} ({@code listener/app/<lb>/<id>/*} under {@code loadbalancer/app/...}, infix
|
||||
* {@code /}), or the parent's whole resource with a word of the child's own before the open id
|
||||
* ({@code bucket/<name>/table/*} under {@code bucket/<name>}, infix {@code /table/}).
|
||||
*/
|
||||
record Created(String type, String parent, Member arn, String parentWord, String childWord, String separator) {
|
||||
record Created(String type, String parent, Member arn, String parentWord, String childWord, String infix) {
|
||||
}
|
||||
|
||||
/** An ordered heterogeneous id list; unsupported ids retain wildcard authorization. */
|
||||
@@ -783,15 +787,18 @@ final class RequestResourceMembers {
|
||||
String parent = created.path("parent").asText("");
|
||||
Matcher child = formats.containsKey(type) ? RESOURCE_PART.matcher(formats.get(type).format()) : null;
|
||||
Matcher under = formats.containsKey(parent) ? RESOURCE_PART.matcher(formats.get(parent).format()) : null;
|
||||
boolean nested = child != null && under != null && child.matches() && under.matches()
|
||||
&& child.group(1).equals(under.group(1));
|
||||
if (child == null || under == null || !child.matches() || !under.matches()
|
||||
|| child.group(1).equals(under.group(1)) || !child.group(2).startsWith(under.group(2))
|
||||
|| !APPENDED_ID.matcher(child.group(2).substring(under.group(2).length())).matches()) {
|
||||
|| !child.group(2).startsWith(under.group(2))
|
||||
|| !(nested ? NESTED_ID : APPENDED_ID).matcher(child.group(2).substring(under.group(2).length()))
|
||||
.matches()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " creates a " + type + " for " + operation
|
||||
+ " under a " + parent + ", no parent of it");
|
||||
}
|
||||
String separator = child.group(2).substring(under.group(2).length(), under.group(2).length() + 1);
|
||||
String appended = child.group(2).substring(under.group(2).length());
|
||||
return new Created(type, parent, member(operation, created.path("arn").asText("")), under.group(1),
|
||||
child.group(1), separator);
|
||||
child.group(1), appended.substring(0, appended.indexOf("${")));
|
||||
}
|
||||
|
||||
private static Member member(String operation, String path) {
|
||||
|
||||
@@ -367,7 +367,7 @@ public final class RequestResources {
|
||||
AwsArnUtils.Arn parent = AwsArnUtils.parse(parentFormat.checked(named));
|
||||
String under = parent.resource().substring(created.parentWord().length());
|
||||
addNamed(arns, new AwsArnUtils.Arn(parent.partition(), parent.service(), parent.region(),
|
||||
parent.accountId(), created.childWord() + under + created.separator() + "*").toString(),
|
||||
parent.accountId(), created.childWord() + under + created.infix() + "*").toString(),
|
||||
false, partition, region, accountId, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,8 +155,15 @@ public class S3TablesController {
|
||||
@GET
|
||||
@Path("/get-table")
|
||||
public Response getTable(@Context HttpHeaders headers, @QueryParam("tableBucketARN") String tableBucketArn,
|
||||
@QueryParam("namespace") String namespace, @QueryParam("name") String name) {
|
||||
return Response.ok(tableRepresentation(service.getTable(decode(tableBucketArn), namespace, name, region(headers)))).build();
|
||||
@QueryParam("namespace") String namespace, @QueryParam("name") String name,
|
||||
@QueryParam("tableArn") String tableArn) {
|
||||
// A table ARN names the table whatever else the request gives, as IAM checks the request on it. Query
|
||||
// parameters arrive decoded, as IAM reads them, so they are not decoded again: a double-encoded ARN would
|
||||
// name one table to IAM and another here.
|
||||
S3Table table = tableArn != null
|
||||
? service.getTableByArn(tableArn, region(headers))
|
||||
: service.getTable(tableBucketArn, namespace, name, region(headers));
|
||||
return Response.ok(tableRepresentation(table)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -21,12 +21,14 @@ import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
public class S3TablesService {
|
||||
private static final Logger LOG = Logger.getLogger(S3TablesService.class);
|
||||
private static final String BUCKET_PREFIX = "bucket/";
|
||||
private static final String TABLE_INFIX = "/table/";
|
||||
private static final int LIST_PAGE_SIZE = 1000;
|
||||
|
||||
private final StorageBackend<String, TableBucket> store;
|
||||
@@ -124,7 +126,9 @@ public class S3TablesService {
|
||||
if (namespace.getTables().containsKey(name)) {
|
||||
throw conflict("The table already exists.");
|
||||
}
|
||||
S3Table table = new S3Table(name, bucket.getArn() + "/table/" + name, namespaceName, format,
|
||||
// AWS ends a table's ARN in an id it mints, not the table's name, so tables of one name in two namespaces
|
||||
// differ, and a rename keeps it.
|
||||
S3Table table = new S3Table(name, bucket.getArn() + TABLE_INFIX + UUID.randomUUID(), namespaceName, format,
|
||||
versionToken(), metadataLocation(metadata), now(), regionResolver.getAccountId(), metadata,
|
||||
encryptionConfiguration, storageClassConfiguration, tags);
|
||||
table.setWarehouseLocation(createWarehouse(region));
|
||||
@@ -146,6 +150,43 @@ public class S3TablesService {
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* The table {@code tableArn} names, as {@code GetTable} reads it when given one: in the table bucket the ARN's
|
||||
* resource starts with, whichever namespace holds it. A table persisted before ARNs ended in a minted id keeps
|
||||
* the name-based ARN it was stored with, and is found by it.
|
||||
*/
|
||||
public synchronized S3Table getTableByArn(String tableArn, String region) {
|
||||
int infix = tableArn == null ? -1 : tableArn.lastIndexOf(TABLE_INFIX);
|
||||
if (infix < 0) {
|
||||
throw notFound("The table does not exist.");
|
||||
}
|
||||
TableBucket bucket = getTableBucket(tableArn.substring(0, infix), region);
|
||||
S3Table table = bucket.getNamespaces().values().stream()
|
||||
.flatMap(namespace -> namespace.getTables().values().stream())
|
||||
.filter(candidate -> tableArn.equals(candidate.getArn()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> notFound("The table does not exist."));
|
||||
withWarehouses(bucket, List.of(table), region);
|
||||
return table;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN of the table a request names by its table bucket's ARN, its namespace and its name, as IAM asks for it
|
||||
* before the handler runs; empty when there is no such table. Reads only: a table without a warehouse yet keeps
|
||||
* it so.
|
||||
*/
|
||||
public synchronized Optional<String> tableArn(String tableBucketArn, String namespaceName, String name,
|
||||
String region) {
|
||||
try {
|
||||
return Optional.of(getTable(getTableBucket(tableBucketArn, region), namespaceName, name).getArn());
|
||||
} catch (AwsException e) {
|
||||
if (!"NotFoundException".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<S3Table> listTables(String tableBucketArn, String namespaceName, String prefix, String region) {
|
||||
TableBucket bucket = getTableBucket(tableBucketArn, region);
|
||||
List<S3Table> tables = new ArrayList<>();
|
||||
@@ -261,7 +302,6 @@ public class S3TablesService {
|
||||
sourceNamespace.getTables().remove(name);
|
||||
table.setNamespace(targetNamespaceName);
|
||||
table.setName(targetName);
|
||||
table.setArn(bucket.getArn() + "/table/" + targetName);
|
||||
touch(table);
|
||||
targetNamespace.getTables().put(targetName, table);
|
||||
persist(bucket, region);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.floci.aws.services.s3tables;
|
||||
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks S3 Tables of the resources a request names: the table a request names by its table bucket's ARN, its
|
||||
* namespace and its name, whose ARN AWS ends in an id the service mints and Floci in the table's name, so only the
|
||||
* stored table gives it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class S3TablesStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private final S3TablesService s3TablesService;
|
||||
|
||||
@Inject
|
||||
public S3TablesStoredResourceArns(S3TablesService s3TablesService) {
|
||||
this.s3TablesService = s3TablesService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "s3tables";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
if (!"Table".equals(type) || names.size() != 3) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return s3TablesService.tableArn(names.get(0), names.get(1), names.get(2), region);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user