Skip to content

Commit b94e2b4

Browse files
authored
Merge pull request #298 from HRanjan-11/CYP-884
replaced request library with axios library to remove vulnerabilities
2 parents 32f97c4 + e992eb8 commit b94e2b4

File tree

11 files changed

+553
-980
lines changed

11 files changed

+553
-980
lines changed

commands/build_info.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45

@@ -40,6 +41,7 @@ function get_build_info(args) {
4041
}
4142
}
4243
let options = {
44+
method: 'get',
4345
url: constants[env].BUILD_BASE_URL + args.buildId,
4446
auth: {
4547
username: username,
@@ -55,25 +57,38 @@ function get_build_info(args) {
5557
return;
5658
} else {
5759
if (args["reject_unauthorized"] == "false") {
58-
options["rejectUnauthorized"] = false;
60+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
5961
console.log("Setting rejectUnauthorized to false for web requests");
6062
}
6163
}
6264
}
6365

64-
request.get(options, (err, res, body) => {
65-
if (err) {
66-
reject(err);
66+
axios(options)
67+
.then(response => {
68+
if (response.data.status == "success") {
69+
resolve(response.data.data);
6770
} else {
68-
if (res.statusCode == "401") {
71+
resolve(response.data.message);
72+
}
73+
})
74+
.catch(error => {
75+
if (error.response) {
76+
// The request was made and the server responded with a status code
77+
// that falls out of the range of 2xx
78+
if (error.response.status == 401) {
6979
resolve("Unauthorized");
70-
} else if (JSON.parse(body).status == "success") {
71-
resolve(JSON.parse(body).data);
7280
} else {
73-
resolve(JSON.parse(body).message);
81+
console.log(error.response.data);
7482
}
83+
} else if (error.request) {
84+
// The request was made but no response was received
85+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
86+
// http.ClientRequest in node.js
87+
console.log(error.cause);
88+
} else {
89+
reject(error);
7590
}
76-
});
91+
})
7792
});
7893
}
7994

commands/build_stop.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const fs = require("fs");
@@ -66,6 +67,7 @@ function stop_session(args) {
6667
}
6768

6869
let options = {
70+
method: 'put',
6971
url: constants[env].BUILD_STOP_URL + args.session_id,
7072
headers: {
7173
Authorization: "Token " + access_key,
@@ -82,39 +84,38 @@ function stop_session(args) {
8284
return;
8385
} else {
8486
if (args["reject_unauthorized"] == "false") {
85-
options["rejectUnauthorized"] = false;
87+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
8688
console.log("Setting rejectUnauthorized to false for web requests");
8789
}
8890
}
8991
}
90-
request.put(options, function (err, resp, body) {
91-
if (err) {
92-
reject(err);
92+
93+
axios(options)
94+
.then(response => {
95+
if(response.data.length == 0){
96+
resolve("No tests to stop in session " + args.session_id);
9397
} else {
94-
try {
95-
responseData = JSON.parse(body);
96-
} catch (e) {
97-
console.log("Error in JSON response", body);
98-
responseData = null;
99-
}
100-
if (resp.statusCode != 200) {
101-
if (responseData && responseData["error"]) {
102-
reject(responseData["error"]);
103-
} else {
104-
console.log(responseData);
105-
reject("error", responseData);
106-
}
107-
} else {
108-
if (responseData.length == 0) {
109-
resolve("No tests to stop in session " + args.session_id);
110-
}
111-
resolve(
112-
"Session Stopped successfully, No. of tests stopped are: " +
113-
responseData.length
114-
);
98+
resolve(
99+
"Session Stopped successfully, No. of tests stopped are: " +
100+
response.data.length
101+
);
102+
}
103+
})
104+
.catch(error => {
105+
if (error.response != null) {
106+
if (error.response.status != 200) {
107+
reject(error.response.data)
115108
}
109+
} else if (error.request) {
110+
// The request was made but no response was received
111+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
112+
reject(error.cause);
113+
} else {
114+
reject(error);
116115
}
117-
});
116+
})
117+
118+
118119
});
119120
}
120121

