Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): authorize an operation as the action AWS checks for it (#5604)
Priamo Germoseneba058266b91
diff --git a/.github/workflows/iam-data-drift.yml b/.github/workflows/iam-data-drift.ymlindex 5799cb6e41..ec4b6b2b23 100644--- a/.github/workflows/iam-data-drift.yml+++ b/.github/workflows/iam-data-drift.yml@@ -1,17 +1,18 @@name: IAM Data Drift-# Checks the two vendored files that come from AWS's live service reference rather than from a-# pinned dependency: the IAM service namespace list, and the map of which resource each IAM action-# is authorized against. That needs the live service reference index, so unlike every other check here it can+# Checks the three vendored files that come from AWS's live service reference rather than from a+# pinned dependency: the IAM service namespace list, the map of which resource each IAM action is+# authorized against, and the map of operations AWS authorizes as an action of another name. That+# needs the live service reference index, so unlike every other check here it can# fail without anything in the repository having changed: an AWS outage, or a namespace AWS has# retired. It therefore runs on its own, never as part of a job that ordinary changes trigger.## Two triggers, for two different failures:#-# - On a pull request that touches either vendored file or its generator, which is where a+# - On a pull request that touches a vendored file or its generator, which is where a# hand-typed or mistyped entry would enter the repository. A pull request that does not touch# them cannot introduce one, so it does not run and cannot be failed by an outage.-# - Weekly, because both files also go stale without anyone touching them: AWS ships services+# - Weekly, because the files also go stale without anyone touching them: AWS ships services# and actions, and occasionally retires them. Tying the only upstream check to pull request# activity would mean a quiet month is a month with no drift detection at all.#@@ -32,6 +33,9 @@ on:- 'tools/aws/test_regen_service_namespaces.py'- 'tools/aws/regen_iam_action_resources.py'- 'tools/aws/test_regen_iam_action_resources.py'+ - 'src/main/resources/aws/iam-authorized-actions.json'+ - 'tools/aws/regen_iam_authorized_actions.py'+ - 'tools/aws/test_regen_iam_authorized_actions.py'- '.github/workflows/iam-data-drift.yml'schedule:# Mondays at 06:00 UTC, before the working week rather than during it.diff --git a/.github/workflows/partition-literals.yml b/.github/workflows/partition-literals.ymlindex 9185b0c496..e91992e8da 100644--- a/.github/workflows/partition-literals.yml+++ b/.github/workflows/partition-literals.yml@@ -108,6 +108,11 @@ jobs:- name: Check the vendored IAM action resource map is well-formedrun: make iam-action-resources-check+ # And for the map of operations AWS authorizes under another action's name: a malformed+ # entry would authorize a request as an action AWS does not check for it.+ - name: Check the vendored map of operations to their IAM actions is well-formed+ run: make iam-authorized-actions-check+# Also offline: the service-linked role table is checked against the vendored managed# policy documents in the repository, never against AWS. The half that comes from a# recorded create cannot be re-derived without the snapshot, which is why every row indiff --git a/Makefile b/Makefileindex 8dc81eeca4..8d98fa8768 100644--- a/Makefile+++ b/Makefile@@ -48,7 +48,8 @@ PREFIX ?= $(HOME)/.localaws-data-sync aws-data-check aws-data-test \iam-namespaces-sync iam-namespaces-check iam-namespaces-verify iam-namespaces-test \iam-action-resources-sync iam-action-resources-check iam-action-resources-verify \- iam-action-resources-test iam-data-verify+ iam-action-resources-test iam-authorized-actions-sync iam-authorized-actions-check \+ iam-authorized-actions-verify iam-authorized-actions-test iam-data-verifyhelp: ## List the targets below@grep -hE '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " %-14s %s\n", $$1, $$2}'@@ -290,10 +291,11 @@ iam-data-verify: ## CI gate (online): every vendored IAM file must still match A@# Both halves run even when the first fails, so one file's drift cannot hide the other's@# state. Sequential steps in CI stop at the first failure, which once left the action@# resource gate unexercised while the namespace list was stale.- @namespaces=0; actions=0; \+ @namespaces=0; actions=0; authorized=0; \$(MAKE) --no-print-directory iam-namespaces-verify || namespaces=$$?; \$(MAKE) --no-print-directory iam-action-resources-verify || actions=$$?; \- if [ $$namespaces -ne 0 ] || [ $$actions -ne 0 ]; then \+ $(MAKE) --no-print-directory iam-authorized-actions-verify || authorized=$$?; \+ if [ $$namespaces -ne 0 ] || [ $$actions -ne 0 ] || [ $$authorized -ne 0 ]; then \echo ""; \echo "error: vendored IAM data does not match upstream:"; \if [ $$namespaces -ne 0 ]; then \@@ -302,8 +304,38 @@ iam-data-verify: ## CI gate (online): every vendored IAM file must still match Aif [ $$actions -ne 0 ]; then \echo " the action resource map, see its message above"; \fi; \+ if [ $$authorized -ne 0 ]; then \+ echo " the map of operations to their IAM actions, see its message above"; \+ fi; \exit 1; \fiiam-action-resources-test: ## Run the IAM action resource generator's unit tests$(PYTHON) -m pytest tools/aws/test_regen_iam_action_resources.py -q++iam-authorized-actions-sync: ## Regenerate src/main/resources/aws/iam-authorized-actions.json from AWS (commit the result)+ $(PYTHON) tools/aws/regen_iam_authorized_actions.py++iam-authorized-actions-check: ## CI gate (offline): the vendored map of operations to their IAM actions must be well-formed+ @$(PYTHON) tools/aws/regen_iam_authorized_actions.py --check || { \+ echo ""; \+ echo "error: src/main/resources/aws/iam-authorized-actions.json is malformed."; \+ echo " Run 'make iam-authorized-actions-sync' and commit the result."; \+ exit 1; \+ }++iam-authorized-actions-verify: ## CI gate (online): every vendored operation must still be authorized as AWS says+ @$(PYTHON) tools/aws/regen_iam_authorized_actions.py --verify; status=$$?; \+ if [ $$status -eq 2 ]; then \+ echo ""; \+ echo "error: could not reach AWS's service reference, so nothing was verified."; \+ exit 1; \+ elif [ $$status -ne 0 ]; then \+ echo ""; \+ echo "error: the vendored map authorizes an operation as an action AWS does not check for it."; \+ echo " Run 'make iam-authorized-actions-sync' and commit the result."; \+ exit 1; \+ fi++iam-authorized-actions-test: ## Run the IAM authorized-action generator's unit tests+ $(PYTHON) -m pytest tools/aws/test_regen_iam_authorized_actions.py -qdiff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamAuthorizedActionTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamAuthorizedActionTest.javanew file mode 100644index 0000000000..3111585701--- /dev/null+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/IamAuthorizedActionTest.java@@ -0,0 +1,143 @@+package com.floci.test;++import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.Assumptions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.DisplayName;+import org.junit.jupiter.api.Test;+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;+import software.amazon.awssdk.services.iam.IamClient;+import software.amazon.awssdk.services.iam.model.AccessKey;+import software.amazon.awssdk.services.sns.SnsClient;+import software.amazon.awssdk.services.sns.model.PublishBatchResultEntry;+import software.amazon.awssdk.services.sns.model.SnsException;+import software.amazon.awssdk.services.sqs.SqsClient;+import software.amazon.awssdk.services.sqs.model.SendMessageBatchResultEntry;+import software.amazon.awssdk.services.sqs.model.SqsException;+import software.amazon.awssdk.utils.SdkAutoCloseable;++import static org.assertj.core.api.Assertions.assertThat;+import static org.assertj.core.api.Assertions.assertThatThrownBy;++@DisplayName("Batch operations authorized as their single-item IAM action")+class IamAuthorizedActionTest {++ private IamClient iam;+ private SqsClient sqsAdmin;+ private SnsClient snsAdmin;+ private SqsClient sqs;+ private SnsClient sns;+ private String user;+ private AccessKey accessKey;+ private boolean policyCreated;+ private String queueUrl;+ private String topicArn;++ @BeforeEach+ void setUp() {+ iam = TestFixtures.iamClient();+ sqsAdmin = TestFixtures.sqsClient();+ snsAdmin = TestFixtures.snsClient();+ String name = TestFixtures.uniqueName("authorized-action");+ iam.createUser(r -> r.userName(name));+ user = name;+ accessKey = iam.createAccessKey(r -> r.userName(user)).accessKey();+ StaticCredentialsProvider credentials = StaticCredentialsProvider.create(+ AwsBasicCredentials.create(accessKey.accessKeyId(), accessKey.secretAccessKey()));+ sqs = SqsClient.builder().endpointOverride(TestFixtures.endpoint()).region(TestFixtures.region())+ .credentialsProvider(credentials).build();+ sns = SnsClient.builder().endpointOverride(TestFixtures.endpoint()).region(TestFixtures.region())+ .credentialsProvider(credentials).build();+ boolean enforced = false;+ try {+ sqs.listQueues();+ } catch (SqsException error) {+ assertThat(error.awsErrorDetails().errorCode()).contains("AccessDenied");+ enforced = true;+ }+ Assumptions.assumeTrue(enforced, "Requires floci.services.iam.enforcement-enabled=true");+ queueUrl = sqsAdmin.createQueue(r -> r.queueName(name)).queueUrl();+ topicArn = snsAdmin.createTopic(r -> r.name(name)).topicArn();+ }++ @AfterEach+ void cleanUp() {+ try {+ if (queueUrl != null) {+ sqsAdmin.deleteQueue(r -> r.queueUrl(queueUrl));+ }+ if (topicArn != null) {+ snsAdmin.deleteTopic(r -> r.topicArn(topicArn));+ }+ } finally {+ try {+ if (policyCreated) {+ iam.deleteUserPolicy(r -> r.userName(user).policyName("authorized"));+ }+ if (accessKey != null) {+ iam.deleteAccessKey(r -> r.userName(user).accessKeyId(accessKey.accessKeyId()));+ }+ if (user != null) {+ iam.deleteUser(r -> r.userName(user));+ }+ } finally {+ for (SdkAutoCloseable client : new SdkAutoCloseable[] {sqs, sns, sqsAdmin, snsAdmin, iam}) {+ if (client != null) {+ client.close();+ }+ }+ }+ }+ }++ @Test+ @DisplayName("SendMessageBatch is granted by sqs:SendMessage")+ void sendMessageBatchIsGrantedBySendMessage() {+ allow("sqs:SendMessage");++ assertThat(sqs.sendMessageBatch(r -> r.queueUrl(queueUrl)+ .entries(e -> e.id("one").messageBody("hello"))).successful())+ .extracting(SendMessageBatchResultEntry::id)+ .containsExactly("one");+ }++ @Test+ @DisplayName("a policy naming SendMessageBatch, which is no IAM action, grants nothing")+ void aPolicyNamingSendMessageBatchGrantsNothing() {+ allow("sqs:SendMessageBatch");++ assertThatThrownBy(() -> sqs.sendMessageBatch(r -> r.queueUrl(queueUrl)+ .entries(e -> e.id("one").messageBody("hello"))))+ .isInstanceOfSatisfying(SqsException.class,+ error -> assertThat(error.awsErrorDetails().errorCode()).contains("AccessDenied"));+ }++ @Test+ @DisplayName("PublishBatch is granted by sns:Publish")+ void publishBatchIsGrantedByPublish() {+ allow("sns:Publish");++ assertThat(sns.publishBatch(r -> r.topicArn(topicArn)+ .publishBatchRequestEntries(e -> e.id("one").message("hello"))).successful())+ .extracting(PublishBatchResultEntry::id)+ .containsExactly("one");+ }++ @Test+ @DisplayName("a policy naming PublishBatch, which is no IAM action, grants nothing")+ void aPolicyNamingPublishBatchGrantsNothing() {+ allow("sns:PublishBatch");++ assertThatThrownBy(() -> sns.publishBatch(r -> r.topicArn(topicArn)+ .publishBatchRequestEntries(e -> e.id("one").message("hello"))))+ .isInstanceOfSatisfying(SnsException.class, error -> assertThat(error.statusCode()).isEqualTo(403));+ }++ private void allow(String action) {+ String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\""+ + action + "\",\"Resource\":\"*\"}]}";+ iam.putUserPolicy(r -> r.userName(user).policyName("authorized").policyDocument(document));+ policyCreated = true;+ }+}diff --git a/docs/services/iam.md b/docs/services/iam.mdindex fc7f3a76c0..69d16f1ae2 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -1114,6 +1114,17 @@ a Query `Action` field on it does not change the action it is checked as, and itstill percent-encoded, as the router matches it, so an encoded `/` inside a parameter cannot makethe route's rule miss.+A request that names its operation, in a Query `Action` or an `X-Amz-Target` header, is authorized+as the action AWS checks for that operation, which is usually the operation's own name. Where AWS+authorizes an operation as a single action of another name, Floci does the same: `SendMessageBatch`+is checked as `sqs:SendMessage` and `PublishBatch` as `sns:Publish`, so a policy granting the+single-message action covers the batch call, and one naming the batch operation, which is not an+IAM action, grants nothing. The mapping is taken from AWS's Service Reference and vendored in+`src/main/resources/aws/iam-authorized-actions.json`. DynamoDB `TransactGetItems`, which AWS checks+as `dynamodb:GetItem`, is checked item by item, as described under+[Condition keys floci populates](#condition-keys-floci-populates): each `Get` as+`dynamodb:GetItem` on its own table, with its own `dynamodb:LeadingKeys`.+### Resource-based policiesWhen `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` is active, Floci also queries registered `ResourcePolicyProvider` SPI implementations (such as S3 bucket policies) during request authorization:diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.javaindex cb85dc0436..a2a1097326 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.services.cloudtrail.CloudTrailService;import io.github.hectorvent.floci.services.iam.IamActionRegistry;+import io.github.hectorvent.floci.services.iam.IamAuthorizedActions;import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourceAccountRelationship;@@ -561,7 +562,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {String servingScope = catalog.byExternalKey(service).map(descriptor -> iamServiceScope(descriptor, claimedScope)).orElseGet(() -> catalog.canonicalCredentialScope(service));- return new ResolvedAuthorization(servingScope, servingScope + ":" + queryAction);+ return new ResolvedAuthorization(servingScope,+ IamAuthorizedActions.actionFor(servingScope, queryAction));}ResolvedAuthorization routeAuthorization = restRouteAuthorization(ctx);@@ -1403,7 +1405,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {/** Whether the operation this request names is one AWS serves without credentials. */private boolean servesWithoutCredentials(ProtocolClaim claim, ContainerRequestContext ctx) {if (claim.protocol() == WireProtocol.AWS_QUERY) {- return NO_AUTH_QUERY_OPERATIONS.contains(queryOperation(ctx));+ String operation = queryOperation(ctx);+ return operation != null && NO_AUTH_QUERY_OPERATIONS.contains(operation);}if (claim.service() == null || claim.operation() == null) {// Nothing names the operation, so there is nothing to match against the list. Refusing@@ -1418,12 +1421,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {/*** The {@code Action} a Query request names, via the registry so the form body is read and put- * back the one way this codebase already does it. The scope passed in is a placeholder: for a- * Query request the registry returns {@code <scope>:<Action>} and only the suffix is wanted.+ * back the one way this codebase already does it.*/private String queryOperation(ContainerRequestContext ctx) {- String resolved = actionRegistry.resolve("", ctx);- return resolved == null ? null : resolved.substring(resolved.indexOf(':') + 1);+ return actionRegistry.queryAction(ctx);}/**diff --git a/src/main/java/io/github/hectorvent/floci/core/common/VendoredResources.java b/src/main/java/io/github/hectorvent/floci/core/common/VendoredResources.javanew file mode 100644index 0000000000..1801a8f514--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/VendoredResources.java@@ -0,0 +1,34 @@+package io.github.hectorvent.floci.core.common;++import java.io.IOException;+import java.io.InputStream;++/**+ * Reads a file vendored on the classpath, such as the IAM data generated from AWS's Service+ * Reference. A missing or unreadable resource is a build defect, not a runtime condition, so it+ * fails with {@link IllegalStateException} rather than leaving the caller to degrade to an empty+ * catalog.+ */+public final class VendoredResources {++ /** Reads the resource's content into what the caller keeps. */+ @FunctionalInterface+ public interface Parser<T> {+ T parse(InputStream in) throws IOException;+ }++ private VendoredResources() {+ }++ /** The resource {@code name} as {@code parser} reads it; {@code description} names it in a failure. */+ public static <T> T read(String name, String description, Parser<T> parser) {+ try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name)) {+ if (in == null) {+ throw new IllegalStateException(description + " resource not found: " + name);+ }+ return parser.parse(in);+ } catch (IOException e) {+ throw new IllegalStateException("Failed to read the " + description + " " + name, e);+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.javaindex 1372f3821a..e184c9a26d 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java@@ -251,15 +251,15 @@ public class IamActionRegistry {if (isQueryRequest(ctx)) {String queryAction = queryAction(ctx);if (queryAction != null && !queryAction.isBlank()) {- return credentialScope + ":" + queryAction;+ return IamAuthorizedActions.actionFor(credentialScope, queryAction);}}- // JSON 1.1: X-Amz-Target → service:OperationName+ // JSON 1.1: X-Amz-Target → the action AWS checks for the operation it namesString target = ctx.getHeaderString("X-Amz-Target");if (target != null && target.contains(".")) {String operationName = target.substring(target.lastIndexOf('.') + 1);- return credentialScope + ":" + operationName;+ return IamAuthorizedActions.actionFor(credentialScope, operationName);}return resolveRoute(credentialScope, ctx);diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActions.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActions.javanew file mode 100644index 0000000000..f9173f526e--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActions.java@@ -0,0 +1,101 @@+package io.github.hectorvent.floci.services.iam;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.core.common.VendoredResources;++import java.io.IOException;+import java.io.InputStream;+import java.util.LinkedHashMap;+import java.util.Map;+import java.util.Set;++/**+ * The IAM action AWS checks for an operation whose action is not the operation's own name, read once+ * from the vendored {@code aws/iam-authorized-actions.json}.+ *+ * <p>A request that names its operation, in a Query {@code Action} or an {@code X-Amz-Target}+ * header, is authorized as {@code <namespace>:<Operation>}. For most operations that is the action+ * AWS checks, but not for all: SQS {@code SendMessageBatch} is+ * authorized as {@code sqs:SendMessage} and SNS {@code PublishBatch} as {@code sns:Publish}, and+ * neither batch name is an IAM action at all. Authorized as its own name, such a call is denied+ * under a least-privilege policy AWS honours, and granted by a policy naming something AWS does not+ * check.+ *+ * <p>The file is generated by {@code tools/aws/regen_iam_authorized_actions.py} from the+ * {@code Operations} AWS's Service Reference publishes per service, which name the actions each SDK+ * operation is authorized by. It carries only operations authorized by exactly one action of their+ * own namespace under another name; the generator documents the shapes it leaves out and why.+ * {@code make iam-authorized-actions-check} gates its shape offline and+ * {@code make iam-authorized-actions-verify} checks every entry against AWS. A namespace Floci+ * authorizes under a name other than the IAM namespace matches nothing here, and its operations keep+ * their own names.+ *+ * <p>A missing or malformed resource is a build defect, not a runtime condition, so loading fails+ * with {@link IllegalStateException} rather than degrading to an empty map, which would silently put+ * every operation back on its own name.+ */+public final class IamAuthorizedActions {++ static final String RESOURCE_NAME = "aws/iam-authorized-actions.json";++ /**+ * Operations kept on their own name because another check recognizes them by it. AWS authorizes+ * DynamoDB {@code TransactGetItems} as {@code dynamodb:GetItem}, and Floci does too, item by+ * item: the transaction check in {@code IamConditionContextResolver} picks the call out by its own+ * name and checks every {@code Get} as {@code dynamodb:GetItem} on its own table, with its own+ * {@code dynamodb:LeadingKeys}. Renamed here, the call would skip that check and be authorized as+ * a single {@code GetItem} whose body has no top-level {@code Key}, so no+ * {@code dynamodb:LeadingKeys}, which a {@code ForAllValues} condition accepts: a fine-grained+ * policy would grant every key.+ */+ private static final Set<String> KEPT_FOR_ITEM_CHECKS = Set.of("dynamodb:TransactGetItems");++ private static final ObjectMapper MAPPER = new ObjectMapper();++ private static final class Holder {+ static final Map<String, String> ACTIONS = VendoredResources.read(RESOURCE_NAME,+ "IAM authorized action map", IamAuthorizedActions::parse);+ }++ private IamAuthorizedActions() {+ }++ /**+ * The action AWS checks for {@code operation} under {@code namespace}: the one the vendored file+ * names for it, or else the operation's own name.+ */+ public static String actionFor(String namespace, String operation) {+ String own = namespace + ":" + operation;+ if (KEPT_FOR_ITEM_CHECKS.contains(own)) {+ return own;+ }+ return Holder.ACTIONS.getOrDefault(own, own);+ }++ /** The file's entries, keyed by {@code <namespace>:<Operation>}. */+ static Map<String, String> parse(InputStream in) throws IOException {+ JsonNode namespaces = MAPPER.readTree(in).path("authorizedActions");+ if (!namespaces.isObject() || namespaces.isEmpty()) {+ throw new IllegalStateException(RESOURCE_NAME + " carries no authorizedActions");+ }+ Map<String, String> actions = new LinkedHashMap<>();+ namespaces.properties().forEach(namespace -> {+ if (!namespace.getValue().isObject()) {+ throw new IllegalStateException(RESOURCE_NAME + " has authorizedActions."+ + namespace.getKey() + " as something other than a map");+ }+ namespace.getValue().properties().forEach(operation -> {+ String action = operation.getValue().asText("");+ if (!action.startsWith(namespace.getKey() + ":")+ || action.length() == namespace.getKey().length() + 1) {+ throw new IllegalStateException(RESOURCE_NAME + " maps " + namespace.getKey() + ":"+ + operation.getKey() + " to " + operation.getValue()+ + ", which is not an action of " + namespace.getKey());+ }+ actions.put(namespace.getKey() + ":" + operation.getKey(), action);+ });+ });+ return Map.copyOf(actions);+ }+}diff --git a/src/main/resources/aws/iam-authorized-actions.json b/src/main/resources/aws/iam-authorized-actions.jsonnew file mode 100644index 0000000000..b677ec7531--- /dev/null+++ b/src/main/resources/aws/iam-authorized-actions.json@@ -0,0 +1,311 @@+{+ "_source": {+ "generator": "tools/aws/regen_iam_authorized_actions.py",+ "reference": "https://servicereference.us-east-1.amazonaws.com/",+ "retrieved": "2026-10-14",+ "note": "make iam-authorized-actions-verify checks every entry against AWS."+ },+ "authorizedActions": {+ "access-analyzer": {+ "GetFindingV2": "access-analyzer:GetFinding",+ "ListFindingsV2": "access-analyzer:ListFindings"+ },+ "agent-registry": {+ "BatchGetDiscoverableRegistryRecord": "agent-registry:GetDiscoverableRegistryRecord"+ },+ "apigateway": {+ "CreateApiMapping": "apigateway:POST",+ "CreateBasePathMapping": "apigateway:POST",+ "CreateDocumentationPart": "apigateway:POST",+ "CreateDocumentationVersion": "apigateway:POST",+ "CreateIntegrationResponse": "apigateway:POST",+ "CreateRequestValidator": "apigateway:POST",+ "CreateResource": "apigateway:POST",+ "CreateRoute": "apigateway:POST",+ "CreateRouteResponse": "apigateway:POST",+ "CreateUsagePlanKey": "apigateway:POST",+ "DeleteAccessLogSettings": "apigateway:DELETE",+ "DeleteApi": "apigateway:DELETE",+ "DeleteApiKey": "apigateway:DELETE",+ "DeleteApiMapping": "apigateway:DELETE",+ "DeleteBasePathMapping": "apigateway:DELETE",+ "DeleteClientCertificate": "apigateway:DELETE",+ "DeleteCorsConfiguration": "apigateway:DELETE",+ "DeleteDocumentationPart": "apigateway:DELETE",+ "DeleteDocumentationVersion": "apigateway:DELETE",+ "DeleteDomainNameAccessAssociation": "apigateway:DELETE",+ "DeleteGatewayResponse": "apigateway:DELETE",+ "DeleteMethod": "apigateway:DELETE",+ "DeleteMethodResponse": "apigateway:DELETE",+ "DeleteRequestValidator": "apigateway:DELETE",+ "DeleteResource": "apigateway:DELETE",+ "DeleteRestApi": "apigateway:DELETE",+ "DeleteRoute": "apigateway:DELETE",+ "DeleteRouteRequestParameter": "apigateway:DELETE",+ "DeleteRouteResponse": "apigateway:DELETE",+ "DeleteRouteSettings": "apigateway:DELETE",+ "DeleteUsagePlan": "apigateway:DELETE",+ "DeleteUsagePlanKey": "apigateway:DELETE",+ "ExportApi": "apigateway:GET",+ "FlushStageAuthorizersCache": "apigateway:DELETE",+ "FlushStageCache": "apigateway:DELETE",+ "GetAccount": "apigateway:GET",+ "GetApi": "apigateway:GET",+ "GetApiKey": "apigateway:GET",+ "GetApiKeys": "apigateway:GET",+ "GetApiMapping": "apigateway:GET",+ "GetApiMappings": "apigateway:GET",+ "GetApis": "apigateway:GET",+ "GetBasePathMapping": "apigateway:GET",+ "GetBasePathMappings": "apigateway:GET",+ "GetClientCertificate": "apigateway:GET",+ "GetClientCertificates": "apigateway:GET",+ "GetDocumentationPart": "apigateway:GET",+ "GetDocumentationParts": "apigateway:GET",+ "GetDocumentationVersion": "apigateway:GET",+ "GetDocumentationVersions": "apigateway:GET",+ "GetDomainNameAccessAssociations": "apigateway:GET",+ "GetExport": "apigateway:GET",+ "GetGatewayResponse": "apigateway:GET",+ "GetGatewayResponses": "apigateway:GET",+ "GetIntegrationResponses": "apigateway:GET",+ "GetIntegrations": "apigateway:GET",+ "GetMethod": "apigateway:GET",+ "GetMethodResponse": "apigateway:GET",+ "GetRequestValidator": "apigateway:GET",+ "GetRequestValidators": "apigateway:GET",+ "GetResource": "apigateway:GET",+ "GetResources": "apigateway:GET",+ "GetRestApi": "apigateway:GET",+ "GetRestApis": "apigateway:GET",+ "GetRoute": "apigateway:GET",+ "GetRouteResponse": "apigateway:GET",+ "GetRouteResponses": "apigateway:GET",+ "GetRoutes": "apigateway:GET",+ "GetSdk": "apigateway:GET",+ "GetSdkType": "apigateway:GET",+ "GetSdkTypes": "apigateway:GET",+ "GetUsage": "apigateway:GET",+ "GetUsagePlan": "apigateway:GET",+ "GetUsagePlanKey": "apigateway:GET",+ "GetUsagePlanKeys": "apigateway:GET",+ "GetUsagePlans": "apigateway:GET",+ "ImportApiKeys": "apigateway:POST",+ "ImportDocumentationParts": "apigateway:PUT",+ "PutGatewayResponse": "apigateway:PUT",+ "PutIntegrationResponse": "apigateway:PUT",+ "PutMethod": "apigateway:PUT",+ "PutMethodResponse": "apigateway:PUT",+ "PutRoutingRule": "apigateway:UpdateRoutingRule",+ "RejectDomainNameAccessAssociation": "apigateway:RejectAccessAssociation",+ "ResetAuthorizersCache": "apigateway:DELETE",+ "TestInvokeAuthorizer": "apigateway:POST",+ "TestInvokeMethod": "apigateway:POST",+ "UpdateApiKey": "apigateway:PATCH",+ "UpdateApiMapping": "apigateway:PATCH",+ "UpdateBasePathMapping": "apigateway:PATCH",+ "UpdateClientCertificate": "apigateway:PATCH",+ "UpdateDocumentationPart": "apigateway:PATCH",+ "UpdateDocumentationVersion": "apigateway:PATCH",+ "UpdateGatewayResponse": "apigateway:PATCH",+ "UpdateMethod": "apigateway:PATCH",+ "UpdateMethodResponse": "apigateway:PATCH",+ "UpdateRequestValidator": "apigateway:PATCH",+ "UpdateResource": "apigateway:PATCH",+ "UpdateRoute": "apigateway:PATCH",+ "UpdateRouteResponse": "apigateway:PATCH",+ "UpdateUsage": "apigateway:PATCH",+ "UpdateUsagePlan": "apigateway:PATCH"+ },+ "aws-marketplace": {+ "BatchDescribeEntities": "aws-marketplace:DescribeEntity"+ },+ "batch": {+ "CancelJobs": "batch:CancelJob",+ "TerminateJobs": "batch:TerminateJob",+ "TerminateServiceJobs": "batch:TerminateServiceJob"+ },+ "bcm-pricing-calculator": {+ "BatchCreateBillScenarioCommitmentModification": "bcm-pricing-calculator:CreateBillScenarioCommitmentModification",+ "BatchCreateBillScenarioUsageModification": "bcm-pricing-calculator:CreateBillScenarioUsageModification",+ "BatchCreateWorkloadEstimateUsage": "bcm-pricing-calculator:CreateWorkloadEstimateUsage",+ "BatchDeleteBillScenarioCommitmentModification": "bcm-pricing-calculator:DeleteBillScenarioCommitmentModification",+ "BatchDeleteBillScenarioUsageModification": "bcm-pricing-calculator:DeleteBillScenarioUsageModification",+ "BatchDeleteWorkloadEstimateUsage": "bcm-pricing-calculator:DeleteWorkloadEstimateUsage",+ "BatchUpdateBillScenarioCommitmentModification": "bcm-pricing-calculator:UpdateBillScenarioCommitmentModification",+ "BatchUpdateBillScenarioUsageModification": "bcm-pricing-calculator:UpdateBillScenarioUsageModification",+ "BatchUpdateWorkloadEstimateUsage": "bcm-pricing-calculator:UpdateWorkloadEstimateUsage"+ },+ "bedrock": {+ "InvokeModelWithBidirectionalStream": "bedrock:InvokeModel",+ "RetrieveAndGenerateStream": "bedrock:RetrieveAndGenerate"+ },+ "cassandra": {+ "GetKeyspace": "cassandra:Select",+ "GetTable": "cassandra:Select",+ "GetType": "cassandra:Select",+ "ListKeyspaces": "cassandra:Select",+ "ListTables": "cassandra:Select",+ "ListTypes": "cassandra:Select"+ },+ "cognito-idp": {+ "DescribeTermsByClient": "cognito-idp:DescribeTerms"+ },+ "connect": {+ "UpdateUserConfig": "connect:UpdateUserPhoneConfig"+ },+ "deadline": {+ "BatchGetJob": "deadline:GetJob",+ "BatchGetSession": "deadline:GetSession",+ "BatchGetSessionAction": "deadline:GetSessionAction",+ "BatchGetStep": "deadline:GetStep",+ "BatchGetTask": "deadline:GetTask",+ "BatchGetWorker": "deadline:GetWorker",+ "BatchUpdateJob": "deadline:UpdateJob",+ "BatchUpdateTask": "deadline:UpdateTask"+ },+ "detective": {+ "ListOrganizationAdminAccounts": "detective:ListOrganizationAdminAccount"+ },+ "dms": {+ "DescribeDataProviders": "dms:ListDataProviders",+ "DescribeExtensionPackAssociations": "dms:ListExtensionPacks",+ "DescribeInstanceProfiles": "dms:ListInstanceProfiles",+ "DescribeMetadataModelAssessments": "dms:ListMetadataModelAssessments",+ "DescribeMetadataModelConversions": "dms:ListMetadataModelConversions",+ "DescribeMetadataModelExportsAsScript": "dms:ListMetadataModelExports",+ "DescribeMetadataModelExportsToTarget": "dms:ListMetadataModelExports",+ "DescribeMigrationProjects": "dms:ListMigrationProjects",+ "ModifyConversionConfiguration": "dms:UpdateConversionConfiguration",+ "StartExtensionPackAssociation": "dms:AssociateExtensionPack",+ "StartMetadataModelExportAsScript": "dms:StartMetadataModelExportAsScripts"+ },+ "dynamodb": {+ "TransactGetItems": "dynamodb:GetItem"+ },+ "glue": {+ "BatchGetDataQualityResult": "glue:GetDataQualityResult",+ "BatchGetDataQualityResultV2": "glue:GetDataQualityResult",+ "BatchGetDataQualityRulesetEvaluationRun": "glue:GetDataQualityRulesetEvaluationRun",+ "BatchPutDataQualityStatisticAnnotation": "glue:PutDataQualityStatisticAnnotation",+ "GetDataQualityResultV2": "glue:GetDataQualityResult",+ "ListDataQualityResultsV2": "glue:ListDataQualityResults",+ "ListDataQualityStatisticAnnotations": "glue:GetDataQualityResult",+ "ListDataQualityStatistics": "glue:GetDataQualityResult",+ "PublishDataQualityResultV2": "glue:PublishDataQuality"+ },+ "lakeformation": {+ "GetTemporaryDataLocationCredentials": "lakeformation:GetDataAccess"+ },+ "lambda": {+ "GetLayerVersionByArn": "lambda:GetLayerVersion",+ "Invoke": "lambda:InvokeFunction",+ "InvokeWithResponseStream": "lambda:InvokeFunction"+ },+ "lex": {+ "ListUtteranceAnalyticsData": "lex:ListAggregatedUtterances",+ "ListUtteranceMetrics": "lex:ListAggregatedUtterances"+ },+ "mediaconnect": {+ "BatchGetRouterInput": "mediaconnect:GetRouterInput",+ "BatchGetRouterNetworkInterface": "mediaconnect:GetRouterNetworkInterface",+ "BatchGetRouterOutput": "mediaconnect:GetRouterOutput"+ },+ "qapps": {+ "CreatePresignedUrl": "qapps:ImportDocument"+ },+ "quicksight": {+ "DeleteTopicV2": "quicksight:DeleteTopic",+ "DescribeAnalysisDefinition": "quicksight:DescribeAnalysis",+ "DescribeDashboardDefinition": "quicksight:DescribeDashboard",+ "DescribeTemplateDefinition": "quicksight:DescribeTemplate",+ "DescribeTopicPermissionsV2": "quicksight:DescribeTopicPermissions",+ "DescribeTopicV2": "quicksight:DescribeTopic",+ "ListTopicsV2": "quicksight:ListTopics",+ "SearchTopicsV2": "quicksight:SearchTopics",+ "UpdateTopicPermissionsV2": "quicksight:UpdateTopicPermissions"+ },+ "redshift-data": {+ "GetStatementResultV2": "redshift-data:GetStatementResult"+ },+ "resource-explorer-2": {+ "ListResources": "resource-explorer-2:Search"+ },+ "route53resolver": {+ "BatchCreateFirewallRule": "route53resolver:CreateFirewallRule",+ "BatchDeleteFirewallRule": "route53resolver:DeleteFirewallRule",+ "BatchUpdateFirewallRule": "route53resolver:UpdateFirewallRule"+ },+ "s3": {+ "DeleteBucketAnalyticsConfiguration": "s3:PutAnalyticsConfiguration",+ "DeleteBucketCors": "s3:PutBucketCORS",+ "DeleteBucketEncryption": "s3:PutEncryptionConfiguration",+ "DeleteBucketIntelligentTieringConfiguration": "s3:PutIntelligentTieringConfiguration",+ "DeleteBucketInventoryConfiguration": "s3:PutInventoryConfiguration",+ "DeleteBucketLifecycle": "s3:PutLifecycleConfiguration",+ "DeleteBucketMetadataConfiguration": "s3:DeleteBucketMetadataTableConfiguration",+ "DeleteBucketMetricsConfiguration": "s3:PutMetricsConfiguration",+ "DeleteBucketOwnershipControls": "s3:PutBucketOwnershipControls",+ "GetBucketAccelerateConfiguration": "s3:GetAccelerateConfiguration",+ "GetBucketAnalyticsConfiguration": "s3:GetAnalyticsConfiguration",+ "GetBucketEncryption": "s3:GetEncryptionConfiguration",+ "GetBucketIntelligentTieringConfiguration": "s3:GetIntelligentTieringConfiguration",+ "GetBucketInventoryConfiguration": "s3:GetInventoryConfiguration",+ "GetBucketLifecycle": "s3:GetLifecycleConfiguration",+ "GetBucketMetadataConfiguration": "s3:GetBucketMetadataTableConfiguration",+ "GetBucketMetricsConfiguration": "s3:GetMetricsConfiguration",+ "GetBucketNotificationConfiguration": "s3:GetBucketNotification",+ "GetObjectLockConfiguration": "s3:GetBucketObjectLockConfiguration",+ "HeadBucket": "s3:ListBucket",+ "ListBucketAnalyticsConfigurations": "s3:GetAnalyticsConfiguration",+ "ListBucketIntelligentTieringConfigurations": "s3:GetIntelligentTieringConfiguration",+ "ListBucketInventoryConfigurations": "s3:GetInventoryConfiguration",+ "ListBucketMetricsConfigurations": "s3:GetMetricsConfiguration",+ "ListBuckets": "s3:ListAllMyBuckets",+ "ListObjectVersions": "s3:ListBucketVersions",+ "PutBucketAccelerateConfiguration": "s3:PutAccelerateConfiguration",+ "PutBucketAnalyticsConfiguration": "s3:PutAnalyticsConfiguration",+ "PutBucketEncryption": "s3:PutEncryptionConfiguration",+ "PutBucketIntelligentTieringConfiguration": "s3:PutIntelligentTieringConfiguration",+ "PutBucketInventoryConfiguration": "s3:PutInventoryConfiguration",+ "PutBucketLifecycle": "s3:PutLifecycleConfiguration",+ "PutBucketMetricsConfiguration": "s3:PutMetricsConfiguration",+ "PutBucketNotificationConfiguration": "s3:PutBucketNotification",+ "PutObjectLockConfiguration": "s3:PutBucketObjectLockConfiguration",+ "SelectObjectContent": "s3:GetObject"+ },+ "sagemaker": {+ "UpdateRecord": "sagemaker:PutRecord"+ },+ "securityhub": {+ "BatchUpdateFindingsV2": "securityhub:BatchUpdateFindings",+ "GetFindingStatisticsV2": "securityhub:GetAdhocInsightResults",+ "GetFindingsV2": "securityhub:GetFindings"+ },+ "signin": {+ "CreateOAuth2TokenWithIAM": "signin:CreateOAuth2Token",+ "IntrospectOAuth2TokenWithIAM": "signin:IntrospectOAuth2Token",+ "RevokeOAuth2TokenWithIAM": "signin:RevokeOAuth2Token"+ },+ "sns": {+ "PublishBatch": "sns:Publish"+ },+ "sqs": {+ "ChangeMessageVisibilityBatch": "sqs:ChangeMessageVisibility",+ "DeleteMessageBatch": "sqs:DeleteMessage",+ "SendMessageBatch": "sqs:SendMessage"+ },+ "sso-oauth": {+ "CreateToken": "sso-oauth:CreateTokenWithIAM"+ },+ "tax": {+ "BatchGetTaxExemptions": "tax:GetExemptions",+ "GetTaxExemptionTypes": "tax:GetExemptions",+ "ListTaxExemptions": "tax:GetExemptions",+ "PutTaxExemption": "tax:UpdateExemptions"+ },+ "vpc-lattice": {+ "BatchUpdateRule": "vpc-lattice:UpdateRule"+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/VendoredResourcesTest.java b/src/test/java/io/github/hectorvent/floci/core/common/VendoredResourcesTest.javanew file mode 100644index 0000000000..c89cf7cecb--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/VendoredResourcesTest.java@@ -0,0 +1,42 @@+package io.github.hectorvent.floci.core.common;++import org.junit.jupiter.api.Test;++import java.io.IOException;++import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.instanceOf;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertThrows;++class VendoredResourcesTest {++ private static final String PRESENT = "aws/iam-authorized-actions.json";++ @Test+ void aPresentResourceIsHandedToTheParser() {+ int firstByte = VendoredResources.read(PRESENT, "test map", in -> in.read());++ assertEquals('{', firstByte);+ }++ @Test+ void aMissingResourceIsABuildDefect() {+ IllegalStateException e = assertThrows(IllegalStateException.class,+ () -> VendoredResources.read("aws/absent.json", "test map", in -> in.read()));++ assertEquals("test map resource not found: aws/absent.json", e.getMessage());+ }++ @Test+ void anUnreadableResourceKeepsItsCause() {+ IllegalStateException e = assertThrows(IllegalStateException.class,+ () -> VendoredResources.read(PRESENT, "test map", in -> {+ throw new IOException("truncated");+ }));++ assertThat(e.getMessage(), containsString(PRESENT));+ assertThat(e.getCause(), instanceOf(IOException.class));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionEnforcementIntegrationTest.javanew file mode 100644index 0000000000..2279564f58--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionEnforcementIntegrationTest.java@@ -0,0 +1,231 @@+package io.github.hectorvent.floci.services.iam;++import io.github.hectorvent.floci.testing.EnforcementFixtures;+import io.github.hectorvent.floci.testing.IamEnforcementProfile;+import io.github.hectorvent.floci.testing.PartitionCleanup;+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.common.http.TestHTTPResource;+import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.TestProfile;+import io.restassured.response.ValidatableResponse;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.extension.RegisterExtension;++import java.net.URI;+import java.util.List;+import java.util.Map;+import java.util.UUID;++import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;+import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;++/**+ * Operations AWS authorizes as an action of another name are checked as that action, on the Query+ * and JSON paths alike: a batch call needs the single-item permission AWS documents, and a policy+ * naming the batch operation, which is no IAM action, grants nothing.+ */+@QuarkusTest+@TestProfile(IamEnforcementProfile.class)+class IamAuthorizedActionEnforcementIntegrationTest {++ @TestHTTPResource("/")+ static URI baseUri;++ @RegisterExtension+ final PartitionCleanup cleanup = new PartitionCleanup();++ private static final String ACCOUNT_ID = "000000000000";++ @BeforeAll+ static void configureRestAssured() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ @Test+ void aBatchSendIsGrantedBySendMessageOverQuery() {+ String queueUrl = createQueue();+ String accessKeyId = userAllowed("sqs:SendMessage");++ sqsQuery(accessKeyId, "SendMessageBatch", Map.of("QueueUrl", queueUrl,+ "SendMessageBatchRequestEntry.1.Id", "one",+ "SendMessageBatchRequestEntry.1.MessageBody", "hello"))+ .statusCode(200);+ }++ @Test+ void aBatchSendIsGrantedBySendMessageOverJson() {+ String queueUrl = createQueue();+ String accessKeyId = userAllowed("sqs:SendMessage");++ given()+ .header("Authorization", auth(accessKeyId, "sqs"))+ .header("X-Amz-Target", "AmazonSQS.SendMessageBatch")+ .contentType("application/x-amz-json-1.0")+ .body("""+ {"QueueUrl": "%s", "Entries": [{"Id": "one", "MessageBody": "hello"}]}+ """.formatted(queueUrl))+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void aPolicyNamingTheBatchOperationGrantsNothing() {+ String queueUrl = createQueue();+ String accessKeyId = userAllowed("sqs:SendMessageBatch");++ sqsQuery(accessKeyId, "SendMessageBatch", Map.of("QueueUrl", queueUrl,+ "SendMessageBatchRequestEntry.1.Id", "one",+ "SendMessageBatchRequestEntry.1.MessageBody", "hello"))+ .statusCode(403)+ .body(containsString("sqs:SendMessage"));+ }++ @Test+ void aBatchPublishIsGrantedByPublish() {+ String topicArn = given()+ .header("Authorization", auth(ACCOUNT_ID, "sns"))+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateTopic")+ .formParam("Version", "2010-03-31")+ .formParam("Name", "authorized-" + UUID.randomUUID())+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().xmlPath().getString("CreateTopicResponse.CreateTopicResult.TopicArn");+ cleanup.register(() -> given()+ .header("Authorization", auth(ACCOUNT_ID, "sns"))+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DeleteTopic")+ .formParam("Version", "2010-03-31")+ .formParam("TopicArn", topicArn)+ .when().post("/").then().statusCode(200));+ String accessKeyId = userAllowed("sns:Publish");++ given()+ .header("Authorization", auth(accessKeyId, "sns"))+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "PublishBatch")+ .formParam("Version", "2010-03-31")+ .formParam("TopicArn", topicArn)+ .formParam("PublishBatchRequestEntries.member.1.Id", "one")+ .formParam("PublishBatchRequestEntries.member.1.Message", "hello")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("PublishBatchResponse.PublishBatchResult.Successful.member.Id", equalTo("one"));+ }++ @Test+ void aTransactGetIsCheckedItemByItemAgainstEachLeadingKey() {+ // AWS checks TransactGetItems as dynamodb:GetItem, and Floci checks each Get that way with+ // the item's own leading key. Authorized as one GetItem instead, the call would carry no+ // dynamodb:LeadingKeys, and a ForAllValues condition would grant every key.+ String table = createTable();+ for (String id : List.of("mine", "theirs")) {+ dynamoDb(ACCOUNT_ID, "PutItem", """+ {"TableName": "%s", "Item": {"id": {"S": "%s"}}}+ """.formatted(table, id))+ .statusCode(200);+ }+ String accessKeyId = userWithPolicy("""+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"dynamodb:GetItem","Resource":"*",+ "Condition":{"ForAllValues:StringEquals":{"dynamodb:LeadingKeys":["mine"]}}}+ ]}""");++ dynamoDb(accessKeyId, "TransactGetItems", transactGet(table, "mine"))+ .statusCode(200)+ .body("Responses[0].Item.id.S", equalTo("mine"));+ dynamoDb(accessKeyId, "TransactGetItems", transactGet(table, "theirs"))+ // DynamoDB answers a denial with 400 and its AccessDeniedException, as AWS does.+ .statusCode(400)+ .body("__type", equalTo("AccessDeniedException"))+ .body(containsString("dynamodb:GetItem"));+ }++ private String createTable() {+ String table = "authorized-" + UUID.randomUUID();+ dynamoDb(ACCOUNT_ID, "CreateTable", """+ {"TableName": "%s", "KeySchema": [{"AttributeName": "id", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "id", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST"}+ """.formatted(table))+ .statusCode(200);+ cleanup.register(() -> dynamoDb(ACCOUNT_ID, "DeleteTable", """+ {"TableName": "%s"}+ """.formatted(table))+ .statusCode(200));+ return table;+ }++ private static String transactGet(String table, String id) {+ return """+ {"TransactItems": [{"Get": {"TableName": "%s", "Key": {"id": {"S": "%s"}}}}]}+ """.formatted(table, id);+ }++ private String createQueue() {+ String queueUrl = sqsQuery(ACCOUNT_ID, "CreateQueue",+ Map.of("QueueName", "authorized-" + UUID.randomUUID()))+ .statusCode(200)+ .extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");+ cleanup.register(() -> sqsQuery(ACCOUNT_ID, "DeleteQueue", Map.of("QueueUrl", queueUrl))+ .statusCode(200));+ return queueUrl;+ }++ private String userAllowed(String action) {+ return userWithPolicy("""+ {"Version":"2012-10-17","Statement":[+ {"Effect":"Allow","Action":"%s","Resource":"*"}+ ]}""".formatted(action));+ }++ private String userWithPolicy(String document) {+ String userName = "authorized-" + UUID.randomUUID().toString().substring(0, 8);+ String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),+ auth(ACCOUNT_ID, "iam"), userName).accessKeyId();+ given()+ .header("Authorization", auth(ACCOUNT_ID, "iam"))+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "PutUserPolicy")+ .formParam("Version", "2010-05-08")+ .formParam("UserName", userName)+ .formParam("PolicyName", "only")+ .formParam("PolicyDocument", document)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ return accessKeyId;+ }++ private static ValidatableResponse dynamoDb(String accessKeyId, String operation, String body) {+ return given()+ .header("Authorization", auth(accessKeyId, "dynamodb"))+ .header("X-Amz-Target", "DynamoDB_20120810." + operation)+ .contentType("application/x-amz-json-1.0")+ .body(body)+ .when()+ .post("/")+ .then();+ }++ private static ValidatableResponse sqsQuery(String accessKeyId, String action, Map<String, String> params) {+ RequestSpecification spec = given()+ .header("Authorization", auth(accessKeyId, "sqs"))+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", action)+ .formParam("Version", "2012-11-05");+ params.forEach(spec::formParam);+ return spec.when().post("/").then();+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionsTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionsTest.javanew file mode 100644index 0000000000..00fe570234--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamAuthorizedActionsTest.java@@ -0,0 +1,68 @@+package io.github.hectorvent.floci.services.iam;++import org.junit.jupiter.api.Test;++import java.io.ByteArrayInputStream;+import java.io.IOException;+import java.io.InputStream;+import java.nio.charset.StandardCharsets;+import java.util.Map;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertThrows;++class IamAuthorizedActionsTest {++ @Test+ void batchOperationsAreAuthorizedAsTheirSingleItemAction() {+ assertEquals("sqs:SendMessage", IamAuthorizedActions.actionFor("sqs", "SendMessageBatch"));+ assertEquals("sqs:DeleteMessage", IamAuthorizedActions.actionFor("sqs", "DeleteMessageBatch"));+ assertEquals("sqs:ChangeMessageVisibility",+ IamAuthorizedActions.actionFor("sqs", "ChangeMessageVisibilityBatch"));+ assertEquals("sns:Publish", IamAuthorizedActions.actionFor("sns", "PublishBatch"));+ }++ @Test+ void anOperationTheFileDoesNotNameKeepsItsOwnName() {+ assertEquals("sqs:SendMessage", IamAuthorizedActions.actionFor("sqs", "SendMessage"));+ }++ @Test+ void transactGetItemsKeepsItsOwnNameForTheItemByItemCheck() {+ assertEquals("dynamodb:TransactGetItems", IamAuthorizedActions.actionFor("dynamodb", "TransactGetItems"));+ }++ @Test+ void entriesAreKeyedByNamespaceAndOperation() throws IOException {+ Map<String, String> parsed = IamAuthorizedActions.parse(json("""+ {"authorizedActions": {"sqs": {"SendMessageBatch": "sqs:SendMessage"}}}+ """));+ assertEquals(Map.of("sqs:SendMessageBatch", "sqs:SendMessage"), parsed);+ }++ @Test+ void anActionOfAnotherNamespaceIsRefused() {+ assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("""+ {"authorizedActions": {"sqs": {"SendMessageBatch": "sns:Publish"}}}+ """)));+ }++ @Test+ void aNamespaceWithNoActionNameIsRefused() {+ assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("""+ {"authorizedActions": {"sqs": {"SendMessageBatch": "sqs:"}}}+ """)));+ }++ @Test+ void aFileWithoutEntriesIsRefused() {+ assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("{}")));+ assertThrows(IllegalStateException.class, () -> IamAuthorizedActions.parse(json("""+ {"authorizedActions": {"sqs": ["SendMessageBatch"]}}+ """)));+ }++ private static InputStream json(String text) {+ return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));+ }+}diff --git a/tools/aws/regen_iam_authorized_actions.py b/tools/aws/regen_iam_authorized_actions.pynew file mode 100644index 0000000000..96baa4aa4f--- /dev/null+++ b/tools/aws/regen_iam_authorized_actions.py@@ -0,0 +1,324 @@+#!/usr/bin/env python3+"""Regenerate src/main/resources/aws/iam-authorized-actions.json from AWS's Service Reference.++Under IAM enforcement a request that names its operation, in a Query `Action` or an `X-Amz-Target`+header, is authorized as `<namespace>:<Operation>`, the operation's own name. For most operations+that is the IAM action AWS checks, but not for all: SQS `SendMessageBatch` is authorized as+`sqs:SendMessage`, SNS `PublishBatch` as `sns:Publish`, and `sqs:SendMessageBatch` is not an IAM+action at all. A least-privilege policy AWS honours for a batch call is then denied, and a policy+naming the batch operation grants something AWS would not.++AWS publishes the mapping per service, in the `Operations` list of each Service Reference file:++ https://servicereference.us-east-1.amazonaws.com/ (the index of services)+ https://servicereference.us-east-1.amazonaws.com/v1/<svc>/<svc>.json++Each operation names the actions it is authorized by. Only one shape is vendored: an operation+authorized by exactly one action of its own namespace under another name, where no action of the+operation's own name exists. Everything else stays authorized as the operation's own name, and+each exclusion is deliberate:++- **Several actions.** `CreateAutoScalingGroup` lists `autoscaling:CreateOrUpdateTags` and+ `iam:PassRole` beside its own action, but AWS checks those only when tags or a role are passed,+ which the reference does not say. Requiring them all would deny calls AWS allows.+- **No action.** Elastic Beanstalk lists almost every operation with none, which is missing data,+ not operations that need no permission.+- **Another namespace.** A lone `iam:PassRole` is the side permission with the operation's own+ action missing from the data, and a namespace alias such as `opensearch` to `es` is a question+ of which namespace a service is authorized under, not of the action's name.+- **An action of the operation's own name exists.** Glue lists `CreatePartitionIndex` as+ authorized by `glue:UpdateTable` while also publishing `glue:CreatePartitionIndex`, and no source+ says which one AWS checks. A name differing only in case falls here too: IAM matches action+ names without regard to case, so it is not a different action.+- **A tagging action alone.** EC2 lists `CreateCapacityReservationDateChangeQuote` as authorized by+ `ec2:CreateTags` only, the tag-on-create permission with the operation's own action missing.++The endpoint is live rather than a pinned dependency, so a fresh generation is not byte-identical.+As with `regen_iam_action_resources.py`, what must hold is that nothing here is invented:++- `--check` is offline and structural: sorted, every name usable, every action in its own+ namespace and differing from the operation, and the anchor cases still present.+- `--verify` is the real gate and needs the network. Every vendored entry must still be what AWS+ publishes for that operation. An entry AWS has added is reported without failing: being behind+ leaves that operation authorized as its own name, which is where it was before this existed.++Run from anywhere in the repo:+ python3 tools/aws/regen_iam_authorized_actions.py # rewrite the vendored file in place+ python3 tools/aws/regen_iam_authorized_actions.py --check # offline: shape of the vendored file+ python3 tools/aws/regen_iam_authorized_actions.py --verify # online: nothing in it is invented+ python3 tools/aws/regen_iam_authorized_actions.py --source-dir <dir> # read saved <svc>.json files+"""+from __future__ import annotations++import argparse+import json+import re+import sys+import urllib.request+from concurrent.futures import ThreadPoolExecutor+from datetime import datetime, timezone+from pathlib import Path++REPO_ROOT = Path(__file__).resolve().parents[2]+OUTPUT = REPO_ROOT / "src/main/resources/aws/iam-authorized-actions.json"+INDEX_URL = "https://servicereference.us-east-1.amazonaws.com/"+FETCH_TIMEOUT_SECONDS = 30+FETCH_WORKERS = 8++# An IAM service namespace, the prefix of an action.+NAMESPACE_RE = re.compile(r"^[a-z0-9][a-z0-9-]{0,63}$")+# An operation or action name.+NAME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9]{0,127}$")+# The tagging actions AWS lists as a side permission of a create-with-tags operation.+TAGGING_ACTION_RE = re.compile(r"^(CreateTags|DeleteTags|TagResource|UntagResource|CreateOrUpdateTags"+ r"|AddTags\w*|RemoveTags\w*)$")++# The cases that justify vendoring this: batch operations authorized as their single-item action,+# none of them an IAM action of its own.+ANCHORS = {+ ("sqs", "SendMessageBatch"): "sqs:SendMessage",+ ("sqs", "DeleteMessageBatch"): "sqs:DeleteMessage",+ ("sqs", "ChangeMessageVisibilityBatch"): "sqs:ChangeMessageVisibility",+ ("sns", "PublishBatch"): "sns:Publish",+ ("dynamodb", "TransactGetItems"): "dynamodb:GetItem",+}+++def _get_json(url: str):+ request = urllib.request.Request(url, headers={"Accept": "application/json"})+ with urllib.request.urlopen(request, timeout=FETCH_TIMEOUT_SECONDS) as response:+ return json.loads(response.read().decode("utf-8"))+++def fetch_references(source_dir: Path | None) -> tuple[list[dict], str]:+ """Every service's reference document and a provenance label for where they came from."""+ if source_dir is not None:+ documents = [json.loads(path.read_text(encoding="utf-8"))+ for path in sorted(source_dir.glob("*.json")) if not path.name.startswith("_")]+ return documents, f"--source-dir {source_dir}"+ index = _get_json(INDEX_URL)+ if not isinstance(index, list) or not index:+ raise ValueError("the Service Reference index must be a non-empty list")+ urls = []+ for entry in index:+ if not isinstance(entry, dict) or "url" not in entry:+ raise ValueError(f"index entry without a 'url' field: {entry!r}")+ urls.append(entry["url"])+ with ThreadPoolExecutor(max_workers=FETCH_WORKERS) as pool:+ documents = list(pool.map(_get_json, urls))+ return documents, INDEX_URL+++def authorized_actions_from(documents: list[dict]) -> dict[str, dict[str, str]]:+ """Each namespace's operations authorized by one action of another name, sorted.++ An operation keeps its own name when the reference gives it no single answer: when it is listed+ for several SDK clients, which the reference gives one action between them although AWS can+ authorize each client's call differently (S3's GetPublicAccessBlock is+ s3:GetBucketPublicAccessBlock, S3 Control's is the account's), or when its name is listed more+ than once with different answers.+ """+ answers: dict[str, dict[str, set[str | None]]] = {}+ for document in documents:+ namespace = document.get("Name")+ if not isinstance(namespace, str) or not NAMESPACE_RE.match(namespace):+ raise ValueError(f"{namespace!r} is not usable as an IAM namespace")+ own_actions = {action["Name"].lower() for action in document.get("Actions", [])+ if isinstance(action, dict) and isinstance(action.get("Name"), str)}+ for operation in document.get("Operations", []):+ name = operation.get("Name") if isinstance(operation, dict) else None+ if not isinstance(name, str) or not NAME_RE.match(name):+ raise ValueError(f"{namespace}: operation entry without a usable 'Name': {operation!r}")+ authorized = _authorized_actions(namespace, operation)+ mapped = None+ if len(authorized) == 1 and len(_sdk_clients(namespace, operation)) <= 1:+ service, action = authorized[0]+ if (service == namespace and NAME_RE.match(action) and action != name+ and name.lower() not in own_actions and not TAGGING_ACTION_RE.match(action)):+ mapped = f"{namespace}:{action}"+ answers.setdefault(namespace, {}).setdefault(name, set()).add(mapped)+ found = {namespace: {name: next(iter(mapped)) for name, mapped in sorted(operations.items())+ if len(mapped) == 1 and None not in mapped}+ for namespace, operations in sorted(answers.items())}+ return {namespace: operations for namespace, operations in found.items() if operations}+++def _authorized_actions(namespace: str, operation: dict) -> list[tuple[str, str]]:+ """The operation's authorized actions as (service, name) pairs. A record of the wrong shape is+ a malformed response, not an operation to leave out: skipping it would vendor a map that+ silently lacks the operation."""+ authorized = operation.get("AuthorizedActions") or []+ if not isinstance(authorized, list) or not all(+ isinstance(entry, dict) and isinstance(entry.get("Service"), str)+ and isinstance(entry.get("Name"), str) for entry in authorized):+ raise ValueError(f"{namespace}:{operation['Name']}: 'AuthorizedActions' is not a list of "+ f"objects with a string 'Service' and 'Name': {authorized!r}")+ return [(entry["Service"], entry["Name"]) for entry in authorized]+++def _sdk_clients(namespace: str, operation: dict) -> set[str]:+ """The SDK clients the operation is listed for."""+ sdks = operation.get("SDK") or []+ if not isinstance(sdks, list) or not all(+ isinstance(sdk, dict) and isinstance(sdk.get("Name"), str) for sdk in sdks):+ raise ValueError(f"{namespace}:{operation['Name']}: 'SDK' is not a list of objects with a "+ f"string 'Name': {sdks!r}")+ return {sdk["Name"] for sdk in sdks}+++def build(documents: list[dict], provenance: str) -> dict:+ return {+ "_source": {+ "generator": "tools/aws/regen_iam_authorized_actions.py",+ "reference": provenance,+ "retrieved": datetime.now(timezone.utc).strftime("%Y-%m-%d"),+ "note": "make iam-authorized-actions-verify checks every entry against AWS.",+ },+ "authorizedActions": authorized_actions_from(documents),+ }+++def render(document: dict) -> str:+ return json.dumps(document, indent=2) + "\n"+++def validate(document: dict) -> list[str]:+ """Problems that make the vendored file unusable, rather than merely out of date."""+ problems: list[str] = []+ namespaces = document.get("authorizedActions")+ if not isinstance(namespaces, dict) or not namespaces:+ return ["authorizedActions is missing or empty"]+ if list(namespaces) != sorted(namespaces):+ problems.append("authorizedActions is not sorted by namespace")+ for namespace, operations in namespaces.items():+ if not NAMESPACE_RE.match(namespace):+ problems.append(f"{namespace!r} is not usable as an IAM namespace")+ if not isinstance(operations, dict) or not operations:+ problems.append(f"{namespace!r} carries no operations")+ continue+ if list(operations) != sorted(operations):+ problems.append(f"{namespace!r} lists its operations out of order")+ for operation, action in operations.items():+ if not NAME_RE.match(operation):+ problems.append(f"{namespace}: {operation!r} is not usable as an operation name")+ prefix, _, name = action.partition(":") if isinstance(action, str) else ("", "", "")+ if prefix != namespace or not NAME_RE.match(name):+ problems.append(f"{namespace}:{operation} maps to {action!r}, "+ f"which is not an action of {namespace}")+ elif name.lower() == operation.lower():+ problems.append(f"{namespace}:{operation} maps to its own name")+ for (namespace, operation), expected in ANCHORS.items():+ actual = namespaces.get(namespace, {}).get(operation)+ if actual != expected:+ problems.append(f"expected {namespace}:{operation} to be authorized as {expected}, "+ f"not {actual}")+ return problems+++def verify_against(vendored: dict[str, dict[str, str]],+ upstream: dict[str, dict[str, str]]) -> tuple[list[str], list[str]]:+ """What the vendored file claims and AWS does not, and what AWS has since added.++ A claim AWS no longer makes is a failure: the operation would be authorized as an action AWS+ does not check for it. An entry AWS has added only means the file is behind.+ """+ untrue: list[str] = []+ added: list[str] = []+ for namespace, operations in vendored.items():+ for operation, action in operations.items():+ actual = upstream.get(namespace, {}).get(operation)+ if actual != action:+ untrue.append(f"AWS does not authorize {namespace}:{operation} as {action}"+ + (f" but as {actual}" if actual else ""))+ for namespace, operations in upstream.items():+ for operation, action in operations.items():+ if operation not in vendored.get(namespace, {}):+ added.append(f"{namespace}:{operation} -> {action}")+ return untrue, sorted(added)+++def _shown(path: Path) -> Path:+ """The path relative to the repository when it is inside it, as the messages print it."""+ try:+ return path.resolve().relative_to(REPO_ROOT)+ except ValueError:+ return path+++def main(argv: list[str] | None = None) -> int:+ parser = argparse.ArgumentParser(description=__doc__,+ formatter_class=argparse.RawDescriptionHelpFormatter)+ parser.add_argument("--check", action="store_true",+ help="offline: validate the shape of the vendored file")+ parser.add_argument("--verify", action="store_true",+ help="online: nothing in the vendored file is invented")+ parser.add_argument("--source-dir", type=Path, default=None,+ help="read saved <service>.json reference files instead of fetching them")+ parser.add_argument("--output", type=Path, default=OUTPUT, help=argparse.SUPPRESS)+ args = parser.parse_args(argv)++ if args.check:+ if not args.output.exists():+ print(f"error: {args.output} does not exist", file=sys.stderr)+ return 1+ document = json.loads(args.output.read_text(encoding="utf-8"))+ problems = validate(document)+ for problem in problems:+ print(f"error: {problem}", file=sys.stderr)+ if problems:+ return 1+ entries = sum(len(operations) for operations in document["authorizedActions"].values())+ print(f"{_shown(args.output)} is well-formed "+ f"({entries} operations over {len(document['authorizedActions'])} namespaces)")+ return 0++ if args.verify:+ if not args.output.exists():+ print(f"error: {args.output} does not exist", file=sys.stderr)+ return 1+ vendored = json.loads(args.output.read_text(encoding="utf-8"))["authorizedActions"]+ try:+ documents, provenance = fetch_references(args.source_dir)+ upstream = authorized_actions_from(documents)+ except (OSError, ValueError) as e:+ # The gate could not run, which CI has to tell apart from having found something+ # invented. ValueError covers a malformed or truncated response as well.+ print(f"error: could not read the Service Reference ({e})", file=sys.stderr)+ return 2+ untrue, added = verify_against(vendored, upstream)+ for claim in untrue:+ print(f"error: {claim}", file=sys.stderr)+ entries = sum(len(operations) for operations in vendored.values())+ if untrue:+ print(f"error: {len(untrue)} vendored claim(s) do not hold upstream; those operations "+ f"would be authorized as an action AWS does not check.", file=sys.stderr)+ return 1+ if added:+ print(f"{_shown(args.output)}: all {entries} entries verified. AWS has "+ f"since added {len(added)}, so the file is incomplete but not wrong (run without "+ f"--verify to refresh): {', '.join(added[:10])}{'...' if len(added) > 10 else ''}")+ else:+ print(f"{_shown(args.output)}: all {entries} entries verified against "+ f"{provenance}, nothing added upstream.")+ return 0++ try:+ documents, provenance = fetch_references(args.source_dir)+ document = build(documents, provenance)+ except (OSError, ValueError) as e:+ print(f"error: could not read the Service Reference ({e})", file=sys.stderr)+ return 2+ problems = validate(document)+ for problem in problems:+ print(f"error: {problem}", file=sys.stderr)+ if problems:+ return 1+ args.output.write_text(render(document), encoding="utf-8")+ entries = sum(len(operations) for operations in document["authorizedActions"].values())+ print(f"wrote {_shown(args.output)} ({entries} operations over "+ f"{len(document['authorizedActions'])} namespaces, from {provenance})")+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tools/aws/test_regen_iam_authorized_actions.py b/tools/aws/test_regen_iam_authorized_actions.pynew file mode 100644index 0000000000..3cbad657de--- /dev/null+++ b/tools/aws/test_regen_iam_authorized_actions.py@@ -0,0 +1,294 @@+"""Tests for regen_iam_authorized_actions.++Run with: pytest tools/aws -q (or: make iam-authorized-actions-test)+"""+from __future__ import annotations++import json++import pytest++import regen_iam_authorized_actions as r+++def service(namespace: str, operations: dict[str, list[str]], actions: list[str] | None = None) -> dict:+ """A reference document shaped like AWS's. Each operation maps to the actions authorizing it,+ written `service:Name`; `actions` are the IAM actions the namespace publishes."""+ return {+ "Name": namespace,+ "Actions": [{"Name": name} for name in (actions or [])],+ "Operations": [+ {"Name": name,+ "AuthorizedActions": [{"Service": a.split(":")[0], "Name": a.split(":")[1]} for a in authorized]}+ for name, authorized in operations.items()+ ],+ }+++ANCHOR_SERVICES = [+ service("sqs", {"SendMessageBatch": ["sqs:SendMessage"],+ "DeleteMessageBatch": ["sqs:DeleteMessage"],+ "ChangeMessageVisibilityBatch": ["sqs:ChangeMessageVisibility"]},+ ["SendMessage", "DeleteMessage", "ChangeMessageVisibility"]),+ service("sns", {"PublishBatch": ["sns:Publish"]}, ["Publish"]),+ service("dynamodb", {"TransactGetItems": ["dynamodb:GetItem"]}, ["GetItem"]),+]+++# --------------------------------------------------------------------------- #+# authorized_actions_from: which operations are vendored+# --------------------------------------------------------------------------- #+def test_an_operation_authorized_by_one_differently_named_action_is_kept():+ found = r.authorized_actions_from([service("sqs", {"SendMessageBatch": ["sqs:SendMessage"]},+ ["SendMessage"])])+ assert found == {"sqs": {"SendMessageBatch": "sqs:SendMessage"}}+++def test_an_operation_authorized_as_its_own_name_is_not_vendored():+ assert r.authorized_actions_from([service("sqs", {"SendMessage": ["sqs:SendMessage"]},+ ["SendMessage"])]) == {}+++def test_several_actions_are_left_to_the_operations_own_name():+ # CreateAutoScalingGroup also lists the tag and PassRole permissions, which AWS checks only+ # when tags or a role are passed; requiring them all would deny calls AWS allows.+ found = r.authorized_actions_from([service("autoscaling", {"CreateAutoScalingGroup": [+ "autoscaling:CreateAutoScalingGroup", "autoscaling:CreateOrUpdateTags", "iam:PassRole"]})])+ assert found == {}+++def test_an_operation_with_no_action_is_missing_data_not_a_free_call():+ assert r.authorized_actions_from([service("elasticbeanstalk", {"AbortEnvironmentUpdate": []})]) == {}+++def test_an_action_in_another_namespace_is_not_vendored():+ found = r.authorized_actions_from([+ service("transcribe", {"UpdateLanguageModel": ["iam:PassRole"]}),+ service("opensearch", {"AddTags": ["es:AddTags"]}),+ ])+ assert found == {}+++def test_an_operation_whose_own_name_is_also_an_action_is_left_alone():+ found = r.authorized_actions_from([service(+ "glue", {"CreatePartitionIndex": ["glue:UpdateTable"]}, ["CreatePartitionIndex", "UpdateTable"])])+ assert found == {}+++def test_a_name_differing_only_in_case_is_the_same_action():+ found = r.authorized_actions_from([service(+ "memorydb", {"DeleteACL": ["memorydb:DeleteAcl"]}, ["DeleteAcl"])])+ assert found == {}+++def test_a_tagging_action_alone_is_a_side_permission():+ found = r.authorized_actions_from([service(+ "ec2", {"CreateCapacityReservationDateChangeQuote": ["ec2:CreateTags"]}, ["CreateTags"])])+ assert found == {}+++def test_namespaces_and_operations_are_sorted():+ found = r.authorized_actions_from([+ service("sqs", {"SendMessageBatch": ["sqs:SendMessage"],+ "DeleteMessageBatch": ["sqs:DeleteMessage"]}),+ service("dms", {"DescribeDataProviders": ["dms:ListDataProviders"]}),+ ])+ assert list(found) == ["dms", "sqs"]+ assert list(found["sqs"]) == ["DeleteMessageBatch", "SendMessageBatch"]++[email protected]("namespace", [None, "", "Not A Namespace", 7])+def test_an_unusable_namespace_is_rejected(namespace):+ with pytest.raises(ValueError):+ r.authorized_actions_from([{"Name": namespace, "Operations": []}])+++def test_an_operation_without_a_name_is_rejected():+ with pytest.raises(ValueError):+ r.authorized_actions_from([{"Name": "sqs", "Operations": [{"AuthorizedActions": []}]}])++[email protected]("authorized", [+ [{"Service": "sqs", "Name": 7}],+ [{"Service": "sqs"}],+ [{"Service": None, "Name": "SendMessage"}],+ [None],+ ["sqs:SendMessage"],+ "sqs:SendMessage",+])+def test_an_authorized_action_record_of_the_wrong_shape_is_a_malformed_response(authorized):+ # Skipping it would vendor a map that silently lacks the operation.+ with pytest.raises(ValueError):+ r.authorized_actions_from([{"Name": "sqs", "Operations": [+ {"Name": "SendMessageBatch", "AuthorizedActions": authorized}]}])+++def test_an_action_name_iam_would_not_accept_is_left_out_not_rejected():+ # AWS publishes this one for vpc-lattice; a well-formed record naming it keeps the operation's own name.+ found = r.authorized_actions_from([service(+ "vpc-lattice", {"AssociateServiceNetwork": ["vpc-lattice:AssociateViaAWSService-EventsAndStates"]})])+ assert found == {}+++def with_sdk_clients(document: dict, *clients: str) -> dict:+ for operation in document["Operations"]:+ operation["SDK"] = [{"Name": client, "Method": "m", "Package": "Boto3"} for client in clients]+ return document+++def test_an_operation_listed_for_several_sdk_clients_keeps_its_own_name():+ # S3 Control's GetPublicAccessBlock is the account's, which the one action given for both misses.+ shared = with_sdk_clients(service("s3", {"GetPublicAccessBlock": ["s3:GetBucketPublicAccessBlock"]}),+ "s3", "s3control")+ assert r.authorized_actions_from([shared]) == {}++ single = with_sdk_clients(service("sqs", {"SendMessageBatch": ["sqs:SendMessage"]}), "sqs")+ assert r.authorized_actions_from([single]) == {"sqs": {"SendMessageBatch": "sqs:SendMessage"}}+++def test_a_name_listed_twice_with_different_answers_keeps_its_own_name():+ found = r.authorized_actions_from([+ service("s3", {"GetPublicAccessBlock": ["s3:GetBucketPublicAccessBlock"]}),+ service("s3", {"GetPublicAccessBlock": ["s3:GetAccountPublicAccessBlock"]}),+ ])+ assert found == {}++ found = r.authorized_actions_from([+ service("s3", {"GetPublicAccessBlock": ["s3:GetBucketPublicAccessBlock"]}),+ service("s3", {"GetPublicAccessBlock": ["s3:GetBucketPublicAccessBlock", "s3:TagResource"]}),+ ])+ assert found == {}+++def test_a_name_listed_twice_with_the_same_answer_is_kept():+ found = r.authorized_actions_from([+ service("sqs", {"SendMessageBatch": ["sqs:SendMessage"]}),+ service("sqs", {"SendMessageBatch": ["sqs:SendMessage"]}),+ ])+ assert found == {"sqs": {"SendMessageBatch": "sqs:SendMessage"}}+++def test_a_malformed_sdk_list_is_a_malformed_response():+ document = service("sqs", {"SendMessageBatch": ["sqs:SendMessage"]})+ document["Operations"][0]["SDK"] = [None]+ with pytest.raises(ValueError):+ r.authorized_actions_from([document])+++# --------------------------------------------------------------------------- #+# validate: the offline --check gate+# --------------------------------------------------------------------------- #+def anchored(extra: dict[str, dict[str, str]] | None = None) -> dict:+ document = r.build(ANCHOR_SERVICES, "a test")+ for namespace, operations in (extra or {}).items():+ document["authorizedActions"].setdefault(namespace, {}).update(operations)+ document["authorizedActions"] = {+ namespace: dict(sorted(operations.items()))+ for namespace, operations in sorted(document["authorizedActions"].items())}+ return document+++def test_a_freshly_built_document_validates():+ assert r.validate(anchored()) == []+++def test_an_action_in_another_namespace_is_rejected():+ problems = r.validate(anchored({"sqs": {"PurgeQueueBatch": "sns:Publish"}}))+ assert any("not an action of sqs" in problem for problem in problems)+++def test_an_operation_mapped_to_its_own_name_is_rejected():+ problems = r.validate(anchored({"sqs": {"Purgequeue": "sqs:PurgeQueue"}}))+ assert any("its own name" in problem for problem in problems)+++def test_a_missing_anchor_is_rejected():+ document = anchored()+ del document["authorizedActions"]["sns"]+ assert any("sns:PublishBatch" in problem for problem in r.validate(document))+++def test_an_anchor_mapped_elsewhere_is_rejected():+ document = anchored()+ document["authorizedActions"]["sqs"]["SendMessageBatch"] = "sqs:ReceiveMessage"+ assert any("sqs:SendMessageBatch" in problem for problem in r.validate(document))+++def test_an_unsorted_namespace_map_is_rejected():+ document = anchored()+ document["authorizedActions"] = dict(reversed(list(document["authorizedActions"].items())))+ assert "authorizedActions is not sorted by namespace" in r.validate(document)+++def test_an_empty_map_is_rejected():+ assert r.validate({"authorizedActions": {}}) == ["authorizedActions is missing or empty"]+++# --------------------------------------------------------------------------- #+# verify_against: the online --verify gate+# --------------------------------------------------------------------------- #+def test_an_identical_map_verifies_clean():+ vendored = {"sqs": {"SendMessageBatch": "sqs:SendMessage"}}+ assert r.verify_against(vendored, vendored) == ([], [])+++def test_a_claim_aws_does_not_make_fails():+ untrue, _ = r.verify_against({"sqs": {"SendMessageBatch": "sqs:SendMessageBatch"}},+ {"sqs": {"SendMessageBatch": "sqs:SendMessage"}})+ assert untrue == ["AWS does not authorize sqs:SendMessageBatch as sqs:SendMessageBatch "+ "but as sqs:SendMessage"]+++def test_an_entry_aws_no_longer_publishes_fails():+ untrue, _ = r.verify_against({"sqs": {"SendMessageBatch": "sqs:SendMessage"}}, {})+ assert untrue == ["AWS does not authorize sqs:SendMessageBatch as sqs:SendMessage"]+++def test_an_entry_aws_has_added_is_reported_without_failing():+ untrue, added = r.verify_against({}, {"sns": {"PublishBatch": "sns:Publish"}})+ assert untrue == []+ assert added == ["sns:PublishBatch -> sns:Publish"]+++# --------------------------------------------------------------------------- #+# main: the file round trip+# --------------------------------------------------------------------------- #+def test_generating_from_saved_files_writes_a_file_check_accepts(tmp_path):+ sources = tmp_path / "reference"+ sources.mkdir()+ for document in ANCHOR_SERVICES:+ (sources / f"{document['Name']}.json").write_text(json.dumps(document), encoding="utf-8")+ output = tmp_path / "iam-authorized-actions.json"+ assert r.main(["--source-dir", str(sources), "--output", str(output)]) == 0+ assert r.main(["--check", "--output", str(output)]) == 0+ assert r.main(["--verify", "--source-dir", str(sources), "--output", str(output)]) == 0+++def saved_sources(tmp_path, documents: list[dict]):+ sources = tmp_path / "reference"+ sources.mkdir()+ for index, document in enumerate(documents):+ (sources / f"{index}-{document['Name']}.json").write_text(json.dumps(document), encoding="utf-8")+ return sources+++MALFORMED = {"Name": "sqs", "Operations": [+ {"Name": "ChangeMessageVisibilityBatch", "AuthorizedActions": [{"Service": "sqs", "Name": 7}]}]}+++def test_a_malformed_record_makes_generation_write_nothing(tmp_path):+ sources = saved_sources(tmp_path, ANCHOR_SERVICES + [MALFORMED])+ output = tmp_path / "iam-authorized-actions.json"+ assert r.main(["--source-dir", str(sources), "--output", str(output)]) == 2+ assert not output.exists()+++def test_verify_reports_a_malformed_record_as_an_unreadable_reference(tmp_path):+ # Status 2 tells CI the reference could not be read, rather than that the vendored map is wrong.+ output = tmp_path / "iam-authorized-actions.json"+ assert r.main(["--source-dir", str(saved_sources(tmp_path, ANCHOR_SERVICES)), "--output", str(output)]) == 0+ malformed = tmp_path / "malformed"+ malformed.mkdir()+ (malformed / "sqs.json").write_text(json.dumps({"Name": "sqs", "Operations": [+ {"Name": "SendMessageBatch", "AuthorizedActions": [None]}]}), encoding="utf-8")+ assert r.main(["--verify", "--source-dir", str(malformed), "--output", str(output)]) == 2