Search in sources :

Example 1 with InvalidCredentialsException

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

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

the class ProfileConfigFile method refresh.

/**
 * Reread data from disk.
 */
public void refresh() {
    if (credentials == null || profileFile.lastModified() > profileFileLastModified) {
        profileFileLastModified = profileFile.lastModified();
        Map<String, String> profileProperties = null;
        try {
            profileProperties = profileLoader.loadProfile(profileFile);
        } catch (IOException e) {
            LogUtils.logException("ProfilesConfigFile.refresh Exception:", e);
            return;
        }
        String accessKeyId = StringUtils.trim(profileProperties.get(AuthUtils.OSS_ACCESS_KEY_ID));
        String secretAccessKey = StringUtils.trim(profileProperties.get(AuthUtils.OSS_SECRET_ACCESS_KEY));
        String sessionToken = StringUtils.trim(profileProperties.get(AuthUtils.OSS_SESSION_TOKEN));
        if (StringUtils.isNullOrEmpty(accessKeyId)) {
            throw new InvalidCredentialsException("Access key id should not be null or empty.");
        }
        if (StringUtils.isNullOrEmpty(secretAccessKey)) {
            throw new InvalidCredentialsException("Secret access key should not be null or empty.");
        }
        credentials = new DefaultCredentials(accessKeyId, secretAccessKey, sessionToken);
    }
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) IOException(java.io.IOException)

Example 3 with InvalidCredentialsException

use of com.aliyun.oss.common.auth.InvalidCredentialsException in project hadoop by apache.

the class TestAliyunCredentials method validateCredential.

private void validateCredential(Configuration conf) {
    try {
        AliyunCredentialsProvider provider = new AliyunCredentialsProvider(conf);
        Credentials credentials = provider.getCredentials();
        fail("Expected a CredentialInitializationException, got " + credentials);
    } catch (InvalidCredentialsException expected) {
    // expected
    } catch (IOException e) {
        fail("Unexpected exception.");
    }
}
Also used : InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) IOException(java.io.IOException) Credentials(com.aliyun.oss.common.auth.Credentials)

Aggregations

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