fix(iam): authorize an operation as the action AWS checks for it (#5604)

Under enforcement a request naming its operation, in a Query Action or an
X-Amz-Target header, was authorized as <namespace>:<Operation>, the operation's
own name. AWS authorizes some operations as an action of another name: SQS
SendMessageBatch, DeleteMessageBatch and ChangeMessageVisibilityBatch as
sqs:SendMessage, sqs:DeleteMessage and sqs:ChangeMessageVisibility, and SNS
PublishBatch as sns:Publish. None of the batch names is an IAM action, so a policy
AWS honours for a batch call was denied, and a policy naming the batch operation
granted something AWS does not check.

AWS publishes the mapping in the Operations of each Service Reference file.
tools/aws/regen_iam_authorized_actions.py vendors the operations authorized by
exactly one action of their own namespace under another name, 235 over 33
namespaces, into src/main/resources/aws/iam-authorized-actions.json. Everything else
keeps its own name. The generator leaves out operations that list:
- several actions, whose extras apply only when tags or a role are passed;
- no action, which is missing data;
- an action of another namespace;
- another action while one of the operation's own name exists;
- a tagging action alone;
- the same action for several SDK clients, which AWS can authorize differently,
  as S3 and S3 Control's GetPublicAccessBlock;
or whose name is listed again with another answer. A record of the wrong shape
fails the run instead of being skipped.
make iam-authorized-actions-check gates the file's shape on pull requests that touch it, and
iam-authorized-actions-verify, run by the IAM Data Drift workflow, checks every
entry against AWS.

IamAuthorizedActions reads the file through the new core.common
VendoredResources, rather than adding one more copy of the read-or-fail block a
dozen catalogs carry, and the Query and X-Amz-Target paths ask it for the action.
DynamoDB TransactGetItems, which AWS authorizes as dynamodb:GetItem, keeps its own
name: the transaction check recognizes the call by it and checks every Get as
dynamodb:GetItem on its own table, with its own dynamodb:LeadingKeys. Renamed, the
call would skip that check and be authorized as one GetItem whose body has no
top-level Key, which a ForAllValues condition accepts, so a fine-grained policy
would grant every key.

The check for operations AWS serves without credentials reads the Query action
directly, rather than through resolve with a placeholder scope, now that resolve
returns the authorized action. A Query request naming no action is not one of them.

An SDK compatibility test covers SendMessageBatch and PublishBatch, granted by
the single-item action and refused under the batch name, with enforcement on.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-13 22:29:06 -05:00
1 parent c6c2c609c3
commit eba058266b
15 files changed
+1618 -17

No files matched your search

@@ -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));
}
}
@@ -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();
}
}
@@ -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));
}
}