Search in sources :

Example 1 with CredentialsProvider

use of com.aliyun.oss.common.auth.CredentialsProvider in project hadoop by apache.

the class AliyunOSSFileSystemStore method initialize.

public void initialize(URI uri, Configuration conf, FileSystem.Statistics stat) throws IOException {
    statistics = stat;
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS_KEY, MAXIMUM_CONNECTIONS_DEFAULT));
    boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS_KEY, SECURE_CONNECTIONS_DEFAULT);
    clientConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP);
    clientConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES_KEY, MAX_ERROR_RETRIES_DEFAULT));
    clientConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT_KEY, ESTABLISH_TIMEOUT_DEFAULT));
    clientConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT_KEY, SOCKET_TIMEOUT_DEFAULT));
    String proxyHost = conf.getTrimmed(PROXY_HOST_KEY, "");
    int proxyPort = conf.getInt(PROXY_PORT_KEY, -1);
    if (StringUtils.isNotEmpty(proxyHost)) {
        clientConf.setProxyHost(proxyHost);
        if (proxyPort >= 0) {
            clientConf.setProxyPort(proxyPort);
        } else {
            if (secureConnections) {
                LOG.warn("Proxy host set without port. Using HTTPS default 443");
                clientConf.setProxyPort(443);
            } else {
                LOG.warn("Proxy host set without port. Using HTTP default 80");
                clientConf.setProxyPort(80);
            }
        }
        String proxyUsername = conf.getTrimmed(PROXY_USERNAME_KEY);
        String proxyPassword = conf.getTrimmed(PROXY_PASSWORD_KEY);
        if ((proxyUsername == null) != (proxyPassword == null)) {
            String msg = "Proxy error: " + PROXY_USERNAME_KEY + " or " + PROXY_PASSWORD_KEY + " set without the other.";
            LOG.error(msg);
            throw new IllegalArgumentException(msg);
        }
        clientConf.setProxyUsername(proxyUsername);
        clientConf.setProxyPassword(proxyPassword);
        clientConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN_KEY));
        clientConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION_KEY));
    } else if (proxyPort >= 0) {
        String msg = "Proxy error: " + PROXY_PORT_KEY + " set without " + PROXY_HOST_KEY;
        LOG.error(msg);
        throw new IllegalArgumentException(msg);
    }
    String endPoint = conf.getTrimmed(ENDPOINT_KEY, "");
    CredentialsProvider provider = AliyunOSSUtils.getCredentialsProvider(conf);
    ossClient = new OSSClient(endPoint, provider, clientConf);
    uploadPartSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    multipartThreshold = conf.getLong(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY, MIN_MULTIPART_UPLOAD_THRESHOLD_DEFAULT);
    partSize = conf.getLong(MULTIPART_UPLOAD_SIZE_KEY, MULTIPART_UPLOAD_SIZE_DEFAULT);
    if (partSize < MIN_MULTIPART_UPLOAD_PART_SIZE) {
        partSize = MIN_MULTIPART_UPLOAD_PART_SIZE;
    }
    serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM_KEY, "");
    if (uploadPartSize < 5 * 1024 * 1024) {
        LOG.warn(MULTIPART_UPLOAD_SIZE_KEY + " must be at least 5 MB");
        uploadPartSize = 5 * 1024 * 1024;
    }
    if (multipartThreshold < 5 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be at least 5 MB");
        multipartThreshold = 5 * 1024 * 1024;
    }
    if (multipartThreshold > 1024 * 1024 * 1024) {
        LOG.warn(MIN_MULTIPART_UPLOAD_THRESHOLD_KEY + " must be less than 1 GB");
        multipartThreshold = 1024 * 1024 * 1024;
    }
    String cannedACLName = conf.get(CANNED_ACL_KEY, CANNED_ACL_DEFAULT);
    if (StringUtils.isNotEmpty(cannedACLName)) {
        CannedAccessControlList cannedACL = CannedAccessControlList.valueOf(cannedACLName);
        ossClient.setBucketAcl(bucketName, cannedACL);
    }
    maxKeys = conf.getInt(MAX_PAGING_KEYS_KEY, MAX_PAGING_KEYS_DEFAULT);
    bucketName = uri.getHost();
}
Also used : OSSClient(com.aliyun.oss.OSSClient) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 2 with CredentialsProvider

