-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
110 lines (100 loc) · 3.3 KB
/
helpers.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
const { ConnectionString } = require("connection-string");
const childProcess = require("child_process");
const { promisify } = require("util");
const exec = promisify(childProcess.exec);
async function mongodumpInstalled() {
try {
await exec("which mongodump");
} catch (error) {
console.error("WARNING: mongodump is not installed on Kaholo agent! Attempting to install...");
await exec("apk add mongodb-tools");
console.error("Apparent success installing mongodump!\n");
}
}
async function runCommand(command, envVars) {
try {
const result = await exec(command, envVars);
// stdout is typically null
// stderr has the details about what mongodump has done
if (result.stdout) {
console.error(result.stdout);
}
if (result.stderr) {
return result.stderr;
}
} catch (error) {
throw new Error(error);
}
return "The command seems to have somehow produced neither results nor errors.";
}
async function consolidateUri(uri, username, password) {
const uriObject = new ConnectionString(uri);
if (!Reflect.has(uriObject, "protocol")) {
throw new Error("URI cannot be parsed. Should resemble pattern: mongodb://user:password@host:port/defaultauthdb");
}
if (uriObject.protocol !== "mongodb") {
throw new Error("Only a MongDB URI may be used with this plugin - mongodb://user:password@host:port/defaultauthdb");
}
if (username) {
uriObject.setDefaults({ user: username });
}
if (password) {
uriObject.setDefaults({ password });
}
return uriObject.toString();
}
async function parseConnectionStringToShellArguments(uri, username, password) {
const args = [];
const uriObject = new ConnectionString(uri);
if (!Reflect.has(uriObject, "protocol")) {
throw new Error("URI cannot be parsed. Should resemble pattern: mongodb://user:password@host:port/defaultauthdb");
}
if (uriObject.protocol !== "mongodb") {
throw new Error("Only a MongDB URI may be used with this plugin - mongodb://user:password@host:port/defaultauthdb");
}
if (username) {
args.push("--username", username);
} else if (Reflect.has(uriObject, "user")) {
args.push("--username", uriObject.user);
}
let envVars = {};
if (password) {
args.push("--password", "$TMPPASS60FA");
envVars = {
env: {
...process.env,
TMPPASS60FA: password,
},
};
} else if (Reflect.has(uriObject, "password")) {
args.push("--password", "$TMPPASS60FA");
envVars = {
env: {
...process.env,
TMPPASS60FA: uriObject.password,
},
};
}
if (Reflect.has(uriObject, "hostname")) {
args.push("--host", uriObject.hostname);
}
if (Reflect.has(uriObject, "port")) {
args.push("--port", uriObject.port);
}
if (Reflect.has(uriObject, "path")) {
if (uriObject.path.length === 1) {
args.push("--authenticationDatabase", uriObject.path[0]);
} else {
throw new Error("The path part of the URI is for the database that holds the user's credentials. To specify a MongoDB database or collection use parameters other than URI. - mongodb://user:password@host:port/defaultauthdb");
}
} else {
args.push("--authenticationDatabase", "admin");
}
return { args, envVars };
}
module.exports = {
mongodumpInstalled,
runCommand,
consolidateUri,
parseConnectionStringToShellArguments,
};