commands/generate_reports.js

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const build_stats = require("./utils/poller/build_stats.js");
@@ -27,33 +28,34 @@ function download_artefact(
2728
const stream = fs.createWriteStream(file_path);
2829
stream.end();
2930
let options = {
31+
method: 'get',
3032
url: constants[env].REPORT_URL + test_id,
3133
auth: {
3234
username: username,
3335
password: access_key,
3436
},
3537
gzip: true,
3638
timeout: 120000,
39+
responseType: 'stream'
3740
};
3841
if (rejectUnauthorized == false) {
39-
options["rejectUnauthorized"] = false;
42+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
4043
console.log("Setting rejectUnauthorized to false for web requests");
4144
}
4245

43-
request(options, (err, res, body) => {
44-
if (err) {
45-
reject(err);
46-
}
47-
response_code = res.statusCode;
48-
resp = res
49-
}).pipe(
50-
fs
51-
.createWriteStream(file_path, {
46+
axios(options)
47+
.then((response) => {
48+
response_code = response.status;
49+
resp = response;
50+
response.data.pipe(
51+
fs.createWriteStream(file_path, {
5252
overwrite: true,
5353
})
54-
.on("finish", function () {
55-
if (response_code == 200) {
56-
const zip = new StreamZip({ file: file_path });
54+
);
55+
56+
response.data.on('end', function () {
57+
if (response_code == 200) {
58+
const zip = new StreamZip({ file: file_path });
5759
zip.on("ready", () => {
5860
zip.extract(null, old_path, (err, count) => {
5961
zip.close();
@@ -64,18 +66,37 @@ function download_artefact(
6466
: `Extracted ${count} entries for ` + test_id
6567
);
6668
});
67-
});
68-
} else {
69-
fs.unlinkSync(file_path);
70-
if (resp.body != null) {
71-
const responseObject = JSON.parse(resp.body);
72-
const dataValue = responseObject.data;
73-
reject("Could not download artefacts for test id " + test_id + " with reason " + dataValue);
74-
}
75-
reject("Could not download artefacts for test id " + test_id);
69+
})
70+
}
71+
});
72+
73+
})
74+
.catch((error) => {
75+
76+
if (error.response) {
77+
resp = error.response
78+
// The request was made and the server responded with a status code
79+
// that falls out of the range of 2xx
80+
if (error.response.status == 401) {
81+
resolve("Unauthorized");
82+
} else {
83+
fs.unlinkSync(file_path);
84+
if (resp.data != null) {
85+
const responseObject = resp.data;
86+
const dataValue = responseObject.data;
87+
reject("Could not download artefacts for test id " + test_id + " with reason " + dataValue);
7688
}
77-
})
78-
);
89+
reject("Could not download artefacts for test id " + test_id);
90+
}
91+
} else if (error.request) {
92+
console.log(error.cause);
93+
} else {
94+
reject(error);
95+
}
96+
97+
});
98+
99+
79100
});
80101
}
81102

@@ -233,6 +254,15 @@ function generate_report(args) {
233254
});
234255
}
235256

257+
function generate_report_command(args) {
258+
generate_report(args)
259+
.then(function (resp) {})
260+
.catch(function (err) {
261+
console.log("ERR:", err);
262+
});
263+
};
264+
236265
module.exports = {
237-
generate_report:generate_report
266+
generate_report:generate_report,
267+
generate_report_command:generate_report_command
238268
};

commands/utils/batch/batch_runner.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const process = require("process");
77
const archive = require("../archive.js");
88
const WebSocket = require("ws");
99
const { type } = require("os");
10-
const request = require("request");
11-
const { del } = require("request");
1210
const { delete_archive } = require("../archive.js");
1311
const poller = require("../poller/poller.js");
1412
const builds = require("../poller/build");
1513
const batcher = require("./batcher.js");
1614
const reports = require("../../../commands/generate_reports.js");
1715
const { fail } = require("yargs");
16+
const https = require('https');
17+
const axios = require('axios');
1818

1919
var batchCounter = 0;
2020
var totalBatches = 0;
@@ -23,30 +23,18 @@ function run_test(payload, env = "prod", rejectUnauthorized) {
2323
return new Promise(function (resolve, reject) {
2424
let options = {
2525
url: constants[env].INTEGRATION_BASE_URL + constants.RUN_URL,
26-
body: payload,
26+
data: payload,
2727
};
2828
if (rejectUnauthorized == false) {
29-
options["rejectUnauthorized"] = false;
29+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
3030
}
3131
let responseData = null;
32-
request.post(options, function (err, resp, body) {
33-
if (err) {
34-
reject(err);
35-
} else {
36-
try {
37-
responseData = JSON.parse(body);
38-
} catch (e) {
39-
console.log("Error in JSON response", body);
40-
responseData = null;
41-
}
42-
if (resp.statusCode != 200) {
43-
if (responseData && responseData["error"]) {
44-
reject(responseData["error"]);
45-
} else {
46-
reject(responseData);
47-
}
48-
} else {
49-
build_id = responseData["value"]["message"]
32+
33+
axios.post(options.url, options.data, options)
34+
.then(response => {
35+
responseData = response.data;
36+
// console.log(response);
37+
build_id = responseData["value"]["message"]
5038
.split("=")
5139
[responseData["value"]["message"].split("=").length - 1].split(
5240
"&"
@@ -79,9 +67,30 @@ function run_test(payload, env = "prod", rejectUnauthorized) {
7967
}
8068
resolve(session_id);
8169
}
70+
})
71+
.catch(error => {
72+
if (error.response) {
73+
// The request was made and the server responded with a status code
74+
// that falls out of the range of 2xx
75+
if (error.response.status != 200) {
76+
if (error.response && error.response.data) {
77+
reject(error.response.data);
78+
} else {
79+
reject(error.response);
80+
}
81+
} else {
82+
reject(error.response);
8283
}
84+
} else if (error.request) {
85+
// The request was made but no response was received
86+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
87+
// http.ClientRequest in node.js
88+
reject(error.cause);
89+
} else {
90+
reject(error);
8391
}
84-
});
92+
})
93+
8594
});
8695
}
8796

0 commit comments

Comments
 (0)