-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.js
374 lines (336 loc) · 12.3 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
require("dotenv").config();
const exec = require("child_process").exec;
const cookieParser = require("cookie-parser");
const cors = require("cors");
const crypto = require("crypto");
const csrf = require('csurf');
const express = require("express");
const expressPino = require('express-pino-logger');
const rateLimit = require("express-rate-limit");
const expressRequestId = require('express-request-id');
const session = require("express-session");
const fs = require("fs");
const helmet = require('helmet');
const https = require("https");
const path = require("path");
const pino = require('pino');
const app = express();
const PARENT_FOLDER_FOR_LOCAL_REPO = process.env.BUILD_DIRECTORY_PATH || "Builds";
// These are really not errors but unsure why child process is considering these as errors. So ignoring those which are ideally not.
const IGNORABLE_ERRORS = [
"Java HotSpot(TM) 64-Bit Server VM warning",
"Cloning into",
`Already on`,
"Total",
"Switched to a new branch",
];
// add helmet middleware
app.use(helmet());
// initialize session
app.use(
session({
//secret to sign the session ID cookie
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
})
);
// add CSRF to requests.
app.use(csrf());
// apply rate limit to all requests
const limiter = rateLimit({
windowMs: (process.env.TIME_INTERVAL || 30) * 60 * 1000, // 30 minutes
max: process.env.MAX_NO_OF_REQUESTS || 10 // limit each IP to 5 requests per windowMs
});
app.use(limiter);
// CORS check
const corsOptions = {
origin: process.env.HOST,
};
app.use(cors(corsOptions));
// Access to static files
app.use(express.static(path.join(__dirname, "public")));
// Add logger framework
const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
const expressLogger = expressPino({ logger });
app.use(expressLogger);
// add Request ID
app.use(expressRequestId());
// add cookie-parser
app.use(cookieParser());
getIsIgnorableError = (errMsg) => {
// check if the error msg is part of ignorable list.
return IGNORABLE_ERRORS.some((ignorableErr) => errMsg.indexOf(ignorableErr) > -1);
}
getPrepareLocalRepoCommands = (tempDirName, branchName, req) => {
const cloneCommand = process.env.REPO_CLONE_COMMAND;
addLog(req, "info", `Temp Directory named ${tempDirName} will be created under ${PARENT_FOLDER_FOR_LOCAL_REPO}.`);
let prepareLocalRepoCommands = `cd ${PARENT_FOLDER_FOR_LOCAL_REPO} && mkdir ${tempDirName} && cd ${tempDirName} && `;
prepareLocalRepoCommands += `${!cloneCommand.startsWith("git clone ") ? `git clone ${cloneCommand}` : cloneCommand}`;
prepareLocalRepoCommands += ` && cd ${getRepoName()} && git checkout ${branchName} && git reset --hard origin/${branchName} && git pull`;
return prepareLocalRepoCommands;
}
getRepoName = () => {
return process.env.REPO_NAME;
}
getPathToLocalRepo = (tempDirName) => {
return path.join(PARENT_FOLDER_FOR_LOCAL_REPO, tempDirName, getRepoName());
}
getBuildCommands = (tempDirName) => {
return `cd ${getPathToLocalRepo(tempDirName)} ${process.env.LOAD_MAVEN ? `&& ${process.env.LOAD_MAVEN}` : ``
} && ${process.env.BUILD_COMMANDS}`;
}
getDockerCommands = (tempDirName, req) => {
const dockerImageName = `${process.env.DOCKER_IMAGE_NAME}${tempDirName}`;
req.session["dockerImageName"] = dockerImageName;
return `cd ${getPathToLocalRepo(
tempDirName
)} && docker build -t ${dockerImageName} -f ${
process.env.DOCKER_FILE_PATH ? process.env.DOCKER_FILE_PATH : "./DockerFile"
} ${
process.env.ACTUAL_PATH_TO_DOCKERIZE
? `${process.env.ACTUAL_PATH_TO_DOCKERIZE}`
: `./`
} && docker save -o ./${dockerImageName}.tar ${dockerImageName} ${
process.env.DELETE_DOCKER_IMAGE_POST_COMPLETE === "false"
? ``
: `&& docker image rm ${dockerImageName}`
}`;
}
getRandomNumber = (req) => {
const randomNumber = crypto.randomBytes(4).toString("hex");
addLog(req, "info", `Generated Random Number is : ${randomNumber}.`);
return randomNumber;
}
downloadArchive = (req, res, tempDirNameFromSession) => {
const tempDirName = tempDirNameFromSession ? req.session["tempDirName"] : req.params.tempDirectoryName;
const pathToLocalRepo = getPathToLocalRepo(tempDirName || "");
const fileToDownload = path.resolve(pathToLocalRepo, req.session["dockerImageName"] || "") + ".tar";
addLog(req, "debug", `Path to download file : ${fileToDownload}`);
if (fs.existsSync(fileToDownload)) {
addLog(req, "info", "File exists. Downloading it.");
res.download(fileToDownload);
}
else {
addLog(req, "error", "Either the file is unavailable to download or the request to download is invalid");
res.status(200).set({
"content-type": "application/json",
}).send({
status: "error",
reason: "Either the file is unavailable to download or the request to download is invalid"
});
}
}
writeResponse = (req, res, type, resToBeWritten, logMsg) => {
addLog(req, "info", logMsg);
if (res && type && resToBeWritten) {
const data = {
type: type,
result: resToBeWritten
};
res.write("data:" + `${JSON.stringify(data)}\n\n`);
}
}
addLog = (req, type, logMsg) => {
switch (type) {
case "info":
logger.info(`[${req.session["requestId"]}] ${logMsg}`);
break;
case "debug":
logger.debug(`[${req.session["requestId"]}] ${logMsg}`);
break;
case "error":
logger.error(`[${req.session["requestId"]}] ${logMsg}`);
break;
default:
logger.info(`[${req.session["requestId"]}] ${logMsg}`);
break;
}
}
isValidCSRF = (csrfFromCookie, req) => {
return csrfFromCookie === req.session["csrf"];
}
execChildProcess = (command, isLastCommand, req, res) => {
let closedDueToError = false;
let errorMsg = "";
return new Promise((resolve, reject) => {
addLog(req, "debug", `The command to be executed is "${command}"`);
var child = exec(command);
child.stdout.setEncoding("utf8");
/**
* On Receiving Valid Data
*/
child.stdout.on("data", (chunk) => {
chunk = { type: "data", result: chunk };
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
});
/**
* On Receiving Error Data
*/
child.stderr.on("data", (err) => {
addLog(req, "info", `Child process throw an error : ${err}. Checking if it can be ignored and proceeded.`);
const isIgnorableError = getIsIgnorableError(err);
isIgnorableError ? addLog(req, "info", "Ignorable. Proceeding further with the process.") : addLog(req, "error", "Not Ignorable!! Check if it can be added to ignorable list or a genuine error.");
if (!isIgnorableError) {
closedDueToError = true;
const data = {
type: "error",
result: err
};
res.write("data:" + `${JSON.stringify(data)}\n\n`);
}
});
/**
* On Closing the process
*/
child.on("close", (close) => {
if (isLastCommand && !closedDueToError) {
addLog(req, "info", "Last Command done.!!");
close = { type: "close", result: "SuccessFully Created a Docker archive !! " };
res.write("data:" + `${JSON.stringify(close)}\n\n`);
res.end();
} else if (closedDueToError) {
addLog(req, "error", `Ending Response due to the error!!`);
// response written by stderr block. Just closing the response.
res.end();
// to close the lifecycle of promise
reject(errorMsg);
}
resolve("Success");
});
});
}
app.get("/", (req, res) => {
const csrfToken = req.csrfToken();
res.cookie('X-CSRF-TOKEN', csrfToken);
req.session["csrf"] = csrfToken;
res.status(200).set({
"content-type": "application/json",
}).send({
status: "running",
pathToStartABuild: `/branch/<branchName>`,
configuredFor: process.env.REPO_NAME
});
});
app.get("/branch/:branchName", async (req, res) => {
const branchName = req.params.branchName;
const csrfToken = req.csrfToken();
req.session["branchName"] = branchName;
req.session["requestId"] = req.id;
res.cookie('X-CSRF-TOKEN', csrfToken);
req.session["csrf"] = csrfToken;
res.writeHead(200, { "Content-Type": "text/html" });
res.write(fs.readFileSync("./index.html"));
res.end();
});
app.get("/start-build", async (req, res) => {
const branchName = req.session["branchName"];
addLog(req, "info", `Branch to be processed : ${branchName}`);
const csrfInCookie = req.cookies["X-CSRF-TOKEN"];
addLog(req, "debug", `CSRF Token in Cookie : ${csrfInCookie}`);
if (!isValidCSRF(csrfInCookie, req)) {
addLog(req, "debug", `Invalid CSRF.`);
res.status(403).send({
status: "forbidden"
});
return;
}
res.status(200).set({
connection: "keep-alive",
"cache-control": "no-cache",
"content-type": "text/event-stream",
});
const tempDirName = getRandomNumber(req);
req.session["tempDirName"] = tempDirName;
let prepareLocalRepoProcess = undefined;
let buildCommandsProcess = undefined;
let dockerCommandsProcess = undefined;
if (!branchName) {
writeResponse(req, res, "error", "\n\n**********Branch Name Not Specified!! Will not Start the process.***********\n\n", "No Branch Name!!");
res.end(); execChildProcess
} else {
writeResponse(req, res, "eocLine", "\n\n**********Preparing the local repo!!***********\n\n", "Starting the process!!");
prepareLocalRepoProcess = await execChildProcess(getPrepareLocalRepoCommands(tempDirName, branchName, req), false, req, res).catch((err) => { addLog(req, "error, "`Unexpected Error : ${err}`); })
if (prepareLocalRepoProcess === "Success") {
writeResponse(req, res, "eocLine", "\n\n**********End of Preparing the local repo. Starting Build Commands!!***********\n\n", "Cloned and prepared the local repo!!");
buildCommandsProcess = await execChildProcess(getBuildCommands(tempDirName), false, req, res).catch((err) => { addLog(req, "error", `Unexpected Error : ${err}`); });
if (buildCommandsProcess === "Success") {
writeResponse(req, res, "eocLine", "\n\n**********End of Build Commands. Starting Docker Commands!!***********\n\n", "Ran Build commands!!");
dockerCommandsProcess = await execChildProcess(getDockerCommands(tempDirName, req), true, req, res).catch((err) => { addLog(req, "error", `Unexpected Error : ${err}`); });
}
if (dockerCommandsProcess === "Success") {
writeResponse(req, undefined, undefined, undefined, "Ran Docker commands!! This finishes the entire process!!");
}
}
}
});
app.get("/test/branch/:branchname", (req, res) => {
const branchName = req.params.branchname;
const csrfInCookie = req.cookies["X-CSRF-TOKEN"];
addLog(req, "debug", `CSRF Token in Cookie : ${csrfInCookie}`);
if (!isValidCSRF(csrfInCookie, req)) {
addLog(req, "debug", `Invalid CSRF.`);
res.status(403).send({
status: "forbidden"
});
return;
}
res.status(200).set({
connection: "keep-alive",
"cache-control": "no-cache",
//"content-type": "application/json",
"content-type": "text/event-stream",
});
let counter = 0;
//res.write("event: message\n");
res.write("data:" + `${counter} : ` + `${branchName}\n\n\n`);
setInterval(() => {
counter++;
if (counter <= 3) {
res.write("data:" + `${counter} : ` + `${branchName}\n\n\n`);
} else {
res.end();
}
}, 2000);
});
app.get("/download/:tempDirectoryName", (req, res) => {
const csrfInCookie = req.cookies["X-CSRF-TOKEN"];
addLog(req, "debug", `CSRF Token in Cookie : ${csrfInCookie}`);
if (!isValidCSRF(csrfInCookie, req)) {
addLog(req, "debug", `Invalid CSRF.`);
res.status(403).send({
status: "forbidden"
});
return;
}
downloadArchive(req, res, false);
});
app.get("/download", (req, res) => {
const csrfInCookie = req.cookies["X-CSRF-TOKEN"];
addLog(req, "debug", `CSRF Token in Cookie : ${csrfInCookie}`);
if (!isValidCSRF(csrfInCookie, req)) {
addLog(req, "debug", `Invalid CSRF.`);
res.status(403).send({
status: "forbidden"
});
return;
}
downloadArchive(req, res, true);
});
https.createServer(
{
pfx: fs.readFileSync(process.env.SERVER_CERT_PATH),
passphrase: process.env.PASSPHRASE,
},
app
)
.listen(process.env.HTTPS_PORT, () => logger.info(
`Secure Service started at: https://${process.env.HOST}:${process.env.HTTPS_PORT}`
));
if (process.env.HTTP_PORT) {
app.listen(process.env.HTTP_PORT, () => {
logger.info(
`Insecure Service started at: http://${process.env.HOST}:${process.env.HTTP_PORT}`
);
});
}