Skip to content

Commit

Permalink
Updates SDK middleware values scoped to the stack being invoked
Browse files Browse the repository at this point in the history
Updates the SDK's middleware metadata to be scoped to the individual
stack's execution. This ensures that operations invoked nested
within a stack will not be polluted with values from parent stack(s).

Fixes #914
  • Loading branch information
jasdel committed Jan 8, 2021
1 parent bcfdab3 commit b6b5057
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 53 deletions.
60 changes: 48 additions & 12 deletions aws/middleware/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,67 +50,103 @@ type (
)

// GetServiceID retrieves the service id from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetServiceID(ctx context.Context) (v string) {
v, _ = ctx.Value(serviceIDKey{}).(string)
v, _ = middleware.GetStackValue(ctx, serviceIDKey{}).(string)
return v
}

// GetSigningName retrieves the service signing name from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetSigningName(ctx context.Context) (v string) {
v, _ = ctx.Value(signingNameKey{}).(string)
v, _ = middleware.GetStackValue(ctx, signingNameKey{}).(string)
return v
}

// GetSigningRegion retrieves the region from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetSigningRegion(ctx context.Context) (v string) {
v, _ = ctx.Value(signingRegionKey{}).(string)
v, _ = middleware.GetStackValue(ctx, signingRegionKey{}).(string)
return v
}

// GetRegion retrieves the endpoint region from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetRegion(ctx context.Context) (v string) {
v, _ = ctx.Value(regionKey{}).(string)
v, _ = middleware.GetStackValue(ctx, regionKey{}).(string)
return v
}

// GetOperationName retrieves the service operation metadata from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetOperationName(ctx context.Context) (v string) {
v, _ = ctx.Value(operationNameKey{}).(string)
v, _ = middleware.GetStackValue(ctx, operationNameKey{}).(string)
return v
}

// GetPartitionID retrieves the endpoint partition id from the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetPartitionID(ctx context.Context) string {
v, _ := ctx.Value(partitionIDKey{}).(string)
v, _ := middleware.GetStackValue(ctx, partitionIDKey{}).(string)
return v
}

// SetSigningName set or modifies the signing name on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetSigningName(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, signingNameKey{}, value)
return middleware.WithStackValue(ctx, signingNameKey{}, value)
}

// SetSigningRegion sets or modifies the region on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetSigningRegion(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, signingRegionKey{}, value)
return middleware.WithStackValue(ctx, signingRegionKey{}, value)
}

// SetServiceID sets the service id on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetServiceID(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, serviceIDKey{}, value)
return middleware.WithStackValue(ctx, serviceIDKey{}, value)
}

// setRegion sets the endpoint region on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func setRegion(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, regionKey{}, value)
return middleware.WithStackValue(ctx, regionKey{}, value)
}

// setOperationName sets the service operation on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func setOperationName(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, operationNameKey{}, value)
return middleware.WithStackValue(ctx, operationNameKey{}, value)
}

// SetPartitionID sets the partition id of a resolved region on the context
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetPartitionID(ctx context.Context, value string) context.Context {
return context.WithValue(ctx, partitionIDKey{}, value)
return middleware.WithStackValue(ctx, partitionIDKey{}, value)
}
19 changes: 14 additions & 5 deletions aws/retry/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
awsmiddle "github.com/aws/aws-sdk-go-v2/aws/middleware"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
"github.com/aws/smithy-go/logging"
"github.com/aws/smithy-go/middleware"
smithymiddle "github.com/aws/smithy-go/middleware"
"github.com/aws/smithy-go/transport/http"
)
Expand All @@ -26,8 +27,6 @@ type retryMetadata struct {
AttemptClockSkew time.Duration
}

type retryMetadataKey struct{}

// Attempt is a Smithy FinalizeMiddleware that handles retry attempts using the provided
// Retryer implementation
type Attempt struct {
Expand Down Expand Up @@ -191,14 +190,24 @@ func (r MetricsHeader) HandleFinalize(ctx context.Context, in smithymiddle.Final
return next.HandleFinalize(ctx, in)
}

// getRetryMetadata retrieves retryMetadata from the context and a bool indicating if it was set
type retryMetadataKey struct{}

// getRetryMetadata retrieves retryMetadata from the context and a bool
// indicating if it was set.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func getRetryMetadata(ctx context.Context) (metadata retryMetadata, ok bool) {
metadata, ok = ctx.Value(retryMetadataKey{}).(retryMetadata)
metadata, ok = middleware.GetStackValue(ctx, retryMetadataKey{}).(retryMetadata)
return metadata, ok
}

