@opscompass/opscompass-cli 1.0.36 → 1.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/environment/current.js +5 -5
- package/environment/demo.js +5 -4
- package/environment/dev.js +4 -3
- package/environment/local.js +5 -4
- package/environment/production.js +5 -5
- package/index.js +1 -0
- package/lib/consts.js +25 -0
- package/lib/maskValues.js +545 -0
- package/lib/utils.js +604 -0
- package/modules/data-gathering-scripts.js +0 -3
- package/modules/defaults.js +66 -20
- package/modules/license-manager.js +349 -240
- package/package.json +8 -5
package/lib/utils.js
ADDED
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Module to provide utility functions for the OpsCompass data gathering
|
|
5
|
+
*
|
|
6
|
+
* @module utils_js
|
|
7
|
+
* @exports utils
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
const crypto = require("crypto");
|
|
11
|
+
const fs = require("fs");
|
|
12
|
+
const path = require("path");
|
|
13
|
+
const { opscompassHome, decoder } = require("../environment/current");
|
|
14
|
+
const { resourceTypes, encryptAttrs } = require("./consts");
|
|
15
|
+
const findRemoveSync = require("find-remove");
|
|
16
|
+
|
|
17
|
+
const HOBKEY = Buffer.from(`${decoder}=`, "base64").toString();
|
|
18
|
+
const hashPrefix = "oc$hash:"; // Prefix for encrypted values
|
|
19
|
+
const hashRegex = /^oc\$hash:/; // Regex for encrypted values
|
|
20
|
+
|
|
21
|
+
// Test for any null or undefined objects
|
|
22
|
+
function hasNullOrUndefined(obj, count = Object.keys(obj).length) {
|
|
23
|
+
if (count !== Object.keys(obj).length) return false;
|
|
24
|
+
return Object.values(obj).some(
|
|
25
|
+
(value) => value === null || value === undefined
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Function to get a timestamp
|
|
30
|
+
|
|
31
|
+
// (E) MANUAL FORMAT timestamp
|
|
32
|
+
function getTimestamp() {
|
|
33
|
+
var dNow = new Date(),
|
|
34
|
+
tsYr = dNow.getFullYear(),
|
|
35
|
+
tsMo = prezero(dNow.getMonth() + 1),
|
|
36
|
+
tsDy = prezero(dNow.getDate()),
|
|
37
|
+
tsHr = prezero(dNow.getHours()),
|
|
38
|
+
tsMi = prezero(dNow.getMinutes()),
|
|
39
|
+
tsSe = prezero(dNow.getSeconds());
|
|
40
|
+
return `${tsYr}${tsMo}${tsDy}-${tsHr}${tsMi}${tsSe}`;
|
|
41
|
+
}
|
|
42
|
+
const tStamp = getTimestamp();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Process standard arguments (eg company) and remove them
|
|
46
|
+
* @param {string[]} args
|
|
47
|
+
*/
|
|
48
|
+
function processStandardArgs(args) {
|
|
49
|
+
// There should be global values from opscompassHome/defaults.json for these
|
|
50
|
+
// - They are picked up by index.js
|
|
51
|
+
// - Override them here from the command line
|
|
52
|
+
for (let i = args.length - 2; i >= 0; i--) {
|
|
53
|
+
switch (args[i].toLowerCase()) {
|
|
54
|
+
case "--company":
|
|
55
|
+
global.company = args[i + 1];
|
|
56
|
+
args.splice(i, 2);
|
|
57
|
+
break;
|
|
58
|
+
case "--account":
|
|
59
|
+
global.account = args[i + 1];
|
|
60
|
+
args.splice(i, 2);
|
|
61
|
+
break;
|
|
62
|
+
case "--monitoring-configuration-id":
|
|
63
|
+
case "--monitoringconfigurationid":
|
|
64
|
+
global.monitoringConfig = args[i + 1];
|
|
65
|
+
args.splice(i, 2);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Function to determine where to write a configuration file
|
|
73
|
+
*
|
|
74
|
+
* @param {string}[] args - command line arguments
|
|
75
|
+
* @returns {string} path to the configuration file
|
|
76
|
+
*/
|
|
77
|
+
function getConfigFilePath(args = []) {
|
|
78
|
+
processStandardArgs(args); // in case it has not already been called
|
|
79
|
+
|
|
80
|
+
// Handle any additional arguments
|
|
81
|
+
let explicitFilePath = undefined;
|
|
82
|
+
for (let i = args.length - 2; i >= 0; i--) {
|
|
83
|
+
switch (args[i].toLowerCase()) {
|
|
84
|
+
case "--config":
|
|
85
|
+
// This is an optional qaualifier for the filename
|
|
86
|
+
explicitFilePath = args[i + 1];
|
|
87
|
+
break;
|
|
88
|
+
case "--client":
|
|
89
|
+
// This is a legacy synonym for account
|
|
90
|
+
global.account = args[i + 1];
|
|
91
|
+
break;
|
|
92
|
+
case "--id":
|
|
93
|
+
// This is a synonym for monitoring-configuration-id
|
|
94
|
+
global.monitoringConfig = args[i + 1];
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// If there's an extra argument, use it as the explicit file path
|
|
100
|
+
// the file may not exist yet if we are creating it ...
|
|
101
|
+
if (args.length & 1) {
|
|
102
|
+
explicitFilePath = args[args.length - 1];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const cfgPath = path.join(opscompassHome, "configs");
|
|
106
|
+
const cfgName = "dgConfig.json";
|
|
107
|
+
|
|
108
|
+
var cfgFile = new Object({
|
|
109
|
+
CUST_NAME: global.company,
|
|
110
|
+
CUST_TOKEN: global.account,
|
|
111
|
+
configurationId: global.monitoringConfig,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// See if anything is missing
|
|
115
|
+
if (!explicitFilePath && hasNullOrUndefined(cfgFile, 3)) {
|
|
116
|
+
if (!cfgFile.CUST_NAME || !cfgFile.CUST_TOKEN) {
|
|
117
|
+
console.error("\nWARNING: filePath missing values:", cfgFile);
|
|
118
|
+
// console.error(`remaining args (${args.length}):`, args);
|
|
119
|
+
return undefined;
|
|
120
|
+
} else {
|
|
121
|
+
cfgFile.configurationId = 0; // This implies it was null or undefined
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// If we have an odd number of arguments the last one should be an explicit file path
|
|
126
|
+
if (explicitFilePath && !explicitFilePath.startsWith("--")) {
|
|
127
|
+
cfgFile.filePath = explicitFilePath;
|
|
128
|
+
} else {
|
|
129
|
+
cfgFile.filePath = `${cfgPath}/${global.company}/${global.account}/${cfgFile.configurationId}-${cfgName}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// If it's a relative path prepend PWD
|
|
133
|
+
if (!cfgFile.filePath.startsWith("/")) {
|
|
134
|
+
cfgFile.filePath = `${process.env.PWD}/${cfgFile.filePath}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// If they specified an existing file, use the ID from the file
|
|
138
|
+
if (fs.existsSync(cfgFile.filePath)) {
|
|
139
|
+
// get configurationId from the file
|
|
140
|
+
let fileData = undefined;
|
|
141
|
+
let fileObj = undefined;
|
|
142
|
+
try {
|
|
143
|
+
fileData = fs.readFileSync(cfgFile.filePath);
|
|
144
|
+
fileObj = JSON.parse(fileData);
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(
|
|
147
|
+
`Error reading file: ${cfgFile.filePath} - ${error.message}`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (fileObj?.configurationId) {
|
|
151
|
+
cfgFile.configurationId = fileObj.configurationId;
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
// try to get configurationId from the file name
|
|
155
|
+
const fParts = cfgFile.filePath.split("-")[0].replace(/^.*\//, "");
|
|
156
|
+
if (+fParts > 0) {
|
|
157
|
+
cfgFile.configurationId = +fParts;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// If file extension is not already json, add it
|
|
161
|
+
if (!cfgFile.filePath.endsWith(".json")) {
|
|
162
|
+
cfgFile.filePath += ".json";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// If filePath contains undefined, we do not have all the needed pieces
|
|
167
|
+
if (!cfgFile?.filePath || cfgFile.filePath.match(/undefined/)) {
|
|
168
|
+
// This message can produce confusing output during setup, so suppress it
|
|
169
|
+
// console.error(`\n********** Invalid file path:\n ${cfgFile?.filePath}\n`);
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return cfgFile;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Function read a configuration file
|
|
178
|
+
* @param {string}[] args - license-manager arguments
|
|
179
|
+
*/
|
|
180
|
+
function readConfigFile(args) {
|
|
181
|
+
const cfgFile = getConfigFilePath(args);
|
|
182
|
+
if (!cfgFile) return undefined;
|
|
183
|
+
|
|
184
|
+
if (!fs.existsSync(cfgFile.filePath)) {
|
|
185
|
+
console.error("Configuration file does not exist:", cfgFile.filePath);
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const json = fs.readFileSync(cfgFile.filePath, "utf8");
|
|
190
|
+
const config = JSON.parse(json);
|
|
191
|
+
decryptValues(config);
|
|
192
|
+
return config;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Function to write a configuration file
|
|
197
|
+
* Write the config file from an object
|
|
198
|
+
* The name of the file to write is derived from args
|
|
199
|
+
*
|
|
200
|
+
* If cfgData is provided we will use that as the new config
|
|
201
|
+
* If there is an existing file (based on args), we'll create a copy first
|
|
202
|
+
*/
|
|
203
|
+
async function writeConfigFile(args, cfgData = undefined, overwrite = false) {
|
|
204
|
+
var cfgFile = getConfigFilePath(args);
|
|
205
|
+
if (!cfgFile) return undefined;
|
|
206
|
+
|
|
207
|
+
var cfgName = cfgFile?.filePath;
|
|
208
|
+
// console.error("cfgFile:", cfgFile);
|
|
209
|
+
|
|
210
|
+
// Allow command line overwrite flag as well
|
|
211
|
+
const argOverwrite = args.indexOf("--overwrite") >= 0 ? true : false;
|
|
212
|
+
if (argOverwrite) overwrite = true;
|
|
213
|
+
|
|
214
|
+
// Figure out the name of the config file to write
|
|
215
|
+
if (!cfgFile) {
|
|
216
|
+
console.error("\n***** No config file specified to write");
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
//console.error("js: writeConfig file: " + cfgFile.filePath);
|
|
220
|
+
|
|
221
|
+
if (overwrite) {
|
|
222
|
+
const cfgBaseName = path.basename(cfgFile.filePath);
|
|
223
|
+
// if overwriting, delete the existing file
|
|
224
|
+
if (!cfgBaseName.startsWith("opscompass-monitoring-configuration.")) {
|
|
225
|
+
console.log("WARNING: overwriting existing config file:", cfgBaseName);
|
|
226
|
+
}
|
|
227
|
+
fs.rmSync(cfgFile.filePath, { force: true });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
var newConfig = cfgData;
|
|
231
|
+
if (newConfig === undefined) {
|
|
232
|
+
// We did not get anything to write, so create an empty config file
|
|
233
|
+
newConfig = new Object({
|
|
234
|
+
CUST_NAME: cfgFile.CUST_NAME,
|
|
235
|
+
CUST_TOKEN: cfgFile.CUST_TOKEN,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const fExt = cfgName.split(".").pop();
|
|
240
|
+
const cfgPath = path.dirname(cfgName);
|
|
241
|
+
let cfgBase = path.basename(cfgName, "." + fExt);
|
|
242
|
+
|
|
243
|
+
// Remove any timestamp from the end
|
|
244
|
+
cfgBase = cfgBase.replace(/_[0-9-]+/, "");
|
|
245
|
+
|
|
246
|
+
// Construct the name of the backup file
|
|
247
|
+
const buCfg = `${cfgPath}/${cfgBase}_${tStamp}.${fExt}`;
|
|
248
|
+
cfgName = `${cfgPath}/${cfgBase}.${fExt}`;
|
|
249
|
+
|
|
250
|
+
// Make a versioned backup of the config file (if it exists)
|
|
251
|
+
try {
|
|
252
|
+
fs.accessSync(cfgName, fs.constants.F_OK);
|
|
253
|
+
try {
|
|
254
|
+
// Create a copy with a current timestamp
|
|
255
|
+
fs.copyFileSync(cfgName, buCfg);
|
|
256
|
+
// console.error(`renamed\n ${cfgName}\nto\n ${buCfg}`)
|
|
257
|
+
fs.rmSync(cfgName, { force: true });
|
|
258
|
+
} catch (err) {
|
|
259
|
+
console.error("ERROR: " + err);
|
|
260
|
+
throw err;
|
|
261
|
+
}
|
|
262
|
+
} catch (err) {
|
|
263
|
+
if (err.code !== "ENOENT") console.error(err);
|
|
264
|
+
// else silently fail - the file did not exist
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Write the new file
|
|
268
|
+
delete newConfig.filePath; // Remove the filePath attribute first
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Encrypt sensitive values before writing the config file
|
|
272
|
+
*
|
|
273
|
+
* This modifies the original object by replacing sensitive values with
|
|
274
|
+
* their encrypted equivalents
|
|
275
|
+
*
|
|
276
|
+
* @params {Object} newConfig - the configuration object (configuration file)
|
|
277
|
+
* @returns {Object} newConfig - the configuration object with values encrypted
|
|
278
|
+
*/
|
|
279
|
+
encryptValues(newConfig);
|
|
280
|
+
const json = JSON.stringify(newConfig, null, 2) + "\n";
|
|
281
|
+
fs.mkdirSync(cfgPath, { recursive: true });
|
|
282
|
+
fs.writeFileSync(cfgName, json, (err) => {
|
|
283
|
+
if (err) {
|
|
284
|
+
console.error("ERROR: " + err);
|
|
285
|
+
throw err;
|
|
286
|
+
}
|
|
287
|
+
fs.chmodSync(cfgName, "0600");
|
|
288
|
+
console.log(cfgName);
|
|
289
|
+
});
|
|
290
|
+
decryptValues(newConfig);
|
|
291
|
+
newConfig.filePath = cfgName;
|
|
292
|
+
|
|
293
|
+
// Cleanup old files matching this name
|
|
294
|
+
// It processes in file order (which is apparently sorted), so with no restrictions
|
|
295
|
+
// it will get a list of all non-current config files
|
|
296
|
+
const result = findRemoveSync(cfgPath, {
|
|
297
|
+
// test: true, // Just get the list - do not actually remove
|
|
298
|
+
prefix: cfgBase + "_", // Add _ to get all non-current
|
|
299
|
+
extensions: fExt,
|
|
300
|
+
maxLevel: 1, // Do not include subdirectories
|
|
301
|
+
age: { seconds: 2505600 }, // 29 days
|
|
302
|
+
});
|
|
303
|
+
// console.log(JSON.stringify(result, null, 2));
|
|
304
|
+
|
|
305
|
+
return newConfig;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Get the cipher for the current customer by combining HOBKEY and customer token
|
|
310
|
+
* We use a salted key based on HOBKEY + account (MLSS cloudSubscripttionId),
|
|
311
|
+
* so that it's not possible for one customer to decrypt another's config file.
|
|
312
|
+
*
|
|
313
|
+
* The construction of the cypher is based on pgp, but there are enough disparities
|
|
314
|
+
* between platforms to cause problems, so we implemented it here for consistency.
|
|
315
|
+
*
|
|
316
|
+
* @returns {Object} cipher - the cipher object
|
|
317
|
+
*/
|
|
318
|
+
const algorithm = "aes-256-cbc";
|
|
319
|
+
const hashType = "md5";
|
|
320
|
+
function getCipher() {
|
|
321
|
+
let custTok;
|
|
322
|
+
if (global.account) {
|
|
323
|
+
custTok = global.account;
|
|
324
|
+
} else {
|
|
325
|
+
// Punt, try to use environment
|
|
326
|
+
custTok = process.env.OC_DEFAULT_account;
|
|
327
|
+
}
|
|
328
|
+
if (!custTok) {
|
|
329
|
+
throw new Error("Customer Token not defined");
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const password = Buffer.from(HOBKEY + custTok);
|
|
333
|
+
const salt = crypto.randomBytes(8);
|
|
334
|
+
|
|
335
|
+
// concat the password and salt to generate the key and initialization vector (iv)
|
|
336
|
+
// https://www.baeldung.com/java-encryption-iv
|
|
337
|
+
var base = Buffer.concat([password, salt]);
|
|
338
|
+
var hash = [];
|
|
339
|
+
hash[0] = crypto.createHash(hashType).update(base).digest();
|
|
340
|
+
hash[1] = crypto
|
|
341
|
+
.createHash(hashType)
|
|
342
|
+
.update(Buffer.concat([hash[0], base]))
|
|
343
|
+
.digest();
|
|
344
|
+
hash[2] = crypto
|
|
345
|
+
.createHash(hashType)
|
|
346
|
+
.update(Buffer.concat([hash[1], base]))
|
|
347
|
+
.digest();
|
|
348
|
+
var result = Buffer.concat(hash); //concat 3 md5 to get a 48-byte length data.
|
|
349
|
+
var key = result.subarray(0, 32);
|
|
350
|
+
var iv = result.subarray(32);
|
|
351
|
+
|
|
352
|
+
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
353
|
+
return { cipher: cipher, salt: salt };
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// Decrypt a text string
|
|
357
|
+
/* Decrypt openssl command line:
|
|
358
|
+
echo "${1##oc\$hash:}" | openssl aes-256-cbc -d -A -a \
|
|
359
|
+
-pass "pass:${HOBKEY}${CUST_TOKEN}"
|
|
360
|
+
*/
|
|
361
|
+
function decryptAES(text) {
|
|
362
|
+
let custTok;
|
|
363
|
+
if (global.account) {
|
|
364
|
+
custTok = global.account;
|
|
365
|
+
} else {
|
|
366
|
+
// Punt, try to use environment
|
|
367
|
+
custTok = process.env.OC_DEFAULT_account;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const password = Buffer.from(HOBKEY + custTok);
|
|
371
|
+
const input = text.replace(hashRegex, "");
|
|
372
|
+
|
|
373
|
+
var buf = Buffer.from(input, "base64"); //decode base64 string to buffer
|
|
374
|
+
//console.log('decrypt buf', buf.toString('hex'));
|
|
375
|
+
|
|
376
|
+
var salt = buf.subarray(8, 16); //8 byte salt
|
|
377
|
+
//console.log('salt', salt.toString('hex'));
|
|
378
|
+
|
|
379
|
+
var base = Buffer.concat([password, salt]); //concat the password and salt to generate key and iv
|
|
380
|
+
var hash = [];
|
|
381
|
+
hash[0] = crypto.createHash(hashType).update(base).digest();
|
|
382
|
+
hash[1] = crypto
|
|
383
|
+
.createHash(hashType)
|
|
384
|
+
.update(Buffer.concat([hash[0], base]))
|
|
385
|
+
.digest();
|
|
386
|
+
hash[2] = crypto
|
|
387
|
+
.createHash(hashType)
|
|
388
|
+
.update(Buffer.concat([hash[1], base]))
|
|
389
|
+
.digest();
|
|
390
|
+
var result = Buffer.concat(hash); //concat 3 md5 to get a 48-byte length data.
|
|
391
|
+
var key = result.subarray(0, 32);
|
|
392
|
+
var iv = result.subarray(32);
|
|
393
|
+
|
|
394
|
+
//console.log('key', key.toString('hex'));
|
|
395
|
+
//console.log('iv', iv.toString('hex'));
|
|
396
|
+
|
|
397
|
+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
398
|
+
var content = buf.subarray(16);
|
|
399
|
+
var output = decipher.update(content, null, "utf8");
|
|
400
|
+
output += decipher.final("utf8");
|
|
401
|
+
//console.log('output', output);
|
|
402
|
+
return output;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Encrypt a text string
|
|
406
|
+
/* Encrypt openssl command line:
|
|
407
|
+
openssl aes-256-cbc -A -a -salt \
|
|
408
|
+
-pass "pass:${HOBKEY}${CUST_TOKEN}" | awk '{print "oc$hash:"$1}'
|
|
409
|
+
*/
|
|
410
|
+
function encryptAES(data) {
|
|
411
|
+
const enc = getCipher();
|
|
412
|
+
|
|
413
|
+
const encrypted = Buffer.concat([
|
|
414
|
+
Buffer.from("Salted__"),
|
|
415
|
+
enc.salt,
|
|
416
|
+
enc.cipher.update(data),
|
|
417
|
+
enc.cipher.final(),
|
|
418
|
+
]);
|
|
419
|
+
|
|
420
|
+
return hashPrefix + encrypted.toString("base64");
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Encrypt a file, as a stream
|
|
424
|
+
function encryptFileAES(args) {
|
|
425
|
+
const enc = getCipher();
|
|
426
|
+
const outFname = `${args.filePath}.gpg`;
|
|
427
|
+
|
|
428
|
+
var infile = fs.createReadStream(args.filePath);
|
|
429
|
+
var outfile = fs.createWriteStream(outFname);
|
|
430
|
+
|
|
431
|
+
// var size = fs.statSync(args.filePath).size;
|
|
432
|
+
infile.on("data", function (data) {
|
|
433
|
+
// var percentage = parseInt(infile.bytesRead) / parseInt(size);
|
|
434
|
+
// console.log(percentage * 100);
|
|
435
|
+
// var encrypted = cipher.update(data);
|
|
436
|
+
var encrypted = Buffer.concat([
|
|
437
|
+
Buffer.from("Salted__"),
|
|
438
|
+
enc.salt,
|
|
439
|
+
enc.cipher.update(data),
|
|
440
|
+
]);
|
|
441
|
+
if (encrypted) {
|
|
442
|
+
// console.log(encrypted);
|
|
443
|
+
outfile.write(encrypted);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
infile.on("close", function () {
|
|
447
|
+
outfile.write(enc.cipher.final());
|
|
448
|
+
outfile.close();
|
|
449
|
+
});
|
|
450
|
+
return outFname;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Decrypt all values after reading a file containing encrypted values
|
|
455
|
+
*
|
|
456
|
+
* This modifies the original object by replacing encrypted values with
|
|
457
|
+
* their plain text equivalents
|
|
458
|
+
*
|
|
459
|
+
* @params {Object} obj - the object (possibly a configuration file)
|
|
460
|
+
* @returns {Object} obj - the modified object
|
|
461
|
+
*/
|
|
462
|
+
function decryptValues(obj) {
|
|
463
|
+
if (HOBKEY === undefined) {
|
|
464
|
+
console.error("Decryption not possible - HoB Key is not defined");
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (typeof obj !== "object") return;
|
|
468
|
+
|
|
469
|
+
// Recurse through all properties
|
|
470
|
+
for (let k in obj) {
|
|
471
|
+
if (typeof obj[k] === "object") {
|
|
472
|
+
decryptValues(obj[k]);
|
|
473
|
+
} else {
|
|
474
|
+
// if the value is an encrypted string, decrypt it
|
|
475
|
+
if (typeof obj[k] === "string" && obj[k].match(hashRegex)) {
|
|
476
|
+
obj[k] = decryptAES(obj[k]);
|
|
477
|
+
// console.log("%s, decrypted: %s", k, obj[k]);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Encrypt sensitive values
|
|
484
|
+
function encryptValues(config) {
|
|
485
|
+
if (HOBKEY === undefined) {
|
|
486
|
+
console.error("Encryption not possible - HoB Key is not defined");
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (typeof config !== "object") return;
|
|
490
|
+
|
|
491
|
+
// Loop through the resources types
|
|
492
|
+
for (const resType of Object.keys(resourceTypes)) {
|
|
493
|
+
// If we have an array of resourceType, then look through it
|
|
494
|
+
if (config.hasOwnProperty(resType) && config[resType].length > 0) {
|
|
495
|
+
// Examine each resource for sensistive values
|
|
496
|
+
for (const resource of config[resType]) {
|
|
497
|
+
for (const attr of Object.keys(encryptAttrs)) {
|
|
498
|
+
if (resource.hasOwnProperty(attr) && resource[attr] !== null) {
|
|
499
|
+
// If this is a sensitive value and it is not already encrypted
|
|
500
|
+
if (!resource[attr].match(hashRegex)) {
|
|
501
|
+
resource[attr] = encryptAES(resource[attr]);
|
|
502
|
+
// console.error("Encrypted attribute %s: %s ", attr, resource[attr]);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* PREPEND ZERO to a digit, used for timestamps
|
|
513
|
+
* @param {*} num
|
|
514
|
+
* @returns
|
|
515
|
+
*/
|
|
516
|
+
function prezero(num) {
|
|
517
|
+
return num < 10 ? "0" + num : num;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Test if a string represents an IPv4 address
|
|
522
|
+
*
|
|
523
|
+
* @param {*} address
|
|
524
|
+
* @returns boolean
|
|
525
|
+
*/
|
|
526
|
+
function isIPv4(address) {
|
|
527
|
+
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
|
|
528
|
+
return (
|
|
529
|
+
ipv4Regex.test(address) &&
|
|
530
|
+
address.split(".").every((num) => Number(num) >= 0 && Number(num) <= 255)
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Test if a string represents an IPv6 address
|
|
536
|
+
*
|
|
537
|
+
* @param {*} address
|
|
538
|
+
* @returns boolean
|
|
539
|
+
*/
|
|
540
|
+
function isIPv6(address) {
|
|
541
|
+
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)$/;
|
|
542
|
+
return ipv6Regex.test(address);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Produce an md5sum for the input string
|
|
547
|
+
* @param {*} input
|
|
548
|
+
* @returns string
|
|
549
|
+
*/
|
|
550
|
+
function md5sum(input) {
|
|
551
|
+
return crypto.createHash("md5").update(input).digest("hex");
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Produce a sha256sum for the input string
|
|
556
|
+
* @param {*} input
|
|
557
|
+
* @returns string
|
|
558
|
+
*/
|
|
559
|
+
function sha256sum(input) {
|
|
560
|
+
return crypto.createHash("sha256").update(input).digest("hex");
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Read from stdin if anything is present
|
|
565
|
+
*/
|
|
566
|
+
async function readStdin() {
|
|
567
|
+
try {
|
|
568
|
+
const fileBuffer = fs.readFileSync(0, "utf-8");
|
|
569
|
+
if (fileBuffer.length === 0) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
return fileBuffer;
|
|
573
|
+
} catch (err) {
|
|
574
|
+
if (err.code === "EAGAIN") {
|
|
575
|
+
// 'resource temporarily unavailable'
|
|
576
|
+
// Happens on OS X (not Windows)
|
|
577
|
+
} else if (err.code === "EOF") {
|
|
578
|
+
// Happens on Windows, but not OS X:
|
|
579
|
+
// simply signals the end of *piped* stdin input.
|
|
580
|
+
} else {
|
|
581
|
+
console.error(err);
|
|
582
|
+
}
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Export module artifacts
|
|
588
|
+
const utilsModule = {
|
|
589
|
+
processStandardArgs,
|
|
590
|
+
decryptValues,
|
|
591
|
+
encryptValues,
|
|
592
|
+
getConfigFilePath,
|
|
593
|
+
getTimestamp,
|
|
594
|
+
opscompassHome,
|
|
595
|
+
readConfigFile,
|
|
596
|
+
readStdin,
|
|
597
|
+
writeConfigFile,
|
|
598
|
+
isIPv4,
|
|
599
|
+
isIPv6,
|
|
600
|
+
md5sum,
|
|
601
|
+
sha256sum,
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
module.exports = { utilsModule };
|
|
@@ -10,8 +10,6 @@ const {
|
|
|
10
10
|
previousScriptsDownloaded,
|
|
11
11
|
} = require("../lib/homeInteraction.js");
|
|
12
12
|
const { canReadFile } = require("../lib/cliHelpers.js");
|
|
13
|
-
const { readDefaults, writeDefaults, checkDefaults } = require("./defaults");
|
|
14
|
-
const { opscompassHome } = require("../environment/current.js");
|
|
15
13
|
|
|
16
14
|
const semver = require("semver");
|
|
17
15
|
const path = require("path");
|
|
@@ -78,7 +76,6 @@ async function listLocalScripts(args) {
|
|
|
78
76
|
* Downloads data gathering scripts based on the given scriptType.
|
|
79
77
|
*/
|
|
80
78
|
async function downloadScripts(args) {
|
|
81
|
-
const defaultsPath = path.join(opscompassHome, "defaults.json");
|
|
82
79
|
let scriptType = undefined;
|
|
83
80
|
let scriptVersion = undefined;
|
|
84
81
|
for (let i = 0; i < args.length - 1; i++) {
|