Search in sources :

Example 11 with Credentials

use of com.aliyun.oss.common.auth.Credentials in project hadoop by apache.

the class TestAliyunCredentials method validateCredential.

private void validateCredential(Configuration conf) {
    try {
        AliyunCredentialsProvider provider = new AliyunCredentialsProvider(conf);
        Credentials credentials = provider.getCredentials();
        fail("Expected a CredentialInitializationException, got " + credentials);
    } catch (InvalidCredentialsException expected) {
    // expected
    } catch (IOException e) {
        fail("Unexpected exception.");
    }
}
Also used : InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) IOException(java.io.IOException) Credentials(com.aliyun.oss.common.auth.Credentials)

Example 12 with Credentials

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

the class SwitchCredentialsAndEndpointTest method testSwitchCredentialsSynchronously.

@Test
public void testSwitchCredentialsSynchronously() throws Exception {
    /* Ensure credentials switching prior to credentials verification at first time */
    final Object ensureSwitchFirst = new Object();
    final Object verifySynchronizer = new Object();
    final Object switchSynchronizer = new Object();
    // Verify whether credentials switching work as expected
    Thread verifyThread = new Thread(new Runnable() {

        @Override
        public void run() {
            synchronized (ensureSwitchFirst) {
                if (!switchStarted) {
                    try {
                        ensureSwitchFirst.wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
            int l = 0;
            do {
                // Wait for credentials switching completion
                synchronized (verifySynchronizer) {
                    try {
                        verifySynchronizer.wait();
                    } catch (InterruptedException e) {
                    }
                }
                CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
                Credentials currentCreds = credsProvider.getCredentials();
                try {
                    String loc = ossClient.getBucketLocation(bucketName);
                    assertEquals(OSS_TEST_REGION, loc);
                    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, currentCreds.getAccessKeyId());
                    assertEquals(OSS_TEST_ACCESS_KEY_SECRET_1, currentCreds.getSecretAccessKey());
                } catch (OSSException ex) {
                    assertEquals(OSSErrorCode.INVALID_ACCESS_KEY_ID, ex.getErrorCode());
                    assertEquals(OSS_TEST_ACCESS_KEY_ID, currentCreds.getAccessKeyId());
                    assertEquals(OSS_TEST_ACCESS_KEY_SECRET, currentCreds.getSecretAccessKey());
                }
                // Notify credentials switching
                synchronized (switchSynchronizer) {
                    switchSynchronizer.notify();
                }
            } while (++l < loopTimes);
        }
    });
    // Switch credentials(including valid and invalid ones) synchronously
    Thread switchThread = new Thread(new Runnable() {

        @Override
        public void run() {
            int l = 0;
            boolean firstSwitch = false;
            do {
                Credentials secondCreds = new DefaultCredentials(OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
                ossClient.switchCredentials(secondCreds);
                CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
                secondCreds = credsProvider.getCredentials();
                assertEquals(OSS_TEST_ACCESS_KEY_ID, secondCreds.getAccessKeyId());
                assertEquals(OSS_TEST_ACCESS_KEY_SECRET, secondCreds.getSecretAccessKey());
                if (!firstSwitch) {
                    synchronized (ensureSwitchFirst) {
                        switchStarted = true;
                        ensureSwitchFirst.notify();
                    }
                    firstSwitch = true;
                }
                try {
                    Thread.sleep(switchInterval);
                } catch (InterruptedException e) {
                }
                /* 
                     * Notify credentials verification and wait for next credentials switching.
                     * TODO: The two synchronized clauses below should be combined as atomic operation.
                     */
                synchronized (verifySynchronizer) {
                    verifySynchronizer.notify();
                }
                synchronized (switchSynchronizer) {
                    try {
                        switchSynchronizer.wait();
                    } catch (InterruptedException e) {
                    }
                }
            } while (++l < loopTimes);
        }
    });
    verifyThread.start();
    switchThread.start();
    verifyThread.join();
    switchThread.join();
    restoreDefaultCredentials();
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSException(com.aliyun.oss.OSSException) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) Test(org.junit.Test)

Example 13 with Credentials

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

the class SwitchCredentialsAndEndpointTest method testSwitchInvalidCredentialsAndEndpoint.

@Test
public void testSwitchInvalidCredentialsAndEndpoint() {
    CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
    Credentials defaultCreds = credsProvider.getCredentials();
    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getAccessKeyId());
    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds.getSecretAccessKey());
    // Switch to invalid credentials
    Credentials invalidCreds = new DefaultCredentials(INVALID_ACCESS_ID, INVALID_ACCESS_KEY);
    ossClient.switchCredentials(invalidCreds);
    // Verify invalid credentials under default endpoint
    try {
        ossClient.getBucketLocation(bucketName);
        fail("Should not be able to get bucket location with invalid credentials.");
    } catch (OSSException ex) {
        assertEquals(OSSErrorCode.INVALID_ACCESS_KEY_ID, ex.getErrorCode());
    }
    // Switch to valid endpoint
    ossClient.setEndpoint(INVALID_ENDPOINT);
    // Verify second credentials under invalid endpoint
    try {
        ossClient.getBucketLocation(bucketName);
        fail("Should not be able to get bucket location with second credentials.");
    } catch (ClientException ex) {
        assertEquals(ClientErrorCode.UNKNOWN_HOST, ex.getErrorCode());
    } finally {
        restoreDefaultCredentials();
        restoreDefaultEndpoint();
    }
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSException(com.aliyun.oss.OSSException) CredentialsProvider(com.aliyun.oss.common.auth.CredentialsProvider) ClientException(com.aliyun.oss.ClientException) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) Test(org.junit.Test)

Example 14 with Credentials

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

the class ProxySignTest method testProxyAuth.

@Ignore
public void testProxyAuth() {
    String bucketName = "sdk-test-md-1";
    String key = "mingdi/test.txt";
    String content = "Hello OSS.";
    String proxyHost = "";
    String endpoint = "";
    String accessKeyId = "";
    String secretAccessKey = "";
    try {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setProxyHost(proxyHost);
        conf.setProxyPort(8080);
        Credentials credentials = new ProxyCredentials("mingditest");
        ProxyRequestSigner proxySigner = new ProxyRequestSigner(credentials);
        List<RequestSigner> signerHandlers = new LinkedList<RequestSigner>();
        signerHandlers.add(proxySigner);
        conf.setSignerHandlers(signerHandlers);
        Map<String, String> proxyHeaders = new LinkedHashMap<String, String>();
        proxyHeaders.put(HEADER_PROXY_TYPE, "default");
        proxyHeaders.put(HEADER_PROXY_USER, "diff_bucket_sync_test_user_1");
        proxyHeaders.put(HEADER_PROXY_DEST, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_DEST_REGION, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_REAL_HOST, endpoint);
        conf.setDefaultHeaders(proxyHeaders);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey, conf);
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.getObject(bucketName, key);
        ossClient.deleteObject(bucketName, key);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : LinkedList(java.util.LinkedList) OSS(com.aliyun.oss.OSS) LinkedHashMap(java.util.LinkedHashMap) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Credentials(com.aliyun.oss.common.auth.Credentials) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) RequestSigner(com.aliyun.oss.common.auth.RequestSigner) Ignore(org.junit.Ignore)

Example 15 with Credentials

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

the class BucketAclTest method testUnormalDoesBucketExist.

@Test
public void testUnormalDoesBucketExist() {
    final String nonexistentBucket = "unormal-does-bucket-exist";
    try {
        Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        OSSClient ossClient = new OSSClient("http://oss-cn-taikang.aliyuncs.com", new DefaultCredentialProvider(credentials));
        ossClient.doesBucketExist(nonexistentBucket);
        Assert.fail("Does bucket exist should not be successful");
    } catch (Exception e) {
        Assert.assertEquals("UnknownHost", e.getMessage());
    }
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

Credentials (com.aliyun.oss.common.auth.Credentials)25 Test (org.junit.Test)18 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)8 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)7 HashMap (java.util.HashMap)7 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 OSS (com.aliyun.oss.OSS)4 OSSException (com.aliyun.oss.OSSException)4 BasicCredentials (com.aliyun.oss.common.auth.BasicCredentials)4 CustomSessionCredentialsProvider (com.aliyun.oss.common.auth.CustomSessionCredentialsProvider)4 InvalidCredentialsException (com.aliyun.oss.common.auth.InvalidCredentialsException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OSSClient (com.aliyun.oss.OSSClient)3 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)3 EnvironmentVariableCredentialsProvider (com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider)3 ProfileConfigFile (com.aliyun.oss.common.auth.ProfileConfigFile)3 SystemPropertiesCredentialsProvider (com.aliyun.oss.common.auth.SystemPropertiesCredentialsProvider)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3