Search in sources :

Example 6 with CustomSessionCredentialsProvider

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsFromAuthInOss.

@Test
public void testGetCredentialsFromAuthInOss() {
    try {
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 516);
        Assert.assertTrue(credentials.useSecurityToken());
        String key = "test.txt";
        String content = "HelloOSS";
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSecurityToken());
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 7 with CustomSessionCredentialsProvider

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

the class CustomSessionCredentialsProviderTest method testGetNormalCredentials.

@Test
public void testGetNormalCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Normal);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        BasicCredentials credentials = (BasicCredentials) 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());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 8 with CustomSessionCredentialsProvider

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

the class CustomSessionCredentialsProviderTest method testGetExceptionalCredentials.

@Test
public void testGetExceptionalCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Exceptional);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertNull(credentials);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 9 with CustomSessionCredentialsProvider

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

the class CustomSessionCredentialsProviderTest method testRefreshCredentials.

@Test
public void testRefreshCredentials() {
    try {
        CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Expired);
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
        BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertTrue(credentials.willSoonExpire());
        credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Normal);
        credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.ECS_ROLE_NAME).withCredentialsFetcher(credentialsFetcher);
        credentials = (BasicCredentials) credentialsProvider.getCredentials();
        Assert.assertFalse(credentials.willSoonExpire());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Test(org.junit.Test)

Example 10 with CustomSessionCredentialsProvider

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsUseInOss.

@Test
public void testGetCredentialsUseInOss() {
    try {
        CustomSessionCredentialsProvider credentialsProvider = CredentialsProviderFactory.newCustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
        String key = "test.txt";
        String content = "HelloOSS";
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentialsProvider);
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        Thread.sleep(1000 * 10);
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Aggregations

CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)10 Test (org.junit.Test)10 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)9 CustomSessionCredentialsFetcher (com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher)7 CustomSessionCredentialsFetcherMock (com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock)7 Credentials (com.aliyun.oss.common.auth.Credentials)4 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2