@opscompass/opscompass-cli 1.0.25 → 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.
@@ -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,12 @@ 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
+ // returns false and continues otherwise
206
+ scriptDownloadInfo.filePath = path.join(scriptTypeFolderPath, fileName);
207
+ await pgp.Verify(scriptDownloadInfo);
208
+
195
209
  await unzipFile(scriptTypeFolderPath, fileName);
196
210
 
197
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
+ };
@@ -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(
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.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.25",
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": {