@opscompass/opscompass-cli 1.0.24 → 1.0.26

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/index.js CHANGED
@@ -11,29 +11,28 @@ const path = require("path");
11
11
  let defaultParams = JSON.parse("{}");
12
12
 
13
13
  function checkDefaults() {
14
- const defaultsPath = path.join(opscompassHome, 'defaults.json');
14
+ const defaultsPath = path.join(opscompassHome, "defaults.json");
15
15
  let rawdata;
16
16
  try {
17
17
  rawdata = fs.readFileSync(defaultsPath);
18
18
  defaultParams = JSON.parse(rawdata);
19
19
  } catch (err) {
20
20
  // We just want to catch the error, no logging necessary
21
- }
22
- /**
23
- * When an individual string is passed to the API, default param will be provided if it exists.
24
- * Default value is overwritten when explicitly included in CLI command.
25
- */
21
+ }
22
+ /**
23
+ * When an individual string is passed to the API, default param will be provided if it exists.
24
+ * Default value is overwritten when explicitly included in CLI command.
25
+ */
26
26
  companySlug = defaultParams.company;
27
27
  company = defaultParams.company;
28
28
  account = defaultParams.account;
29
29
  monitoringConfig = defaultParams.monitoringConfiguration;
30
30
 
31
- /**
32
- * When a callParams object is passed to the API rather than one of the above strings, default values will be used unless explicitly replaced.
31
+ /**
32
+ * When a callParams object is passed to the API rather than one of the above strings, default values will be used unless explicitly replaced.
33
33
  * Other properties can still be added to the callParams object. Unneeded default parameters may be included, but are ignored by the API.
34
- */
34
+ */
35
35
  callParams = defaultParams;
36
-
37
36
  }
38
37
 
