use of com.amazonaws.auth.AWSCredentialsProvider in project hadoop by apache.
the class S3AUtils method createAWSCredentialProvider.
/**
* Create an AWS credential provider from its class by using reflection. The
* class must implement one of the following means of construction, which are
* attempted in order:
*
* <ol>
* <li>a public constructor accepting java.net.URI and
* org.apache.hadoop.conf.Configuration</li>
* <li>a public static method named getInstance that accepts no
* arguments and returns an instance of
* com.amazonaws.auth.AWSCredentialsProvider, or</li>
* <li>a public default constructor.</li>
* </ol>
*
* @param conf configuration
* @param credClass credential class
* @param uri URI of the FS
* @return the instantiated class
* @throws IOException on any instantiation failure.
*/
static AWSCredentialsProvider createAWSCredentialProvider(Configuration conf, Class<?> credClass, URI uri) throws IOException {
AWSCredentialsProvider credentials = null;
String className = credClass.getName();
if (!AWSCredentialsProvider.class.isAssignableFrom(credClass)) {
throw new IOException("Class " + credClass + " " + NOT_AWS_PROVIDER);
}
if (Modifier.isAbstract(credClass.getModifiers())) {
throw new IOException("Class " + credClass + " " + ABSTRACT_PROVIDER);
}
LOG.debug("Credential provider class is {}", className);
try {
// new X(uri, conf)
Constructor cons = getConstructor(credClass, URI.class, Configuration.class);
if (cons != null) {
credentials = (AWSCredentialsProvider) cons.newInstance(uri, conf);
return credentials;
}
// X.getInstance()
Method factory = getFactoryMethod(credClass, AWSCredentialsProvider.class, "getInstance");
if (factory != null) {
credentials = (AWSCredentialsProvider) factory.invoke(null);
return credentials;
}
// new X()
cons = getConstructor(credClass);
if (cons != null) {
credentials = (AWSCredentialsProvider) cons.newInstance();
return credentials;
}
// no supported constructor or factory method found
throw new IOException(String.format("%s " + CONSTRUCTOR_EXCEPTION + ". A class specified in %s must provide a public constructor " + "accepting URI and Configuration, or a public factory method named " + "getInstance that accepts no arguments, or a public default " + "constructor.", className, AWS_CREDENTIALS_PROVIDER));
} catch (ReflectiveOperationException | IllegalArgumentException e) {
// supported constructor or factory method found, but the call failed
throw new IOException(className + " " + INSTANTIATION_EXCEPTION + ".", e);
} finally {
if (credentials != null) {
LOG.debug("Using {} for {}.", credentials, uri);
}
}
}
use of com.amazonaws.auth.AWSCredentialsProvider in project hadoop by apache.
the class TestS3AAWSCredentialsProvider method testConfiguredChain.
@Test
public void testConfiguredChain() throws Exception {
URI uri1 = new URI("s3a://bucket1"), uri2 = new URI("s3a://bucket2");
Configuration conf = new Configuration();
List<Class<? extends AWSCredentialsProvider>> expectedClasses = Arrays.asList(EnvironmentVariableCredentialsProvider.class, SharedInstanceProfileCredentialsProvider.class, AnonymousAWSCredentialsProvider.class);
conf.set(AWS_CREDENTIALS_PROVIDER, buildClassListString(expectedClasses));
AWSCredentialProviderList list1 = S3AUtils.createAWSCredentialProviderSet(uri1, conf, uri1);
AWSCredentialProviderList list2 = S3AUtils.createAWSCredentialProviderSet(uri2, conf, uri2);
assertCredentialProviders(expectedClasses, list1);
assertCredentialProviders(expectedClasses, list2);
assertSameInstanceProfileCredentialsProvider(list1.getProviders().get(1), list2.getProviders().get(1));
}
use of com.amazonaws.auth.AWSCredentialsProvider in project hadoop by apache.
the class TestS3AAWSCredentialsProvider method assertCredentialProviders.
/**
* Asserts expected provider classes in list.
* @param expectedClasses expected provider classes
* @param list providers to check
*/
private static void assertCredentialProviders(List<Class<? extends AWSCredentialsProvider>> expectedClasses, AWSCredentialProviderList list) {
assertNotNull(list);
List<AWSCredentialsProvider> providers = list.getProviders();
assertEquals(expectedClasses.size(), providers.size());
for (int i = 0; i < expectedClasses.size(); ++i) {
Class<? extends AWSCredentialsProvider> expectedClass = expectedClasses.get(i);
AWSCredentialsProvider provider = providers.get(i);
assertNotNull(String.format("At position %d, expected class is %s, but found null.", i, expectedClass), provider);
assertTrue(String.format("At position %d, expected class is %s, but found %s.", i, expectedClass, provider.getClass()), expectedClass.isAssignableFrom(provider.getClass()));
}
}
use of com.amazonaws.auth.AWSCredentialsProvider in project hadoop by apache.
the class TestS3AAWSCredentialsProvider method testInstantiationChain.
@Test
public void testInstantiationChain() throws Throwable {
Configuration conf = new Configuration();
conf.set(AWS_CREDENTIALS_PROVIDER, TemporaryAWSCredentialsProvider.NAME + ", \t" + SimpleAWSCredentialsProvider.NAME + " ,\n " + AnonymousAWSCredentialsProvider.NAME);
Path testFile = new Path(conf.getTrimmed(KEY_CSVTEST_FILE, DEFAULT_CSVTEST_FILE));
URI uri = testFile.toUri();
AWSCredentialProviderList list = S3AUtils.createAWSCredentialProviderSet(uri, conf, uri);
List<Class<? extends AWSCredentialsProvider>> expectedClasses = Arrays.asList(TemporaryAWSCredentialsProvider.class, SimpleAWSCredentialsProvider.class, AnonymousAWSCredentialsProvider.class);
assertCredentialProviders(expectedClasses, list);
}
Aggregations