Search in sources :

Example 1 with PublicKey

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());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 2 with PublicKey

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());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) IOException(java.io.IOException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Example 3 with PublicKey

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);
}
Also used : DefaultProfile(com.aliyuncs.profile.DefaultProfile) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) PublicKey(com.aliyun.oss.common.auth.PublicKey) UploadPublicKeyRequest(com.aliyuncs.ram.model.v20150501.UploadPublicKeyRequest) UploadPublicKeyResponse(com.aliyuncs.ram.model.v20150501.UploadPublicKeyResponse)

Example 4 with PublicKey

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;
}
Also used : DefaultProfile(com.aliyuncs.profile.DefaultProfile) DefaultAcsClient(com.aliyuncs.DefaultAcsClient) PublicKey(com.aliyun.oss.common.auth.PublicKey) ArrayList(java.util.ArrayList) ListPublicKeysRequest(com.aliyuncs.ram.model.v20150501.ListPublicKeysRequest) ListPublicKeysResponse(com.aliyuncs.ram.model.v20150501.ListPublicKeysResponse)

Example 5 with PublicKey

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());
    }
}
Also used : PublicKey(com.aliyun.oss.common.auth.PublicKey) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ClientException(com.aliyuncs.exceptions.ClientException) Test(org.junit.Test)

Aggregations

PublicKey (com.aliyun.oss.common.auth.PublicKey)9 ClientException (com.aliyuncs.exceptions.ClientException)7 IOException (java.io.IOException)7 Test (org.junit.Test)7 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)4 FileNotFoundException (java.io.FileNotFoundException)3 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)2 DefaultAcsClient (com.aliyuncs.DefaultAcsClient)2 DefaultProfile (com.aliyuncs.profile.DefaultProfile)2 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 ListPublicKeysRequest (com.aliyuncs.ram.model.v20150501.ListPublicKeysRequest)1 ListPublicKeysResponse (com.aliyuncs.ram.model.v20150501.ListPublicKeysResponse)1 UploadPublicKeyRequest (com.aliyuncs.ram.model.v20150501.UploadPublicKeyRequest)1 UploadPublicKeyResponse (com.aliyuncs.ram.model.v20150501.UploadPublicKeyResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1