dherault / serverless-offline

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

x-api-key no longer being provided #1749

Open woodl3y opened 7 months ago

woodl3y commented 7 months ago

Bug Report

Current Behavior

With the latest version v13.3.2 we're no longer being provided with an API key when running locally.

We see only the line Remember to use 'x-api-key' on the request headers. in the terminal, but no value for x-api-key is provided. Reverting back to v13.3.1 we do get the additional line Key with token: 'some-generated-token'

service: my-service
frameworkVersion: ">=3.0.0 <4.0.0"
provider:
  name: aws
  role: ${cf:my-service-resources-${opt:stage}.LambdaExecutionRole}
  runtime: nodejs18.x
  region: us-east-1
  versionFunctions: false
  apiGateway:
    apiKeys:
      - ${self:service}-${opt:stage}-internal

plugins:
  - serverless-offline
  - serverless-plugin-aws-alerts
  - serverless-domain-manager
  - serverless-sqs-plugin-alarms
  - serverless-logless-plugin
  - serverless-log-forwarding
  - serverless-plugin-cloudwatch-dashboard

functions:
  graphql:
    handler: src/graphql/index.handler
    memorySize: 512
    timeout: 20
    provisionedConcurrency: ${ssm:/${opt:stage}/my-service/config/PROVISIONED_CONCURRENCY}
    events:
      - http:
          path: graphql
          method: post
          cors: true
          private: true
      - http:
          path: graphql
          method: get
          cors: true
    alarms:
      - name: functionInvocations
        threshold: 3000
    tags:
      FunctionType: graphql_api

Expected behavior/code

When running locally using sls offline start -s some_stage we should be provided with a value for x-api-key. The following line should be printed to the terminal as before:

Key with token: 'some-token'

Environment

raikkon88 commented 7 months ago

I encountered the same problem, and it's just after we upgraded from 13.3.1 to 13.3.2. We also had to downgrade to the previous version, I'm using node 20.9.0.

nakshathru commented 6 months ago

Facing similar issue with node 18x

Deamon19 commented 4 months ago

Same, in 13.3.2 version after webpack building message Key with token: 'some generated api key hash' does not appear anymore

ddbhagat commented 4 months ago

This looks to be the BUG, I was checking their code, https://github.com/dherault/serverless-offline/blob/c85a19272c59ad9e7cf1aea74e3442c7607b533a/src/events/http/HttpServer.js#L469 I can see that there is a check for x-api-key but the master keySet doesnt have the required value. In my case when printed the whole key set, the only value that I see is [stage]-[lambda-name] . Eg. local-test-lambda-api

And now if I set the "x-api-key" to "local-test-lambda-api". THIS WORKS !!

Eventually the issue is with below piece of serverless.yml config

  apiGateway:
    apiKeys:
      - ${self:service}-${opt:stage}-internal

So If you declare any of apiKeys under api gateway, the automatically created x-api-key will not be shown at all. just remove this apiKeys section, start local and you can see that x-api-key is shown on startup.

I hope this may help someone as quickfix. Other alternative is to use --noAuth with sls command, this will bypass the auth check alltogether.