use of com.aliyun.oss.common.auth.Credentials in project aliyun-oss-java-sdk by aliyun.
the class OSSClient method generatePresignedUrl.
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest request) throws ClientException {
assertParameterNotNull(request, "request");
String bucketName = request.getBucketName();
if (request.getBucketName() == null) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetBucketName"));
}
ensureBucketNameValid(request.getBucketName());
if (request.getExpiration() == null) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetExpiration"));
}
Credentials currentCreds = credsProvider.getCredentials();
String accessId = currentCreds.getAccessKeyId();
String accessKey = currentCreds.getSecretAccessKey();
boolean useSecurityToken = currentCreds.useSecurityToken();
HttpMethod method = request.getMethod() != null ? request.getMethod() : HttpMethod.GET;
String expires = String.valueOf(request.getExpiration().getTime() / 1000L);
String key = request.getKey();
ClientConfiguration config = serviceClient.getClientConfiguration();
String resourcePath = OSSUtils.determineResourcePath(bucketName, key, config.isSLDEnabled());
RequestMessage requestMessage = new RequestMessage(bucketName, key);
requestMessage.setEndpoint(OSSUtils.determineFinalEndpoint(endpoint, bucketName, config));
requestMessage.setMethod(method);
requestMessage.setResourcePath(resourcePath);
requestMessage.setHeaders(request.getHeaders());
requestMessage.addHeader(HttpHeaders.DATE, expires);
if (request.getContentType() != null && !request.getContentType().trim().equals("")) {
requestMessage.addHeader(HttpHeaders.CONTENT_TYPE, request.getContentType());
}
if (request.getContentMD5() != null && request.getContentMD5().trim().equals("")) {
requestMessage.addHeader(HttpHeaders.CONTENT_MD5, request.getContentMD5());
}
for (Map.Entry<String, String> h : request.getUserMetadata().entrySet()) {
requestMessage.addHeader(OSSHeaders.OSS_USER_METADATA_PREFIX + h.getKey(), h.getValue());
}
Map<String, String> responseHeaderParams = new HashMap<String, String>();
populateResponseHeaderParameters(responseHeaderParams, request.getResponseHeaders());
if (responseHeaderParams.size() > 0) {
requestMessage.setParameters(responseHeaderParams);
}
if (request.getQueryParameter() != null && request.getQueryParameter().size() > 0) {
for (Map.Entry<String, String> entry : request.getQueryParameter().entrySet()) {
requestMessage.addParameter(entry.getKey(), entry.getValue());
}
}
if (request.getProcess() != null && !request.getProcess().trim().equals("")) {
requestMessage.addParameter(RequestParameters.SUBRESOURCE_PROCESS, request.getProcess());
}
if (useSecurityToken) {
requestMessage.addParameter(SECURITY_TOKEN, currentCreds.getSecurityToken());
}
String canonicalResource = "/" + ((bucketName != null) ? bucketName : "") + ((key != null ? "/" + key : ""));
String canonicalString = SignUtils.buildCanonicalString(method.toString(), canonicalResource, requestMessage, expires);
String signature = ServiceSignature.create().computeSignature(accessKey, canonicalString);
Map<String, String> params = new LinkedHashMap<String, String>();
params.put(HttpHeaders.EXPIRES, expires);
params.put(OSS_ACCESS_KEY_ID, accessId);
params.put(SIGNATURE, signature);
params.putAll(requestMessage.getParameters());
String queryString = HttpUtil.paramToQueryString(params, DEFAULT_CHARSET_NAME);
/* Compse HTTP request uri. */
String url = requestMessage.getEndpoint().toString();
if (!url.endsWith("/")) {
url += "/";
}
url += resourcePath + "?" + queryString;
try {
return new URL(url);
} catch (MalformedURLException e) {
throw new ClientException(e);
}
}
use of com.aliyun.oss.common.auth.Credentials in project aliyun-oss-java-sdk by aliyun.
the class CustomSessionCredentialsProviderTest method testGetCredentialsFromAuthInOss.
@Test
public void testGetCredentialsFromAuthInOss() {
try {
CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
Credentials credentials = credentialsProvider.getCredentials();
Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
Assert.assertEquals(credentials.getSecurityToken().length(), 516);
Assert.assertTrue(credentials.useSecurityToken());
String key = "test.txt";
String content = "HelloOSS";
OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSecurityToken());
ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.Credentials in project aliyun-oss-java-sdk by aliyun.
the class CustomSessionCredentialsProviderTest method testGetExceptionalCredentials.
@Test
public void testGetExceptionalCredentials() {
try {
CustomSessionCredentialsFetcher credentialsFetcher = new CustomSessionCredentialsFetcherMock(TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.Exceptional);
CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher);
Credentials credentials = credentialsProvider.getCredentials();
Assert.assertNull(credentials);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.Credentials in project aliyun-oss-java-sdk by aliyun.
the class ProfilesConfigFileTest method testGetCredentialsWithDefaultParameters.
@Test
public void testGetCredentialsWithDefaultParameters() {
try {
// STS
Map<String, String> options = new HashMap<String, String>();
options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
options.put(AuthUtils.OSS_SESSION_TOKEN, TEST_SECURITY_TOKEN);
generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
ProfileConfigFile configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
Credentials credentials = configFile.getCredentials();
Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
Assert.assertEquals(TEST_SECURITY_TOKEN, credentials.getSecurityToken());
Assert.assertTrue(credentials.useSecurityToken());
new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
// Normal
options = new HashMap<String, String>();
options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
credentials = configFile.getCredentials();
Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
Assert.assertNull(credentials.getSecurityToken());
Assert.assertFalse(credentials.useSecurityToken());
new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.common.auth.Credentials in project aliyun-oss-java-sdk by aliyun.
the class ProfilesConfigFileTest method testGetFreshCredentials.
@Test
public void testGetFreshCredentials() {
try {
new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
Thread.sleep(1000);
Map<String, String> options = new HashMap<String, String>();
options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
ProfileConfigFile configFile = new ProfileConfigFile(AuthUtils.DEFAULT_PROFILE_PATH);
Credentials credentials = configFile.getCredentials();
Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
Assert.assertNull(credentials.getSecurityToken());
Assert.assertFalse(credentials.useSecurityToken());
new File(AuthUtils.DEFAULT_PROFILE_PATH).delete();
Thread.sleep(1000);
// Fresh
options = new HashMap<String, String>();
options.put(AuthUtils.OSS_ACCESS_KEY_ID, TEST_ACCESS_KEY_ID);
options.put(AuthUtils.OSS_SECRET_ACCESS_KEY, TEST_ACCESS_KEY_SECRET);
options.put(AuthUtils.OSS_SESSION_TOKEN, TEST_SECURITY_TOKEN);
generateProfileFile(AuthUtils.DEFAULT_PROFILE_PATH, AuthUtils.DEFAULT_SECTION_NAME, options);
credentials = configFile.getCredentials();
Assert.assertEquals(TEST_ACCESS_KEY_ID, credentials.getAccessKeyId());
Assert.assertEquals(TEST_ACCESS_KEY_SECRET, credentials.getSecretAccessKey());
Assert.assertEquals(TEST_SECURITY_TOKEN, credentials.getSecurityToken());
Assert.assertTrue(credentials.useSecurityToken());
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations