Error when logging in on Forest with admin backend deployed on AWS in lambda function

Since we made the upgrade to Liana 6, we face frequently following issue when logging into Forest with google SSO:
Oops can not reach the endpoint on your application.Check your Liana version.

Expected behavior

No error

Actual behavior

Error “Oops can not reach the endpoint on your application.Check your Liana version”

Failure Logs

On the backend we see also this log:
POST /forest/sessions-google 404

Context

Please provide any relevant information about your setup.

  • Package Version: 6.2.0
  • Express Version: 4.16.3
  • Sequelize Version: 5.15.1
  • Database Dialect: MySQL
  • Database Version: 5.6.10a
  • Project Name: Elyps

Important note: Forest backend is deploy on AWS in lambda function. We do not run the code in file server.js. Instead here is the code of our lambda function:

const awsServerlessExpress = require("aws-serverless-express");

const app = require("./app");

const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => {

awsServerlessExpress.proxy(server, event, context);

};

Hello @Cyril_Limam !
Can you reach your forest backend manually (just hit the ip address + port) ?
What logs do you have on your server ? (I never used lambda functions, so I might ask dumb questions :sweat_smile:)

Did you follow these steps while upgrading to v6 ?

Hi Nicolas, yes it does work. Here is the response I get from Postman:

<html>

<head>
	<style>
		body {
			font-family: Arial, Helvetica, sans-serif;
			margin: 60px;
			font-size: 16px;
			background-color: #fcfdfc;
			color: #060f19;
		}

		#logo {
			display: block;
			width: 5rem;
			margin: 2rem auto;

		}

		h1 {
			text-align: center;
			color: #3b2512;
			font-weight: 500;
		}

		h2 {
			color: #d68a41;
			font-weight: 400;
			text-align: center;
		}

		span {
			display: block;
			margin: 4rem auto;
			height: 2px;
			width: 2rem;
			background-color: #61cf96;
		}

		div {
			border-radius: .25rem;
			background: white;
			box-shadow: 0 0 1rem rgba(59, 37, 18, 0.16);
			width: 50%;
			margin: 0 auto;
			padding: 1rem;
		}

		li {
			margin: 1rem;
			font-size: 1.25rem;
			line-height: 1.5rem;
		}

		ul {
			list-style: none;
			margin-bottom: 2rem;
		}

		ul li::before {
			content: "\2022";
			font-size: 1rem;
			color: #61cf96;
			display: inline-block;
			width: 1rem;
			height: 1rem;
			margin-left: -1rem;
		}
	</style>
	<title>LumberJS</title>
</head>

<body>
	<img id="logo" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOYAAAEiCAYAAADtZK6tAAAZIUlEQVR42u3deXAb130HcNmO0+kfmbTTdpJJpn8l02md5m4yTZMmJkBZcW3Zkiw5juPEFXX4zOEkztEjUeK0sQlKsmWJIHVQlG8r9SFb9y1eokiRWIAkzgVAggcuSpRIiBRlW9t9Cyy0hAASABcPb3e/35md8R+yNLNvP3jv/d7btwsWIAiCIAiCIAiClC+CINwsXjfiTiAIQygvR/m3pqL+F4ETQZhCyQvJCzgRhAGU/jevoUxdcf4F4EQQllACJ4KUEWXM/0ZOlPIV43cBJ4LQQfmhvFCmcQYagRNBWEIJnAhCBeX/FYwydU3H/DuBE0EYQgmcCFIalH+aL0oFzgbgRBCGUAIngqiDcrfaKBU4d4j/xg240wjCCErgRJAiUE7F+NdLjVK+xB+A7cCJILOjvIkmSuBEkPxQvkYb5TWc/DbgRBCGUAIngmRF6X+13CjTOKP8VuBEgJIhlMCJACVBGfW/whpKBc564ESAEjgRpOwoX2Yd5TWc/jrgRIASOBGEOsqXtIZSgdMKnIjeUN6oZZTAiegV5YtaR5nGGffXAicClCzijPm3ACeiXZRx/gW9oVTiRCsjQMkmzs1obUQ7KGP8Lr2jBE4EKJnHyT+P1kcYRhloNBpK4ESAkn2cm/A0IMygJAcpGx0lcCJAyTzOwHN4OpByomwAROBE2EF5A1DmtZTyLJ4WhCbKHYCXN86NeGoQoAROxGgoycHIgFb0K2Mb8BQhJUDJbwMw4ESAUq841+OpQuaPMspvBSjgRIDSIDj5GjxlSDEo6wGo1MeU8BY8bUjeCb380DcT3pYg8JT+uhLv/xKeOCSvOGvMC101lSPjvpZ+4CkxzNHAV/HEIXnFa6msdFZXCMBJA6b/K3jiDJqVnPXOKs76unj1iP/tquJq967irFXrBOHG2WACZ/lg7hZ231Rlr1slttnOlVytTWy3ppU267Or7VYTnmiN5zHXrr8SG/W1lbZaIdslNnrLqr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Z+X8z786z8GfMXgAAAABJRU5ErkJggg==">
	<h1>Your application is running!</h1>
