Search in sources :

Example 1 with S3Signer

use of com.amazonaws.services.s3.internal.S3Signer in project aws-sdk-android by aws-amplify.

the class S3SignerTest method testSign.

@Ignore
@Test
public void testSign() throws URISyntaxException {
    final GetObjectRequest gr = new GetObjectRequest("test-bucket123456", "TestFile.txt");
    final Request<?> req = new DefaultRequest(gr, Constants.S3_SERVICE_DISPLAY_NAME);
    req.setHttpMethod(HttpMethodName.GET);
    req.setResourcePath("/test-bucket123456/TestFile.txt");
    req.setEndpoint(new URI("https://test-bucket123456.s3-us-west-2.amazonaws.com"));
    req.addHeader(Headers.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8");
    final S3Signer signer = new S3Signer("GET", "/test-bucket123456/TestFile.txt");
    // These are fake bogus credentials just for tesitng
    signer.sign(req, new BasicAWSCredentials("AKI11BOGUS11ACCESS11KEYOZQ", "LYd/ZD611BOGUS11SECRET11KEYSiD6"), new Date(1431374979760L));
    assertEquals(getSignature(req), "kD6n4rzH5+82Nw5wFIhaD1pKXNM=");
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) URI(java.net.URI) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Date(java.util.Date) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with S3Signer

use of com.amazonaws.services.s3.internal.S3Signer in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method createSigner.

/**
 * Returns a "complete" S3 specific signer, taking into the S3 bucket, key,
 * and the current S3 client configuration into account.
 *
 * Signer = get a Signer based on the bucket endpoint // InternalConfig in CoreRuntime makes this decision
 * [If the region is not specified, uses SigV2 signer,
 * Else If the region is specified, and If the region only supports SigV4, then uses SigV4]
 * If Signer is not Overriden {
 *    If Signer is a V4 Signer and Region is null
 *       Get the region from AmazonS3Client or BucketRegionCache
 *       If available
 *           Use SigV4
 *       Else If PresignedUrl
 *           Use SigV2
 *   If Signer Region is overriden
 *       Use SigV4
 *   If BucketRegionCache has a region
 *       Use SigV4
 * }
 *
 * If Signer is SigV2 signer
 *      Use SigV2
 */
protected Signer createSigner(final Request<?> request, final String bucketName, final String key) {
    // Instead of using request.getEndpoint() for this parameter, we use
    // endpoint which is because
    // in accelerate mode, the endpoint in request is regionless. We need
    // the client-wide endpoint
    // to fetch the region information and pick the correct signer.
    final URI uri = clientOptions.isAccelerateModeEnabled() ? endpoint : request.getEndpoint();
    // This method retrieves the signer based on the URI. Currently
    // S3's default signer is a SigV2 signer implemented in S3Signer
    final Signer signer = getSignerByURI(uri);
    if (!isSignerOverridden()) {
        if ((signer instanceof AWSS3V4Signer) && noExplicitRegionProvided(request)) {
            final String region = clientRegion == null ? bucketRegionCache.get(bucketName) : clientRegion;
            if (region != null) {
                // If cache contains the region for the bucket, create an endpoint for the region and
                // update the request with that endpoint.
                resolveRequestEndpoint(request, bucketName, key, RuntimeHttpUtils.toUri(RegionUtils.getRegion(region).getServiceEndpoint(S3_SERVICE_NAME), clientConfiguration));
                AWSS3V4Signer sigV4Signer = (AWSS3V4Signer) signer;
                setAWSS3V4SignerWithServiceNameAndRegion((AWSS3V4Signer) signer, region);
                return sigV4Signer;
            } else if (request.getOriginalRequest() instanceof GeneratePresignedUrlRequest) {
                return createSigV2Signer(request, bucketName, key);
            }
        }
        // use the signer override if provided, else see if you can get the
        // signer from bucketreqion cache.
        final String regionOverride = getSignerRegionOverride() == null ? (clientRegion == null ? bucketRegionCache.get(bucketName) : clientRegion) : getSignerRegionOverride();
        if (regionOverride != null) {
            AWSS3V4Signer sigV4Signer = new AWSS3V4Signer();
            setAWSS3V4SignerWithServiceNameAndRegion(sigV4Signer, regionOverride);
            return sigV4Signer;
        }
    }
    if (signer instanceof S3Signer) {
        // new one with the appropriate values for this request.
        return createSigV2Signer(request, bucketName, key);
    }
    return signer;
}
Also used : S3Signer(com.amazonaws.services.s3.internal.S3Signer) S3QueryStringSigner(com.amazonaws.services.s3.internal.S3QueryStringSigner) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) Signer(com.amazonaws.auth.Signer) S3Signer(com.amazonaws.services.s3.internal.S3Signer) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) URI(java.net.URI)

Aggregations

URI (java.net.URI)2 DefaultRequest (com.amazonaws.DefaultRequest)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 Signer (com.amazonaws.auth.Signer)1 AWSS3V4Signer (com.amazonaws.services.s3.internal.AWSS3V4Signer)1 S3QueryStringSigner (com.amazonaws.services.s3.internal.S3QueryStringSigner)1 S3Signer (com.amazonaws.services.s3.internal.S3Signer)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 Date (java.util.Date)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1