-
Notifications
You must be signed in to change notification settings - Fork 675
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
Add Recursion Detection middleware to all SDK requests #2105
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good overall!
"id": "d74f8a81-3ddb-431f-b600-6abefbdaba1b", | ||
"type": "feature", | ||
"description": "add recursion detection middleware to all SDK requests to avoid recursion invocation in Lambda", | ||
"modules": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: I think we want this changelog entry to be a "rollup" to prevent duplicated changelogs for every service affected which can cause issues with release.
aws/middleware/middleware.go
Outdated
@@ -166,3 +167,84 @@ func AddRawResponseToMetadata(stack *middleware.Stack) error { | |||
func GetRawResponse(metadata middleware.Metadata) interface{} { | |||
return metadata.Get(rawResponseKey{}) | |||
} | |||
|
|||
// AddRecursionDetection adds recursionDetection to the middleware stack | |||
func AddRecursionDetection(stack *middleware.Stack) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: I can see we put several middleware here but let's move this to it's own file (e.g. recursion_detection.go
) with it's own tests.
aws/middleware/middleware_test.go
Outdated
t.Run(name, func(t *testing.T) { | ||
// has to pre-check if current os has lambda function and trace ID environment variable | ||
// if exists, need to restore them after each test case | ||
initialLambdaFunc, hasInitialLambdaFunc := os.LookupEnv(lambdaFunc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that the testing utils that already exist for this depend on aws
package.
I think we can simplify this by just clearing the environment and restoring it after the test such that each test gets a clean environment, example
Then you won't need the additional setEnvVar
/recordEnvVar
functions and we guarantee tests are isolated
aws/middleware/middleware_test.go
Outdated
@@ -187,3 +188,98 @@ func TestAttemptClockSkewHandler(t *testing.T) { | |||
}) | |||
} | |||
} | |||
|
|||
func TestRecursionDetection(t *testing.T) { | |||
const lambdaFunc = "AWS_LAMBDA_FUNCTION_NAME" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: You could define these as internal constants in the middleware and the test can just re-use them
e.g.
// file: recursion_detection.go
const envAwsLambdaFunctionName = "AWS_LAMBDA_FUNCTION_NAME"
const envAmznTraceId = "_X_AMZN_TRACE_ID"
const amznTraceIdHeader = "X-Amzn-Trace-Id"
Add Recursion Detection middleware to all SDK requests, which detects Lambda environment and sets its X-Ray trace ID to request header if absent to avoid recursion invocation in Lambda environment. Solves #2025