Search in sources :

Example 11 with CredentialsProvider

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

the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableStsCredentials.

@Test
public void testGetEnvironmentVariableStsCredentials() {
    try {
        // unset evn
        List<String> envSet = new ArrayList<String>();
        envSet.add(AuthUtils.ACCESS_KEY_ENV_VAR);
        envSet.add(AuthUtils.SECRET_KEY_ENV_VAR);
        envSet.add(AuthUtils.SESSION_TOKEN_ENV_VAR);
        unsetEnv(envSet);
        CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
        // set env
        Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
        Map<String, String> envMap = new HashMap<String, String>(System.getenv());
        envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, assumeRoleCred.getAccessKeyId());
        envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, assumeRoleCred.getSecretAccessKey());
        envMap.put(AuthUtils.SESSION_TOKEN_ENV_VAR, assumeRoleCred.getSecurityToken());
        setEnv(envMap);
        // env provider
        EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
        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 : EnvironmentVariableCredentialsProvider(com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EnvironmentVariableCredentialsProvider(com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 12 with CredentialsProvider

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

the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableStsCredentialsInOss.

@Test
public void testGetEnvironmentVariableStsCredentialsInOss() {
    try {
        // unset evn
        List<String> envSet = new ArrayList<String>();
        envSet.add(AuthUtils.ACCESS_KEY_ENV_VAR);
        envSet.add(AuthUtils.SECRET_KEY_ENV_VAR);
        envSet.add(AuthUtils.SESSION_TOKEN_ENV_VAR);
        unsetEnv(envSet);
        CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
        // set env
        Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
        Map<String, String> envMap = new HashMap<String, String>(System.getenv());
        envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, assumeRoleCred.getAccessKeyId());
        envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, assumeRoleCred.getSecretAccessKey());
        envMap.put(AuthUtils.SESSION_TOKEN_ENV_VAR, assumeRoleCred.getSecurityToken());
        setEnv(envMap);
        // env provider
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        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 : EnvironmentVariableCredentialsProvider(com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) EnvironmentVariableCredentialsProvider(com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider) 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)

Example 13 with CredentialsProvider

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

the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderInOss.

@Test
public void testStsKeyPairCredentialsProviderInOss() {
    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));
        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) PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 14 with CredentialsProvider

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

the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProvider.

@Test
public void testStsKeyPairCredentialsProvider() {
    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(900);
        Thread.sleep(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
        Assert.assertTrue(credentials.getAccessKeyId().startsWith("TMPSK."));
        Assert.assertEquals(credentials.getAccessKeyId().length(), 130);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
    } 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 15 with CredentialsProvider

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

the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProvider.

@Test
public void testStsAssumeRoleCredentialsProvider() {
    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);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
        Assert.assertTrue(credentials.getAccessKeyId().startsWith("STS."));
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertTrue(credentials.getSecretAccessKey().length() > 0);
        Assert.assertTrue(credentials.getSecurityToken().length() > 0);
    } 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)

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