mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(auth,s3): resolve account context from X-Amz-Credential query param in presigned URLs (#1413)
* feat(auth): parse X-Amz-Credential from query params in AccountResolver and RegionResolver * fix(auth): fall back to X-Amz-Credential query param in AccountContextFilter * fix(s3): use request-scoped account ID in PreSignedUrlGenerator credential string * test(s3): add presigned URL account isolation integration test
This commit is contained in:
1 parent
dace94fa80
commit
b2aea42072
9 files changed
+466
-7
No files matched your search
@@ -10,7 +10,8 @@ import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
/**
|
||||
* Populates {@link RequestContext} with the account ID and region derived from
|
||||
* the incoming AWS Authorization header. Runs at AUTHENTICATION priority so that
|
||||
* the incoming AWS Authorization header or, for presigned URL requests, the
|
||||
* X-Amz-Credential query parameter. Runs at AUTHENTICATION priority so that
|
||||
* downstream filters (e.g. IAM enforcement) can rely on the context being set.
|
||||
*/
|
||||
@Provider
|
||||
@@ -34,7 +35,18 @@ public class AccountContextFilter implements ContainerRequestFilter {
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
String auth = ctx.getHeaderString("Authorization");
|
||||
requestContext.setAccountId(accountResolver.resolve(auth));
|
||||
requestContext.setRegion(regionResolver.resolveRegionFromAuth(auth));
|
||||
if (auth != null && !auth.isEmpty()) {
|
||||
requestContext.setAccountId(accountResolver.resolve(auth));
|
||||
requestContext.setRegion(regionResolver.resolveRegionFromAuth(auth));
|
||||
} else {
|
||||
String credential = ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential");
|
||||
if (credential != null && !credential.isEmpty()) {
|
||||
requestContext.setAccountId(accountResolver.resolveFromPresignedCredential(credential));
|
||||
requestContext.setRegion(regionResolver.resolveRegionFromPresignedCredential(credential));
|
||||
} else {
|
||||
requestContext.setAccountId(accountResolver.resolve(null));
|
||||
requestContext.setRegion(regionResolver.resolveRegionFromAuth(null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,10 @@ public class AccountResolver {
|
||||
this.defaultAccountId = config.defaultAccountId();
|
||||
}
|
||||
|
||||
public AccountResolver(String defaultAccountId) {
|
||||
this.defaultAccountId = defaultAccountId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the account ID for the given Authorization header.
|
||||
* When the access key ID is exactly 12 digits it is used directly as the account ID,
|
||||
@@ -44,4 +48,25 @@ public class AccountResolver {
|
||||
Matcher m = AKID_PATTERN.matcher(authorizationHeader);
|
||||
return m.find() ? m.group(1) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the account ID from an X-Amz-Credential value found in
|
||||
* presigned URL query parameters.
|
||||
* Format: accessKeyID/date/region/service/aws4_request
|
||||
* When the access key ID is exactly 12 digits it is used directly
|
||||
* as the account ID, matching LocalStack's multi-account convention.
|
||||
* Any other key format or missing/malformed value falls back to
|
||||
* the configured default account.
|
||||
*/
|
||||
public String resolveFromPresignedCredential(String credentialValue) {
|
||||
if (credentialValue == null || credentialValue.isEmpty()) {
|
||||
return defaultAccountId;
|
||||
}
|
||||
String[] parts = credentialValue.split("/");
|
||||
String akid = parts[0];
|
||||
if (akid.matches("\\d{12}")) {
|
||||
return akid;
|
||||
}
|
||||
return defaultAccountId;
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,24 @@ public class RegionResolver {
|
||||
return matcher.find() ? matcher.group(1) : defaultRegion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the region from an X-Amz-Credential value found in
|
||||
* presigned URL query parameters.
|
||||
* Format: accessKeyID/date/region/service/aws4_request
|
||||
* Falls back to the configured default region if the credential value
|
||||
* is null, empty, or does not contain enough segments.
|
||||
*/
|
||||
public String resolveRegionFromPresignedCredential(String credentialValue) {
|
||||
if (credentialValue == null || credentialValue.isEmpty()) {
|
||||
return defaultRegion;
|
||||
}
|
||||
String[] parts = credentialValue.split("/");
|
||||
if (parts.length >= 3) {
|
||||
return parts[2];
|
||||
}
|
||||
return defaultRegion;
|
||||
}
|
||||
|
||||
public String getDefaultRegion() {
|
||||
return defaultRegion;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.context.ContextNotActiveException;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
@@ -22,29 +25,54 @@ public class PreSignedUrlGenerator {
|
||||
private final int defaultExpiry;
|
||||
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;
|
||||
|
||||
@Inject
|
||||
public PreSignedUrlGenerator(EmulatorConfig config) {
|
||||
this(config.auth().presignSecret(),
|
||||
config.services().s3().defaultPresignExpirySeconds(),
|
||||
config.auth().validateSignatures(),
|
||||
config.defaultRegion());
|
||||
config.defaultRegion(),
|
||||
config.defaultAccountId());
|
||||
}
|
||||
|
||||
/** Package-private constructor for testing. */
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry) {
|
||||
this(secret, defaultExpiry, false, "us-east-1");
|
||||
this(secret, defaultExpiry, false, "us-east-1", "000000000000");
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures) {
|
||||
this(secret, defaultExpiry, validateSignatures, "us-east-1");
|
||||
this(secret, defaultExpiry, validateSignatures, "us-east-1", "000000000000");
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion) {
|
||||
this(secret, defaultExpiry, validateSignatures, defaultRegion, "000000000000");
|
||||
}
|
||||
|
||||
PreSignedUrlGenerator(String secret, int defaultExpiry, boolean validateSignatures, String defaultRegion, String defaultAccountId) {
|
||||
this.secret = secret;
|
||||
this.defaultExpiry = defaultExpiry;
|
||||
this.validateSignatures = validateSignatures;
|
||||
this.defaultRegion = defaultRegion;
|
||||
this.defaultAccountId = defaultAccountId;
|
||||
}
|
||||
|
||||
private String resolveAccessKeyId() {
|
||||
if (requestContextInstance != null) {
|
||||
try {
|
||||
String accountId = requestContextInstance.get().getAccountId();
|
||||
if (accountId != null) {
|
||||
return accountId;
|
||||
}
|
||||
} catch (ContextNotActiveException ignored) {
|
||||
// outside request scope — fall through to default
|
||||
}
|
||||
}
|
||||
return defaultAccountId;
|
||||
}
|
||||
|
||||
public boolean shouldValidateSignatures() {
|
||||
@@ -55,7 +83,7 @@ public class PreSignedUrlGenerator {
|
||||
String method, int expiresSeconds) {
|
||||
int expiry = expiresSeconds > 0 ? expiresSeconds : defaultExpiry;
|
||||
String amzDate = AMZ_DATE_FORMAT.format(Instant.now());
|
||||
String credential = "AKIAIOSFODNN7EXAMPLE/" + amzDate.substring(0, 8) + "/" + defaultRegion + "/s3/aws4_request";
|
||||
String credential = resolveAccessKeyId() + "/" + amzDate.substring(0, 8) + "/" + defaultRegion + "/s3/aws4_request";
|
||||
|
||||
String signature = computeSignature(method, bucket, key, amzDate, expiry);
|
||||
|
||||
|
||||
Reference in new issue
Block a user