-
Notifications
You must be signed in to change notification settings - Fork 567
feat: apply $AWS_LAMBDA_EXEC_WRAPPER
if set
#524
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
Open
RomainMuller
wants to merge
7
commits into
aws:main
Choose a base branch
from
RomainMuller:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+134
−0
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1766dc2
feat: apply `$AWS_LAMBDA_EXEC_WRAPPER` if set
RomainMuller 460eea2
lint fix
RomainMuller 31a466b
add legacy build tagging
RomainMuller 15f432b
switch from init to option enablement
RomainMuller 0aeedb0
Merge branch 'main' into main
RomainMuller 5953b5c
remove build tag (no longer needed)
RomainMuller d4c6b0d
remove generics from test to make it compatible with go < 1.18
RomainMuller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
// Specify the noexecwrapper build tag to remove the wrapper tampoline from | ||
// this library if it is undesirable. | ||
//go:build unix && !noexecwrapper | ||
// +build unix,!noexecwrapper | ||
|
||
package lambda | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
const awsLambdaExecWrapper = "AWS_LAMBDA_EXEC_WRAPPER" | ||
|
||
func init() { | ||
// Honor the AWS_LAMBDA_EXEC_WRAPPER configuration at startup, trying to emulate | ||
// the behavior of managed runtimes, as this configuration is otherwise not applied | ||
// by provided runtimes (or go1.x). | ||
execAWSLambdaExecWrapper(os.Getenv, syscall.Exec) | ||
} | ||
|
||
// If AWS_LAMBDA_EXEC_WRAPPER is defined, replace the current process by spawning | ||
// it with the current process' arguments (including the program name). If the call | ||
// to syscall.Exec fails, this aborts the process with a fatal error. | ||
func execAWSLambdaExecWrapper( | ||
getenv func(key string) string, | ||
sysExec func(argv0 string, argv []string, envv []string) error, | ||
) { | ||
wrapper := getenv(awsLambdaExecWrapper) | ||
if wrapper == "" { | ||
return | ||
} | ||
|
||
// The AWS_LAMBDA_EXEC_WRAPPER variable is blanked before replacing the process | ||
// in order to avoid endlessly restarting the process. | ||
env := append(os.Environ(), awsLambdaExecWrapper+"=") | ||
if err := sysExec(wrapper, append([]string{wrapper}, os.Args...), env); err != nil { | ||
log.Fatalf("failed to sysExec() %s=%s: %v", awsLambdaExecWrapper, wrapper, err) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2016-present Datadog, Inc. | ||
|
||
//go:build unix && !noexecwrapper | ||
// +build unix,!noexecwrapper | ||
|
||
package lambda | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExecAwsLambdaExecWrapperNotSet(t *testing.T) { | ||
exec, execCalled := mockExec(t, "<nope>") | ||
execAWSLambdaExecWrapper( | ||
mockedGetenv(t, ""), | ||
exec, | ||
) | ||
require.False(t, *execCalled) | ||
} | ||
|
||
func TestExecAwsLambdaExecWrapperSet(t *testing.T) { | ||
wrapper := "/path/to/wrapper/entry/point" | ||
exec, execCalled := mockExec(t, wrapper) | ||
execAWSLambdaExecWrapper( | ||
mockedGetenv(t, wrapper), | ||
exec, | ||
) | ||
require.True(t, *execCalled) | ||
} | ||
|
||
func mockExec(t *testing.T, value string) (mock func(string, []string, []string) error, called *bool) { | ||
mock = func(argv0 string, argv []string, envv []string) error { | ||
*called = true | ||
require.Equal(t, value, argv0) | ||
require.Equal(t, append([]string{value}, os.Args...), argv) | ||
require.Equal(t, awsLambdaExecWrapper+"=", envv[len(envv)-1]) | ||
return nil | ||
} | ||
called = ptrTo(false) | ||
return | ||
} | ||
|
||
func mockedGetenv(t *testing.T, value string) func(string) string { | ||
return func(key string) string { | ||
require.Equal(t, awsLambdaExecWrapper, key) | ||
return value | ||
} | ||
} | ||
|
||
func ptrTo[T any](val T) *T { | ||
return &val | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.