@opscompass/opscompass-cli 1.0.43 → 1.0.45

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
@@ -3,7 +3,6 @@
3
3
  const modules = require("./modules.js");
4
4
  const handleError = require("./error.js");
5
5
  const { canReadStoredCredentials } = require("./credentials.js");
6
- const { versionCheck } = require("./updates.js");
7
6
  const { isLocal, opscompassHome } = require("./environment/current.js");
8
7
 
9
8
  const fs = require("fs");
@@ -11,6 +10,16 @@ const path = require("path");
11
10
 
12
11
  let defaultParams = JSON.parse("{}");
13
12
 
13
+ // If an offline file exists in ~/.opscompass, set the flag to true
14
+ let isOffline = false;
15
+ const offlinePath = path.join(opscompassHome, ".offline");
16
+ if (fs.existsSync(offlinePath)) {
17
+ isOffline = true;
18
+ console.error(" NOTE: Using CLI in offline mode");
19
+ }
20
+ global.isOffline = isOffline;
21
+ const { versionCheck } = require("./updates.js");
22
+
14
23
  function checkDefaults() {
15
24
  const defaultsPath = path.join(opscompassHome, "defaults.json");
16
25
  let rawdata;
package/lib/pgpTools.js CHANGED
@@ -8,12 +8,14 @@ const axios = require("axios");
8
8
  let publicKey = undefined;
9
9
 
10
10
  async function Verify(scriptDownloadInfo) {
11
+ // Dynamically import openpgp v6
12
+
11
13
  // If the download info includes a gpg signature, verify it
12
14
  if (scriptDownloadInfo?.gpgSignature) {
13
15
  // Create a readable stream for the archive file
14
16
  let readableStream = undefined;
15
17
  if (fs.existsSync(scriptDownloadInfo.filePath)) {
16
- // This seems to break openpgp.js if the file is > 128kb
18
+ // This might break if the file is > 128kb
17
19
  // readableStream = fs.createReadStream(scriptDownloadInfo.filePath);
18
20
  readableStream = fs.readFileSync(scriptDownloadInfo.filePath);
19
21
  } else {
@@ -24,26 +26,21 @@ async function Verify(scriptDownloadInfo) {
24
26
  // Run asynchronously to get the public key, if we don't already have it
25
27
  if (!publicKey) {
26
28
  // If signer is not included, assume cli-token@opscompass.com
27
- var publicKeyArmored = undefined;
29
+ let publicKeyArmored = undefined;
28
30
  const gpgSigner =
29
31
  scriptDownloadInfo?.gpgSigner || "cli-token@opscompass.com";
30
- const getPGP = axios
31
- .get(
32
+ try {
33
+ const response = await axios.get(
32
34
  "https://keys.openpgp.org/pks/lookup?op=get&options=mr&search=" +
33
35
  gpgSigner
34
- )
35
- .then(function (response) {
36
- publicKeyArmored = response?.data;
37
- })
38
- .catch(function (error) {
39
- console.error("Key error:", error.message);
40
- });
36
+ );
37
+ publicKeyArmored = response?.data;
38
+ } catch (error) {
39
+ console.error("Key error:", error.message || error);
40
+ return false;
41
+ }
41
42
 
42
- // Wait for API response from public key repo
43
- await getPGP;
44
- if (!!publicKeyArmored) {
45
- //console.log(publicKeyArmored);
46
- } else {
43
+ if (!publicKeyArmored) {
47
44
  console.error(
48
45
  "Validation error retrieving public key for: %s",
49
46
  gpgSigner
@@ -75,7 +72,6 @@ async function Verify(scriptDownloadInfo) {
75
72
  console.error("ERROR:", JSON.stringify(err, null, 2));
76
73
  return false;
77
74
  }
78
- // Signature is valid
79
75
 
80
76
  // And, finally - verify the signature and document
81
77
  const message = await openpgp.createMessage({ binary: readableStream });
@@ -86,9 +82,7 @@ async function Verify(scriptDownloadInfo) {
86
82
  });
87
83
  const { verified, keyID } = verificationResult.signatures[0];
88
84
  try {
89
- // console.log("Awaiting verify...", verified);
90
- await verified; // throws on invalid signature
91
- // console.log("VERIFIED: Signed by key id " + keyID.toHex());
85
+ await verified;
92
86
  } catch (e) {
93
87
  console.log("Archive file verification failed:\n" + e.message);
94
88
  return false;
package/lib/utils.js CHANGED
@@ -24,6 +24,7 @@ const findRemoveSync = require("find-remove");
24
24
  const HOBKEY = Buffer.from(`${decoder}=`, "base64").toString();
25
25
  const hashPrefix = "oc$hash:"; // Prefix for encrypted values
26
26
  const hashRegex = /^oc\$hash:/; // Regex for encrypted values
27
+ const obfuscatedRegex = /^oc\$(hash|mask)/; // Regex for encrypted or masked values
27
28
 
28
29
  // Test for any null or undefined objects
29
30
  function hasNullOrUndefined(obj, count = Object.keys(obj).length) {
@@ -263,7 +264,7 @@ function getConfigFilePath(args = []) {
263
264
  * Function read a configuration file
264
265
  * @param {string}[] args - license-manager arguments
265
266
  */
266
- function readConfigFile(args) {
267
+ async function readConfigFile(args) {
267
268
  const cfgFile = getConfigFilePath(args);
268
269
  if (!cfgFile) return undefined;
269
270
 
@@ -279,6 +280,10 @@ function readConfigFile(args) {
279
280
  }
280
281
 
281
282
  decryptValues(cfgFile);
283
+ // Also unmask the cfg file here - in case there are masked values
284
+ const { unmaskObjValues } = require("./maskValues");
285
+ await unmaskObjValues(cfgFile);
286
+
282
287
  return cfgFile;
283
288
  }
284
289
 
@@ -602,8 +607,8 @@ function encryptValues(config) {
602
607
  for (const resource of config[resType]) {
603
608
  for (const attr of Object.keys(encryptAttrs)) {
604
609
  if (resource.hasOwnProperty(attr) && resource[attr] !== null) {
605
- // If this is a sensitive value and it is not already encrypted
606
- if (!resource[attr].match(hashRegex)) {
610
+ // If this is a sensitive value and it is not already obfuscated
611
+ if (!resource[attr].match(obfuscatedRegex)) {
607
612
  resource[attr] = encryptAES(resource[attr]);
608
613
  // console.error("Encrypted attribute %s: %s ", attr, resource[attr]);
609
614
  }
@@ -356,7 +356,7 @@ async function readMonitoringConfiguration(args) {
356
356
  } else console.log("undefined");
357
357
  return;
358
358
  }
359
- const monitoringConfig = utils.readConfigFile(args);
359
+ const monitoringConfig = await utils.readConfigFile(args);
360
360
  if (monitoringConfig) {
361
361
  console.log(JSON.stringify(monitoringConfig, null, 2));
362
362
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opscompass/opscompass-cli",
3
- "version": "1.0.43",
3
+ "version": "1.0.45",
4
4
  "description": "Calls OpsCompass's APIs",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@
16
16
  "author": "opscompass",
17
17
  "license": "SEE LICENSE IN license.txt",
18
18
  "dependencies": {
19
- "@opscompass/opscompass-api": "^1.0.42",
19
+ "@opscompass/opscompass-api": "^1.0.45",
20
20
  "adm-zip": "^0.5.16",
21
21
  "axios": "^0.28.1",
22
22
  "better-sqlite3": "^11.8.0",
@@ -24,7 +24,7 @@
24
24
  "fs-extra": "^10.1.0",
25
25
  "https-proxy-agent": "^5.0.1",
26
26
  "open": "^8.2.1",
27
- "openpgp": "^5.11.0",
27
+ "openpgp": "^6.2.2",
28
28
  "semver": "^7.3.7"
29
29
  },
30
30
  "bin": {
package/updates.js CHANGED
@@ -2,30 +2,70 @@ const pkg = require("./package.json");
2
2
  const semver = require("semver");
3
3
  const { exec } = require("child_process");
4
4
  const { greenText, yellowText } = require("./colors.js");
5
+ const { opscompassHome } = require("./environment/current.js");
6
+ const fs = require("fs");
7
+ const path = require("path");
5
8
 
6
9
  /* versionCheck sends a command to the terminal to check the latest package version and compares it to the current version specified in package.json, prompting the user to upgrade if it is out of date. */
7
10
  /* This is turned off by default when using the local environment. To turn it on for dev purposes, comment out isLocal check in index.js */
8
11
 
9
12
  function versionCheck() {
13
+ if (global.isOffline) {
14
+ return;
15
+ }
16
+ const checkPath = path.join(opscompassHome, ".versioncheck");
10
17
 
11
- const environmentSpecificPackageVersion = `npm view ${pkg.name} version`
12
- /* latestVersion is the standard output(stdout), which is read and compared to current package version */
13
- exec(environmentSpecificPackageVersion, (error, latestVersion, stderr) => {
18
+ // If .versioncheck exists and is less than 24 hours old, return
19
+ if (fs.existsSync(checkPath)) {
20
+ const stats = fs.statSync(checkPath);
21
+ const now = Date.now();
22
+ const mtime = new Date(stats.mtime).getTime();
23
+ const hours24 = 24 * 60 * 60 * 1000;
24
+ if (now - mtime < hours24) {
25
+ return;
26
+ }
27
+ }
28
+
29
+ const environmentSpecificPackageVersion = `npm view ${pkg.name} version`;
30
+ /* latestVersion is the standard output(stdout), which is read and compared to current package version */
31
+ exec(
32
+ environmentSpecificPackageVersion,
33
+ { timeout: 5000 },
34
+ (error, latestVersion, stderr) => {
14
35
  if (error) {
15
- console.log(`error: ${error.message}`);
36
+ if (error.killed) {
37
+ console.log("Version check error: timed out.");
38
+ } else {
39
+ console.log(`Version check error: ${error.message}`);
40
+ }
16
41
  return;
17
42
  }
18
43
 
19
- /* Semver used to semantically compare version numbers */
20
- if (semver.gt(latestVersion, pkg.version)) {
21
- console.error(yellowText, `Warning! You are using CLI version ${pkg.version}, but the latest version is ${latestVersion}To upgrade to the latest version, please run:`);
44
+ // Update the timestamp of .versioncheck file after successful check
45
+ try {
46
+ fs.utimesSync(checkPath, new Date(), new Date());
47
+ } catch (err) {
48
+ // If .versioncheck doesn't exist, create it
49
+ fs.closeSync(fs.openSync(checkPath, "w"));
50
+ }
51
+
52
+ /* Semver used to semantically compare version numbers */
53
+ if (semver.gt(latestVersion, pkg.version)) {
54
+ console.error(
55
+ yellowText,
56
+ `Warning! You are using CLI version ${pkg.version}, but the latest version is ${latestVersion}To upgrade to the latest version, please run:`
57
+ );
22
58
  /* Prompt user to upgrade to the correct package based on their current environment */
23
- console.error(greenText, `\nnpm install -g ${pkg.name}@${latestVersion}\n`);
59
+ console.error(
60
+ greenText,
61
+ `\nnpm install -g ${pkg.name}@${latestVersion}\n`
62
+ );
24
63
  return;
25
64
  }
26
- });
27
- };
65
+ }
66
+ );
67
+ }
28
68
 
29
69
  module.exports = {
30
- versionCheck
70
+ versionCheck,
31
71
  };