mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(core): reject colons in region names (#5705)
This commit is contained in:
1 parent
a522c5a9da
commit
ae3ff3405a
11 files changed
+445
-29
No files matched your search
@@ -82,10 +82,14 @@ A scope region that no partition publishes or admits by its region pattern, such
|
||||
The pattern rule is the AWS SDKs' own, so a region AWS launches after the vendored data was
|
||||
refreshed (`eu-south-9`, say) is still served; it is looser than S3's `LocationConstraint` enum,
|
||||
which stays published-only. Set `FLOCI_PARTITIONS_ALLOW_UNKNOWN_REGIONS=true`
|
||||
(`floci.partitions.allow-unknown-regions`) to serve any label with its own namespace, as Floci
|
||||
did before. Such a label belongs to the deployment's partition. ARNs minted through Floci's
|
||||
shared region resolver use that partition too; services that build their ARNs directly still fall
|
||||
back to `aws` for a label no partition publishes.
|
||||
(`floci.partitions.allow-unknown-regions`) to serve custom labels with their own namespaces.
|
||||
Such a label belongs to the deployment's partition. ARNs minted through Floci's shared region
|
||||
resolver use that partition too; services that build their ARNs directly still fall back to
|
||||
`aws` for a label no partition publishes.
|
||||
|
||||
A region label containing `:` is always refused because the colon separates ARN fields and
|
||||
storage identities. This also applies to `FLOCI_DEFAULT_REGION`; Floci refuses to start with
|
||||
a colon in that setting. Existing stored namespaces are not migrated or deleted by this check.
|
||||
|
||||
## What changes per partition
|
||||
|
||||
|
||||
@@ -45,9 +45,13 @@ public class PartitionStartupValidator {
|
||||
* The partition the deployment serves.
|
||||
*
|
||||
* @throws IllegalStateException when {@code configuredPartition} names no published partition,
|
||||
* or disagrees with a default region the catalog recognises
|
||||
* or disagrees with a default region the catalog recognises,
|
||||
* or the default region contains an ARN field delimiter
|
||||
*/
|
||||
static AwsPartition validate(String defaultRegion, Optional<String> configuredPartition) {
|
||||
if (RegionResolver.containsArnDelimiter(defaultRegion)) {
|
||||
throw new IllegalStateException("floci.default-region must not contain ':': " + defaultRegion);
|
||||
}
|
||||
String effective = RegionResolver.effectivePartition(defaultRegion, configuredPartition);
|
||||
AwsPartition partition = AwsPartitions.find(effective).orElseThrow(() -> new IllegalStateException(
|
||||
"floci.partitions.id names no AWS partition: '" + effective + "'. Published partitions: "
|
||||
|
||||
@@ -77,6 +77,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
}
|
||||
String sessionToken = extractSessionToken(ctx);
|
||||
String auth = ctx.getHeaderString("Authorization");
|
||||
if (rejectAmbiguousRegion(ctx, auth)) {
|
||||
return;
|
||||
}
|
||||
String akid = auth != null && !auth.isEmpty() ? accountResolver.extractAccessKeyId(auth) : null;
|
||||
if (akid != null) {
|
||||
requestContext.setAccountId(resolveAccount(akid, accountResolver.resolve(auth)));
|
||||
@@ -84,7 +87,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setSessionToken(sessionToken);
|
||||
String region = regionResolver.resolveRegionFromAuth(auth);
|
||||
applyRegion(region);
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth));
|
||||
if (rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(auth))) {
|
||||
return;
|
||||
}
|
||||
rejectPartitionAbsentService(ctx, SigV4CredentialScope.serviceName(auth).orElse(null));
|
||||
} else {
|
||||
// A present Authorization header wins, as in IamEnforcementFilter: an unrecognised one
|
||||
@@ -101,7 +106,9 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setSessionToken(sessionToken);
|
||||
String region = regionResolver.resolveRegionFromPresignedCredential(credential);
|
||||
applyRegion(region);
|
||||
rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential));
|
||||
if (rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceNameFromCredential(credential))) {
|
||||
return;
|
||||
}
|
||||
rejectPartitionAbsentService(ctx,
|
||||
SigV4CredentialScope.serviceNameFromCredential(credential).orElse(null));
|
||||
} else {
|
||||
@@ -135,6 +142,25 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
requestContext.setPartition(regionResolver.partitionForRegion(region));
|
||||
}
|
||||
|
||||
/** Refuses delimiters in region interpretations used by request context or downstream dispatch. */
|
||||
private boolean rejectAmbiguousRegion(ContainerRequestContext ctx, String authorization) {
|
||||
String queryCredential = authorization == null || authorization.isBlank()
|
||||
? ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential") : null;
|
||||
String parsedAuthorization = queryCredential == null ? authorization
|
||||
: IamEnforcementFilter.requestAuthorization(null, ctx.getUriInfo().getQueryParameters());
|
||||
// Some Query routes synthesize this header even without X-Amz-Algorithm. Inspect the
|
||||
// same region they use, in addition to the split credential used by request context.
|
||||
String region = regionResolver.resolveRegionFromAuthOrNull(parsedAuthorization);
|
||||
if (!RegionResolver.containsArnDelimiter(region) && queryCredential != null) {
|
||||
region = regionResolver.resolveRegionFromPresignedCredential(queryCredential);
|
||||
}
|
||||
if (!RegionResolver.containsArnDelimiter(region)) {
|
||||
return false;
|
||||
}
|
||||
applyRegion(region);
|
||||
return rejectUnknownRegion(ctx, region, SigV4CredentialScope.serviceName(parsedAuthorization));
|
||||
}
|
||||
|
||||
/**
|
||||
* A scope region no partition publishes or admits by its region pattern is refused, as moto
|
||||
* and LocalStack refuse it by default: on AWS such a label never resolves a host, and served
|
||||
@@ -142,28 +168,45 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
* region and partition stay on the request context so the error mappers see them. S3
|
||||
* clients get S3's own XML {@code AuthorizationHeaderMalformed} (the wording S3 and minio use
|
||||
* for a wrong scope region). Everyone else gets {@code InvalidSignatureException}, as a Query
|
||||
* {@code <ErrorResponse>} for a form-encoded request and as JSON otherwise: this filter runs
|
||||
* before the protocol claim, and a Query SDK cannot parse a JSON error body.
|
||||
* {@code <ErrorResponse>} for a claimed Query or form-encoded request, CBOR for a claimed
|
||||
* CBOR request, and JSON otherwise. The pre-matching protocol claim is already available.
|
||||
*/
|
||||
private void rejectUnknownRegion(ContainerRequestContext ctx, String region, Optional<String> signingName) {
|
||||
if (RegionResolver.isKnownRegion(region) || configProvider.get().partitions().allowUnknownRegions()) {
|
||||
return;
|
||||
private boolean rejectUnknownRegion(ContainerRequestContext ctx, String region, Optional<String> signingName) {
|
||||
boolean ambiguous = RegionResolver.containsArnDelimiter(region);
|
||||
if (!ambiguous && (RegionResolver.isKnownRegion(region)
|
||||
|| configProvider.get().partitions().allowUnknownRegions())) {
|
||||
return false;
|
||||
}
|
||||
LOG.debugv("Refusing request signed for unknown region {0} (service {1}): {2} {3}",
|
||||
region, signingName.orElse("?"), ctx.getMethod(), ctx.getUriInfo().getPath());
|
||||
if (signingName.isPresent() && S3_SIGNING_NAMES.contains(signingName.get())) {
|
||||
ctx.abortWith(PreSignedUrlFilter.errorResponse(400, "AuthorizationHeaderMalformed",
|
||||
"The authorization header is malformed; the region '" + region
|
||||
ambiguous ? "The authorization header is malformed; the region '" + region
|
||||
+ "' must not contain ':'."
|
||||
: "The authorization header is malformed; the region '" + region
|
||||
+ "' is wrong; expecting a region AWS publishes."));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
String message = "Region '" + region + "' is not a region in any AWS partition. Sign the request "
|
||||
String message = ambiguous ? "Region '" + region + "' must not contain ':' because it separates ARN fields."
|
||||
: "Region '" + region + "' is not a region in any AWS partition. Sign the request "
|
||||
+ "for a published region, or set floci.partitions.allow-unknown-regions=true.";
|
||||
if (isFormEncoded(ctx.getMediaType())) {
|
||||
WireProtocol protocol = ctx.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY) instanceof ProtocolClaim claim
|
||||
? claim.protocol() : WireProtocol.REST;
|
||||
if (protocol == WireProtocol.RPCV2_CBOR || protocol == WireProtocol.AWS_CBOR_TARGET) {
|
||||
String contentType = ctx.getHeaderString(AwsCborContentTypeFilter.ORIGINAL_CONTENT_TYPE_HEADER);
|
||||
if (contentType == null) {
|
||||
contentType = ctx.getHeaderString("Content-Type");
|
||||
}
|
||||
ctx.abortWith(CborErrorResponses.of(new AwsException("InvalidSignatureException", message, 400),
|
||||
CborErrorResponses.mediaTypeFor(contentType)));
|
||||
return true;
|
||||
}
|
||||
if (protocol == WireProtocol.AWS_QUERY || isFormEncoded(ctx.getMediaType())) {
|
||||
ctx.abortWith(AwsQueryResponse.error("InvalidSignatureException", message, null, 400));
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
ctx.abortWith(AwsProtocolClaimFilter.errorResponse(400, "InvalidSignatureException", message));
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isFormEncoded(MediaType mediaType) {
|
||||
|
||||
@@ -773,10 +773,6 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
for (Map<String, List<String>> targetContext : targetContexts) {
|
||||
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
|
||||
targetContext, resource, region, accountId, resourceOwnerAccountId);
|
||||
if ("logs".equals(credentialScope)
|
||||
&& ("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action))) {
|
||||
effectiveContext.put("aws:ResourceAccount", List.of(accountId));
|
||||
}
|
||||
if ("dynamodb".equals(credentialScope)) {
|
||||
Map<String, List<String>> batchContext =
|
||||
conditionContextResolver.dynamoDbBatchConditionContext(action, ctx, resource);
|
||||
|
||||
@@ -127,6 +127,11 @@ public class RegionResolver {
|
||||
return AwsPartitions.forRegion(region).isPresent();
|
||||
}
|
||||
|
||||
/** A colon separates ARN fields and cannot be part of a request's region label. */
|
||||
public static boolean containsArnDelimiter(String region) {
|
||||
return region != null && region.indexOf(':') >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the region from an X-Amz-Credential value found in
|
||||
* presigned URL query parameters.
|
||||
|
||||
@@ -3864,13 +3864,16 @@ public class S3Controller {
|
||||
* {@code AuthorizationHeaderMalformed}.
|
||||
*/
|
||||
private void rejectUnknownPostRegion(String credential) {
|
||||
if (credential == null || credential.isEmpty() || config.partitions().allowUnknownRegions()) {
|
||||
if (credential == null || credential.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String region = regionResolver.resolveRegionFromPresignedCredential(credential);
|
||||
if (!RegionResolver.isKnownRegion(region)) {
|
||||
throw new AwsException("AuthorizationHeaderMalformed", "The authorization header is malformed; "
|
||||
+ "the region '" + region + "' is wrong; expecting a region AWS publishes.", 400);
|
||||
boolean ambiguous = RegionResolver.containsArnDelimiter(region);
|
||||
if (ambiguous || (!RegionResolver.isKnownRegion(region) && !config.partitions().allowUnknownRegions())) {
|
||||
String message = ambiguous ? "The authorization header is malformed; the region '" + region
|
||||
+ "' must not contain ':'." : "The authorization header is malformed; "
|
||||
+ "the region '" + region + "' is wrong; expecting a region AWS publishes.";
|
||||
throw new AwsException("AuthorizationHeaderMalformed", message, 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsPartition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -61,4 +62,13 @@ class PartitionStartupValidatorTest {
|
||||
void aBlankPartitionIdMeansUnset() {
|
||||
assertEquals("aws-us-gov", PartitionStartupValidator.validate("us-gov-west-1", Optional.of(" ")).id());
|
||||
}
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"us-east-1:222222222222", "custom::region"})
|
||||
void aDefaultRegionCannotContainAnArnDelimiter(String region) {
|
||||
IllegalStateException error = assertThrows(IllegalStateException.class,
|
||||
() -> PartitionStartupValidator.validate(region, Optional.empty()));
|
||||
assertTrue(error.getMessage().contains("floci.default-region"));
|
||||
assertTrue(error.getMessage().contains(region));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Map;
|
||||
@@ -514,4 +516,32 @@ class AccountContextFilterTest {
|
||||
|
||||
return ctx;
|
||||
}
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void colonRefusalPrecedesStrictPartitionServiceRefusal(boolean query) {
|
||||
allowUnknownRegions = true;
|
||||
strictPartitions = true;
|
||||
RegionResolver gov = new RegionResolver("us-gov-west-1", DEFAULT_ACCOUNT);
|
||||
AccountContextFilter govFilter = new AccountContextFilter(accountResolver, gov, requestContext,
|
||||
key -> Optional.empty(), this::config);
|
||||
String credential = "AKID/20261015/custom:222222222222/cloudfront/aws4_request";
|
||||
ContainerRequestContext ctx = mockContext(query ? null : "AWS4-HMAC-SHA256 Credential=" + credential,
|
||||
query ? credential : null);
|
||||
govFilter.filter(ctx);
|
||||
ArgumentCaptor<Response> error = ArgumentCaptor.forClass(Response.class);
|
||||
verify(ctx).abortWith(error.capture());
|
||||
assertEquals(400, error.getValue().getStatus());
|
||||
assertEquals("InvalidSignatureException", error.getValue().getHeaderString("X-Amzn-Errortype"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"AKID:alias/20261015/us-east-1/sqs/aws4_request",
|
||||
"AKID/20261015/us-east-1/custom:service/aws4_request"})
|
||||
void aColonOutsideTheRegionDoesNotChangeAdmission(String credential) {
|
||||
ContainerRequestContext ctx = mockContext("AWS4-HMAC-SHA256 Credential=" + credential, null);
|
||||
filter.filter(ctx);
|
||||
verify(ctx, never()).abortWith(any());
|
||||
assertEquals(DEFAULT_REGION, requestContext.getRegion());
|
||||
}
|
||||
|
||||
}
|
||||
+321
@@ -0,0 +1,321 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsService;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.PartitionMatrix;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
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.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(PartitionUnknownRegionAllowedIntegrationTest.AllowUnknownRegionsProfile.class)
|
||||
class ColonRegionRejectionIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String REGION = "custom:222222222222";
|
||||
private static final String DEFAULT_REGION = "us-east-1";
|
||||
|
||||
@Inject
|
||||
CloudWatchLogsService logs;
|
||||
|
||||
@Inject
|
||||
SqsService sqs;
|
||||
|
||||
@Inject
|
||||
CloudWatchMetricsService metrics;
|
||||
|
||||
@Inject
|
||||
S3Service s3;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@BeforeAll
|
||||
static void contentTypes() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void jsonCredentialsCannotCreateAColonRegionGroup(boolean query) {
|
||||
String name = "colon-group-" + UUID.randomUUID();
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, REGION, () -> logs.describeLogGroups(name, REGION)
|
||||
.forEach(group -> logs.deleteLogGroup(group.getLogGroupName(), REGION))));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, REGION, () -> logs.describeLogGroups(name, REGION)).isEmpty());
|
||||
request("logs", query).contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "Logs_20140328.CreateLogGroup").body(Map.of("logGroupName", name))
|
||||
.when().post("/").then().statusCode(400).body("__type", equalTo("InvalidSignatureException"));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, REGION, () -> logs.describeLogGroups(name, REGION)).isEmpty());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void queryProtocolCannotAllocateAColonRegionQueue(boolean query) {
|
||||
String name = "colon-queue-" + UUID.randomUUID();
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> sqs.listQueues(name, region)
|
||||
.forEach(queue -> sqs.deleteQueue(queue.getQueueUrl(), region))));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> sqs.listQueues(name, region)).isEmpty());
|
||||
}
|
||||
request("sqs", query).contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateQueue").formParam("QueueName", name)
|
||||
.when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidSignatureException"));
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> sqs.listQueues(name, region)).isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3HeaderCredentialsCannotWriteAnObject() {
|
||||
String bucket = bucket();
|
||||
request("s3", false).body("rejected").when().put("/" + bucket + "/new").then().statusCode(400)
|
||||
.body("Error.Code", equalTo("AuthorizationHeaderMalformed"));
|
||||
assertObjectsUnchanged(bucket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3QueryCredentialsCannotWriteAnObject() {
|
||||
String bucket = bucket();
|
||||
request("s3", true, DEFAULT_REGION).body("control").when().put("/" + bucket + "/control")
|
||||
.then().statusCode(200);
|
||||
given().header("Authorization", PartitionMatrix.sigV4Auth(DEFAULT_REGION, "s3"))
|
||||
.when().get("/" + bucket + "/control").then().statusCode(200).body(equalTo("control"));
|
||||
request("s3", true).body("rejected").when().put("/" + bucket + "/new").then().statusCode(400)
|
||||
.body("Error.Code", equalTo("AuthorizationHeaderMalformed"));
|
||||
assertObjectsUnchanged(bucket);
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipartCredentialsCannotWriteAnObject() {
|
||||
String bucket = bucket();
|
||||
given().multiPart("key", "new")
|
||||
.multiPart("x-amz-credential", "AKID/20261015/" + REGION + "/s3/aws4_request")
|
||||
.multiPart("file", "new", "rejected".getBytes(StandardCharsets.UTF_8), "text/plain")
|
||||
.when().post("/" + bucket).then().statusCode(400)
|
||||
.body("Error.Code", equalTo("AuthorizationHeaderMalformed"));
|
||||
assertObjectsUnchanged(bucket);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void rejectedDeleteKeepsHistoricalColonRegionGroups(boolean query) {
|
||||
String name = "colon-history-" + UUID.randomUUID();
|
||||
logs.createLogGroupForAccount(ACCOUNT, name, null, Map.of("retained", "true"), REGION);
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, REGION, () -> logs.describeLogGroups(name, REGION)
|
||||
.forEach(group -> logs.deleteLogGroup(group.getLogGroupName(), REGION))));
|
||||
assertEquals(1, RequestScopes.callAs(ACCOUNT, REGION, () -> logs.describeLogGroups(name, REGION)).size());
|
||||
request("logs", query).contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "Logs_20140328.DeleteLogGroup").body(Map.of("logGroupName", name))
|
||||
.when().post("/").then().statusCode(400).body("__type", equalTo("InvalidSignatureException"));
|
||||
assertEquals(Map.of("retained", "true"), RequestScopes.callAs(ACCOUNT, REGION,
|
||||
() -> logs.describeLogGroups(name, REGION).getFirst().getTags()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnrecognizedSchemeCannotSmuggleAParsedColonScope() {
|
||||
String name = "colon-unrecognized-" + UUID.randomUUID();
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> sqs.listQueues(name, region)
|
||||
.forEach(queue -> sqs.deleteQueue(queue.getQueueUrl(), region))));
|
||||
}
|
||||
given().header("Authorization", "Bearer Credential=AKID/20261015/" + REGION + "/sqs/aws4_request")
|
||||
.contentType("application/x-www-form-urlencoded").formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", name).when().post("/").then().statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidSignatureException"));
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> sqs.listQueues(name, region)).isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCloudWatchGetCannotUseAColonCredentialWithoutAlgorithm() {
|
||||
assertCloudWatchGetRefused("AKID/20261015/" + REGION + "/monitoring/aws4_request");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNestedDateCannotHideTheRegionParsedByQueryDispatch() {
|
||||
String credential = "AKID/20261015/" + DEFAULT_REGION + "/20261015/" + REGION + "/monitoring/aws4_request";
|
||||
RegionResolver resolver = new RegionResolver(DEFAULT_REGION, ACCOUNT);
|
||||
assertEquals(DEFAULT_REGION, resolver.resolveRegionFromPresignedCredential(credential));
|
||||
assertEquals(REGION, resolver.resolveRegionFromAuth("Credential=" + credential));
|
||||
assertCloudWatchGetRefused(credential);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void colonRefusalsRetainTheCborWireFormat(boolean rpcV2) throws Exception {
|
||||
String name = "colon-cbor-alarm-" + UUID.randomUUID();
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> metrics.deleteAlarms(List.of(name), region)));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> metrics.describeAlarms(List.of(name), null, region))
|
||||
.isEmpty());
|
||||
}
|
||||
ObjectMapper cbor = new ObjectMapper(new CBORFactory());
|
||||
String mediaType = rpcV2 ? "application/cbor" : "application/x-amz-cbor-1.1";
|
||||
RequestSpecification request = given().contentType(mediaType).accept(mediaType)
|
||||
.header("Authorization", PartitionMatrix.sigV4Auth(REGION, "monitoring"))
|
||||
.body(cbor.writeValueAsBytes(Map.of("AlarmName", name, "MetricName", "metric", "Namespace", "colon",
|
||||
"ComparisonOperator", "GreaterThanThreshold", "EvaluationPeriods", 1,
|
||||
"Period", 60, "Statistic", "Average", "Threshold", 1)));
|
||||
if (rpcV2) {
|
||||
request.header("smithy-protocol", "rpc-v2-cbor");
|
||||
} else {
|
||||
request.header("X-Amz-Target", "GraniteServiceVersion20100801.PutMetricAlarm");
|
||||
}
|
||||
byte[] response = request.when().post(rpcV2
|
||||
? "/service/GraniteServiceVersion20100801/operation/PutMetricAlarm" : "/").then().statusCode(400)
|
||||
.contentType(mediaType).header("x-amzn-query-error", equalTo("InvalidSignatureException;Sender"))
|
||||
.extract().asByteArray();
|
||||
JsonNode error = cbor.readTree(response);
|
||||
assertEquals("InvalidSignatureException", error.path("__type").asText());
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> metrics.describeAlarms(List.of(name), null, region))
|
||||
.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertCloudWatchGetRefused(String credential) {
|
||||
String name = "colon-alarm-" + UUID.randomUUID();
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> metrics.deleteAlarms(List.of(name), region)));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> metrics.describeAlarms(List.of(name), null, region))
|
||||
.isEmpty());
|
||||
}
|
||||
metricRequest(name, credential).when().get("/").then().statusCode(400)
|
||||
.contentType("application/xml")
|
||||
.body("ErrorResponse.Error.Code", equalTo("InvalidSignatureException"));
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, region, () -> metrics.describeAlarms(List.of(name), null, region))
|
||||
.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"logs,header-wins", "sqs,header-wins", "logs,colon-header", "sqs,colon-header",
|
||||
"logs,blank-header", "sqs,blank-header"})
|
||||
void headerPrecedenceStillSelectsWhichRegionIsChecked(String service, String mode) {
|
||||
String name = "colon-precedence-" + UUID.randomUUID();
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> {
|
||||
if (service.equals("logs")) {
|
||||
logs.describeLogGroups(name, region).forEach(group -> logs.deleteLogGroup(group.getLogGroupName(), region));
|
||||
} else {
|
||||
sqs.listQueues(name, region).forEach(queue -> sqs.deleteQueue(queue.getQueueUrl(), region));
|
||||
}
|
||||
}));
|
||||
}
|
||||
boolean allowed = mode.equals("header-wins");
|
||||
String queryRegion = mode.equals("colon-header") ? DEFAULT_REGION : REGION;
|
||||
RequestSpecification request = request(service, true, queryRegion).header("Authorization",
|
||||
mode.equals("blank-header") ? "" : PartitionMatrix.sigV4Auth(allowed ? DEFAULT_REGION : REGION, service));
|
||||
if (service.equals("logs")) {
|
||||
request.contentType("application/x-amz-json-1.1").header("X-Amz-Target", "Logs_20140328.CreateLogGroup")
|
||||
.body(Map.of("logGroupName", name));
|
||||
} else {
|
||||
request.contentType("application/x-www-form-urlencoded").formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", name);
|
||||
}
|
||||
request.when().post("/").then().statusCode(allowed ? 200 : 400);
|
||||
for (String region : List.of(REGION, DEFAULT_REGION)) {
|
||||
int expected = allowed && region.equals(DEFAULT_REGION) ? 1 : 0;
|
||||
int actual = RequestScopes.callAs(ACCOUNT, region, () -> service.equals("logs")
|
||||
? logs.describeLogGroups(name, region).size() : sqs.listQueues(name, region).size());
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonColonNestedCredentialsRetainTheirExistingAdmission() {
|
||||
String region = "custom-nested-region";
|
||||
String name = "noncolon-alarm-" + UUID.randomUUID();
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, region, () -> metrics.deleteAlarms(List.of(name), region)));
|
||||
metricRequest(name, "AKID/20261015/" + DEFAULT_REGION + "/20261015/" + region + "/monitoring/aws4_request")
|
||||
.when().get("/").then().statusCode(200);
|
||||
assertEquals(1, RequestScopes.callAs(ACCOUNT, region,
|
||||
() -> metrics.describeAlarms(List.of(name), null, region)).size());
|
||||
}
|
||||
|
||||
private String bucket() {
|
||||
String name = "colon-bucket-" + UUID.randomUUID();
|
||||
RequestScopes.runAs(ACCOUNT, DEFAULT_REGION, () -> {
|
||||
s3.createBucket(name, DEFAULT_REGION);
|
||||
s3.putObject(name, "retained", "kept".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of());
|
||||
});
|
||||
cleanup.register(() -> RequestScopes.runAs(ACCOUNT, DEFAULT_REGION, () -> {
|
||||
s3.deleteObject(name, "new");
|
||||
s3.deleteObject(name, "control");
|
||||
s3.deleteObject(name, "retained");
|
||||
s3.deleteBucket(name);
|
||||
}));
|
||||
given().header("Authorization", PartitionMatrix.sigV4Auth(DEFAULT_REGION, "s3"))
|
||||
.when().get("/" + name + "/retained").then().statusCode(200).body(equalTo("kept"));
|
||||
return name;
|
||||
}
|
||||
|
||||
private void assertObjectsUnchanged(String bucket) {
|
||||
RequestScopes.runAs(ACCOUNT, DEFAULT_REGION, () -> {
|
||||
AwsException missing = assertThrows(AwsException.class, () -> s3.getObject(bucket, "new"));
|
||||
assertEquals("NoSuchKey", missing.getErrorCode());
|
||||
});
|
||||
given().header("Authorization", PartitionMatrix.sigV4Auth(DEFAULT_REGION, "s3"))
|
||||
.when().get("/" + bucket + "/retained").then().statusCode(200).body(equalTo("kept"));
|
||||
}
|
||||
|
||||
private static RequestSpecification metricRequest(String name, String credential) {
|
||||
return given().queryParam("Action", "PutMetricAlarm").queryParam("Version", "2010-08-01")
|
||||
.queryParam("X-Amz-Credential", credential)
|
||||
.queryParam("AlarmName", name).queryParam("ComparisonOperator", "GreaterThanThreshold")
|
||||
.queryParam("EvaluationPeriods", "1").queryParam("MetricName", "QueueDepth")
|
||||
.queryParam("Namespace", "ColonScope").queryParam("Threshold", "10")
|
||||
.queryParam("Period", "60").queryParam("Statistic", "Average");
|
||||
}
|
||||
|
||||
private static RequestSpecification request(String service, boolean query) {
|
||||
return request(service, query, REGION);
|
||||
}
|
||||
|
||||
private static RequestSpecification request(String service, boolean query, String region) {
|
||||
RequestSpecification request = given();
|
||||
if (query) {
|
||||
String date = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC)
|
||||
.format(Instant.now());
|
||||
request.queryParam("X-Amz-Algorithm", "AWS4-HMAC-SHA256")
|
||||
.queryParam("X-Amz-Credential", "AKID/" + date.substring(0, 8) + "/" + region
|
||||
+ "/" + service + "/aws4_request")
|
||||
.queryParam("X-Amz-Date", date).queryParam("X-Amz-Expires", "3600")
|
||||
.queryParam("X-Amz-SignedHeaders", "host").queryParam("X-Amz-Signature", "abc");
|
||||
} else {
|
||||
request.header("Authorization", PartitionMatrix.sigV4Auth(region, service));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -14,7 +14,7 @@ import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
/** {@code floci.partitions.allow-unknown-regions=true}: the old behaviour, any label is a region. */
|
||||
/** {@code floci.partitions.allow-unknown-regions=true}: custom colon-free labels have their own region. */
|
||||
@QuarkusTest
|
||||
@TestProfile(PartitionUnknownRegionAllowedIntegrationTest.AllowUnknownRegionsProfile.class)
|
||||
class PartitionUnknownRegionAllowedIntegrationTest {
|
||||
|
||||
+3
-3
@@ -97,7 +97,7 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
|
||||
Map.of("logGroupName", group, "logStreamName", "s"));
|
||||
assertAll("preserve stored identity across an opted-in region delimiter",
|
||||
() -> response.then().statusCode(400).body(containsString(
|
||||
conditional ? "AccessDeniedException" : "ResourceNotFoundException")),
|
||||
"InvalidSignatureException")),
|
||||
() -> assertPresent(port, REGION, historical, historicalStream, delete),
|
||||
() -> {
|
||||
if (delete) {
|
||||
@@ -119,7 +119,7 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
|
||||
void resourceAccountUsesStorageOwnerInOptedInRegion(String action, String effect, boolean actualOwner) {
|
||||
int port = endpoint.getPort();
|
||||
String otherAccount = "222222222222";
|
||||
String region = "custom:" + otherAccount;
|
||||
String region = "custom-" + otherAccount;
|
||||
String group = "logs-resource-owner-" + UUID.randomUUID();
|
||||
cleanup.register(() -> call(port, ACCOUNT, region, "DeleteLogGroup", Map.of("logGroupName", group))
|
||||
.then().statusCode(200));
|
||||
@@ -145,7 +145,7 @@ class LogsUnknownRegionAuthorizationIntegrationTest {
|
||||
.when().post("/").then().statusCode(200);
|
||||
boolean allowed = effect.equals("Allow") == actualOwner;
|
||||
Response response = call(port, caller, region, action, Map.of("logGroupName", group, "logStreamName", "s"));
|
||||
assertAll("use the canonical account independently of region punctuation",
|
||||
assertAll("use the owning account in an admitted custom region",
|
||||
() -> response.then().statusCode(allowed ? 200 : 400),
|
||||
() -> assertPresent(port, region, group, allowed != delete),
|
||||
() -> {
|
||||
|
||||
Reference in new issue
Block a user