use of com.aliyun.oss.common.auth.PublicKey in project aliyun-oss-java-sdk by aliyun.
the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderExpire.
@Test
public void testStsKeyPairCredentialsProviderExpire() {
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)).withExpiredFactor(0.001).withExpiredDuration(2000);
BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials();
Assert.assertFalse(credentials.willSoonExpire());
Thread.sleep(3000);
Assert.assertTrue(credentials.willSoonExpire());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.PublicKey in project aliyun-oss-java-sdk by aliyun.
the class STSKeyPairSessionCredentialsProviderTest method testStsKeyPairCredentialsProviderNegative.
@Test
public void testStsKeyPairCredentialsProviderNegative() throws ClientException, IOException {
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(899);
Assert.assertNull(credentialsProvider.getCredentials());
credentialsProvider = CredentialsProviderFactory.newSTSKeyPairSessionCredentialsProvider(TestConfig.RAM_REGION_ID, publicKey.getPublicKeyId(), AuthUtils.loadPrivateKeyFromFile(TestConfig.PRIVATE_KEY_PATH)).withExpiredDuration(100);
Assert.assertNull(credentialsProvider.getCredentials());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.PublicKey in project aliyun-oss-java-sdk by aliyun.
the class AuthUtils method uploadPublicKey.
/**
* Upload the public key of RSA key pair.
*
* @param regionId
* RAM's available area.
* @param accessKeyId
* Access Key ID of the root user.
* @param accessKeySecret
* Secret Access Key of the root user.
* @param publicKey
* Public key content.
* @return Public key description, include public key id etc.
* @throws ClientException
*/
public static PublicKey uploadPublicKey(String regionId, String accessKeyId, String accessKeySecret, String publicKey) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
UploadPublicKeyRequest uploadPublicKeyRequest = new UploadPublicKeyRequest();
uploadPublicKeyRequest.setPublicKeySpec(publicKey);
UploadPublicKeyResponse uploadPublicKeyResponse = client.getAcsResponse(uploadPublicKeyRequest);
com.aliyuncs.ram.model.v20150501.UploadPublicKeyResponse.PublicKey pubKey = uploadPublicKeyResponse.getPublicKey();
return new PublicKey(pubKey);
}
use of com.aliyun.oss.common.auth.PublicKey in project aliyun-oss-java-sdk by aliyun.
the class AuthUtils method listPublicKeys.
/**
* List the public keys that has been uploaded.
*
* @param regionId
* RAM's available area.
* @param accessKeyId
* Access Key ID of the root user.
* @param accessKeySecret
* Secret Access Key of the root user.
* @return Public keys.
* @throws ClientException
*/
public static List<PublicKey> listPublicKeys(String regionId, String accessKeyId, String accessKeySecret) throws ClientException {
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
ListPublicKeysRequest listPublicKeysRequest = new ListPublicKeysRequest();
ListPublicKeysResponse listPublicKeysResponse = client.getAcsResponse(listPublicKeysRequest);
List<PublicKey> publicKeys = new ArrayList<PublicKey>();
for (com.aliyuncs.ram.model.v20150501.ListPublicKeysResponse.PublicKey publicKey : listPublicKeysResponse.getPublicKeys()) {
publicKeys.add(new PublicKey(publicKey));
}
return publicKeys;
}
use of com.aliyun.oss.common.auth.PublicKey in project aliyun-oss-java-sdk by aliyun.
the class RamUtilsTest method testDeletePublicKey.
@Test
public void testDeletePublicKey() {
try {
// upload
String pubKey = AuthUtils.loadPublicKeyFromFile(TestConfig.PUBLIC_KEY_PATH);
PublicKey publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, pubKey);
Assert.assertNotNull(publicKey);
Assert.assertEquals(publicKey.getPublicKeyId().length(), "LTRSA.2Qcjm****XW7M0NO".length());
publicKey = AuthUtils.uploadPublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, pubKey);
Assert.assertNotNull(publicKey);
Assert.assertEquals(publicKey.getPublicKeyId().length(), "LTRSA.2Qcjm****XW7M0NO".length());
// check
List<PublicKey> publicKeys = AuthUtils.listPublicKeys(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET);
Assert.assertEquals(2, publicKeys.size());
// delete
for (PublicKey pk : publicKeys) {
AuthUtils.deletePublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, pk.getPublicKeyId());
}
// check
publicKeys = AuthUtils.listPublicKeys(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET);
Assert.assertEquals(0, publicKeys.size());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations