Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation uses spaces instead of tabs, which is inconsistent with the surrounding Go code that uses tabs. Go convention and gofmt typically use tabs for indentation.

Suggested change
"crypto/sha256"
"crypto/sha256"

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new imports 'crypto/sha256' and 'encoding/hex' are only used when withAWSV4Signature is enabled, but they are included unconditionally. Consider moving these imports inside the conditional block or adding appropriate mustache conditionals to only include them when needed.

Suggested change
"crypto/sha256"
"crypto/sha256"

Copilot uses AI. Check for mistakes.
"encoding/hex"
{{/withAWSV4Signature}}
)

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading