Search in sources :

Example 6 with Credentials

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

the class SwitchCredentialsAndEndpointTest method restoreDefaultCredentials.

private static void restoreDefaultCredentials() {
    Credentials credentials = new DefaultCredentials(OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
    ossClient.switchCredentials(credentials);
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials)

Example 7 with Credentials

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

the class SwitchCredentialsAndEndpointTest method testSwitchEndpointSynchronously.

@Test
public void testSwitchEndpointSynchronously() throws Exception {
    /* Ensure endpoint switching prior to endpoint verification at first time */
    final Object ensureSwitchFirst = new Object();
    final Object verifySynchronizer = new Object();
    final Object switchSynchronizer = new Object();
    // Verify whether endpoint 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 endpoint switching completion
                synchronized (verifySynchronizer) {
                    try {
                        verifySynchronizer.wait();
                    } catch (InterruptedException e) {
                    }
                }
                CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
                Credentials currentCreds = credsProvider.getCredentials();
                String loc = ossClient.getBucketLocation(bucketName);
                assertEquals(OSS_TEST_REGION, loc);
                assertEquals(OSS_TEST_ACCESS_KEY_ID, currentCreds.getAccessKeyId());
                assertEquals(OSS_TEST_ACCESS_KEY_SECRET, currentCreds.getSecretAccessKey());
                /*
                     * Since the default OSSClient is the same as the second OSSClient, let's
                     * do a simple verification. 
                     */
                String secondLoc = ossClient.getBucketLocation(bucketName);
                assertEquals(loc, secondLoc);
                assertEquals(OSS_TEST_REGION, secondLoc);
                CredentialsProvider secondCredsProvider = ossClient.getCredentialsProvider();
                Credentials secondCreds = secondCredsProvider.getCredentials();
                assertEquals(OSS_TEST_ACCESS_KEY_ID, secondCreds.getAccessKeyId());
                assertEquals(OSS_TEST_ACCESS_KEY_SECRET, secondCreds.getSecretAccessKey());
                // Notify endpoint switching
                synchronized (switchSynchronizer) {
                    restoreDefaultCredentials();
                    restoreDefaultEndpoint();
                    switchSynchronizer.notify();
                }
            } while (++l < loopTimes);
        }
    });
    // Switch endpoint synchronously
    Thread switchThread = new Thread(new Runnable() {

        @Override
        public void run() {
            int l = 0;
            boolean firstSwitch = false;
            do {
                /* 
                     * Switch both credentials and endpoint, now the default OSSClient is the same as 
                     * the second OSSClient actually.
                     */
                Credentials secondCreds = new DefaultCredentials(OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
                ossClient.switchCredentials(secondCreds);
                ossClient.setEndpoint(OSS_TEST_ENDPOINT);
                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();
    restoreDefaultEndpoint();
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) 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 8 with Credentials

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

the class SwitchCredentialsAndEndpointTest method testSwitchValidCredentialsAndEndpoint.

@Ignore
public void testSwitchValidCredentialsAndEndpoint() {
    CredentialsProvider credsProvider = ossClient.getCredentialsProvider();
    Credentials defaultCreds = credsProvider.getCredentials();
    assertEquals(OSS_TEST_ACCESS_KEY_ID, defaultCreds.getAccessKeyId());
    assertEquals(OSS_TEST_ACCESS_KEY_SECRET, defaultCreds.getSecretAccessKey());
    // Verify default credentials under default endpoint
    try {
        String loc = ossClient.getBucketLocation(bucketName);
        assertEquals(OSS_TEST_REGION, loc);
    } catch (OSSException ex) {
        fail("Unable to get bucket location with default credentials.");
    }
    // Switch to another default credentials that belongs to the same user acount.
    Credentials defaultCreds2 = new DefaultCredentials(OSS_TEST_ACCESS_KEY_ID_1, OSS_TEST_ACCESS_KEY_SECRET_1);
    ossClient.switchCredentials(defaultCreds2);
    defaultCreds2 = credsProvider.getCredentials();
    assertEquals(OSS_TEST_ACCESS_KEY_ID_1, defaultCreds2.getAccessKeyId());
    assertEquals(OSS_TEST_ACCESS_KEY_SECRET_1, defaultCreds2.getSecretAccessKey());
    // Verify another default credentials under default endpoint
    try {
        String loc = ossClient.getBucketLocation(bucketName);
        assertEquals(OSS_TEST_REGION, loc);
    } catch (OSSException ex) {
        restoreDefaultCredentials();
        fail("Unable to get bucket location with another default credentials.");
    }
    // Switch to second credentials that belongs to another user acount,
    // Note that the default credentials are only valid under default endpoint
    // and the second credentials are only valid under second endpoint.
    Credentials secondCreds = new DefaultCredentials(OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
    ossClient.switchCredentials(secondCreds);
    secondCreds = credsProvider.getCredentials();
    assertEquals(OSS_TEST_ACCESS_KEY_ID, secondCreds.getAccessKeyId());
    assertEquals(OSS_TEST_ACCESS_KEY_SECRET, secondCreds.getSecretAccessKey());
    // Verify second credentials under default endpoint
    try {
        ossClient.getBucketLocation(bucketName);
        fail("Should not be able to get bucket location with second credentials.");
    } catch (OSSException ex) {
        assertEquals(OSSErrorCode.INVALID_ACCESS_KEY_ID, ex.getErrorCode());
    }
    // Switch to second endpoint
    ossClient.setEndpoint(OSS_TEST_ENDPOINT);
    // Verify second credentials under second endpoint
    try {
        assertEquals(OSS_TEST_ENDPOINT, ossClient.getEndpoint().toString());
        String loc = ossClient.getBucketLocation(bucketName);
        assertEquals(OSS_TEST_REGION, loc);
        // After switching both credentials and endpoint, the default OSSClient is the same
        // as the second OSSClient actually.
        assertEquals(OSS_TEST_ENDPOINT, ossClient.getEndpoint().toString());
        loc = ossClient.getBucketLocation(bucketName);
        assertEquals(OSS_TEST_REGION, loc);
    } catch (OSSException ex) {
        fail("Unable to create bucket with second credentials.");
    } finally {
        restoreDefaultCredentials();
        restoreDefaultEndpoint();
    }
}
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) Ignore(org.junit.Ignore)

Example 9 with Credentials

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

the class TestBase method getOSSClient.

public static OSSClient getOSSClient() {
    if (ossClient == null) {
        resetTestConfig();
        ClientConfiguration conf = new ClientConfiguration().setSupportCname(false);
        Credentials credentials = new DefaultCredentials(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        ossClient = new OSSClient(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(credentials), conf);
    }
    return ossClient;
}
Also used : DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) ClientConfiguration(com.aliyun.oss.ClientConfiguration) Credentials(com.aliyun.oss.common.auth.Credentials) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials)

Example 10 with Credentials

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

the class ClientBuilderTest method testClientBuilderWithSTS.

@Test
public void testClientBuilderWithSTS() {
    try {
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setSupportCname(true);
        config.setConnectionTimeout(10000);
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET, "TOKEN"), config);
        Assert.assertTrue(ossClient.getClientConfiguration().isSupportCname());
        Assert.assertEquals(ossClient.getClientConfiguration().getConnectionTimeout(), 10000);
        Credentials cred = ossClient.getCredentialsProvider().getCredentials();
        Assert.assertEquals(cred.getAccessKeyId(), TestConfig.OSS_TEST_ACCESS_KEY_ID);
        Assert.assertEquals(cred.getSecretAccessKey(), TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        Assert.assertEquals(cred.getSecurityToken(), "TOKEN");
        Assert.assertTrue(cred.useSecurityToken());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Credentials(com.aliyun.oss.common.auth.Credentials) 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