Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom authorizer handler cannot be resolved #802

Closed
afreix opened this issue Sep 5, 2019 · 2 comments

Comments

@afreix
Copy link

commented Sep 5, 2019

I'm using TypeScript and serverless-webpack, and it might be an issue related to serverless-webpack since I've seen a lot of similar issues (they are closed though and should be fixed).

I ran serverless offline in debug mode and traced the issue to this line: https://github.com/dherault/serverless-offline/blob/master/src/lambda/handler-runner/InProcessRunner.js#L22.

By the time my code hits this it has correctly pulled the handlerPath from serverless.yml, so when trying to run the custom authorizer it's looking for /path/to/directory/server/server. If I modify this path during execution to /path/to/directory/server/server.ts it can at least resolve the path, but it hits another error because of the es6 style imports in my server.ts. I imagine that it should really be trying to resolve the server.js that is built by webpack and put in /path/to/directory/.webpack/service/server/server.js

I'm new to serverless, but not to webpack or typescript and I've been googling and tampering with the configuration for the past 8 hours. I found some potentially related issues:
serverless-heaven/serverless-webpack#505
serverless-heaven/serverless-webpack#383
serverless-heaven/serverless-webpack#43
serverless-heaven/serverless-webpack#230

Happy to help provide any more information or assist with the debugging process!

Version:
6.x alpha release

serverless.yml

service: splor-serverless
app: splor-graphql-server
org: afreix
provider:
  name: aws
  runtime: nodejs10.x
  environment:
    AUTH0_CLIENT_ID: ${file(./secrets.json):AUTH0_CLIENT_ID}
    AUTH0_CLIENT_PUBLIC_KEY: ${file(./public_key)}

functions:
  auth:
    handler: server/server.auth
    cors: true
  graphql:
    handler: server/server.graphqlHandler
    events:
      - http:
          path: graphql
          method: post
          authorizer: auth
          cors: true
      - http:
          path: graphql
          method: get
          authorizer: auth
          cors: true

resources:
  Resources:
    # This response is needed for custom authorizer failures cors support ¯\_(ツ)_/¯
    GatewayResponse:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseType: EXPIRED_TOKEN
        RestApiId:
          Ref: "ApiGatewayRestApi"
        StatusCode: "401"
    AuthFailureGatewayResponse:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseType: UNAUTHORIZED
        RestApiId:
          Ref: "ApiGatewayRestApi"
        StatusCode: "401"

plugins:
  - serverless-webpack
  - serverless-offline
custom:
  serverless-offline:
    port: 4000
  webpack:
    webpackConfig: "webpack.config.js" # Name of webpack configuration file
    includeModules: true # Node modules configuration for packaging
    packager: "yarn" # Packager that will be used to package your external modules

webpack.config.js

const path = require("path");
const slsw = require("serverless-webpack");
const nodeExternals = require("webpack-node-externals");

module.exports = {
  entry: slsw.lib.entries,
  mode: "development",
  externals: [nodeExternals()],
  devtool: "source-map",
  output: {
    libraryTarget: "commonjs",
    path: path.join(__dirname, ".webpack"),
    filename: "[name].js"
  },
  resolve: {
    // Add '.ts' and '.tsx' as a resolvable extension.
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"]
  },
  module: {
    rules: [
      // All files with a '.ts' or '.tsx'
      // extension will be handled by 'ts-loader'
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: "ts-loader"
          }
        ]
      }
    ]
  },
  target: "node"
};

package.json

{
  "name": "apollo-sample-app",
  "version": "1.0.0",
  "description": "Sample app",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "build": "webpack --config webpack.config.js",
    "start": "yarn run build && node ./dist/server.js",
    "debug": "export SLS_DEBUG=* && node --inspect ./node_modules/serverless/bin/serverless offline -s dev"
  },
  "dependencies": {
    "apollo-server": "^2.1.0",
    "apollo-server-lambda": "^2.9.2",
    "dotenv": "^6.1.0",
    "graphql": "^14.0.2",
    "graphql-tools": "^4.0.0",
    "jsonwebtoken": "^8.5.1",
    "jwks-rsa": "^1.6.0",
    "nodemon": "^1.18.4",
    "source-map-support": "^0.5.13",
    "ts-loader": "^5.2.1",
    "ts-node": "^7.0.1",
    "tslint": "^5.11.0",
    "typescript": "^3.1.1",
    "webpack": "~4.39.1"
  },
  "devDependencies": {
    "@types/aws-lambda": "^8.10.31",
    "@types/dotenv": "^4.0.3",
    "@types/express": "^4.16.0",
    "@types/graphql": "^14.0.3",
    "@types/jsonwebtoken": "^8.3.3",
    "fork-ts-checker-webpack-plugin": "^1.5.0",
    "serverless": "^1.51.0",
    "serverless-offline": "^6.0.0-alpha.31",
    "serverless-plugin-typescript": "^1.1.9",
    "serverless-webpack": "^5.3.1",
    "webpack-cli": "^3.1.2",
    "webpack-node-externals": "^1.7.2"
  }
}

tsconfig.

{
  "files": ["server/server.ts"],
  "include": ["server/**/*.ts"],
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": true,
    "target": "es2017",
    "outDir": "dist",
    "rootDir": "server",
    "sourceMap": true,
    "baseUrl": ".",
    "moduleResolution": "node",
    "removeComments": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["es2017"],
    "typeRoots": ["node_modules/@types"],
    "paths": {
      "*": ["node_modules/@types/*", "./server/@types/*"]
    }
  }
}
@dnalborczyk

This comment has been minimized.

Copy link
Collaborator

commented Sep 5, 2019

hey @afreix

thank you for filing the issue. I kind of got your setup running, but with some modifications. the handler file is also missing. Could you create a small repository which I can check out?

@afreix

This comment has been minimized.

Copy link
Author

commented Sep 9, 2019

Hey @dnalborczyk I started with the aws-nodejs-typescript starter here: https://serverless.com/framework/docs/providers/aws/cli-reference/create/ and incrementally added things in to get close to my previous set up and everything is working. I realized it's probably not specific to servervless-offline and most likely serverless-webpack because the same issue occurs when I actually deploy with Serverless.

At some point I'll try to keep adding configurations to my setup until I identify what actually caused the issue. I'll post back here once I do, but it may be a while

@afreix afreix closed this Sep 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.