Skip to content

Commit 247ec75

Browse files
committed
Add initial AuthSchemeProvider codegen
This includes: * AuthSchemeParams interface and implementation. * AuthSchemeProvider interface * AuthSchemeProvider implementation is stubbed out for now. * Plumbing to generate authscheme related code
1 parent 64d5212 commit 247ec75

File tree

19 files changed

+863
-1
lines changed

19 files changed

+863
-1
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/AddMetadata.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static Metadata constructMetadata(ServiceModel serviceModel,
5959
.withPaginatorsPackageName(namingStrategy.getPaginatorsPackageName(serviceName))
6060
.withWaitersPackageName(namingStrategy.getWaitersPackageName(serviceName))
6161
.withEndpointRulesPackageName(namingStrategy.getEndpointRulesPackageName(serviceName))
62+
.withAuthSchemePackageName(namingStrategy.getAuthSchemePackageName(serviceName))
6263
.withServiceAbbreviation(serviceMetadata.getServiceAbbreviation())
6364
.withServiceFullName(serviceMetadata.getServiceFullName())
6465
.withServiceName(serviceName)

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/GeneratorPathProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,13 @@ public String getEndpointRulesInternalResourcesDirectory() {
9595
public String getEndpointRulesTestDirectory() {
9696
return testDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullEndpointRulesPackageName());
9797
}
98+
99+
public String getAuthSchemeDirectory() {
100+
return sourceDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullAuthSchemePackageName());
101+
}
102+
103+
public String getAuthSchemeInternalDirectory() {
104+
return sourceDirectory + "/" + Utils.packageToDirectory(model.getMetadata().getFullInternalAuthSchemePackageName());
105+
}
106+
98107
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.emitters.tasks;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import software.amazon.awssdk.codegen.emitters.GeneratorTask;
21+
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
22+
import software.amazon.awssdk.codegen.emitters.PoetGeneratorTask;
23+
import software.amazon.awssdk.codegen.poet.authscheme.AuthSchemeParamsSpec;
24+
import software.amazon.awssdk.codegen.poet.authscheme.AuthSchemeProviderSpec;
25+
import software.amazon.awssdk.codegen.poet.authscheme.DefaultAuthSchemeParamsSpec;
26+
import software.amazon.awssdk.codegen.poet.authscheme.DefaultAuthSchemeProviderSpec;
27+
28+
public final class AuthSchemeGeneratorTasks extends BaseGeneratorTasks {
29+
private final GeneratorTaskParams generatorTaskParams;
30+
31+
public AuthSchemeGeneratorTasks(GeneratorTaskParams dependencies) {
32+
super(dependencies);
33+
this.generatorTaskParams = dependencies;
34+
}
35+
36+
@Override
37+
protected List<GeneratorTask> createTasks() throws Exception {
38+
List<GeneratorTask> tasks = new ArrayList<>();
39+
tasks.add(generateParamsInterface());
40+
tasks.add(generateProviderInterface());
41+
tasks.add(generateDefaultParams());
42+
tasks.add(generateDefaultProvider());
43+
return tasks;
44+
}
45+
46+
private GeneratorTask generateParamsInterface() {
47+
return new PoetGeneratorTask(authSchemeDir(), model.getFileHeader(), new AuthSchemeParamsSpec(model));
48+
}
49+
50+
private GeneratorTask generateDefaultParams() {
51+
return new PoetGeneratorTask(authSchemeInternalDir(), model.getFileHeader(), new DefaultAuthSchemeParamsSpec(model));
52+
}
53+
54+
private GeneratorTask generateProviderInterface() {
55+
return new PoetGeneratorTask(authSchemeDir(), model.getFileHeader(), new AuthSchemeProviderSpec(model));
56+
}
57+
58+
private GeneratorTask generateDefaultProvider() {
59+
return new PoetGeneratorTask(authSchemeInternalDir(), model.getFileHeader(), new DefaultAuthSchemeProviderSpec(model));
60+
}
61+
62+
private String authSchemeDir() {
63+
return generatorTaskParams.getPathProvider().getAuthSchemeDirectory();
64+
}
65+
66+
private String authSchemeInternalDir() {
67+
return generatorTaskParams.getPathProvider().getAuthSchemeInternalDirectory();
68+
}
69+
}

