Search in sources :

Example 1 with Signer

use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.

the class AmazonHttpClientTest method testTemporaryRedirect.

@Test
public void testTemporaryRedirect() throws IOException, URISyntaxException {
    HttpResponse redirectResponse = HttpResponse.builder().statusCode(307).header("Location", "https://www.redirect.com").build();
    HttpResponse successfulResponse = HttpResponse.builder().statusCode(200).content(null).build();
    final Request<?> request = new DefaultRequest<String>(new AmazonWebServiceRequest() {
    }, "TestService");
    request.setHttpMethod(HttpMethodName.GET);
    request.setEndpoint(new URI("https://www.test.com"));
    request.setResourcePath("/test/table");
    HttpResponseHandler<AmazonWebServiceResponse<String>> responseHandler = new HttpResponseHandler<AmazonWebServiceResponse<String>>() {

        @Override
        public AmazonWebServiceResponse<String> handle(HttpResponse response) throws Exception {
            AmazonWebServiceResponse<String> awsResponse = new AmazonWebServiceResponse<String>();
            awsResponse.setResult("Result");
            return awsResponse;
        }

        @Override
        public boolean needsConnectionLeftOpen() {
            return false;
        }
    };
    ExecutionContext ec = EasyMock.createMock(ExecutionContext.class);
    EasyMock.reset(httpClient, ec);
    final List<Boolean> signerCalled = new ArrayList<Boolean>();
    EasyMock.expect(ec.getAwsRequestMetrics()).andReturn(new AWSRequestMetrics()).anyTimes();
    EasyMock.expect(ec.getContextUserAgent()).andReturn("TestUserAgent").anyTimes();
    EasyMock.expect(ec.getCredentials()).andReturn(new AnonymousAWSCredentials());
    EasyMock.expect(ec.getSignerByURI(EasyMock.anyObject(URI.class))).andReturn(new Signer() {

        @Override
        public void sign(Request<?> requestToSign, AWSCredentials credentials) {
            assertSame(request, requestToSign);
            assertTrue(credentials instanceof AnonymousAWSCredentials);
            signerCalled.add(true);
        }
    });
    EasyMock.expect(httpClient.execute(EasyMock.<HttpRequest>anyObject())).andReturn(redirectResponse);
    Capture<HttpRequest> capture = new Capture<HttpRequest>();
    EasyMock.expect(httpClient.execute(EasyMock.capture(capture))).andReturn(successfulResponse);
    EasyMock.replay(httpClient, ec);
    Response<String> response = client.executeHelper(request, responseHandler, null, ec);
    assertEquals(response.getAwsResponse(), "Result");
    assertEquals(signerCalled.size(), 2);
    assertTrue(signerCalled.get(0));
    assertTrue(signerCalled.get(1));
    assertEquals(capture.getValue().getUri().toString(), "https://www.redirect.com/");
    EasyMock.verify(httpClient, ec);
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) AWSRequestMetrics(com.amazonaws.util.AWSRequestMetrics) ArrayList(java.util.ArrayList) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AmazonWebServiceResponse(com.amazonaws.AmazonWebServiceResponse) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest) URI(java.net.URI) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) Capture(org.easymock.Capture) Signer(com.amazonaws.auth.Signer) Test(org.junit.Test)

Example 2 with Signer

use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.

the class AmazonPollyPresigningClient method getPresignedSynthesizeSpeechUrl.

/**
 * <p>
 * Returns a pre-signed URL for accessing an Amazon Polly resource.
 * </p>
 * <p>
 * Pre-signed URLs allow clients to form a URL for an Amazon Polly
 * resource, and then sign it with the current AWS security credentials.
 * The pre-signed URL can be shared to other users, allowing access to
 * the resource without providing an account's AWS security credentials.
 * </p>
 * <p>
 * Pre-signed URLs are useful in many situations where AWS security
 * credentials aren't available from the client that needs to make the
 * actual request to Amazon Polly.
 * </p>
 * <p>
 * For example, a pre-signed URL to GET the synthesized speech audio stream
 * using the owner's AWS account can be generated and passed to a system
 * media player.
 * </p>
 *
 * @param synthesizeSpeechPresignRequest Object providing pre-signed synthesize speech
 *              request parameters.
 * @return Pre-signed URL of audio stream containing synthesized speech
 *              matching parameters provided in synthesizeSpeechPresignRequest
 *              that is possible to obtain using HTTP GET method.
 */
