Skip to content

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static Metadata constructMetadata(ServiceModel serviceModel,
.withPaginatorsPackageName(namingStrategy.getPaginatorsPackageName(serviceName))
.withWaitersPackageName(namingStrategy.getWaitersPackageName(serviceName))
.withEndpointRulesPackageName(namingStrategy.getEndpointRulesPackageName(serviceName))
.withAuthSchemePackageName(namingStrategy.getAuthSchemePackageName(serviceName))
.withServiceAbbreviation(serviceMetadata.getServiceAbbreviation())
.withServiceFullName(serviceMetadata.getServiceFullName())
.withServiceName(serviceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ public String getEndpointRulesInternalResourcesDirectory() {
public String getEndpointRulesTestDirectory() {
return testDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullEndpointRulesPackageName());
}

public String getAuthSchemeDirectory() {
return sourceDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullAuthSchemePackageName());
}

public String getAuthSchemeInternalDirectory() {
return sourceDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullInternalAuthSchemePackageName());
}

}
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.auth.scheme.AuthSchemeParamsSpec;
import software.amazon.awssdk.codegen.poet.auth.scheme.AuthSchemeProviderSpec;
import software.amazon.awssdk.codegen.poet.auth.scheme.DefaultAuthSchemeParamsSpec;
import software.amazon.awssdk.codegen.poet.auth.scheme.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
Expand Up @@ -27,6 +27,7 @@ public AwsGeneratorTasks(GeneratorTaskParams params) {
new PaginatorsGeneratorTasks(params),
new EventStreamGeneratorTasks(params),
new WaitersGeneratorTasks(params),
new EndpointProviderTasks(params));
new EndpointProviderTasks(params),
new AuthSchemeGeneratorTasks(params));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public final class Constant {

public static final String PACKAGE_NAME_RULES_PATTERN = "%s.endpoints";

// TODO: reviewer: maybe just auth instead of authscheme? or auth.scheme?
Copy link
Contributor

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.authscheme is also another option that sounds good to me. Does it make sense to have authscheme a sub-package of the auth package?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like auth as prefix

Copy link
Contributor Author

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.

public static final String PACKAGE_NAME_AUTH_SCHEME_PATTERN = "%s.auth.scheme";

public static final String PACKAGE_NAME_SMOKE_TEST_PATTERN = "%s.smoketests";

public static final String PACKAGE_NAME_CUSTOM_AUTH_PATTERN = "%s.auth";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class Metadata {

private String endpointRulesPackageName;

private String authSchemePackageName;

private String serviceAbbreviation;

private String serviceFullName;
Expand Down Expand Up @@ -723,6 +725,27 @@ public String getFullInternalEndpointRulesPackageName() {
return joinPackageNames(getFullEndpointRulesPackageName(), "internal");
}

public void setAuthSchemePackageName(String authSchemePackageName) {
this.authSchemePackageName = authSchemePackageName;
}

public Metadata withAuthSchemePackageName(String authSchemePackageName) {
setAuthSchemePackageName(authSchemePackageName);
return this;
}

public String getAuthSchemePackageName() {
return authSchemePackageName;
}

public String getFullAuthSchemePackageName() {
return joinPackageNames(rootPackageName, getAuthSchemePackageName());
}

public String getFullInternalAuthSchemePackageName() {
return joinPackageNames(getFullAuthSchemePackageName(), "internal");
}

public String getFullInternalPackageName() {
return joinPackageNames(getFullClientPackageName(), "internal");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ public String getEndpointRulesPackageName(String serviceName) {
return getCustomizedPackageName(concatServiceNameIfShareModel(serviceName), Constant.PACKAGE_NAME_RULES_PATTERN);
}

@Override
public String getAuthSchemePackageName(String serviceName) {
return getCustomizedPackageName(concatServiceNameIfShareModel(serviceName), Constant.PACKAGE_NAME_AUTH_SCHEME_PATTERN);
}

@Override
public String getSmokeTestPackageName(String serviceName) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
import software.amazon.awssdk.codegen.model.service.Shape;
import software.amazon.awssdk.core.SdkField;

/**
* Strategy to name various Java constructs based on the naming in the model and potentially customizations.
Expand Down Expand Up @@ -63,6 +64,11 @@ public interface NamingStrategy {
*/
String getEndpointRulesPackageName(String serviceName);

/**
* Retrieve the auth scheme package name that should be used based on the service name.
*/
String getAuthSchemePackageName(String serviceName);

/**
* Retrieve the smote test package name that should be used based on the service name.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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.auth.scheme;

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;

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: Should region be Regions? (Regions.class isn't available here though)
.returns(ParameterizedTypeName.get(Optional.class, String.class))
.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: Reference the SigV4 AuthScheme when implemented
.build());
}
}
}
Loading