From 8c628e23bd666344c5331a2b29b9ba134ba8f777 Mon Sep 17 00:00:00 2001 From: Shahan Nedadahandeh Date: Tue, 14 Jun 2022 01:50:50 -0400 Subject: [PATCH] Added auth --- .env-example | 2 ++ .gitignore | 1 + csccp.js | 26 +++++++++++++++++++++++--- package-lock.json | 5 +++++ package.json | 1 + run.sh | 0 stop.sh | 1 + 7 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 .env-example mode change 100644 => 100755 run.sh create mode 100755 stop.sh diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..1374b22 --- /dev/null +++ b/.env-example @@ -0,0 +1,2 @@ +#rename this file to .env +authToken=EXAMPLE_TOKEN diff --git a/.gitignore b/.gitignore index c2658d7..713d500 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.env diff --git a/csccp.js b/csccp.js index 28b6285..417b44d 100644 --- a/csccp.js +++ b/csccp.js @@ -3,20 +3,40 @@ const express = require('express') const app = express() const port = 2344 +require('dotenv').config(); +if(!process.env.authToken){ + console.error("No Auth token set!! – fill out .env-example and rename to .env!"); +} app.get('/cscclassprofilestaging', async (req, res) => { + console.log("Recieved build request", req); + const buildId = req.query.ref; - const name = `${buildId}-cscclassprofilestaging` + const name = `${buildId}-csc-class-profile-staging` const publicUrl = `${name}-snedadah.k8s.csclub.cloud` const image = `registry.cloud.csclub.uwaterloo.ca/snedadah/csc-class-profile-staging:${buildId}` - console.log("branch is " + req.query.ref, req.query); + + + if (!req.headers.authorization) { + console.log("No auth!"); + return res.status(403).json({ error: 'No credentials sent!' }); + } + + if(req.headers.authorization != process.env.authToken){ + console.log("Invalid auth!"); + return res.status(401).json({error: 'Invalid Auth Token!'}); + } + + + + console.log("Doing deploy for for branch: " + req.query.ref); await call(`kubectl delete deployment ${name}`); await call(`kubectl create deployment ${name} --image ${image} --port=80`); await call(`kubectl expose deployment ${name}`); await call(`kubectl create ingress ${name} --rule='${publicUrl}/*=${name}:80'`); - console.log("Deploying on " + publicUrl); + console.log("Deployed to " + publicUrl); res.send(publicUrl); }) diff --git a/package-lock.json b/package-lock.json index 3dc1479..de39db9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -620,6 +620,11 @@ "is-obj": "^1.0.0" } }, + "dotenv": { + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz", + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==" + }, "duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", diff --git a/package.json b/package.json index 8cd2249..954623d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "await-exec": "^0.1.2", + "dotenv": "^16.0.1", "express": "^4.18.1", "forever": "^4.0.3", "forver": "0.0.6" diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..542faf3 --- /dev/null +++ b/stop.sh @@ -0,0 +1 @@ +npx forever stop csccp.js