Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve v4 Signer.SignHTTP & Signer.PresignHTTP performance #2956

Closed
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
9 changes: 9 additions & 0 deletions .changelog/7bacfa97-ac95-4390-8c46-9d0528bda2c1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": "7bacfa97-ac95-4390-8c46-9d0528bda2c1",
"type": "feature",
"description": "Improve v4 Signer.SignHTTP & Signer.PresignHTTP performance",
"collapse": false,
"modules": [
"."
]
}
4 changes: 2 additions & 2 deletions aws/signer/internal/v4/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func lookupKey(service, region string) string {
var s strings.Builder
s.Grow(len(region) + len(service) + 3)
s.Grow(len(region) + 1 + len(service))
s.WriteString(region)
s.WriteRune('/')
s.WriteByte('/')
s.WriteString(service)
return s.String()
}
Expand Down
19 changes: 13 additions & 6 deletions aws/signer/internal/v4/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import "strings"

// BuildCredentialScope builds the Signature Version 4 (SigV4) signing scope
func BuildCredentialScope(signingTime SigningTime, region, service string) string {
return strings.Join([]string{
signingTime.ShortTimeFormat(),
region,
service,
"aws4_request",
}, "/")
const suffix = "aws4_request"
t := signingTime.ShortTimeFormat()

var sb strings.Builder
sb.Grow(len(t) + 1 + len(region) + 1 + len(service) + 1 + len(suffix))
Copy link
Contributor

Choose a reason for hiding this comment

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

In fact, is this actually an improvement? strings.Join uses strings.Builder internally.

Choose a reason for hiding this comment

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

It's a minor improvement of around -0.45% in the Benchmark.

I agree that it's a little less readable but I also expect this code to not change any time soon and in our solution we sign a lot for requests and every little improvement helps.

                  │ 1.sha-62c09690f2.txt │        2.sha-ccdf28e449.txt        │
                  │        sec/op        │   sec/op     vs base               │
PresignRequest-16            10.49µ ± 3%   10.45µ ± 3%       ~ (p=0.837 n=10)
SignRequest-16               5.564µ ± 2%   5.534µ ± 2%       ~ (p=0.210 n=10)
geomean                      7.641µ        7.606µ       -0.45%

However, I'm fine removing the related commit from the PR if it's a deal breaker.

Choose a reason for hiding this comment

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

FYI not using strings.Join has some precedence in lookupKey and buildAuthorizationHeader

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's older code and it's something we very much want to get away from. I made a point to do away with all of it in the rewrite in the aws-http-auth module.

sb.WriteString(t)
sb.WriteByte('/')
sb.WriteString(region)
sb.WriteByte('/')
sb.WriteString(service)
sb.WriteByte('/')
sb.WriteString(suffix)
return sb.String()
}
59 changes: 37 additions & 22 deletions aws/signer/v4/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
v4Internal "github.com/aws/aws-sdk-go-v2/aws/signer/internal/v4"
"github.com/aws/smithy-go/encoding/httpbinding"
"github.com/aws/smithy-go/logging"

"github.com/aws/aws-sdk-go-v2/aws"
v4Internal "github.com/aws/aws-sdk-go-v2/aws/signer/internal/v4"
)

const (
Expand Down Expand Up @@ -234,7 +235,7 @@ func buildAuthorizationHeader(credentialStr, signedHeadersStr, signingSignature
len(signature) + len(signingSignature),
)
parts.WriteString(signingAlgorithm)
parts.WriteRune(' ')
parts.WriteByte(' ')
parts.WriteString(credential)
parts.WriteString(credentialStr)
parts.WriteString(commaSpace)
Expand Down Expand Up @@ -374,7 +375,7 @@ func (s *Signer) PresignHTTP(

logSigningInfo(ctx, options, &signedRequest, true)

signedHeaders = make(http.Header)
signedHeaders = make(http.Header, len(signedRequest.SignedHeaders))

// For the signed headers we canonicalize the header keys in the returned map.
// This avoids situations where can standard library double headers like host header. For example the standard
Expand Down Expand Up @@ -459,46 +460,60 @@ func (s *httpSigner) buildCanonicalHeaders(host string, rule v4Internal.Rule, he
for i := 0; i < n; i++ {
if headers[i] == hostHeader {
canonicalHeaders.WriteString(hostHeader)
canonicalHeaders.WriteRune(colon)
canonicalHeaders.WriteByte(colon)
canonicalHeaders.WriteString(v4Internal.StripExcessSpaces(host))
} else {
canonicalHeaders.WriteString(headers[i])
canonicalHeaders.WriteRune(colon)
canonicalHeaders.WriteByte(colon)
// Trim out leading, trailing, and dedup inner spaces from signed header values.
values := signed[headers[i]]
for j, v := range values {
cleanedValue := strings.TrimSpace(v4Internal.StripExcessSpaces(v))
canonicalHeaders.WriteString(cleanedValue)
if j < len(values)-1 {
canonicalHeaders.WriteRune(',')
canonicalHeaders.WriteByte(',')
}
}
}
canonicalHeaders.WriteRune('\n')
canonicalHeaders.WriteByte('\n')
}
canonicalHeadersStr = canonicalHeaders.String()

return signed, signedHeaders, canonicalHeadersStr
}

func (s *httpSigner) buildCanonicalString(method, uri, query, signedHeaders, canonicalHeaders string) string {
return strings.Join([]string{
method,
uri,
query,
canonicalHeaders,
signedHeaders,
s.PayloadHash,
}, "\n")
var sb strings.Builder
sb.Grow(len(method) + 1 + len(uri) + 1 + len(query) + 1 + len(canonicalHeaders) + 1 + len(signedHeaders) + 1 + len(s.PayloadHash))
sb.WriteString(method)
sb.WriteByte('\n')
sb.WriteString(uri)
sb.WriteByte('\n')
sb.WriteString(query)
sb.WriteByte('\n')
sb.WriteString(canonicalHeaders)
sb.WriteByte('\n')
sb.WriteString(signedHeaders)
sb.WriteByte('\n')
sb.WriteString(s.PayloadHash)
return sb.String()
}

func (s *httpSigner) buildStringToSign(credentialScope, canonicalRequestString string) string {
return strings.Join([]string{
signingAlgorithm,
s.Time.TimeFormat(),
credentialScope,
hex.EncodeToString(makeHash(sha256.New(), []byte(canonicalRequestString))),
}, "\n")
h := sha256.Sum256([]byte(canonicalRequestString))
encodedHash := hex.EncodeToString(h[:])
t := s.Time.TimeFormat()

var sb strings.Builder
sb.Grow(len(signingAlgorithm) + 1 + len(t) + 1 + len(credentialScope) + 1 + len(encodedHash))
sb.WriteString(signingAlgorithm)
sb.WriteByte('\n')
sb.WriteString(t)
sb.WriteByte('\n')
sb.WriteString(credentialScope)
sb.WriteByte('\n')
sb.WriteString(encodedHash)
return sb.String()
}

func makeHash(hash hash.Hash, b []byte) []byte {
Expand Down
Loading