@opscompass/opscompass-cli 1.0.44 → 1.0.46
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 +22 -1
- package/lib/pgpTools.js +14 -20
- package/package.json +3 -3
- package/updates.js +51 -11
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,28 @@ const path = require("path");
|
|
|
11
10
|
|
|
12
11
|
let defaultParams = JSON.parse("{}");
|
|
13
12
|
|
|
13
|
+
// Make sure opscompassHome exists
|
|
14
|
+
try {
|
|
15
|
+
if (!fs.existsSync(opscompassHome)) {
|
|
16
|
+
fs.mkdirSync(opscompassHome, { recursive: true });
|
|
17
|
+
}
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(
|
|
20
|
+
`Error: Unable to create Opscompass home directory at ${opscompassHome}: ${error.message}`
|
|
21
|
+
);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// If an offline file exists in ~/.opscompass, set the flag to true
|
|
26
|
+
let isOffline = false;
|
|
27
|
+
const offlinePath = path.join(opscompassHome, ".offline");
|
|
28
|
+
if (fs.existsSync(offlinePath)) {
|
|
29
|
+
isOffline = true;
|
|
30
|
+
console.error(" NOTE: Using CLI in offline mode");
|
|
31
|
+
}
|
|
32
|
+
global.isOffline = isOffline;
|
|
33
|
+
const { versionCheck } = require("./updates.js");
|
|
34
|
+
|
|
14
35
|
function checkDefaults() {
|
|
15
36
|
const defaultsPath = path.join(opscompassHome, "defaults.json");
|
|
16
37
|
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
|
|
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
|
-
|
|
29
|
+
let publicKeyArmored = undefined;
|
|
28
30
|
const gpgSigner =
|
|
29
31
|
scriptDownloadInfo?.gpgSigner || "cli-token@opscompass.com";
|
|
30
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
36
|
+
);
|
|
37
|
+
publicKeyArmored = response?.data;
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("Key error:", error.message || error);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
41
42
|
|
|
42
|
-
|
|
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
|
-
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opscompass/opscompass-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.46",
|
|
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.
|
|
19
|
+
"@opscompass/opscompass-api": "^1.0.46",
|
|
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": "^
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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(
|
|
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
|
};
|