@opscompass/opscompass-cli 1.0.25 → 1.0.27

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.
@@ -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,
@@ -134,8 +136,14 @@ async function downloadScriptsToHome(
134
136
  scriptType,
135
137
  scriptVersion,
136
138
  companySlug,
137
- client
139
+ client,
140
+ scriptsRemote = undefined
138
141
  ) {
142
+ // If scriptsRemote is provided, use it instead of fetching from api
143
+ if (!!scriptsRemote) {
144
+ scriptsAvailable = scriptsRemote;
145
+ }
146
+
139
147
  // Ensure base scripts are downloaded/updated for all modules
140
148
  if (scriptType !== ScriptType.baseScripts) {
141
149
  const localBaseVersion = await getInstalledScriptVersion(
@@ -192,6 +200,13 @@ async function downloadScriptsToHome(
192
200
  scriptTypeFolderPath,
193
201
  fileName
194
202
  );
203
+
204
+ // Attempt to verify the download - returns true if signed and verified
205
+ // outputs validation error(s) and returns false otherwise
206
+ // - we continue regardless of validation for now...
207
+ scriptDownloadInfo.filePath = path.join(scriptTypeFolderPath, fileName);
208
+ const zipValidated = await pgp.Verify(scriptDownloadInfo);
209
+
195
210
  await unzipFile(scriptTypeFolderPath, fileName);
196
211
 
197
212
  // Move contents from script version folder to src
@@ -0,0 +1,106 @@
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("Key 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(
46
+ "Validation error retrieving public key for: %s",
47
+ gpgSigner
48
+ );
49
+ return false;
50
+ }
51
+
52
+ // Read the (armored) public key
53
+ try {
54
+ publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });
55
+ } catch (err) {
56
+ console.error("Validation error reading public key:\n", err);
57
+ return false;
58
+ }
59
+ // Public key is good
60
+ }
61
+
62
+ // Read the (armored) detached signature from download info
63
+ let signature = undefined;
64
+ try {
65
+ signature = await openpgp.readSignature({
66
+ armoredSignature: scriptDownloadInfo.gpgSignature,
67
+ });
68
+ } catch (err) {
69
+ console.error(
70
+ "Validation error reading file signature:\n",
71
+ scriptDownloadInfo?.gpgSignature
72
+ );
73
+ console.error("ERROR:", JSON.stringify(err, null, 2));
74
+ return false;
75
+ }
76
+ // Signature is valid
77
+
78
+ // And, finally - verify the signature and document
79
+ const message = await openpgp.createMessage({ binary: readableStream });
80
+ const verificationResult = await openpgp.verify({
81
+ message,
82
+ signature,
83
+ verificationKeys: publicKey,
84
+ });
85
+ const { verified, keyID } = verificationResult.signatures[0];
86
+ try {
87
+ await verified; // throws on invalid signature
88
+ //console.log("VERIFIED: Signed by key id " + keyID.toHex());
89
+ } catch (e) {
90
+ console.log("Archive file verification failed:\n" + e.message);
91
+ return false;
92
+ }
93
+ console.log(
94
+ "Archive file digitally signed by:\n %s",
95
+ publicKey?.users[0]?.userID?.userID || gpgSigner
96
+ );
97
+ return true;
98
+ } else {
99
+ console.log("Archive file NOT digitally signed");
100
+ return false;
101
+ }
102
+ }
103
+
104
+ module.exports = {
105
+ Verify,
106
+ };
@@ -116,7 +116,7 @@ async function downloadScripts(args) {
116
116
 
117
117
  console.log("Downloaded %s (v%s)", scriptType, rcvdVersion);
118
118
  } catch (err) {
119
- console.log("Download failed...");
119
+ console.log("Download script type %s failed...", scriptType);
120
120
  console.log(err);
121
121
  }
122
122
  }
@@ -224,9 +224,9 @@ async function checkScriptUpdates(args) {
224
224
  client
225
225
  );
226
226
 
227
- console.log("Downloaded %s (v%s)", scriptVer.scriptType, rcvdVersion);
227
+ console.log("Downloaded %s (v%s)", scriptType, rcvdVersion);
228
228
  } catch (err) {
229
- console.log("Download failed...");
229
+ console.log("Update script type %s failed...", scriptType);
230
230
  console.log(err);
231
231
  }
232
232
  }
@@ -262,7 +262,7 @@ async function checkScriptUpdates(args) {
262
262
  : 0;
263
263
  });
264
264
 
265
- console.log("\nFetching remote versions...");
265
+ console.log("\nCheck-Updates fetching remote versions...");
266
266
  const scriptsRemote = await listAvailableScripts([...args, "--quiet"]);
267
267
 
