Search in sources :

Example 1 with ProfileConfigFile

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

the class ProfilesConfigFileTest method testGetCredentialsNegative.

@Test
public void testGetCredentialsNegative() {
    try {
        Map<String, String> options = new HashMap<String, String>();
        options.put(AuthUtils.OSS_ACCESS_KEY_ID, "");
        options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
        generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
        ProfileConfigFile configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
        try {
            configFile.getCredentials();
            Assert.fail("ProfileConfigFile.getCredentials should not be successful");
        } catch (InvalidCredentialsException e) {
        }
        options = new HashMap<String, String>();
        options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
        options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, "");
        generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
        try {
            configFile.getCredentials();
            Assert.fail("ProfileConfigFile.getCredentials should not be successful");
        } catch (InvalidCredentialsException e) {
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
    }
}
Also used : HashMap(java.util.HashMap) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) File(java.io.File) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) Test(org.junit.Test)

Example 2 with ProfileConfigFile

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

the class ProfilesConfigFileTest method testGetCredentialsWithDefaultParameters.

@Test
public void testGetCredentialsWithDefaultParameters() {
    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);
        ProfileConfigFile configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
        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(AuthUtils.DEFAULT_PROFILE_PATH);
        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 : 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 3 with ProfileConfigFile

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

the class ProfilesConfigFileTest method testGetFreshCredentials.

@Test
public void testGetFreshCredentials() {
    try {
        new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
        Thread.sleep(1000);
        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);
        generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
        ProfileConfigFile configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
        Credentials 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();
        Thread.sleep(1000);
        // Fresh
        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);
        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());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : 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 4 with ProfileConfigFile

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

Aggregations

InvalidCredentialsException (com.aliyun.oss.common.auth.InvalidCredentialsException)4 ProfileConfigFile (com.aliyun.oss.common.auth.ProfileConfigFile)4 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Credentials (com.aliyun.oss.common.auth.Credentials)3 ProfileConfigLoader (com.aliyun.oss.common.auth.ProfileConfigLoader)1