dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.2k stars 794 forks source link

Mocking Remote Authorizers broken since v13.x #1826

Open tbehunin opened 1 month ago

tbehunin commented 1 month ago

Bug Report

Current Behavior

From (at least) version 9.x up through 12.x, we were able to mock authorizers, based on what the readme currently says:

Remote authorizers

You are able to mock the response from remote authorizers by setting the environmental variable AUTHORIZER before running sls offline start

Example:

Unix: export AUTHORIZER='{"principalId": "123"}'

Windows: SET AUTHORIZER='{"principalId": "123"}'

Ever since 13.x was released, this fails to work.

Sample Code

service: sls-playground

provider:
  name: aws
  runtime: nodejs16.x

custom:
  serverless-offline:
    httpPort: 4000
    websocketPort: 4001
    lambdaPort: 4002

functions:
  hello:
    handler: handler.hello
    events:
      - httpApi:
          path: /
          method: get

plugins:
  - serverless-offline
exports.hello = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: "Go Serverless v4! Your function executed successfully!",
      event
    }),
  };
};

Run the following from a terminal window:

export AUTHORIZER='{"principalId":"123"}' && sls offline start

Expected behavior/code

The event.requestContext object should have an authorizer prop with what was set on the terminal:

    "requestContext": {
      "accountId": "offlineContext_accountId",
      "apiId": "offlineContext_apiId",
      "authorizer": {
        "principalId": "123"
      },

Environment

Possible Solution

PR that introduced this feature: https://github.com/dherault/serverless-offline/pull/475

Additional context/Screenshots

cullylarson commented 3 weeks ago

It may have been broken by https://github.com/dherault/serverless-offline/pull/1786. That PR removes the authorizer prop if no authorizer is configured for the endpoint. Downgrading to serverless-offline: "13.5.0" resolved it for me, at least for now.