mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): read S3's actions from its user guide, by model and by version (#9147)
Where the Service Reference gives an S3 operation no one answer, S3's user guide does. tools/aws/fetch_s3_required_actions.py copies its "Required permissions for Amazon S3 API operations" rows unchanged into tools/aws/sources/s3-required-actions.json, and the authorized-actions generator reads them: an operation's one required action (ListObjectsV2 is s3:ListBucket, CopyObject s3:PutObject), the action for one model's requests of an operation S3 and S3 Control both have (authorizedByModel: S3 Control's account-level GetPublicAccessBlock is s3:GetAccountPublicAccessBlock), and an Either row's choice by versionId where botocore gives it in the query (versioned). Measured entries quote the Service Authorization Reference for the account-level GetPublicAccessBlock the page mislabels and the version annotation actions it omits. The one answer the reference gives an operation several clients list goes to the namesake client (S3's GetBucketLifecycleConfiguration is s3:GetLifecycleConfiguration), and the file records the reference's spelling of an action named as its operation in another case (MemoryDB's CreateACL is memorydb:CreateAcl). IamAuthorizedActions reads the new sections and gives an action for a route's model and query, which IamActionRegistry uses wherever it reads a route's action. S3 stays hand-routed, so its rules still decide its requests; only the spellings show, in the action an AccessDenied message names. A hand-routed scope's routes list what their requests must carry, for the coverage test, which now also checks each model-specific action against that model's routes.
This commit is contained in:
1 parent
d9914bfb93
commit
84cea7714a
16 files changed
+2509
-162
No files matched your search
@@ -710,12 +710,13 @@ public class IamActionRegistry {
|
||||
return rule.action();
|
||||
}
|
||||
}
|
||||
Optional<String> operation = restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, path),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx),
|
||||
ctx.getHeaders()).map(IamRestRoutes.Match::operation);
|
||||
if (operation.isPresent()) {
|
||||
String action = IamAuthorizedActions.actionFor(credentialScope, operation.get(), method);
|
||||
if (checkedAsAws(credentialScope, operation.get(), action)) {
|
||||
MultivaluedMap<String, String> query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters();
|
||||
Optional<IamRestRoutes.Match> match = restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, path),
|
||||
query, movedUnderPrefix(ctx), ctx.getHeaders());
|
||||
if (match.isPresent()) {
|
||||
String action = IamAuthorizedActions.actionFor(credentialScope, match.get().operation(), method,
|
||||
match.get().model(), query);
|
||||
if (checkedAsAws(credentialScope, match.get().operation(), action)) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
@@ -782,12 +783,12 @@ public class IamActionRegistry {
|
||||
return action;
|
||||
}
|
||||
String method = ctx.getMethod().toUpperCase();
|
||||
return restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)),
|
||||
ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters(), movedUnderPrefix(ctx),
|
||||
ctx.getHeaders())
|
||||
.map(IamRestRoutes.Match::operation)
|
||||
.filter(operation -> action.equals(IamAuthorizedActions.actionFor(credentialScope, operation, method)))
|
||||
.map(operation -> credentialScope + ":" + operation)
|
||||
MultivaluedMap<String, String> query = ctx.getUriInfo() == null ? null : ctx.getUriInfo().getQueryParameters();
|
||||
return restRoutes.match(credentialScope, method, withoutInternalPrefix(ctx, routedPath(ctx)), query,
|
||||
movedUnderPrefix(ctx), ctx.getHeaders())
|
||||
.filter(match -> action.equals(IamAuthorizedActions.actionFor(credentialScope, match.operation(),
|
||||
method, match.model(), query)))
|
||||
.map(match -> credentialScope + ":" + match.operation())
|
||||
.orElse(action);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.floci.aws.services.iam;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.VendoredResources;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -67,10 +68,20 @@ public final class IamAuthorizedActions {
|
||||
|
||||
/**
|
||||
* The file: the action of each operation it names, keyed by {@code <namespace>:<Operation>}, the actions of
|
||||
* no resource type, as {@code <namespace>:<Action>} in lower case, and the operations authorized by method, as
|
||||
* {@code <namespace>:<Operation>}.
|
||||
* no resource type, as {@code <namespace>:<Action>} in lower case, the operations authorized by method, as
|
||||
* {@code <namespace>:<Operation>}, the action of an operation for a request of one model's route, keyed by
|
||||
* {@code <namespace>:<Operation>@<model>}, the operations whose action a request parameter chooses, and the
|
||||
* reference's spelling of an action named as its operation in another case.
|
||||
*/
|
||||
record Table(Map<String, String> actions, Set<String> withoutResourceTypes, Set<String> byMethod) {
|
||||
record Table(Map<String, String> actions, Set<String> withoutResourceTypes, Set<String> byMethod,
|
||||
Map<String, String> byModel, Map<String, Versioned> versioned, Map<String, String> spellings) {
|
||||
}
|
||||
|
||||
/**
|
||||
* An operation whose action is one or another by whether its request names {@code parameter}: S3's GetObject is
|
||||
* {@code s3:GetObject}, and {@code s3:GetObjectVersion} for a request naming a {@code versionId}.
|
||||
*/
|
||||
record Versioned(String parameter, String without, String with) {
|
||||
}
|
||||
|
||||
private static final class Holder {
|
||||
@@ -90,7 +101,28 @@ public final class IamAuthorizedActions {
|
||||
if (KEPT_FOR_ITEM_CHECKS.contains(own)) {
|
||||
return own;
|
||||
}
|
||||
return Holder.TABLE.actions().getOrDefault(own, own);
|
||||
String renamed = Holder.TABLE.actions().get(own);
|
||||
return renamed != null ? renamed : Holder.TABLE.spellings().getOrDefault(own, own);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #actionFor(String, String, String)}, for a request of the route of {@code model}'s operation carrying
|
||||
* {@code query}: the action the file gives the operation for that model's requests (S3 Control's account-level
|
||||
* GetPublicAccessBlock is {@code s3:GetAccountPublicAccessBlock}, S3's {@code s3:GetBucketPublicAccessBlock}), or
|
||||
* the one its request's parameter chooses (a GetObject naming a {@code versionId} is {@code s3:GetObjectVersion}).
|
||||
*/
|
||||
public static String actionFor(String namespace, String operation, String method, String model,
|
||||
MultivaluedMap<String, String> query) {
|
||||
String own = namespace + ":" + operation;
|
||||
String byModel = model == null ? null : Holder.TABLE.byModel().get(own + "@" + model);
|
||||
if (byModel != null) {
|
||||
return byModel;
|
||||
}
|
||||
Versioned versioned = Holder.TABLE.versioned().get(own);
|
||||
if (versioned != null) {
|
||||
return query != null && query.containsKey(versioned.parameter()) ? versioned.with() : versioned.without();
|
||||
}
|
||||
return actionFor(namespace, operation, method);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +170,58 @@ public final class IamAuthorizedActions {
|
||||
});
|
||||
});
|
||||
return new Table(Map.copyOf(actions), withoutResourceTypes(document.path("actionsWithoutResourceTypes")),
|
||||
byMethod(document.path("authorizedByMethod")));
|
||||
byMethod(document.path("authorizedByMethod")), byModel(document.path("authorizedByModel")),
|
||||
versioned(document.path("versioned")), spellings(document.path("spellings")));
|
||||
}
|
||||
|
||||
/** Each namespace's operations' actions for one model's requests, keyed {@code <namespace>:<Operation>@<model>}. */
|
||||
private static Map<String, String> byModel(JsonNode namespaces) {
|
||||
Map<String, String> found = new LinkedHashMap<>();
|
||||
namespaces.properties().forEach(namespace -> namespace.getValue().properties().forEach(model ->
|
||||
model.getValue().properties().forEach(operation -> found.put(
|
||||
namespace.getKey() + ":" + operation.getKey() + "@" + model.getKey(),
|
||||
actionOf(namespace.getKey(), operation.getKey(), operation.getValue())))));
|
||||
return Map.copyOf(found);
|
||||
}
|
||||
|
||||
/** Each namespace's operations whose action a request parameter chooses. */
|
||||
private static Map<String, Versioned> versioned(JsonNode namespaces) {
|
||||
Map<String, Versioned> found = new LinkedHashMap<>();
|
||||
namespaces.properties().forEach(namespace -> namespace.getValue().properties().forEach(operation -> {
|
||||
JsonNode choice = operation.getValue();
|
||||
if (!choice.path("parameter").isTextual() || choice.path("parameter").asText().isEmpty()) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " gives " + namespace.getKey() + ":"
|
||||
+ operation.getKey() + " a version choice naming no parameter");
|
||||
}
|
||||
found.put(namespace.getKey() + ":" + operation.getKey(), new Versioned(choice.path("parameter").asText(),
|
||||
actionOf(namespace.getKey(), operation.getKey(), choice.path("without")),
|
||||
actionOf(namespace.getKey(), operation.getKey(), choice.path("with"))));
|
||||
}));
|
||||
return Map.copyOf(found);
|
||||
}
|
||||
|
||||
/** Each namespace's actions spelled otherwise than their operations, keyed {@code <namespace>:<Operation>}. */
|
||||
private static Map<String, String> spellings(JsonNode namespaces) {
|
||||
Map<String, String> found = new LinkedHashMap<>();
|
||||
namespaces.properties().forEach(namespace -> namespace.getValue().properties().forEach(operation -> {
|
||||
String action = actionOf(namespace.getKey(), operation.getKey(), operation.getValue());
|
||||
if (!action.substring(namespace.getKey().length() + 1).equalsIgnoreCase(operation.getKey())) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " spells " + namespace.getKey() + ":"
|
||||
+ operation.getKey() + " as " + action + ", another name");
|
||||
}
|
||||
found.put(namespace.getKey() + ":" + operation.getKey(), action);
|
||||
}));
|
||||
return Map.copyOf(found);
|
||||
}
|
||||
|
||||
/** The action a value names, refused unless it is one of the namespace's. */
|
||||
private static String actionOf(String namespace, String operation, JsonNode value) {
|
||||
String action = value.asText("");
|
||||
if (!action.startsWith(namespace + ":") || action.length() == namespace.length() + 1) {
|
||||
throw new IllegalStateException(RESOURCE_NAME + " maps " + namespace + ":" + operation + " to " + value
|
||||
+ ", which is not an action of " + namespace);
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
/** The operations authorized by method, each scope's a list of operation names. */
|
||||
|
||||
Reference in new issue
Block a user