mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): authorize ListTables against its regional collection (#7336)
* fix(dynamodb): authorize ListTables against its regional collection * test(dynamodb): reject ListTables authorization through ignored selectors * fix: generate the DynamoDB ListTables request resource Record the measured table collection as a generated correction and dispatch table-owned operations before hand-built fallbacks. Preserve batch, transaction and PartiQL resource extraction. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
2b07f85a71
commit
918c57a64e
10 files changed
+223
-10
No files matched your search
+5
-1
@@ -42,7 +42,11 @@ class RequestResourceMembersTest {
|
||||
RequestResourceMembers.Table table = RequestResourceMembers.table();
|
||||
assertTrue(table.namespaces().containsAll(List.of("cognito-idp", "ecr", "elasticloadbalancing", "states")));
|
||||
assertTrue(table.handBuilt().contains("lambda"));
|
||||
assertTrue(table.namespaces().stream().noneMatch(table.handBuilt()::contains));
|
||||
assertEquals(Set.of("dynamodb"), table.namespaces().stream().filter(table.handBuilt()::contains)
|
||||
.collect(Collectors.toSet()));
|
||||
assertEquals(Set.of("dynamodb:ListTables"), table.operations().keySet().stream()
|
||||
.filter(operation -> table.handBuilt().contains(operation.substring(0, operation.indexOf(':'))))
|
||||
.collect(Collectors.toSet()), "only explicit corrections override hand-built operations");
|
||||
assertTrue(table.namespaces().stream().noneMatch(table.notCovered()::contains));
|
||||
assertTrue(table.otherNamespaces().stream().noneMatch(table.notCovered()::contains));
|
||||
table.operations().forEach((operation, resources) -> {
|
||||
|
||||
@@ -16,6 +16,8 @@ import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -25,6 +27,19 @@ class RequestResourcesTest {
|
||||
private static final ObjectMapper CBOR = new ObjectMapper(new CBORFactory());
|
||||
private static final String MACHINE = "arn:aws:states:us-east-1:000000000000:stateMachine:orders";
|
||||
|
||||
@Test
|
||||
void listTablesCorrectionReadsTheCollectionAndLeavesOtherDynamoOperationsHandBuilt() {
|
||||
assertTrue(RequestResources.reads("dynamodb:ListTables"));
|
||||
assertFalse(RequestResources.reads("dynamodb:BatchWriteItem"));
|
||||
assertFalse(RequestResources.reads("dynamodb:ExecuteStatement"));
|
||||
assertFalse(RequestResources.reads(null));
|
||||
for (String body : List.of("{}", "{\"TableName\":\"ignored\",\"ExclusiveStartTableName\":\"cursor\"}")) {
|
||||
assertEquals(List.of("arn:aws-us-gov:dynamodb:us-gov-west-1:111122223333:table/*"),
|
||||
RequestResources.of("dynamodb:ListTables", json(body), "aws-us-gov", "us-gov-west-1",
|
||||
"111122223333"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMemberHoldingAnArnOfATypeTheActionActsOnNamesThatResource() {
|
||||
assertEquals(List.of(MACHINE), of("states:DescribeStateMachine",
|
||||
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import io.github.hectorvent.floci.testing.SignedIamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.SignedIamUsers;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
||||
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(SignedIamEnforcementProfile.class)
|
||||
class DynamoDbListTablesAuthorizationIntegrationTest {
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/*,false,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/*,true,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/orders,false,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/orders,true,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/orders-*,false,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/orders-*,true,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/,false,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/,true,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/?,false,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/?,true,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:*,false,true",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:*,true,false",
|
||||
"arn:aws:dynamodb:eu-central-1:000000000000:table/*,false,false",
|
||||
"arn:aws:dynamodb:eu-central-1:000000000000:table/*,true,true",
|
||||
"arn:aws:dynamodb:us-east-1:123400009876:table/*,false,false",
|
||||
"arn:aws:dynamodb:us-east-1:123400009876:table/*,true,true",
|
||||
"arn:aws:dynamodb:*:*:table/*,false,true",
|
||||
"arn:aws:dynamodb:*:*:table/*,true,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/*/index/*,false,false",
|
||||
"arn:aws:dynamodb:us-east-1:000000000000:table/*/index/*,true,true",
|
||||
"*,false,true",
|
||||
"*,true,false"
|
||||
})
|
||||
void signedSdkUsesTheRegionalTableCollection(String resource, boolean explicitDeny, boolean allowed)
|
||||
throws Exception {
|
||||
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "list-tables-", "list",
|
||||
policy(resource, explicitDeny));
|
||||
DynamoDbClient client = DynamoDbClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(user.accessKeyId(), user.secretAccessKey()))).build()) {
|
||||
if (allowed) {
|
||||
assertEquals(200, client.listTables(request -> request.limit(1)).sdkHttpResponse().statusCode());
|
||||
} else {
|
||||
DynamoDbException denied = assertThrows(DynamoDbException.class,
|
||||
() -> client.listTables(request -> request.limit(1)));
|
||||
assertEquals("AccessDeniedException", denied.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"json,false", "json,true", "cbor,false", "cbor,true"})
|
||||
void ignoredTableSelectorsCannotRedirectCollectionAuthorization(String protocol, boolean explicitDeny)
|
||||
throws Exception {
|
||||
boolean cbor = "cbor".equals(protocol);
|
||||
ObjectMapper mapper = cbor ? new ObjectMapper(new CBORFactory()) : new ObjectMapper();
|
||||
URI uri = cbor ? endpoint.resolve("/service/DynamoDB_20120810/operation/ListTables") : endpoint;
|
||||
String contentType = cbor ? "application/cbor" : "application/x-amz-json-1.0";
|
||||
Map<String, String> headers = cbor ? Map.of("content-type", contentType, "accept", contentType)
|
||||
: Map.of("content-type", contentType, "x-amz-target", "DynamoDB_20120810.ListTables");
|
||||
byte[] body = mapper.writeValueAsBytes(Map.of("Limit", 1, "ExclusiveStartTableName", "page-start",
|
||||
"TableName", "decoy", "ResourceArn", "arn:aws:dynamodb:us-east-1:000000000000:table/decoy"));
|
||||
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "list-selectors-", "list",
|
||||
policy("arn:aws:dynamodb:us-east-1:000000000000:table/decoy", explicitDeny))) {
|
||||
Response response = given().filter(user.signer("dynamodb")).headers(headers).body(body).post(uri);
|
||||
assertEquals(explicitDeny ? 200 : (cbor ? 403 : 400), response.statusCode(), response.asString());
|
||||
if (!explicitDeny) {
|
||||
assertTrue(mapper.readTree(response.asByteArray()).path("__type").asText()
|
||||
.contains("AccessDeniedException"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String policy(String resource, boolean explicitDeny) {
|
||||
String statement = "{\"Effect\":\"%s\",\"Action\":\"dynamodb:ListTables\",\"Resource\":\"%s\"}";
|
||||
return "{\"Version\":\"2012-10-17\",\"Statement\":["
|
||||
+ (explicitDeny ? statement.formatted("Allow", "*") + "," : "")
|
||||
+ statement.formatted(explicitDeny ? "Deny" : "Allow", resource) + "]}";
|
||||
}
|
||||
}
|
||||
@@ -854,6 +854,28 @@ class ResourceArnBuilderTest {
|
||||
|
||||
// ── DynamoDB ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
void listTablesUsesTheCollectionInTheRequestPartitionRegionAndAccount() {
|
||||
for (String body : List.of("{}", "{\"ExclusiveStartTableName\":\"page-start\"}",
|
||||
"{\"TableName\":\"decoy\",\"ResourceArn\":\"decoy\"}")) {
|
||||
setJsonBody(body);
|
||||
assertEquals(List.of("arn:aws-cn:dynamodb:cn-north-1:123456789012:table/*"),
|
||||
builder.buildResources("dynamodb", "dynamodb:ListTables", ctx,
|
||||
"cn-north-1", "123456789012", "aws-cn"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTablesDoesNotChangeOtherTablelessOperations() {
|
||||
setJsonBody("{}");
|
||||
assertEquals(List.of("*"), builder.buildResources("dynamodb", "dynamodb:DescribeLimits",
|
||||
ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
setJsonBody("{\"TableName\":\"Orders\"}");
|
||||
assertEquals(List.of("arn:aws:dynamodb:us-east-1:000000000000:table/Orders"),
|
||||
builder.buildResources("dynamodb", "dynamodb:DescribeTable",
|
||||
ctx, "us-east-1", ACCOUNT, "aws"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dynamoDbBuildsArnFromShortTableName() {
|
||||
setJsonBody("{\"TableName\":\"FgacTable\"}");
|
||||
|
||||
Reference in new issue
Block a user