use of com.aliyun.oss.common.auth.CredentialsProvider in project hadoop by apache.

the class AliyunOSSUtils method getCredentialsProvider.

/**
   * Create credential provider specified by configuration, or create default
   * credential provider if not specified.
   *
   * @param conf configuration
   * @return a credential provider
   * @throws IOException on any problem. Class construction issues may be
   * nested inside the IOE.
   */
public static CredentialsProvider getCredentialsProvider(Configuration conf) throws IOException {
    CredentialsProvider credentials;
    String className = conf.getTrimmed(ALIYUN_OSS_CREDENTIALS_PROVIDER_KEY);
    if (StringUtils.isEmpty(className)) {
        Configuration newConf = ProviderUtils.excludeIncompatibleCredentialProviders(conf, AliyunOSSFileSystem.class);
        credentials = new AliyunCredentialsProvider(newConf);
    } else {
        try {
            LOG.debug("Credential provider class is:" + className);
            Class<?> credClass = Class.forName(className);
            try {
                credentials = (CredentialsProvider) credClass.getDeclaredConstructor(Configuration.class).newInstance(conf);
            } catch (NoSuchMethodException | SecurityException e) {
                credentials = (CredentialsProvider) credClass.getDeclaredConstructor().newInstance();
            }
        } catch (ClassNotFoundException e) {
            throw new IOException(className + " not found.", e);
        } catch (NoSuchMethodException | SecurityException e) {
            throw new IOException(String.format("%s constructor exception.  A " + "class specified in %s must provide an accessible constructor " + "accepting URI and Configuration, or an accessible default " + "constructor.", className, ALIYUN_OSS_CREDENTIALS_PROVIDER_KEY), e);
        } catch (ReflectiveOperationException | IllegalArgumentException e) {
            throw new IOException(className + " instantiation exception.", e);
        }
    }
    return credentials;
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) IOException(java.io.IOException)

Example 3 with CredentialsProvider

use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.

the class InstanceProfileCredentialsProviderTest method testGetOssCredentialsOnEcs.

/**
 * NOTE: Run this case on ecs.
 */
@Ignore
public void testGetOssCredentialsOnEcs() {
    try {
        // TODO: Establish a simulated ECS metadata service
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newInstanceProfileCredentialsProvider(TestConfig.RAM_REGION_ID);
        String key = "test.txt";
        String content = "HelloOSS";
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentialsProvider);
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InstanceProfileCredentialsProvider(com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Example 4 with CredentialsProvider

use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.

the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderExpire.

@Test
public void testStsKeyPairCredentialsProviderExpire() {
    try {
        PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredFactor(0.001).withExpiredDuration(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
        Thread.sleep(3000);
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 5 with CredentialsProvider

use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.

the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderNegative.

@Test
public void testStsKeyPairCredentialsProviderNegative() throws ClientException, IOException {
    try {
        PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredDuration(899);
        Assert.assertNull(credentialsProvider.getCredentials());
        credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredDuration(100);
        Assert.assertNull(credentialsProvider.getCredentials());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Aggregations

CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)19 Test (org.junit.Test)15 Credentials (com.aliyun.oss.common.auth.Credentials)8 OSS (com.aliyun.oss.OSS)5 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)4 PublicKey (com.aliyun.oss.common.auth.PublicKey)4 ClientException (com.aliyuncs.exceptions.ClientException)4 OSSException (com.aliyun.oss.OSSException)3 EnvironmentVariableCredentialsProvider (com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider)2 SystemPropertiesCredentialsProvider (com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 ClientConfiguration (com.aliyun.oss.ClientConfiguration)1 ClientException (com.aliyun.oss.ClientException)1 OSSClient (com.aliyun.oss.OSSClient)1