mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(redshift): read S3 as the access keys a COPY names (#7334)
This commit is contained in:
1 parent
b0721f9795
commit
a93de121ae
11 files changed
+541
-45
No files matched your search
+67
-12
@@ -20,6 +20,15 @@ public final class CopyStatementParser {
|
||||
boolean defaultIamRoleRequested();
|
||||
}
|
||||
|
||||
/** Access keys a COPY names, as {@code CREDENTIALS 'aws_access_key_id=...'} or {@code ACCESS_KEY_ID}. */
|
||||
public record AccessKeys(String accessKeyId, String secretAccessKey, String sessionToken) {
|
||||
// Keeps the secret and token out of logs and error messages that print the statement
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AccessKeys[accessKeyId=" + accessKeyId + "]";
|
||||
}
|
||||
}
|
||||
|
||||
/** COPY options that change field content; {@link #NONE} keeps the plain byte-copy path. */
|
||||
public record CopyTransforms(
|
||||
boolean blanksAsNull,
|
||||
@@ -99,7 +108,8 @@ public final class CopyStatementParser {
|
||||
boolean jsonAutoIgnoreCase,
|
||||
boolean manifest,
|
||||
CopyTransforms transforms,
|
||||
boolean defaultIamRoleRequested) implements S3Statement {
|
||||
boolean defaultIamRoleRequested,
|
||||
AccessKeys accessKeys) implements S3Statement {
|
||||
|
||||
public S3CopyFrom {
|
||||
if (transforms == null) {
|
||||
@@ -107,6 +117,27 @@ public final class CopyStatementParser {
|
||||
}
|
||||
}
|
||||
|
||||
public S3CopyFrom(
|
||||
String targetTable,
|
||||
List<String> columns,
|
||||
String bucket,
|
||||
String keyOrPrefix,
|
||||
String delimiter,
|
||||
int headerLines,
|
||||
boolean gzip,
|
||||
boolean csv,
|
||||
String nullAs,
|
||||
String iamRoleArn,
|
||||
boolean jsonAuto,
|
||||
boolean jsonAutoIgnoreCase,
|
||||
boolean manifest,
|
||||
CopyTransforms transforms,
|
||||
boolean defaultIamRoleRequested) {
|
||||
this(targetTable, columns, bucket, keyOrPrefix, delimiter, headerLines, gzip, csv,
|
||||
nullAs, iamRoleArn, jsonAuto, jsonAutoIgnoreCase, manifest, transforms,
|
||||
defaultIamRoleRequested, null);
|
||||
}
|
||||
|
||||
public S3CopyFrom(
|
||||
String targetTable,
|
||||
List<String> columns,
|
||||
@@ -333,9 +364,11 @@ public final class CopyStatementParser {
|
||||
private static final Pattern CREDENTIALS_ROLE = Pattern.compile(
|
||||
"(?i)(?:^|;)\\s*aws_iam_role\\s*=\\s*([^;\\s]+)");
|
||||
private static final Pattern CREDENTIALS_KEY_ID = Pattern.compile(
|
||||
"(?i)(?:^|;)\\s*aws_access_key_id\\s*=\\s*[^;\\s]+");
|
||||
"(?i)(?:^|;)\\s*aws_access_key_id\\s*=\\s*([^;\\s]+)");
|
||||
private static final Pattern CREDENTIALS_SECRET = Pattern.compile(
|
||||
"(?i)(?:^|;)\\s*aws_secret_access_key\\s*=\\s*[^;\\s]+");
|
||||
"(?i)(?:^|;)\\s*aws_secret_access_key\\s*=\\s*([^;\\s]+)");
|
||||
private static final Pattern CREDENTIALS_TOKEN = Pattern.compile(
|
||||
"(?i)(?:^|;)\\s*token\\s*=\\s*([^;\\s]+)");
|
||||
private static final Pattern IGNOREHEADER_PATTERN = Pattern.compile(
|
||||
"(?i)\\bIGNOREHEADER\\s+(?:AS\\s+)?(\\d+)\\b");
|
||||
private static final Pattern HEADER_PATTERN = Pattern.compile("(?i)\\bHEADER\\b");
|
||||
@@ -895,13 +928,14 @@ public final class CopyStatementParser {
|
||||
}
|
||||
|
||||
return new S3CopyFrom(table, columns, bucket, keyOrPrefix, delimiter, headerLines, gzip, csv,
|
||||
nullAs, iamRoleArn, jsonAuto, jsonAutoIgnoreCase, manifest, transforms, auth.defaultIamRoleRequested);
|
||||
nullAs, iamRoleArn, jsonAuto, jsonAutoIgnoreCase, manifest, transforms, auth.defaultIamRoleRequested,
|
||||
auth.accessKeys());
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the S3 authorization and region clauses shared by COPY and UNLOAD. Key material is
|
||||
* checked for shape only and never stored: the simulator reads S3 as the cluster role or as an
|
||||
* unsigned request.
|
||||
* Collects the S3 authorization and region clauses shared by COPY and UNLOAD. COPY keeps the
|
||||
* keys it names so the simulator reads S3 as that principal; UNLOAD ignores them and writes as
|
||||
* the cluster role or as an unsigned request.
|
||||
*/
|
||||
private static final class AuthClauses {
|
||||
static final int NO_MATCH = -1;
|
||||
@@ -926,6 +960,9 @@ public final class CopyStatementParser {
|
||||
private boolean seenRegion;
|
||||
private boolean credentialsHaveKeys;
|
||||
private boolean credentialsMalformed;
|
||||
private String clauseAccessKeyId;
|
||||
private String clauseSecretAccessKey;
|
||||
private String clauseSessionToken;
|
||||
|
||||
AuthClauses(String options, String defaultIamRoleArn) {
|
||||
this.defaultIamRoleArn = defaultIamRoleArn;
|
||||
@@ -960,21 +997,24 @@ public final class CopyStatementParser {
|
||||
return REJECT;
|
||||
}
|
||||
seenAccessKeyId = true;
|
||||
return isBlank(extractNullValue(accessKeyId)) ? REJECT : accessKeyId.end();
|
||||
clauseAccessKeyId = extractNullValue(accessKeyId);
|
||||
return isBlank(clauseAccessKeyId) ? REJECT : accessKeyId.end();
|
||||
}
|
||||
if (matchClause(secretAccessKey, offset, end)) {
|
||||
if (seenSecretAccessKey) {
|
||||
return REJECT;
|
||||
}
|
||||
seenSecretAccessKey = true;
|
||||
return isBlank(extractNullValue(secretAccessKey)) ? REJECT : secretAccessKey.end();
|
||||
clauseSecretAccessKey = extractNullValue(secretAccessKey);
|
||||
return isBlank(clauseSecretAccessKey) ? REJECT : secretAccessKey.end();
|
||||
}
|
||||
if (matchClause(sessionToken, offset, end)) {
|
||||
if (seenSessionToken) {
|
||||
return REJECT;
|
||||
}
|
||||
seenSessionToken = true;
|
||||
return isBlank(extractNullValue(sessionToken)) ? REJECT : sessionToken.end();
|
||||
clauseSessionToken = extractNullValue(sessionToken);
|
||||
return isBlank(clauseSessionToken) ? REJECT : sessionToken.end();
|
||||
}
|
||||
if (matchClause(region, offset, end)) {
|
||||
if (seenRegion) {
|
||||
@@ -991,10 +1031,25 @@ public final class CopyStatementParser {
|
||||
if (role.find()) {
|
||||
credentialsRoleArn = role.group(1);
|
||||
}
|
||||
boolean hasId = CREDENTIALS_KEY_ID.matcher(value).find();
|
||||
boolean hasSecret = CREDENTIALS_SECRET.matcher(value).find();
|
||||
Matcher id = CREDENTIALS_KEY_ID.matcher(value);
|
||||
Matcher secret = CREDENTIALS_SECRET.matcher(value);
|
||||
Matcher token = CREDENTIALS_TOKEN.matcher(value);
|
||||
boolean hasId = id.find();
|
||||
boolean hasSecret = secret.find();
|
||||
credentialsHaveKeys = hasId && hasSecret;
|
||||
credentialsMalformed = hasId != hasSecret;
|
||||
if (credentialsHaveKeys) {
|
||||
clauseAccessKeyId = id.group(1);
|
||||
clauseSecretAccessKey = secret.group(1);
|
||||
clauseSessionToken = token.find() ? token.group(1) : null;
|
||||
}
|
||||
}
|
||||
|
||||
AccessKeys accessKeys() {
|
||||
if (clauseAccessKeyId == null || clauseSecretAccessKey == null) {
|
||||
return null;
|
||||
}
|
||||
return new AccessKeys(clauseAccessKeyId, clauseSecretAccessKey, clauseSessionToken);
|
||||
}
|
||||
|
||||
boolean consistent() {
|
||||
|
||||
+69
-2
@@ -11,9 +11,12 @@ import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -121,8 +124,72 @@ public final class RedshiftRoleAccess {
|
||||
return new RoleSession(accessKeyId, secretAccessKey, sessionToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts the keys a {@code COPY} names only when IAM knows them: the secret must match and a
|
||||
* temporary key must come with its issued token, so an expired or invented key fails here
|
||||
* instead of reading S3 anonymously. The session is not registered, so there is nothing to release.
|
||||
*/
|
||||
public static RoleSession resolveKeySession(CopyStatementParser.AccessKeys keys, IamService iamService) {
|
||||
Optional<String> secret = iamService.findSecretKey(keys.accessKeyId(), keys.sessionToken());
|
||||
if (secret.isEmpty()) {
|
||||
throw new S3CopySimulator.S3TransferException(SQLSTATE_INTERNAL,
|
||||
"S3ServiceException: The AWS Access Key Id you provided does not exist in our records, "
|
||||
+ "or the session has expired", null);
|
||||
}
|
||||
boolean secretMatches = MessageDigest.isEqual(
|
||||
secret.get().getBytes(StandardCharsets.UTF_8), keys.secretAccessKey().getBytes(StandardCharsets.UTF_8));
|
||||
if (!secretMatches) {
|
||||
throw new S3CopySimulator.S3TransferException(SQLSTATE_INTERNAL,
|
||||
"S3ServiceException: The request signature we calculated does not match the signature "
|
||||
+ "you provided", null);
|
||||
}
|
||||
return new RoleSession(keys.accessKeyId(), keys.secretAccessKey(), keys.sessionToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the identity policy of whoever {@code session} belongs to: the role when COPY named
|
||||
* one, otherwise the principal behind the access key.
|
||||
*/
|
||||
public static void authorizeSessionAction(S3Service s3, IamService iamService, RoleSession session,
|
||||
String roleArn, String action, String resourceArn) {
|
||||
authorizeSessionAction(s3, iamService, session, roleArn, action, resourceArn, Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param conditionContext the condition keys the request itself offers, such as {@code s3:prefix}; for an
|
||||
* access key the keys that say who is calling are added, so a policy that matches
|
||||
* on {@code aws:PrincipalArn} sees the same principal as on a signed request
|
||||
*/
|
||||
public static void authorizeSessionAction(S3Service s3, IamService iamService, RoleSession session,
|
||||
String roleArn, String action, String resourceArn,
|
||||
Map<String, List<String>> conditionContext) {
|
||||
if (roleArn != null) {
|
||||
authorizeRoleAction(s3, iamService, roleArn, action, resourceArn, conditionContext);
|
||||
return;
|
||||
}
|
||||
if (!s3.isAuthEnforced()) {
|
||||
return;
|
||||
}
|
||||
CallerContext caller = iamService.resolveCallerContext(session.accessKeyId());
|
||||
if (caller == null) {
|
||||
throw new S3CopySimulator.S3TransferException(SQLSTATE_INSUFFICIENT_PRIVILEGE,
|
||||
"S3 access denied for access key '" + session.accessKeyId() + "': " + action + " on "
|
||||
+ resourceArn, null);
|
||||
}
|
||||
Map<String, List<String>> context = new HashMap<>(conditionContext);
|
||||
iamService.resolveCallerArns(session.accessKeyId())
|
||||
.ifPresent(callerArns -> callerArns.putPrincipalKeys(context));
|
||||
IamPolicyEvaluator.SimulationDecision decision =
|
||||
ROLE_POLICY_EVALUATOR.simulatePrincipalPolicy(caller, action, resourceArn, context);
|
||||
if (decision != IamPolicyEvaluator.SimulationDecision.ALLOWED) {
|
||||
throw new S3CopySimulator.S3TransferException(SQLSTATE_INSUFFICIENT_PRIVILEGE,
|
||||
"S3 access denied for access key '" + session.accessKeyId() + "': " + action + " on "
|
||||
+ resourceArn, null);
|
||||
}
|
||||
}
|
||||
|
||||
public static void releaseRoleSession(RoleSession roleSession, String iamRoleArn, IamService iamService) {
|
||||
if (roleSession == null) {
|
||||
if (roleSession == null || iamRoleArn == null) {
|
||||
return;
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(iamRoleArn);
|
||||
@@ -169,7 +236,7 @@ public final class RedshiftRoleAccess {
|
||||
authorizeRoleAction(s3, iamService, roleArn, "s3:ListBucket", bucketArn(roleArn, bucket),
|
||||
Map.of("s3:prefix", List.of(prefix == null ? "" : prefix)));
|
||||
try {
|
||||
s3.authorizeSignedListBucket(roleSession.accessKeyId(), roleSession.sessionToken(), bucket);
|
||||
s3.authorizeSignedListBucket(roleSession.accessKeyId(), roleSession.sessionToken(), bucket, prefix);
|
||||
} catch (AwsException e) {
|
||||
throw signedAccessFailure(e, "s3://" + bucket);
|
||||
}
|
||||
|
||||
+84
-14
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.redshift.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.s3.model.PutObjectOptions;
|
||||
@@ -22,6 +24,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.IntConsumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
@@ -115,25 +118,32 @@ public final class S3CopySimulator {
|
||||
|
||||
static CopyInput prepareCopy(CopyStatementParser.S3CopyFrom spec, S3Service s3, IamService iamService,
|
||||
String clusterAccountId, List<String> associatedRoleArns) {
|
||||
return asKeyAccount(spec, s3, iamService,
|
||||
() -> prepareCopyScoped(spec, s3, iamService, clusterAccountId, associatedRoleArns));
|
||||
}
|
||||
|
||||
private static CopyInput prepareCopyScoped(CopyStatementParser.S3CopyFrom spec, S3Service s3,
|
||||
IamService iamService, String clusterAccountId,
|
||||
List<String> associatedRoleArns) {
|
||||
requireDefaultIamRole(spec);
|
||||
RedshiftRoleAccess.RoleSession roleSession = spec.iamRoleArn() != null
|
||||
? RedshiftRoleAccess.resolveRoleSession(spec.iamRoleArn(), iamService, clusterAccountId, associatedRoleArns)
|
||||
: null;
|
||||
RedshiftRoleAccess.RoleSession roleSession = resolveCopySession(spec, s3, iamService, clusterAccountId,
|
||||
associatedRoleArns);
|
||||
try {
|
||||
try {
|
||||
if (spec.manifest()) {
|
||||
if (roleSession != null) {
|
||||
RedshiftRoleAccess.authorizeRoleAction(s3, iamService, spec.iamRoleArn(), "s3:GetObject",
|
||||
RedshiftRoleAccess.objectArn(spec.iamRoleArn(), spec.bucket(), spec.keyOrPrefix()));
|
||||
RedshiftRoleAccess.authorizeSessionAction(s3, iamService, roleSession, spec.iamRoleArn(),
|
||||
"s3:GetObject", copyObjectArn(spec, s3, spec.keyOrPrefix()));
|
||||
s3.authorizeSignedGetObject(roleSession.accessKeyId(), roleSession.sessionToken(), spec.bucket(), spec.keyOrPrefix());
|
||||
} else {
|
||||
s3.authorizeAnonymousGetObject(spec.bucket(), spec.keyOrPrefix());
|
||||
}
|
||||
} else {
|
||||
if (roleSession != null) {
|
||||
RedshiftRoleAccess.authorizeRoleAction(s3, iamService, spec.iamRoleArn(), "s3:ListBucket",
|
||||
RedshiftRoleAccess.bucketArn(spec.iamRoleArn(), spec.bucket()));
|
||||
s3.authorizeSignedListBucket(roleSession.accessKeyId(), roleSession.sessionToken(), spec.bucket());
|
||||
RedshiftRoleAccess.authorizeSessionAction(s3, iamService, roleSession, spec.iamRoleArn(),
|
||||
"s3:ListBucket", copyBucketArn(spec, s3), Map.of("s3:prefix", List.of(spec.keyOrPrefix())));
|
||||
s3.authorizeSignedListBucket(roleSession.accessKeyId(), roleSession.sessionToken(), spec.bucket(),
|
||||
spec.keyOrPrefix());
|
||||
} else {
|
||||
s3.authorizeAnonymousListBucket(spec.bucket());
|
||||
}
|
||||
@@ -158,8 +168,8 @@ public final class S3CopySimulator {
|
||||
try {
|
||||
for (String key : keys) {
|
||||
if (roleSession != null) {
|
||||
RedshiftRoleAccess.authorizeRoleAction(s3, iamService, spec.iamRoleArn(), "s3:GetObject",
|
||||
RedshiftRoleAccess.objectArn(spec.iamRoleArn(), spec.bucket(), key));
|
||||
RedshiftRoleAccess.authorizeSessionAction(s3, iamService, roleSession, spec.iamRoleArn(),
|
||||
"s3:GetObject", copyObjectArn(spec, s3, key));
|
||||
s3.authorizeSignedGetObject(roleSession.accessKeyId(), roleSession.sessionToken(), spec.bucket(), key);
|
||||
} else {
|
||||
s3.authorizeAnonymousGetObject(spec.bucket(), key);
|
||||
@@ -176,6 +186,55 @@ public final class S3CopySimulator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The session COPY reads S3 as: the role's, or the one the statement's own access keys name. Keys
|
||||
* are only checked when S3 authorization is on, since with it off every read is allowed and a
|
||||
* statement may carry placeholder keys.
|
||||
*/
|
||||
private static RedshiftRoleAccess.RoleSession resolveCopySession(
|
||||
CopyStatementParser.S3CopyFrom spec, S3Service s3, IamService iamService,
|
||||
String clusterAccountId, List<String> associatedRoleArns) {
|
||||
if (spec.iamRoleArn() != null) {
|
||||
return RedshiftRoleAccess.resolveRoleSession(spec.iamRoleArn(), iamService, clusterAccountId,
|
||||
associatedRoleArns);
|
||||
}
|
||||
if (spec.accessKeys() != null && s3.isAuthEnforced()) {
|
||||
return RedshiftRoleAccess.resolveKeySession(spec.accessKeys(), iamService);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A role's ARNs live in its partition; an access key has none, so the bucket's own region decides. */
|
||||
private static String copyBucketArn(CopyStatementParser.S3CopyFrom spec, S3Service s3) {
|
||||
if (spec.iamRoleArn() != null) {
|
||||
return RedshiftRoleAccess.bucketArn(spec.iamRoleArn(), spec.bucket());
|
||||
}
|
||||
return AwsArnUtils.Arn.global(s3.bucketPartition(spec.bucket()), "s3", "",
|
||||
spec.bucket()).toString();
|
||||
}
|
||||
|
||||
private static String copyObjectArn(CopyStatementParser.S3CopyFrom spec, S3Service s3, String key) {
|
||||
return copyBucketArn(spec, s3) + "/" + key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs {@code body} in the account that owns the statement's access key, since IAM and S3 look a key up in
|
||||
* the current account and the proxy's connection thread carries none. A role-based or keyless COPY, or one
|
||||
* whose keys are ignored because S3 authorization is off, runs as is.
|
||||
*/
|
||||
private static <T> T asKeyAccount(CopyStatementParser.S3CopyFrom spec, S3Service s3, IamService iamService,
|
||||
Supplier<T> body) {
|
||||
return RequestScopes.callAs(keyAccount(spec, s3, iamService), body);
|
||||
}
|
||||
|
||||
/** Null when no key is in play, which {@link RequestScopes} reads as "stay in the current account". */
|
||||
private static String keyAccount(CopyStatementParser.S3CopyFrom spec, S3Service s3, IamService iamService) {
|
||||
if (spec.iamRoleArn() != null || spec.accessKeys() == null || !s3.isAuthEnforced()) {
|
||||
return null;
|
||||
}
|
||||
return iamService.findAccountForAccessKeyId(spec.accessKeys().accessKeyId()).orElse(null);
|
||||
}
|
||||
|
||||
static String copyBackendSql(CopyStatementParser.S3CopyFrom spec) {
|
||||
return fabricateCopy(spec, null);
|
||||
}
|
||||
@@ -200,8 +259,19 @@ public final class S3CopySimulator {
|
||||
static void streamCopyInput(CopyInput input, List<String> discoveredColumns,
|
||||
List<Integer> columnMaxBytes, List<CopyRecordTransformer.ColumnKind> columnKinds,
|
||||
OutputStream backendOut) throws IOException {
|
||||
streamObjects(input.spec(), discoveredColumns, columnMaxBytes, columnKinds, input.s3(), input.iamService(),
|
||||
input.roleSession(), input.keys(), backendOut);
|
||||
String account = keyAccount(input.spec(), input.s3(), input.iamService());
|
||||
try {
|
||||
RequestScopes.callAsChecked(account, () -> {
|
||||
streamObjects(input.spec(), discoveredColumns, columnMaxBytes, columnKinds, input.s3(),
|
||||
input.iamService(), input.roleSession(), input.keys(), backendOut);
|
||||
return null;
|
||||
});
|
||||
} catch (IOException | RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// The body throws only IOException and unchecked exceptions, so this is unreachable.
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static void releaseCopySession(CopyInput input) {
|
||||
@@ -504,8 +574,8 @@ public final class S3CopySimulator {
|
||||
byte[] buffer = new byte[CHUNK];
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
if (roleSession != null) {
|
||||
RedshiftRoleAccess.authorizeRoleAction(s3, iamService, spec.iamRoleArn(), "s3:GetObject",
|
||||
RedshiftRoleAccess.objectArn(spec.iamRoleArn(), spec.bucket(), keys.get(i)));
|
||||
RedshiftRoleAccess.authorizeSessionAction(s3, iamService, roleSession, spec.iamRoleArn(),
|
||||
"s3:GetObject", copyObjectArn(spec, s3, keys.get(i)));
|
||||
s3.authorizeSignedGetObject(roleSession.accessKeyId(), roleSession.sessionToken(), spec.bucket(), keys.get(i));
|
||||
} else {
|
||||
s3.authorizeAnonymousGetObject(spec.bucket(), keys.get(i));
|
||||
|
||||
@@ -1236,6 +1236,16 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
authorizeListBucket(bucketName, new RequestAuthorization(true, accessKeyId, sessionToken));
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorize a signed {@code s3:ListBucket} that lists under {@code prefix}. The bucket policy sees
|
||||
* {@code s3:prefix} as a real listing offers it, so a statement conditioned on the prefix matches
|
||||
* instead of failing on the absent key.
|
||||
*/
|
||||
public void authorizeSignedListBucket(String accessKeyId, String sessionToken, String bucketName, String prefix) {
|
||||
authorizeBucketRead(bucketName, "s3:ListBucket", new RequestAuthorization(true, accessKeyId, sessionToken),
|
||||
Map.of("s3:prefix", List.of(prefix == null ? "" : prefix)));
|
||||
}
|
||||
|
||||
/** Authorize a signed {@code s3:DeleteObject}; see {@link #authorizeSignedGetObject}. */
|
||||
public void authorizeSignedDeleteObject(String accessKeyId, String sessionToken, String bucketName, String key) {
|
||||
authorizeDeleteObject(bucketName, key, null, new RequestAuthorization(true, accessKeyId, sessionToken));
|
||||
@@ -1307,8 +1317,13 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
void authorizeBucketRead(String bucketName, String action, RequestAuthorization authorization) {
|
||||
authorizeBucketRead(bucketName, action, authorization, null);
|
||||
}
|
||||
|
||||
void authorizeBucketRead(String bucketName, String action, RequestAuthorization authorization,
|
||||
Map<String, List<String>> requestConditions) {
|
||||
String bucketArn = S3PublicAccessEvaluator.bucketArn(bucketPartition(bucketName), bucketName);
|
||||
authorizeS3Read(bucketName, null, null, action, bucketArn, authorization);
|
||||
authorizeS3Read(bucketName, null, null, action, bucketArn, authorization, requestConditions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1369,7 +1384,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
void authorizeObjectRead(String bucketName, String key, String versionId, String action, RequestAuthorization authorization) {
|
||||
String objectArn = S3PublicAccessEvaluator.objectArn(bucketPartition(bucketName), bucketName, key);
|
||||
authorizeS3Read(bucketName, key, versionId, S3ObjectActions.forVersion(action, versionId), objectArn,
|
||||
authorization);
|
||||
authorization, null);
|
||||
}
|
||||
|
||||
void authorizePutObject(String bucketName, String key, RequestAuthorization authorization) {
|
||||
@@ -1488,7 +1503,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return enforceAuth;
|
||||
}
|
||||
|
||||
private void authorizeS3Read(String bucketName, String key, String versionId, String action, String resourceArn, RequestAuthorization authorization) {
|
||||
private void authorizeS3Read(String bucketName, String key, String versionId, String action, String resourceArn,
|
||||
RequestAuthorization authorization, Map<String, List<String>> requestConditions) {
|
||||
if (!enforceAuth) {
|
||||
return;
|
||||
}
|
||||
@@ -1498,7 +1514,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
: RequestAuthorization.unsigned();
|
||||
|
||||
if (requestAuthorization.signed()) {
|
||||
authorizeSignedBucketPolicy(bucketName, key, action, resourceArn, requestAuthorization, null);
|
||||
authorizeSignedBucketPolicy(bucketName, key, action, resourceArn, requestAuthorization, requestConditions);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -7505,7 +7521,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
* partition can still reach the bucket, and a policy naming it must match as written. A
|
||||
* bucket that does not exist yet takes the request's partition, where it would be created.
|
||||
*/
|
||||
String bucketPartition(String bucketName) {
|
||||
public String bucketPartition(String bucketName) {
|
||||
return resolveBucket(bucketName)
|
||||
.map(Bucket::getRegion)
|
||||
.map(AwsRegions::partitionFor)
|
||||
|
||||
Reference in new issue
Block a user