use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.
the class STSAssumeRoleSessionCredentialsProviderTest method testStsAssumeRoleCredentialsProviderInOss.
@Test
public void testStsAssumeRoleCredentialsProviderInOss() {
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);
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()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.CredentialsProvider 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());
}
}
use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.
the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesStsCredentialsProvider.
@Test
public void testSystemPropertiesStsCredentialsProvider() {
try {
CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, assumeRoleCred.getAccessKeyId());
System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, assumeRoleCred.getSecretAccessKey());
System.setProperty(AuthUtils.SESSION_TOKEN_SYSTEM_PROPERTY, assumeRoleCred.getSecurityToken());
SystemPropertiesCredentialsProvider credentialsProvider = new SystemPropertiesCredentialsProvider();
Credentials credentials = credentialsProvider.getCredentials();
Assert.assertEquals(credentials.getAccessKeyId(), assumeRoleCred.getAccessKeyId());
Assert.assertEquals(credentials.getSecretAccessKey(), assumeRoleCred.getSecretAccessKey());
Assert.assertEquals(credentials.getSecurityToken(), assumeRoleCred.getSecurityToken());
Assert.assertTrue(credentials.useSecurityToken());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.
the class SystemPropertiesCredentialsProviderTest method testSystemPropertiesStsCredentialsProviderInOss.
@Test
public void testSystemPropertiesStsCredentialsProviderInOss() {
try {
CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
System.setProperty(AuthUtils.ACCESS_KEY_SYSTEM_PROPERTY, assumeRoleCred.getAccessKeyId());
System.setProperty(AuthUtils.SECRET_KEY_SYSTEM_PROPERTY, assumeRoleCred.getSecretAccessKey());
System.setProperty(AuthUtils.SESSION_TOKEN_SYSTEM_PROPERTY, assumeRoleCred.getSecurityToken());
SystemPropertiesCredentialsProvider credentialsProvider = CredentialsProviderFactory.newSystemPropertiesCredentialsProvider();
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()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations