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:
Priamo Germosen authored and GitHub committed 2026-11-21 14:13:43 -04:00
1 parent d9914bfb93
commit 84cea7714a
16 files changed
+2509 -162

No files matched your search

@@ -1,5 +1,7 @@
package io.floci.aws.services.iam;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
@@ -56,6 +58,68 @@ class IamAuthorizedActionsTest {
assertEquals(Set.of("apigateway:GetStage", "apigateway:TagResource"), parsed.byMethod());
}
@Test
void theActionsOfAModelsRequestsAVersionChoiceAndSpellingsAreKeyedByOperation() throws IOException {
IamAuthorizedActions.Table parsed = IamAuthorizedActions.parse(json("""
{"authorizedActions": {"sqs": {"SendMessageBatch": "sqs:SendMessage"}},
"actionsWithoutResourceTypes": {}, "authorizedByMethod": {},
"authorizedByModel": {"s3": {"s3control": {"GetPublicAccessBlock": "s3:GetAccountPublicAccessBlock"}}},
"versioned": {"s3": {"GetObject": {"parameter": "versionId", "without": "s3:GetObject",
"with": "s3:GetObjectVersion"}}},
"spellings": {"memorydb": {"CreateACL": "memorydb:CreateAcl"}}}
"""));
assertEquals(Map.of("s3:GetPublicAccessBlock@s3control", "s3:GetAccountPublicAccessBlock"), parsed.byModel());
assertEquals(Map.of("s3:GetObject", new IamAuthorizedActions.Versioned("versionId", "s3:GetObject",
"s3:GetObjectVersion")), parsed.versioned());
assertEquals(Map.of("memorydb:CreateACL", "memorydb:CreateAcl"), parsed.spellings());
}
@Test
void aMalformedModelVersionOrSpellingEntryIsRefused() {
String base = "\"authorizedActions\": {\"sqs\": {\"A\": \"sqs:B\"}}, \"actionsWithoutResourceTypes\": {}, "
+ "\"authorizedByMethod\": {}, ";
assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("{" + base
+ "\"authorizedByModel\": {\"s3\": {\"s3control\": {\"GetPublicAccessBlock\": \"sqs:SendMessage\"}}}}")));
assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("{" + base
+ "\"versioned\": {\"s3\": {\"GetObject\": {\"without\": \"s3:GetObject\", \"with\": \"s3:X\"}}}}")));
assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("{" + base
+ "\"spellings\": {\"memorydb\": {\"CreateACL\": \"memorydb:CreateCluster\"}}}")));
}
/**
* A request of a route's model and query is checked as the action the file gives that model's requests, or the
* one its query chooses: S3 Control's account-level GetPublicAccessBlock and S3's bucket-level one, a GetObject
* naming a version, and S3's GetBucketLifecycleConfiguration, the action the reference gives its namesake client.
*/
@Test
void aRouteOfAModelOrARequestNamingAVersionIsCheckedAsTheActionTheFileGivesIt() {
MultivaluedMap<String, String> none = new MultivaluedHashMap<>();
MultivaluedMap<String, String> version = new MultivaluedHashMap<>();
version.add("versionId", "null");
assertEquals("s3:GetAccountPublicAccessBlock",
IamAuthorizedActions.actionFor("s3", "GetPublicAccessBlock", "GET", "s3control", none));
assertEquals("s3:GetBucketPublicAccessBlock",
IamAuthorizedActions.actionFor("s3", "GetPublicAccessBlock", "GET", "s3", none));
assertEquals("s3:GetObject", IamAuthorizedActions.actionFor("s3", "GetObject", "GET", "s3", none));
assertEquals("s3:GetObjectVersion", IamAuthorizedActions.actionFor("s3", "GetObject", "GET", "s3", version));
assertEquals("s3:GetObjectVersionAnnotation",
IamAuthorizedActions.actionFor("s3", "GetObjectAnnotation", "GET", "s3", version));
assertEquals("s3:GetLifecycleConfiguration",
IamAuthorizedActions.actionFor("s3", "GetBucketLifecycleConfiguration", "GET", "s3", none));
assertEquals("s3:ListBucket", IamAuthorizedActions.actionFor("s3", "ListObjectsV2", "GET", "s3", none));
assertEquals("s3:PutObject", IamAuthorizedActions.actionFor("s3", "CopyObject", "PUT", "s3", none));
// A model the file gives nothing for keeps the answer the operation gets otherwise.
assertEquals("apigateway:PUT", IamAuthorizedActions.actionFor("apigateway", "TagResource", "PUT", null, none));
}
/** An action the reference spells otherwise than its operation, in case alone, is named with its spelling. */
@Test
void anActionIsNamedAsTheReferenceSpellsIt() {
assertEquals("memorydb:CreateAcl", IamAuthorizedActions.actionFor("memorydb", "CreateACL"));
assertEquals("s3:PutBucketCORS", IamAuthorizedActions.actionFor("s3", "PutBucketCors", "PUT"));
assertEquals("sqs:SendMessage", IamAuthorizedActions.actionFor("sqs", "SendMessageBatch"));
}
/**
* A request on the route of an operation the file authorizes by method is checked as the action its method
* names: API Gateway's {@code TagResource} is a PUT in v1 and a POST in v2. An operation the file renames keeps
@@ -113,6 +113,38 @@ class IamRestRouteCoverageTest {
return tests.stream();
}
/**
* Each action the vendored file gives an operation for one model's requests is what a request on that model's route
* resolves to: S3's GetPublicAccessBlock is s3:GetBucketPublicAccessBlock, S3 Control's account-level one
* s3:GetAccountPublicAccessBlock, and S3's GetBucketLifecycleConfiguration s3:GetLifecycleConfiguration.
*/
@TestFactory
Stream<DynamicTest> eachVendoredActionOfAModelsRequestsIsWhatItsRouteResolvesTo() throws IOException {
JsonNode models = load("/aws/rest-routes.json").path("models");
JsonNode byModel = load("/aws/iam-authorized-actions.json").path("authorizedByModel");
List<DynamicTest> tests = new ArrayList<>();
for (String namespace : ROUTED_NAMESPACES) {
Iterator<Map.Entry<String, JsonNode>> clients = byModel.path(namespace).fields();
while (clients.hasNext()) {
Map.Entry<String, JsonNode> client = clients.next();
Iterator<Map.Entry<String, JsonNode>> entries = client.getValue().fields();
while (entries.hasNext()) {
Map.Entry<String, JsonNode> entry = entries.next();
List<Route> routes = routesOf(models, namespace, entry.getKey()).stream()
.filter(route -> route.model().equals(client.getKey())).toList();
String expected = entry.getValue().asText();
tests.add(DynamicTest.dynamicTest(client.getKey() + " " + entry.getKey(), () -> {
assertFalse(routes.isEmpty(), "no " + client.getKey() + " route for " + entry.getKey());
for (Route route : routes) {
assertEquals(expected, resolve(route), route.toString());
}
}));
}
}
}
return tests.stream();
}
/**
* Every operation of a scope the vendored routes do not leave to the rules alone resolves to its action when
* Floci can check it as AWS does: its resources built by hand for its namespace or read by the vendored request
@@ -245,8 +277,10 @@ class IamRestRouteCoverageTest {
Map.Entry<String, JsonNode> model = all.next();
JsonNode route = model.getValue().path("operations").path(operation);
if (scope.equals(model.getValue().path("credentialScope").asText()) && !route.isMissingNode()) {
// A hand-routed scope's route lists all its request must carry; another's what tells it apart.
JsonNode carries = route.has("required") ? route.path("required") : route.path("requires");
routes.add(new Route(scope, model.getKey(), operation, route.path("method").asText(),
route.path("requestUri").asText()));
route.path("requestUri").asText(), carries));
}
}
return routes;
@@ -263,7 +297,17 @@ class IamRestRouteCoverageTest {
}
/** An operation's route: its method, and its request URI with each label filled in. */
private record Route(String scope, String model, String operation, String method, String requestUri) {
/**
* A route of a model's operation, and what botocore requires its request to carry ({@code required} for a
* hand-routed scope's, {@code requires} where several operations share it), which the request built on it carries
* as an SDK sends it.
*/
private record Route(String scope, String model, String operation, String method, String requestUri,
JsonNode requires) {
Route(String scope, String model, String operation, String method, String requestUri) {
this(scope, model, operation, method, requestUri, MAPPER.createObjectNode());
}
ContainerRequestContext request() {
int question = requestUri.indexOf('?');
@@ -276,7 +320,14 @@ class IamRestRouteCoverageTest {
equals < 0 ? "" : parameter.substring(equals + 1));
}
}
requires.path("query").forEach(parameter -> query.add(parameter.asText(), "required-value"));
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
requires.path("headers").forEach(header -> {
headers.add(header.asText(), "required-value");
when(ctx.getHeaderString(header.asText())).thenReturn("required-value");
});
when(ctx.getHeaders()).thenReturn(headers);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getQueryParameters()).thenReturn(query);
when(uriInfo.getPath()).thenReturn(path);