Search in sources :

Example 6 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsFetcherTest method testFetchNormalCredentials.

@Test
public void testFetchNormalCredentials() {
    try {
        InstanceProfileCredentialsFetcherMock credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Normal);
        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) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 7 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsTest method testExpiredFactor.

@Test
public void testExpiredFactor() {
    try {
        InstanceProfileCredentials credentials = new InstanceProfileCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null, "2010-11-11T11:11:11Z").withExpiredFactor(10.0);
        Thread.sleep(1000);
        Assert.assertTrue(credentials.willSoonExpire());
        credentials = new InstanceProfileCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null, "2020-11-11T11:11:11Z").withExpiredFactor(0.8);
        Thread.sleep(1000);
        Assert.assertFalse(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) Test(org.junit.Test)

Example 8 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsTest method testWillSoonExpire.

@Test
public void testWillSoonExpire() {
    try {
        InstanceProfileCredentials credentials = new InstanceProfileCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null, "2016-11-11T11:11:11Z");
        Assert.assertTrue(credentials.willSoonExpire());
        Assert.assertTrue(credentials.isExpired());
        Assert.assertTrue(credentials.shouldRefresh());
        credentials = new InstanceProfileCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null, "2020-11-11T11:11:11Z");
        Assert.assertFalse(credentials.willSoonExpire());
        Assert.assertFalse(credentials.isExpired());
        Assert.assertTrue(credentials.shouldRefresh());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) Test(org.junit.Test)

Example 9 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsTest method testBasicTest.

@Test
public void testBasicTest() {
    try {
        InstanceProfileCredentials credentials = new InstanceProfileCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET, null, "2017-11-03T05:10:02Z");
        Assert.assertEquals(credentials.getAccessKeyId(), ACCESS_KEY_ID);
        Assert.assertEquals(credentials.getSecretAccessKey(), ACCESS_KEY_SECRET);
        Assert.assertEquals(credentials.getSecurityToken(), null);
        Assert.assertFalse(credentials.useSecurityToken());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InstanceProfileCredentials(com.aliyun.oss.common.auth.InstanceProfileCredentials) Test(org.junit.Test)

Example 10 with InstanceProfileCredentials

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

the class InstanceProfileCredentialsProviderTest method testGetCredentialsOnEcs.

/**
 * NOTE: Run this case on ecs.
 */
@Ignore
public void testGetCredentialsOnEcs() {
    try {
        // TODO: Establish a simulated ECS metadata service
        InstanceProfileCredentialsProvider credentialsProvider = new InstanceProfileCredentialsProvider(TestConfig.ECS_ROLE_NAME);
        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());
    } 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) Ignore(org.junit.Ignore)

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