Search in sources :

Example 1 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsProviderTest method testGetNormalCredentials.

@Test
public void testGetNormalCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Normal).withRoleName(TestConfig.ECS_ROLE_NAME);
        InstanceProfileCredentialsProvider credentialsProvider = new InstanceProfileCredentialsProvider(TestConfig.ECS_ROLE_NAME).withCredentialsFetcher(credentialsFetcher);
        InstanceProfileCredentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
        Assert.assertFalse(credentials.isExpired());
        Assert.assertTrue(credentials.shouldRefresh());
        credentials.setLastFailedRefreshTime();
        Assert.assertFalse(credentials.isExpired());
        Assert.assertFalse(credentials.shouldRefresh());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentialsProvider(com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider) InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) Test(org.junit.Test)

Example 2 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsProviderTest method testGetCredentialsServerHalt.

@Test
public void testGetCredentialsServerHalt() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.ServerHalt).withRoleName(TestConfig.ECS_ROLE_NAME);
        InstanceProfileCredentialsProvider credentialsProvider = new InstanceProfileCredentialsProvider(TestConfig.ECS_ROLE_NAME).withCredentialsFetcher(credentialsFetcher);
        InstanceProfileCredentials credentials = credentialsProvider.getCredentials();
        Assert.assertNull(credentials);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentialsProvider(com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider) InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) Test(org.junit.Test)

Example 3 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsProviderTest method testGetExpiredCredentials.

@Test
public void testGetExpiredCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Expired).withRoleName(TestConfig.ECS_ROLE_NAME);
        InstanceProfileCredentialsProvider credentialsProvider = new InstanceProfileCredentialsProvider(TestConfig.ECS_ROLE_NAME).withCredentialsFetcher(credentialsFetcher);
        InstanceProfileCredentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertTrue(credentials.willSoonExpire());
        Assert.assertTrue(credentials.isExpired());
        Assert.assertTrue(credentials.shouldRefresh());
        credentials.setLastFailedRefreshTime();
        Assert.assertTrue(credentials.isExpired());
        Assert.assertFalse(credentials.shouldRefresh());
        credentials = credentialsProvider.getCredentials();
        Assert.assertNotNull(credentials);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentialsProvider(com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider) InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) Test(org.junit.Test)

Example 4 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsFetcherTest method testFetchCredentialsOnEcs.

/**
 * NOTE: Run this case on ecs.
 */
@Ignore
public void testFetchCredentialsOnEcs() {
    try {
        // TODO: Establish a simulated ECS metadata service
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcher().withRoleName(TestConfig.ECS_ROLE_NAME);
        InstanceProfileCredentials credentials = (InstanceProfileCredentials) credentialsFetcher.fetch(3);
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
        credentials = (InstanceProfileCredentials) credentialsFetcher.fetch();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertFalse(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) ClientException(com.aliyuncs.exceptions.ClientException) Ignore(org.junit.Ignore)

Example 5 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsFetcherTest method testFetchExpiredCredentials.

@Test
public void testFetchExpiredCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Expired);
        InstanceProfileCredentials credentials = (InstanceProfileCredentials) credentialsFetcher.fetch(3);
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertTrue(credentials.willSoonExpire());
        credentials = (InstanceProfileCredentials) credentialsFetcher.fetch();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 536);
        Assert.assertTrue(credentials.useSecurityToken());
        Assert.assertTrue(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Aggregations

InstanceProfileCredentials (com.aliyun.oss.common.auth.InstanceProfileCredentials)13 Test (org.junit.Test)11 InstanceProfileCredentialsFetcher (com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher)7 InstanceProfileCredentialsProvider (com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider)7 InstanceProfileCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock)7 ClientException (com.aliyuncs.exceptions.ClientException)3 Ignore (org.junit.Ignore)2