Search in sources :

Example 16 with Credentials

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Credentials(com.aliyun.oss.common.auth.Credentials)

Example 17 with Credentials

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());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 18 with Credentials

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());
    }
}
Also used : CustomSessionCredentialsFetcherMock(com.aliyun.oss.common.provider.mock.CustomSessionCredentialsFetcherMock) CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) CustomSessionCredentialsFetcher(com.aliyun.oss.common.auth.CustomSessionCredentialsFetcher) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 19 with Credentials

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());
    }
}
Also used : HashMap(java.util.HashMap) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) File(java.io.File) Credentials(com.aliyun.oss.common.auth.Credentials) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) Test(org.junit.Test)

Example 20 with Credentials

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());
    }
}
Also used : HashMap(java.util.HashMap) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) ProfileConfigFile(com.aliyun.oss.common.auth.ProfileConfigFile) File(java.io.File) Credentials(com.aliyun.oss.common.auth.Credentials) InvalidCredentialsException(com.aliyun.oss.common.auth.InvalidCredentialsException) 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