Skip to content

Commit fbb15fd

Browse files
Update AWS Go SDK from v1 to v2 in Go client templates (#3) (#21653)
* Update AWS Go SDK from v1 to v2 in Go client templates (#3) * Initial plan * Update AWS Go SDK from v1 to v2 in Go client templates Co-authored-by: dennismouwen-eye <[email protected]> * Final validation: AWS SDK v2 integration working correctly Co-authored-by: dennismouwen-eye <[email protected]> * Fix AWS v4 signature implementation: retrieve credentials properly from provider Co-authored-by: dennismouwen-eye <[email protected]> * Fix AWS v4 signature payload hash: properly compute SHA-256 hash for request body Co-authored-by: dennismouwen-eye <[email protected]> * Implement proper payload hashing * Revert go.sum file to original state as requested Co-authored-by: dennismouwen-eye <[email protected]> * Revert go.mod and rename import alias from v4 back to awsv4 Co-authored-by: dennismouwen-eye <[email protected]> * Revert go.mod file to original state removing all changes Co-authored-by: dennismouwen-eye <[email protected]> * Rename credentials import to awscredentials alias as requested Co-authored-by: dennismouwen-eye <[email protected]> * Fix spacing * Fix credentials and imports * Update aws sdk to latest version --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: dennismouwen-eye <[email protected]> Co-authored-by: Dennis Mouwen <[email protected]> * Update indenting --------- Co-authored-by: Copilot <[email protected]>
1 parent 7c1dce4 commit fbb15fd

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

modules/openapi-generator/src/main/resources/go/client.mustache

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"golang.org/x/oauth2"
2828
{{/hasOAuthMethods}}
2929
{{#withAWSV4Signature}}
30-
awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
31-
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
30+
awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
31+
awscredentials "github.com/aws/aws-sdk-go-v2/credentials"
32+
"crypto/sha256"
33+
"encoding/hex"
3234
{{/withAWSV4Signature}}
3335
)
3436

@@ -458,13 +460,10 @@ func (c *APIClient) prepareRequest(
458460
{{#withAWSV4Signature}}
459461
// AWS Signature v4 Authentication
460462
if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
461-
creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, auth.SessionToken)
462-
signer := awsv4.NewSigner(creds)
463-
var reader *strings.Reader
464-
if body == nil {
465-
reader = strings.NewReader("")
466-
} else {
467-
reader = strings.NewReader(body.String())
463+
credsProvider := awscredentials.NewStaticCredentialsProvider(auth.AccessKey, auth.SecretKey, auth.SessionToken)
464+
creds, err := credsProvider.Retrieve(ctx)
465+
if err != nil {
466+
return nil, err
468467
}
469468
470469
// Define default values for region and service to maintain backward compatibility
@@ -477,8 +476,22 @@ func (c *APIClient) prepareRequest(
477476
service = "oapi"
478477
}
479478
480-
timestamp := time.Now()
481-
_, err := signer.Sign(localVarRequest, reader, service, region, timestamp)
479+
// Compute payload hash from the request body
480+
var payloadHash string
481+
if body == nil {
482+
// Empty body
483+
hash := sha256.Sum256([]byte(""))
484+
payloadHash = hex.EncodeToString(hash[:])
485+
} else {
486+
// Hash the actual body content
487+
bodyBytes := []byte(body.String())
488+
hash := sha256.Sum256(bodyBytes)
489+
payloadHash = hex.EncodeToString(hash[:])
490+
}
491+
492+
// Sign the request with the computed payload hash
493+
signer := awsv4.NewSigner()
494+
err = signer.SignHTTP(ctx, creds, localVarRequest, payloadHash, service, region, time.Now())
482495
if err != nil {
483496
return nil, err
484497
}

modules/openapi-generator/src/main/resources/go/go.mod.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
golang.org/x/oauth2 v0.27.0
99
{{/hasOAuthMethods}}
1010
{{#withAWSV4Signature}}
11-
github.com/aws/aws-sdk-go v1.34.14
11+
github.com/aws/aws-sdk-go-v2 v1.37.0
1212
{{/withAWSV4Signature}}
1313
{{#importValidator}}
1414
gopkg.in/validator.v2 v2.0.1

0 commit comments

Comments
 (0)