codegen/src/main/java/software/amazon/awssdk/codegen/emitters/tasks/AwsGeneratorTasks.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public AwsGeneratorTasks(GeneratorTaskParams params) {
2727
new PaginatorsGeneratorTasks(params),
2828
new EventStreamGeneratorTasks(params),
2929
new WaitersGeneratorTasks(params),
30-
new EndpointProviderTasks(params));
30+
new EndpointProviderTasks(params),
31+
new AuthSchemeGeneratorTasks(params));
3132
}
3233
}

codegen/src/main/java/software/amazon/awssdk/codegen/internal/Constant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public final class Constant {
7070

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

73+
// TODO: reviewer: maybe just auth instead of authscheme? or auth.scheme?
74+
public static final String PACKAGE_NAME_AUTH_SCHEME_PATTERN = "%s.authscheme";
75+
7376
public static final String PACKAGE_NAME_SMOKE_TEST_PATTERN = "%s.smoketests";
7477

7578
public static final String PACKAGE_NAME_CUSTOM_AUTH_PATTERN = "%s.auth";

codegen/src/main/java/software/amazon/awssdk/codegen/model/intermediate/Metadata.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public class Metadata {
7272

7373
private String endpointRulesPackageName;
7474

75+
private String authSchemePackageName;
76+
7577
private String serviceAbbreviation;
7678

7779
private String serviceFullName;
@@ -723,6 +725,27 @@ public String getFullInternalEndpointRulesPackageName() {
723725
return joinPackageNames(getFullEndpointRulesPackageName(), "internal");
724726
}
725727

728+
public void setAuthSchemePackageName(String authSchemePackageName) {
729+
this.authSchemePackageName = authSchemePackageName;
730+
}
731+
732+
public Metadata withAuthSchemePackageName(String authSchemePackageName) {
733+
setAuthSchemePackageName(authSchemePackageName);
734+
return this;
735+
}
736+
737+
public String getAuthSchemePackageName() {
738+
return authSchemePackageName;
739+
}
740+
741+
public String getFullAuthSchemePackageName() {
742+
return joinPackageNames(rootPackageName, getAuthSchemePackageName());
743+
}
744+
745+
public String getFullInternalAuthSchemePackageName() {
746+
return joinPackageNames(getFullAuthSchemePackageName(), "internal");
747+
}
748+
726749
public String getFullInternalPackageName() {
727750
return joinPackageNames(getFullClientPackageName(), "internal");
728751
}

codegen/src/main/java/software/amazon/awssdk/codegen/naming/DefaultNamingStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ public String getEndpointRulesPackageName(String serviceName) {
179179
return getCustomizedPackageName(concatServiceNameIfShareModel(serviceName), Constant.PACKAGE_NAME_RULES_PATTERN);
180180
}
181181

182+
@Override
183+
public String getAuthSchemePackageName(String serviceName) {
184+
return getCustomizedPackageName(concatServiceNameIfShareModel(serviceName), Constant.PACKAGE_NAME_AUTH_SCHEME_PATTERN);
185+
}
186+
182187
@Override
183188
public String getSmokeTestPackageName(String serviceName) {
184189

codegen/src/main/java/software/amazon/awssdk/codegen/naming/NamingStrategy.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
1919
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
2020
import software.amazon.awssdk.codegen.model.service.Shape;
21+
import software.amazon.awssdk.core.SdkField;
2122

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

67+
/**
68+
* Retrieve the auth scheme package name that should be used based on the service name.
69+
*/
70+
String getAuthSchemePackageName(String serviceName);
71+
6672
/**
6773
* Retrieve the smote test package name that should be used based on the service name.
6874
*/
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.poet.authscheme;
17+
18+
import com.squareup.javapoet.ClassName;
19+
import com.squareup.javapoet.CodeBlock;
20+
import com.squareup.javapoet.MethodSpec;
21+
import com.squareup.javapoet.ParameterSpec;
22+
import com.squareup.javapoet.ParameterizedTypeName;
23+
import com.squareup.javapoet.TypeSpec;
24+
import java.util.Optional;
25+
import javax.lang.model.element.Modifier;
26+
import software.amazon.awssdk.annotations.SdkPublicApi;
27+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
28+
import software.amazon.awssdk.codegen.poet.ClassSpec;
29+
import software.amazon.awssdk.codegen.poet.PoetUtils;
30+
31+
// TODO: reviewer: Params v/s Parameters?
32+
// : Suffix with Spec?
33+
public class AuthSchemeParamsSpec implements ClassSpec {
34+
private final IntermediateModel intermediateModel;
35+
private final AuthSchemeSpecUtils authSchemeSpecUtils;
36+
37+
public AuthSchemeParamsSpec(IntermediateModel intermediateModel) {
38+
this.intermediateModel = intermediateModel;
39+
this.authSchemeSpecUtils = new AuthSchemeSpecUtils(intermediateModel);
40+
}
41+
42+
@Override
43+
public ClassName className() {
44+
return authSchemeSpecUtils.parametersInterfaceName();
45+
}
46+
47+
@Override
48+
public TypeSpec poetSpec() {
49+
TypeSpec.Builder b = PoetUtils.createInterfaceBuilder(className())
50+
.addModifiers(Modifier.PUBLIC)
51+
.addAnnotation(SdkPublicApi.class)
52+
.addJavadoc(interfaceJavadoc())
53+
.addMethod(builderMethod())
54+
.addType(builderInterfaceSpec());
55+
56+
addAccessorMethods(b);
57+
return b.build();
58+
}
59+
60+
private void addAccessorMethods(TypeSpec.Builder b) {
61+
b.addMethod(MethodSpec.methodBuilder("operation")
62+
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
63+
.returns(String.class)
64+
.build());
65+
66+
if (authSchemeSpecUtils.usesSigV4()) {
67+
b.addMethod(MethodSpec.methodBuilder("region")
68+
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
69+
// TODO: reviewer: Should region be Regions (Regions.class isn't available here though)
70+
.returns(ParameterizedTypeName.get(Optional.class, String.class))
71+
.build());
72+
}
73+
}
74+
75+
private CodeBlock interfaceJavadoc() {
76+
CodeBlock.Builder b = CodeBlock.builder();
77+
78+
b.add("The parameters object used to resolve the auth schemes for the $N service.",
79+
intermediateModel.getMetadata().getServiceName());
80+
81+
return b.build();
82+
}
83+
84+
private MethodSpec builderMethod() {
85+
return MethodSpec.methodBuilder("builder")
86+
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
87+
.returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName())
88+
.addStatement("return $T.builder()", authSchemeSpecUtils.parametersDefaultImplName())
89+
.build();
90+
}
91+
92+
private TypeSpec builderInterfaceSpec() {
93+
TypeSpec.Builder b = TypeSpec.interfaceBuilder(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName())
94+
.addModifiers(Modifier.PUBLIC, Modifier.STATIC);
95+
96+
addBuilderSetterMethods(b);
97+
98+
b.addMethod(MethodSpec.methodBuilder("build")
99+
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
100+
.returns(className())
101+
.build());
102+
103+
return b.build();
104+
}
105+
106+
private void addBuilderSetterMethods(TypeSpec.Builder b) {
107+
b.addMethod(MethodSpec.methodBuilder("operation")
108+
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
109+
.addParameter(ParameterSpec.builder(String.class, "operation").build())
110+
.returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName())
111+
.build());
112+
113+
if (authSchemeSpecUtils.usesSigV4()) {
114+
b.addMethod(MethodSpec.methodBuilder("region")
115+
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
116+
.addParameter(ParameterSpec.builder(String.class, "region").build())
117+
.returns(authSchemeSpecUtils.parametersInterfaceBuilderInterfaceName())
118+
.build());
119+
}
120+
}
121+
122+
}

0 commit comments

Comments
 (0)