Search in sources :

Example 16 with CredentialsProvider

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

the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderInOss.

@Test
public void testStsAssumeRoleCredentialsProviderInOss() {
    try {
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN).withExpiredDuration(900);
        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) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 17 with CredentialsProvider

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

the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderRefresh.

@Test
public void testStsAssumeRoleCredentialsProviderRefresh() {
    try {
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN).withExpiredFactor(0.001).withExpiredDuration(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
        Thread.sleep(3000);
        Assert.assertTrue(credentials.willSoonExpire());
        BasicCredentials freshCredentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(freshCredentials.willSoonExpire());
        Assert.assertFalse(freshCredentials.getAccessKeyId().equals(credentials.getAccessKeyId()));
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 18 with CredentialsProvider

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

the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesStsCredentialsProvider.

@Test
public void testSystemPropertiesStsCredentialsProvider() {
    try {
        CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
        Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
        System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, assumeRoleCred.getAccessKeyId());
        System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, assumeRoleCred.getSecretAccessKey());
        System.setProperty(AuthUtils.SESSION_TOKEN_SYSTEM_PROPERTY, assumeRoleCred.getSecurityToken());
        SystemPropertiesCredentialsProvider credentialsProvider = new SystemPropertiesCredentialsProvider();
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId(), assumeRoleCred.getAccessKeyId());
        Assert.assertEquals(credentials.getSecretAccessKey(), assumeRoleCred.getSecretAccessKey());
        Assert.assertEquals(credentials.getSecurityToken(), assumeRoleCred.getSecurityToken());
        Assert.assertTrue(credentials.useSecurityToken());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SystemPropertiesCredentialsProvider(com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider) SystemPropertiesCredentialsProvider(com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 19 with CredentialsProvider

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

the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesStsCredentialsProviderInOss.

@Test
public void testSystemPropertiesStsCredentialsProviderInOss() {
    try {
        CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
        Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
        System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, assumeRoleCred.getAccessKeyId());
        System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, assumeRoleCred.getSecretAccessKey());
        System.setProperty(AuthUtils.SESSION_TOKEN_SYSTEM_PROPERTY, assumeRoleCred.getSecurityToken());
        SystemPropertiesCredentialsProvider credentialsProvider = CredentialsProviderFactory.newSystemPropertiesCredentialsProvider();
        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 : SystemPropertiesCredentialsProvider(com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) SystemPropertiesCredentialsProvider(com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) Credentials(com.aliyun.oss.common.auth.Credentials) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) 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