268
268
  for (const scriptVer of scriptsLocal.versions) {
@@ -294,7 +294,8 @@ async function checkScriptUpdates(args) {
294
294
  scriptVer.scriptType,
295
295
  undefined,
296
296
  company,
297
- client
297
+ client,
298
+ scriptsRemote
298
299
  );
299
300
 
300
301
  console.log(
@@ -303,7 +304,7 @@ async function checkScriptUpdates(args) {
303
304
  rcvdVersion
304
305
  );
305
306
  } catch (err) {
306
- console.log("Download failed...");
307
+ console.log("Auto update script type %s failed...", scriptType);
307
308
  console.log(err);
308
309
  }
309
310
  }
@@ -661,7 +661,7 @@ async function createOracleDatabaseConfiguration(args) {
661
661
 
662
662
  async function updateMonitoringConfiguration(args) {
663
663
  // Prep any arguments that are arrays
664
- args = setArgumentArrays(["--mailFrom", "--emailTo"], args);
664
+ args = setArgumentArrays(["--emailTo", "--notificationEmailTo"], args);
665
665
 
666
666
  callParams.id = monitoringConfig;
667
667
  for (let i = 0; i < args.length - 1; i++) {
@@ -700,6 +700,9 @@ async function updateMonitoringConfiguration(args) {
700
700
  case "--emailto":
701
701
  callParams.emailTo = args[++i];
702
702
  break;
703
+ case "--notificationemailto":
704
+ callParams.notificationEmailTo = args[++i];
705
+ break;
703
706
  case "--logfileretaindays":
704
707
  callParams.logFileRetainDays = args[++i];
705
708
  break;
@@ -1629,6 +1632,9 @@ async function printHelp() {
1629
1632
  console.log(
1630
1633
  "--emailTo (optional) [Use parameter multiple times to send multiple email addresses. Identifies the destination email addresses when file delivery is done through email.]"
1631
1634
  );
1635
+ console.log(
1636
+ "--notificationEmailTo (optional) [Use parameter multiple times to send multiple email addresses. Identifies the destination email addresses when notifications are sent.]"
1637
+ );
1632
1638
  console.log(
1633
1639
  "--logFileRetainDays (optional) [determines how many days to retain log files locally]"
1634
1640
  );
@@ -366,6 +366,41 @@ async function getLatestConfiguration(args) {
366
366
  console.log(JSON.stringify(latestConfiguration, null, 2));
367
367
  }
368
368
 
369
+ async function getOverprovisioned(args) {
370
+ for (let i = 0; i < args.length - 1; i++) {
371
+ switch (args[i].toLowerCase()) {
372
+ case "--company":
373
+ callParams.company = args[++i];
374
+ break;
375
+ }
376
+ }
377
+
378
+ const client = await getOpsCompassClient();
379
+ const resource = await client.resources.getOverprovisioned(callParams);
380
+ console.log(JSON.stringify(resource, null, 2));
381
+ }
382
+
383
+ async function getMetricCheckResults(args) {
384
+ for (let i = 0; i < args.length - 1; i++) {
385
+ switch (args[i].toLowerCase()) {
386
+ case "--company":
387
+ callParams.company = args[++i];
388
+ break;
389
+ case "--id":
390
+ callParams.id = args[++i];
391
+ break;
392
+ case "--resourceid":
393
+ case "--resource-id":
394
+ callParams.resourceId = args[++i];
395
+ break;
396
+ }
397
+ }
398
+
399
+ const client = await getOpsCompassClient();
400
+ const metricCheckResults = await client.resources.getMetricCheckResults(callParams);
401
+ console.log(JSON.stringify(metricCheckResults, null, 2));
402
+ }
403
+
369
404
  async function printHelp() {
370
405
  console.log("Actions available for resources:");
371
406
  console.log();
@@ -467,6 +502,16 @@ async function printHelp() {
467
502
  console.log("--id [resource id]");
468
503
  console.log("--resource-id [cloud-resource-id]");
469
504
  console.log();
505
+ console.log("get-overprovisioned", "Gets resources that have been marked as being overprovisioned.");
506
+ console.log("Parameters:");
507
+ console.log("--company [company_id]");
508
+ console.log();
509
+ console.log("get-metric-check-results", "Gets the metric check results for a resource.");
510
+ console.log("Parameters:");
511
+ console.log("--company [company_id]");
512
+ console.log("--id [resource id]");
513
+ console.log("--resource-id [cloud-resource-id]");
514
+ console.log();
470
515
  }
471
516
 
472
517
  const resourcesModule = (action, ...args) => {
@@ -493,6 +538,10 @@ const resourcesModule = (action, ...args) => {
493
538
  return uploadMetrics(args);
494
539
  case "get-latest-configuration":
495
540
  return getLatestConfiguration(args);
541
+ case "get-overprovisioned":
542
+ return getOverprovisioned(args);
543
+ case "get-metric-check-results":
544
+ return getMetricCheckResults(args);
496
545
  case "help":
497
546
  case "":
498
547
  return printHelp();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opscompass/opscompass-cli",
3
- "version": "1.0.25",
3
+ "version": "1.0.27",
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.25",
18
+ "@opscompass/opscompass-api": "^1.0.27",
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": {