Skip to content

Commit db3f767

Browse files
committed
chore: Pull upstream changes 2023-11
1 parent 15263ba commit db3f767

File tree

99 files changed

+6728
-2717
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+6728
-2717
lines changed

cmd/aws-lambda-rie/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime/debug"
1212

1313
"github.com/jessevdk/go-flags"
14+
"go.amzn.com/lambda/interop"
1415
"go.amzn.com/lambda/rapidcore"
1516

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

106-
func getBootstrap(args []string, opts options) (*rapidcore.Bootstrap, string) {
107+
func getBootstrap(args []string, opts options) (interop.Bootstrap, string) {
107108
var bootstrapLookupCmd []string
108109
var handler string
109110
currentWorkingDir := "/var/task" // default value
@@ -149,5 +150,5 @@ func getBootstrap(args []string, opts options) (*rapidcore.Bootstrap, string) {
149150
log.Panic("insufficient arguments: bootstrap not provided")
150151
}
151152

152-
return rapidcore.NewBootstrapSingleCmd(bootstrapLookupCmd, currentWorkingDir, ""), handler
153+
return NewSimpleBootstrap(bootstrapLookupCmd, currentWorkingDir), handler
153154
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
11+
"go.amzn.com/lambda/fatalerror"
12+
"go.amzn.com/lambda/interop"
13+
"go.amzn.com/lambda/rapidcore/env"
14+
)
15+
16+
// the type implement a simpler version of the Bootstrap
17+
// this is useful in the Standalone Core implementation.
18+
type simpleBootstrap struct {
19+
cmd []string
20+
workingDir string
21+
}
22+
23+
func NewSimpleBootstrap(cmd []string, currentWorkingDir string) interop.Bootstrap {
24+
if currentWorkingDir == "" {
25+
// use the root directory as the default working directory
26+
currentWorkingDir = "/"
27+
}
28+
29+
// a single candidate command makes it automatically valid
30+
return &simpleBootstrap{
31+
cmd: cmd,
32+
workingDir: currentWorkingDir,
33+
}
34+
}
35+
36+
func (b *simpleBootstrap) Cmd() ([]string, error) {
37+
return b.cmd, nil
38+
}
39+
40+
// Cwd returns the working directory of the bootstrap process
41+
// The path is validated against the chroot identified by `root`
42+
func (b *simpleBootstrap) Cwd() (string, error) {
43+
if !filepath.IsAbs(b.workingDir) {
44+
return "", fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", b.workingDir)
45+
}
46+
47+
// evaluate the path relatively to the domain's mnt namespace root
48+
if _, err := os.Stat(b.workingDir); os.IsNotExist(err) {
49+
return "", fmt.Errorf("the working directory doesn't exist: %s", b.workingDir)
50+
}
51+
52+
return b.workingDir, nil
53+
}
54+
55+
// Env returns the environment variables available to
56+
// the bootstrap process
57+
func (b *simpleBootstrap) Env(e *env.Environment) map[string]string {
58+
return e.RuntimeExecEnv()
59+
}
60+
61+
// ExtraFiles returns the extra file descriptors apart from 1 & 2 to be passed to runtime
62+
func (b *simpleBootstrap) ExtraFiles() []*os.File {
63+
return make([]*os.File, 0)
64+
}
65+
66+
func (b *simpleBootstrap) CachedFatalError(err error) (fatalerror.ErrorType, string, bool) {
67+
// not implemented as it is not needed in Core but we need to fullfil the interface anyway
68+
return fatalerror.ErrorType(""), "", false
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"os"
8+
"reflect"
9+
"testing"
10+
11+
"go.amzn.com/lambda/rapidcore/env"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestSimpleBootstrap(t *testing.T) {
17+
tmpFile, err := os.CreateTemp("", "oci-test-bootstrap")
18+
assert.NoError(t, err)
19+
defer os.Remove(tmpFile.Name())
20+
21+
// Setup single cmd candidate
22+
file := []string{tmpFile.Name(), "--arg1 s", "foo"}
23+
cmdCandidate := file
24+
25+
// Setup working dir
26+
cwd, err := os.Getwd()
27+
assert.NoError(t, err)
28+
29+
// Setup environment
30+
environment := env.NewEnvironment()
31+
environment.StoreRuntimeAPIEnvironmentVariable("host:port")
32+
environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "")
33+
34+
// Test
35+
b := NewSimpleBootstrap(cmdCandidate, cwd)
36+
bCwd, err := b.Cwd()
37+
assert.NoError(t, err)
38+
assert.Equal(t, cwd, bCwd)
39+
assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment)))
40+
41+
cmd, err := b.Cmd()
42+
assert.NoError(t, err)
43+
assert.Equal(t, file, cmd)
44+
}
45+
46+
func TestSimpleBootstrapCmdNonExistingCandidate(t *testing.T) {
47+
// Setup inexistent single cmd candidate
48+
file := []string{"/foo/bar", "--arg1 s", "foo"}
49+
cmdCandidate := file
50+
51+
// Setup working dir
52+
cwd, err := os.Getwd()
53+
assert.NoError(t, err)
54+
55+
// Setup environment
56+
environment := env.NewEnvironment()
57+
environment.StoreRuntimeAPIEnvironmentVariable("host:port")
58+
environment.StoreEnvironmentVariablesFromInit(map[string]string{}, "", "", "", "", "", "")
59+
60+
// Test
61+
b := NewSimpleBootstrap(cmdCandidate, cwd)
62+
bCwd, err := b.Cwd()
63+
assert.NoError(t, err)
64+
assert.Equal(t, cwd, bCwd)
65+
assert.True(t, reflect.DeepEqual(environment.RuntimeExecEnv(), b.Env(environment)))
66+
67+
// No validations run against single candidates
68+
cmd, err := b.Cmd()
69+
assert.NoError(t, err)
70+
assert.Equal(t, file, cmd)
71+
}
72+
73+
func TestSimpleBootstrapCmdDefaultWorkingDir(t *testing.T) {
74+
b := NewSimpleBootstrap([]string{}, "")
75+
bCwd, err := b.Cwd()
76+
assert.NoError(t, err)
77+
assert.Equal(t, "/", bCwd)
78+
}

