Search in sources :

Example 16 with AWSCredentialsProvider

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);
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Method(java.lang.reflect.Method) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 17 with AWSCredentialsProvider

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));
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) URI(java.net.URI) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.junit.Test)

Example 18 with AWSCredentialsProvider

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()));
    }
}
Also used : AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 19 with AWSCredentialsProvider

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);
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) URI(java.net.URI) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) Test(org.junit.Test)

Aggregations

AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)19 Test (org.junit.Test)7 AWSCredentials (com.amazonaws.auth.AWSCredentials)5 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)5 Configuration (org.apache.hadoop.conf.Configuration)5 URI (java.net.URI)4 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)3 AWSSessionCredentials (com.amazonaws.auth.AWSSessionCredentials)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 S3ClientOptions (com.amazonaws.services.s3.S3ClientOptions)2 AWSCredentialsConfig (io.druid.common.aws.AWSCredentialsConfig)2 AmazonClientException (com.amazonaws.AmazonClientException)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSCredentialsProviderChain (com.amazonaws.auth.AWSCredentialsProviderChain)1 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)1 EnvironmentVariableCredentialsProvider (com.amazonaws.auth.EnvironmentVariableCredentialsProvider)1 SystemPropertiesCredentialsProvider (com.amazonaws.auth.SystemPropertiesCredentialsProvider)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 StaticCredentialsProvider (com.amazonaws.internal.StaticCredentialsProvider)1 Region (com.amazonaws.regions.Region)1