mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
fix(apigateway): authorize management calls against the path ARN they name (#6825)
This commit is contained in:
1 parent
c3fdacc3ce
commit
981b885348
8 files changed
+391
-10
No files matched your search
@@ -402,6 +402,7 @@ class IamEnforcementFilterTest {
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
requestContext.setAccountId("000000000000");
|
||||
requestContext.setRegion("us-east-1");
|
||||
requestContext.setPartition("aws");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIAUSER");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(containerRequest.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY))
|
||||
@@ -417,7 +418,7 @@ class IamEnforcementFilterTest {
|
||||
|
||||
verify(actionRegistry).resolveRoute("apigateway", containerRequest);
|
||||
verify(actionRegistry, never()).resolveRoute("iam", containerRequest);
|
||||
verify(arnBuilder).buildResources(eq("apigateway"), any(), eq(containerRequest), eq("us-east-1"), eq("000000000000"), eq("aws"));
|
||||
verify(arnBuilder).buildResources("apigateway", containerRequest, "us-east-1", "000000000000", "aws");
|
||||
verify(conditionContextResolver).resolve("apigateway", "apigateway:POST", containerRequest);
|
||||
}
|
||||
|
||||
@@ -446,6 +447,31 @@ class IamEnforcementFilterTest {
|
||||
verify(arnBuilder).buildResources("scheduler", containerRequest, "xx-local-1", "000000000000", "aws-cn");
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiGatewayResourcesAreBuiltInTheRequestsPartition() {
|
||||
// A Region no partition publishes takes the deployment's partition, the one its path ARNs are in.
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=AKIAUSER/20260924/xx-local-1/apigateway/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
requestContext.setAccountId("000000000000");
|
||||
requestContext.setRegion("xx-local-1");
|
||||
requestContext.setPartition("aws-cn");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIAUSER");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(containerRequest.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY))
|
||||
.thenReturn(ProtocolClaim.rest());
|
||||
when(catalog.byResourceClass(ApiGatewayController.class))
|
||||
.thenReturn(Optional.of(descriptor("apigateway", ServiceProtocol.REST_JSON,
|
||||
Set.of("apigateway", "execute-api"), ApiGatewayController.class)));
|
||||
when(actionRegistry.resolveRoute("apigateway", containerRequest)).thenReturn("apigateway:GET");
|
||||
when(arnBuilder.buildResources(any(), any(), any(), any(), any())).thenReturn(List.of("*"));
|
||||
when(iamService.resolveCallerContext(eq("AKIAUSER"), any())).thenReturn(CallerContext.of(List.of()));
|
||||
|
||||
newFilter(resourceInfo(ApiGatewayController.class)).filter(containerRequest);
|
||||
|
||||
verify(arnBuilder).buildResources("apigateway", containerRequest, "xx-local-1", "000000000000", "aws-cn");
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3RouteDoesNotUseApiGatewayRules() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
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.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* API Gateway authorizes a management call as its HTTP method on the path it names,
|
||||
* {@code arn:<partition>:apigateway:<region>::<path>}, with no account and without the {@code /v2} of an HTTP
|
||||
* API path, so a grant or a deny of {@code apigateway:GET} on a path covers that path's reads and no others.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class ApiGatewayPathArnEnforcementIntegrationTest {
|
||||
|
||||
private static final String ADMIN = "test";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
/** Each user's access key ID, by user name. */
|
||||
private final Map<String, String> keys = new HashMap<>();
|
||||
|
||||
@Test
|
||||
void getRestApisIsCheckedOnTheRestApisPath() {
|
||||
String user = user("apigw-restapis");
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/restapis"));
|
||||
get(user, "/restapis").statusCode(200);
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/restapis/*/stages"));
|
||||
refused(get(user, "/restapis"));
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:eu-west-1::/restapis"));
|
||||
refused(get(user, "/restapis"));
|
||||
|
||||
policy(user, allow("apigateway:*", "*") + ","
|
||||
+ deny("apigateway:GET", "arn:aws:apigateway:us-east-1::/restapis"));
|
||||
refused(get(user, "/restapis"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getStagesIsCheckedOnTheStagesPathOfItsApi() throws Exception {
|
||||
String apiId = createRestApi();
|
||||
String user = user("apigw-stages");
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/restapis/*/stages"));
|
||||
get(user, "/restapis/" + apiId + "/stages").statusCode(200);
|
||||
get(user, "/restapis/" + encodedFirstCharacter(apiId) + "/stages").statusCode(200);
|
||||
get(user, "/restapis/x/%2E%2E/" + apiId + "/stages").statusCode(200);
|
||||
assertReached(getAsWritten(user, "/restapis//" + apiId + "/stages"));
|
||||
assertReached(getAsWritten(user, "/restapis/x/%2E%2E//" + apiId + "/stages"));
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/restapis"));
|
||||
refused(get(user, "/restapis/" + apiId + "/stages"));
|
||||
|
||||
policy(user, allow("apigateway:*", "*") + ","
|
||||
+ deny("apigateway:GET", "arn:aws:apigateway:*::/restapis/" + apiId + "/stages"));
|
||||
refused(get(user, "/restapis/" + apiId + "/stages"));
|
||||
refused(get(user, "/restapis/" + encodedFirstCharacter(apiId) + "/stages"));
|
||||
refused(get(user, "/restapis/x/%2E%2E/" + apiId + "/stages"));
|
||||
assertRefused(getAsWritten(user, "/restapis//" + apiId + "/stages"));
|
||||
assertRefused(getAsWritten(user, "/restapis/x/%2E%2E//" + apiId + "/stages"));
|
||||
get(user, "/restapis").statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getApisIsCheckedOnTheApisPathWithoutItsV2Prefix() {
|
||||
String user = user("apigw-apis");
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/apis"));
|
||||
get(user, "/v2/apis").statusCode(200);
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/v2/apis"));
|
||||
refused(get(user, "/v2/apis"));
|
||||
|
||||
policy(user, allow("apigateway:*", "*") + ","
|
||||
+ deny("apigateway:GET", "arn:aws:apigateway:*::/apis"));
|
||||
refused(get(user, "/v2/apis"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getStageIsCheckedOnTheStageNameAsWritten() {
|
||||
String apiId = createHttpApiWithDefaultStage();
|
||||
String user = user("apigw-default-stage");
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:*::/apis/" + apiId + "/stages/$default"));
|
||||
get(user, "/v2/apis/" + apiId + "/stages/$default").statusCode(200);
|
||||
get(user, "/v2/apis/" + apiId + "/stages/%24default").statusCode(200);
|
||||
|
||||
policy(user, allow("apigateway:*", "*") + ","
|
||||
+ deny("apigateway:GET", "arn:aws:apigateway:*::/apis/" + apiId + "/stages/$default"));
|
||||
refused(get(user, "/v2/apis/" + apiId + "/stages/%24default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTagsIsCheckedOnTheEncodedArnItTagsHoweverItIsSent() {
|
||||
String apiId = createHttpApiWithDefaultStage();
|
||||
String apiArn = "arn:aws:apigateway:us-east-1::/apis/" + apiId;
|
||||
String encoded = "arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Fapis%2F" + apiId;
|
||||
String user = user("apigw-tags");
|
||||
|
||||
policy(user, allow("apigateway:GET", "arn:aws:apigateway:us-east-1::/tags/" + encoded));
|
||||
get(user, "/v2/tags/" + apiArn).statusCode(200);
|
||||
get(user, "/v2/tags/" + encoded).statusCode(200);
|
||||
|
||||
policy(user, allow("apigateway:*", "*") + ","
|
||||
+ deny("apigateway:GET", "arn:aws:apigateway:us-east-1::/tags/" + encoded));
|
||||
refused(get(user, "/v2/tags/" + apiArn));
|
||||
refused(get(user, "/v2/tags/" + encoded));
|
||||
}
|
||||
|
||||
private String createHttpApiWithDefaultStage() {
|
||||
String apiId = given()
|
||||
.header("Authorization", auth(ADMIN, "apigateway"))
|
||||
.contentType("application/json")
|
||||
.body("{\"name\":\"apigw-path-" + suffix() + "\",\"protocolType\":\"HTTP\"}")
|
||||
.when().post("/v2/apis")
|
||||
.then().statusCode(201)
|
||||
.extract().path("apiId");
|
||||
cleanup.register(() -> given().header("Authorization", auth(ADMIN, "apigateway"))
|
||||
.delete("/v2/apis/" + apiId));
|
||||
given()
|
||||
.header("Authorization", auth(ADMIN, "apigateway"))
|
||||
.contentType("application/json")
|
||||
.body("{\"stageName\":\"$default\"}")
|
||||
.when().post("/v2/apis/" + apiId + "/stages")
|
||||
.then().statusCode(201);
|
||||
return apiId;
|
||||
}
|
||||
|
||||
private String createRestApi() {
|
||||
String apiId = given()
|
||||
.header("Authorization", auth(ADMIN, "apigateway"))
|
||||
.contentType("application/json")
|
||||
.body("{\"name\":\"apigw-path-" + suffix() + "\"}")
|
||||
.when().post("/restapis")
|
||||
.then().statusCode(201)
|
||||
.extract().path("id");
|
||||
cleanup.register(() -> given().header("Authorization", auth(ADMIN, "apigateway"))
|
||||
.delete("/restapis/" + apiId));
|
||||
return apiId;
|
||||
}
|
||||
|
||||
private ValidatableResponse get(String user, String path) {
|
||||
return given().urlEncodingEnabled(false)
|
||||
.header("Authorization", auth(keys.get(user), "apigateway")).get(path).then();
|
||||
}
|
||||
|
||||
/** Sends {@code path} with its empty segments, which RestAssured collapses before sending. */
|
||||
private HttpResponse<String> getAsWritten(String user, String path) throws Exception {
|
||||
URI uri = URI.create(baseUri.getScheme() + "://" + baseUri.getRawAuthority() + path);
|
||||
try (HttpClient client = HttpClient.newHttpClient()) {
|
||||
return client.send(HttpRequest.newBuilder(uri)
|
||||
.header("Authorization", auth(keys.get(user), "apigateway")).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertReached(HttpResponse<String> response) {
|
||||
assertEquals(200, response.statusCode(), response.body());
|
||||
assertThat(response.body(), containsString("\"item\""));
|
||||
}
|
||||
|
||||
private static void assertRefused(HttpResponse<String> response) {
|
||||
assertEquals(403, response.statusCode(), response.body());
|
||||
assertThat(response.body(), containsString("AccessDeniedException"));
|
||||
assertThat(response.body(), containsString("apigateway:GET"));
|
||||
}
|
||||
|
||||
/** {@code id} with its first character percent-encoded, a path that routes to the same API. */
|
||||
private static String encodedFirstCharacter(String id) {
|
||||
return "%%%02X".formatted((int) id.charAt(0)) + id.substring(1);
|
||||
}
|
||||
|
||||
private static void refused(ValidatableResponse response) {
|
||||
response.statusCode(403)
|
||||
.body(containsString("AccessDeniedException"))
|
||||
.body(containsString("apigateway:GET"));
|
||||
}
|
||||
|
||||
private static String allow(String action, String resource) {
|
||||
return "{\"Effect\":\"Allow\",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}";
|
||||
}
|
||||
|
||||
private static String deny(String action, String resource) {
|
||||
return "{\"Effect\":\"Deny\",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}";
|
||||
}
|
||||
|
||||
/** Replaces the user's one inline policy. */
|
||||
private void policy(String user, String statements) {
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ADMIN, "iam"), user, "p",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}");
|
||||
}
|
||||
|
||||
private String user(String name) {
|
||||
String userName = name + "-" + suffix();
|
||||
String accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ADMIN, "iam"), userName)
|
||||
.accessKeyId();
|
||||
keys.put(userName, accessKeyId);
|
||||
return userName;
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return Long.toString(System.nanoTime(), 36);
|
||||
}
|
||||
}
|
||||
@@ -1896,6 +1896,70 @@ class ResourceArnBuilderTest {
|
||||
builder.buildResources("dsql", ctx, "polygondwanaland-west-1", "000000000000", "aws-cn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void apiGatewayRoutesResolveThePathTheyNameWithNoAccount() {
|
||||
request("/restapis/abc123/stages");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123/stages"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// Each segment is decoded as the router decodes it, so an encoded id names the API it routes to.
|
||||
request("/restapis/%61bc123/stages");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123/stages"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// Dot segments are resolved after decoding, as the router resolves them.
|
||||
request("/restapis/x/%2E%2E/abc123/stages");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123/stages"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/restapis/%2e/abc123");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// Empty segments are dropped once the dots are resolved, as the router collapses them.
|
||||
request("/restapis//abc123");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/restapis/x/..//abc123");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/abc123"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/restapis/x//../abc123");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis/x/abc123"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/v2/tags/arn:aws:apigateway:us-east-1::/apis/x/%2E%2E/a1b2c3");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Fapis%2Fa1b2c3"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/restapis/");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/restapis"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// An HTTP or WebSocket API path's /v2 is not part of the resource it names.
|
||||
request("/v2/apis");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/apis"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/v2/apis/a1b2c3/stages/prod");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/apis/a1b2c3/stages/prod"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// A stage name keeps the characters it is written with, sent literally or encoded.
|
||||
request("/v2/apis/a1b2c3/stages/$default");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/apis/a1b2c3/stages/$default"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/v2/apis/a1b2c3/stages/%24default");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/apis/a1b2c3/stages/$default"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/v2domains");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/v2domains"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
// A tagging route names the ARN it tags still encoded, as AWS names its Tags resource.
|
||||
request("/v2/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Fapis%2Fa1b2c3");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Fapis%2Fa1b2c3"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/v2/tags/arn:aws:apigateway:us-east-1::/apis/a1b2c3");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Fapis%2Fa1b2c3"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/tags/arn%3aaws%3aapigateway%3aus-east-1%3a%3a%2frestapis%2fabc123");
|
||||
assertEquals(List.of("arn:aws:apigateway:us-east-1::/tags/arn%3Aaws%3Aapigateway%3Aus-east-1%3A%3A%2Frestapis%2Fabc123"),
|
||||
builder.buildResources("apigateway", ctx, "us-east-1", ACCOUNT));
|
||||
request("/restapis");
|
||||
assertEquals(List.of("arn:aws-us-gov:apigateway:us-gov-west-1::/restapis"),
|
||||
builder.buildResources("apigateway", ctx, "us-gov-west-1", ACCOUNT, "aws-us-gov"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void schedulerRoutesResolveTheScheduleInItsGroupTheGroupOrTheTaggedArn() {
|
||||
when(uriInfo.getPath()).thenReturn("/schedules/nightly");
|
||||
|
||||
Reference in new issue
Block a user