lambda/agents/agent.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ func ListExternalAgentPaths(dir string, root string) []string {
2020
}
2121
fullDir := path.Join(root, dir)
2222
files, err := os.ReadDir(fullDir)
23+
2324
if err != nil {
24-
log.WithError(err).Warning("Cannot list external agents")
25+
if os.IsNotExist(err) {
26+
log.Infof("The extension's directory %q does not exist, assuming no extensions to be loaded.", fullDir)
27+
} else {
28+
// TODO - Should this return an error rather than ignore failing to load?
29+
log.WithError(err).Error("Cannot list external agents")
30+
}
31+
2532
return agentPaths
2633
}
34+
2735
for _, file := range files {
2836
if !file.IsDir() {
2937
// The returned path is absolute wrt to `root`. This allows

lambda/appctx/appctx.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ type Key int
1313
type InitType int
1414

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

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

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

26+
// AppCtxResponseSenderKey is used to store a reference to the response sender
27+
AppCtxResponseSenderKey
28+
2629
// AppCtxFirstFatalErrorKey is used to store first unrecoverable error message encountered to propagate it to slicer with DONE(errortype) or DONEFAIL(errortype)
2730
AppCtxFirstFatalErrorKey
2831

lambda/appctx/appctxutil.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ func UpdateAppCtxWithRuntimeRelease(request *http.Request, appCtx ApplicationCon
119119
return false
120120
}
121121

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

127-
// LoadErrorResponse retrieves response from the application context.
128-
func LoadErrorResponse(appCtx ApplicationContext) *interop.ErrorResponse {
129-
v, ok := appCtx.Load(AppCtxInvokeErrorResponseKey)
127+
// LoadInvokeErrorTraceData retrieves invocation error x-ray cause header from the application context.
128+
func LoadInvokeErrorTraceData(appCtx ApplicationContext) *interop.InvokeErrorTraceData {
129+
v, ok := appCtx.Load(AppCtxInvokeErrorTraceDataKey)
130130
if ok {
131-
return v.(*interop.ErrorResponse)
131+
return v.(*interop.InvokeErrorTraceData)
132132
}
133133
return nil
134134
}
@@ -147,6 +147,20 @@ func LoadInteropServer(appCtx ApplicationContext) interop.Server {
147147
return nil
148148
}
149149

150+
// StoreResponseSender stores a reference to the response sender
151+
func StoreResponseSender(appCtx ApplicationContext, server interop.InvokeResponseSender) {
152+
appCtx.Store(AppCtxResponseSenderKey, server)
153+
}
154+
155+
// LoadResponseSender retrieves the response sender
156+
func LoadResponseSender(appCtx ApplicationContext) interop.InvokeResponseSender {
157+
v, ok := appCtx.Load(AppCtxResponseSenderKey)
158+
if ok {
159+
return v.(interop.InvokeResponseSender)
160+
}
161+
return nil
162+
}
163+
150164
// StoreFirstFatalError stores unrecoverable error code in appctx once. This error is considered to be the rootcause of failure
151165
func StoreFirstFatalError(appCtx ApplicationContext, err fatalerror.ErrorType) {
152166
if existing := appCtx.StoreIfNotExists(AppCtxFirstFatalErrorKey, err); existing != nil {

0 commit comments

Comments
 (0)