A few fixes

- EasyMock instead of Mockito
- FileSessionCredentialsProvider fields should be volatile
- getRestS3Service should create AWSCredentials not a AWSSessionCredentials with fixed credentials
This commit is contained in:
Jae Hyeon Bae 2014-07-02 15:53:39 -07:00
parent 31f7bd8bcb
commit 5e5fa1f357
5 changed files with 33 additions and 26 deletions

View File

@ -70,9 +70,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -8,7 +8,10 @@ public class AWSSessionCredentialsAdapter extends AWSSessionCredentials {
public AWSSessionCredentialsAdapter(AWSCredentialsProvider provider) {
super(null, null, null);
this.provider = provider;
if(provider.getCredentials() instanceof com.amazonaws.auth.AWSSessionCredentials)
this.provider = provider;
else
throw new IllegalArgumentException("provider does not contain session credentials");
}
@Override
@ -18,7 +21,7 @@ public class AWSSessionCredentialsAdapter extends AWSSessionCredentials {
@Override
public String getVersionPrefix() {
return "Netflix AWSSessionCredentialsAdapter, version: ";
return "AWSSessionCredentialsAdapter, version: ";
}
@Override
@ -32,12 +35,8 @@ public class AWSSessionCredentialsAdapter extends AWSSessionCredentials {
}
public String getSessionToken() {
if (provider.getCredentials() instanceof com.amazonaws.auth.AWSSessionCredentials) {
com.amazonaws.auth.AWSSessionCredentials sessionCredentials =
(com.amazonaws.auth.AWSSessionCredentials) provider.getCredentials();
return sessionCredentials.getSessionToken();
} else {
return "";
}
com.amazonaws.auth.AWSSessionCredentials sessionCredentials =
(com.amazonaws.auth.AWSSessionCredentials) provider.getCredentials();
return sessionCredentials.getSessionToken();
}
}

View File

@ -13,9 +13,9 @@ import java.util.Properties;
public class FileSessionCredentialsProvider implements AWSCredentialsProvider {
private final String sessionCredentials;
private String sessionToken;
private String accessKey;
private String secretKey;
private volatile String sessionToken;
private volatile String accessKey;
private volatile String secretKey;
public FileSessionCredentialsProvider(String sessionCredentials) {
this.sessionCredentials = sessionCredentials;

View File

@ -30,6 +30,7 @@ import io.druid.guice.JsonConfigProvider;
import io.druid.guice.LazySingleton;
import io.druid.initialization.DruidModule;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;
import java.util.List;
@ -92,8 +93,15 @@ public class S3StorageDruidModule implements DruidModule
@Provides
@LazySingleton
public RestS3Service getRestS3Service(AWSCredentialsProvider credentialsProvider)
public RestS3Service getRestS3Service(AWSCredentialsProvider provider)
{
return new RestS3Service(new AWSSessionCredentialsAdapter(credentialsProvider));
if(provider.getCredentials() instanceof com.amazonaws.auth.AWSSessionCredentials) {
return new RestS3Service(new AWSSessionCredentialsAdapter(provider));
} else {
return new RestS3Service(new AWSCredentials(
provider.getCredentials().getAWSAccessKeyId(),
provider.getCredentials().getAWSSecretKey()
));
}
}
}

View File

@ -3,6 +3,7 @@ package io.druid.storage.s3;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSSessionCredentials;
import org.easymock.EasyMock;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@ -13,17 +14,16 @@ import java.io.PrintWriter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
public class TestAWSCredentialsProvider {
@Test
public void testWithFixedAWSKeys() {
S3StorageDruidModule module = new S3StorageDruidModule();
AWSCredentialsConfig config = mock(AWSCredentialsConfig.class);
doReturn("accessKeySample").when(config).getAccessKey();
doReturn("secretKeySample").when(config).getSecretKey();
AWSCredentialsConfig config = EasyMock.createMock(AWSCredentialsConfig.class);
EasyMock.expect(config.getAccessKey()).andReturn("accessKeySample").atLeastOnce();
EasyMock.expect(config.getSecretKey()).andReturn("secretKeySample").atLeastOnce();
EasyMock.replay(config);
AWSCredentialsProvider provider = module.getAWSCredentialsProvider(config);
AWSCredentials credentials = provider.getCredentials();
@ -41,14 +41,15 @@ public class TestAWSCredentialsProvider {
public void testWithFileSessionCredentials() throws IOException {
S3StorageDruidModule module = new S3StorageDruidModule();
AWSCredentialsConfig config = mock(AWSCredentialsConfig.class);
doReturn("").when(config).getAccessKey();
doReturn("").when(config).getSecretKey();
AWSCredentialsConfig config = EasyMock.createMock(AWSCredentialsConfig.class);
EasyMock.expect(config.getAccessKey()).andReturn("");
EasyMock.expect(config.getSecretKey()).andReturn("");
File file = folder.newFile();
PrintWriter out = new PrintWriter(file.getAbsolutePath());
out.println("sessionToken=sessionTokenSample\nsecretKey=secretKeySample\naccessKey=accessKeySample");
out.close();
doReturn(file.getAbsolutePath()).when(config).getFileSessionCredentials();
EasyMock.expect(config.getFileSessionCredentials()).andReturn(file.getAbsolutePath()).atLeastOnce();
EasyMock.replay(config);
AWSCredentialsProvider provider = module.getAWSCredentialsProvider(config);
AWSCredentials credentials = provider.getCredentials();