39
38
  function requiresLogin() {
@@ -77,7 +76,11 @@ function help() {
77
76
  const module = modules[moduleName];
78
77
  if (!module.requiresLogin || loggedIn()) {
79
78
  try {
80
- await module(...args);
79
+ if (typeof module === "function") {
80
+ await module(...args);
81
+ } else {
82
+ console.log(JSON.stringify(module, null, 2));
83
+ }
81
84
  } catch (e) {
82
85
  handleError(e);
83
86
  }
@@ -1,5 +1,7 @@
1
1
  const fs = require("fs-extra");
2
2
  const path = require("path");
3
+ const pgp = require("./pgpTools.js");
4
+
3
5
  const {
4
6
  opscompassHome,
5
7
  ScriptType,
@@ -19,6 +21,8 @@ const {
19
21
  } = require("./cliHelpers.js");
20
22
  const { nextTick } = require("process");
21
23
 
24
+ const semver = require("semver");
25
+
22
26
  const SRC_FOLDER_NAME = "src";
23
27
  const METADATA_FILE_NAME = "metadata.json";
24
28
  const BASE_SCRIPTS_RUN_ALL_RELATIVE_PATH = "scripts/runall.sh";
@@ -127,21 +131,57 @@ function scriptTypeFolderPath(scriptType) {
127
131
  * @param {string} companySlug company slug to use when downloading scripts
128
132
  * @param {OpsCompassClient} client client to use to call OpsCompass api
129
133
  */
134
+ var scriptsAvailable = undefined;
130
135
  async function downloadScriptsToHome(
131
136
  scriptType,
132
137
  scriptVersion,
133
138
  companySlug,
134
- client
139
+ client,
140
+ scriptsRemote = undefined
135
141
  ) {
136
- // Ensure base scripts are downloaded/updated
142
+ // If scriptsRemote is provided, use it instead of fetching from api
143
+ if (!!scriptsRemote) {
144
+ scriptsAvailable = scriptsRemote;
145
+ }
146
+
147
+ // Ensure base scripts are downloaded/updated for all modules
137
148
  if (scriptType !== ScriptType.baseScripts) {
138
- const baseVersion = await downloadScriptsToHome(
139
- ScriptType.baseScripts,
140
- undefined,
141
- companySlug,
142
- client
149
+ const localBaseVersion = await getInstalledScriptVersion(
150
+ ScriptType.baseScripts
143
151
  );
144
- console.log("Base scripts (v%s)", baseVersion);
152
+
153
+ // Determine what is available
154
+ if (!scriptsAvailable) {
155
+ console.log("Fetching remote versions...");
156
+ scriptsAvailable = await client.dataGatheringScripts.getScriptVersions(
157
+ companySlug
158
+ );
159
+ }
160
+
161
+ var availableBase = undefined;
162
+ if (
163
+ Array.isArray(scriptsAvailable?.versions) &&
164
+ scriptsAvailable.versions?.length > 0
165
+ ) {
166
+ availableBase = scriptsAvailable.versions.find(
167
+ (version) => version.scriptType === ScriptType.baseScripts
168
+ );
169
+ }
170
+
171
+ // If base is available and not installed or out of date
172
+ if (
173
+ availableBase &&
174
+ (!localBaseVersion ||
175
+ semver.gt(availableBase.scriptVersion, localBaseVersion))
176
+ ) {
177
+ const baseVersion = await downloadScriptsToHome(
178
+ ScriptType.baseScripts,
179
+ undefined,
180
+ companySlug,
181
+ client
182
+ );
183
+ console.log("Base scripts updated (v%s)", baseVersion);
184
+ }
145
185
  }
146
186
 
147
187
  // Download specific script
@@ -160,6 +200,12 @@ async function downloadScriptsToHome(
160
200
  scriptTypeFolderPath,
161
201
  fileName
162
202
  );
203
+
204
+ // Attempt to verify the download - returns true if signed and verified
205
+ // returns false and continues otherwise
206
+ scriptDownloadInfo.filePath = path.join(scriptTypeFolderPath, fileName);
207
+ await pgp.Verify(scriptDownloadInfo);
208
+
163
209
  await unzipFile(scriptTypeFolderPath, fileName);
164
210
 
165
211
  // Move contents from script version folder to src
@@ -0,0 +1,103 @@
1
+ // Module for PGP tools
2
+
3
+ const openpgp = require("openpgp");
4
+ const fs = require("fs");
5
+ const axios = require("axios");
6
+
7
+ // publicKey object is used to verify the signature
8
+ let publicKey = undefined;
9
+
10
+ async function Verify(scriptDownloadInfo) {
11
+ // If the download info includes a gpg signature, verify it
12
+ if (scriptDownloadInfo?.gpgSignature) {
13
+ // Create a readable stream for the archive file
14
+ let readableStream = undefined;
15
+ if (fs.existsSync(scriptDownloadInfo.filePath)) {
16
+ readableStream = fs.createReadStream(scriptDownloadInfo.filePath);
17
+ } else {
18
+ console.error("Cannot read archive file:", scriptDownloadInfo.filePath);
19
+ return false;
20
+ }
21
+
22
+ // Run asynchronously to get the public key, if we don't already have it
23
+ if (!publicKey) {
24
+ // If signer is not included, assume cli-token@opscompass.com
25
+ var publicKeyArmored = undefined;
26
+ const gpgSigner =
27
+ scriptDownloadInfo?.gpgSigner || "cli-token@opscompass.com";
28
+ const getPGP = axios
29
+ .get(
30
+ "https://keys.openpgp.org/pks/lookup?op=get&options=mr&search=" +
31
+ gpgSigner
32
+ )
33
+ .then(function (response) {
34
+ publicKeyArmored = response?.data;
35
+ })
36
+ .catch(function (error) {
37
+ console.error("Error:", error.message);
38
+ });
39
+
40
+ // Wait for API response from public key repo
41
+ await getPGP;
42
+ if (!!publicKeyArmored) {
43
+ //console.log(publicKeyArmored);
44
+ } else {
45
+ console.error("Error retrieving public key for signer: %s", gpgSigner);
46
+ return false;
47
+ }
48
+
49
+ // Read the (armored) public key
50
+ try {
51
+ publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
52
+ } catch (err) {
53
+ console.error("Problems reading public key:", err);
54
+ return false;
55
+ }
56
+ // Public key is good
57
+ }
58
+
59
+ // Read the (armored) detached signature from download info
60
+ let signature = undefined;
61
+ try {
62
+ signature = await openpgp.readSignature({
63
+ armoredSignature: scriptDownloadInfo.gpgSignature,
64
+ });
65
+ } catch (err) {
66
+ console.error(
67
+ "Problems reading signature:",
68
+ scriptDownloadInfo?.gpgSignature
69
+ );
70
+ console.error("ERROR:", JSON.stringify(err, null, 2));
71
+ return false;
72
+ }
73
+ // Signature is valid
74
+
75
+ // And, finally - verify the signature and document
76
+ const message = await openpgp.createMessage({ binary: readableStream });
77
+ const verificationResult = await openpgp.verify({
78
+ message,
79
+ signature,
80
+ verificationKeys: publicKey,
81
+ });
82
+ const { verified, keyID } = verificationResult.signatures[0];
83
+ try {
84
+ await verified; // throws on invalid signature
85
+ //console.log("VERIFIED: Signed by key id " + keyID.toHex());
86
+ } catch (e) {
87
+ console.log("Archive file verification failed: " + e.message);
88
+ return false;
89
+ }
90
+ console.log(
91
+ "Archive file digitally signed by: %s",
92
+ publicKey?.users[0]?.userID?.userID || gpgSigner
93
+ );
94
+ return true;
95
+ } else {
96
+ console.log("Archive file NOT digitally signed");
97
+ return false;
98
+ }
99
+ }
100
+
101
+ module.exports = {
102
+ Verify,
103
+ };
@@ -20,7 +20,7 @@ const util = require("util");
20
20
  const exec = util.promisify(require("child_process").exec);
21
21
 
22
22
  /**
23
- * Lists script types that are locally installed
23
+ * Lists script types that are locally downloaded
24
24
  */
25
25
  async function listLocalScripts(args) {
26
26
  let quiet = false;
@@ -84,7 +84,7 @@ async function downloadScripts(args) {
84
84
  for (let i = 0; i < args.length - 1; i++) {
85
85
  switch (args[i].toLowerCase()) {
86
86
  case "--company":
87
- companySlug = args[++i];
87
+ company = args[++i];
88
88
  break;
89
89
  case "--script-type":
90
90
  case "--scripttype":
@@ -110,7 +110,7 @@ async function downloadScripts(args) {
110
110
  const rcvdVersion = await downloadScriptsToHome(
111
111
  scriptType,
112
112
  scriptVersion,
113
- companySlug,
113
+ company,
114
114
  client
115
115
  );
116
116
 
@@ -129,7 +129,7 @@ async function listAvailableScripts(args) {
129
129
  for (let i = 0; i < args.length; i++) {
130
130
  switch (args[i].toLowerCase()) {
131
131
  case "--company":
132
- companySlug = args[++i];
132
+ company = args[++i];
133
133
  break;
134
134
  case "--quiet":
135
135
  quiet = true;
@@ -156,7 +156,7 @@ async function listAvailableScripts(args) {
156
156
  }
157
157
 
158
158
  /**
159
- * Checks the installed scripts version against the latest scripts version for a given scriptType.
159
+ * Checks the downloaded scripts version against the latest scripts version for a given scriptType.
160
160
  * or all scriptTypes if not specified
161
161
  */
162
162
  async function checkScriptUpdates(args) {
@@ -169,7 +169,7 @@ async function checkScriptUpdates(args) {
169
169
  autoUpdate = true;
170
170
  break;
171
171
  case "--company":
172
- companySlug = args[++i];
172
+ company = args[++i];
173
173
  break;
174
174
  case "--script-type":
175
175
  case "--scripttype":
@@ -193,7 +193,7 @@ async function checkScriptUpdates(args) {
193
193
 
194
194
  if (!currentScriptVersion) {
195
195
  console.log(
196
- `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
196
+ `Local scripts for type ${scriptType} not found. Please make sure they are downloaded.`
197
197
  );
198
198
  console.log("If the issue persists, please contact your OpsCompass rep.");
199
199
  return;
@@ -201,10 +201,7 @@ async function checkScriptUpdates(args) {
201
201
 
202
202
  console.log("Checking for latest version of module %s...", scriptType);
203
203
  const latestScriptVersion =
204
- await client.dataGatheringScripts.checkScriptUpdates(
205
- companySlug,
206
- scriptType
207
- );
204
+ await client.dataGatheringScripts.checkScriptUpdates(company, scriptType);
208
205
 
209
206
  //Semver returns true if the latest version is higher than the currentScriptVersion.
210
207
  if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
@@ -214,8 +211,25 @@ async function checkScriptUpdates(args) {
214
211
  );
215
212
  console.log(
216
213
  greenText,
217
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
214
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptType} `
218
215
  );
216
+ if (autoUpdate) {
217
+ console.log("Updating...");
218
+ try {
219
+ // Get the latest version
220
+ const rcvdVersion = await downloadScriptsToHome(
221
+ scriptType,
222
+ undefined,
223
+ company,
224
+ client
225
+ );
226
+
227
+ console.log("Downloaded %s (v%s)", scriptVer.scriptType, rcvdVersion);
228
+ } catch (err) {
229
+ console.log("Download failed...");
230
+ console.log(err);
231
+ }
232
+ }
219
233
  console.log();
220
234
  } else {
221
235
  console.log(
@@ -234,7 +248,7 @@ async function checkScriptUpdates(args) {
234
248
  ) {
235
249
  // We have at least one scriptType downloaded
236
250
  } else {
237
- console.error("Scripts available for download:");
251
+ console.error("\nScripts available for download:");
238
252
  listAvailableScripts(args);
239
253
  return;
240
254
  }
@@ -248,7 +262,7 @@ async function checkScriptUpdates(args) {
248
262
  : 0;
249
263
  });
250
264
 
251
- console.log("\nFetching remote versions...");
265
+ console.log("\nCheck-Updates fetching remote versions...");
252
266
  const scriptsRemote = await listAvailableScripts([...args, "--quiet"]);
253
267
 
254
268
  for (const scriptVer of scriptsLocal.versions) {
@@ -270,16 +284,18 @@ async function checkScriptUpdates(args) {
270
284
  );
271
285
  console.log(
272
286
  greenText,
273
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptVer.scriptType} \n`
287
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptVer.scriptType} \n`
274
288
  );
275
289
  if (autoUpdate) {
290
+ console.log("Updating...");
276
291
  try {
277
292
  // Get the latest version
278
293
  const rcvdVersion = await downloadScriptsToHome(
279
294
  scriptVer.scriptType,
280
295
  undefined,
281
- companySlug,
282
- client
296
+ company,
297
+ client,
298
+ scriptsRemote
283
299
  );
284
300
 
285
301
  console.log(
@@ -292,6 +308,7 @@ async function checkScriptUpdates(args) {
292
308
  console.log(err);
293
309
  }
294
310
  }
311
+ console.log();
295
312
  } else {
296
313
  console.log(
297
314
  "\nmodule %s: Local=%s, is up to date",
@@ -309,7 +326,7 @@ async function autoCheckScriptUpdates(args) {
309
326
  for (let i = 0; i < args.length - 1; i++) {
310
327
  switch (args[i].toLowerCase()) {
311
328
  case "--company":
312
- companySlug = args[++i];
329
+ company = args[++i];
313
330
  break;
314
331
  case "--script-type":
315
332
  case "--scripttype":
@@ -326,17 +343,14 @@ async function autoCheckScriptUpdates(args) {
326
343
  const currentScriptVersion = await getInstalledScriptVersion(scriptType);
327
344
  if (!currentScriptVersion) {
328
345
  console.log(
329
- `Local scripts for type ${scriptType} not found. Please make sure they are installed.`
346
+ `Local scripts for type ${scriptType} not found. Please make sure they are downloaded.`
330
347
  );
331
348
  console.log("If the issue persists, please contact your OpsCompass rep.");
332
349
  return;
333
350
  }
334
351
 
335
352
  const latestScriptVersion =
336
- await client.dataGatheringScripts.checkScriptUpdates(
337
- companySlug,
338
- scriptType
339
- );
353
+ await client.dataGatheringScripts.checkScriptUpdates(company, scriptType);
340
354
 
341
355
  //Semver returns true if the latest version is higher than the currentScriptVersion.
342
356
  if (semver.gt(latestScriptVersion.scriptVersion, currentScriptVersion)) {
@@ -347,15 +361,15 @@ async function autoCheckScriptUpdates(args) {
347
361
  console.log();
348
362
  console.log(
349
363
  greenText,
350
- ` ${cliCommand} data-gathering-scripts download-scripts --company ${companySlug} --scriptType ${scriptType} `
364
+ ` ${cliCommand} data-gathering-scripts download-scripts --company ${company} --scriptType ${scriptType} `
351
365
  );
352
366
  console.log();
353
367
  }
354
368
  }
355
369
 
356
370
  /**
357
- * Manually runs the installed scripts. The base scripts has a runall.sh file.
358
- * This file runs all installed modules.
371
+ * Manually runs the configured scripts. The base scripts has a runall.sh file.
372
+ * The runall.sh file runs all configured modules.
359
373
  */
360
374
  async function manuallyRunScripts() {
361
375
  try {
@@ -622,7 +622,7 @@ async function getMssqlEntitlements(args) {
622
622
 
623
623
  async function createOracleDatabaseConfiguration(args) {
624
624
  let name = undefined;
625
- let optionalParams = { };
625
+ let optionalParams = {};
626
626
  for (let i = 0; i < args.length - 1; i++) {
627
627
  switch (args[i].toLowerCase()) {
628
628
  case "--monitoringconfigurationid":
@@ -674,6 +674,14 @@ async function updateMonitoringConfiguration(args) {
674
674
  case "--configuration-name":
675
675
  callParams.configurationName = args[++i];
676
676
  break;
677
+ case "--autoupdateconfigurations":
678
+ case "--auto-update-configurations":
679
+ callParams.autoUpdateConfigurations = args[++i];
680
+ break;
681
+ case "--autoupdatescripts":
682
+ case "--auto-update-scripts":
683
+ callParams.autoUpdateScripts = args[++i];
684
+ break;
677
685
  case "--datafileretaindays":
678
686
  callParams.dataFileRetainDays = args[++i];
679
687
  break;
@@ -1601,6 +1609,8 @@ async function printHelp() {
1601
1609
  console.log("Parameters:");
1602
1610
  console.log("--id [monitoring_configuration id]");
1603
1611
  console.log("--name or --configuration-name (optional) [unique name]");
1612
+ console.log("--autoUpdateConfigurations (optional) [true | false]");
1613
+ console.log("--autoUpdateScripts (optional) [true | false]");
1604
1614
  console.log(
1605
1615
  "--dataFileRetainDays (optional) [determines how many days to retain encrypted output files locally]"
1606
1616
  );
package/modules.js CHANGED
@@ -2,9 +2,19 @@ const resources = require("./modules/resources.js");
2
2
  const accounts = require("./modules/accounts.js");
3
3
  const licenseManager = require("./modules/license-manager.js");
4
4
  const compliance = require("./modules/compliance.js");
5
- const dataGatheringScripts = require("./modules/data-gathering-scripts.js");
6
5
  const defaults = require("./modules/defaults.js");
7
6
 
7
+ // Dynaimcally loaded modules - may be optional
8
+ const fs = require("fs");
9
+ const path = require("path");
10
+ const dataGatheringScripts = fs.existsSync(
11
+ path.join(__dirname, "modules/data-gathering-scripts.js")
12
+ )
13
+ ? require("./modules/data-gathering-scripts.js")
14
+ : {
15
+ requiresLogin: false,
16
+ description: "removed for security",
17
+ };
8
18
 
9
19
  /* login acts like a module, even though it's special */
10
20
  const login = require("./login.js");
@@ -14,7 +24,7 @@ module.exports = {
14
24
  resources,
15
25
  accounts,
16
26
  compliance,
17
- defaults,
27
+ defaults,
18
28
  "license-manager": licenseManager,
19
- "data-gathering-scripts": dataGatheringScripts
29
+ "data-gathering-scripts": dataGatheringScripts,
20
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opscompass/opscompass-cli",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "Calls OpsCompass's APIs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,12 +15,13 @@
15
15
  "author": "opscompass",
16
16
  "license": "SEE LICENSE IN license.txt",
17
17
  "dependencies": {
18
- "@opscompass/opscompass-api": "^1.0.24",
18
+ "@opscompass/opscompass-api": "^1.0.26",
19
19
  "adm-zip": "^0.5.9",
20
20
  "axios": "^0.21.1",
21
21
  "fs-extra": "^10.1.0",
22
22
  "https-proxy-agent": "^5.0.1",
23
23
  "open": "^8.2.1",
24
+ "openpgp": "^5.10.2",
24
25
  "semver": "^7.3.7"
25
26
  },
26
27
  "bin": {