</body>

</html>

Here are the logs I got on the server side:

GET / 200 10026 - 7.470 ms

Note that before Liana upgrade we never had this issue and even after upgrade, Forest does work most of the time. The issue mentioned here only happen sometime.

Also, yes we did follow the steps mentioned in the upgrade procedure.

Do you have any logs on your lambda function when this happens ?

Yes,

POST /forest/sessions-google 404

Hello @Cyril_Limam :wave:

It looks like your server is running, but forest is not initialised. Can you check by attempting to reach /forest/healthcheck please ?

Keep me posted :raised_hands:

Steve.

Hi steve,

Maybe I do something wrong but I get this response:

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<title>Error</title>
</head>

<body>
	<pre>Cannot GET /forest/healthcheck</pre>
</body>

</html>

Hi @Cyril_Limam :wave:

This is correct :+1: here we clearly see that the forest routes are not initialised, although your server is running. Somehow the init is never called in your lambda.

If you have recently migrated to V6, I suspect this breaking change has not been taken into account :

Can you confirm ?

Steve.

Hi Steve,

No I do not confirm. We did implement the breaking changes you mentioned.

Best regards,
Cyril.

@Cyril_Limam

May I see the app.js file please ?

Steve.

Sure, here it is

const express = require("express");
const requireAll = require("require-all");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const cors = require("cors");
const jwt = require("express-jwt");
const morgan = require("morgan");
const {
  ensureAuthenticated,
  PUBLIC_ROUTES,
} = require("forest-express-sequelize");

const app = express();

