Skip to content

Commit

Permalink
[DSC-1897] Fixes awsCredentialSupplier creation for awsSessionToken
Browse files Browse the repository at this point in the history
  • Loading branch information
vins01-4science committed Nov 22, 2024
1 parent 230ca94 commit 2ac3949
Showing 1 changed file with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
public class S3BitStoreService extends BaseBitStoreService {
protected static final String DEFAULT_BUCKET_PREFIX = "dspace-asset-";
protected static final Gson GSON = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
public static final String REGEX_SECRET = "^(.{3})(.*)(.{3})$";
// Prefix indicating a registered bitstream
protected final String REGISTERED_FLAG = "-R";
/**
Expand Down Expand Up @@ -272,8 +273,8 @@ protected static Supplier<AWSStaticCredentialsProvider> getAwsCredentialsSupplie
BasicAWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
log.info(
"AmazonS3Client credentials - accessKey: {}, secretKey: {}",
credentials.getAWSAccessKeyId().replaceFirst("^(.{3})(.*)(.{3})$", "$1***$3"),
credentials.getAWSSecretKey().replaceFirst("^(.{3})(.*)(.{3})$", "$1***$3")
credentials.getAWSAccessKeyId().replaceFirst(REGEX_SECRET, "$1***$3"),
credentials.getAWSSecretKey().replaceFirst(REGEX_SECRET, "$1***$3")
);
return getAwsCredentialsSupplier(credentials);
}
Expand All @@ -289,9 +290,10 @@ protected static Supplier<AWSStaticCredentialsProvider> getBasicCredentialsSuppl
) {
BasicSessionCredentials credentials = new BasicSessionCredentials(awsAccessKey, awsSecretKey, awsSessionToken);
log.info(
"AmazonS3Client credentials - accessKey: {}, secretKey: {}",
credentials.getAWSAccessKeyId().replaceFirst("^(.{3})(.*)(.{3})$", "$1***$3"),
credentials.getAWSSecretKey().replaceFirst("^(.{3})(.*)(.{3})$", "$1***$3")
"AmazonS3Client credentials - accessKey: {}, secretKey: {}, awsSessionToken: {}",
credentials.getAWSAccessKeyId().replaceFirst(REGEX_SECRET, "$1***$3"),
credentials.getAWSSecretKey().replaceFirst(REGEX_SECRET, "$1***$3"),
credentials.getSessionToken().replaceFirst(REGEX_SECRET, "$1***$3")
);
return getAwsCredentialsSupplier(credentials);
}
Expand Down Expand Up @@ -345,9 +347,15 @@ public void init() throws IOException {
try {
Supplier<? extends AWSCredentialsProvider> awsCredentialsSupplier;
if (StringUtils.isNotBlank(getAwsAccessKey()) && StringUtils.isNotBlank(getAwsSecretKey())) {
log.warn("Use local defined S3 credentials");
awsCredentialsSupplier = getBasicCredentialsSupplier(getAwsAccessKey(), getAwsSecretKey(),
getAwsSessionToken());
if (StringUtils.isNotBlank(getAwsSessionToken())) {
log.warn("Use local S3 credentials with session token");
awsCredentialsSupplier =
getBasicCredentialsSupplier(getAwsAccessKey(), getAwsSecretKey(), getAwsSessionToken());
} else {
log.warn("Use local S3 credentials with access and secret keys");
awsCredentialsSupplier =
getAwsCredentialsSupplier(getAwsAccessKey(), getAwsSecretKey());
}
} else {
log.info("Use an IAM role or aws environment credentials");
awsCredentialsSupplier = DefaultAWSCredentialsProviderChain::new;
Expand Down

0 comments on commit 2ac3949

Please sign in to comment.