|
@@ -17,13 +17,40 @@
|
|
*/
|
|
*/
|
|
package org.apache.hadoop.fs.s3;
|
|
package org.apache.hadoop.fs.s3;
|
|
|
|
|
|
|
|
+import org.apache.commons.logging.Log;
|
|
|
|
+import org.apache.commons.logging.LogFactory;
|
|
|
|
+import org.apache.hadoop.conf.Configuration;
|
|
|
|
+import org.apache.hadoop.security.ProviderUtils;
|
|
|
|
+import org.apache.hadoop.security.alias.CredentialProvider;
|
|
|
|
+import org.apache.hadoop.security.alias.CredentialProviderFactory;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
|
|
|
|
-import junit.framework.TestCase;
|
|
|
|
|
|
+import org.junit.Before;
|
|
|
|
+import org.junit.Rule;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.rules.TemporaryFolder;
|
|
|
|
+import org.junit.rules.TestName;
|
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
|
+import static org.junit.Assert.fail;
|
|
|
|
|
|
-import org.apache.hadoop.conf.Configuration;
|
|
|
|
|
|
+public class TestS3Credentials {
|
|
|
|
+ public static final Log LOG = LogFactory.getLog(TestS3Credentials.class);
|
|
|
|
+
|
|
|
|
+ @Rule
|
|
|
|
+ public final TestName test = new TestName();
|
|
|
|
+
|
|
|
|
+ @Before
|
|
|
|
+ public void announce() {
|
|
|
|
+ LOG.info("Running test " + test.getMethodName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static final String EXAMPLE_ID = "AKASOMEACCESSKEY";
|
|
|
|
+ private static final String EXAMPLE_KEY =
|
|
|
|
+ "RGV0cm9pdCBSZ/WQgY2xl/YW5lZCB1cAEXAMPLE";
|
|
|
|
|
|
-public class TestS3Credentials extends TestCase {
|
|
|
|
|
|
+ @Test
|
|
public void testInvalidHostnameWithUnderscores() throws Exception {
|
|
public void testInvalidHostnameWithUnderscores() throws Exception {
|
|
S3Credentials s3Credentials = new S3Credentials();
|
|
S3Credentials s3Credentials = new S3Credentials();
|
|
try {
|
|
try {
|
|
@@ -33,4 +60,78 @@ public class TestS3Credentials extends TestCase {
|
|
assertEquals("Invalid hostname in URI s3://a:b@c_d", e.getMessage());
|
|
assertEquals("Invalid hostname in URI s3://a:b@c_d", e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testPlaintextConfigPassword() throws Exception {
|
|
|
|
+ S3Credentials s3Credentials = new S3Credentials();
|
|
|
|
+ Configuration conf = new Configuration();
|
|
|
|
+ conf.set("fs.s3.awsAccessKeyId", EXAMPLE_ID);
|
|
|
|
+ conf.set("fs.s3.awsSecretAccessKey", EXAMPLE_KEY);
|
|
|
|
+ s3Credentials.initialize(new URI("s3://foobar"), conf);
|
|
|
|
+ assertEquals("Could not retrieve proper access key", EXAMPLE_ID,
|
|
|
|
+ s3Credentials.getAccessKey());
|
|
|
|
+ assertEquals("Could not retrieve proper secret", EXAMPLE_KEY,
|
|
|
|
+ s3Credentials.getSecretAccessKey());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testPlaintextConfigPasswordWithWhitespace() throws Exception {
|
|
|
|
+ S3Credentials s3Credentials = new S3Credentials();
|
|
|
|
+ Configuration conf = new Configuration();
|
|
|
|
+ conf.set("fs.s3.awsAccessKeyId", "\r\n " + EXAMPLE_ID +
|
|
|
|
+ " \r\n");
|
|
|
|
+ conf.set("fs.s3.awsSecretAccessKey", "\r\n " + EXAMPLE_KEY +
|
|
|
|
+ " \r\n");
|
|
|
|
+ s3Credentials.initialize(new URI("s3://foobar"), conf);
|
|
|
|
+ assertEquals("Could not retrieve proper access key", EXAMPLE_ID,
|
|
|
|
+ s3Credentials.getAccessKey());
|
|
|
|
+ assertEquals("Could not retrieve proper secret", EXAMPLE_KEY,
|
|
|
|
+ s3Credentials.getSecretAccessKey());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Rule
|
|
|
|
+ public final TemporaryFolder tempDir = new TemporaryFolder();
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void testCredentialProvider() throws Exception {
|
|
|
|
+ // set up conf to have a cred provider
|
|
|
|
+ final Configuration conf = new Configuration();
|
|
|
|
+ final File file = tempDir.newFile("test.jks");
|
|
|
|
+ final URI jks = ProviderUtils.nestURIForLocalJavaKeyStoreProvider(
|
|
|
|
+ file.toURI());
|
|
|
|
+ conf.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH,
|
|
|
|
+ jks.toString());
|
|
|
|
+
|
|
|
|
+ // add our creds to the provider
|
|
|
|
+ final CredentialProvider provider =
|
|
|
|
+ CredentialProviderFactory.getProviders(conf).get(0);
|
|
|
|
+ provider.createCredentialEntry("fs.s3.awsSecretAccessKey",
|
|
|
|
+ EXAMPLE_KEY.toCharArray());
|
|
|
|
+ provider.flush();
|
|
|
|
+
|
|
|
|
+ // make sure S3Creds can retrieve things.
|
|
|
|
+ S3Credentials s3Credentials = new S3Credentials();
|
|
|
|
+ conf.set("fs.s3.awsAccessKeyId", EXAMPLE_ID);
|
|
|
|
+ s3Credentials.initialize(new URI("s3://foobar"), conf);
|
|
|
|
+ assertEquals("Could not retrieve proper access key", EXAMPLE_ID,
|
|
|
|
+ s3Credentials.getAccessKey());
|
|
|
|
+ assertEquals("Could not retrieve proper secret", EXAMPLE_KEY,
|
|
|
|
+ s3Credentials.getSecretAccessKey());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test(expected=IllegalArgumentException.class)
|
|
|
|
+ public void noSecretShouldThrow() throws Exception {
|
|
|
|
+ S3Credentials s3Credentials = new S3Credentials();
|
|
|
|
+ Configuration conf = new Configuration();
|
|
|
|
+ conf.set("fs.s3.awsAccessKeyId", EXAMPLE_ID);
|
|
|
|
+ s3Credentials.initialize(new URI("s3://foobar"), conf);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test(expected=IllegalArgumentException.class)
|
|
|
|
+ public void noAccessIdShouldThrow() throws Exception {
|
|
|
|
+ S3Credentials s3Credentials = new S3Credentials();
|
|
|
|
+ Configuration conf = new Configuration();
|
|
|
|
+ conf.set("fs.s3.awsSecretAccessKey", EXAMPLE_KEY);
|
|
|
|
+ s3Credentials.initialize(new URI("s3://foobar"), conf);
|
|
|
|
+ }
|
|
}
|
|
}
|