mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ec2): use EC2 envelopes for token and signature errors (#7640)
This commit is contained in:
1 parent
07d75c8c8e
commit
81ff58630e
7 files changed
+216
-5
No files matched your search
@@ -22,6 +22,10 @@ import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
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.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -77,6 +81,22 @@ class Ec2IamErrorIntegrationTest {
|
||||
"AWS was not able to validate the provided access credentials");
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingCredentialsUseEc2Envelope() {
|
||||
given().formParam("Action", "DescribeVpcs").formParam("Version", "2016-11-15")
|
||||
.post(endpoint).then().statusCode(403)
|
||||
.body("Response.Errors.Error.Code", equalTo("MissingAuthenticationToken"))
|
||||
.body("Response.RequestID", not(emptyOrNullString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unreadableCredentialUsesEc2Envelope() {
|
||||
given().header("Authorization", "AWS4-HMAC-SHA256 broken")
|
||||
.formParam("Action", "DescribeVpcs").formParam("Version", "2016-11-15")
|
||||
.post(endpoint).then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("IncompleteSignature"));
|
||||
}
|
||||
|
||||
private UserCredentials user(String name) {
|
||||
return EnforcementFixtures.createUser(cleanup, endpoint.getPort(), auth("000000000000", "iam"), name);
|
||||
}
|
||||
|
||||
+100
@@ -1,20 +1,32 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
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 jakarta.inject.Inject;
|
||||
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.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
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.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
@@ -29,6 +41,49 @@ class Ec2SignatureErrorIntegrationTest {
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"matching", "wrong", "missing", "live"})
|
||||
void temporaryCredentialExpiryRequiresTheMatchingToken(String tokenMode) throws Exception {
|
||||
String key = "ASIA" + UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||
cleanup.register(() -> iamService.unregisterSession("000000000000", key));
|
||||
iamService.registerSession(key, "session-secret", "issued-token",
|
||||
"arn:aws:iam::000000000000:role/expired-session", "live".equals(tokenMode)
|
||||
? Instant.now().plusSeconds(3600) : Instant.parse("2020-01-01T00:00:00Z"), null);
|
||||
AwsCredentials credentials = "missing".equals(tokenMode)
|
||||
? AwsBasicCredentials.create(key, "session-secret")
|
||||
: AwsSessionCredentials.create(key, "session-secret",
|
||||
"wrong".equals(tokenMode) ? "wrong-token" : "issued-token");
|
||||
try (Ec2Client client = Ec2Client.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.httpClientBuilder(Apache5HttpClient.builder())
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials)).build()) {
|
||||
if ("live".equals(tokenMode)) {
|
||||
assertNotNull(client.describeVpcs().vpcs());
|
||||
return;
|
||||
}
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals("matching".equals(tokenMode) ? 400 : 401, denied.statusCode());
|
||||
assertEquals("matching".equals(tokenMode) ? "RequestExpired" : "AuthFailure",
|
||||
denied.awsErrorDetails().errorCode());
|
||||
assertNotNull(denied.requestId());
|
||||
}
|
||||
Map<String, String> query = new HashMap<>(Map.of("Action", "DescribeVpcs", "Version", "2016-11-15"));
|
||||
if (!"missing".equals(tokenMode)) {
|
||||
query.put("X-Amz-Security-Token", "wrong".equals(tokenMode) ? "wrong-token" : "issued-token");
|
||||
}
|
||||
URI presigned = AwsRequestSigner.signedAs(key, "session-secret", "ec2")
|
||||
.presignedUri("GET", endpoint, query, new byte[0]);
|
||||
given().urlEncodingEnabled(false).get(presigned).then()
|
||||
.statusCode("matching".equals(tokenMode) ? 400 : 401)
|
||||
.body("Response.Errors.Error.Code",
|
||||
equalTo("matching".equals(tokenMode) ? "RequestExpired" : "AuthFailure"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkParsesAnUnknownAccessKeyWithSignatureValidation() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(),
|
||||
@@ -50,6 +105,51 @@ class Ec2SignatureErrorIntegrationTest {
|
||||
"AWS was not able to validate the provided access credentials");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void badSignaturesOnSignedAndPresignedGetUseEc2Envelope(boolean presigned) throws Exception {
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs("test", "wrong-secret", "ec2");
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, presigned, 403, "SignatureDoesNotMatch",
|
||||
"The request signature we calculated does not match");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {-1800, 1800})
|
||||
void headerTimestampSkewUsesRequestExpired(long offset) throws Exception {
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs("test", "test", "ec2")
|
||||
.signedAt(Instant.now().plusSeconds(offset));
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, false, 400, "RequestExpired",
|
||||
offset < 0 ? "Signature expired:" : "Signature not yet current:");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(longs = {-1800, 1800})
|
||||
void presignedExpiryAndFutureTimestampUseRequestExpired(long offset) throws Exception {
|
||||
AwsRequestSigner signer = AwsRequestSigner.signedAs("test", "test", "ec2")
|
||||
.signedAt(Instant.now().plusSeconds(offset));
|
||||
Ec2ErrorAssertions.assertGetError(signer, endpoint, true, 400, "RequestExpired",
|
||||
offset < 0 ? "Signature expired:" : "Signature not yet current:");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sdkParsesBadSignature() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(),
|
||||
"test", "wrong-secret", Region.US_EAST_1, endpoint)) {
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals(403, denied.statusCode());
|
||||
assertEquals("SignatureDoesNotMatch", denied.awsErrorDetails().errorCode());
|
||||
assertNotNull(denied.requestId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void incompleteSignatureUsesEc2Envelope() {
|
||||
given().header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20261105/us-east-1/ec2/aws4_request")
|
||||
.queryParam("Action", "DescribeVpcs").queryParam("Version", "2016-11-15")
|
||||
.get(endpoint).then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("IncompleteSignature"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEc2SigningScopeDoesNotChangeRestJsonErrors() {
|
||||
given().filter(AwsRequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret", "ec2"))
|
||||
|
||||
@@ -740,6 +740,27 @@ class IamEnforcementFilterTest {
|
||||
entityString(response.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExpiredSessionOnEc2UsesItsOwnEnvelope() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=ASIAEXPIRED/20260629/us-east-1/ec2/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=garbage";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIAEXPIRED");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(containerRequest.getMediaType()).thenReturn(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
when(actionRegistry.resolve("ec2", containerRequest)).thenReturn("ec2:DescribeVpcs");
|
||||
when(iamService.isExpiredSession(eq("ASIAEXPIRED"), any())).thenReturn(true);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
ArgumentCaptor<Response> response = ArgumentCaptor.captor();
|
||||
verify(containerRequest).abortWith(response.capture());
|
||||
assertEquals(400, response.getValue().getStatus());
|
||||
assertTrue(entityString(response.getValue()).contains("<Response><Errors><Error><Code>RequestExpired</Code>"),
|
||||
entityString(response.getValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExpiredSessionOnAnS3RequestIsRejectedWithTheS3ErrorCode() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
|
||||
+22
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
@@ -13,6 +14,9 @@ import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
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.nio.charset.StandardCharsets;
|
||||
@@ -24,6 +28,9 @@ 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.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* With enforcement on, a session past its expiration on the injected clock is answered with AWS's
|
||||
@@ -65,6 +72,21 @@ class ExpiredSessionEnforcementIntegrationTest {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void ec2SdkReadsRequestExpiredWithoutDeletingTheSession() {
|
||||
String key = expiredSession();
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(), key, "secret",
|
||||
Region.US_EAST_1, baseUri)) {
|
||||
for (int attempt = 0; attempt < 2; attempt++) {
|
||||
Ec2Exception denied = assertThrows(Ec2Exception.class, client::describeVpcs);
|
||||
assertEquals(400, denied.statusCode());
|
||||
assertEquals("RequestExpired", denied.awsErrorDetails().errorCode());
|
||||
assertNotNull(denied.requestId());
|
||||
}
|
||||
}
|
||||
getCallerIdentity(key).then().statusCode(403).body(containsString("<Code>ExpiredTokenException</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExpiredSessionIsRefusedAsExpired() {
|
||||
String accessKeyId = expiredSession();
|
||||
|
||||
Reference in new issue
Block a user