Search in sources :

Example 21 with Credentials

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

the class ProfilesConfigFileTest method testGetCredentialsWithLoader.

@Test
public void testGetCredentialsWithLoader() {
    try {
        // STS
        Map<String, String> options = new HashMap<String, String>();
        options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
        options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
        options.put(AuthUtils.OSS_SESSION_TOKEN, TEST_SECURITY_TOKEN);
        generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
        ProfileConfigLoader profileLoader = new ProfileConfigLoader();
        ProfileConfigFile configFile = new ProfileConfigFile(new File(AuthUtils.DEFAULT_PROFILE_PATH), profileLoader);
        Credentials credentials = configFile.getCredentials();
        Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
        Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
        Assert.assertEquals(TEST_SECURITY_TOKEN, credentials.getSecurityToken());
        Assert.assertTrue(credentials.useSecurityToken());
        new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
        // Normal
        options = new HashMap<String, String>();
        options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
        options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
        generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
        configFile = new ProfileConfigFile(new File(AuthUtils.DEFAULT_PROFILE_PATH), profileLoader);
        credentials = configFile.getCredentials();
        Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
        Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
        Assert.assertNull(credentials.getSecurityToken());
        Assert.assertFalse(credentials.useSecurityToken());
        new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ProfileConfigLoader(com.aliyun.oss.common.auth.ProfileConfigLoader) HashMap(java.util.HashMap) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) File(java.io.File) Credentials(com.aliyun.oss.common.auth.Credentials) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) Test(org.junit.Test)

Example 22 with Credentials

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

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

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

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

Credentials (com.aliyun.oss.common.auth.Credentials)25 Test (org.junit.Test)18 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)8 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)7 HashMap (java.util.HashMap)7 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 OSS (com.aliyun.oss.OSS)4 OSSException (com.aliyun.oss.OSSException)4 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)4 CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)4 InvalidCredentialsException (com.aliyun.oss.common.auth.InvalidCredentialsException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OSSClient (com.aliyun.oss.OSSClient)3 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)3 EnvironmentVariableCredentialsProvider (com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider)3 ProfileConfigFile (com.aliyun.oss.common.auth.ProfileConfigFile)3 SystemPropertiesCredentialsProvider (com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3