Skip to content

Commit 1fe9d1b

Browse files
authored
Add TenantID to LambdaContext (#604)
1 parent 7dfe2bb commit 1fe9d1b

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

lambda/invoke_loop.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func handleInvoke(invoke *invoke, handler *handlerOptions) error {
5353
lc := lambdacontext.LambdaContext{
5454
AwsRequestID: invoke.id,
5555
InvokedFunctionArn: invoke.headers.Get(headerInvokedFunctionARN),
56+
TenantID: invoke.headers.Get(headerTenantID),
5657
}
5758
if err := parseClientContext(invoke, &lc.ClientContext); err != nil {
5859
return reportFailure(invoke, lambdaErrorResponse(err))

lambda/invoke_loop_test.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,16 @@ func TestRuntimeAPIContextPlumbing(t *testing.T) {
212212
}, nil
213213
})
214214

215-
ts, record := runtimeAPIServer(``, 1)
215+
metadata2 := defaultInvokeMetadata()
216+
metadata2.tenantID = "some-tenant-id"
217+
ts, record := runtimeAPIServer(``, 2, defaultInvokeMetadata(), metadata2)
216218
defer ts.Close()
217219

218220
endpoint := strings.Split(ts.URL, "://")[1]
219221
expectedError := fmt.Sprintf("failed to GET http://%s/2018-06-01/runtime/invocation/next: got unexpected status code: 410", endpoint)
220222
assert.EqualError(t, startRuntimeAPILoop(endpoint, handler), expectedError)
221223

222-
expected := `
224+
expected1 := `
223225
{
224226
"Context": {
225227
"AwsRequestID": "dummyid",
@@ -244,7 +246,35 @@ func TestRuntimeAPIContextPlumbing(t *testing.T) {
244246
"Deadline": 22
245247
}
246248
`
247-
assert.JSONEq(t, expected, string(record.responses[0]))
249+
expected2 := `
250+
{
251+
"Context": {
252+
"AwsRequestID": "dummyid",
253+
"InvokedFunctionArn": "dummyarn",
254+
"TenantID": "some-tenant-id",
255+
"Identity": {
256+
"CognitoIdentityID": "dummyident",
257+
"CognitoIdentityPoolID": "dummypool"
258+
},
259+
"ClientContext": {
260+
"Client": {
261+
"installation_id": "dummyinstallid",
262+
"app_title": "dummytitle",
263+
"app_version_code": "dummycode",
264+
"app_package_name": "dummyname"
265+
},
266+
"env": null,
267+
"custom": null
268+
}
269+
},
270+
"TraceID": "its-xray-time",
271+
"EnvTraceID": "its-xray-time",
272+
"Deadline": 22
273+
}
274+
`
275+
276+
assert.JSONEq(t, expected1, string(record.responses[0]))
277+
assert.JSONEq(t, expected2, string(record.responses[1]))
248278
}
249279

250280
func TestReadPayload(t *testing.T) {
@@ -387,6 +417,7 @@ type eventMetadata struct {
387417
deadline string
388418
requestID string
389419
functionARN string
420+
tenantID string
390421
}
391422

392423
func defaultInvokeMetadata() eventMetadata {
@@ -440,6 +471,9 @@ func runtimeAPIServer(eventPayload string, failAfter int, overrides ...eventMeta
440471
w.Header().Add(string(headerClientContext), metadata.clientContext)
441472
w.Header().Add(string(headerCognitoIdentity), metadata.cognito)
442473
w.Header().Add(string(headerTraceID), metadata.xray)
474+
if metadata.tenantID != "" {
475+
w.Header().Add(string(headerTenantID), metadata.tenantID)
476+
}
443477
w.WriteHeader(http.StatusOK)
444478
_, _ = w.Write([]byte(eventPayload))
445479
case http.MethodPost:

lambda/runtime_api_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
headerCognitoIdentity = "Lambda-Runtime-Cognito-Identity"
2525
headerClientContext = "Lambda-Runtime-Client-Context"
2626
headerInvokedFunctionARN = "Lambda-Runtime-Invoked-Function-Arn"
27+
headerTenantID = "Lambda-Runtime-Aws-Tenant-Id"
2728
headerXRayErrorCause = "Lambda-Runtime-Function-Xray-Error-Cause"
2829
trailerLambdaErrorType = "Lambda-Runtime-Function-Error-Type"
2930
trailerLambdaErrorBody = "Lambda-Runtime-Function-Error-Body"

lambdacontext/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type LambdaContext struct {
7777
InvokedFunctionArn string //nolint: stylecheck
7878
Identity CognitoIdentity
7979
ClientContext ClientContext
80+
TenantID string `json:",omitempty"`
8081
}
8182

8283
// An unexported type to be used as the key for types in this package.

0 commit comments

Comments
 (0)