From fa8d49b22ad091c292f62d519499ddd3e3e513c8 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 10:30:03 +0200 Subject: [PATCH 1/2] 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 <107839749+dennismouwen-eye@users.noreply.github.com> * Final validation: AWS SDK v2 integration working correctly Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Fix AWS v4 signature implementation: retrieve credentials properly from provider Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Fix AWS v4 signature payload hash: properly compute SHA-256 hash for request body Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Implement proper payload hashing * Revert go.sum file to original state as requested Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Revert go.mod and rename import alias from v4 back to awsv4 Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Revert go.mod file to original state removing all changes Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Rename credentials import to awscredentials alias as requested Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> * Fix spacing * Fix credentials and imports * Update aws sdk to latest version --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> Co-authored-by: Dennis Mouwen --- .../src/main/resources/go/client.mustache | 35 +++++++++++++------ .../src/main/resources/go/go.mod.mustache | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index 0bff8913d4a5..307aa0acc250 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -27,8 +27,10 @@ import ( "golang.org/x/oauth2" {{/hasOAuthMethods}} {{#withAWSV4Signature}} - awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4" - awscredentials "github.com/aws/aws-sdk-go/aws/credentials" + awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" + awscredentials "github.com/aws/aws-sdk-go-v2/credentials" + "crypto/sha256" + "encoding/hex" {{/withAWSV4Signature}} ) @@ -458,13 +460,10 @@ func (c *APIClient) prepareRequest( {{#withAWSV4Signature}} // AWS Signature v4 Authentication if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok { - creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, auth.SessionToken) - signer := awsv4.NewSigner(creds) - var reader *strings.Reader - if body == nil { - reader = strings.NewReader("") - } else { - reader = strings.NewReader(body.String()) + credsProvider := awscredentials.NewStaticCredentialsProvider(auth.AccessKey, auth.SecretKey, auth.SessionToken) + creds, err := credsProvider.Retrieve(ctx) + if err != nil { + return nil, err } // Define default values for region and service to maintain backward compatibility @@ -477,8 +476,22 @@ func (c *APIClient) prepareRequest( service = "oapi" } - timestamp := time.Now() - _, err := signer.Sign(localVarRequest, reader, service, region, timestamp) + // Compute payload hash from the request body + var payloadHash string + if body == nil { + // Empty body + hash := sha256.Sum256([]byte("")) + payloadHash = hex.EncodeToString(hash[:]) + } else { + // Hash the actual body content + bodyBytes := []byte(body.String()) + hash := sha256.Sum256(bodyBytes) + payloadHash = hex.EncodeToString(hash[:]) + } + + // Sign the request with the computed payload hash + signer := awsv4.NewSigner() + err = signer.SignHTTP(ctx, creds, localVarRequest, payloadHash, service, region, time.Now()) if err != nil { return nil, err } diff --git a/modules/openapi-generator/src/main/resources/go/go.mod.mustache b/modules/openapi-generator/src/main/resources/go/go.mod.mustache index c094d63543c3..30ac351f4231 100644 --- a/modules/openapi-generator/src/main/resources/go/go.mod.mustache +++ b/modules/openapi-generator/src/main/resources/go/go.mod.mustache @@ -8,7 +8,7 @@ require ( golang.org/x/oauth2 v0.27.0 {{/hasOAuthMethods}} {{#withAWSV4Signature}} - github.com/aws/aws-sdk-go v1.34.14 + github.com/aws/aws-sdk-go-v2 v1.37.0 {{/withAWSV4Signature}} {{#importValidator}} gopkg.in/validator.v2 v2.0.1 From 50cc0e3470bd51d4e6f27c19333227a99a5a0018 Mon Sep 17 00:00:00 2001 From: dennismouwen-eye <107839749+dennismouwen-eye@users.noreply.github.com> Date: Tue, 29 Jul 2025 11:26:48 +0200 Subject: [PATCH 2/2] Update indenting --- modules/openapi-generator/src/main/resources/go/client.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/go/client.mustache b/modules/openapi-generator/src/main/resources/go/client.mustache index 307aa0acc250..dea298410dc3 100644 --- a/modules/openapi-generator/src/main/resources/go/client.mustache +++ b/modules/openapi-generator/src/main/resources/go/client.mustache @@ -29,7 +29,7 @@ import ( {{#withAWSV4Signature}} awsv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" awscredentials "github.com/aws/aws-sdk-go-v2/credentials" - "crypto/sha256" + "crypto/sha256" "encoding/hex" {{/withAWSV4Signature}} )