Search in sources :

Example 36 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class QueryParamBindingTest method testReservedCharInParamValue.

/**
 * Make sure the marshaller is able to handle @UriLabel parameter values
 * containing special characters.
 */
@Test
public void testReservedCharInParamValue() {
    final String VALUE_WITH_SEMICOLON = ";foo";
    final String VALUE_WITH_AMPERSAND = "&bar";
    final String VALUE_WITH_QUESTION_MARK = "?charlie";
    ListHealthChecksRequest listReq = ListHealthChecksRequest.builder().marker(VALUE_WITH_SEMICOLON).maxItems(VALUE_WITH_AMPERSAND).build();
    SdkHttpFullRequest httpReq_List = new ListHealthChecksRequestMarshaller(PROTOCOL_FACTORY).marshall(listReq);
    assertEquals("/2013-04-01/healthcheck", httpReq_List.encodedPath());
    Map<String, List<String>> queryParams = httpReq_List.rawQueryParameters();
    assertEquals(2, queryParams.size());
    assertEquals(VALUE_WITH_SEMICOLON, queryParams.get("marker").get(0));
    assertEquals(VALUE_WITH_AMPERSAND, queryParams.get("maxitems").get(0));
    GetHealthCheckLastFailureReasonRequest getFailureReq = GetHealthCheckLastFailureReasonRequest.builder().healthCheckId(VALUE_WITH_QUESTION_MARK).build();
    SdkHttpFullRequest httpReq_GetFailure = new GetHealthCheckLastFailureReasonRequestMarshaller(PROTOCOL_FACTORY).marshall(getFailureReq);
    System.out.println(httpReq_GetFailure);
    // parameter value should be URL encoded
    assertEquals("/2013-04-01/healthcheck/%3Fcharlie/lastfailurereason", httpReq_GetFailure.encodedPath());
    queryParams = httpReq_GetFailure.rawQueryParameters();
    assertEquals(0, queryParams.size());
}
Also used : ListHealthChecksRequestMarshaller(software.amazon.awssdk.services.route53.transform.ListHealthChecksRequestMarshaller) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) GetHealthCheckLastFailureReasonRequestMarshaller(software.amazon.awssdk.services.route53.transform.GetHealthCheckLastFailureReasonRequestMarshaller) List(java.util.List) GetHealthCheckLastFailureReasonRequest(software.amazon.awssdk.services.route53.model.GetHealthCheckLastFailureReasonRequest) ListHealthChecksRequest(software.amazon.awssdk.services.route53.model.ListHealthChecksRequest) Test(org.junit.jupiter.api.Test)

Example 37 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class AwsS3V4SignerIntegrationTest method test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient.

@Test
public void test_SignMethod_WithModeledParam_And_WithoutUsingSdkClient() throws Exception {
    AwsS3V4Signer signer = AwsS3V4Signer.create();
    SdkHttpFullRequest httpFullRequest = generateBasicGetRequest();
    // sign the request
    SdkHttpFullRequest signedRequest = signer.sign(httpFullRequest, constructSignerParams());
    SdkHttpClient httpClient = ApacheHttpClient.builder().build();
    HttpExecuteResponse response = httpClient.prepareRequest(HttpExecuteRequest.builder().request(signedRequest).build()).call();
    assertEquals("Non success http status code", 200, response.httpResponse().statusCode());
    String actualResult = IoUtils.toUtf8String(response.responseBody().get());
    assertEquals(CONTENT, actualResult);
}
Also used : HttpExecuteResponse(software.amazon.awssdk.http.HttpExecuteResponse) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) Test(org.junit.Test)

Example 38 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class DefaultS3Presigner method initializePresignedRequest.

/**
 * Initialize the provided presigned request.
 */
