aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

SDK 2.10.45 SavingsPlans client fails sign request same way as AWS server does

Open vvvinn opened this issue 6 years ago • 6 comments

SDK 2.10.45 SavingsPlans client (software.amazon.awssdk.services.savingsplans.SavingsplansAsyncClient) builds HTTP request for signing (authorising) different way comparing AWS server side

Describe the bug

Client outputs Content-Type header (with two values) as two headers: content-type:application/x-amz-json-1.0 content-type:application/json And signs it like this. At the server side server receives Content-Type header as one with two values in it: content-type:application/x-amz-json-1.0,application/json And then signs it (with user creds). So hash is always different and request never successful.

Expected Behavior

Client should build request (headers) and sign it same way as server does. So either client should be fixed, or server. Considering SavingsPlans is a relatively new service, and SDK uses AbstractAws4Signer which is used by other services, - I suggest fix should be done at server side. See AbstractAws4Signer.getCanonicalizedHeaderString():300 . The reason for two values in Content-Type header - SDK client ALWAYS implicitly adds 'application/x-amz-json-1.0' value, But if I do not provide 'application/json' when sending request - I got 415 response: Unsupported Media Type (Service: Savingsplans, Status Code: 415, Request ID: And when I add 'application/json' - client will send two values for header: 'application/x-amz-json-1.0,application/json'.

Current Behavior

java.util.concurrent.ExecutionException: software.amazon.awssdk.services.savingsplans.model.SavingsplansException: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

The Canonical String for this request should have been 'POST /DescribeSavingsPlans

amz-sdk-invocation-id:e096d3aa-e1d3-3377-2983-55f10f99b908 amz-sdk-retry:0/0/500 content-length:2 content-type:application/x-amz-json-1.0,application/json host:savingsplans.amazonaws.com x-amz-date:20200320T155941Z

amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;x-amz-date 44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a'

The String-to-Sign should have been 'AWS4-HMAC-SHA256 20200320T155941Z 20200320/us-east-1/savingsplans/aws4_request 3e0a69f60d5ca75d8f1c73fc6008bc984c48c5c5c21992fc2bcba7886def60d4' (Service: Savingsplans, Status Code: 403, Request ID: 9b8ef3c8-97b5-4116-963f-d7f81df3979f) at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) at com.cirba.audit.aws.Main4.main(Main4.java:43) Caused by: software.amazon.awssdk.services.savingsplans.model.SavingsplansException: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

The Canonical String for this request should have been 'POST /DescribeSavingsPlans

amz-sdk-invocation-id:e096d3aa-e1d3-3377-2983-55f10f99b908 amz-sdk-retry:0/0/500 content-length:2 content-type:application/x-amz-json-1.0,application/json host:savingsplans.amazonaws.com x-amz-date:20200320T155941Z

amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;x-amz-date 44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a'

The String-to-Sign should have been 'AWS4-HMAC-SHA256 20200320T155941Z 20200320/us-east-1/savingsplans/aws4_request 3e0a69f60d5ca75d8f1c73fc6008bc984c48c5c5c21992fc2bcba7886def60d4' (Service: Savingsplans, Status Code: 403, Request ID: 9b8ef3c8-97b5-4116-963f-d7f81df3979f) at software.amazon.awssdk.services.savingsplans.model.SavingsplansException$BuilderImpl.build(SavingsplansException.java:95) at software.amazon.awssdk.services.savingsplans.model.SavingsplansException$BuilderImpl.build(SavingsplansException.java:55) at software.amazon.awssdk.protocols.json.internal.unmarshall.AwsJsonProtocolErrorUnmarshaller.unmarshall(AwsJsonProtocolErrorUnmarshaller.java:88) at software.amazon.awssdk.protocols.json.internal.unmarshall.AwsJsonProtocolErrorUnmarshaller.handle(AwsJsonProtocolErrorUnmarshaller.java:63) at software.amazon.awssdk.protocols.json.internal.unmarshall.AwsJsonProtocolErrorUnmarshaller.handle(AwsJsonProtocolErrorUnmarshaller.java:42) at software.amazon.awssdk.core.internal.http.async.AsyncResponseHandler.lambda$prepare$0(AsyncResponseHandler.java:88) at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952) at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) at java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962) at software.amazon.awssdk.core.internal.http.async.AsyncResponseHandler$BaosSubscriber.onComplete(AsyncResponseHandler.java:129) at software.amazon.awssdk.http.nio.netty.internal.ResponseHandler.runAndLogError(ResponseHandler.java:171) at software.amazon.awssdk.http.nio.netty.internal.ResponseHandler.access$500(ResponseHandler.java:68) at software.amazon.awssdk.http.nio.netty.internal.ResponseHandler$PublisherAdapter$1.onComplete(ResponseHandler.java:287) at com.typesafe.netty.HandlerPublisher.publishMessage(HandlerPublisher.java:362) at com.typesafe.netty.HandlerPublisher.flushBuffer(HandlerPublisher.java:304) at com.typesafe.netty.HandlerPublisher.receivedDemand(HandlerPublisher.java:258) at com.typesafe.netty.HandlerPublisher.access$200(HandlerPublisher.java:41) at com.typesafe.netty.HandlerPublisher$ChannelSubscription$1.run(HandlerPublisher.java:452) at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:510) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:518) at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1044) at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) at java.lang.Thread.run(Thread.java:748)

