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

feat: apply $AWS_LAMBDA_EXEC_WRAPPER if set #524

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
46 changes: 46 additions & 0 deletions lambda/wrapper.go
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() {
RomainMuller marked this conversation as resolved.
Show resolved Hide resolved
// 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)
}
}
58 changes: 58 additions & 0 deletions lambda/wrapper_test.go
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
}