Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dynamodb] Allow multiple credentials sources and types #1660

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dynamodb/conf/AWSCredentials.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@
# http://aws.amazon.com/security-credentials
#accessKey =
#secretKey =

# The below parameter is optional, it is used for enhanced security, when
# the machine used for running YCSB can't be trusted with the actual account
# credentials and temporary credentials were generated using 'aws sts',
# this parameter should be set to the 'SessionToken' field of the Credentials
# object returned # from the sts command.
# for more information:
# https://docs.aws.amazon.com/cli/latest/reference/sts/get-session-token.html#get-session-token
#sessionToken =
8 changes: 5 additions & 3 deletions dynamodb/conf/dynamodb.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

## Mandatory parameters

# AWS credentials associated with your aws account.
#dynamodb.awsCredentialsFile = <path to AWSCredentials.properties>

# Primarykey of table 'usertable'
#dynamodb.primaryKey = <firstname>

Expand All @@ -30,6 +27,11 @@

## Optional parameters

# AWS credentials associated with your aws account. If it is not provided, the
# default system credentials will be searched according to the DynamoDB library
# rules
#dynamodb.awsCredentialsFile = <path to AWSCredentials.properties>

# The property "primaryKeyType" below specifies the type of primary key
# you have setup for the test table. There are two choices:
# - HASH (default)
Expand Down
33 changes: 23 additions & 10 deletions dynamodb/src/main/java/site/ycsb/db/DynamoDBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
Expand All @@ -29,9 +30,10 @@
import org.apache.log4j.Logger;
import site.ycsb.*;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
Expand Down Expand Up @@ -137,15 +139,26 @@ public void init() throws DBException {
dynamoDBBuilder.withRegion(this.region) :
dynamoDBBuilder.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(this.endpoint, this.region)
);
dynamoDB = dynamoDBBuilder
.withClientConfiguration(
new ClientConfiguration()
.withTcpKeepAlive(true)
.withMaxConnections(this.maxConnects)
)
.withCredentials(new AWSStaticCredentialsProvider(new PropertiesCredentials(new File(credentialsFile))))
.build();
.withClientConfiguration(
new ClientConfiguration()
.withTcpKeepAlive(true)
.withMaxConnections(this.maxConnects)
);

if (credentialsFile != null) {
Properties credentialProperties = new Properties();
credentialProperties.load(new FileInputStream(credentialsFile));
String accessKey = credentialProperties.getProperty("accessKey");
String secretKey = credentialProperties.getProperty("secretKey");
String sessionToken = credentialProperties.getProperty("sessionToken", null);
AWSStaticCredentialsProvider credentialsProvider = null ==sessionToken ?
new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)) :
new AWSStaticCredentialsProvider(new BasicSessionCredentials(accessKey, secretKey, sessionToken));
dynamoDBBuilder.withCredentials(credentialsProvider);
}
dynamoDB = dynamoDBBuilder.build();

primaryKeyName = primaryKey;
LOGGER.info("dynamodb connection created with " + this.endpoint);
} catch (Exception e1) {
Expand Down