From daa9e2bc92cf10976d9cac2893b74834ded16150 Mon Sep 17 00:00:00 2001 From: Luc Talatinian <102624213+lucix-aws@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:09:45 -0500 Subject: [PATCH] codegen: use real sigv4a trait (#491) * codegen: use real sigv4a trait * include region auth param for sigv4a --- .../b39efe9f60e14df1b6fcab1c5795d0a4.json | 8 ++ codegen/gradle.properties | 2 +- .../smithy/go/codegen/auth/SigV4aTrait.java | 41 -------- .../integration/auth/SigV4ADefinition.java | 94 +++++++++++++++++++ .../integration/auth/SigV4AuthScheme.java | 13 +-- .../integration/auth/SigV4aDefinition.java | 50 ---------- 6 files changed, 110 insertions(+), 98 deletions(-) create mode 100644 .changelog/b39efe9f60e14df1b6fcab1c5795d0a4.json delete 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 delete mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java diff --git a/.changelog/b39efe9f60e14df1b6fcab1c5795d0a4.json b/.changelog/b39efe9f60e14df1b6fcab1c5795d0a4.json new file mode 100644 index 000000000..0bbd3db59 --- /dev/null +++ b/.changelog/b39efe9f60e14df1b6fcab1c5795d0a4.json @@ -0,0 +1,8 @@ +{ + "id": "b39efe9f-60e1-4df1-b6fc-ab1c5795d0a4", + "type": "feature", + "description": "Add codegen definition for sigv4a trait.", + "modules": [ + "." + ] +} \ No newline at end of file diff --git a/codegen/gradle.properties b/codegen/gradle.properties index 3c7e35179..2ba2fdc35 100644 --- a/codegen/gradle.properties +++ b/codegen/gradle.properties @@ -1,2 +1,2 @@ -smithyVersion=1.41.1 +smithyVersion=1.42.0 smithyGradleVersion=0.7.0 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 deleted file mode 100644 index d36ecb5e5..000000000 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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/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..9d78cfa2f --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4ADefinition.java @@ -0,0 +1,94 @@ +/* + * 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.emptyGoTemplate; +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; + +import java.util.Map; +import software.amazon.smithy.aws.traits.auth.SigV4ATrait; +import software.amazon.smithy.aws.traits.auth.SigV4Trait; +import software.amazon.smithy.aws.traits.auth.UnsignedPayloadTrait; +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; +import software.amazon.smithy.utils.MapUtils; + +/** + * Implements codegen for aws.auth#sigv4a. + */ +public class SigV4ADefinition implements AuthSchemeDefinition { + private static final Map COMMON_ENV = MapUtils.of( + "properties", SmithyGoTypes.Smithy.Properties, + "option", SmithyGoTypes.Auth.Option, + "schemeId", SmithyGoTypes.Auth.SchemeIDSigV4A, + "setSigningName", SmithyGoTypes.Transport.Http.SetSigV4ASigningName, + "setSigningRegions", SmithyGoTypes.Transport.Http.SetSigV4ASigningRegions + ); + + @Override + public GoWriter.Writable generateServiceOption( + ProtocolGenerator.GenerationContext context, ServiceShape service + ) { + var trait = service.expectTrait(SigV4ATrait.class); + return goTemplate(""" + &$option:T{ + SchemeID: $schemeId:T, + SignerProperties: func() $properties:T { + var props $properties:T + $setSigningName:T(&props, $name:S) + $setSigningRegions:T(&props, []string{params.Region}) + return props + }(), + },""", + COMMON_ENV, + MapUtils.of( + "name", trait.getName() + )); + } + + @Override + public GoWriter.Writable generateOperationOption( + ProtocolGenerator.GenerationContext context, OperationShape operation + ) { + var trait = context.getService().expectTrait(SigV4Trait.class); + return goTemplate(""" + &$option:T{ + SchemeID: $schemeId:T, + SignerProperties: func() $properties:T { + var props $properties:T + $setSigningName:T(&props, $name:S) + $setSigningRegions:T(&props, []string{params.Region}) + $unsignedPayload:W + return props + }(), + },""", + COMMON_ENV, + MapUtils.of( + "name", trait.getName(), + "unsignedPayload", generateIsUnsignedPayload(operation) + )); + } + + private GoWriter.Writable generateIsUnsignedPayload(OperationShape operation) { + return operation.hasTrait(UnsignedPayloadTrait.class) + ? goTemplate("$T(&props, true)", SmithyGoTypes.Transport.Http.SetIsUnsignedPayload) + : emptyGoTemplate(); + } +} diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4AuthScheme.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4AuthScheme.java index 087be72ab..eaf410df0 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4AuthScheme.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4AuthScheme.java @@ -16,6 +16,7 @@ package software.amazon.smithy.go.codegen.integration.auth; import java.util.List; +import software.amazon.smithy.aws.traits.auth.SigV4ATrait; import software.amazon.smithy.aws.traits.auth.SigV4Trait; import software.amazon.smithy.go.codegen.auth.AuthParameter; import software.amazon.smithy.go.codegen.integration.GoIntegration; @@ -25,20 +26,20 @@ import software.amazon.smithy.utils.ListUtils; /** - * Code generation for SigV4. + * Code generation for SigV4/SigV4A. */ public class SigV4AuthScheme implements GoIntegration { - private boolean isSigV4Service(Model model, ServiceShape service) { - return service.hasTrait(SigV4Trait.class); + private boolean isSigV4XService(Model model, ServiceShape service) { + return service.hasTrait(SigV4Trait.class) || service.hasTrait(SigV4ATrait.class); } @Override public List getClientPlugins() { - // FUTURE: add default Region client option, scheme definition, and resolver - we need a more structured way of - // suppressing elements of a GoIntegration before we do so, for now those live on the SDK side + // FUTURE: add default Region client option and scheme definitions - we need a more structured way of + // suppressing elements of a GoIntegration before we do so, for now those are registered SDK-side return ListUtils.of( RuntimeClientPlugin.builder() - .servicePredicate(this::isSigV4Service) + .servicePredicate(this::isSigV4XService) .addAuthParameter(AuthParameter.REGION) .build() ); 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 deleted file mode 100644 index af4f3b88b..000000000 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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{SchemeID: $T},", - SmithyGoTypes.Auth.Option, - SmithyGoTypes.Auth.SchemeIDSigV4A); - } - - @Override - public GoWriter.Writable generateOperationOption( - ProtocolGenerator.GenerationContext context, OperationShape operation - ) { - return goTemplate("&$T{SchemeID: $T},", - SmithyGoTypes.Auth.Option, - SmithyGoTypes.Auth.SchemeIDSigV4A); - } -}