Skip to content

Commit daa9e2b

Browse files
authored
codegen: use real sigv4a trait (#491)
* codegen: use real sigv4a trait * include region auth param for sigv4a
1 parent 4909204 commit daa9e2b

File tree

6 files changed

+110
-98
lines changed

6 files changed

+110
-98
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "b39efe9f-60e1-4df1-b6fc-ab1c5795d0a4",
3+
"type": "feature",
4+
"description": "Add codegen definition for sigv4a trait.",
5+
"modules": [
6+
"."
7+
]
8+
}

codegen/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
smithyVersion=1.41.1
1+
smithyVersion=1.42.0
22
smithyGradleVersion=0.7.0

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/auth/SigV4aTrait.java

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2023 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.smithy.go.codegen.integration.auth;
17+
18+
import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate;
19+
import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;
20+
21+
import java.util.Map;
22+
import software.amazon.smithy.aws.traits.auth.SigV4ATrait;
23+
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
24+
import software.amazon.smithy.aws.traits.auth.UnsignedPayloadTrait;
25+
import software.amazon.smithy.go.codegen.GoWriter;
26+
import software.amazon.smithy.go.codegen.SmithyGoTypes;
27+
import software.amazon.smithy.go.codegen.integration.AuthSchemeDefinition;
28+
import software.amazon.smithy.go.codegen.integration.ProtocolGenerator;
29+
import software.amazon.smithy.model.shapes.OperationShape;
30+
import software.amazon.smithy.model.shapes.ServiceShape;
31+
import software.amazon.smithy.utils.MapUtils;
32+
33+
/**
34+
* Implements codegen for aws.auth#sigv4a.
35+
*/
36+
public class SigV4ADefinition implements AuthSchemeDefinition {
37+
private static final Map<String, Object> COMMON_ENV = MapUtils.of(
38+
"properties", SmithyGoTypes.Smithy.Properties,
39+
"option", SmithyGoTypes.Auth.Option,
40+
"schemeId", SmithyGoTypes.Auth.SchemeIDSigV4A,
41+
"setSigningName", SmithyGoTypes.Transport.Http.SetSigV4ASigningName,
42+
"setSigningRegions", SmithyGoTypes.Transport.Http.SetSigV4ASigningRegions
43+
);
44+
45+
@Override
46+
public GoWriter.Writable generateServiceOption(
47+
ProtocolGenerator.GenerationContext context, ServiceShape service
48+
) {
49+
var trait = service.expectTrait(SigV4ATrait.class);
50+
return goTemplate("""
51+
&$option:T{
52+
SchemeID: $schemeId:T,
53+
SignerProperties: func() $properties:T {
54+
var props $properties:T
55+
$setSigningName:T(&props, $name:S)
56+
$setSigningRegions:T(&props, []string{params.Region})
57+
return props
58+
}(),
59+
},""",
60+
COMMON_ENV,
61+
MapUtils.of(
62+
"name", trait.getName()
63+
));
64+
}
65+
66+
@Override
67+
public GoWriter.Writable generateOperationOption(
68+
ProtocolGenerator.GenerationContext context, OperationShape operation
69+
) {
70+
var trait = context.getService().expectTrait(SigV4Trait.class);
71+
return goTemplate("""
72+
&$option:T{
73+
SchemeID: $schemeId:T,
74+
SignerProperties: func() $properties:T {
75+
var props $properties:T
76+
$setSigningName:T(&props, $name:S)
77+
$setSigningRegions:T(&props, []string{params.Region})
78+
$unsignedPayload:W
79+
return props
80+
}(),
81+
},""",
82+
COMMON_ENV,
83+
MapUtils.of(
84+
"name", trait.getName(),
85+
"unsignedPayload", generateIsUnsignedPayload(operation)
86+
));
87+
}
88+
89+
private GoWriter.Writable generateIsUnsignedPayload(OperationShape operation) {
90+
return operation.hasTrait(UnsignedPayloadTrait.class)
91+
? goTemplate("$T(&props, true)", SmithyGoTypes.Transport.Http.SetIsUnsignedPayload)
92+
: emptyGoTemplate();
93+
}
94+
}

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4AuthScheme.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.smithy.go.codegen.integration.auth;
1717

1818
import java.util.List;
19+
import software.amazon.smithy.aws.traits.auth.SigV4ATrait;
1920
import software.amazon.smithy.aws.traits.auth.SigV4Trait;
2021
import software.amazon.smithy.go.codegen.auth.AuthParameter;
2122
import software.amazon.smithy.go.codegen.integration.GoIntegration;
@@ -25,20 +26,20 @@
2526
import software.amazon.smithy.utils.ListUtils;
2627

2728
/**
28-
* Code generation for SigV4.
29+
* Code generation for SigV4/SigV4A.
2930
*/
3031
public class SigV4AuthScheme implements GoIntegration {
31-
private boolean isSigV4Service(Model model, ServiceShape service) {
32-
return service.hasTrait(SigV4Trait.class);
32+
private boolean isSigV4XService(Model model, ServiceShape service) {
33+
return service.hasTrait(SigV4Trait.class) || service.hasTrait(SigV4ATrait.class);
3334
}
3435

3536
@Override
3637
public List<RuntimeClientPlugin> getClientPlugins() {
37-
// FUTURE: add default Region client option, scheme definition, and resolver - we need a more structured way of
38-
// suppressing elements of a GoIntegration before we do so, for now those live on the SDK side
38+
// FUTURE: add default Region client option and scheme definitions - we need a more structured way of
39+
// suppressing elements of a GoIntegration before we do so, for now those are registered SDK-side
3940
return ListUtils.of(
4041
RuntimeClientPlugin.builder()
41-
.servicePredicate(this::isSigV4Service)
42+
.servicePredicate(this::isSigV4XService)
4243
.addAuthParameter(AuthParameter.REGION)
4344
.build()
4445
);

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/auth/SigV4aDefinition.java

-50
This file was deleted.

0 commit comments

Comments
 (0)