use of com.aliyun.oss.common.auth.CredentialsProvider in project aliyun-oss-java-sdk by aliyun.
the class EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableStsCredentials.
@Test
public void testGetEnvironmentVariableStsCredentials() {
try {
// unset evn
List<String> envSet = new ArrayList<String>();
envSet.add(AuthUtils.ACCESS_KEY_ENV_VAR);
envSet.add(AuthUtils.SECRET_KEY_ENV_VAR);
envSet.add(AuthUtils.SESSION_TOKEN_ENV_VAR);
unsetEnv(envSet);
CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
// set env
Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
Map<String, String> envMap = new HashMap<String, String>(System.getenv());
envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, assumeRoleCred.getAccessKeyId());
envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, assumeRoleCred.getSecretAccessKey());
envMap.put(AuthUtils.SESSION_TOKEN_ENV_VAR, assumeRoleCred.getSecurityToken());
setEnv(envMap);
// env provider
EnvironmentVariableCredentialsProvider credentialsProvider = new EnvironmentVariableCredentialsProvider();
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 EnvironmentVariableCredentialsProviderTest method testGetEnvironmentVariableStsCredentialsInOss.
@Test
public void testGetEnvironmentVariableStsCredentialsInOss() {
try {
// unset evn
List<String> envSet = new ArrayList<String>();
envSet.add(AuthUtils.ACCESS_KEY_ENV_VAR);
envSet.add(AuthUtils.SECRET_KEY_ENV_VAR);
envSet.add(AuthUtils.SESSION_TOKEN_ENV_VAR);
unsetEnv(envSet);
CredentialsProvider assumeRoleCredProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider(TestConfig.RAM_REGION_ID, TestConfig.USER_ACCESS_KEY_ID, TestConfig.USER_ACCESS_KEY_SECRET, TestConfig.RAM_ROLE_ARN);
// set env
Credentials assumeRoleCred = assumeRoleCredProvider.getCredentials();
Map<String, String> envMap = new HashMap<String, String>(System.getenv());
envMap.put(AuthUtils.ACCESS_KEY_ENV_VAR, assumeRoleCred.getAccessKeyId());
envMap.put(AuthUtils.SECRET_KEY_ENV_VAR, assumeRoleCred.getSecretAccessKey());
envMap.put(AuthUtils.SESSION_TOKEN_ENV_VAR, assumeRoleCred.getSecurityToken());
setEnv(envMap);
// env provider
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
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 STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderInOss.
@Test
public void testStsKeyPairCredentialsProviderInOss() {
try {
PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH));
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 STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProvider.
@Test
public void testStsKeyPairCredentialsProvider() {
try {
PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH));
CredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredDuration(900);
Thread.sleep(2000);
BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
Assert.assertFalse(credentials.useSecurityToken());
Assert.assertFalse(credentials.willSoonExpire());
Assert.assertTrue(credentials.getAccessKeyId().startsWith("TMPSK."));
Assert.assertEquals(credentials.getAccessKeyId().length(), 130);
Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
} 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 testStsAssumeRoleCredentialsProvider.
@Test
public void testStsAssumeRoleCredentialsProvider() {
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);
BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
Assert.assertTrue(credentials.useSecurityToken());
Assert.assertFalse(credentials.willSoonExpire());
Assert.assertTrue(credentials.getAccessKeyId().startsWith("STS."));
Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
Assert.assertTrue(credentials.getSecretAccessKey().length() > 0);
Assert.assertTrue(credentials.getSecurityToken().length() > 0);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations