-
Notifications
You must be signed in to change notification settings - Fork 995
Move endpoint resolution from interceptors to pipeline stage #6820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/master/core-interceptors-migration
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,11 +25,13 @@ | |
| import com.squareup.javapoet.ParameterizedTypeName; | ||
| import com.squareup.javapoet.TypeName; | ||
| import com.squareup.javapoet.TypeVariableName; | ||
| import com.squareup.javapoet.WildcardTypeName; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.function.Consumer; | ||
| import java.util.stream.Collectors; | ||
| import javax.lang.model.element.Modifier; | ||
|
|
@@ -51,11 +53,16 @@ | |
| import software.amazon.awssdk.core.SdkClient; | ||
| import software.amazon.awssdk.core.SdkPlugin; | ||
| import software.amazon.awssdk.core.SdkRequest; | ||
| import software.amazon.awssdk.core.SelectedAuthScheme; | ||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkClientConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkClientOption; | ||
| import software.amazon.awssdk.core.exception.SdkClientException; | ||
| import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
| import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute; | ||
| import software.amazon.awssdk.core.retry.RetryMode; | ||
| import software.amazon.awssdk.core.signer.Signer; | ||
| import software.amazon.awssdk.endpoints.Endpoint; | ||
| import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; | ||
| import software.amazon.awssdk.retries.api.RetryStrategy; | ||
| import software.amazon.awssdk.utils.AttributeMap; | ||
|
|
@@ -406,9 +413,6 @@ private static void addSimpleAuthSchemeResolution(MethodSpec.Builder builder, | |
| ClassName.get(Set.class), awsClientOption); | ||
| builder.beginControlFlow("if (!$T.isNullOrEmpty(sigv4aRegionSet))", CollectionUtils.class); | ||
| builder.addStatement("paramsBuilder.regionSet($T.create(sigv4aRegionSet))", regionSet); | ||
| builder.nextControlFlow("else"); | ||
| builder.addStatement("paramsBuilder.regionSet($T.create(clientConfiguration.option($T.AWS_REGION).id()))", | ||
| regionSet, awsClientOption); | ||
| builder.endControlFlow(); | ||
| } | ||
|
|
||
|
|
@@ -422,7 +426,7 @@ private static void addEndpointBasedAuthSchemeResolution(MethodSpec.Builder buil | |
| ClassName paramsInterface = authSchemeSpecUtils.parametersInterfaceName(); | ||
| ClassName awsClientOption = ClassName.get("software.amazon.awssdk.awscore.client.config", "AwsClientOption"); | ||
| ClassName endpointParamsClass = endpointRulesSpecUtils.parametersClassName(); | ||
| ClassName resolverInterceptor = endpointRulesSpecUtils.resolverInterceptorName(); | ||
| ClassName endpointResolverUtils = endpointRulesSpecUtils.endpointResolverUtilsName(); | ||
| ClassName executionAttributesClass = ClassName.get("software.amazon.awssdk.core.interceptor", "ExecutionAttributes"); | ||
| ClassName awsExecutionAttribute = ClassName.get("software.amazon.awssdk.awscore", "AwsExecutionAttribute"); | ||
| ClassName sdkExecutionAttribute = ClassName.get("software.amazon.awssdk.core.interceptor", "SdkExecutionAttribute"); | ||
|
|
@@ -447,7 +451,7 @@ private static void addEndpointBasedAuthSchemeResolution(MethodSpec.Builder buil | |
| sdkInternalExecutionAttribute, SdkClientOption.class); | ||
|
|
||
| builder.addStatement("$T endpointParams = $T.ruleParams(request, executionAttributes)", | ||
| endpointParamsClass, resolverInterceptor); | ||
| endpointParamsClass, endpointResolverUtils); | ||
|
|
||
| builder.addStatement("$T.Builder paramsBuilder = $T.builder()", paramsInterface, paramsInterface); | ||
|
|
||
|
|
@@ -467,6 +471,15 @@ private static void addEndpointBasedAuthSchemeResolution(MethodSpec.Builder buil | |
| builder.addStatement("paramsBuilder.region(clientConfiguration.option($T.AWS_REGION))", awsClientOption); | ||
| } | ||
|
|
||
| if (authSchemeSpecUtils.hasSigV4aSupport()) { | ||
| ClassName regionSet = ClassName.get("software.amazon.awssdk.http.auth.aws.signer", "RegionSet"); | ||
| builder.addStatement("$T<String> sigv4aRegionSet = clientConfiguration.option($T.AWS_SIGV4A_SIGNING_REGION_SET)", | ||
| ClassName.get(Set.class), awsClientOption); | ||
| builder.beginControlFlow("if (!$T.isNullOrEmpty(sigv4aRegionSet))", CollectionUtils.class); | ||
| builder.addStatement("paramsBuilder.regionSet($T.create(sigv4aRegionSet))", regionSet); | ||
| builder.endControlFlow(); | ||
| } | ||
|
|
||
| ClassName paramsBuilderClass = authSchemeSpecUtils.parametersEndpointAwareDefaultImplName().nestedClass("Builder"); | ||
| ClassName endpointProviderInterface = endpointRulesSpecUtils.providerInterfaceName(); | ||
|
|
||
|
|
@@ -482,4 +495,86 @@ private static void addEndpointBasedAuthSchemeResolution(MethodSpec.Builder buil | |
| builder.addStatement("$T<$T> options = authSchemeProvider.resolveAuthScheme(paramsBuilder.build())", | ||
| List.class, AuthSchemeOption.class); | ||
| } | ||
|
|
||
| static MethodSpec resolveEndpointMethod(AuthSchemeSpecUtils authSchemeSpecUtils, | ||
| EndpointRulesSpecUtils endpointRulesSpecUtils) { | ||
| ClassName utilsClass = endpointRulesSpecUtils.endpointResolverUtilsName(); | ||
| ClassName endpointParamsClass = endpointRulesSpecUtils.parametersClassName(); | ||
| ClassName providerInterface = endpointRulesSpecUtils.providerInterfaceName(); | ||
| ClassName awsEndpointProviderUtils = endpointRulesSpecUtils.sharedAwsEndpointProviderUtilsName(); | ||
| ClassName awsEndpointAttribute = ClassName.get("software.amazon.awssdk.awscore.endpoints", "AwsEndpointAttribute"); | ||
| ClassName endpointAuthScheme = ClassName.get("software.amazon.awssdk.awscore.endpoints.authscheme", "EndpointAuthScheme"); | ||
|
Comment on lines
+505
to
+506
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? |
||
|
|
||
| MethodSpec.Builder b = MethodSpec.methodBuilder("resolveEndpoint") | ||
| .addModifiers(PRIVATE) | ||
| .returns(Endpoint.class) | ||
| .addParameter(SdkRequest.class, "request") | ||
| .addParameter(ExecutionAttributes.class, "executionAttributes") | ||
| .addParameter(String.class, "operationName"); | ||
|
|
||
| b.addStatement("$1T provider = ($1T) executionAttributes.getAttribute($2T.ENDPOINT_PROVIDER)", | ||
| providerInterface, SdkInternalExecutionAttribute.class); | ||
|
|
||
| b.beginControlFlow("try"); | ||
| b.addStatement("$T endpointParams = $T.ruleParams(request, executionAttributes)", | ||
| endpointParamsClass, utilsClass); | ||
| b.addStatement("$T endpoint = provider.resolveEndpoint(endpointParams).join()", Endpoint.class); | ||
|
|
||
| b.beginControlFlow("if (!$T.disableHostPrefixInjection(executionAttributes))", awsEndpointProviderUtils); | ||
| b.addStatement("$T hostPrefix = $T.hostPrefix(operationName, request)", | ||
| ParameterizedTypeName.get(Optional.class, String.class), utilsClass); | ||
| b.beginControlFlow("if (hostPrefix.isPresent())"); | ||
| b.addStatement("endpoint = $T.addHostPrefix(endpoint, hostPrefix.get())", awsEndpointProviderUtils); | ||
| b.endControlFlow(); | ||
| b.endControlFlow(); | ||
|
|
||
| b.addStatement("$T endpointAuthSchemes = endpoint.attribute($T.AUTH_SCHEMES)", | ||
| ParameterizedTypeName.get(ClassName.get(List.class), endpointAuthScheme), | ||
| awsEndpointAttribute); | ||
| b.addStatement("$T selectedAuthScheme = executionAttributes.getAttribute($T.SELECTED_AUTH_SCHEME)", | ||
| ParameterizedTypeName.get(ClassName.get(SelectedAuthScheme.class), | ||
| WildcardTypeName.subtypeOf(Object.class)), | ||
| SdkInternalExecutionAttribute.class); | ||
| b.beginControlFlow("if (endpointAuthSchemes != null && selectedAuthScheme != null)"); | ||
| b.addStatement("selectedAuthScheme = $T.authSchemeWithEndpointSignerProperties(endpointAuthSchemes, selectedAuthScheme)", | ||
| utilsClass); | ||
|
|
||
| if (authSchemeSpecUtils.usesSigV4a() || authSchemeSpecUtils.generateEndpointBasedParams()) { | ||
| ClassName awsV4aAuthScheme = ClassName.get("software.amazon.awssdk.http.auth.aws.scheme", "AwsV4aAuthScheme"); | ||
| ClassName awsV4aHttpSigner = ClassName.get("software.amazon.awssdk.http.auth.aws.signer", "AwsV4aHttpSigner"); | ||
| ClassName regionSet = ClassName.get("software.amazon.awssdk.http.auth.aws.signer", "RegionSet"); | ||
| ClassName authSchemeOption = ClassName.get("software.amazon.awssdk.http.auth.spi.scheme", "AuthSchemeOption"); | ||
|
|
||
| b.addComment("Precedence of SigV4a RegionSet is set according to multi-auth SigV4a specifications"); | ||
| b.beginControlFlow("if (selectedAuthScheme.authSchemeOption().schemeId().equals($T.SCHEME_ID) " | ||
| + "&& selectedAuthScheme.authSchemeOption().signerProperty($T.REGION_SET) == null)", | ||
| awsV4aAuthScheme, awsV4aHttpSigner); | ||
| b.addStatement("$T optionBuilder = selectedAuthScheme.authSchemeOption().toBuilder()", | ||
| authSchemeOption.nestedClass("Builder")); | ||
| b.addStatement("$1T rs = $1T.create(endpointParams.region().id())", regionSet); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add a null check here for the endpointParams.region() here?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nvm, seems that this won't be null |
||
| b.addStatement("optionBuilder.putSignerProperty($T.REGION_SET, rs)", awsV4aHttpSigner); | ||
| b.addStatement("selectedAuthScheme = new $T(selectedAuthScheme.identity(), selectedAuthScheme.signer(), " | ||
| + "optionBuilder.build())", SelectedAuthScheme.class); | ||
| b.endControlFlow(); | ||
| } | ||
|
|
||
| b.addStatement("executionAttributes.putAttribute($T.SELECTED_AUTH_SCHEME, selectedAuthScheme)", | ||
| SdkInternalExecutionAttribute.class); | ||
| b.endControlFlow(); | ||
|
|
||
| b.addStatement("$T.setMetricValues(endpoint, executionAttributes)", utilsClass); | ||
|
|
||
| b.addStatement("return endpoint"); | ||
|
|
||
| b.nextControlFlow("catch ($T e)", CompletionException.class); | ||
| b.addStatement("$T cause = e.getCause()", Throwable.class); | ||
| b.beginControlFlow("if (cause instanceof $T)", SdkClientException.class); | ||
| b.addStatement("throw ($T) cause", SdkClientException.class); | ||
| b.endControlFlow(); | ||
| b.addStatement("throw $T.create($S + cause.getMessage(), cause)", | ||
| SdkClientException.class, "Endpoint resolution failed: "); | ||
| b.endControlFlow(); | ||
|
|
||
| return b.build(); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here, we need codegen tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://code.amazon.com/packages/AWSJavaSDKClientUpdater/blobs/f4e4a25e53d132b3fb05ca9cb47aeac4277cdc7b/--/src/com/amazonaws/transform/EndpointsJsonTransformer.java#L51-L53