Skip to content

Commit

Permalink
Skip Signing of requests which are defined with authtype as none (#3281)
Browse files Browse the repository at this point in the history
  • Loading branch information
joviegas committed Jul 8, 2022
1 parent d220360 commit 1d92f36
Show file tree
Hide file tree
Showing 18 changed files with 564 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-52c39b2.json
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Requests which are defined with AuthType as None should not be signed or authorized by the SDK."
}
Expand Up @@ -41,6 +41,7 @@
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.EventStreamSpecHelper;
import software.amazon.awssdk.core.SdkPojoBuilder;
Expand Down Expand Up @@ -177,7 +178,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(".withInput($L)\n", opModel.getInput().getVariableName())
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));

if (opModel.hasStreamingInput()) {
codeBlock.add(".withRequestBody(requestBody)")
Expand Down Expand Up @@ -245,6 +247,7 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(asyncRequestBody)
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(".withInput($L)$L);",
opModel.getInput().getVariableName(), asyncResponseTransformerVariable(isStreaming, isRestJson, opModel));

Expand Down
Expand Up @@ -30,6 +30,7 @@
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.http.HttpResponseHandler;
Expand Down Expand Up @@ -113,7 +114,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
.add(".withInput($L)", opModel.getInput().getVariableName())
.add(".withMetricCollector(apiCallMetricCollector)")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));


if (opModel.hasStreamingInput()) {
return codeBlock.add(".withRequestBody(requestBody)")
Expand Down Expand Up @@ -145,7 +148,9 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(".withErrorResponseHandler(errorResponseHandler)\n")
.add(".withMetricCollector(apiCallMetricCollector)\n")
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));


builder.add(hostPrefixExpression(opModel) + asyncRequestBody + ".withInput($L)$L);",
opModel.getInput().getVariableName(),
Expand Down
Expand Up @@ -36,6 +36,7 @@
import software.amazon.awssdk.codegen.poet.PoetExtension;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumRequiredTrait;
import software.amazon.awssdk.codegen.poet.client.traits.HttpChecksumTrait;
import software.amazon.awssdk.codegen.poet.client.traits.NoneAuthTypeRequestTrait;
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.EventStreamSpecHelper;
import software.amazon.awssdk.core.SdkPojoBuilder;
Expand Down Expand Up @@ -132,7 +133,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
discoveredEndpoint(opModel))
.add(".withInput($L)", opModel.getInput().getVariableName())
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));


s3ArnableFields(opModel, model).ifPresent(codeBlock::add);

Expand Down Expand Up @@ -207,7 +210,8 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(".withMetricCollector(apiCallMetricCollector)\n")
.add(asyncRequestBody(opModel))
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel));
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel));

s3ArnableFields(opModel, model).ifPresent(builder::add);

Expand Down
@@ -0,0 +1,51 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.codegen.poet.client.traits;

import com.squareup.javapoet.CodeBlock;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
import software.amazon.awssdk.codegen.model.service.AuthType;
import software.amazon.awssdk.core.client.handler.ClientExecutionParams;
import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute;

