Search in sources :

Example 1 with EnvironmentVariableCredentialsProvider

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

the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableCredentialsInOss.

@Test
public void testGetEnvironmentVariableCredentialsInOss() {
    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);
        // set env
        Map<String, String> env = new HashMap<String, String>(System.getenv());
        env.put(AuthUtils.ACCESS_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_ID);
        env.put(AuthUtils.SECRET_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_SECRET);
        setEnv(env);
        // env provider
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        String key = "test.txt";
        String content = "HelloOSS";
        // oss put
        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) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 2 with EnvironmentVariableCredentialsProvider

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

the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableCredentials.

@Test
public void testGetEnvironmentVariableCredentials() {
    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);
        // set env
        Map<String, String> envMap = new HashMap<String, String>(System.getenv());
        envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_ID);
        envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, TestConfig.ROOT_ACCESS_KEY_SECRET);
        setEnv(envMap);
        // env provider
        EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId(), TestConfig.ROOT_ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), TestConfig.ROOT_ACCESS_KEY_SECRET);
        Assert.assertFalse(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) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 3 with EnvironmentVariableCredentialsProvider

use of com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider 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 4 with EnvironmentVariableCredentialsProvider

use of com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider 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)

Aggregations

EnvironmentVariableCredentialsProvider (com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Credentials (com.aliyun.oss.common.auth.Credentials)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2