From f234157fe98bea68c70ad5bfc609cee85f5a1cee Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Fri, 20 Oct 2023 11:55:29 -0400 Subject: [PATCH] add sigv4a --- auth/identity.go | 21 ++++ .../smithy/go/codegen/GoStdlibTypes.java | 4 + .../amazon/smithy/go/codegen/GoWriter.java | 4 + .../smithy/go/codegen/OperationGenerator.java | 26 ++-- .../smithy/go/codegen/ServiceGenerator.java | 117 +++++++++++++----- .../SetOperationInputContextMiddleware.java | 41 ++++++ .../smithy/go/codegen/SmithyGoTypes.java | 10 ++ .../amazon/smithy/go/codegen/SymbolUtils.java | 20 ++- .../auth/GetIdentityMiddlewareGenerator.java | 16 +-- .../ResolveAuthSchemeMiddlewareGenerator.java | 29 ++--- .../smithy/go/codegen/auth/SigV4aTrait.java | 41 ++++++ .../auth/SignRequestMiddlewareGenerator.java | 15 ++- .../EndpointClientPluginsGenerator.java | 78 ++++-------- .../EndpointMiddlewareGenerator.java | 84 ++++++------- .../integration/ConfigFieldResolver.java | 9 +- .../EndpointHostPrefixMiddleware.java | 12 +- .../integration/auth/AnonymousDefinition.java | 5 + .../integration/auth/SigV4aDefinition.java | 46 +++++++ 18 files changed, 386 insertions(+), 192 deletions(-) create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SetOperationInputContextMiddleware.java create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java diff --git a/auth/identity.go b/auth/identity.go index 0e174bda1..f78466b44 100644 --- a/auth/identity.go +++ b/auth/identity.go @@ -24,3 +24,24 @@ type IdentityResolver interface { type IdentityResolverOptions interface { GetIdentityResolver(schemeID string) IdentityResolver } + +// AnonymousIdentity is a sentinel to indicate no identity. +type AnonymousIdentity struct{} + +var _ Identity = (*AnonymousIdentity)(nil) + +// Expiration returns the zero value for time, as anonymous identity never +// expires. +func (*AnonymousIdentity) Expiration() time.Time { + return time.Time{} +} + +// AnonymousIdentityResolver returns AnonymousIdentity. +type AnonymousIdentityResolver struct{} + +var _ IdentityResolver = (*AnonymousIdentityResolver)(nil) + +// GetIdentity returns AnonymousIdentity. +func (*AnonymousIdentityResolver) GetIdentity(ctx context.Context, _ smithy.Properties) (Identity, error) { + return &AnonymousIdentity{}, nil +} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoStdlibTypes.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoStdlibTypes.java index 89e65ccab..f818ce7d1 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoStdlibTypes.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoStdlibTypes.java @@ -38,4 +38,8 @@ public static final class Http { public static final Symbol Response = SmithyGoDependency.NET_HTTP.pointableSymbol("Response"); } } + + public static final class Path { + public static final Symbol Join = SmithyGoDependency.PATH.valueSymbol("Join"); + } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java index 7c1d35b51..195113c2b 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoWriter.java @@ -854,6 +854,10 @@ private boolean isTargetDeprecated(Model model, MemberShape member) { && !Prelude.isPreludeShape(member.getTarget()); } + public void write(Writable w) { + write("$W", w); + } + @Override public String toString() { String contents = super.toString(); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java index aea33c500..74443210c 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java @@ -22,9 +22,6 @@ import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolProvider; -import software.amazon.smithy.go.codegen.auth.GetIdentityMiddlewareGenerator; -import software.amazon.smithy.go.codegen.auth.ResolveAuthSchemeMiddlewareGenerator; -import software.amazon.smithy.go.codegen.auth.SignRequestMiddlewareGenerator; import software.amazon.smithy.go.codegen.endpoints.EndpointParameterOperationBindingsGenerator; import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; @@ -204,8 +201,6 @@ private void generateAddOperationMiddleware() { MiddlewareRegistrar middlewareRegistrar = runtimeClientPlugin.registerMiddleware().get(); Collection functionArguments = middlewareRegistrar.getFunctionArguments(); - // TODO these functions do not all return err like they should. This should be fixed. - // TODO Must be fixed for all public functions. if (middlewareRegistrar.getInlineRegisterMiddlewareStatement() != null) { String registerStatement = String.format("if err = stack.%s", middlewareRegistrar.getInlineRegisterMiddlewareStatement()); @@ -232,12 +227,6 @@ private void generateAddOperationMiddleware() { } }); - writer.write("$W", GoWriter.ChainWritable.of( - ResolveAuthSchemeMiddlewareGenerator.generateAddMiddleware(operationSymbol.getName()), - SignRequestMiddlewareGenerator.generateAddMiddleware(), - GetIdentityMiddlewareGenerator.generateAddMiddleware() - ).compose()); - writer.write("return nil"); }); } @@ -251,6 +240,12 @@ private void generateOperationProtocolMiddlewareAdders() { } writer.addUseImports(SmithyGoDependency.SMITHY_MIDDLEWARE); + // persist operation input to context for internal build/finalize middleware access + writer.write(""" + if err := stack.Serialize.Add(&setOperationInputMiddleware{}, middleware.After); err != nil { + return err + }"""); + // Add request serializer middleware String serializerMiddlewareName = ProtocolGenerator.getSerializeMiddlewareName( operation.getId(), service, protocolGenerator.getProtocolName()); @@ -262,6 +257,15 @@ private void generateOperationProtocolMiddlewareAdders() { operation.getId(), service, protocolGenerator.getProtocolName()); writer.write("err = stack.Deserialize.Add(&$L{}, middleware.After)", deserializerMiddlewareName); writer.write("if err != nil { return err }"); + + // FUTURE: retry middleware should be at the front of finalize, right now it's added by the SDK + writer.write(""" + if err := addProtocolFinalizerMiddlewares(stack, options, $S); err != nil { + return $T("add protocol finalizers: %v", err) + }""", + operationSymbol.getName(), + GoStdlibTypes.Fmt.Errorf); + writer.write(""); } /** diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java index ee8805e59..22a751912 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ServiceGenerator.java @@ -25,8 +25,13 @@ import java.util.Map; import java.util.function.Predicate; import java.util.stream.Collectors; +import java.util.stream.Stream; import software.amazon.smithy.codegen.core.SymbolProvider; import software.amazon.smithy.go.codegen.auth.AuthSchemeResolverGenerator; +import software.amazon.smithy.go.codegen.auth.GetIdentityMiddlewareGenerator; +import software.amazon.smithy.go.codegen.auth.ResolveAuthSchemeMiddlewareGenerator; +import software.amazon.smithy.go.codegen.auth.SignRequestMiddlewareGenerator; +import software.amazon.smithy.go.codegen.endpoints.EndpointMiddlewareGenerator; import software.amazon.smithy.go.codegen.integration.AuthSchemeDefinition; import software.amazon.smithy.go.codegen.integration.ClientMember; import software.amazon.smithy.go.codegen.integration.ClientMemberResolver; @@ -34,10 +39,12 @@ import software.amazon.smithy.go.codegen.integration.ConfigFieldResolver; import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin; +import software.amazon.smithy.go.codegen.integration.auth.AnonymousDefinition; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.knowledge.ServiceIndex; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.model.traits.synthetic.NoAuthTrait; import software.amazon.smithy.utils.MapUtils; /** @@ -83,7 +90,7 @@ final class ServiceGenerator implements Runnable { @Override public void run() { - writer.write("$W", generate()); + writer.write(generate()); writeProtocolResolverImpls(); } @@ -93,7 +100,9 @@ private GoWriter.Writable generate() { generateClient(), generateNew(), generateOptions(), - generateInvokeOperation() + generateInvokeOperation(), + generateInputContextFuncs(), + generateAddProtocolFinalizerMiddleware() ).compose(); } @@ -172,11 +181,13 @@ func New(options $options:L, optFns ...func(*$options:L)) *$client:L { fn(&options) } + $finalizeResolvers:W + client := &$client:L{ options: options, } - $finalizeResolvers:W + $finalizeWithClientResolvers:W $clientMemberResolvers:W @@ -188,20 +199,22 @@ func New(options $options:L, optFns ...func(*$options:L)) *$client:L { "client", serviceSymbol.getName(), "protocolResolvers", generateProtocolResolvers(), "initializeResolvers", GoWriter.ChainWritable.of( - plugins.stream() - .flatMap(it -> it.getConfigFieldResolvers().stream()) - .filter(it -> it.getLocation().equals(ConfigFieldResolver.Location.CLIENT)) - .filter(it -> it.getTarget().equals(ConfigFieldResolver.Target.INITIALIZATION)) - .map(this::generateConfigFieldResolver) - .toList() + getConfigResolvers( + ConfigFieldResolver.Location.CLIENT, + ConfigFieldResolver.Target.INITIALIZATION + ).map(this::generateConfigFieldResolver).toList() ).compose(), "finalizeResolvers", GoWriter.ChainWritable.of( - plugins.stream() - .flatMap(it -> it.getConfigFieldResolvers().stream()) - .filter(it -> it.getLocation().equals(ConfigFieldResolver.Location.CLIENT)) - .filter(it -> it.getTarget().equals(ConfigFieldResolver.Target.FINALIZATION)) - .map(this::generateConfigFieldResolver) - .toList() + getConfigResolvers( + ConfigFieldResolver.Location.CLIENT, + ConfigFieldResolver.Target.FINALIZATION + ).map(this::generateConfigFieldResolver).toList() + ).compose(), + "finalizeWithClientResolvers", GoWriter.ChainWritable.of( + getConfigResolvers( + ConfigFieldResolver.Location.CLIENT, + ConfigFieldResolver.Target.FINALIZATION_WITH_CLIENT + ).map(this::generateConfigFieldResolver).toList() ).compose(), "clientMemberResolvers", GoWriter.ChainWritable.of( plugins.stream() @@ -443,6 +456,7 @@ func resolveAuthSchemes(options *Options) { private GoWriter.Writable generateOptionsGetIdentityResolver() { return goTemplate(""" func (o $L) GetIdentityResolver(schemeID string) $T { + $W $W return nil } @@ -455,7 +469,8 @@ private GoWriter.Writable generateOptionsGetIdentityResolver() { .filter(authSchemes::containsKey) .map(trait -> generateGetIdentityResolverMapping(trait, authSchemes.get(trait))) .toList() - ).compose(false)); + ).compose(false), + generateGetIdentityResolverMapping(NoAuthTrait.ID, new AnonymousDefinition())); } private GoWriter.Writable generateGetIdentityResolverMapping(ShapeId schemeId, AuthSchemeDefinition scheme) { @@ -467,9 +482,6 @@ private GoWriter.Writable generateGetIdentityResolverMapping(ShapeId schemeId, A @SuppressWarnings("checkstyle:LineLength") private GoWriter.Writable generateInvokeOperation() { - var plugins = runtimePlugins.stream() - .filter(it -> it.matchesService(model, service)) - .toList(); return goTemplate(""" func (c *Client) invokeOperation(ctx $context:T, opID string, params interface{}, optFns []func(*Options), stackFns ...func($stack:P, Options) error) (result interface{}, metadata $metadata:T, err error) { ctx = $clearStackValues:T(ctx) @@ -516,20 +528,16 @@ private GoWriter.Writable generateInvokeOperation() { "newStackHandler", generateNewStackHandler(), "operationError", SmithyGoTypes.Smithy.OperationError, "resolvers", GoWriter.ChainWritable.of( - plugins.stream() - .flatMap(it -> it.getConfigFieldResolvers().stream()) - .filter(it -> it.getLocation().equals(ConfigFieldResolver.Location.OPERATION)) - .filter(it -> it.getTarget().equals(ConfigFieldResolver.Target.INITIALIZATION)) - .map(this::generateConfigFieldResolver) - .toList() + getConfigResolvers( + ConfigFieldResolver.Location.OPERATION, + ConfigFieldResolver.Target.INITIALIZATION + ).map(this::generateConfigFieldResolver).toList() ).compose(), "finalizers", GoWriter.ChainWritable.of( - plugins.stream() - .flatMap(it -> it.getConfigFieldResolvers().stream()) - .filter(it -> it.getLocation().equals(ConfigFieldResolver.Location.OPERATION)) - .filter(it -> it.getTarget().equals(ConfigFieldResolver.Target.FINALIZATION)) - .map(this::generateConfigFieldResolver) - .toList() + getConfigResolvers( + ConfigFieldResolver.Location.OPERATION, + ConfigFieldResolver.Target.FINALIZATION + ).map(this::generateConfigFieldResolver).toList() ).compose() )); } @@ -552,4 +560,51 @@ private void ensureSupportedProtocol() { "Protocols other than HTTP are not yet implemented: " + applicationProtocol); } } + + private Stream getConfigResolvers( + ConfigFieldResolver.Location location, ConfigFieldResolver.Target target + ) { + return runtimePlugins.stream() + .filter(it -> it.matchesService(model, service)) + .flatMap(it -> it.getConfigFieldResolvers().stream()) + .filter(it -> it.getLocation() == location && it.getTarget() == target); + } + + private GoWriter.Writable generateInputContextFuncs() { + return goTemplate(""" + type operationInputKey struct{} + + func setOperationInput(ctx $1T, input interface{}) $1T { + return $2T(ctx, operationInputKey{}, input) + } + + func getOperationInput(ctx $1T) interface{} { + return $3T(ctx, operationInputKey{}) + } + + $4W + """, + GoStdlibTypes.Context.Context, + SmithyGoTypes.Middleware.WithStackValue, + SmithyGoTypes.Middleware.GetStackValue, + new SetOperationInputContextMiddleware().generate()); + } + + private GoWriter.Writable generateAddProtocolFinalizerMiddleware() { + ensureSupportedProtocol(); + return goTemplate(""" + func addProtocolFinalizerMiddlewares(stack $P, options $L, operation string) error { + $W + return nil + } + """, + SmithyGoTypes.Middleware.Stack, + CONFIG_NAME, + GoWriter.ChainWritable.of( + ResolveAuthSchemeMiddlewareGenerator.generateAddToProtocolFinalizers(), + GetIdentityMiddlewareGenerator.generateAddToProtocolFinalizers(), + EndpointMiddlewareGenerator.generateAddToProtocolFinalizers(), + SignRequestMiddlewareGenerator.generateAddToProtocolFinalizers() + ).compose(false)); + } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SetOperationInputContextMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SetOperationInputContextMiddleware.java new file mode 100644 index 000000000..7854b2eba --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SetOperationInputContextMiddleware.java @@ -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; + +import static software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator.createSerializeStepMiddleware; +import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate; +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; + +/** + * Middleware to set the final operation input on the context at the start of the serialize step such that protocol + * middlewares in later phases can use it. + */ +public class SetOperationInputContextMiddleware { + public static final String MIDDLEWARE_NAME = "setOperationInputMiddleware"; + public static final String MIDDLEWARE_ID = "setOperationInput"; + + public GoWriter.Writable generate() { + return createSerializeStepMiddleware(MIDDLEWARE_NAME, MiddlewareIdentifier.string(MIDDLEWARE_ID)) + .asWritable(generateBody(), emptyGoTemplate()); + } + + private GoWriter.Writable generateBody() { + return goTemplate(""" + ctx = setOperationInput(ctx, in.Parameters) + return next.HandleSerialize(ctx, in) + """); + } +} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoTypes.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoTypes.java index ab888644b..6d453c588 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoTypes.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoTypes.java @@ -59,6 +59,10 @@ public static final class Http { public static final Symbol NewStackRequest = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("NewStackRequest"); public static final Symbol NewClientHandler = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("NewClientHandler"); + public static final Symbol JoinPath = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("JoinPath"); + + public static final Symbol GetHostnameImmutable = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("GetHostnameImmutable"); + public static final Symbol AuthScheme = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("AuthScheme"); public static final Symbol SchemeIDAnonymous = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("SchemeIDAnonymous"); public static final Symbol NewAnonymousScheme = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("NewAnonymousScheme"); @@ -71,6 +75,11 @@ public static final class Http { public static final Symbol SigV4Properties = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.pointableSymbol("SigV4Properties"); public static final Symbol SigV4AProperties = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.pointableSymbol("SigV4AProperties"); + public static final Symbol GetSigV4SigningName = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("GetSigV4SigningName"); + public static final Symbol GetSigV4SigningRegion = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("GetSigV4SigningName"); + public static final Symbol GetSigV4ASigningName = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("GetSigV4ASigningName"); + public static final Symbol GetSigV4ASigningRegions = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("GetSigV4ASigningRegions"); + public static final Symbol SetSigV4SigningName = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("SetSigV4SigningName"); public static final Symbol SetSigV4SigningRegion = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("SetSigV4SigningRegion"); public static final Symbol SetSigV4ASigningName = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("SetSigV4ASigningName"); @@ -84,6 +93,7 @@ public static final class Auth { public static final Symbol Option = SmithyGoDependency.SMITHY_AUTH.pointableSymbol("Option"); public static final Symbol IdentityResolver = SmithyGoDependency.SMITHY_AUTH.valueSymbol("IdentityResolver"); public static final Symbol Identity = SmithyGoDependency.SMITHY_AUTH.valueSymbol("Identity"); + public static final Symbol AnonymousIdentityResolver = SmithyGoDependency.SMITHY_AUTH.pointableSymbol("AnonymousIdentityResolver"); public static final Symbol GetAuthOptions = SmithyGoDependency.SMITHY_AUTH.valueSymbol("GetAuthOptions"); public static final Symbol SetAuthOptions = SmithyGoDependency.SMITHY_AUTH.valueSymbol("SetAuthOptions"); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SymbolUtils.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SymbolUtils.java index 3530931c7..e3059dcaf 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SymbolUtils.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SymbolUtils.java @@ -193,17 +193,13 @@ public static boolean isUniverseType(Symbol symbol) { .orElse(false); } - public static Symbol.Builder getPackageSymbol( - String importPath, String symbolName, String namespaceAlias, boolean pointable - ) { - Symbol.Builder builder; - if (pointable) { - builder = SymbolUtils.createPointableSymbolBuilder(symbolName); - } else { - builder = SymbolUtils.createValueSymbolBuilder(symbolName); - } - - // TODO this doesn't seem right - return builder.namespace(importPath, "/").putProperty(SymbolUtils.NAMESPACE_ALIAS, namespaceAlias); + /** + * Builds a symbol within the context of the package in which codegen is taking place. + * + * @param name Symbol name. + * @return The built symbol. + */ + public static Symbol buildPackageSymbol(String name) { + return Symbol.builder().name(name).build(); } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/GetIdentityMiddlewareGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/GetIdentityMiddlewareGenerator.java index 4d6f8fa44..643743b42 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/GetIdentityMiddlewareGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/GetIdentityMiddlewareGenerator.java @@ -35,15 +35,17 @@ public GetIdentityMiddlewareGenerator(ProtocolGenerator.GenerationContext contex this.context = context; } - public static GoWriter.Writable generateAddMiddleware() { + public static GoWriter.Writable generateAddToProtocolFinalizers() { return goTemplate(""" - err = stack.Finalize.Add(&$L{ - options: options, - }, $T) - if err != nil { - return err + if err := stack.Finalize.Insert(&$L{options: options}, $S, $T); err != nil { + return $T("add $L: %v", err) } - """, MIDDLEWARE_NAME, SmithyGoTypes.Middleware.Before); + """, + MIDDLEWARE_NAME, + ResolveAuthSchemeMiddlewareGenerator.MIDDLEWARE_ID, + SmithyGoTypes.Middleware.After, + GoStdlibTypes.Fmt.Errorf, + MIDDLEWARE_ID); } public GoWriter.Writable generate() { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/ResolveAuthSchemeMiddlewareGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/ResolveAuthSchemeMiddlewareGenerator.java index 18de4a725..a8149f4cb 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/ResolveAuthSchemeMiddlewareGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/ResolveAuthSchemeMiddlewareGenerator.java @@ -15,7 +15,7 @@ package software.amazon.smithy.go.codegen.auth; -import static software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator.createSerializeStepMiddleware; +import static software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator.createFinalizeStepMiddleware; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; import software.amazon.smithy.codegen.core.Symbol; @@ -23,7 +23,6 @@ import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.MiddlewareIdentifier; import software.amazon.smithy.go.codegen.SmithyGoTypes; -import software.amazon.smithy.go.codegen.endpoints.EndpointMiddlewareGenerator; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.utils.MapUtils; @@ -37,27 +36,21 @@ public ResolveAuthSchemeMiddlewareGenerator(ProtocolGenerator.GenerationContext this.context = context; } - public static GoWriter.Writable generateAddMiddleware(String operation) { + public static GoWriter.Writable generateAddToProtocolFinalizers() { return goTemplate(""" - err = stack.Serialize.Insert(&$name:L{ - operation: $operation:S, - options: options, - }, $resolveEndpointV2:S, $before:T) - if err != nil { - return err + if err := stack.Finalize.Add(&$L{operation: operation, options: options}, $T); err != nil { + return $T("add $L: %v", err) } """, - MapUtils.of( - "name", MIDDLEWARE_NAME, - "operation", operation, - "resolveEndpointV2", EndpointMiddlewareGenerator.MIDDLEWARE_ID, - "before", SmithyGoTypes.Middleware.Before - )); + MIDDLEWARE_NAME, + SmithyGoTypes.Middleware.Before, + GoStdlibTypes.Fmt.Errorf, + MIDDLEWARE_ID); } public GoWriter.Writable generate() { return GoWriter.ChainWritable.of( - createSerializeStepMiddleware(MIDDLEWARE_NAME, MiddlewareIdentifier.string(MIDDLEWARE_ID)) + createFinalizeStepMiddleware(MIDDLEWARE_NAME, MiddlewareIdentifier.string(MIDDLEWARE_ID)) .asWritable(generateBody(), generateFields()), generateSelectScheme(), generateContextFuncs() @@ -73,7 +66,7 @@ private GoWriter.Writable generateFields() { private GoWriter.Writable generateBody() { return goTemplate(""" - params := $1L(m.operation, in.Parameters, m.options) + params := $1L(m.operation, getOperationInput(ctx), m.options) options, err := m.options.AuthSchemeResolver.ResolveAuthSchemes(ctx, params) if err != nil { return out, metadata, $2T("resolve auth scheme: %v", err) @@ -85,7 +78,7 @@ private GoWriter.Writable generateBody() { } ctx = setResolvedAuthScheme(ctx, scheme) - return next.HandleSerialize(ctx, in) + return next.HandleFinalize(ctx, in) """, AuthParametersResolverGenerator.FUNC_NAME, GoStdlibTypes.Fmt.Errorf diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java new file mode 100644 index 000000000..d36ecb5e5 --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java @@ -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.model.node.Node; +import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.model.traits.AbstractTrait; + +/** + * Fake trait for aws.auth#sigv4a until smithy adds it. + */ +public final class SigV4aTrait extends AbstractTrait { + public static final ShapeId ID = ShapeId.from("aws.auth#sigv4a"); + + public SigV4aTrait() { + super(ID, Node.objectNode()); + } + + @Override + protected Node createNode() { + return Node.objectNode(); + } + + @Override + public boolean isSynthetic() { + return true; + } +} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SignRequestMiddlewareGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SignRequestMiddlewareGenerator.java index e38da278e..0fac9fa7a 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SignRequestMiddlewareGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SignRequestMiddlewareGenerator.java @@ -23,6 +23,7 @@ import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.MiddlewareIdentifier; import software.amazon.smithy.go.codegen.SmithyGoTypes; +import software.amazon.smithy.go.codegen.endpoints.EndpointMiddlewareGenerator; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.utils.MapUtils; @@ -36,13 +37,17 @@ public SignRequestMiddlewareGenerator(ProtocolGenerator.GenerationContext contex this.context = context; } - public static GoWriter.Writable generateAddMiddleware() { + public static GoWriter.Writable generateAddToProtocolFinalizers() { return goTemplate(""" - err = stack.Finalize.Add(&$L{}, $T) - if err != nil { - return err + if err := stack.Finalize.Insert(&$L{}, $S, $T); err != nil { + return $T("add $L: %v", err) } - """, MIDDLEWARE_NAME, SmithyGoTypes.Middleware.Before); + """, + MIDDLEWARE_NAME, + EndpointMiddlewareGenerator.MIDDLEWARE_ID, + SmithyGoTypes.Middleware.After, + GoStdlibTypes.Fmt.Errorf, + MIDDLEWARE_ID); } public GoWriter.Writable generate() { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointClientPluginsGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointClientPluginsGenerator.java index 22a3e39e5..5efc73b1d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointClientPluginsGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointClientPluginsGenerator.java @@ -17,19 +17,13 @@ import java.util.ArrayList; import java.util.List; -import java.util.Optional; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.go.codegen.GoSettings; import software.amazon.smithy.go.codegen.SymbolUtils; import software.amazon.smithy.go.codegen.integration.ConfigField; import software.amazon.smithy.go.codegen.integration.GoIntegration; -import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar; import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin; import software.amazon.smithy.model.Model; -import software.amazon.smithy.model.knowledge.TopDownIndex; -import software.amazon.smithy.model.shapes.OperationShape; -import software.amazon.smithy.model.shapes.ServiceShape; -import software.amazon.smithy.model.shapes.ToShapeId; import software.amazon.smithy.rulesengine.language.EndpointRuleSet; import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter; import software.amazon.smithy.rulesengine.traits.ClientContextParamsTrait; @@ -83,60 +77,32 @@ public List getClientPlugins() { @Override public void processFinalizedModel(GoSettings settings, Model model) { - ServiceShape service = settings.getService(model); - var rulesetTrait = service.getTrait(EndpointRuleSetTrait.class); - Optional rulesetOpt = (rulesetTrait.isPresent()) - ? Optional.of(EndpointRuleSet.fromNode(rulesetTrait.get().getRuleSet())) - : Optional.empty(); - var clientContextParamsTrait = service.getTrait(ClientContextParamsTrait.class); - - if (!rulesetOpt.isPresent()) { + var service = settings.getService(model); + if (!service.hasTrait(EndpointRuleSetTrait.class) || !service.hasTrait(ClientContextParamsTrait.class)) { return; } - var topDownIndex = TopDownIndex.of(model); - - for (ToShapeId operationId : topDownIndex.getContainedOperations(service)) { - OperationShape operationShape = model.expectShape(operationId.toShapeId(), OperationShape.class); - - Symbol addFunc = SymbolUtils.createValueSymbolBuilder(EndpointMiddlewareGenerator.ADD_FUNC_NAME).build(); - runtimeClientPlugins.add(RuntimeClientPlugin.builder() - .operationPredicate((m, s, o) -> { - return o.equals(operationShape); - }) - .registerMiddleware(MiddlewareRegistrar.builder() - .resolvedFunction(addFunc) - .useClientOptions() - .build()) - .build()); - - if (clientContextParamsTrait.isPresent()) { - if (rulesetOpt.isPresent()) { - var clientContextParams = clientContextParamsTrait.get(); - var parameters = rulesetOpt.get().getParameters(); - parameters.forEach(param -> { - if ( - clientContextParams.getParameters().containsKey(param.getName().getName().getValue()) - && !param.getBuiltIn().isPresent() - ) { - var documentation = param.getDocumentation().isPresent() - ? param.getDocumentation().get() - : ""; - - runtimeClientPlugins.add(RuntimeClientPlugin.builder() - .configFields(ListUtils.of( - ConfigField.builder() - .name(getExportedParameterName(param)) - .type(parameterAsSymbol(param)) - .documentation(documentation) - .build() - )) - .build()); - } - }); - } + var ruleset = EndpointRuleSet.fromNode(service.expectTrait(EndpointRuleSetTrait.class).getRuleSet()); + var ccParams = service.expectTrait(ClientContextParamsTrait.class).getParameters(); + var parameters = ruleset.getParameters(); + parameters.forEach(param -> { + var ccParam = ccParams.get(param.getName().getName().getValue()); + if (ccParam == null || param.getBuiltIn().isPresent()) { + return; } - } + + runtimeClientPlugins.add( + RuntimeClientPlugin.builder() + .addConfigField( + ConfigField.builder() + .name(getExportedParameterName(param)) + .type(parameterAsSymbol(param)) + .documentation(ccParam.getDocumentation().orElse("")) + .build() + ) + .build() + ); + }); } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointMiddlewareGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointMiddlewareGenerator.java index cb21f5e77..683056d8a 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointMiddlewareGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/endpoints/EndpointMiddlewareGenerator.java @@ -15,13 +15,14 @@ package software.amazon.smithy.go.codegen.endpoints; +import static software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator.createFinalizeStepMiddleware; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; -import software.amazon.smithy.go.codegen.GoStackStepMiddlewareGenerator; import software.amazon.smithy.go.codegen.GoStdlibTypes; import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.MiddlewareIdentifier; import software.amazon.smithy.go.codegen.SmithyGoTypes; +import software.amazon.smithy.go.codegen.auth.GetIdentityMiddlewareGenerator; import software.amazon.smithy.go.codegen.integration.GoIntegration; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; @@ -34,7 +35,6 @@ public final class EndpointMiddlewareGenerator { public static final String MIDDLEWARE_NAME = "resolveEndpointV2Middleware"; public static final String MIDDLEWARE_ID = "ResolveEndpointV2"; - public static final String ADD_FUNC_NAME = "addResolveEndpointV2Middleware"; private final ProtocolGenerator.GenerationContext context; @@ -42,36 +42,36 @@ public EndpointMiddlewareGenerator(ProtocolGenerator.GenerationContext context) this.context = context; } - public GoWriter.Writable generate() { - if (!context.getService().hasTrait(EndpointRuleSetTrait.class)) { - return goTemplate(""); - } - + public static GoWriter.Writable generateAddToProtocolFinalizers() { return goTemplate(""" - $W - - $W + if err := stack.Finalize.Insert(&$L{options: options}, $S, $T); err != nil { + return $T("add $L: %v", err) + } """, - generateMiddleware(), - generateAddFunc()); + MIDDLEWARE_NAME, + GetIdentityMiddlewareGenerator.MIDDLEWARE_ID, + SmithyGoTypes.Middleware.After, + GoStdlibTypes.Fmt.Errorf, + MIDDLEWARE_ID); } - private GoWriter.Writable generateMiddleware() { - return writer -> { - GoStackStepMiddlewareGenerator - .createSerializeStepMiddleware(MIDDLEWARE_NAME, MiddlewareIdentifier.string(MIDDLEWARE_ID)) - .writeMiddleware(writer, this::generateBody, this::generateFields); - }; + public GoWriter.Writable generate() { + return createFinalizeStepMiddleware(MIDDLEWARE_NAME, MiddlewareIdentifier.string(MIDDLEWARE_ID)) + .asWritable(generateBody(), generateFields()); } - private void generateFields(GoStackStepMiddlewareGenerator generator, GoWriter writer) { - writer.writeGoTemplate(""" + private GoWriter.Writable generateFields() { + return goTemplate(""" options Options """); } - private void generateBody(GoStackStepMiddlewareGenerator generator, GoWriter writer) { - writer.writeGoTemplate(""" + private GoWriter.Writable generateBody() { + if (!context.getService().hasTrait(EndpointRuleSetTrait.class)) { + return goTemplate("return next.HandleFinalize(ctx, in)"); + } + + return goTemplate(""" $pre:W $assertRequest:W @@ -84,7 +84,7 @@ private void generateBody(GoStackStepMiddlewareGenerator generator, GoWriter wri $post:W - return next.HandleSerialize(ctx, in) + return next.HandleFinalize(ctx, in) """, MapUtils.of( "pre", generatePreResolutionHooks(), @@ -126,18 +126,25 @@ private GoWriter.Writable generateAssertResolver() { private GoWriter.Writable generateResolveEndpoint() { return goTemplate(""" - params := bindEndpointParams(in.Parameters, m.options) - resolvedEndpoint, err := m.options.EndpointResolverV2.ResolveEndpoint(ctx, *params) + params := bindEndpointParams(getOperationInput(ctx), m.options) + endpt, err := m.options.EndpointResolverV2.ResolveEndpoint(ctx, *params) if err != nil { - return out, metadata, $T("failed to resolve service endpoint, %w", err) + return out, metadata, $1T("failed to resolve service endpoint, %w", err) } - req.URL = &resolvedEndpoint.URI - for k := range resolvedEndpoint.Headers { - req.Header.Set(k, resolvedEndpoint.Headers.Get(k)) + if endpt.URI.RawPath == "" && req.URL.RawPath != "" { + endpt.URI.RawPath = endpt.URI.Path + } + req.URL.Scheme = endpt.URI.Scheme + req.URL.Host = endpt.URI.Host + req.URL.Path = $2T(endpt.URI.Path, req.URL.Path) + req.URL.RawPath = $2T(endpt.URI.RawPath, req.URL.RawPath) + for k := range endpt.Headers { + req.Header.Set(k, endpt.Headers.Get(k)) } """, - GoStdlibTypes.Fmt.Errorf); + GoStdlibTypes.Fmt.Errorf, + SmithyGoTypes.Transport.Http.JoinPath); } private GoWriter.Writable generateMergeAuthProperties() { @@ -147,7 +154,7 @@ private GoWriter.Writable generateMergeAuthProperties() { return out, metadata, $T("no resolved auth scheme") } - opts, _ := $T(&resolvedEndpoint.Properties) + opts, _ := $T(&endpt.Properties) for _, o := range opts { rscheme.SignerProperties.SetAll(&o.SignerProperties) } @@ -161,19 +168,4 @@ private GoWriter.Writable generatePostResolutionHooks() { } }; } - - private GoWriter.Writable generateAddFunc() { - return goTemplate(""" - func $funcName:L(stack $stack:P, options Options) error { - return stack.Serialize.Insert(&$structName:L{ - options: options, - }, "ResolveEndpoint", middleware.After) - } - """, - MapUtils.of( - "funcName", ADD_FUNC_NAME, - "structName", MIDDLEWARE_NAME, - "stack", SmithyGoTypes.Middleware.Stack - )); - } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ConfigFieldResolver.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ConfigFieldResolver.java index 67874cce3..93e229d87 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ConfigFieldResolver.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ConfigFieldResolver.java @@ -120,7 +120,14 @@ public enum Target { /** * Indicates that the resolver targets config fields after customer mutation. */ - FINALIZATION + FINALIZATION, + + /** + * Indicates that the resolver targets config fields after the client has been instantiated. Resolvers with this + * target can then take a reference to the instantiated client in existing Options, but CANNOT modify fields on + * Options since it is passed to the already-existing client by value. + */ + FINALIZATION_WITH_CLIENT } public static class Builder implements SmithyBuilder { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java index 93484e73f..8b7853eb8 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/EndpointHostPrefixMiddleware.java @@ -27,6 +27,7 @@ import software.amazon.smithy.go.codegen.MiddlewareIdentifier; import software.amazon.smithy.go.codegen.SmithyGoDependency; import software.amazon.smithy.go.codegen.SymbolUtils; +import software.amazon.smithy.go.codegen.endpoints.EndpointMiddlewareGenerator; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.knowledge.TopDownIndex; import software.amazon.smithy.model.pattern.SmithyPattern; @@ -89,8 +90,8 @@ public void writeAdditionalFiles( middlewareHelperName, () -> { writer.write( - "return stack.Serialize.Insert(&$L{}, `OperationSerializer`, middleware.After)", - middlewareName); + "return stack.Finalize.Insert(&$L{}, $S, middleware.After)", + middlewareName, EndpointMiddlewareGenerator.MIDDLEWARE_ID); }); }); }); @@ -104,7 +105,7 @@ private static void writeMiddleware( SmithyPattern pattern ) { GoStackStepMiddlewareGenerator middlewareGenerator = - GoStackStepMiddlewareGenerator.createSerializeStepMiddleware( + GoStackStepMiddlewareGenerator.createFinalizeStepMiddleware( getMiddlewareName(operation), MIDDLEWARE_ID ); @@ -130,9 +131,10 @@ private static void writeMiddleware( writer.addUseImports(SmithyGoDependency.STRINGS); writer.addUseImports(SmithyGoDependency.SMITHY); StructureShape input = ProtocolUtils.expectInput(model, operation); - writer.write("input, ok := in.Parameters.($P)", symbolProvider.toSymbol(input)); + writer.write("opaqueInput := getOperationInput(ctx)"); + writer.write("input, ok := opaqueInput.($P)", symbolProvider.toSymbol(input)); w.openBlock("if !ok {", "}", () -> { - writer.write("return out, metadata, fmt.Errorf(\"unknown input type %T\", in.Parameters)"); + writer.write("return out, metadata, fmt.Errorf(\"unknown input type %T\", opaqueInput)"); }).write(""); w.write("var prefix strings.Builder"); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/AnonymousDefinition.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/AnonymousDefinition.java index d05daa509..c7c0c7574 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/AnonymousDefinition.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/AnonymousDefinition.java @@ -37,4 +37,9 @@ public GoWriter.Writable generateServiceOption(ProtocolGenerator.GenerationConte public GoWriter.Writable generateOperationOption(ProtocolGenerator.GenerationContext c, OperationShape o) { return goTemplate("$T(),", SmithyGoTypes.Transport.Http.NewAnonymousOption); } + + @Override + public GoWriter.Writable generateOptionsIdentityResolver() { + return goTemplate("&$T{}", SmithyGoTypes.Auth.AnonymousIdentityResolver); + } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java new file mode 100644 index 000000000..26e0803ed --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java @@ -0,0 +1,46 @@ +/* + * 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.integration.auth; + +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; + +import software.amazon.smithy.go.codegen.GoWriter; +import software.amazon.smithy.go.codegen.SmithyGoTypes; +import software.amazon.smithy.go.codegen.integration.AuthSchemeDefinition; +import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.ServiceShape; + +/** + * Implements codegen for aws.auth#sigv4a. + */ +public class SigV4aDefinition implements AuthSchemeDefinition { + // FUTURE: reference modeled sigv4a trait + + @Override + public GoWriter.Writable generateServiceOption( + ProtocolGenerator.GenerationContext context, ServiceShape service + ) { + return goTemplate("$T(),", SmithyGoTypes.Transport.Http.NewSigV4AOption); + } + + @Override + public GoWriter.Writable generateOperationOption( + ProtocolGenerator.GenerationContext context, OperationShape operation + ) { + return goTemplate("$T(),", SmithyGoTypes.Transport.Http.NewSigV4AOption); + } +}