mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(s3): sign generated presigned URLs with SigV4 (#4373)
* fix(s3): align generated presigned URLs with SigV4 and request region * fix(s3): refresh presign credentials after storage reset * fix(s3): retire expired generated presign sessions * fix(iam): sweep expired sessions by stored key * fix(s3): scope generated presign credentials * fix(s3): authorize conditional generated URLs and slash keys * test(s3): preserve signed URL semantics in tagging and SES regressions * fix(s3): reject tag injection on scoped generated PUT URLs * style(s3): alphabetize presigner Java imports * fix(s3): deny scoped requests without a raw HTTP path * fix(s3): authorize proxied presigned URLs by signed path * fix(s3): use signed path for proxied object requests * style(s3): order presigned URL test imports
This commit is contained in:
1 parent
5ec18c507c
commit
864f79cf31
20 files changed
+1232
-104
No files matched your search
@@ -426,7 +426,7 @@ Suppressed recipients are filtered out of the SMTP relay step (non-suppressed re
|
||||
|
||||
`BatchGetMetricData` reads those same stored timelines, so it and `GetMessageInsights` cannot disagree, and it shares the VDM gate. Counts are per recipient, in whole UTC days, and sparse: a day with no data is absent, not zero. `OPEN`, `CLICK` and their delivery variants stay empty because they need a real mailbox, and `TRANSIENT_BOUNCE` because every bounce Floci derives is permanent. Dimensions filter the send: `EMAIL_IDENTITY` on the address or its domain, `CONFIGURATION_SET` on the set it resolved to, `TENANT_NAME` on the tenant it named, and `ISP` only ever on `UNKNOWN_ISP`. Each value is validated with AWS's own rule and message: the configuration set, the tenant and the ISP take ASCII letters, digits, `_` and `-`, and an identity is a bare name or `local@domain`. Where AWS's validation order could not be observed, Floci ranks the member constraints below a duplicate `Id` and the interval cap below the start-before-end check, an absent date fails with a null-constraint message, and a query with two invalid dimension values reports the first in `EMAIL_IDENTITY`, `ISP`, `CONFIGURATION_SET`, `TENANT_NAME` order.
|
||||
|
||||
Export jobs render those same timelines as CSV or JSON. Floci writes the file into its own S3 emulation, under a bucket named `floci-<account>-<region>-ses-export-files`, so the presigned URL on a finished job resolves against Floci. The metrics export emits one row per dimension combination Floci has seen, where real SES emits its whole ISP catalogue with zeros, and a `RATE` is the metric's volume over the send volume for that combination. `LastEngagementEvent` filters select nothing, since Floci derives no opens or clicks. An export finishes in milliseconds, so `CancelExportJob` rarely finds a running job to catch. With S3 authentication enforcement on, the `S3Url` of a finished job answers 403: the shared presigner signs with Floci's own HMAC, which S3 then verifies as SigV4.
|
||||
Export jobs render those same timelines as CSV or JSON. Floci writes the file into its own S3 emulation, under a bucket named `floci-<account>-<region>-ses-export-files`, so the presigned URL on a finished job resolves against Floci. The metrics export emits one row per dimension combination Floci has seen, where real SES emits its whole ISP catalogue with zeros, and a `RATE` is the metric's volume over the send volume for that combination. `LastEngagementEvent` filters select nothing, since Floci derives no opens or clicks. An export finishes in milliseconds, so `CancelExportJob` rarely finds a running job to catch.
|
||||
|
||||
Import jobs read their source from Floci's own S3 emulation, so the object has to be uploaded there rather than to real S3; the CSV and newline-delimited JSON record formats are AWS's own ([suppression list](https://docs.aws.amazon.com/ses/latest/dg/sending-email-suppression-list.html#sending-email-suppression-list-manual-add-bulk), [contact list](https://docs.aws.amazon.com/ses/latest/dg/sending-email-list-management.html#configuring-list-management-bulk-import)). A job still running when the emulator restarts is marked `FAILED` on the next start. Not emulated: `FailureInfo.FailedRecordsS3Url` (Floci writes no failed-record file), the per-file record limits, the same-region bucket rule, and `ListImportJobs` pagination, which returns every job in one page.
|
||||
|
||||
|
||||
@@ -8,11 +8,14 @@ import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourceAccountRelationship;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.ResourcePolicyDecision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.IamService.PresignedScope;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider;
|
||||
import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -25,6 +28,8 @@ import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -274,7 +279,11 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
String credentialScope = resolvedAuthorization.credentialScope();
|
||||
String action = resolvedAuthorization.action();
|
||||
if (action == null) {
|
||||
return; // unknown action → ALLOW (permissive)
|
||||
if (!"OPTIONS".equalsIgnoreCase(ctx.getMethod())
|
||||
&& iamService.presignedScope(akid).isPresent()) {
|
||||
ctx.abortWith(accessDeniedResponse("Unknown", credentialScope, ctx.getMediaType()));
|
||||
}
|
||||
return; // unknown action → ALLOW for ordinary credentials (permissive)
|
||||
}
|
||||
if ("sts:GetCallerIdentity".equals(action)) {
|
||||
return; // AWS returns caller identity even when an identity policy explicitly denies it
|
||||
@@ -328,6 +337,49 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
List<String> resources = resolveResourceArns(credentialScope,
|
||||
arnBuilder.buildResources(credentialScope, ctx, region, accountId));
|
||||
|
||||
Optional<PresignedScope> presignedScope = iamService.presignedScope(akid);
|
||||
if (presignedScope.isPresent()) {
|
||||
PresignedScope scope = presignedScope.get();
|
||||
if ("s3".equals(credentialScope)) {
|
||||
// UriInfo has normalized consecutive slashes. S3 and the SigV4 verifier use
|
||||
// the wire path, where /bucket//key names the distinct object /key.
|
||||
RoutingContext routingContext = currentVertxRequest.getCurrent();
|
||||
HttpServerRequest request = routingContext == null ? null : routingContext.request();
|
||||
// Use the wire path that S3's SigV4 verifier signs. uri() may instead be an
|
||||
// absolute-form request target forwarded by a proxy.
|
||||
String rawPath = request == null ? null : request.path();
|
||||
if (rawPath == null || !rawPath.startsWith("/")) {
|
||||
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType()));
|
||||
return;
|
||||
}
|
||||
String decodedPath;
|
||||
try {
|
||||
decodedPath = URLDecoder.decode(rawPath.replace("+", "%2B"), StandardCharsets.UTF_8);
|
||||
} catch (IllegalArgumentException invalidEncoding) {
|
||||
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType()));
|
||||
return;
|
||||
}
|
||||
resources = List.of(AwsArnUtils.Arn.global(AwsRegions.partitionFor(region),
|
||||
"s3", "", decodedPath.substring(1)).toString());
|
||||
}
|
||||
// IAM's resource glob treats * and ? in object keys as patterns. An internal URL
|
||||
// credential is narrower: it may authorize only its literal object and action.
|
||||
if (!scope.action().equals(action) || resources.isEmpty()
|
||||
|| resources.stream().anyMatch(resource -> !scope.resourceArn().equals(resource))) {
|
||||
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType()));
|
||||
return;
|
||||
}
|
||||
// A tagged PutObject additionally requires s3:PutObjectTagging. Generated URLs
|
||||
// grant only their object operation (and GetObject for conditional writes), so
|
||||
// an unsigned tagging header must not add permissions to the scoped session.
|
||||
if ("s3:PutObject".equals(action)
|
||||
&& (ctx.getHeaderString("x-amz-tagging") != null
|
||||
|| ctx.getUriInfo().getQueryParameters().containsKey("x-amz-tagging"))) {
|
||||
ctx.abortWith(accessDeniedResponse("s3:PutObjectTagging", credentialScope, ctx.getMediaType()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, List<String>> conditionContext = conditionContextResolver.resolve(credentialScope, action, ctx);
|
||||
// A request naming several resources is authorized once per resource, as on AWS, so a
|
||||
// permitted first target cannot carry later targets that the policy does not allow.
|
||||
|
||||
@@ -154,6 +154,19 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
return delegate.scan(k -> true);
|
||||
}
|
||||
|
||||
/** Removes matching values using their actual stored keys, including unprefixed legacy keys. */
|
||||
public int deleteAllAccountsMatching(Predicate<V> valueFilter) {
|
||||
int removed = 0;
|
||||
for (String rawKey : new ArrayList<>(delegate.keys())) {
|
||||
if (delegate.get(rawKey).filter(valueFilter).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
delegate.delete(rawKey);
|
||||
removed++;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns every stored entry with its raw delegate key intact.
|
||||
*
|
||||
|
||||
@@ -70,6 +70,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
private static final Logger LOG = Logger.getLogger(IamService.class);
|
||||
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final String TEMPORARY_ACCESS_KEY_PREFIX = "ASIA";
|
||||
private static final String SCOPED_IDENTITY_SESSION_BASE_POLICY =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}";
|
||||
private static final String DEFAULT_DEPLOYER_USER = "floci-deployer";
|
||||
private static final String DEFAULT_DEPLOYER_ACCESS_KEY_ID = "floci";
|
||||
private static final String DEFAULT_DEPLOYER_SECRET_ACCESS_KEY = "floci";
|
||||
@@ -2279,6 +2281,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
SessionCredential session = new SessionCredential(
|
||||
sessionAccessKeyId, secretAccessKey, sessionToken, roleArn, expiration, sessionPolicyDocument,
|
||||
accountId);
|
||||
putSessionForAccount(accountId, sessionAccessKeyId, session);
|
||||
}
|
||||
|
||||
/** An internal URL credential with both a session policy and an exact action/resource guard. */
|
||||
public void registerPresignedUrlSession(String accountId, String accessKeyId, String secretAccessKey,
|
||||
String sessionToken, Instant expiration, String policyDocument,
|
||||
String action, String resourceArn) {
|
||||
if (accountId == null || accountId.isBlank()) {
|
||||
throw new IllegalArgumentException("Session account ID must not be blank");
|
||||
}
|
||||
SessionCredential session = new SessionCredential(
|
||||
accessKeyId, secretAccessKey, sessionToken, null, expiration, policyDocument, accountId);
|
||||
session.setPresignedAction(action);
|
||||
session.setPresignedResourceArn(resourceArn);
|
||||
putSessionForAccount(accountId, accessKeyId, session);
|
||||
}
|
||||
|
||||
private void putSessionForAccount(String accountId, String sessionAccessKeyId, SessionCredential session) {
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.putForAccount(accountId, sessionAccessKeyId, session);
|
||||
} else {
|
||||
@@ -2286,6 +2306,17 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public record PresignedScope(String action, String resourceArn) {
|
||||
}
|
||||
|
||||
public Optional<PresignedScope> presignedScope(String accessKeyId) {
|
||||
return currentSession(accessKeyId)
|
||||
.filter(session -> session.getPresignedAction() != null
|
||||
&& session.getPresignedResourceArn() != null)
|
||||
.map(session -> new PresignedScope(
|
||||
session.getPresignedAction(), session.getPresignedResourceArn()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores a non-expiring session for a Lambda execution role under the function's account.
|
||||
* Lambda launches can happen outside request scope, so the account namespace must be explicit.
|
||||
@@ -2400,6 +2431,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return removed;
|
||||
}
|
||||
|
||||
/** Removes expired temporary sessions, including those left in persistent storage after a restart. */
|
||||
public int sweepExpiredSessions(Instant now) {
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
return aware.deleteAllAccountsMatching(session ->
|
||||
session.getExpiration() != null && !session.getExpiration().isAfter(now));
|
||||
}
|
||||
List<SessionCredential> storedSessions = sessions.scan(key -> true);
|
||||
int removed = 0;
|
||||
for (SessionCredential session : storedSessions) {
|
||||
if (session.getExpiration() == null || session.getExpiration().isAfter(now)) {
|
||||
continue;
|
||||
}
|
||||
deleteSession(session.getAccessKeyId(), session);
|
||||
removed++;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the account an IAM or temporary access key belongs to. Long-term IAM access keys
|
||||
* resolve from their owning account namespace. Temporary credentials resolve from the account
|
||||
@@ -2474,7 +2523,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
|
||||
if (session.getRoleArn() == null) {
|
||||
return null; // identity session without mapped caller context — preserve historical bypass
|
||||
// A locally minted identity session can carry a restrictive session policy.
|
||||
// Unscoped GetSessionToken credentials retain the historical bypass.
|
||||
return session.getPresignedAction() == null || session.getSessionPolicyDocument() == null ? null
|
||||
: new CallerContext(List.of(SCOPED_IDENTITY_SESSION_BASE_POLICY),
|
||||
session.getSessionPolicyDocument(), null);
|
||||
}
|
||||
List<String> identityPolicies = collectRolePolicies(session.getRoleArn());
|
||||
String boundaryDoc = resolveRoleBoundaryDocument(session.getRoleArn());
|
||||
|
||||
@@ -29,6 +29,9 @@ public class SessionCredential {
|
||||
private String ec2RoleId;
|
||||
/** Exact ECS task ARN this session was minted for, when this is a task-role session. */
|
||||
private String ecsTaskArn;
|
||||
/** Exact object action and ARN for an internally generated S3 presigned URL. */
|
||||
private String presignedAction;
|
||||
private String presignedResourceArn;
|
||||
|
||||
public SessionCredential() {}
|
||||
|
||||
@@ -112,6 +115,12 @@ public class SessionCredential {
|
||||
public String getEcsTaskArn() { return ecsTaskArn; }
|
||||
public void setEcsTaskArn(String ecsTaskArn) { this.ecsTaskArn = ecsTaskArn; }
|
||||
|
||||
public String getPresignedAction() { return presignedAction; }
|
||||
public void setPresignedAction(String presignedAction) { this.presignedAction = presignedAction; }
|
||||
|
||||
public String getPresignedResourceArn() { return presignedResourceArn; }
|
||||
public void setPresignedResourceArn(String presignedResourceArn) { this.presignedResourceArn = presignedResourceArn; }
|
||||
|
||||
public boolean isLambdaExecutionRole() { return lambdaExecutionRole; }
|
||||
public void setLambdaExecutionRole(boolean lambdaExecutionRole) { this.lambdaExecutionRole = lambdaExecutionRole; }
|
||||
}
|
||||
@@ -124,37 +124,49 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify signature: SigV4 when enforce-auth is enabled, custom when validateSignatures is enabled
|
||||
if (s3Service.isAuthEnforced()) {
|
||||
// Registered IAM credentials always carry standard SigV4. Account-shaped credentials
|
||||
// from older Floci-generated URLs retain the custom validation path when enforcement is off.
|
||||
boolean authEnforced = s3Service.isAuthEnforced();
|
||||
String rawCredential = queryParams.getFirst("X-Amz-Credential");
|
||||
String scopedAccessKey = URLDecoder.decode(rawCredential, StandardCharsets.UTF_8).split("/", 2)[0];
|
||||
boolean generatedCredential = iamService != null && iamService.presignedScope(scopedAccessKey).isPresent();
|
||||
if (authEnforced || presignGenerator.shouldValidateSignatures() || generatedCredential) {
|
||||
String credential = queryParams.getFirst("X-Amz-Credential");
|
||||
String decodedCredential = URLDecoder.decode(credential, StandardCharsets.UTF_8);
|
||||
String[] credParts = decodedCredential.split("/");
|
||||
if (credParts.length < 5) {
|
||||
if (authEnforced || generatedCredential) {
|
||||
requestContext.abortWith(errorResponse(403, "InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records."));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
String accessKeyId = credParts[0];
|
||||
String secretKey = resolveSecretKey(accessKeyId, queryParams.getFirst("X-Amz-Security-Token"));
|
||||
if (secretKey != null) {
|
||||
if (!verifySigV4Signature(requestContext, signature, secretKey)) {
|
||||
requestContext.abortWith(errorResponse(403, "SignatureDoesNotMatch",
|
||||
"The request signature we calculated does not match the signature you provided."));
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> unsignedChecksumHeaders = unsignedChecksumHeaders(
|
||||
requestContext.getHeaders().keySet(), queryParams.getFirst("X-Amz-SignedHeaders"));
|
||||
if (!unsignedChecksumHeaders.isEmpty()) {
|
||||
requestContext.abortWith(headersNotSignedResponse(unsignedChecksumHeaders));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (authEnforced || generatedCredential) {
|
||||
requestContext.abortWith(errorResponse(403, "InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String accessKeyId = credParts[0];
|
||||
String secretKey = resolveSecretKey(accessKeyId, queryParams.getFirst("X-Amz-Security-Token"));
|
||||
if (secretKey == null) {
|
||||
requestContext.abortWith(errorResponse(403, "InvalidAccessKeyId",
|
||||
"The AWS Access Key Id you provided does not exist in our records."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!verifySigV4Signature(requestContext, signature, secretKey)) {
|
||||
requestContext.abortWith(errorResponse(403, "SignatureDoesNotMatch",
|
||||
"The request signature we calculated does not match the signature you provided."));
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> unsignedChecksumHeaders = unsignedChecksumHeaders(
|
||||
requestContext.getHeaders().keySet(), queryParams.getFirst("X-Amz-SignedHeaders"));
|
||||
if (!unsignedChecksumHeaders.isEmpty()) {
|
||||
requestContext.abortWith(headersNotSignedResponse(unsignedChecksumHeaders));
|
||||
}
|
||||
} else if (presignGenerator.shouldValidateSignatures()) {
|
||||
if (presignGenerator.shouldValidateSignatures()) {
|
||||
String path = requestContext.getUriInfo().getPath();
|
||||
String[] parts = path.split("/", 3);
|
||||
if (parts.length < 3) {
|
||||
@@ -208,16 +220,6 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
|
||||
String authority = S3VirtualHostFilter.resolveHost(requestContext.getHeaderString("Host"),
|
||||
requestContext.getHeaderString("X-Forwarded-Host"), requestUri);
|
||||
|
||||
StringBuilder canonicalHeaders = new StringBuilder();
|
||||
for (String header : signedHeaders.split(";")) {
|
||||
if ("host".equals(header)) {
|
||||
canonicalHeaders.append("host:").append(authority).append("\n");
|
||||
} else {
|
||||
String canonicalValue = canonicalizeHeaderValue(requestContext.getHeaderString(header));
|
||||
canonicalHeaders.append(header).append(":").append(canonicalValue).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Canonical request: the wire path, since a leading-slash key travels (and is
|
||||
// signed) as a double slash that JAX-RS would otherwise collapse.
|
||||
String path = S3HeaderSignatureFilter.signedPath(currentVertxRequest, requestUri);
|
||||
@@ -229,18 +231,12 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
|
||||
if (payloadHash == null) {
|
||||
payloadHash = "UNSIGNED-PAYLOAD";
|
||||
}
|
||||
String canonicalRequest = requestContext.getMethod() + "\n"
|
||||
+ path + "\n"
|
||||
+ canonicalQueryString + "\n"
|
||||
+ canonicalHeaders + "\n"
|
||||
+ signedHeaders + "\n"
|
||||
+ payloadHash;
|
||||
|
||||
// String to sign
|
||||
String stringToSign = "AWS4-HMAC-SHA256\n"
|
||||
+ amzDate + "\n"
|
||||
+ credentialScope + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(canonicalRequest);
|
||||
String canonicalRequest = S3PresignedCanonicalRequest.build(
|
||||
requestContext.getMethod(), path, canonicalQueryString, signedHeaders,
|
||||
header -> "host".equals(header) ? authority : requestContext.getHeaderString(header),
|
||||
payloadHash);
|
||||
String stringToSign = S3PresignedCanonicalRequest.stringToSign(
|
||||
amzDate, credentialScope, canonicalRequest);
|
||||
|
||||
// Derive signing key and compute expected signature
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, date, region, service);
|
||||
|
||||
@@ -1,23 +1,43 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class PreSignedUrlGenerator {
|
||||
|
||||
private static final int MAX_PRESIGN_EXPIRY_SECONDS = 604800;
|
||||
private static final ObjectMapper POLICY_MAPPER = new ObjectMapper();
|
||||
private static final char[] ACCESS_KEY_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
|
||||
private static final DateTimeFormatter AMZ_DATE_FORMAT =
|
||||
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
|
||||
|
||||
@@ -26,53 +46,83 @@ public class PreSignedUrlGenerator {
|
||||
private final boolean validateSignatures;
|
||||
private final String defaultRegion;
|
||||
private final String defaultAccountId;
|
||||
|
||||
// Field-injected so the package-private test constructors remain valid
|
||||
@Inject
|
||||
Instance<RequestContext> requestContextInstance;
|
||||
private final IamService iamService;
|
||||
private final Instance<RequestContext> requestContextInstance;
|
||||
private final SecureRandom random;
|
||||
private final Clock clock;
|
||||
private final ConcurrentMap<CredentialKey, TemporaryCredential> temporaryCredentials =
|
||||
new ConcurrentHashMap<>();
|
||||
private final ConcurrentMap<String, RetiredCredential> retiredCredentials = new ConcurrentHashMap<>();
|
||||
private final AtomicBoolean expiredSessionsSwept = new AtomicBoolean();
|
||||
|
||||
@Inject
|
||||
public PreSignedUrlGenerator(EmulatorConfig config) {
|
||||
public PreSignedUrlGenerator(EmulatorConfig config, IamService iamService,
|
||||
Instance<RequestContext> requestContextInstance) {
|
||||
this(config.auth().presignSecret(),
|
||||
config.services().s3().defaultPresignExpirySeconds(),
|
||||
config.auth().validateSignatures(),
|
||||
config.defaultRegion(),
|
||||
config.defaultAccountId());
|
||||
config.defaultAccountId(),
|
||||
iamService,
|
||||
requestContextInstance,
|
||||
new SecureRandom());
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing. */
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry) {
|
||||
this(secret, defaultExpiry, false, "us-east-1", "000000000000"); // partition-literal: test-shaped constructor default
|
||||
this(secret, defaultExpiry, false, "us-east-1", "000000000000", null, null, new SecureRandom()); // partition-literal: test-shaped constructor default
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures) {
|
||||
this(secret, defaultExpiry, validateSignatures, "us-east-1", "000000000000"); // partition-literal: test-shaped constructor default
|
||||
this(secret, defaultExpiry, validateSignatures, "us-east-1", "000000000000", // partition-literal: test-shaped constructor default
|
||||
null, null, new SecureRandom());
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion) {
|
||||
this(secret, defaultExpiry, validateSignatures, defaultRegion, "000000000000");
|
||||
this(secret, defaultExpiry, validateSignatures, defaultRegion, "000000000000",
|
||||
null, null, new SecureRandom());
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion, String defaultAccountId) {
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion,
|
||||
String defaultAccountId) {
|
||||
this(secret, defaultExpiry, validateSignatures, defaultRegion, defaultAccountId,
|
||||
null, null, new SecureRandom());
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion,
|
||||
String defaultAccountId, IamService iamService,
|
||||
Instance<RequestContext> requestContextInstance, SecureRandom random) {
|
||||
this(secret, defaultExpiry, validateSignatures, defaultRegion, defaultAccountId,
|
||||
iamService, requestContextInstance, random, Clock.systemUTC());
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion,
|
||||
String defaultAccountId, IamService iamService,
|
||||
Instance<RequestContext> requestContextInstance, SecureRandom random, Clock clock) {
|
||||
this.secret = secret;
|
||||
this.defaultExpiry = defaultExpiry;
|
||||
this.validateSignatures = validateSignatures;
|
||||
this.defaultRegion = defaultRegion;
|
||||
this.defaultAccountId = defaultAccountId;
|
||||
this.iamService = iamService;
|
||||
this.requestContextInstance = requestContextInstance;
|
||||
this.random = random;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
private String resolveAccessKeyId() {
|
||||
private SigningIdentity resolveSigningIdentity(String region) {
|
||||
String accountId = defaultAccountId;
|
||||
if (requestContextInstance != null) {
|
||||
try {
|
||||
String accountId = requestContextInstance.get().getAccountId();
|
||||
if (accountId != null) {
|
||||
return accountId;
|
||||
RequestContext requestContext = requestContextInstance.get();
|
||||
if (requestContext.getAccountId() != null && !requestContext.getAccountId().isBlank()) {
|
||||
accountId = requestContext.getAccountId();
|
||||
}
|
||||
} catch (ContextNotActiveException ignored) {
|
||||
// outside request scope — fall through to default
|
||||
// Direct callers outside HTTP request scope use the configured defaults.
|
||||
}
|
||||
}
|
||||
return defaultAccountId;
|
||||
return new SigningIdentity(accountId, region);
|
||||
}
|
||||
|
||||
public boolean shouldValidateSignatures() {
|
||||
@@ -91,20 +141,176 @@ public class PreSignedUrlGenerator {
|
||||
public String generatePresignedUrl(String baseUrl, String bucket, String key,
|
||||
String method, int expiresSeconds, String region) {
|
||||
int expiry = expiresSeconds > 0 ? expiresSeconds : defaultExpiry;
|
||||
String amzDate = AMZ_DATE_FORMAT.format(Instant.now());
|
||||
String credential = resolveAccessKeyId() + "/" + amzDate.substring(0, 8) + "/" + region + "/s3/aws4_request";
|
||||
if (expiry < 1 || expiry > MAX_PRESIGN_EXPIRY_SECONDS) {
|
||||
throw new IllegalArgumentException("Presigned URL expiry must be between 1 and 604800 seconds");
|
||||
}
|
||||
Instant signedAt = clock.instant();
|
||||
String amzDate = AMZ_DATE_FORMAT.format(signedAt);
|
||||
|
||||
String signature = computeSignature(method, bucket, key, amzDate, expiry);
|
||||
if (iamService == null) {
|
||||
return generateLegacyPresignedUrl(baseUrl, bucket, key, method, expiry, amzDate, region);
|
||||
}
|
||||
|
||||
SigningIdentity identity = resolveSigningIdentity(region);
|
||||
TemporaryCredential temporaryCredential = temporaryCredential(identity, bucket, key, method, signedAt, expiry);
|
||||
String date = amzDate.substring(0, 8);
|
||||
String credentialScope = date + "/" + identity.region() + "/s3/aws4_request";
|
||||
String credential = temporaryCredential.accessKeyId() + "/" + credentialScope;
|
||||
|
||||
URI baseUri = URI.create(baseUrl);
|
||||
String authority = baseUri.getRawAuthority();
|
||||
if (authority == null || authority.isBlank()) {
|
||||
throw new IllegalArgumentException("Pre-signed URL base must include an authority: " + baseUrl);
|
||||
}
|
||||
|
||||
String path = "/" + PreSignedUrlFilter.awsUriEncode(bucket) + "/" + awsUriEncodePath(key);
|
||||
MultivaluedHashMap<String, String> queryParams = new MultivaluedHashMap<>();
|
||||
queryParams.putSingle("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
||||
queryParams.putSingle("X-Amz-Credential", credential);
|
||||
queryParams.putSingle("X-Amz-Date", amzDate);
|
||||
queryParams.putSingle("X-Amz-Expires", Integer.toString(expiry));
|
||||
queryParams.putSingle("X-Amz-Security-Token", temporaryCredential.sessionToken());
|
||||
queryParams.putSingle("X-Amz-SignedHeaders", "host");
|
||||
String canonicalQuery = PreSignedUrlFilter.buildCanonicalQueryString(queryParams);
|
||||
String canonicalRequest = S3PresignedCanonicalRequest.build(
|
||||
method, path, canonicalQuery, "host", header -> authority, "UNSIGNED-PAYLOAD");
|
||||
String signature;
|
||||
try {
|
||||
String stringToSign = S3PresignedCanonicalRequest.stringToSign(
|
||||
amzDate, credentialScope, canonicalRequest);
|
||||
byte[] signingKey = SigV4RequestValidator.deriveSigningKey(
|
||||
temporaryCredential.secretAccessKey(), date, identity.region(), "s3");
|
||||
signature = SigV4RequestValidator.hexEncode(
|
||||
SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Failed to compute S3 SigV4 pre-signed URL", e);
|
||||
}
|
||||
|
||||
String normalizedBaseUrl = baseUrl.endsWith("/")
|
||||
? baseUrl.substring(0, baseUrl.length() - 1)
|
||||
: baseUrl;
|
||||
return normalizedBaseUrl + path + "?" + canonicalQuery + "&X-Amz-Signature=" + signature;
|
||||
}
|
||||
|
||||
private String generateLegacyPresignedUrl(String baseUrl, String bucket, String key,
|
||||
String method, int expiry, String amzDate, String region) {
|
||||
String accountId = resolveSigningIdentity(region).accountId();
|
||||
String credential = accountId + "/" + amzDate.substring(0, 8)
|
||||
+ "/" + region + "/s3/aws4_request";
|
||||
|
||||
String signature = computeLegacySignature(method, bucket, key, amzDate, expiry);
|
||||
|
||||
return baseUrl + "/" + bucket + "/" + key
|
||||
+ "?X-Amz-Algorithm=AWS4-HMAC-SHA256"
|
||||
+ "&X-Amz-Credential=" + URLEncoder.encode(credential, StandardCharsets.UTF_8)
|
||||
+ "&X-Amz-Credential=" + PreSignedUrlFilter.awsUriEncode(credential)
|
||||
+ "&X-Amz-Date=" + amzDate
|
||||
+ "&X-Amz-Expires=" + expiry
|
||||
+ "&X-Amz-SignedHeaders=host"
|
||||
+ "&X-Amz-Signature=" + signature;
|
||||
}
|
||||
|
||||
private TemporaryCredential temporaryCredential(SigningIdentity identity, String bucket, String key,
|
||||
String method, Instant signedAt, int expiry) {
|
||||
// The in-memory retirement list cannot survive a restart. Sweep persisted expired
|
||||
// sessions once on first use, then retire this process's replacements as they expire.
|
||||
if (expiredSessionsSwept.compareAndSet(false, true)) {
|
||||
try {
|
||||
iamService.sweepExpiredSessions(signedAt);
|
||||
} catch (RuntimeException failure) {
|
||||
expiredSessionsSwept.set(false);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
// Previously issued URLs can still use a replaced credential. Sweep it only after expiry.
|
||||
retiredCredentials.forEach((accessKeyId, retired) -> {
|
||||
if (retired.credential().expiration().isBefore(signedAt)
|
||||
&& retiredCredentials.remove(accessKeyId, retired)) {
|
||||
iamService.unregisterSession(retired.accountId(), accessKeyId);
|
||||
}
|
||||
});
|
||||
temporaryCredentials.forEach((keyToRemove, credential) -> {
|
||||
if (credential.expiration().isBefore(signedAt)
|
||||
&& temporaryCredentials.remove(keyToRemove, credential)) {
|
||||
iamService.unregisterSession(keyToRemove.accountId(), credential.accessKeyId());
|
||||
}
|
||||
});
|
||||
// The signed timestamp is second-granular. Keep the credential until the URL's
|
||||
// final second, but never for the old seven-day floor.
|
||||
Instant requiredExpiration = signedAt.truncatedTo(ChronoUnit.SECONDS).plusSeconds(expiry + 1L);
|
||||
CredentialKey cacheKey = new CredentialKey(identity.accountId(), identity.region(), method, bucket, key);
|
||||
return temporaryCredentials.compute(cacheKey, (ignored, existing) -> {
|
||||
if (existing != null && !existing.expiration().isBefore(requiredExpiration)
|
||||
&& iamService.findSecretKey(existing.accessKeyId(), existing.sessionToken())
|
||||
.filter(existing.secretAccessKey()::equals).isPresent()) {
|
||||
return existing;
|
||||
}
|
||||
TemporaryCredential created = new TemporaryCredential(
|
||||
randomAccessKeyId(), randomUrlSafeValue(30), randomUrlSafeValue(48), requiredExpiration);
|
||||
String action = presignedAction(method);
|
||||
String resource = S3PublicAccessEvaluator.objectArn(
|
||||
AwsRegions.partitionFor(identity.region()), bucket, key);
|
||||
iamService.registerPresignedUrlSession(
|
||||
identity.accountId(),
|
||||
created.accessKeyId(),
|
||||
created.secretAccessKey(),
|
||||
created.sessionToken(),
|
||||
created.expiration(),
|
||||
scopedPolicy(method, action, resource), action, resource);
|
||||
if (existing != null) {
|
||||
if (existing.expiration().isBefore(signedAt)) {
|
||||
iamService.unregisterSession(identity.accountId(), existing.accessKeyId());
|
||||
} else {
|
||||
retiredCredentials.put(existing.accessKeyId(),
|
||||
new RetiredCredential(identity.accountId(), existing));
|
||||
}
|
||||
}
|
||||
return created;
|
||||
});
|
||||
}
|
||||
|
||||
private static String presignedAction(String method) {
|
||||
return switch (method) {
|
||||
case "GET", "HEAD" -> "s3:GetObject";
|
||||
case "PUT" -> "s3:PutObject";
|
||||
case "DELETE" -> "s3:DeleteObject";
|
||||
default -> throw new IllegalArgumentException("Unsupported S3 presigned URL method: " + method);
|
||||
};
|
||||
}
|
||||
|
||||
private static String scopedPolicy(String method, String action, String resource) {
|
||||
try {
|
||||
// S3 also checks GetObject for a conditional PutObject with If-Match. The
|
||||
// presigned scope still restricts the credential to the URL's PUT operation.
|
||||
Object actions = "PUT".equals(method)
|
||||
? List.of(action, "s3:GetObject") : action;
|
||||
return POLICY_MAPPER.writeValueAsString(Map.of(
|
||||
"Version", "2012-10-17",
|
||||
"Statement", List.of(Map.of("Effect", "Allow", "Action", actions, "Resource", resource))));
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Failed to encode S3 presigned session policy", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String randomAccessKeyId() {
|
||||
StringBuilder value = new StringBuilder("ASIA");
|
||||
for (int index = 0; index < 16; index++) {
|
||||
value.append(ACCESS_KEY_ALPHABET[random.nextInt(ACCESS_KEY_ALPHABET.length)]);
|
||||
}
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
private String randomUrlSafeValue(int byteCount) {
|
||||
byte[] bytes = new byte[byteCount];
|
||||
random.nextBytes(bytes);
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||
}
|
||||
|
||||
private static String awsUriEncodePath(String value) {
|
||||
return Arrays.stream(value.split("/", -1))
|
||||
.map(PreSignedUrlFilter::awsUriEncode)
|
||||
.collect(Collectors.joining("/"));
|
||||
}
|
||||
|
||||
public boolean isExpired(String amzDate, int expiresSeconds) {
|
||||
try {
|
||||
Instant signedAt = Instant.from(AMZ_DATE_FORMAT.parse(amzDate));
|
||||
@@ -116,12 +322,12 @@ public class PreSignedUrlGenerator {
|
||||
|
||||
public boolean verifySignature(String method, String bucket, String key,
|
||||
String amzDate, int expiresSeconds, String signature) {
|
||||
String expected = computeSignature(method, bucket, key, amzDate, expiresSeconds);
|
||||
String expected = computeLegacySignature(method, bucket, key, amzDate, expiresSeconds);
|
||||
return expected.equals(signature);
|
||||
}
|
||||
|
||||
private String computeSignature(String method, String bucket, String key,
|
||||
String amzDate, int expiresSeconds) {
|
||||
private String computeLegacySignature(String method, String bucket, String key,
|
||||
String amzDate, int expiresSeconds) {
|
||||
String stringToSign = method + "\n" + bucket + "/" + key + "\n" + amzDate + "\n" + expiresSeconds;
|
||||
return hmacSha256(secret, stringToSign);
|
||||
}
|
||||
@@ -131,7 +337,7 @@ public class PreSignedUrlGenerator {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
||||
byte[] hash = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
var sb = new StringBuilder();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
@@ -140,4 +346,17 @@ public class PreSignedUrlGenerator {
|
||||
throw new RuntimeException("Failed to compute HMAC-SHA256", e);
|
||||
}
|
||||
}
|
||||
|
||||
private record SigningIdentity(String accountId, String region) {
|
||||
}
|
||||
|
||||
private record CredentialKey(String accountId, String region, String method, String bucket, String key) {
|
||||
}
|
||||
|
||||
private record TemporaryCredential(String accessKeyId, String secretAccessKey,
|
||||
String sessionToken, Instant expiration) {
|
||||
}
|
||||
|
||||
private record RetiredCredential(String accountId, TemporaryCredential credential) {
|
||||
}
|
||||
}
|
||||
@@ -3807,7 +3807,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the object key from the raw Vert.x request URI, preserving leading slashes
|
||||
* Extracts the object key from the raw Vert.x request path, preserving leading slashes
|
||||
* that JAX-RS path normalization would otherwise strip.
|
||||
*/
|
||||
private String extractObjectKey(UriInfo uriInfo, String bucket) {
|
||||
@@ -3816,9 +3816,7 @@ public class S3Controller {
|
||||
|
||||
private String extractObjectKey(UriInfo uriInfo, String bucket, String fallbackKey) {
|
||||
validateRawUri();
|
||||
String rawUri = currentVertxRequest.getCurrent().request().uri();
|
||||
int qIdx = rawUri.indexOf('?');
|
||||
String rawPath = qIdx >= 0 ? rawUri.substring(0, qIdx) : rawUri;
|
||||
String rawPath = currentVertxRequest.getCurrent().request().path();
|
||||
String bucketPrefix = "/" + bucket + "/";
|
||||
String rawKey;
|
||||
if (isVirtualHostedRawPath(uriInfo, bucket, rawPath)) {
|
||||
@@ -3883,9 +3881,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
private void validateRawUri() {
|
||||
String rawUri = currentVertxRequest.getCurrent().request().uri();
|
||||
int queryIndex = rawUri.indexOf('?');
|
||||
String rawPath = queryIndex >= 0 ? rawUri.substring(0, queryIndex) : rawUri;
|
||||
String rawPath = currentVertxRequest.getCurrent().request().path();
|
||||
String decodedPath;
|
||||
try {
|
||||
decodedPath = URLDecoder.decode(rawPath.replace("+", "%2B"), StandardCharsets.UTF_8);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/** The canonical request and string-to-sign used by both S3 URL issuance and verification. */
|
||||
final class S3PresignedCanonicalRequest {
|
||||
|
||||
private S3PresignedCanonicalRequest() {
|
||||
}
|
||||
|
||||
static String build(String method, String signedPath, String canonicalQuery,
|
||||
String signedHeaders, Function<String, String> headerValue, String payloadHash) {
|
||||
StringBuilder canonicalHeaders = new StringBuilder();
|
||||
for (String header : signedHeaders.split(";")) {
|
||||
canonicalHeaders.append(header).append(':')
|
||||
.append(PreSignedUrlFilter.canonicalizeHeaderValue(headerValue.apply(header)))
|
||||
.append('\n');
|
||||
}
|
||||
return method + "\n" + signedPath + "\n" + canonicalQuery + "\n"
|
||||
+ canonicalHeaders + "\n" + signedHeaders + "\n" + payloadHash;
|
||||
}
|
||||
|
||||
static String stringToSign(String amzDate, String credentialScope, String canonicalRequest) throws Exception {
|
||||
return "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n"
|
||||
+ SigV4RequestValidator.sha256Hex(canonicalRequest);
|
||||
}
|
||||
}
|
||||
@@ -373,7 +373,7 @@ public class SesExportJobService implements Resettable {
|
||||
}
|
||||
return Optional.of(preSignedUrlGenerator.generatePresignedUrl(baseUrl,
|
||||
exportBucket(job.getAccountId(), job.getRegion()), job.getObjectKey(), "GET",
|
||||
PRESIGNED_URL_EXPIRY_SECONDS));
|
||||
PRESIGNED_URL_EXPIRY_SECONDS, job.getRegion()));
|
||||
}
|
||||
|
||||
public List<ExportJob> listExportJobs(String region, String sourceType, String jobStatus) {
|
||||
|
||||
@@ -15,6 +15,8 @@ import io.github.hectorvent.floci.services.iam.ScpProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.s3.S3Controller;
|
||||
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
@@ -99,6 +101,10 @@ class IamEnforcementFilterTest {
|
||||
}
|
||||
|
||||
private IamEnforcementFilter newFilter() {
|
||||
return newFilter(mock(CurrentVertxRequest.class));
|
||||
}
|
||||
|
||||
private IamEnforcementFilter newFilter(CurrentVertxRequest currentVertxRequest) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<ScpProvider> scpProvider =
|
||||
mock(Instance.class);
|
||||
@@ -107,7 +113,7 @@ class IamEnforcementFilterTest {
|
||||
config, accountResolver, iamService, evaluator, actionRegistry, arnBuilder,
|
||||
requestContext, conditionContextResolver,
|
||||
mock(CloudTrailService.class),
|
||||
mock(CurrentVertxRequest.class),
|
||||
currentVertxRequest,
|
||||
catalog, scpProvider, sessionAccountLookup);
|
||||
}
|
||||
|
||||
@@ -475,6 +481,91 @@ class IamEnforcementFilterTest {
|
||||
verifyNoInteractions(evaluator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignedCredentialCannotBypassAnUnmappedAction() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(containerRequest.getMethod()).thenReturn("POST");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIASCOPE");
|
||||
when(actionRegistry.resolve("s3", containerRequest)).thenReturn(null);
|
||||
when(iamService.presignedScope("ASIASCOPE"))
|
||||
.thenReturn(Optional.of(new IamService.PresignedScope(
|
||||
"s3:GetObject", "arn:aws:s3:::bucket/key")));
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
verify(containerRequest).abortWith(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopedPresignedRequestWithoutRoutingContextIsDenied() {
|
||||
assertScopedPresignedRequestWithoutHttpRequestIsDenied(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopedPresignedRequestWithoutHttpRequestIsDenied() {
|
||||
assertScopedPresignedRequestWithoutHttpRequestIsDenied(mock(RoutingContext.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopedPresignedRequestUsesWirePathForAbsoluteFormProxyTarget() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class);
|
||||
RoutingContext routingContext = mock(RoutingContext.class);
|
||||
HttpServerRequest httpRequest = mock(HttpServerRequest.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIASCOPE");
|
||||
when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:GetObject");
|
||||
when(iamService.resolveCallerContext("ASIASCOPE"))
|
||||
.thenReturn(CallerContext.of(List.of("{\"Version\":\"2012-10-17\",\"Statement\":[]}")));
|
||||
when(iamService.presignedScope("ASIASCOPE"))
|
||||
.thenReturn(Optional.of(new IamService.PresignedScope(
|
||||
"s3:GetObject", "arn:aws:s3:::bucket//key")));
|
||||
when(currentVertxRequest.getCurrent()).thenReturn(routingContext);
|
||||
when(routingContext.request()).thenReturn(httpRequest);
|
||||
when(httpRequest.uri()).thenReturn("https://proxy.example/bucket//key?X-Amz-Algorithm=AWS4-HMAC-SHA256");
|
||||
when(httpRequest.path()).thenReturn("/bucket//key");
|
||||
when(conditionContextResolver.resolveRemainingTargets("s3", "s3:GetObject", containerRequest))
|
||||
.thenReturn(List.of());
|
||||
when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(IamPolicyEvaluator.Decision.ALLOW);
|
||||
|
||||
newFilter(currentVertxRequest).filter(containerRequest);
|
||||
|
||||
verify(containerRequest, never()).abortWith(any());
|
||||
verify(evaluator).evaluateResolvedResourcePolicy(any(), any(), any(),
|
||||
eq("s3:GetObject"), eq("arn:aws:s3:::bucket//key"), any());
|
||||
}
|
||||
|
||||
private void assertScopedPresignedRequestWithoutHttpRequestIsDenied(RoutingContext routingContext) {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
CurrentVertxRequest currentVertxRequest = mock(CurrentVertxRequest.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=ASIASCOPE/20260927/us-east-1/s3/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIASCOPE");
|
||||
when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:GetObject");
|
||||
when(iamService.resolveCallerContext("ASIASCOPE"))
|
||||
.thenReturn(CallerContext.of(List.of("{\"Version\":\"2012-10-17\",\"Statement\":[]}")));
|
||||
when(iamService.presignedScope("ASIASCOPE"))
|
||||
.thenReturn(Optional.of(new IamService.PresignedScope(
|
||||
"s3:GetObject", "arn:aws:s3:::bucket/key")));
|
||||
if (routingContext != null) {
|
||||
when(currentVertxRequest.getCurrent()).thenReturn(routingContext);
|
||||
}
|
||||
|
||||
newFilter(currentVertxRequest).filter(containerRequest);
|
||||
|
||||
ArgumentCaptor<Response> response = ArgumentCaptor.forClass(Response.class);
|
||||
verify(containerRequest).abortWith(response.capture());
|
||||
assertEquals(403, response.getValue().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void bareAccountIdKeyWithNoScpCeilingStillPassesThrough() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
|
||||
+56
-1
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
@@ -108,7 +109,61 @@ class IamServicePersistenceTest {
|
||||
assertTrue(restarted.findSecretKey(accessKeyId, "legacy-session-token").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredTemporarySessionsAreRemovedAfterRestart(@TempDir Path dir) {
|
||||
Instant now = Instant.now();
|
||||
IamService first = newService(dir);
|
||||
first.registerSessionForAccount("000000000000", "ASIAEXPIREDPRESIGN", "expired-secret",
|
||||
"expired-token", null, now.minusSeconds(1), null);
|
||||
first.registerSessionForAccount("000000000000", "ASIAVALIDPRESIGN", "valid-secret",
|
||||
"valid-token", null, now.plusSeconds(3600), null);
|
||||
|
||||
IamService restarted = newService(dir);
|
||||
assertEquals(1, restarted.sweepExpiredSessions(now));
|
||||
assertEquals(0, restarted.sweepExpiredSessions(now));
|
||||
assertEquals("valid-secret", restarted.findSecretKey("ASIAVALIDPRESIGN", "valid-token").orElseThrow());
|
||||
|
||||
IamService subsequentRestart = newService(dir);
|
||||
assertEquals(0, subsequentRestart.sweepExpiredSessions(now));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expiredSessionsWithoutOriginAreRemovedFromTheirStoredAccounts(@TempDir Path dir) {
|
||||
Instant now = Instant.now();
|
||||
String foreignKey = "ASIAFOREIGNEXPIRED";
|
||||
String legacyKey = "ASIALEGACYEXPIRED";
|
||||
String validKey = "ASIAFOREIGNVALID";
|
||||
StorageBackend<String, SessionCredential> raw = load(dir, "iam-sessions.json",
|
||||
new TypeReference<Map<String, SessionCredential>>() {});
|
||||
raw.put("111122223333/" + foreignKey,
|
||||
new SessionCredential(foreignKey, "secret", "token", null, now.minusSeconds(1), null));
|
||||
raw.put(legacyKey,
|
||||
new SessionCredential(legacyKey, "secret", "token", null, now.minusSeconds(1), null));
|
||||
raw.put("111122223333/" + validKey,
|
||||
new SessionCredential(validKey, "secret", "token", null, now.plusSeconds(3600), null));
|
||||
|
||||
StorageBackend<String, SessionCredential> reloaded = load(dir, "iam-sessions.json",
|
||||
new TypeReference<Map<String, SessionCredential>>() {});
|
||||
IamService restarted = newService(dir,
|
||||
new AccountAwareStorageBackend<>(reloaded, null, "000000000000"));
|
||||
assertEquals(2, restarted.sweepExpiredSessions(now));
|
||||
assertTrue(reloaded.get("111122223333/" + foreignKey).isEmpty());
|
||||
assertTrue(reloaded.get(legacyKey).isEmpty());
|
||||
assertTrue(reloaded.get("111122223333/" + validKey).isPresent());
|
||||
|
||||
StorageBackend<String, SessionCredential> subsequentReload = load(dir, "iam-sessions.json",
|
||||
new TypeReference<Map<String, SessionCredential>>() {});
|
||||
IamService subsequentRestart = newService(dir,
|
||||
new AccountAwareStorageBackend<>(subsequentReload, null, "000000000000"));
|
||||
assertEquals(0, subsequentRestart.sweepExpiredSessions(now));
|
||||
}
|
||||
|
||||
private IamService newService(Path dir) {
|
||||
return newService(dir, load(dir, "iam-sessions.json",
|
||||
new TypeReference<Map<String, SessionCredential>>() {}));
|
||||
}
|
||||
|
||||
private IamService newService(Path dir, StorageBackend<String, SessionCredential> sessionStore) {
|
||||
return new IamService(
|
||||
load(dir, "iam-users.json", new TypeReference<Map<String, IamUser>>() {}),
|
||||
load(dir, "iam-groups.json", new TypeReference<Map<String, IamGroup>>() {}),
|
||||
@@ -116,7 +171,7 @@ class IamServicePersistenceTest {
|
||||
load(dir, "iam-policies.json", new TypeReference<Map<String, IamPolicy>>() {}),
|
||||
load(dir, "iam-access-keys.json", new TypeReference<Map<String, AccessKey>>() {}),
|
||||
load(dir, "iam-instance-profiles.json", new TypeReference<Map<String, InstanceProfile>>() {}),
|
||||
load(dir, "iam-sessions.json", new TypeReference<Map<String, SessionCredential>>() {}),
|
||||
sessionStore,
|
||||
load(dir, "iam-account-aliases.json", new TypeReference<Map<String, String>>() {}),
|
||||
load(dir, "iam-password-policy.json", new TypeReference<Map<String, AccountPasswordPolicy>>() {}),
|
||||
load(dir, "iam-oidc-providers.json", new TypeReference<Map<String, OpenIDConnectProvider>>() {}),
|
||||
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.testing.S3IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testutil.S3RequestSigner;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.RestAssured;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
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.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3IamEnforcementProfile.class)
|
||||
class GeneratedPreSignedUrlAuthEnforcementIntegrationTest {
|
||||
|
||||
private static final S3RequestSigner OWNER = S3RequestSigner.signedAs("test", "test");
|
||||
|
||||
@Inject
|
||||
PreSignedUrlGenerator presignGenerator;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@Inject
|
||||
IamPolicyEvaluator policyEvaluator;
|
||||
|
||||
@Test
|
||||
void generatedUrlUsesRegisteredSigV4SessionUnderEnforcedAuth() {
|
||||
String suffix = UUID.randomUUID().toString().substring(0, 8);
|
||||
String bucket = "generated-presign-auth-" + suffix;
|
||||
String key = "exports/report with spaces.csv";
|
||||
String content = "recipient,status\nalice@example.com,DELIVERY\n";
|
||||
|
||||
given()
|
||||
.filter(OWNER)
|
||||
.when()
|
||||
.put("/" + bucket)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.filter(OWNER)
|
||||
.body(content)
|
||||
.when()
|
||||
.put("/" + bucket + "/" + key)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String url = presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:" + RestAssured.port, bucket, key, "GET", 3600);
|
||||
URI uri = URI.create(url);
|
||||
String credential = queryParam(uri, "X-Amz-Credential");
|
||||
String sessionToken = queryParam(uri, "X-Amz-Security-Token");
|
||||
String accessKeyId = credential.substring(0, credential.indexOf('/'));
|
||||
|
||||
assertTrue(accessKeyId.matches("ASIA[A-Z0-9]{16}"));
|
||||
assertNotNull(sessionToken);
|
||||
assertEquals("000000000000", iamService.resolveAccountId(accessKeyId).orElseThrow());
|
||||
assertTrue(iamService.findSecretKey(accessKeyId, sessionToken).isPresent());
|
||||
|
||||
CallerContext scopedCaller = iamService.resolveCallerContext(accessKeyId);
|
||||
assertNotNull(scopedCaller);
|
||||
String objectArn = "arn:aws:s3:::" + bucket + "/" + key;
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.ALLOWED,
|
||||
policyEvaluator.simulatePrincipalPolicy(scopedCaller, "s3:GetObject", objectArn, Map.of()));
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.IMPLICIT_DENY,
|
||||
policyEvaluator.simulatePrincipalPolicy(scopedCaller, "s3:PutObject", objectArn, Map.of()));
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.IMPLICIT_DENY,
|
||||
policyEvaluator.simulatePrincipalPolicy(scopedCaller, "s3:GetObject",
|
||||
"arn:aws:s3:::" + bucket + "/other.csv", Map.of()));
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.IMPLICIT_DENY,
|
||||
policyEvaluator.simulatePrincipalPolicy(scopedCaller, "sqs:SendMessage", "*", Map.of()));
|
||||
|
||||
S3RequestSigner scopedSigner = S3RequestSigner.signedAs(accessKeyId,
|
||||
iamService.findSecretKey(accessKeyId, sessionToken).orElseThrow(), sessionToken);
|
||||
given().filter(scopedSigner).when().get("/" + bucket + "/other.csv")
|
||||
.then().statusCode(403).body("Error.Code", equalTo("AccessDenied"));
|
||||
given().filter(scopedSigner).body("changed").when().put("/" + bucket + "/" + key)
|
||||
.then().statusCode(403).body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
URI wildcardUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:" + RestAssured.port, bucket, "wild*.csv", "GET", 60));
|
||||
String wildcardKey = queryParam(wildcardUri, "X-Amz-Credential").split("/", 2)[0];
|
||||
String wildcardToken = queryParam(wildcardUri, "X-Amz-Security-Token");
|
||||
S3RequestSigner wildcardSigner = S3RequestSigner.signedAs(wildcardKey,
|
||||
iamService.findSecretKey(wildcardKey, wildcardToken).orElseThrow(), wildcardToken);
|
||||
given().filter(wildcardSigner).when().get("/" + bucket + "/wild-other.csv")
|
||||
.then().statusCode(403).body("Error.Code", equalTo("AccessDenied"));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get(uri.getRawPath() + "?" + uri.getRawQuery())
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo(content));
|
||||
|
||||
String tamperedQuery = uri.getRawQuery().replaceFirst(
|
||||
"X-Amz-Signature=[0-9a-f]+", "X-Amz-Signature=" + "0".repeat(64));
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get(uri.getRawPath() + "?" + tamperedQuery)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("SignatureDoesNotMatch"));
|
||||
|
||||
given().filter(OWNER).when().delete("/" + bucket + "/" + key).then().statusCode(204);
|
||||
given().filter(OWNER).when().delete("/" + bucket).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedUrlRegistersFreshCredentialsAfterStoredSessionIsRemoved() {
|
||||
String bucket = "generated-presign-reset-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String baseUrl = "http://localhost:" + RestAssured.port;
|
||||
URI oldUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "report.csv", "GET", 3600));
|
||||
String oldAccessKeyId = queryParam(oldUri, "X-Amz-Credential").split("/", 2)[0];
|
||||
|
||||
// A state reset removes the persisted IAM session while this generator stays alive.
|
||||
iamService.unregisterSession("000000000000", oldAccessKeyId);
|
||||
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
given().filter(OWNER).body("after reset").when().put("/" + bucket + "/report.csv")
|
||||
.then().statusCode(200);
|
||||
|
||||
URI newUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "report.csv", "GET", 3600));
|
||||
String newAccessKeyId = queryParam(newUri, "X-Amz-Credential").split("/", 2)[0];
|
||||
assertNotEquals(oldAccessKeyId, newAccessKeyId);
|
||||
assertTrue(iamService.findSecretKey(newAccessKeyId,
|
||||
queryParam(newUri, "X-Amz-Security-Token")).isPresent());
|
||||
|
||||
given().urlEncodingEnabled(false).when().get(newUri.getRawPath() + "?" + newUri.getRawQuery())
|
||||
.then().statusCode(200).body(equalTo("after reset"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedPutUrlCanReplaceObjectConditionallyUnderEnforcedIam() {
|
||||
String bucket = "generated-presign-conditional-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String key = "object.txt";
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
String eTag = given().filter(OWNER).body("old").when().put("/" + bucket + "/" + key)
|
||||
.then().statusCode(200).extract().header("ETag");
|
||||
|
||||
URI uri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:" + RestAssured.port, bucket, key, "PUT", 60));
|
||||
String accessKeyId = queryParam(uri, "X-Amz-Credential").split("/", 2)[0];
|
||||
CallerContext caller = iamService.resolveCallerContext(accessKeyId);
|
||||
String objectArn = "arn:aws:s3:::" + bucket + "/" + key;
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.ALLOWED,
|
||||
policyEvaluator.simulatePrincipalPolicy(caller, "s3:GetObject", objectArn, Map.of()));
|
||||
assertEquals(IamPolicyEvaluator.SimulationDecision.IMPLICIT_DENY,
|
||||
policyEvaluator.simulatePrincipalPolicy(caller, "s3:GetObject",
|
||||
"arn:aws:s3:::" + bucket + "/other.txt", Map.of()));
|
||||
|
||||
given().urlEncodingEnabled(false).header("If-Match", eTag).body("new")
|
||||
.when().put(uri.getRawPath() + "?" + uri.getRawQuery()).then().statusCode(200);
|
||||
given().filter(OWNER).when().get("/" + bucket + "/" + key)
|
||||
.then().statusCode(200).body(equalTo("new"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedPutUrlCannotAddTagsWithoutTaggingPermission() {
|
||||
String bucket = "generated-presign-tags-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
|
||||
URI uri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:" + RestAssured.port, bucket, "object.txt", "PUT", 60));
|
||||
String pathAndQuery = uri.getRawPath() + "?" + uri.getRawQuery();
|
||||
|
||||
given().urlEncodingEnabled(false).header("x-amz-tagging", "team=eng").body("tagged")
|
||||
.when().put(pathAndQuery)
|
||||
.then().statusCode(403).body("Error.Code", equalTo("AccessDenied"));
|
||||
given().urlEncodingEnabled(false).body("untagged")
|
||||
.when().put(pathAndQuery).then().statusCode(200);
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("untagged"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedUrlsKeepLeadingSlashKeysDistinctUnderEnforcedIam() throws Exception {
|
||||
String bucket = "generated-presign-slash-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String baseUrl = "http://localhost:" + RestAssured.port;
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
|
||||
URI putUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "/object.txt", "PUT", 60));
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpResponse<String> put = client.send(HttpRequest.newBuilder(putUri)
|
||||
.PUT(HttpRequest.BodyPublishers.ofString("leading slash")).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, put.statusCode(), put.body());
|
||||
|
||||
URI getUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "/object.txt", "GET", 60));
|
||||
HttpResponse<String> get = client.send(HttpRequest.newBuilder(getUri).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, get.statusCode(), get.body());
|
||||
assertEquals("leading slash", get.body());
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt").then().statusCode(404);
|
||||
|
||||
URI alteredUri = URI.create(getUri.toString().replace("/" + bucket + "//", "/" + bucket + "/"));
|
||||
HttpResponse<String> altered = client.send(HttpRequest.newBuilder(alteredUri).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(403, altered.statusCode(), altered.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxiedGeneratedUrlUsesTheSignedPathWhenHostMatchesBucket() throws Exception {
|
||||
String bucket = "generated-presign-proxy-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
given().filter(OWNER).body("signed object").when().put("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200);
|
||||
given().filter(OWNER).body("other object").when().put("/" + bucket + "/" + bucket + "/object.txt")
|
||||
.then().statusCode(200);
|
||||
|
||||
HttpClient proxyClient = HttpClient.newBuilder()
|
||||
.proxy(ProxySelector.of(new InetSocketAddress("localhost", RestAssured.port)))
|
||||
.build();
|
||||
String baseUrl = "http://" + bucket;
|
||||
URI getUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "object.txt", "GET", 60));
|
||||
HttpResponse<String> get = proxyClient.send(HttpRequest.newBuilder(getUri).GET().build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, get.statusCode(), get.body());
|
||||
assertEquals("signed object", get.body());
|
||||
|
||||
URI putUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
baseUrl, bucket, "object.txt", "PUT", 60));
|
||||
HttpResponse<String> put = proxyClient.send(HttpRequest.newBuilder(putUri)
|
||||
.PUT(HttpRequest.BodyPublishers.ofString("updated object")).build(),
|
||||
HttpResponse.BodyHandlers.ofString());
|
||||
assertEquals(200, put.statusCode(), put.body());
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("updated object"));
|
||||
given().filter(OWNER).when().get("/" + bucket + "/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("other object"));
|
||||
}
|
||||
|
||||
private static String queryParam(URI uri, String name) {
|
||||
for (String pair : uri.getRawQuery().split("&")) {
|
||||
int equals = pair.indexOf('=');
|
||||
if (equals > 0 && name.equals(pair.substring(0, equals))) {
|
||||
return URLDecoder.decode(pair.substring(equals + 1), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class GeneratedPreSignedUrlDefaultAuthIntegrationTest {
|
||||
|
||||
@Inject
|
||||
PreSignedUrlGenerator generator;
|
||||
|
||||
@Test
|
||||
void generatedUrlStillVerifiesItsSignatureWhenGlobalSignatureChecksAreDisabled() {
|
||||
String bucket = "generated-default-auth-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
given().body("original").when().put("/" + bucket + "/key").then().statusCode(200);
|
||||
|
||||
URI uri = URI.create(generator.generatePresignedUrl(
|
||||
"http://localhost:" + RestAssured.port, bucket, "key", "GET", 60));
|
||||
String pathAndQuery = uri.getRawPath() + "?" + uri.getRawQuery();
|
||||
given().urlEncodingEnabled(false).when().get(pathAndQuery)
|
||||
.then().statusCode(200).body(equalTo("original"));
|
||||
|
||||
String tampered = pathAndQuery.replaceFirst("X-Amz-Signature=[0-9a-f]+",
|
||||
"X-Amz-Signature=" + "0".repeat(64));
|
||||
given().urlEncodingEnabled(false).when().get(tampered)
|
||||
.then().statusCode(403).body("Error.Code", equalTo("SignatureDoesNotMatch"));
|
||||
|
||||
given().when().delete("/" + bucket + "/key").then().statusCode(204);
|
||||
given().when().delete("/" + bucket).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class PreSignedUrlGeneratorSessionTest {
|
||||
|
||||
@Test
|
||||
void renewingCredentialsKeepsOldUrlsUsableUntilSessionExpiry() {
|
||||
Instant start = Instant.parse("2026-09-27T00:00:00Z");
|
||||
Clock clock = mock(Clock.class);
|
||||
when(clock.instant()).thenReturn(start, start.plus(Duration.ofDays(1)),
|
||||
start.plus(Duration.ofDays(7)).plusSeconds(2));
|
||||
|
||||
Map<String, String> secrets = new ConcurrentHashMap<>();
|
||||
IamService iamService = mock(IamService.class);
|
||||
doAnswer(invocation -> {
|
||||
secrets.put(invocation.getArgument(1), invocation.getArgument(2));
|
||||
return null;
|
||||
}).when(iamService).registerPresignedUrlSession(anyString(), anyString(), anyString(),
|
||||
anyString(), any(Instant.class), anyString(), anyString(), anyString());
|
||||
when(iamService.findSecretKey(anyString(), anyString())).thenAnswer(invocation ->
|
||||
Optional.ofNullable(secrets.get(invocation.getArgument(0))));
|
||||
doAnswer(invocation -> {
|
||||
secrets.remove(invocation.getArgument(1));
|
||||
return null;
|
||||
}).when(iamService).unregisterSession(anyString(), anyString());
|
||||
|
||||
PreSignedUrlGenerator generator = new PreSignedUrlGenerator(
|
||||
"secret", 3600, true, "us-east-1", "000000000000",
|
||||
iamService, null, new SecureRandom(), clock);
|
||||
String first = generator.generatePresignedUrl(
|
||||
"http://localhost:4566", "bucket", "one", "GET", 604800);
|
||||
String firstKey = accessKey(first);
|
||||
String second = generator.generatePresignedUrl(
|
||||
"http://localhost:4566", "bucket", "two", "GET", 604800);
|
||||
String secondKey = accessKey(second);
|
||||
|
||||
assertNotEquals(firstKey, secondKey);
|
||||
assertTrue(secrets.containsKey(firstKey), "The original URL is still valid after renewal");
|
||||
assertTrue(secrets.containsKey(secondKey));
|
||||
assertEquals(2, secrets.size());
|
||||
|
||||
generator.generatePresignedUrl("http://localhost:4566", "bucket", "three", "GET", 3600);
|
||||
assertFalse(secrets.containsKey(firstKey), "Expired superseded sessions are removed");
|
||||
assertTrue(secrets.containsKey(secondKey));
|
||||
assertEquals(2, secrets.size(), "Unexpired object-scoped sessions remain available");
|
||||
verify(iamService, times(1)).sweepExpiredSessions(start);
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsExpireWithTheirUrlsAndAreScopedToAnObjectAndMethod() {
|
||||
Instant start = Instant.parse("2026-09-27T00:00:00Z");
|
||||
Clock clock = Clock.fixed(start, ZoneOffset.UTC);
|
||||
IamService iamService = mock(IamService.class);
|
||||
PreSignedUrlGenerator generator = new PreSignedUrlGenerator(
|
||||
"secret", 3600, true, "us-east-1", "000000000000",
|
||||
iamService, null, new SecureRandom(), clock);
|
||||
|
||||
String first = generator.generatePresignedUrl("http://localhost:4566", "bucket", "a", "GET", 60);
|
||||
String second = generator.generatePresignedUrl("http://localhost:4566", "bucket", "b", "GET", 60);
|
||||
String third = generator.generatePresignedUrl("http://localhost:4566", "bucket", "a", "PUT", 60);
|
||||
assertNotEquals(accessKey(first), accessKey(second));
|
||||
assertNotEquals(accessKey(first), accessKey(third));
|
||||
|
||||
ArgumentCaptor<Instant> expirations = ArgumentCaptor.forClass(Instant.class);
|
||||
ArgumentCaptor<String> policies = ArgumentCaptor.forClass(String.class);
|
||||
verify(iamService, times(3)).registerPresignedUrlSession(anyString(), anyString(), anyString(),
|
||||
anyString(), expirations.capture(), policies.capture(), anyString(), anyString());
|
||||
assertEquals(start.plusSeconds(61), expirations.getAllValues().getFirst());
|
||||
assertTrue(policies.getAllValues().get(0).contains("\"Action\":\"s3:GetObject\""));
|
||||
assertTrue(policies.getAllValues().get(0).contains("arn:aws:s3:::bucket/a"));
|
||||
assertTrue(policies.getAllValues().get(1).contains("arn:aws:s3:::bucket/b"));
|
||||
assertTrue(policies.getAllValues().get(2).contains("\"Action\":[\"s3:PutObject\",\"s3:GetObject\"]"));
|
||||
assertThrows(IllegalArgumentException.class, () -> generator.generatePresignedUrl(
|
||||
"http://localhost:4566", "bucket", "a", "GET", 604801));
|
||||
}
|
||||
|
||||
private static String accessKey(String url) {
|
||||
String credential = url.split("X-Amz-Credential=", 2)[1].split("&", 2)[0];
|
||||
return credential.split("%2F", 2)[0];
|
||||
}
|
||||
}
|
||||
+72
-11
@@ -1,6 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.RestAssured;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
@@ -10,15 +14,24 @@ import org.junit.jupiter.api.TestMethodOrder;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(PreSignedUrlIntegrationTest.PresignValidationProfile.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class PreSignedUrlIntegrationTest {
|
||||
|
||||
public static final class PresignValidationProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.auth.validate-signatures", "true");
|
||||
}
|
||||
}
|
||||
|
||||
private static final String BUCKET = "presign-test-bucket";
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
@@ -27,6 +40,9 @@ class PreSignedUrlIntegrationTest {
|
||||
@Inject
|
||||
PreSignedUrlGenerator presignGenerator;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createBucketAndUploadObject() {
|
||||
@@ -43,7 +59,7 @@ class PreSignedUrlIntegrationTest {
|
||||
@Test
|
||||
@Order(2)
|
||||
void accessWithPresignedGetUrl() {
|
||||
int port = io.restassured.RestAssured.port;
|
||||
int port = RestAssured.port;
|
||||
String fullBaseUrl = "http://localhost:" + port;
|
||||
|
||||
String presignedUrl = presignGenerator.generatePresignedUrl(
|
||||
@@ -53,6 +69,7 @@ class PreSignedUrlIntegrationTest {
|
||||
URI uri = URI.create(presignedUrl);
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get(uri.getRawPath() + "?" + uri.getRawQuery())
|
||||
.then()
|
||||
@@ -70,6 +87,7 @@ class PreSignedUrlIntegrationTest {
|
||||
assertTrue(url.contains("X-Amz-Credential="));
|
||||
assertTrue(url.contains("X-Amz-Date="));
|
||||
assertTrue(url.contains("X-Amz-Expires=300"));
|
||||
assertTrue(url.contains("X-Amz-Security-Token="));
|
||||
assertTrue(url.contains("X-Amz-SignedHeaders=host"));
|
||||
assertTrue(url.contains("X-Amz-Signature="));
|
||||
|
||||
@@ -77,21 +95,34 @@ class PreSignedUrlIntegrationTest {
|
||||
assertFalse(url.contains("AKIAIOSFODNN7EXAMPLE"),
|
||||
"X-Amz-Credential must not contain hardcoded AKIAIOSFODNN7EXAMPLE");
|
||||
|
||||
int credStart = url.indexOf("X-Amz-Credential=");
|
||||
int credEnd = url.indexOf("&", credStart);
|
||||
String encodedCredential = credEnd > 0
|
||||
? url.substring(credStart + "X-Amz-Credential=".length(), credEnd)
|
||||
: url.substring(credStart + "X-Amz-Credential=".length());
|
||||
String credential = URLDecoder.decode(encodedCredential, StandardCharsets.UTF_8);
|
||||
assertTrue(credential.startsWith("000000000000/"),
|
||||
"X-Amz-Credential should start with 12-digit account ID, got: " + credential);
|
||||
URI uri = URI.create(url);
|
||||
String credential = queryParam(uri, "X-Amz-Credential");
|
||||
String sessionToken = queryParam(uri, "X-Amz-Security-Token");
|
||||
String accessKeyId = credential.substring(0, credential.indexOf('/'));
|
||||
|
||||
assertTrue(accessKeyId.matches("ASIA[A-Z0-9]{16}"),
|
||||
"X-Amz-Credential should use a temporary access key, got: " + credential);
|
||||
assertNotNull(sessionToken);
|
||||
assertTrue(iamService.findSecretKey(accessKeyId, sessionToken).isPresent(),
|
||||
"The temporary credential must be registered for SigV4 validation");
|
||||
|
||||
URI secondUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:8080", BUCKET, "file.txt", "GET", 300));
|
||||
String secondCredential = queryParam(secondUri, "X-Amz-Credential");
|
||||
assertEquals(accessKeyId, secondCredential.substring(0, secondCredential.indexOf('/')),
|
||||
"Only URLs for the same object and method should reuse the scoped credential");
|
||||
assertEquals(sessionToken, queryParam(secondUri, "X-Amz-Security-Token"));
|
||||
|
||||
URI otherUri = URI.create(presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:8080", BUCKET, "second-file.txt", "GET", 300));
|
||||
assertNotEquals(accessKeyId, queryParam(otherUri, "X-Amz-Credential").split("/", 2)[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void expiredPresignedUrlReturns403() {
|
||||
// Create a URL with expired date by constructing manually
|
||||
int port = io.restassured.RestAssured.port;
|
||||
int port = RestAssured.port;
|
||||
|
||||
// Use an obviously expired date (year 2020)
|
||||
String expiredPath = "/" + BUCKET + "/secret-file.txt"
|
||||
@@ -113,7 +144,7 @@ class PreSignedUrlIntegrationTest {
|
||||
@Test
|
||||
@Order(5)
|
||||
void presignedPutUrl() {
|
||||
int port = io.restassured.RestAssured.port;
|
||||
int port = RestAssured.port;
|
||||
String fullBaseUrl = "http://localhost:" + port;
|
||||
String url = presignGenerator.generatePresignedUrl(
|
||||
fullBaseUrl, BUCKET, "uploaded-via-presign.txt", "PUT", 3600);
|
||||
@@ -121,6 +152,7 @@ class PreSignedUrlIntegrationTest {
|
||||
URI uri = URI.create(url);
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.body("uploaded via presigned PUT")
|
||||
.when()
|
||||
.put(uri.getRawPath() + "?" + uri.getRawQuery())
|
||||
@@ -136,6 +168,35 @@ class PreSignedUrlIntegrationTest {
|
||||
.body(equalTo("uploaded via presigned PUT"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void tamperedPresignedUrlReturnsSignatureMismatch() {
|
||||
int port = RestAssured.port;
|
||||
String url = presignGenerator.generatePresignedUrl(
|
||||
"http://localhost:" + port, BUCKET, "secret-file.txt", "GET", 3600);
|
||||
URI uri = URI.create(url);
|
||||
String tamperedQuery = uri.getRawQuery().replaceFirst(
|
||||
"X-Amz-Signature=[0-9a-f]+", "X-Amz-Signature=" + "0".repeat(64));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get(uri.getRawPath() + "?" + tamperedQuery)
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body("Error.Code", equalTo("SignatureDoesNotMatch"));
|
||||
}
|
||||
|
||||
private static String queryParam(URI uri, String name) {
|
||||
for (String pair : uri.getRawQuery().split("&")) {
|
||||
int equals = pair.indexOf('=');
|
||||
if (equals > 0 && name.equals(pair.substring(0, equals))) {
|
||||
return URLDecoder.decode(pair.substring(equals + 1), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// --- response-* query parameter overrides on presigned GET/HEAD ---
|
||||
|
||||
@Test
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class S3PresignedCanonicalRequestTest {
|
||||
|
||||
@Test
|
||||
void preservesWirePathSignedHeadersAndExplicitPayloadHash() {
|
||||
MultivaluedHashMap<String, String> query = new MultivaluedHashMap<>();
|
||||
query.add("tag", "b b");
|
||||
query.add("tag", "a+a");
|
||||
query.add("X-Amz-Signature", "excluded");
|
||||
Map<String, String> headers = Map.of("host", "localhost:4566", "x-custom", " a b ");
|
||||
|
||||
String canonicalRequest = S3PresignedCanonicalRequest.build(
|
||||
"PUT", "/bucket//a%2Bb", PreSignedUrlFilter.buildCanonicalQueryString(query),
|
||||
"host;x-custom", headers::get, "explicit-payload-hash");
|
||||
|
||||
assertEquals("PUT\n/bucket//a%2Bb\ntag=a%2Ba&tag=b%20b\n"
|
||||
+ "host:localhost:4566\nx-custom:a b\n\nhost;x-custom\nexplicit-payload-hash",
|
||||
canonicalRequest);
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
@@ -21,8 +20,11 @@ class S3PresignedPutTaggingIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "presigned-tagging-bucket";
|
||||
|
||||
@Inject
|
||||
PreSignedUrlGenerator presignGenerator;
|
||||
// These tests exercise S3 tagging dispatch with deliberately edited URL parameters.
|
||||
// The CDI presigner now uses registered SigV4 credentials, so editing its signature
|
||||
// would correctly fail. Authenticated SigV4 tagging is covered by the companion suite.
|
||||
private final PreSignedUrlGenerator presignGenerator =
|
||||
new PreSignedUrlGenerator("test", 3600, false);
|
||||
|
||||
/**
|
||||
* Regression test for #932: a presigned PUT URL whose {@code X-Amz-SignedHeaders}
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.testing.S3EnforceAuthProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3EnforceAuthProfile.class)
|
||||
class SesExportJobS3AuthEnforcementIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=111122223333/20260101/eu-west-2/ses/aws4_request";
|
||||
|
||||
@Test
|
||||
void completedExportUrlCanBeFetchedWhenS3AuthIsEnforced() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("{\"VdmAttributes\":{\"VdmEnabled\":\"ENABLED\"}}")
|
||||
.when()
|
||||
.put("/v2/email/account/vdm")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String jobId = given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body("""
|
||||
{"ExportDataSource": {"MessageInsightsDataSource":
|
||||
{"StartDate": %d, "EndDate": %d}},
|
||||
"ExportDestination": {"DataFormat": "CSV"}}
|
||||
""".formatted(Instant.now().minus(Duration.ofDays(1)).getEpochSecond(),
|
||||
Instant.now().plus(Duration.ofDays(1)).getEpochSecond()))
|
||||
.when()
|
||||
.post("/v2/email/export-jobs")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("JobId");
|
||||
|
||||
assertEquals("COMPLETED", awaitTerminal(jobId));
|
||||
|
||||
String url = given()
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.get("/v2/email/export-jobs/" + jobId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("ExportDestination.S3Url");
|
||||
URI uri = URI.create(url);
|
||||
String encodedCredential = Arrays.stream(uri.getRawQuery().split("&"))
|
||||
.filter(parameter -> parameter.startsWith("X-Amz-Credential="))
|
||||
.findFirst().orElseThrow().substring("X-Amz-Credential=".length());
|
||||
String credential = URLDecoder.decode(encodedCredential, StandardCharsets.UTF_8);
|
||||
assertTrue(credential.endsWith("/eu-west-2/s3/aws4_request"));
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.header("Host", uri.getRawAuthority())
|
||||
.when()
|
||||
.get(uri.getRawPath() + "?" + uri.getRawQuery())
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(startsWith("\"messageid\",\"sendtimestamp\""));
|
||||
}
|
||||
|
||||
private static String awaitTerminal(String jobId) {
|
||||
for (int attempt = 0; attempt < 100; attempt++) {
|
||||
Response response = given()
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.get("/v2/email/export-jobs/" + jobId)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().response();
|
||||
String status = response.path("JobStatus");
|
||||
if (!"PROCESSING".equals(status) && !"CREATED".equals(status)) {
|
||||
return status;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new AssertionError("interrupted while waiting for the export job", e);
|
||||
}
|
||||
}
|
||||
throw new AssertionError("export job " + jobId + " never reached a terminal state");
|
||||
}
|
||||
}
|
||||
+14
-8
@@ -7,6 +7,7 @@ import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -51,6 +52,15 @@ class SesExportJobV2IntegrationTest {
|
||||
.when().post("/v2/email/export-jobs");
|
||||
}
|
||||
|
||||
private static String fetchExportCsv(String url) {
|
||||
URI uri = URI.create(url);
|
||||
// SigV4 signs Host as well as path and query. Keep the authority from the issued URL
|
||||
// while routing the request to the random Quarkus test listener.
|
||||
return given().urlEncodingEnabled(false).header("Host", uri.getRawAuthority())
|
||||
.when().get(uri.getRawPath() + "?" + uri.getRawQuery())
|
||||
.then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static String insightsSource() {
|
||||
return """
|
||||
{"ExportDataSource": {"MessageInsightsDataSource":
|
||||
@@ -218,8 +228,7 @@ class SesExportJobV2IntegrationTest {
|
||||
|
||||
// The URL is Floci's own S3 emulation, so the object is fetched back through it.
|
||||
String url = job.path("ExportDestination.S3Url");
|
||||
String path = url.substring(url.indexOf('/', url.indexOf("//") + 2));
|
||||
String body = given().when().get(path).then().statusCode(200).extract().asString();
|
||||
String body = fetchExportCsv(url);
|
||||
assertTrue(body.startsWith("\"messageid\",\"sendtimestamp\""), body.substring(0, 60));
|
||||
assertTrue(body.contains("reader@example.com"), "the send should be in the export");
|
||||
}
|
||||
@@ -241,8 +250,7 @@ class SesExportJobV2IntegrationTest {
|
||||
String url = given().header("Authorization", AUTH)
|
||||
.when().get("/v2/email/export-jobs/" + jobId).then().statusCode(200)
|
||||
.extract().path("ExportDestination.S3Url");
|
||||
String path = url.substring(url.indexOf('/', url.indexOf("//") + 2));
|
||||
String body = given().when().get(path).then().statusCode(200).extract().asString();
|
||||
String body = fetchExportCsv(url);
|
||||
|
||||
assertTrue(body.startsWith("ISP,SEND_VOLUME\n"), body);
|
||||
assertTrue(body.contains("UNKNOWN_ISP,1.0000"), body);
|
||||
@@ -339,8 +347,7 @@ class SesExportJobV2IntegrationTest {
|
||||
String url = given().header("Authorization", AUTH)
|
||||
.when().get("/v2/email/export-jobs/" + jobId).then().statusCode(200)
|
||||
.extract().path("ExportDestination.S3Url");
|
||||
String path = url.substring(url.indexOf('/', url.indexOf("//") + 2));
|
||||
return given().when().get(path).then().statusCode(200).extract().asString();
|
||||
return fetchExportCsv(url);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -384,8 +391,7 @@ class SesExportJobV2IntegrationTest {
|
||||
String url = given().header("Authorization", AUTH)
|
||||
.when().get("/v2/email/export-jobs/" + jobId).then().statusCode(200)
|
||||
.extract().path("ExportDestination.S3Url");
|
||||
String path = url.substring(url.indexOf('/', url.indexOf("//") + 2));
|
||||
String body = given().when().get(path).then().statusCode(200).extract().asString();
|
||||
String body = fetchExportCsv(url);
|
||||
|
||||
assertTrue(body.startsWith("EMAIL_IDENTITY,SEND_VOLUME\n"), body);
|
||||
assertTrue(body.contains(SENDER), "a domain filter has to match the addresses inside it");
|
||||
|
||||
Reference in new issue
Block a user