// setRetryMetadata sets the retryMetadata on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func setRetryMetadata(ctx context.Context, metadata retryMetadata) context.Context {
return context.WithValue(ctx, retryMetadataKey{}, metadata)
return middleware.WithStackValue(ctx, retryMetadataKey{}, metadata)
}

// AddRetryMiddlewaresOptions is the set of options that can be passed to AddRetryMiddlewares for configuring retry
Expand Down
11 changes: 8 additions & 3 deletions aws/signer/v4/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,18 @@ func haveCredentialProvider(p aws.CredentialsProvider) bool {
type payloadHashKey struct{}

// GetPayloadHash retrieves the payload hash to use for signing
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetPayloadHash(ctx context.Context) (v string) {
v, _ = ctx.Value(payloadHashKey{}).(string)
v, _ = middleware.GetStackValue(ctx, payloadHashKey{}).(string)
return v
}

// SetPayloadHash sets the payload hash to be used for signing the request
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetPayloadHash(ctx context.Context, hash string) context.Context {
ctx = context.WithValue(ctx, payloadHashKey{}, hash)
return ctx
return middleware.WithStackValue(ctx, payloadHashKey{}, hash)
}
6 changes: 3 additions & 3 deletions aws/signer/v4/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestComputePayloadHashMiddleware(t *testing.T) {
c := &computePayloadSHA256{}

next := middleware.BuildHandlerFunc(func(ctx context.Context, in middleware.BuildInput) (out middleware.BuildOutput, metadata middleware.Metadata, err error) {
value, ok := ctx.Value(payloadHashKey{}).(string)
if !ok {
value := GetPayloadHash(ctx)
if len(value) == 0 {
t.Fatalf("expected payload hash value to be on context")
}
if e, a := tt.expectedHash, value; e != a {
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestSignHTTPRequestMiddleware(t *testing.T) {
ctx = middleware.SetLogger(ctx, logger)

if len(tt.hash) != 0 {
ctx = context.WithValue(ctx, payloadHashKey{}, tt.hash)
ctx = SetPayloadHash(ctx, tt.hash)
}

_, _, err := c.HandleFinalize(ctx, middleware.FinalizeInput{
Expand Down
2 changes: 1 addition & 1 deletion aws/signer/v4/presign_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestPresignHTTPRequestMiddleware(t *testing.T) {
ctx = middleware.SetLogger(ctx, logger)

if len(c.PayloadHash) != 0 {
ctx = context.WithValue(ctx, payloadHashKey{}, c.PayloadHash)
ctx = SetPayloadHash(ctx, c.PayloadHash)
}

result, _, err := m.HandleFinalize(ctx, middleware.FinalizeInput{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/aws/aws-sdk-go-v2

require (
github.com/aws/smithy-go v0.5.0
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97
github.com/google/go-cmp v0.5.4
github.com/jmespath/go-jmespath v0.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/smithy-go v0.5.0 h1:ArsdWUrb1n6/V/REXhuwq2TZv+kuqOBpMlGBd2EkDYM=
github.com/aws/smithy-go v0.5.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97 h1:CYur6TiTXgezWGjb91FvVZqbzMm9U7GyRX28Hx/MmsI=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0=
Expand Down
44 changes: 35 additions & 9 deletions service/internal/presigned-url/context.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
package presignedurl

import "context"
import (
"context"

// WithIsPresigning adds the isPresigning sentinal value to a context to signal
"github.com/aws/smithy-go/middleware"
)

// WithIsPresigning adds the isPresigning sentinel value to a context to signal
// that the middleware stack is using the presign flow.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func WithIsPresigning(ctx context.Context) context.Context {
return context.WithValue(ctx, isPresigning{}, true)
return middleware.WithStackValue(ctx, isPresigningKey{}, true)
}

// GetIsPresigning returns if the context contains the isPresigning sentinel
// value for presigning flows.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetIsPresigning(ctx context.Context) bool {
v := ctx.Value(isPresigning{})
if v == nil {
return false
}
v, _ := middleware.GetStackValue(ctx, isPresigningKey{}).(bool)
return v
}

type isPresigningKey struct{}

return v.(bool)
// AddAsIsPresigingMiddleware adds a middleware to the head of the stack that
// will update the stack's context to be flagged as being invoked for the
// purpose of presigning.
func AddAsIsPresigingMiddleware(stack *middleware.Stack) error {
return stack.Initialize.Add(asIsPresigningMiddleware{}, middleware.Before)
}

type isPresigning struct{}
type asIsPresigningMiddleware struct{}

func (asIsPresigningMiddleware) ID() string { return "AsIsPresigningMiddleware" }

func (asIsPresigningMiddleware) HandleInitialize(
ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler,
) (
out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
ctx = WithIsPresigning(ctx)
return next.HandleInitialize(ctx, in)
}
2 changes: 1 addition & 1 deletion service/internal/presigned-url/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/aws/aws-sdk-go-v2 v0.31.0
github.com/aws/smithy-go v0.5.0
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97
github.com/google/go-cmp v0.5.4
)

Expand Down
4 changes: 2 additions & 2 deletions service/internal/presigned-url/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/smithy-go v0.5.0 h1:ArsdWUrb1n6/V/REXhuwq2TZv+kuqOBpMlGBd2EkDYM=
github.com/aws/smithy-go v0.5.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97 h1:CYur6TiTXgezWGjb91FvVZqbzMm9U7GyRX28Hx/MmsI=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
18 changes: 12 additions & 6 deletions service/internal/s3shared/arn_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ func (m *ARNLookup) HandleInitialize(ctx context.Context, in middleware.Initiali

// arnResourceKey is the key set on context used to identify, retrive an ARN resource
// if present on the context.
type arnResourceKey struct {
}
type arnResourceKey struct{}

// SetARNResourceOnContext sets arn on context
// SetARNResourceOnContext sets the S3 ARN on the context.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func setARNResourceOnContext(ctx context.Context, value arn.ARN) context.Context {
return context.WithValue(ctx, arnResourceKey{}, value)
return middleware.WithStackValue(ctx, arnResourceKey{}, value)
}

// GetARNResourceFromContext returns an ARN from context and a bool indicating presence of ARN on ctx
// GetARNResourceFromContext returns an ARN from context and a bool indicating
// presence of ARN on ctx.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func GetARNResourceFromContext(ctx context.Context) (arn.ARN, bool) {
v, ok := ctx.Value(arnResourceKey{}).(arn.ARN)
v, ok := middleware.GetStackValue(ctx, arnResourceKey{}).(arn.ARN)
return v, ok
}
2 changes: 1 addition & 1 deletion service/internal/s3shared/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/aws/aws-sdk-go-v2 v0.31.0
github.com/aws/smithy-go v0.5.0
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97
)

replace github.com/aws/aws-sdk-go-v2 => ../../../
4 changes: 2 additions & 2 deletions service/internal/s3shared/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/aws/smithy-go v0.5.0 h1:ArsdWUrb1n6/V/REXhuwq2TZv+kuqOBpMlGBd2EkDYM=
github.com/aws/smithy-go v0.5.0/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97 h1:CYur6TiTXgezWGjb91FvVZqbzMm9U7GyRX28Hx/MmsI=
github.com/aws/smithy-go v0.5.1-0.20210106170010-b13567a94b97/go.mod h1:EzMw8dbp/YJL4A5/sbhGddag+NPT7q084agLbB9LgIw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down
14 changes: 11 additions & 3 deletions service/internal/s3shared/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ package s3shared

import (
"context"

"github.com/aws/smithy-go/middleware"
)

// clonedInputKey used to denote if request input was cloned.
type clonedInputKey struct{}

// SetClonedInputKey sets a key on context to denote input was cloned previously
// SetClonedInputKey sets a key on context to denote input was cloned previously.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func SetClonedInputKey(ctx context.Context, value bool) context.Context {
return context.WithValue(ctx, clonedInputKey{}, value)
return middleware.WithStackValue(ctx, clonedInputKey{}, value)
}

// IsClonedInput retrieves if context key for cloned input was set.
// If set, we can infer that the reuqest input was cloned previously.
//
// Scoped to stack values. Use github.com/aws/smithy-go/middleware#ClearStackValues
// to clear all stack values.
func IsClonedInput(ctx context.Context) bool {
v, _ := ctx.Value(clonedInputKey{}).(bool)
v, _ := middleware.GetStackValue(ctx, clonedInputKey{}).(bool)
return v
}
Loading

0 comments on commit b6b5057

Please sign in to comment.