private void initializePresignedRequest(PresignedRequest.Builder presignedRequest, ExecutionContext execCtx, SdkHttpFullRequest signedHttpRequest) {
    SdkBytes signedPayload = signedHttpRequest.contentStreamProvider().map(p -> SdkBytes.fromInputStream(p.newStream())).orElse(null);
    List<String> signedHeadersQueryParam = signedHttpRequest.rawQueryParameters().get("X-Amz-SignedHeaders");
    Validate.validState(signedHeadersQueryParam != null, "Only SigV4 presigners are supported at this time, but the configured " + "presigner (%s) did not seem to generate a SigV4 signature.", execCtx.signer());
    Map<String, List<String>> signedHeaders = signedHeadersQueryParam.stream().flatMap(h -> Stream.of(h.split(";"))).collect(toMap(h -> h, h -> signedHttpRequest.firstMatchingHeader(h).map(Collections::singletonList).orElseGet(ArrayList::new)));
    boolean isBrowserExecutable = signedHttpRequest.method() == SdkHttpMethod.GET && signedPayload == null && (signedHeaders.isEmpty() || (signedHeaders.size() == 1 && signedHeaders.containsKey("host")));
    presignedRequest.expiration(execCtx.executionAttributes().getAttribute(PRESIGNER_EXPIRATION)).isBrowserExecutable(isBrowserExecutable).httpRequest(signedHttpRequest).signedHeaders(signedHeaders).signedPayload(signedPayload);
}
Also used : InterceptorContext(software.amazon.awssdk.core.interceptor.InterceptorContext) PutObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) SdkBytes(software.amazon.awssdk.core.SdkBytes) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) PresignedPutObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) SdkClientOption(software.amazon.awssdk.core.client.config.SdkClientOption) URI(java.net.URI) Validate(software.amazon.awssdk.utils.Validate) PresignedGetObjectRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest) PresignRequest(software.amazon.awssdk.awscore.presigner.PresignRequest) PresignedUploadPartRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedUploadPartRequest) PresignedRequest(software.amazon.awssdk.awscore.presigner.PresignedRequest) ExecutionInterceptor(software.amazon.awssdk.core.interceptor.ExecutionInterceptor) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) PresignedCompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCompleteMultipartUploadRequest) Instant(java.time.Instant) PutObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.PutObjectRequestMarshaller) UploadPartPresignRequest(software.amazon.awssdk.services.s3.presigner.model.UploadPartPresignRequest) List(java.util.List) Stream(java.util.stream.Stream) PresignedCreateMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedCreateMultipartUploadRequest) Optional(java.util.Optional) S3Configuration(software.amazon.awssdk.services.s3.S3Configuration) SdkRequest(software.amazon.awssdk.core.SdkRequest) AbortMultipartUploadRequest(software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest) PRESIGNER_EXPIRATION(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.PRESIGNER_EXPIRATION) CollectionUtils.mergeLists(software.amazon.awssdk.utils.CollectionUtils.mergeLists) AbortMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.AbortMultipartUploadRequestMarshaller) ExecutionInterceptorChain(software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain) ClientType(software.amazon.awssdk.core.ClientType) ExecutionContext(software.amazon.awssdk.core.http.ExecutionContext) SdkClientConfiguration(software.amazon.awssdk.core.client.config.SdkClientConfiguration) Function(java.util.function.Function) PresignedAbortMultipartUploadRequest(software.amazon.awssdk.services.s3.presigner.model.PresignedAbortMultipartUploadRequest) FunctionalUtils.invokeSafely(software.amazon.awssdk.utils.FunctionalUtils.invokeSafely) ArrayList(java.util.ArrayList) S3Presigner(software.amazon.awssdk.services.s3.presigner.S3Presigner) Presigner(software.amazon.awssdk.core.signer.Presigner) ClasspathInterceptorChainFactory(software.amazon.awssdk.core.interceptor.ClasspathInterceptorChainFactory) GetObjectRequestMarshaller(software.amazon.awssdk.services.s3.transform.GetObjectRequestMarshaller) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) SdkHttpMethod(software.amazon.awssdk.http.SdkHttpMethod) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) SdkDefaultClientBuilder(software.amazon.awssdk.core.client.builder.SdkDefaultClientBuilder) UploadPartRequestMarshaller(software.amazon.awssdk.services.s3.transform.UploadPartRequestMarshaller) CompleteMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CompleteMultipartUploadRequestMarshaller) AwsSignerExecutionAttribute(software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) SdkExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkExecutionAttribute) CreateMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CreateMultipartUploadPresignRequest) AwsExecutionContextBuilder(software.amazon.awssdk.awscore.internal.AwsExecutionContextBuilder) SdkInternalExecutionAttribute(software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) Logger(software.amazon.awssdk.utils.Logger) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) CompleteMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.CompleteMultipartUploadPresignRequest) GetObjectPresignRequest(software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest) AwsExecutionAttribute(software.amazon.awssdk.awscore.AwsExecutionAttribute) ContentStreamProvider(software.amazon.awssdk.http.ContentStreamProvider) AwsS3ProtocolFactory(software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory) AwsS3V4Signer(software.amazon.awssdk.auth.signer.AwsS3V4Signer) AbortMultipartUploadPresignRequest(software.amazon.awssdk.services.s3.presigner.model.AbortMultipartUploadPresignRequest) CreateMultipartUploadRequestMarshaller(software.amazon.awssdk.services.s3.transform.CreateMultipartUploadRequestMarshaller) AwsDefaultClientBuilder(software.amazon.awssdk.awscore.client.builder.AwsDefaultClientBuilder) DefaultServiceEndpointBuilder(software.amazon.awssdk.awscore.endpoint.DefaultServiceEndpointBuilder) RequestBody(software.amazon.awssdk.core.sync.RequestBody) SdkInternalApi(software.amazon.awssdk.annotations.SdkInternalApi) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) Collections(java.util.Collections) IoUtils(software.amazon.awssdk.utils.IoUtils) SdkBytes(software.amazon.awssdk.core.SdkBytes) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 39 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class ProfileFileConfigurationTest method profileIsHonoredForCredentialsAndRegion.

