Skip to content
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

generate auth params and bindings #453

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions codegen/smithy-go-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extra["moduleName"] = "software.amazon.smithy.go.codegen"

dependencies {
api("software.amazon.smithy:smithy-codegen-core:$smithyVersion")
api("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid bringing in AWS dependencies.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

implementation("software.amazon.smithy:smithy-waiters:$smithyVersion")
api("com.atlassian.commonmark:commonmark:0.15.2")
api("org.jsoup:jsoup:1.14.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ void execute() {
protocolGenerator.generateEndpointResolution(context);
});

writers.useFileWriter("auth.go", settings.getModuleName(), writer -> {
ProtocolGenerator.GenerationContext context = contextBuilder.writer(writer).build();
protocolGenerator.generateAuth(context);
Copy link
Contributor

Choose a reason for hiding this comment

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

should auth scheme generation be part of protocol generation? i remember the reasoning we used to put endpoint generation in there. we made the argument that an endpoint does have the transport protocol baked into it, so it is tied to a protocol. and the upside was that putting endpoint generation in protocol generation guarantees only one will be chosen.
i think this same sort of reasoning could be applied to auth scheme.
but im second-guessing whether we should have put endpoint generation in there at all. i think if we put auth scheme in protocol generation as well, we should prob get a better understanding of what belongs in here and what doesnt

});

writers.useFileWriter("endpoints_test.go", settings.getModuleName(), writer -> {
ProtocolGenerator.GenerationContext context = contextBuilder.writer(writer).build();
protocolGenerator.generateEndpointResolutionTests(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class GoUniverseTypes {
public static Symbol tInt32 = universe("int32");
public static Symbol tInt64 = universe("int64");
public static Symbol tRune = universe("rune");
public static Symbol tString = universe("string");
public static final Symbol STRING = universe("string");
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason for this change, but not to the other Symbols?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I put these GoUniverseTypes into a PR first but using them ended up triggering spotbugs/checkstyle errors. I only changed the ones I needed but I'll update them all.


public static Symbol tUint = universe("uint");
public static Symbol tUint8 = universe("uint8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ public void run() {
.renderStructure(() -> {
}, true);

writer.write("""
func (*$T) operationName() string {
return $S
}
""",
Copy link
Contributor

Choose a reason for hiding this comment

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

Fix indentation here

inputSymbol,
operationSymbol.getName()
);

// The output structure gets a metadata member added.
Symbol metadataSymbol = SymbolUtils.createValueSymbolBuilder("Metadata", SmithyGoDependency.SMITHY_MIDDLEWARE)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 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.smithy.go.codegen.auth;

import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;

/**
* Entry point into smithy client auth generation.
*/
public class AuthGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would annotate this with @SmithyInternalApi

private final ProtocolGenerator.GenerationContext context;

public AuthGenerator(ProtocolGenerator.GenerationContext context) {
this.context = context;
}

public void generate() {
if (context.getWriter().isEmpty()) {
throw new CodegenException("writer is required");
}

context.getWriter().get()
.write("$W", new AuthParametersGenerator(context).generate())
.write("")
.write("$W", new AuthParametersResolverGenerator(context).generate());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 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.smithy.go.codegen.auth;

import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.go.codegen.GoUniverseTypes;

public record AuthParameter(String name, String docs, Symbol type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would annotate this with @SmithyInternalApi

public static final AuthParameter OPERATION = new AuthParameter(
"Operation",
"The name of the operation being invoked.",
GoUniverseTypes.STRING
);

public static final AuthParameter REGION = new AuthParameter(
"Region",
"The region in which the operation is being invoked.",
GoUniverseTypes.STRING
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2023 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.smithy.go.codegen.auth;

import static software.amazon.smithy.go.codegen.GoWriter.goDocTemplate;
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;

import java.util.ArrayList;
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.MapUtils;

/**
* Generates auth scheme resolver parameters.
* By default, the only field that exists universally is the name of the operation being invoked. Services that use
* SigV4[A] will also have a field for the region.
* Additional parameters can be loaded via GoIntegration.
*/
public class AuthParametersGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would annotate this with @SmithyInternalApi

public static final String STRUCT_NAME = "AuthResolverParameters";

public static final Symbol STRUCT_SYMBOL = SymbolUtils.createPointableSymbolBuilder(STRUCT_NAME).build();

private final ProtocolGenerator.GenerationContext context;

private final ArrayList<AuthParameter> fields = new ArrayList<>(
ListUtils.of(AuthParameter.OPERATION)
);

public AuthParametersGenerator(ProtocolGenerator.GenerationContext context) {
this.context = context;
}

public GoWriter.Writable generate() {
loadFields();

return goTemplate(
"""
$doc:W
type $name:L struct {
$fields:W
}
""",
MapUtils.of(
"doc", generateDocs(),
"name", STRUCT_NAME,
"fields", generateFields()
)
);
}

private GoWriter.Writable generateDocs() {
return goDocTemplate(
"$name:L contains the set of inputs necessary for auth scheme resolution.",
MapUtils.of("name", STRUCT_NAME)
);
}

private GoWriter.Writable generateFields() {
return (writer) -> {
for (var field: fields) {
writer.write("""
$W
$L $P
""",
goDocTemplate(field.docs()),
field.name(),
field.type()
);
}
};
}

private void loadFields() {
if (context.getService().hasTrait(SigV4Trait.class)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of bringing in a new dependency for smithy-aws-traits, you can check trait application by ShapeId.

Suggested change
if (context.getService().hasTrait(SigV4Trait.class)) {
if (context.getService().hasTrait(ShapeId.from("aws.auth#sigv4"))) {

Overall though, it would be nice to add REGION for Sigv4x through a RuntimeClientPlugin rather than special cased here.

fields.add(AuthParameter.REGION);
}

for (var integration: context.getIntegrations()) {
var plugins = integration.getClientPlugins().stream().filter(it ->
it.matchesService(context.getModel(), context.getService())).toList();
for (var plugin: plugins) {
fields.addAll(plugin.getAuthParameters());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023 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.smithy.go.codegen.auth;

import software.amazon.smithy.codegen.core.Symbol;

public record AuthParametersResolver(Symbol resolver) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would annotate this with @SmithyInternalApi

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2023 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.smithy.go.codegen.auth;

import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;

import java.util.ArrayList;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SymbolUtils;
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.utils.MapUtils;

/**
* Generates a single method which binds auth scheme resolver parameters from operation input and client options.
* The only value bound by default is the operation name. Generators must load additional bindings through
* GoIntegration.
*/
public class AuthParametersResolverGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would annotate this with @SmithyInternalApi

public static final String FUNC_NAME = "bindAuthResolverParams";

public static final Symbol FUNC_SYMBOL = SymbolUtils.createValueSymbolBuilder(FUNC_NAME).build();

private final ProtocolGenerator.GenerationContext context;

private final ArrayList<AuthParametersResolver> resolvers = new ArrayList<>();

public AuthParametersResolverGenerator(ProtocolGenerator.GenerationContext context) {
this.context = context;
}

public GoWriter.Writable generate() {
loadBindings();

var paramsSymbol = SymbolUtils.createPointableSymbolBuilder(AuthParametersGenerator.STRUCT_NAME).build();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use AuthParametersGenerator.STRUCT_SYMBOL where paramsSymbol is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this predates that symbol. Will update.


return goTemplate("""
$operationNamer:W

func $name:L(input interface{}, options Options) $params:P {
params := &$params:T{
Operation: input.(operationNamer).operationName(),
}

$bindings:W

return params
}
""",
MapUtils.of(
"name", FUNC_NAME,
"operationNamer", generateOperationNamer(),
"params", paramsSymbol,
"bindings", generateBindings()
));
}

private GoWriter.Writable generateOperationNamer() {
return (writer) -> {
writer.write("""
type operationNamer interface {
operationName() string
}
""");
};
}

private GoWriter.Writable generateBindings() {
return (writer) -> {
for (var resolver: resolvers) {
writer.write("$T(params, input, options)", resolver.resolver());
}
};
}

private void loadBindings() {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe a better name would be loadResolvers()?

for (var integration: context.getIntegrations()) {
var plugins = integration.getClientPlugins().stream().filter(it ->
it.matchesService(context.getModel(), context.getService())).toList();
for (var plugin: plugins) {
resolvers.addAll(plugin.getAuthParameterResolvers());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.Synthetic;
import software.amazon.smithy.go.codegen.auth.AuthGenerator;
import software.amazon.smithy.go.codegen.endpoints.EndpointResolutionGenerator;
import software.amazon.smithy.go.codegen.endpoints.FnGenerator;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -481,6 +482,15 @@ default void generateEndpointResolutionTests(GenerationContext context) {
generator.generateTests(context);
}

/**
* Generates smithy client auth components.
*
* @param context The generation context.
*/
default void generateAuth(GenerationContext context) {
new AuthGenerator(context).generate();
}

/**
* Context object used for service serialization and deserialization.
*/
Expand Down
Loading
Loading