app.use(morgan("tiny"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

let allowedOrigins = [/forestadmin\.com$/];

if (process.env.CORS_ORIGINS) {
  allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(","));
}

app.use(
  cors({
    origin: allowedOrigins,
    allowedHeaders: ["Authorization", "X-Requested-With", "Content-Type"],
    maxAge: 86400, // NOTICE: 1 day
    credentials: true,
  })
);

app.use(
  jwt({
    secret: process.env.FOREST_AUTH_SECRET,
    credentialsRequired: false,
  })
);

app.use("/forest", (request, response, next) => {
  if (PUBLIC_ROUTES.includes(request.url)) {
    return next();
  }
  return ensureAuthenticated(request, response, next);
});

requireAll({
  dirname: path.join(__dirname, "routes"),
  recursive: true,
  resolve: (Module) => app.use("/forest", Module),
});

requireAll({
  dirname: path.join(__dirname, "middlewares"),
  recursive: true,
  resolve: (Module) => Module(app),
});

module.exports = app;

I believe the implementation you propose can not guarantee that the app.use(await Liana…) is fully resolved before we start our server. We should await for app to be ready before doing http.createServer(app).

I have had similar use case in another project and had to do something like this:

async function startServer() {
    const app = await createApp(...)
    const server = http.createServer(app);
...
}

startServer()

Hi @Cyril_Limam

I believe the implementation you propose can not guarantee that the app.use(await Liana…) is fully resolved before we start our server. We should await for app to be ready before doing http.createServer(app).

Yes indeed, I suspect you have to do the same, and wait the liana for being initialised.

Can you try it please ?

keep me posted :+1:

Steve.

Hi @Steve_Bunlon,

I do confirm that it was the issue.
Here is our app.js:

const express = require("express");
const requireAll = require("require-all");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const cors = require("cors");
const jwt = require("express-jwt");
const morgan = require("morgan");
const {
  ensureAuthenticated,
  PUBLIC_ROUTES,
} = require("forest-express-sequelize");

async function createApp() {
  const app = express();

  app.use(morgan("tiny"));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, "public")));

  let allowedOrigins = [/forestadmin\.com$/];

  if (process.env.CORS_ORIGINS) {
    allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(","));
  }

  app.use(
    cors({
      origin: allowedOrigins,
      allowedHeaders: ["Authorization", "X-Requested-With", "Content-Type"],
      maxAge: 86400, // NOTICE: 1 day
      credentials: true,
    })
  );

  app.use(
    jwt({
      secret: process.env.FOREST_AUTH_SECRET,
      credentialsRequired: false,
    })
  );

  app.use("/forest", (request, response, next) => {
    if (PUBLIC_ROUTES.includes(request.url)) {
      return next();
    }
    return ensureAuthenticated(request, response, next);
  });

  requireAll({
    dirname: path.join(__dirname, "routes"),
    recursive: true,
    resolve: (Module) => app.use("/forest", Module),
  });

  const middlewares = requireAll({
    dirname: path.join(__dirname, "middlewares"),
    recursive: true,
    resolve: (Module) => Module(app),
  });

  await Promise.all(Object.keys(middlewares).map((key) => middlewares[key]));

  return app;
}

module.exports = createApp;

Here is our server.js:

require("dotenv").config();
const createApp = require("./app");
const debug = require("debug")("{name}:server");
const http = require("http");
const chalk = require("chalk");

function normalizePort(val) {
  const port = parseInt(val, 10);

  if (Number.isNaN(port)) {
    return val;
  }
  if (port >= 0) {
    return port;
  }

  return false;
}

async function createServer() {
  const port = normalizePort(
    process.env.PORT || process.env.APPLICATION_PORT || "3310"
  );

  const app = await createApp();
  app.set("port", port);

  const server = http.createServer(app);
  server.listen(port);

  function onError(error) {
    if (error.syscall !== "listen") {
      throw error;
    }

    const bind = typeof port === "string" ? `Pipe ${port}` : `Port ${port}`;

    switch (error.code) {
      case "EACCES":
        console.error(`${bind} requires elevated privileges`);
        process.exit(1);
        break;
      case "EADDRINUSE":
        console.error(`${bind} is already in use`);
        process.exit(1);
        break;
      default:
        throw error;
    }
  }

  function onListening() {
    const addr = server.address();
    const bind =
      typeof addr === "string" ? `pipe ${addr}` : `port ${addr.port}`;
    debug(`Listening on ${bind}`);

    console.log(chalk.cyan(`Your application is listening on ${bind}.`));
  }

  server.on("error", onError);
  server.on("listening", onListening);
}

createServer();

And as a bonus, here is our lambda.js (this is what is running on AWS lambda, we only use server,.js for local tests):

const awsServerlessExpress = require("aws-serverless-express");
const createApp = require("./app");

let app;
let server;

exports.handler = async (event, context) => {
  if (!app) {
    app = await createApp();
  }
  if (!server) {
    server = awsServerlessExpress.createServer(app);
  }
  return awsServerlessExpress.proxy(server, event, context, "PROMISE").promise;
};

1 Like

@Cyril_Limam glad it helped! And thanks for sharing this :green_heart:

Hello @Cyril_Limam :wave:

Woaw amazing! Thanks a lot for providing the entire code :muscle:

I’m glad we figured this out :raised_hands:

Steve.