@Test
public void profileIsHonoredForCredentialsAndRegion() {
    EnvironmentVariableHelper.run(env -> {
        env.remove(SdkSystemSetting.AWS_REGION);
        env.remove(SdkSystemSetting.AWS_ACCESS_KEY_ID);
        env.remove(SdkSystemSetting.AWS_SECRET_ACCESS_KEY);
        String profileContent = "[profile foo]\n" + "region = us-banana-46\n" + "aws_access_key_id = profileIsHonoredForCredentials_akid\n" + "aws_secret_access_key = profileIsHonoredForCredentials_skid";
        String profileName = "foo";
        Signer signer = mock(NoOpSigner.class);
        ProtocolRestJsonClient client = ProtocolRestJsonClient.builder().overrideConfiguration(overrideConfig(profileContent, profileName, signer)).build();
        Mockito.when(signer.sign(any(), any())).thenCallRealMethod();
        try {
            client.allTypes();
        } catch (SdkClientException e) {
        // expected
        }
        ArgumentCaptor<SdkHttpFullRequest> httpRequest = ArgumentCaptor.forClass(SdkHttpFullRequest.class);
        ArgumentCaptor<ExecutionAttributes> attributes = ArgumentCaptor.forClass(ExecutionAttributes.class);
        Mockito.verify(signer).sign(httpRequest.capture(), attributes.capture());
        AwsCredentials credentials = attributes.getValue().getAttribute(AwsSignerExecutionAttribute.AWS_CREDENTIALS);
        assertThat(credentials.accessKeyId()).isEqualTo("profileIsHonoredForCredentials_akid");
        assertThat(credentials.secretAccessKey()).isEqualTo("profileIsHonoredForCredentials_skid");
        Region region = attributes.getValue().getAttribute(AwsExecutionAttribute.AWS_REGION);
        assertThat(region.id()).isEqualTo("us-banana-46");
        assertThat(httpRequest.getValue().getUri().getHost()).contains("us-banana-46");
    });
}
Also used : NoOpSigner(software.amazon.awssdk.core.signer.NoOpSigner) Signer(software.amazon.awssdk.core.signer.Signer) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) AwsCredentials(software.amazon.awssdk.auth.credentials.AwsCredentials) Region(software.amazon.awssdk.regions.Region) ProtocolRestJsonClient(software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient) Test(org.junit.jupiter.api.Test)

Example 40 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class V2DynamoDbAttributeValue method toUtf8ByteArray.

private static byte[] toUtf8ByteArray(Map<String, AttributeValue> item) {
    SdkHttpFullRequest marshalled = putItemRequestMarshaller().marshall(PutItemRequest.builder().item(item).build());
    InputStream content = marshalled.contentStreamProvider().get().newStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[8192];
    int read;
    try {
        while ((read = content.read(buff)) != -1) {
            baos.write(buff, 0, read);
        }
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
    return baos.toByteArray();
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) AbortableInputStream(software.amazon.awssdk.http.AbortableInputStream) InputStream(java.io.InputStream) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)178 Test (org.junit.Test)70 Test (org.junit.jupiter.api.Test)50 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)43 URI (java.net.URI)25 SigningTestCase (software.amazon.awssdk.authcrt.signer.SigningTestCase)23 ByteArrayInputStream (java.io.ByteArrayInputStream)18 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)16 List (java.util.List)13 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)13 RequestExecutionContext (software.amazon.awssdk.core.internal.http.RequestExecutionContext)12 ByteBuffer (java.nio.ByteBuffer)11 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)11 Map (java.util.Map)10 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)10 Instant (java.time.Instant)9 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)9 Aws4SignerParams (software.amazon.awssdk.auth.signer.params.Aws4SignerParams)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8