mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
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:
1 parent
c6c2c609c3
commit
eba058266b
15 files changed
+1618
-17
No files matched your search
+143
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user