|
29 | 29 | import java.util.Map; |
30 | 30 | import java.util.Objects; |
31 | 31 | import java.util.Optional; |
| 32 | +import java.util.Set; |
32 | 33 | import java.util.function.Consumer; |
33 | 34 | import java.util.stream.Collectors; |
34 | 35 | import javax.lang.model.element.Modifier; |
|
45 | 46 | import software.amazon.awssdk.codegen.model.service.HostPrefixProcessor; |
46 | 47 | import software.amazon.awssdk.codegen.poet.PoetExtension; |
47 | 48 | import software.amazon.awssdk.codegen.poet.PoetUtils; |
| 49 | +import software.amazon.awssdk.codegen.poet.auth.scheme.AuthSchemeSpecUtils; |
48 | 50 | import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils; |
| 51 | +import software.amazon.awssdk.core.SdkClient; |
49 | 52 | import software.amazon.awssdk.core.SdkPlugin; |
50 | 53 | import software.amazon.awssdk.core.SdkRequest; |
51 | 54 | import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; |
52 | 55 | import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
53 | 56 | import software.amazon.awssdk.core.client.config.SdkClientOption; |
54 | 57 | import software.amazon.awssdk.core.retry.RetryMode; |
55 | 58 | import software.amazon.awssdk.core.signer.Signer; |
| 59 | +import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; |
56 | 60 | import software.amazon.awssdk.retries.api.RetryStrategy; |
57 | 61 | import software.amazon.awssdk.utils.AttributeMap; |
58 | 62 | import software.amazon.awssdk.utils.CollectionUtils; |
@@ -346,4 +350,136 @@ public static MethodSpec updateRetryStrategyClientConfigurationMethod() { |
346 | 350 | static String transformServiceId(String serviceId) { |
347 | 351 | return serviceId.replace(" ", "_"); |
348 | 352 | } |
| 353 | + |
| 354 | + static MethodSpec resolveAuthSchemeOptionsMethod(AuthSchemeSpecUtils authSchemeSpecUtils, |
| 355 | + EndpointRulesSpecUtils endpointRulesSpecUtils) { |
| 356 | + MethodSpec.Builder builder = MethodSpec.methodBuilder("resolveAuthSchemeOptions") |
| 357 | + .addModifiers(PRIVATE) |
| 358 | + .returns(ParameterizedTypeName.get(ClassName.get(List.class), ClassName.get(AuthSchemeOption.class))) |
| 359 | + .addParameter(SdkRequest.class, "request") |
| 360 | + .addParameter(String.class, "operationName") |
| 361 | + .addParameter(SdkClientConfiguration.class, "clientConfiguration"); |
| 362 | + |
| 363 | + ClassName providerInterface = authSchemeSpecUtils.providerInterfaceName(); |
| 364 | + |
| 365 | + builder.addStatement("$T authSchemeProvider = $T.isInstanceOf($T.class, " |
| 366 | + + "clientConfiguration.option($T.AUTH_SCHEME_PROVIDER), $S)", |
| 367 | + providerInterface, Validate.class, providerInterface, SdkClientOption.class, |
| 368 | + "Expected an instance of " + authSchemeSpecUtils.providerInterfaceName().simpleName()); |
| 369 | + |
| 370 | + if (authSchemeSpecUtils.useEndpointBasedAuthProvider()) { |
| 371 | + addEndpointBasedAuthSchemeResolution(builder, authSchemeSpecUtils, endpointRulesSpecUtils); |
| 372 | + } else { |
| 373 | + addSimpleAuthSchemeResolution(builder, authSchemeSpecUtils); |
| 374 | + } |
| 375 | + |
| 376 | + if (endpointRulesSpecUtils.isS3()) { |
| 377 | + ClassName sdkIdentityProperty = ClassName.get("software.amazon.awssdk.core.identity", "SdkIdentityProperty"); |
| 378 | + builder.addStatement("$T sdkClient = clientConfiguration.option($T.SDK_CLIENT)", |
| 379 | + SdkClient.class, SdkClientOption.class); |
| 380 | + builder.addStatement("return options.stream().map(o -> o.toBuilder()" |
| 381 | + + ".putIdentityProperty($T.SDK_CLIENT, sdkClient).build())" |
| 382 | + + ".collect($T.toList())", |
| 383 | + sdkIdentityProperty, Collectors.class); |
| 384 | + } else { |
| 385 | + builder.addStatement("return options"); |
| 386 | + } |
| 387 | + |
| 388 | + return builder.build(); |
| 389 | + } |
| 390 | + |
| 391 | + private static void addSimpleAuthSchemeResolution(MethodSpec.Builder builder, |
| 392 | + AuthSchemeSpecUtils authSchemeSpecUtils) { |
| 393 | + ClassName paramsInterface = authSchemeSpecUtils.parametersInterfaceName(); |
| 394 | + ClassName awsClientOption = ClassName.get("software.amazon.awssdk.awscore.client.config", "AwsClientOption"); |
| 395 | + |
| 396 | + builder.addStatement("$T.Builder paramsBuilder = $T.builder().operation(operationName)", |
| 397 | + paramsInterface, paramsInterface); |
| 398 | + |
| 399 | + if (authSchemeSpecUtils.usesSigV4()) { |
| 400 | + builder.addStatement("paramsBuilder.region(clientConfiguration.option($T.AWS_REGION))", awsClientOption); |
| 401 | + } |
| 402 | + |
| 403 | + if (authSchemeSpecUtils.hasSigV4aSupport()) { |
| 404 | + ClassName regionSet = ClassName.get("software.amazon.awssdk.http.auth.aws.signer", "RegionSet"); |
| 405 | + builder.addStatement("$T<String> sigv4aRegionSet = clientConfiguration.option($T.AWS_SIGV4A_SIGNING_REGION_SET)", |
| 406 | + ClassName.get(Set.class), awsClientOption); |
| 407 | + builder.beginControlFlow("if (!$T.isNullOrEmpty(sigv4aRegionSet))", CollectionUtils.class); |
| 408 | + builder.addStatement("paramsBuilder.regionSet($T.create(sigv4aRegionSet))", regionSet); |
| 409 | + builder.nextControlFlow("else"); |
| 410 | + builder.addStatement("paramsBuilder.regionSet($T.create(clientConfiguration.option($T.AWS_REGION).id()))", |
| 411 | + regionSet, awsClientOption); |
| 412 | + builder.endControlFlow(); |
| 413 | + } |
| 414 | + |
| 415 | + builder.addStatement("$T<$T> options = authSchemeProvider.resolveAuthScheme(paramsBuilder.build())", |
| 416 | + List.class, AuthSchemeOption.class); |
| 417 | + } |
| 418 | + |
| 419 | + private static void addEndpointBasedAuthSchemeResolution(MethodSpec.Builder builder, |
| 420 | + AuthSchemeSpecUtils authSchemeSpecUtils, |
| 421 | + EndpointRulesSpecUtils endpointRulesSpecUtils) { |
| 422 | + ClassName paramsInterface = authSchemeSpecUtils.parametersInterfaceName(); |
| 423 | + ClassName awsClientOption = ClassName.get("software.amazon.awssdk.awscore.client.config", "AwsClientOption"); |
| 424 | + ClassName endpointParamsClass = endpointRulesSpecUtils.parametersClassName(); |
| 425 | + ClassName resolverInterceptor = endpointRulesSpecUtils.resolverInterceptorName(); |
| 426 | + ClassName executionAttributesClass = ClassName.get("software.amazon.awssdk.core.interceptor", "ExecutionAttributes"); |
| 427 | + ClassName awsExecutionAttribute = ClassName.get("software.amazon.awssdk.awscore", "AwsExecutionAttribute"); |
| 428 | + ClassName sdkExecutionAttribute = ClassName.get("software.amazon.awssdk.core.interceptor", "SdkExecutionAttribute"); |
| 429 | + ClassName sdkInternalExecutionAttribute = ClassName.get("software.amazon.awssdk.core.interceptor", |
| 430 | + "SdkInternalExecutionAttribute"); |
| 431 | + |
| 432 | + builder.addStatement("$T executionAttributes = new $T()", executionAttributesClass, executionAttributesClass); |
| 433 | + builder.addStatement("executionAttributes.putAttribute($T.AWS_REGION, clientConfiguration.option($T.AWS_REGION))", |
| 434 | + awsExecutionAttribute, awsClientOption); |
| 435 | + builder.addStatement("executionAttributes.putAttribute($T.DUALSTACK_ENDPOINT_ENABLED, " |
| 436 | + + "clientConfiguration.option($T.DUALSTACK_ENDPOINT_ENABLED))", |
| 437 | + awsExecutionAttribute, awsClientOption); |
| 438 | + builder.addStatement("executionAttributes.putAttribute($T.FIPS_ENDPOINT_ENABLED, " |
| 439 | + + "clientConfiguration.option($T.FIPS_ENDPOINT_ENABLED))", |
| 440 | + awsExecutionAttribute, awsClientOption); |
| 441 | + builder.addStatement("executionAttributes.putAttribute($T.OPERATION_NAME, operationName)", sdkExecutionAttribute); |
| 442 | + builder.addStatement("executionAttributes.putAttribute($T.CLIENT_ENDPOINT_PROVIDER, " |
| 443 | + + "clientConfiguration.option($T.CLIENT_ENDPOINT_PROVIDER))", |
| 444 | + sdkInternalExecutionAttribute, SdkClientOption.class); |
| 445 | + builder.addStatement("executionAttributes.putAttribute($T.CLIENT_CONTEXT_PARAMS, " |
| 446 | + + "clientConfiguration.option($T.CLIENT_CONTEXT_PARAMS))", |
| 447 | + sdkInternalExecutionAttribute, SdkClientOption.class); |
| 448 | + |
| 449 | + builder.addStatement("$T endpointParams = $T.ruleParams(request, executionAttributes)", |
| 450 | + endpointParamsClass, resolverInterceptor); |
| 451 | + |
| 452 | + builder.addStatement("$T.Builder paramsBuilder = $T.builder()", paramsInterface, paramsInterface); |
| 453 | + |
| 454 | + boolean regionIncluded = false; |
| 455 | + for (String paramName : endpointRulesSpecUtils.parameters().keySet()) { |
| 456 | + if (!authSchemeSpecUtils.includeParamForProvider(paramName)) { |
| 457 | + continue; |
| 458 | + } |
| 459 | + regionIncluded = regionIncluded || paramName.equalsIgnoreCase("region"); |
| 460 | + String methodName = endpointRulesSpecUtils.paramMethodName(paramName); |
| 461 | + builder.addStatement("paramsBuilder.$1N(endpointParams.$1N())", methodName); |
| 462 | + } |
| 463 | + |
| 464 | + builder.addStatement("paramsBuilder.operation(operationName)"); |
| 465 | + |
| 466 | + if (authSchemeSpecUtils.usesSigV4() && !regionIncluded) { |
| 467 | + builder.addStatement("paramsBuilder.region(clientConfiguration.option($T.AWS_REGION))", awsClientOption); |
| 468 | + } |
| 469 | + |
| 470 | + ClassName paramsBuilderClass = authSchemeSpecUtils.parametersEndpointAwareDefaultImplName().nestedClass("Builder"); |
| 471 | + ClassName endpointProviderInterface = endpointRulesSpecUtils.providerInterfaceName(); |
| 472 | + |
| 473 | + builder.beginControlFlow("if (paramsBuilder instanceof $T)", paramsBuilderClass); |
| 474 | + builder.addStatement("$T endpointProvider = clientConfiguration.option($T.ENDPOINT_PROVIDER)", |
| 475 | + ClassName.get("software.amazon.awssdk.endpoints", "EndpointProvider"), SdkClientOption.class); |
| 476 | + builder.beginControlFlow("if (endpointProvider instanceof $T)", endpointProviderInterface); |
| 477 | + builder.addStatement("(($T) paramsBuilder).endpointProvider(($T) endpointProvider)", |
| 478 | + paramsBuilderClass, endpointProviderInterface); |
| 479 | + builder.endControlFlow(); |
| 480 | + builder.endControlFlow(); |
| 481 | + |
| 482 | + builder.addStatement("$T<$T> options = authSchemeProvider.resolveAuthScheme(paramsBuilder.build())", |
| 483 | + List.class, AuthSchemeOption.class); |
| 484 | + } |
349 | 485 | } |
0 commit comments