/**
* Trait which defines if a given request needs to be authenticated.
* A request is not authenticated only if it has "auththpe" trait explicitly marked as "none"
*/
@SdkInternalApi
public class NoneAuthTypeRequestTrait {

private NoneAuthTypeRequestTrait() {
}

/**
* Generate a ".putExecutionAttribute(...)" code-block for the provided operation model. This should be used within the
* context of initializing {@link ClientExecutionParams}. If and only if "authType" trait is explicitly set as "none" the set
* the execution attribute as false.
*/
public static CodeBlock create(OperationModel operationModel) {

if (operationModel.getAuthType() == AuthType.NONE) {
CodeBlock.Builder codeBuilder = CodeBlock.builder();
codeBuilder.add(CodeBlock.of(".putExecutionAttribute($T.IS_NONE_AUTH_TYPE_REQUEST, $L)",
SdkInternalExecutionAttribute.class, operationModel.getAuthType() != AuthType.NONE));
return codeBuilder.build();
} else {
return CodeBlock.of("");
}
}
}
Expand Up @@ -22,6 +22,14 @@
},
"httpChecksumRequired": true
},
"OperationWithNoneAuthType": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"authtype": "none"
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Expand Up @@ -21,6 +21,14 @@
},
"httpChecksumRequired": true
},
"OperationWithNoneAuthType": {
"name": "APostOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"authtype": "none"
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Expand Up @@ -21,6 +21,14 @@
},
"httpChecksumRequired": true
},
"OperationWithNoneAuthType": {
"name": "NoneAuthTypeOperation",
"http": {
"method": "POST",
"requestUri": "/"
},
"authtype": "none"
},
"APostOperation": {
"name": "APostOperation",
"http": {
Expand Down
Expand Up @@ -75,6 +75,8 @@
import software.amazon.awssdk.services.json.model.JsonRequest;
import software.amazon.awssdk.services.json.model.OperationWithChecksumRequiredRequest;
import software.amazon.awssdk.services.json.model.OperationWithChecksumRequiredResponse;
import software.amazon.awssdk.services.json.model.OperationWithNoneAuthTypeRequest;
import software.amazon.awssdk.services.json.model.OperationWithNoneAuthTypeResponse;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithResultKeyRequest;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithResultKeyResponse;
import software.amazon.awssdk.services.json.model.PaginatedOperationWithoutResultKeyRequest;
Expand All @@ -99,6 +101,7 @@
import software.amazon.awssdk.services.json.transform.InputEventMarshaller;
import software.amazon.awssdk.services.json.transform.InputEventTwoMarshaller;
import software.amazon.awssdk.services.json.transform.OperationWithChecksumRequiredRequestMarshaller;
import software.amazon.awssdk.services.json.transform.OperationWithNoneAuthTypeRequestMarshaller;
import software.amazon.awssdk.services.json.transform.PaginatedOperationWithResultKeyRequestMarshaller;
import software.amazon.awssdk.services.json.transform.PaginatedOperationWithoutResultKeyRequestMarshaller;
import software.amazon.awssdk.services.json.transform.StreamingInputOperationRequestMarshaller;
Expand Down Expand Up @@ -617,6 +620,63 @@ public CompletableFuture<OperationWithChecksumRequiredResponse> operationWithChe
}
}

