mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): return modeled authentication error envelopes (#7613)
This commit is contained in:
1 parent
f2debe4ce0
commit
d34c8443cd
11 files changed
+285
-31
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.testutil.AwsRequestSigner;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
final class Ec2ErrorAssertions {
|
||||
|
||||
private Ec2ErrorAssertions() {
|
||||
}
|
||||
|
||||
static void assertGetError(AwsRequestSigner signer, URI endpoint, boolean presigned,
|
||||
int expectedStatus, String expectedCode, String expectedMessage) throws Exception {
|
||||
Map<String, String> query = Map.of("Action", "DescribeVpcs", "Version", "2016-11-15");
|
||||
if (presigned) {
|
||||
given().urlEncodingEnabled(false)
|
||||
.get(signer.presignedUri("GET", endpoint, query, new byte[0])).then()
|
||||
.statusCode(expectedStatus).body("Response.Errors.Error.Code", equalTo(expectedCode))
|
||||
.body("Response.Errors.Error.Message", startsWith(expectedMessage))
|
||||
.body("Response.RequestID", not(emptyOrNullString()));
|
||||
} else {
|
||||
given().filter(signer).queryParams(query).get(endpoint).then()
|
||||
.statusCode(expectedStatus).body("Response.Errors.Error.Code", equalTo(expectedCode))
|
||||
.body("Response.Errors.Error.Message", startsWith(expectedMessage))
|
||||
.body("Response.RequestID", not(emptyOrNullString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testutil.AwsRequestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.http.apache5.Apache5HttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class Ec2IamErrorIntegrationTest {
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void sdkParsesDenialAndAllowsTheSameUserAfterPermissionIsGranted() {
|
||||
String name = "ec2-denial-" + UUID.randomUUID();
|
||||
UserCredentials credentials = user(name);
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(),
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), Region.US_EAST_1, endpoint)) {
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals(403, denied.statusCode());
|
||||
assertEquals("UnauthorizedOperation", denied.awsErrorDetails().errorCode());
|
||||
assertTrue(denied.awsErrorDetails().errorMessage()
|
||||
.startsWith("You are not authorized to perform this operation."));
|
||||
assertNotNull(denied.requestId());
|
||||
EnforcementFixtures.putUserPolicy(endpoint.getPort(), auth("000000000000", "iam"), name,
|
||||
"describe", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"ec2:DescribeVpcs","Resource":"*"}
|
||||
]}
|
||||
""");
|
||||
assertNotNull(client.describeVpcs().vpcs());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void signedAndPresignedGetDenialsUseEc2Envelope(boolean presigned) throws Exception {
|
||||
UserCredentials credentials = user("ec2-get-denial-" + UUID.randomUUID());
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs(
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), "ec2");
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, presigned, 403, "UnauthorizedOperation",
|
||||
"You are not authorized to perform this operation.");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void unknownKeysOnSignedAndPresignedGetUseEc2AuthenticationError(boolean presigned) throws Exception {
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "ec2");
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, presigned, 401, "AuthFailure",
|
||||
"AWS was not able to validate the provided access credentials");
|
||||
}
|
||||
|
||||
private UserCredentials user(String name) {
|
||||
return EnforcementFixtures.createUser(cleanup, endpoint.getPort(), auth("000000000000", "iam"), name);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkParsesAnUnknownAccessKeyWithIamEnforcement() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(),
|
||||
"AKIAUNKNOWNACCESSKEY", "some-secret", Region.US_EAST_1, Apache5HttpClient.builder(), endpoint)) {
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals(401, denied.statusCode());
|
||||
assertEquals("AuthFailure", denied.awsErrorDetails().errorCode());
|
||||
assertEquals("AWS was not able to validate the provided access credentials",
|
||||
denied.awsErrorDetails().errorMessage());
|
||||
assertNotNull(denied.requestId());
|
||||
}
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;
|
||||
import io.github.hectorvent.floci.testutil.AwsRequestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.http.apache5.Apache5HttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ValidateSignaturesProfile.class)
|
||||
class Ec2SignatureErrorIntegrationTest {
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@Test
|
||||
void sdkParsesAnUnknownAccessKeyWithSignatureValidation() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(),
|
||||
"AKIAUNKNOWNACCESSKEY", "some-secret", Region.US_EAST_1, Apache5HttpClient.builder(), endpoint)) {
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals(401, denied.statusCode());
|
||||
assertEquals("AuthFailure", denied.awsErrorDetails().errorCode());
|
||||
assertEquals("AWS was not able to validate the provided access credentials",
|
||||
denied.awsErrorDetails().errorMessage());
|
||||
assertNotNull(denied.requestId());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void unknownKeysOnSignedAndPresignedGetUseEc2Envelope(boolean presigned) throws Exception {
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "ec2");
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, presigned, 401, "AuthFailure",
|
||||
"AWS was not able to validate the provided access credentials");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEc2SigningScopeDoesNotChangeRestJsonErrors() {
|
||||
given().filter(AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "ec2"))
|
||||
.get("/2015-03-31/functions").then()
|
||||
.statusCode(403).contentType("application/json")
|
||||
.body("__type", equalTo("UnrecognizedClientException"));
|
||||
}
|
||||
}
|
||||
@@ -2249,7 +2249,7 @@ class IamEnforcementFilterTest {
|
||||
|
||||
@Test
|
||||
void queryProtocolGetsXmlErrorResponse() {
|
||||
// IAM/STS/EC2/SQS/SNS/RDS/ELBv2/CFN/... — Query protocol, form-encoded body, XML response.
|
||||
// Ordinary Query services retain ErrorResponse; EC2 uses its own envelope.
|
||||
Response r = IamEnforcementFilter.accessDeniedResponse(
|
||||
"iam:ListUsers", "iam", MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
|
||||
@@ -2263,6 +2263,41 @@ class IamEnforcementFilterTest {
|
||||
assertTrue(body.contains("<RequestId>"), body);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"ec2,ec2:DescribeVpcs,UnauthorizedOperation", "iam,iam:ListUsers,AccessDenied",
|
||||
"sts,sts:AssumeRole,AccessDenied", "sns,sns:ListTopics,AuthorizationError"})
|
||||
void queryDenialsUseTheServiceErrorEnvelope(String scope, String action, String code) {
|
||||
Response response = IamEnforcementFilter.accessDeniedResponse(
|
||||
action, scope, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
String body = entityString(response);
|
||||
assertEquals(403, response.getStatus());
|
||||
assertEquals(MediaType.APPLICATION_XML_TYPE, response.getMediaType());
|
||||
assertTrue(body.contains("<Code>" + code + "</Code>"), body);
|
||||
if ("ec2".equals(scope)) {
|
||||
assertTrue(body.contains("<Response><Errors><Error>"), body);
|
||||
assertTrue(body.contains("<Message>You are not authorized to perform this operation."), body);
|
||||
assertTrue(body.contains("<RequestID>"), body);
|
||||
} else {
|
||||
assertTrue(body.contains("<ErrorResponse><Error>"), body);
|
||||
assertTrue(body.contains("<RequestId>"), body);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"ec2,AuthFailure", "iam,InvalidClientTokenId", "sts,InvalidClientTokenId"})
|
||||
void unknownQueryAccessKeysUseTheServiceErrorEnvelope(String scope, String code) {
|
||||
ContainerRequestContext context = mock(ContainerRequestContext.class);
|
||||
when(context.getMediaType()).thenReturn(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
Response response = IamEnforcementFilter.unrecognizedClientResponse(scope, context);
|
||||
String body = entityString(response);
|
||||
assertEquals("ec2".equals(scope) ? 401 : 403, response.getStatus());
|
||||
assertTrue(body.contains("ec2".equals(scope)
|
||||
? "AWS was not able to validate the provided access credentials"
|
||||
: "The security token included in the request is invalid"), body);
|
||||
assertTrue(body.contains("<Code>" + code + "</Code>"), body);
|
||||
assertTrue(body.contains("ec2".equals(scope) ? "<RequestID>" : "<RequestId>"), body);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"REST_XML, application/xml", "REST_JSON, application/json"})
|
||||
void restDenialUsesTheServiceCatalogProtocol(ServiceProtocol serviceProtocol, String expectedContentType) {
|
||||
|
||||
+2
-2
@@ -63,7 +63,7 @@ class Ec2CreateRouteDryRunIntegrationTest {
|
||||
Ec2Exception error = assertThrows(Ec2Exception.class, () -> caller.createRoute(fixture.request()
|
||||
.gatewayId(fixture.gatewayId).dryRun(true).build()));
|
||||
assertEquals(403, error.statusCode());
|
||||
assertTrue(error.awsErrorDetails().rawResponse().asUtf8String().contains("<Code>AccessDenied</Code>"));
|
||||
assertEquals("UnauthorizedOperation", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
assertEquals(before, fixture.routes());
|
||||
}
|
||||
@@ -106,7 +106,7 @@ class Ec2CreateRouteDryRunIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, () -> caller.createRoute(request));
|
||||
assertEquals(403, denied.statusCode());
|
||||
assertTrue(denied.awsErrorDetails().rawResponse().asUtf8String().contains("<Code>AccessDenied</Code>"));
|
||||
assertEquals("UnauthorizedOperation", denied.awsErrorDetails().errorCode());
|
||||
assertEquals(before, fixture.routes());
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -48,7 +48,8 @@ class IamTagKeysEnforcementIntegrationTest {
|
||||
.formParam("Action", "CreateTags").formParam("ResourceId.1", id)
|
||||
.formParam("Tag.1.Key", "Team").formParam("Tag.1.Value", "payments")
|
||||
.formParam("Tag.2.Key", "Admin").formParam("Tag.2.Value", "true")
|
||||
.post("/").then().statusCode(403).body(containsString("AccessDenied"));
|
||||
.post("/").then().statusCode(403)
|
||||
.body("Response.Errors.Error.Code", equalTo("UnauthorizedOperation"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -49,7 +49,8 @@ public final class AwsTestClients {
|
||||
return create(builder, account, "test", region, httpClientBuilder, endpoint);
|
||||
}
|
||||
|
||||
private static <B extends AwsClientBuilder<B, C> & AwsSyncClientBuilder<B, C>, C> C create(
|
||||
/** Explicit credentials and transport for status codes whose bodies URLConnection cannot read. */
|
||||
public static <B extends AwsClientBuilder<B, C> & AwsSyncClientBuilder<B, C>, C> C create(
|
||||
B builder, String accessKey, String secretKey, Region region,
|
||||
SdkHttpClient.Builder<?> httpClientBuilder, URI endpoint) {
|
||||
return builder.endpointOverride(endpoint)
|
||||
|
||||
Reference in new issue
Block a user