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

Add Recursion Detection middleware to all SDK requests #2105

Merged
merged 8 commits into from
Apr 24, 2023

Conversation

wty-Bryant
Copy link
Contributor

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

@wty-Bryant wty-Bryant requested a review from a team as a code owner April 21, 2023 04:20
Copy link
Contributor

@aajtodd aajtodd left a 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": [
Copy link
Contributor

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.

@@ -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 {
Copy link
Contributor

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.

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)
Copy link
Contributor

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

@@ -187,3 +188,98 @@ func TestAttemptClockSkewHandler(t *testing.T) {
})
}
}

func TestRecursionDetection(t *testing.T) {
const lambdaFunc = "AWS_LAMBDA_FUNCTION_NAME"
Copy link
Contributor

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"

@wty-Bryant wty-Bryant merged commit 44acf0c into main Apr 24, 2023
18 of 19 checks passed
@wty-Bryant wty-Bryant deleted the recursion-detection branch April 24, 2023 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants