Skip to content

Commit

Permalink
Optional credentials with latest AWS SDK, closes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
kimchy committed Jun 20, 2012
1 parent 97c776b commit 96a4e5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.3.10</version>
<version>1.3.11</version>
<scope>compile</scope>
<exclusions>
<!-- We need to exclude httpclient, since it pulls the wrong version -->
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/org/elasticsearch/cloud/aws/AwsEc2Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.*;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import org.elasticsearch.ElasticSearchException;
Expand Down Expand Up @@ -70,13 +71,6 @@ public synchronized AmazonEC2 client() {
String account = componentSettings.get("access_key", settings.get("cloud.account"));
String key = componentSettings.get("secret_key", settings.get("cloud.key"));

if (account == null) {
throw new ElasticSearchIllegalArgumentException("No aws access_key defined for ec2 discovery");
}
if (key == null) {
throw new ElasticSearchIllegalArgumentException("No aws secret_key defined for ec2 discovery");
}

String proxyHost = componentSettings.get("proxy_host");
if (proxyHost != null) {
String portString = componentSettings.get("proxy_port", "80");
Expand All @@ -89,7 +83,21 @@ public synchronized AmazonEC2 client() {
clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
}

this.client = new AmazonEC2Client(new BasicAWSCredentials(account, key), clientConfiguration);
AWSCredentialsProvider credentials;

if (account == null && key == null) {
credentials = new AWSCredentialsProviderChain(
new EnvironmentVariableCredentialsProvider(),
new SystemPropertiesCredentialsProvider(),
new InstanceProfileCredentialsProvider()
);
} else {
credentials = new AWSCredentialsProviderChain(
new StaticCredentialsProvider(new BasicAWSCredentials(account, key))
);
}

this.client = new AmazonEC2Client(credentials, clientConfiguration);

if (componentSettings.get("ec2.endpoint") != null) {
client.setEndpoint(componentSettings.get("ec2.endpoint"));
Expand Down
25 changes: 16 additions & 9 deletions src/main/java/org/elasticsearch/cloud/aws/AwsS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.*;
import com.amazonaws.internal.StaticCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import org.elasticsearch.ElasticSearchException;
Expand Down Expand Up @@ -62,13 +63,6 @@ public synchronized AmazonS3 client() {
String account = componentSettings.get("access_key", settings.get("cloud.account"));
String key = componentSettings.get("secret_key", settings.get("cloud.key"));

if (account == null) {
throw new ElasticSearchIllegalArgumentException("No s3 access_key defined for s3 gateway");
}
if (key == null) {
throw new ElasticSearchIllegalArgumentException("No s3 secret_key defined for s3 gateway");
}

String proxyHost = componentSettings.get("proxy_host");
if (proxyHost != null) {
String portString = componentSettings.get("proxy_port", "80");
Expand All @@ -81,7 +75,20 @@ public synchronized AmazonS3 client() {
clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
}

this.client = new AmazonS3Client(new BasicAWSCredentials(account, key), clientConfiguration);
AWSCredentialsProvider credentials;

if (account == null && key == null) {
credentials = new AWSCredentialsProviderChain(
new EnvironmentVariableCredentialsProvider(),
new SystemPropertiesCredentialsProvider(),
new InstanceProfileCredentialsProvider()
);
} else {
credentials = new AWSCredentialsProviderChain(
new StaticCredentialsProvider(new BasicAWSCredentials(account, key))
);
}
this.client = new AmazonS3Client(credentials, clientConfiguration);

if (componentSettings.get("s3.endpoint") != null) {
client.setEndpoint(componentSettings.get("s3.endpoint"));
Expand Down

0 comments on commit 96a4e5d

Please sign in to comment.