Skip to content

Commit

Permalink
feat: secrets util + route (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikosramek authored Feb 13, 2023
1 parent 77a78ed commit 02e61a0
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/express-backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ DB_USER=
DB_PASSWORD=
SENDGRID_API_KEY=
SENDGRID_VERIFIED_SENDER=
FRONT_END_URL=
FRONT_END_URL=
WEBHOOK_SECRET=
15 changes: 15 additions & 0 deletions packages/express-backend/ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
apps: [
{
name: "wgp-api",
script: "./index.js",
watch: true,
env: {
NODE_ENV: "dev",
},
env_production: {
NODE_ENV: "prod",
},
},
],
};
16 changes: 10 additions & 6 deletions packages/express-backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const https = require("https");

const IS_DEV = process.env.NODE_ENV === "dev";

const { DB_NAME, DB_USER, DB_PASSWORD, FRONT_END_URL, DEV_FRONT_END_URL } =
process.env;
const { DB_NAME, DB_USER, DB_PASSWORD, FRONT_END_URL } = process.env;

const dbConnection = `mongodb+srv://${DB_USER}:${DB_PASSWORD}@prod.8tbszom.mongodb.net/${DB_NAME}?retryWrites=true&w=majority`;

Expand All @@ -30,7 +29,15 @@ db.on("open", () => {

const app = express();

const whitelist = [FRONT_END_URL, DEV_FRONT_END_URL];
// https://api.wargameplanner.com:1337/api/v1/webhooks/release
// https://wgp-be.ngrok.io/api/v1/webhooks/release

app.use(bodyParser.json({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));

app.use("/api/v1/webhooks", require("./webhooks/routes"));

const whitelist = [FRONT_END_URL];
app.use(
cors(
IS_DEV
Expand All @@ -47,9 +54,6 @@ app.use(
)
);

app.use(bodyParser.json({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));

app.use("/api/v1/accounts", require("./accounts/routes"));
app.use("/api/v1/features", require("./features/routes/global"));
app.use(require("./sessions/middleware"));
Expand Down
3 changes: 2 additions & 1 deletion packages/express-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "mocha --recursive --exit",
"dev": "NODE_ENV=dev nodemon index.js",
"start": "NODE_ENV=prod node index.js"
"dev-pm2": "pm2 start ecosystem.config.js",
"start": "pm2 start ecosystem.config.js --env production"
},
"keywords": [],
"author": "",
Expand Down
24 changes: 24 additions & 0 deletions packages/express-backend/utils/secrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { WEBHOOK_SECRET } = process.env;

let crypto;
try {
crypto = require("node:crypto");
} catch (err) {
console.error("crypto support is disabled!");
}

const validateRequestSecret = (request) => {
const expectedSignature =
"sha1=" +
crypto
.createHmac("sha1", WEBHOOK_SECRET)
.update(JSON.stringify(request.body))
.digest("hex");
const signature = request.headers["x-hub-signature"];
console.log({ expectedSignature, signature });
if (signature !== expectedSignature) {
throw new Error("Invalid signature.");
}
};

module.exports = { validateRequestSecret };
32 changes: 32 additions & 0 deletions packages/express-backend/webhooks/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { execSync } = require("child_process");
const express = require("express");
const { validateRequestSecret } = require("../utils/secrets");

const router = express.Router();

const IS_DEV = process.env.NODE_ENV === "dev";

router.post("/release", async (req, res) => {
let sentResponse = false;
try {
console.log("VALIDATING HOOK SECRET");
validateRequestSecret(req);

console.log("PULLING FROM MAIN");
await execSync("git pull origin main");

console.log("SENDING SUCCESS");
res.sendStatus(204);
sentResponse = true;

console.log("RESTARTING PM2 SERVICE");
await execSync(`pm2 restart wgp-api ${IS_DEV ? "" : "--env production"}`);
} catch (err) {
console.error(err);
if (!sentResponse) {
return res.status(500).send(err.message);
}
}
});

module.exports = router;

1 comment on commit 02e61a0

@vercel
Copy link

@vercel vercel bot commented on 02e61a0 Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.