use of com.aliyun.oss.common.auth.DefaultCredentials 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();
}
use of com.aliyun.oss.common.auth.DefaultCredentials 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();
}
}
use of com.aliyun.oss.common.auth.DefaultCredentials 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());
}
}
use of com.aliyun.oss.common.auth.DefaultCredentials in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperationTest method testPopulateCopyObjectHeaders.
@Test
public void testPopulateCopyObjectHeaders() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, URISyntaxException {
CopyObjectRequest request = new CopyObjectRequest("srcbucket", "srckey", "destbucket", "destkey");
request.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
Method[] testMethods = OSSObjectOperation.class.getDeclaredMethods();
Method testMethod = null;
for (Method m : testMethods) {
if (m.getName().equals("populateCopyObjectHeaders")) {
testMethod = m;
}
}
testMethod.setAccessible(true);
OSSObjectOperation operations = new OSSObjectOperation(new DefaultServiceClient(new ClientConfiguration()), new DefaultCredentialProvider(new DefaultCredentials("id", "key")));
Map<String, String> headers = new HashMap<String, String>();
Object[] params = new Object[2];
params[0] = request;
params[1] = headers;
testMethod.invoke(operations, params);
assertEquals("/srcbucket/srckey", headers.get(OSSHeaders.COPY_OBJECT_SOURCE));
assertEquals(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION, headers.get(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION));
assertEquals(null, headers.get(OSSHeaders.COPY_OBJECT_METADATA_DIRECTIVE));
}
Aggregations