Skip to content

Commit

Permalink
Merged in task/dspace-cris-2023_02_x/DSC-1897 (pull request DSpace#2663)
Browse files Browse the repository at this point in the history
Task/dspace cris 2023 02 x/DSC-1897

Approved-by: Andrea Bollini
  • Loading branch information
NikitaKr1vonosov authored and abollini committed Dec 13, 2024
2 parents d05f884 + 2ac3949 commit 731e0d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
Expand Down Expand Up @@ -82,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 @@ -115,6 +117,7 @@ public class S3BitStoreService extends BaseBitStoreService {
private String awsAccessKey;
private String awsSecretKey;
private String awsRegionName;
private String awsSessionToken;
private boolean useRelativePath;
private Integer maxConnections;
private Integer connectionTimeout;
Expand Down Expand Up @@ -270,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 @@ -282,6 +285,19 @@ protected static Supplier<AWSStaticCredentialsProvider> getAwsCredentialsSupplie
return () -> new AWSStaticCredentialsProvider(credentials);
}

protected static Supplier<AWSStaticCredentialsProvider> getBasicCredentialsSupplier(
String awsAccessKey, String awsSecretKey, String awsSessionToken
) {
BasicSessionCredentials credentials = new BasicSessionCredentials(awsAccessKey, awsSecretKey, awsSessionToken);
log.info(
"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);
}

protected static Regions getDefaultRegion() {
return Optional.ofNullable(new DefaultAwsRegionProviderChain().getRegion())
.filter(StringUtils::isNotBlank)
Expand Down Expand Up @@ -331,8 +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 = getAwsCredentialsSupplier(getAwsAccessKey(), getAwsSecretKey());
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 Expand Up @@ -669,6 +692,14 @@ public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getAwsSessionToken() {
return awsSessionToken;
}

public void setAwsSessionToken(String awsSessionToken) {
this.awsSessionToken = awsSessionToken;
}

/**
* Contains a command-line testing tool. Expects arguments:
* -a accessKey -s secretKey -f assetFileName
Expand Down
2 changes: 2 additions & 0 deletions dspace/config/modules/assetstore.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ assetstore.s3.connectionTimeout =
# If this property is set, changes the endpoint of the S3 service
assetstore.s3.endpoint =

# session token
assetstore.s3.awsToken =
1 change: 1 addition & 0 deletions dspace/config/spring/api/bitstore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<!-- AWS Security credentials, with policies for specified bucket -->
<property name="awsAccessKey" value="${assetstore.s3.awsAccessKey}"/>
<property name="awsSecretKey" value="${assetstore.s3.awsSecretKey}"/>
<property name="awsSessionToken" value="${assetstore.s3.awsToken}"/>
<property name="useRelativePath" value="${assetstore.s3.useRelativePath}"/>

<!-- S3 bucket name to store assets in. example: longsight-dspace-auk -->
Expand Down

0 comments on commit 731e0d9

Please sign in to comment.