Search in sources :

Example 6 with InstanceProfileCredentialsFetcher

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

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

Example 8 with InstanceProfileCredentialsFetcher

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

the class InstanceProfileCredentialsFetcherTest method testSetRoleNameNegative.

@Test
public void testSetRoleNameNegative() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcher();
        credentialsFetcher.setRoleName(null);
        Assert.fail("EcsInstanceCredentialsFetcher.setRoleName should not be successful.");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcher();
        credentialsFetcher.setRoleName("");
        Assert.fail("EcsInstanceCredentialsFetcher.setRoleName should not be successful.");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
    try {
        new InstanceProfileCredentialsFetcher().withRoleName(null);
        Assert.fail("EcsInstanceCredentialsFetcher.setRoleName should not be successful.");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
    try {
        new InstanceProfileCredentialsFetcher().withRoleName("");
        Assert.fail("EcsInstanceCredentialsFetcher.setRoleName should not be successful.");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
}
Also used : InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 9 with InstanceProfileCredentialsFetcher

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

the class InstanceProfileCredentialsFetcherTest method testFetchInvalidCredentials.

@Test
public void testFetchInvalidCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.FormatInvalid);
        credentialsFetcher.fetch(3);
        Assert.fail("EcsInstanceCredentialsFetcher.fetch should not be successful.");
    } catch (ClientException e) {
        Assert.assertEquals(FORMAT_ERROR_MESSAGE, e.getMessage());
    }
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.FormatInvalid);
        credentialsFetcher.fetch();
        Assert.fail("EcsInstanceCredentialsFetcher.fetch should not be successful.");
    } catch (ClientException e) {
        Assert.assertEquals(FORMAT_ERROR_MESSAGE, e.getMessage());
    }
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.ServerHalt);
        credentialsFetcher.fetch(3);
        Assert.fail("EcsInstanceCredentialsFetcher.fetch should not be successful.");
    } catch (ClientException e) {
    }
}
Also used : InstanceProfileCredentialsFetcher(com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher) ClientException(com.aliyuncs.exceptions.ClientException) InstanceProfileCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock) Test(org.junit.Test)

Example 10 with InstanceProfileCredentialsFetcher

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

the class InstanceProfileCredentialsProviderTest method testGetExceptionalCredentials.

@Test
public void testGetExceptionalCredentials() {
    try {
        InstanceProfileCredentialsFetcher credentialsFetcher = new InstanceProfileCredentialsFetcherMock().withResponseCategory(ResponseCategory.Exceptional).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)

Aggregations

InstanceProfileCredentialsFetcher (com.aliyun.oss.common.auth.InstanceProfileCredentialsFetcher)11 Test (org.junit.Test)10 InstanceProfileCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.InstanceProfileCredentialsFetcherMock)8 InstanceProfileCredentials (com.aliyun.oss.common.auth.InstanceProfileCredentials)7 ClientException (com.aliyuncs.exceptions.ClientException)6 InstanceProfileCredentialsProvider (com.aliyun.oss.common.auth.InstanceProfileCredentialsProvider)5 Ignore (org.junit.Ignore)1