Added auth

This commit is contained in:
Shahan Nedadahandeh 2022-06-14 01:50:50 -04:00
parent 7871cc868b
commit 8c628e23bd
7 changed files with 33 additions and 3 deletions

2
.env-example Normal file
View File

@ -0,0 +1,2 @@
#rename this file to .env
authToken=EXAMPLE_TOKEN

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules/ node_modules/
.env

View File

@ -3,20 +3,40 @@ const express = require('express')
const app = express() const app = express()
const port = 2344 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) => { app.get('/cscclassprofilestaging', async (req, res) => {
console.log("Recieved build request", req);
const buildId = req.query.ref; const buildId = req.query.ref;
const name = `${buildId}-cscclassprofilestaging` const name = `${buildId}-csc-class-profile-staging`
const publicUrl = `${name}-snedadah.k8s.csclub.cloud` const publicUrl = `${name}-snedadah.k8s.csclub.cloud`
const image = `registry.cloud.csclub.uwaterloo.ca/snedadah/csc-class-profile-staging:${buildId}` 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 delete deployment ${name}`);
await call(`kubectl create deployment ${name} --image ${image} --port=80`); await call(`kubectl create deployment ${name} --image ${image} --port=80`);
await call(`kubectl expose deployment ${name}`); await call(`kubectl expose deployment ${name}`);
await call(`kubectl create ingress ${name} --rule='${publicUrl}/*=${name}:80'`); await call(`kubectl create ingress ${name} --rule='${publicUrl}/*=${name}:80'`);
console.log("Deploying on " + publicUrl); console.log("Deployed to " + publicUrl);
res.send(publicUrl); res.send(publicUrl);
}) })

5
package-lock.json generated
View File

@ -620,6 +620,11 @@
"is-obj": "^1.0.0" "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": { "duplexer": {
"version": "0.1.2", "version": "0.1.2",
"resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz",

View File

@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"await-exec": "^0.1.2", "await-exec": "^0.1.2",
"dotenv": "^16.0.1",
"express": "^4.18.1", "express": "^4.18.1",
"forever": "^4.0.3", "forever": "^4.0.3",
"forver": "0.0.6" "forver": "0.0.6"

0
run.sh Normal file → Executable file
View File

1
stop.sh Executable file
View File

@ -0,0 +1 @@
npx forever stop csccp.js