Skip to content

Commit

Permalink
chore: Pull upstream changes 2023-11
Browse files Browse the repository at this point in the history
  • Loading branch information
valerena committed Nov 30, 2023
1 parent 4ef1229 commit d9bbbf1
Show file tree
Hide file tree
Showing 99 changed files with 6,728 additions and 2,717 deletions.
5 changes: 3 additions & 2 deletions cmd/aws-lambda-rie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime/debug"

"github.com/jessevdk/go-flags"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -103,7 +104,7 @@ func isBootstrapFileExist(filePath string) bool {
return !os.IsNotExist(err) && !file.IsDir()
}

func getBootstrap(args []string, opts options) (*rapidcore.Bootstrap, string) {
func getBootstrap(args []string, opts options) (interop.Bootstrap, string) {
var bootstrapLookupCmd []string
var handler string
currentWorkingDir := "/var/task" // default value
Expand Down Expand Up @@ -149,5 +150,5 @@ func getBootstrap(args []string, opts options) (*rapidcore.Bootstrap, string) {
log.Panic("insufficient arguments: bootstrap not provided")
}

return rapidcore.NewBootstrapSingleCmd(bootstrapLookupCmd, currentWorkingDir, ""), handler
return NewSimpleBootstrap(bootstrapLookupCmd, currentWorkingDir), handler
}
69 changes: 69 additions & 0 deletions cmd/aws-lambda-rie/simple_bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"fmt"
"os"
"path/filepath"

"go.amzn.com/lambda/fatalerror"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore/env"
)

// the type implement a simpler version of the Bootstrap
// this is useful in the Standalone Core implementation.
type simpleBootstrap struct {
cmd []string
workingDir string
}

func NewSimpleBootstrap(cmd []string, currentWorkingDir string) interop.Bootstrap {
if currentWorkingDir == "" {
// use the root directory as the default working directory
currentWorkingDir = "/"
}

// a single candidate command makes it automatically valid
return &simpleBootstrap{
cmd: cmd,
workingDir: currentWorkingDir,
}
}

func (b *simpleBootstrap) Cmd() ([]string, error) {
return b.cmd, nil
}

// Cwd returns the working directory of the bootstrap process
// The path is validated against the chroot identified by `root`
func (b *simpleBootstrap) Cwd() (string, error) {
if !filepath.IsAbs(b.workingDir) {
return "", fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", b.workingDir)
}

// evaluate the path relatively to the domain's mnt namespace root
if _, err := os.Stat(b.workingDir); os.IsNotExist(err) {
return "", fmt.Errorf("the working directory doesn't exist: %s", b.workingDir)
}

return b.workingDir, nil
}

// Env returns the environment variables available to
// the bootstrap process
func (b *simpleBootstrap) Env(e *env.Environment) map[string]string {
return e.RuntimeExecEnv()
}

// ExtraFiles returns the extra file descriptors apart from 1 & 2 to be passed to runtime
func (b *simpleBootstrap) ExtraFiles() []*os.File {
return make([]*os.File, 0)
}

func (b *simpleBootstrap) CachedFatalError(err error) (fatalerror.ErrorType, string, bool) {
// not implemented as it is not needed in Core but we need to fullfil the interface anyway
return fatalerror.ErrorType(""), "", false
}
78 changes: 78 additions & 0 deletions cmd/aws-lambda-rie/simple_bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
"os"
"reflect"
"testing"

"go.amzn.com/lambda/rapidcore/env"

"github.com/stretchr/testify/assert"
)

func TestSimpleBootstrap(t *testing.T) {
tmpFile, err := os.CreateTemp("", "oci-test-bootstrap")
assert.NoError(t, err)
defer os.Remove(tmpFile.Name())

// Setup single cmd candidate
file := []string{tmpFile.Name(), "--arg1 s", "foo"}
cmdCandidate := file

// Setup working dir
cwd, err := os.Getwd()
assert.NoError(t, err)

// Setup environment
environment := env.NewEnvironment()
environment.StoreRuntimeAPIEnvironmentVariable("host:port")
environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "")

// Test
b := NewSimpleBootstrap(cmdCandidate, cwd)
bCwd, err := b.Cwd()
assert.NoError(t, err)
assert.Equal(t, cwd, bCwd)
assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment)))

cmd, err := b.Cmd()
assert.NoError(t, err)
assert.Equal(t, file, cmd)
}

func TestSimpleBootstrapCmdNonExistingCandidate(t *testing.T) {
// Setup inexistent single cmd candidate
file := []string{"/foo/bar", "--arg1 s", "foo"}
cmdCandidate := file

// Setup working dir
cwd, err := os.Getwd()
assert.NoError(t, err)

// Setup environment
environment := env.NewEnvironment()
environment.StoreRuntimeAPIEnvironmentVariable("host:port")
environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "")

