Search in sources :

Example 11 with BasicCredentials

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

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

Example 13 with BasicCredentials

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

the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderRefresh.

@Test
public void testStsAssumeRoleCredentialsProviderRefresh() {
    try {
        CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN).withExpiredFactor(0.001).withExpiredDuration(2000);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
        Thread.sleep(3000);
        Assert.assertTrue(credentials.willSoonExpire());
        BasicCredentials freshCredentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(freshCredentials.willSoonExpire());
        Assert.assertFalse(freshCredentials.getAccessKeyId().equals(credentials.getAccessKeyId()));
    } 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

BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)13 Test (org.junit.Test)13 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 CustomSessionCredentialsFetcher (com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher)5 CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)5 CustomSessionCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock)5 PublicKey (com.aliyun.oss.common.auth.PublicKey)2 ClientException (com.aliyuncs.exceptions.ClientException)2 IOException (java.io.IOException)2