/**
* Invokes the OperationWithNoneAuthType operation asynchronously.
*
* @param operationWithNoneAuthTypeRequest
* @return A Java Future containing the result of the OperationWithNoneAuthType operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>JsonException Base class for all service exceptions. Unknown exceptions will be thrown as an instance
* of this type.</li>
* </ul>
* @sample JsonAsyncClient.OperationWithNoneAuthType
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/json-service-2010-05-08/OperationWithNoneAuthType"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<OperationWithNoneAuthTypeResponse> operationWithNoneAuthType(
OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Json Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType");
JsonOperationMetadata operationMetadata = JsonOperationMetadata.builder().hasStreamingSuccessResponse(false)
.isPayloadJson(true).build();

HttpResponseHandler<OperationWithNoneAuthTypeResponse> responseHandler = protocolFactory.createResponseHandler(
operationMetadata, OperationWithNoneAuthTypeResponse::builder);

HttpResponseHandler<AwsServiceException> errorResponseHandler = createErrorResponseHandler(protocolFactory,
operationMetadata);

CompletableFuture<OperationWithNoneAuthTypeResponse> executeFuture = clientHandler
.execute(new ClientExecutionParams<OperationWithNoneAuthTypeRequest, OperationWithNoneAuthTypeResponse>()
.withOperationName("OperationWithNoneAuthType")
.withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory))
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withMetricCollector(apiCallMetricCollector)
.putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false)
.withInput(operationWithNoneAuthTypeRequest));
CompletableFuture<OperationWithNoneAuthTypeResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
executeFuture = CompletableFutureUtils.forwardExceptionTo(whenCompleted, executeFuture);
return executeFuture;
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}

/**
* Some paginated operation with result_key in paginators.json file
*
Expand Down
Expand Up @@ -44,6 +44,8 @@
import software.amazon.awssdk.services.query.model.InvalidInputException;
import software.amazon.awssdk.services.query.model.OperationWithChecksumRequiredRequest;
import software.amazon.awssdk.services.query.model.OperationWithChecksumRequiredResponse;
import software.amazon.awssdk.services.query.model.OperationWithNoneAuthTypeRequest;
import software.amazon.awssdk.services.query.model.OperationWithNoneAuthTypeResponse;
import software.amazon.awssdk.services.query.model.PutOperationWithChecksumRequest;
import software.amazon.awssdk.services.query.model.PutOperationWithChecksumResponse;
import software.amazon.awssdk.services.query.model.QueryException;
Expand All @@ -56,6 +58,7 @@
import software.amazon.awssdk.services.query.transform.APostOperationWithOutputRequestMarshaller;
import software.amazon.awssdk.services.query.transform.GetOperationWithChecksumRequestMarshaller;
import software.amazon.awssdk.services.query.transform.OperationWithChecksumRequiredRequestMarshaller;
import software.amazon.awssdk.services.query.transform.OperationWithNoneAuthTypeRequestMarshaller;
import software.amazon.awssdk.services.query.transform.PutOperationWithChecksumRequestMarshaller;
import software.amazon.awssdk.services.query.transform.StreamingInputOperationRequestMarshaller;
import software.amazon.awssdk.services.query.transform.StreamingOutputOperationRequestMarshaller;
Expand Down Expand Up @@ -322,6 +325,60 @@ public CompletableFuture<OperationWithChecksumRequiredResponse> operationWithChe
}
}

/**
* Invokes the OperationWithNoneAuthType operation asynchronously.
*
* @param operationWithNoneAuthTypeRequest
* @return A Java Future containing the result of the OperationWithNoneAuthType operation returned by the service.<br/>
* The CompletableFuture returned by this method can be completed exceptionally with the following
* exceptions.
* <ul>
* <li>SdkException Base class for all exceptions that can be thrown by the SDK (both service and client).
* Can be used for catch all scenarios.</li>
* <li>SdkClientException If any client side error occurs such as an IO related failure, failure to get
* credentials, etc.</li>
* <li>QueryException Base class for all service exceptions. Unknown exceptions will be thrown as an
* instance of this type.</li>
* </ul>
* @sample QueryAsyncClient.OperationWithNoneAuthType
* @see <a href="https://docs.aws.amazon.com/goto/WebAPI/query-service-2010-05-08/OperationWithNoneAuthType"
* target="_top">AWS API Documentation</a>
*/
@Override
public CompletableFuture<OperationWithNoneAuthTypeResponse> operationWithNoneAuthType(
OperationWithNoneAuthTypeRequest operationWithNoneAuthTypeRequest) {
List<MetricPublisher> metricPublishers = resolveMetricPublishers(clientConfiguration, operationWithNoneAuthTypeRequest
.overrideConfiguration().orElse(null));
MetricCollector apiCallMetricCollector = metricPublishers.isEmpty() ? NoOpMetricCollector.create() : MetricCollector
.create("ApiCall");
try {
apiCallMetricCollector.reportMetric(CoreMetric.SERVICE_ID, "Query Service");
apiCallMetricCollector.reportMetric(CoreMetric.OPERATION_NAME, "OperationWithNoneAuthType");

HttpResponseHandler<OperationWithNoneAuthTypeResponse> responseHandler = protocolFactory
.createResponseHandler(OperationWithNoneAuthTypeResponse::builder);

HttpResponseHandler<AwsServiceException> errorResponseHandler = protocolFactory.createErrorResponseHandler();

CompletableFuture<OperationWithNoneAuthTypeResponse> executeFuture = clientHandler
.execute(new ClientExecutionParams<OperationWithNoneAuthTypeRequest, OperationWithNoneAuthTypeResponse>()
.withOperationName("OperationWithNoneAuthType")
.withMarshaller(new OperationWithNoneAuthTypeRequestMarshaller(protocolFactory))
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withMetricCollector(apiCallMetricCollector)
.putExecutionAttribute(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false)
.withInput(operationWithNoneAuthTypeRequest));
CompletableFuture<OperationWithNoneAuthTypeResponse> whenCompleteFuture = null;
whenCompleteFuture = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
return CompletableFutureUtils.forwardExceptionTo(whenCompleteFuture, executeFuture);
} catch (Throwable t) {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
return CompletableFutureUtils.failedFuture(t);
}
}

/**
* Invokes the PutOperationWithChecksum operation asynchronously.
*
Expand Down

0 comments on commit 1d92f36

Please sign in to comment.