-
Notifications
You must be signed in to change notification settings - Fork 1k
Add initial AuthSchemeProvider codegen #4025
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
Changes from 4 commits
d843a6f
c197b12
cd22005
34da7d0
41b457d
54f74d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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.emitters.tasks; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import software.amazon.awssdk.codegen.emitters.GeneratorTask; | ||
| import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams; | ||
| import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask; | ||
| import software.amazon.awssdk.codegen.poet.authscheme.AuthSchemeParamsSpec; | ||
| import software.amazon.awssdk.codegen.poet.authscheme.AuthSchemeProviderSpec; | ||
| import software.amazon.awssdk.codegen.poet.authscheme.DefaultAuthSchemeParamsSpec; | ||
| import software.amazon.awssdk.codegen.poet.authscheme.DefaultAuthSchemeProviderSpec; | ||
|
|
||
| public final class AuthSchemeGeneratorTasks extends BaseGeneratorTasks { | ||
| private final GeneratorTaskParams generatorTaskParams; | ||
|
|
||
| public AuthSchemeGeneratorTasks(GeneratorTaskParams dependencies) { | ||
| super(dependencies); | ||
| this.generatorTaskParams = dependencies; | ||
| } | ||
|
|
||
| @Override | ||
| protected List<GeneratorTask> createTasks() throws Exception { | ||
| List<GeneratorTask> tasks = new ArrayList<>(); | ||
| tasks.add(generateParamsInterface()); | ||
| tasks.add(generateProviderInterface()); | ||
| tasks.add(generateDefaultParamsImpl()); | ||
| tasks.add(generateDefaultProviderImpl()); | ||
| return tasks; | ||
| } | ||
|
|
||
| private GeneratorTask generateParamsInterface() { | ||
| return new PoetGeneratorTask(authSchemeDir(), model.getFileHeader(), new AuthSchemeParamsSpec(model)); | ||
| } | ||
|
|
||
| private GeneratorTask generateDefaultParamsImpl() { | ||
| return new PoetGeneratorTask(authSchemeInternalDir(), model.getFileHeader(), new DefaultAuthSchemeParamsSpec(model)); | ||
| } | ||
|
|
||
| private GeneratorTask generateProviderInterface() { | ||
| return new PoetGeneratorTask(authSchemeDir(), model.getFileHeader(), new AuthSchemeProviderSpec(model)); | ||
| } | ||
|
|
||
| private GeneratorTask generateDefaultProviderImpl() { | ||
| return new PoetGeneratorTask(authSchemeInternalDir(), model.getFileHeader(), new DefaultAuthSchemeProviderSpec(model)); | ||
| } | ||
|
|
||
| private String authSchemeDir() { | ||
| return generatorTaskParams.getPathProvider().getAuthSchemeDirectory(); | ||
| } | ||
|
|
||
| private String authSchemeInternalDir() { | ||
| return generatorTaskParams.getPathProvider().getAuthSchemeInternalDirectory(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * 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.authscheme; | ||
|
|
||
| import com.squareup.javapoet.ClassName; | ||
| import com.squareup.javapoet.CodeBlock; | ||
| import com.squareup.javapoet.MethodSpec; | ||
| import com.squareup.javapoet.ParameterSpec; | ||
| import com.squareup.javapoet.ParameterizedTypeName; | ||
| import com.squareup.javapoet.TypeSpec; | ||
| import java.util.Optional; | ||
| import javax.lang.model.element.Modifier; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
| import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
| import software.amazon.awssdk.codegen.poet.PoetUtils; | ||
|
|
||
| // TODO: reviewer: Params v/s Parameters? | ||
|
gosar marked this conversation as resolved.
Outdated
|
||
| public class AuthSchemeParamsSpec implements ClassSpec { | ||
| private final IntermediateModel intermediateModel; | ||
| private final AuthSchemeSpecUtils authSchemeSpecUtils; | ||
|
|
||
| public AuthSchemeParamsSpec(IntermediateModel intermediateModel) { | ||
| this.intermediateModel = intermediateModel; | ||
| this.authSchemeSpecUtils = new AuthSchemeSpecUtils(intermediateModel); | ||
| } | ||
|
|
||
| @Override | ||
| public ClassName className() { | ||
| return authSchemeSpecUtils.parametersInterfaceName(); | ||
| } | ||
|
|
||
| @Override | ||
| public TypeSpec poetSpec() { | ||
| TypeSpec.Builder b = PoetUtils.createInterfaceBuilder(className()) | ||
| .addModifiers(Modifier.PUBLIC) | ||
| .addAnnotation(SdkPublicApi.class) | ||
| .addJavadoc(interfaceJavadoc()) | ||
| .addMethod(builderMethod()) | ||
| .addType(builderInterfaceSpec()); | ||
|
|
||
| addAccessorMethods(b); | ||
| return b.build(); | ||
| } | ||
|
|
||
| private CodeBlock interfaceJavadoc() { | ||
| CodeBlock.Builder b = CodeBlock.builder(); | ||
|
|
||
| b.add("The parameters object used to resolve the auth schemes for the $N service.", | ||
| intermediateModel.getMetadata().getServiceName()); | ||
|
|
||
| return b.build(); | ||
| } | ||
|
|
||
| private MethodSpec builderMethod() { | ||
| return MethodSpec.methodBuilder("builder") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.STATIC) | ||
| .returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName()) | ||
| .addStatement("return $T.builder()", authSchemeSpecUtils.parametersDefaultImplName()) | ||
| .addJavadoc("Get a new builder for creating a {@link $T}.", | ||
| authSchemeSpecUtils.parametersInterfaceName()) | ||
| .build(); | ||
| } | ||
|
|
||
| private TypeSpec builderInterfaceSpec() { | ||
| TypeSpec.Builder b = TypeSpec.interfaceBuilder(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName()) | ||
| .addModifiers(Modifier.PUBLIC, Modifier.STATIC) | ||
| .addJavadoc("A builder for a {@link $T}.", authSchemeSpecUtils.parametersInterfaceName()); | ||
|
|
||
| addBuilderSetterMethods(b); | ||
|
|
||
| b.addMethod(MethodSpec.methodBuilder("build") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
| .returns(className()) | ||
| .addJavadoc("Returns a {@link $T} object that is created from the properties that have been set " | ||
| + "on the builder.", authSchemeSpecUtils.parametersInterfaceName()) | ||
| .build()); | ||
|
|
||
| return b.build(); | ||
| } | ||
|
|
||
| private void addAccessorMethods(TypeSpec.Builder b) { | ||
| b.addMethod(MethodSpec.methodBuilder("operation") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
| .returns(String.class) | ||
| .addJavadoc("Returns the operation for which to resolve the auth scheme.") | ||
| .build()); | ||
|
|
||
| if (authSchemeSpecUtils.usesSigV4()) { | ||
| b.addMethod(MethodSpec.methodBuilder("region") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
| // TODO: reviewer: Should region be Regions (Regions.class isn't available here though) | ||
| .returns(ParameterizedTypeName.get(Optional.class, String.class)) | ||
|
gosar marked this conversation as resolved.
|
||
| .addJavadoc("Returns the region. The region is optional. The region parameter may be used " | ||
| + "with $S auth scheme. By default, the region will be empty.", "aws.auth#sigv4") | ||
| .build()); | ||
| } | ||
| } | ||
|
|
||
| private void addBuilderSetterMethods(TypeSpec.Builder b) { | ||
| b.addMethod(MethodSpec.methodBuilder("operation") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
| .addParameter(ParameterSpec.builder(String.class, "operation").build()) | ||
| .returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName()) | ||
| .addJavadoc("Set the operation for which to resolve the auth scheme.") | ||
| .build()); | ||
|
|
||
| if (authSchemeSpecUtils.usesSigV4()) { | ||
| b.addMethod(MethodSpec.methodBuilder("region") | ||
| .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) | ||
| .addParameter(ParameterSpec.builder(String.class, "region").build()) | ||
| .returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName()) | ||
| .addJavadoc("Set the region. The region parameter may be used with the $S auth scheme.", | ||
| "aws.auth#sigv4") // TODO: Could the known auth schemes be defined somewhere? | ||
|
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. If we're not exposing the auth scheme id (slightly unsure), do we need to reference it? Can we reference the AuthScheme class for Sigv4 instead?
Contributor
Author
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. I think they can be considered public. I used it in the javadoc for HttpAuthScheme. Good point about using the AuthScheme class. It doesn't exist yet. So will keep the TODO to update when that exists. |
||
| .build()); | ||
| } | ||
| } | ||
| } | ||
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.
my.service.package.name.auth.authschemeis also another option that sounds good to me. Does it make sense to haveauthschemea sub-package of theauthpackage?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.
I like
authas prefixThere 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.
Updated to use
.auth.scheme.