public URL getPresignedSynthesizeSpeechUrl(SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest) {
    Request<PresigningRequest> request = new SynthesizeSpeechPresignRequestMarshaller().marshall(synthesizeSpeechPresignRequest);
    request.setEndpoint(endpoint);
    request.setTimeOffset(timeOffset);
    if (synthesizeSpeechPresignRequest.getExpiration() == null) {
        synthesizeSpeechPresignRequest.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * DEFAULT_GET_REQUEST_EXPIRATION_MINUTES));
    }
    Signer signer = getSignerByURI(endpoint);
    if (!(signer instanceof Presigner)) {
        throw new AmazonClientException("Unsupported signer");
    }
    Presigner presigner = (Presigner) signer;
    Date expirationDate = synthesizeSpeechPresignRequest.getExpiration();
    AWSCredentials credentials = synthesizeSpeechPresignRequest.getRequestCredentials();
    if (credentials == null) {
        credentials = awsCredentialsProvider.getCredentials();
    }
    presigner.presignRequest(request, credentials, expirationDate);
    // Remove the leading slash (if any) in the resource-path
    return ServiceUtils.convertRequestToUrl(request, true);
}
Also used : Signer(com.amazonaws.auth.Signer) AmazonClientException(com.amazonaws.AmazonClientException) SynthesizeSpeechPresignRequestMarshaller(com.amazonaws.services.polly.model.transform.SynthesizeSpeechPresignRequestMarshaller) PresigningRequest(com.amazonaws.services.polly.internal.PresigningRequest) AWSCredentials(com.amazonaws.auth.AWSCredentials) Date(java.util.Date) Presigner(com.amazonaws.auth.Presigner) AmazonPollyCustomPresigner(com.amazonaws.services.polly.internal.AmazonPollyCustomPresigner)

Example 3 with Signer

use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.

the class AmazonWebServiceClient method setSignerRegionOverride.

/**
 * An internal method used to explicitly override the internal signer region
 * computed by the default implementation. This method is not expected to be
 * normally called except for AWS internal development purposes.
 * @param signerRegionOverride  the signer region override.
 */
@SuppressWarnings("checkstyle:hiddenfield")
public final void setSignerRegionOverride(final String signerRegionOverride) {
    final Signer signer = computeSignerByURI(endpoint, signerRegionOverride, true);
    synchronized (this) {
        this.signer = signer;
        this.signerRegionOverride = signerRegionOverride;
    }
}
Also used : Signer(com.amazonaws.auth.Signer) RegionAwareSigner(com.amazonaws.auth.RegionAwareSigner)

Example 4 with Signer

use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.

the class Amazons3ClientTest method testCreateSigner.

@Test
public void testCreateSigner() {
    s3.setS3ClientOptions(accelerateOption);
    final Regions region = Regions.US_WEST_2;
    s3.setRegion(Region.getRegion(region));
    final String bucketName = "bucket";
    final String key = "key";
    final HttpMethodName method = HttpMethodName.GET;
    final GetObjectRequest originalRequest = new GetObjectRequest(bucketName, key);
    final Request<?> request = s3.createRequest(bucketName, key, originalRequest, method);
    final Signer signer = s3.createSigner(request, bucketName, key);
    assertTrue(signer instanceof AWSS3V4Signer);
    signer.sign(request, creds);
    final String authorization = request.getHeaders().get("Authorization");
    assertNotNull(authorization);
    final String regionName = authorization.split("/")[2];
    assertEquals(region.getName(), regionName);
}
Also used : HttpMethodName(com.amazonaws.http.HttpMethodName) Signer(com.amazonaws.auth.Signer) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) Regions(com.amazonaws.regions.Regions) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.Test)

Example 5 with Signer

use of com.amazonaws.auth.Signer in project aws-sdk-android by aws-amplify.

the class Amazons3ClientTest method testCreateSignerWithSpecialCharacterKeys.

@Test
public void testCreateSignerWithSpecialCharacterKeys() {
    s3.setS3ClientOptions(accelerateOption);
    final Regions region = Regions.US_WEST_2;
    s3.setRegion(Region.getRegion(region));
    final String bucketName = "bucket";
    final String key = "key%^!@#*()";
    final HttpMethodName method = HttpMethodName.GET;
    final GetObjectRequest originalRequest = new GetObjectRequest(bucketName, key);
    final Request<?> request = s3.createRequest(bucketName, key, originalRequest, method);
    final Signer signer = s3.createSigner(request, bucketName, key);
    assertTrue(signer instanceof AWSS3V4Signer);
    signer.sign(request, creds);
    final String authorization = request.getHeaders().get("Authorization");
    assertNotNull(authorization);
    final String regionName = authorization.split("/")[2];
    assertEquals(region.getName(), regionName);
    assertTrue(request.getResourcePath().contains(key));
}
Also used : HttpMethodName(com.amazonaws.http.HttpMethodName) Signer(com.amazonaws.auth.Signer) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) Regions(com.amazonaws.regions.Regions) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.Test)

Aggregations

Signer (com.amazonaws.auth.Signer)15 AWSS3V4Signer (com.amazonaws.services.s3.internal.AWSS3V4Signer)6 URI (java.net.URI)5 RegionAwareSigner (com.amazonaws.auth.RegionAwareSigner)4 AWS4Signer (com.amazonaws.auth.AWS4Signer)3 AWSCredentials (com.amazonaws.auth.AWSCredentials)3 HttpMethodName (com.amazonaws.http.HttpMethodName)3 Test (org.junit.Test)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonWebServiceResponse (com.amazonaws.AmazonWebServiceResponse)2 AnonymousAWSCredentials (com.amazonaws.auth.AnonymousAWSCredentials)2 Presigner (com.amazonaws.auth.Presigner)2 Regions (com.amazonaws.regions.Regions)2 S3QueryStringSigner (com.amazonaws.services.s3.internal.S3QueryStringSigner)2 S3Signer (com.amazonaws.services.s3.internal.S3Signer)2 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 AWSRequestMetrics (com.amazonaws.util.AWSRequestMetrics)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2