Steps to Reproduce

public static void main(String[] args) { String accessKey = "accessKey"; String secretKey = "secretKey";

	AwsCredentialsProvider awsCredentialsProvider= StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
	ClientOverrideConfiguration conf = ClientOverrideConfiguration.builder()
			.putHeader("Content-Type", MediaType.APPLICATION_JSON)
			.build();
	SavingsplansAsyncClientBuilder cBuilder = SavingsplansAsyncClient.builder()
			.credentialsProvider(awsCredentialsProvider).overrideConfiguration(conf);	
	AwsRequestOverrideConfiguration reqOvrCfg = AwsRequestOverrideConfiguration.builder().build();
	DescribeSavingsPlansRequest describeSavingsPlansRequest = DescribeSavingsPlansRequest.builder()
			.overrideConfiguration(reqOvrCfg)
			.build();
	Region region = Region.US_EAST_1;
	try {
		URI uri = URI.create("https://savingsplans.amazonaws.com");
		SavingsplansAsyncClient c = cBuilder.region(region)
				.endpointOverride(uri)
				.build();

		CompletableFuture<DescribeSavingsPlansResponse> f = c.describeSavingsPlans(describeSavingsPlansRequest );
		DescribeSavingsPlansResponse r = f.get();
		System.out.println();
	} catch (InterruptedException | ExecutionException e) {
		e.printStackTrace();
	}
}

If you put break-point in AbstractAws4Signer.getCanonicalizedHeaderString() : 310 you will see client creates two separate headers Content-Type

Possible Solution

Context

Cannot use SDK client to get Savings Plans

Your Environment

  • AWS Java SDK version used: 2.10.45
  • JDK version used: openjdk version "1.8.0_222"
  • Operating System and version: Windows 10 1903

vvvinn avatar Mar 20 '20 16:03 vvvinn

This looks related to a recent bug we fixed regarding incorrect merging of overridden headers like Content-Type; it was fixed in 2.10.82.

Can you try using 2.10.82 or later and see if that fixes your issue.

dagnir avatar Mar 20 '20 19:03 dagnir

Confirmed - SavingsPlans client works with 2.10.82

Thank you Vlad

On Fri, Mar 20, 2020 at 3:29 PM Dongie Agnir [email protected] wrote:

This looks related to a recent bug we fixed regarding incorrect merging of overridden headers like Content-Type; it was fixed in 2.10.82 https://github.com/aws/aws-sdk-java-v2/blob/2.10.82/CHANGELOG.md#bugfixes .

Can you try using 2.10.82 or later and see if that fixes your issue.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-sdk-java-v2/issues/1729#issuecomment-601875059, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXWS2BIDL4LUMGHG55JFCDRIO7ZVANCNFSM4LQPLTTQ .

vvvinn avatar Mar 23 '20 12:03 vvvinn

Thank you for the confirmation @vvvinn.

Closing, feel free to reopen if you have further questions.

debora-ito avatar Mar 23 '20 16:03 debora-ito

Thank you, Debora. It is all good now, I doubt I will need to reopen it. Cheers Vlad

On Mon., Mar. 23, 2020, 12:07 Debora Naomi Ito, [email protected] wrote:

Thank you for the confirmation @vvvinn https://github.com/vvvinn.

Closing, feel free to reopen if you have further questions.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aws/aws-sdk-java-v2/issues/1729#issuecomment-602697825, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXWS2GUQOY4YFLWHDP7RR3RI6CLJANCNFSM4LQPLTTQ .

vvvinn avatar Mar 23 '20 16:03 vvvinn

SavingsPlans client works with 2.10.82, but since 2.10.83 there is the same issue again. Could you please check that?

Thanks, David

dtdavidr avatar Aug 31 '20 14:08 dtdavidr

Hi David

Sorry, can't check it at the moment - we're busy with release, and we are currently on 2.10.82

I'll keep that in mind, when I have some time - i'll pull 2.10.83 and will give it a try

Cheers Vlad

On Mon, Aug 31, 2020 at 10:13 AM dtdavidr [email protected] wrote:

SavingsPlans client works with 2.10.82, but since 2.10.83 there is the same issue again. Could you please check that?

Thanks, David

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aws/aws-sdk-java-v2/issues/1729#issuecomment-683804553, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALXWS2AXRMISTS35CAA4GNLSDOVYHANCNFSM4LQPLTTQ .

vvvinn avatar Aug 31 '20 14:08 vvvinn