// Test
b := NewSimpleBootstrap(cmdCandidate, cwd)
bCwd, err := b.Cwd()
assert.NoError(t, err)
assert.Equal(t, cwd, bCwd)
assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment)))

// No validations run against single candidates
cmd, err := b.Cmd()
assert.NoError(t, err)
assert.Equal(t, file, cmd)
}

func TestSimpleBootstrapCmdDefaultWorkingDir(t *testing.T) {
b := NewSimpleBootstrap([]string{}, "")
bCwd, err := b.Cwd()
assert.NoError(t, err)
assert.Equal(t, "/", bCwd)
}
10 changes: 9 additions & 1 deletion lambda/agents/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@ func ListExternalAgentPaths(dir string, root string) []string {
}
fullDir := path.Join(root, dir)
files, err := os.ReadDir(fullDir)

if err != nil {
log.WithError(err).Warning("Cannot list external agents")
if os.IsNotExist(err) {
log.Infof("The extension's directory %q does not exist, assuming no extensions to be loaded.", fullDir)
} else {
// TODO - Should this return an error rather than ignore failing to load?
log.WithError(err).Error("Cannot list external agents")
}

return agentPaths
}

for _, file := range files {
if !file.IsDir() {
// The returned path is absolute wrt to `root`. This allows
Expand Down
7 changes: 5 additions & 2 deletions lambda/appctx/appctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ type Key int
type InitType int

const (
// AppCtxInvokeErrorResponseKey is used for storing deferred invoke error response.
// AppCtxInvokeErrorTraceDataKey is used for storing deferred invoke error cause header value.
// Only used by xray. TODO refactor xray interface so it doesn't use appctx
AppCtxInvokeErrorResponseKey Key = iota
AppCtxInvokeErrorTraceDataKey Key = iota

// AppCtxRuntimeReleaseKey is used for storing runtime release information (parsed from User_Agent Http header string).
AppCtxRuntimeReleaseKey

// AppCtxInteropServerKey is used to store a reference to the interop server.
AppCtxInteropServerKey

// AppCtxResponseSenderKey is used to store a reference to the response sender
AppCtxResponseSenderKey

// AppCtxFirstFatalErrorKey is used to store first unrecoverable error message encountered to propagate it to slicer with DONE(errortype) or DONEFAIL(errortype)
AppCtxFirstFatalErrorKey

Expand Down
28 changes: 21 additions & 7 deletions lambda/appctx/appctxutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ func UpdateAppCtxWithRuntimeRelease(request *http.Request, appCtx ApplicationCon
return false
}

// StoreErrorResponse stores response in the applicaton context.
func StoreErrorResponse(appCtx ApplicationContext, errorResponse *interop.ErrorResponse) {
appCtx.Store(AppCtxInvokeErrorResponseKey, errorResponse)
// StoreInvokeErrorTraceData stores invocation error x-ray cause header in the applicaton context.
func StoreInvokeErrorTraceData(appCtx ApplicationContext, invokeError *interop.InvokeErrorTraceData) {
appCtx.Store(AppCtxInvokeErrorTraceDataKey, invokeError)
}

// LoadErrorResponse retrieves response from the application context.
func LoadErrorResponse(appCtx ApplicationContext) *interop.ErrorResponse {
v, ok := appCtx.Load(AppCtxInvokeErrorResponseKey)
// LoadInvokeErrorTraceData retrieves invocation error x-ray cause header from the application context.
func LoadInvokeErrorTraceData(appCtx ApplicationContext) *interop.InvokeErrorTraceData {
v, ok := appCtx.Load(AppCtxInvokeErrorTraceDataKey)
if ok {
return v.(*interop.ErrorResponse)
return v.(*interop.InvokeErrorTraceData)
}
return nil
}
Expand All @@ -147,6 +147,20 @@ func LoadInteropServer(appCtx ApplicationContext) interop.Server {
return nil
}

// StoreResponseSender stores a reference to the response sender
func StoreResponseSender(appCtx ApplicationContext, server interop.InvokeResponseSender) {
appCtx.Store(AppCtxResponseSenderKey, server)
}

// LoadResponseSender retrieves the response sender
func LoadResponseSender(appCtx ApplicationContext) interop.InvokeResponseSender {
v, ok := appCtx.Load(AppCtxResponseSenderKey)
if ok {
return v.(interop.InvokeResponseSender)
}
return nil
}

// StoreFirstFatalError stores unrecoverable error code in appctx once. This error is considered to be the rootcause of failure
func StoreFirstFatalError(appCtx ApplicationContext, err fatalerror.ErrorType) {
if existing := appCtx.StoreIfNotExists(AppCtxFirstFatalErrorKey, err); existing != nil {
Expand Down
Loading

0 comments on commit d9bbbf1

Please sign in to comment.