Search in sources :

Example 6 with PublicKey

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

the class RamUtilsTest method testListPublicKey.

@Test
public void testListPublicKey() {
    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);
        // check
        List<PublicKey> publicKeys = AuthUtils.listPublicKeys(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET);
        Assert.assertEquals(1, publicKeys.size());
        Assert.assertEquals(publicKeys.get(0).getPublicKeyId().length(), "LTRSA.2Qcjm****XW7M0NO".length());
        Assert.assertEquals(publicKeys.get(0).getCreateDate().length(), "2017-11-01T08:00:00Z".length());
        Assert.assertEquals(publicKeys.get(0).getStatus(), "Active");
        Assert.assertEquals(publicKeys.get(0).getPublicKeySpec(), null);
        // delete
        AuthUtils.deletePublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, publicKey.getPublicKeyId());
    } 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)

Example 7 with PublicKey

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

the class RamUtilsTest method testUploadPublicKey.

@Test
public void testUploadPublicKey() {
    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());
        // check
        List<PublicKey> publicKeys = AuthUtils.listPublicKeys(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET);
        Assert.assertEquals(1, publicKeys.size());
        // delete
        AuthUtils.deletePublicKey(TestConfig.RAM_REGION_ID, TestConfig.ROOT_ACCESS_KEY_ID, TestConfig.ROOT_ACCESS_KEY_SECRET, publicKey.getPublicKeyId());
    } 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)

Example 8 with PublicKey

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

Example 9 with PublicKey

use of com.aliyun.oss.common.auth.PublicKey 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());
    }
}
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)

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