@p0security/cli 0.3.0

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.
Files changed (103) hide show
  1. package/CONTRIBUTING.md +23 -0
  2. package/LICENSE.md +675 -0
  3. package/README.md +201 -0
  4. package/dist/commands/__tests__/login.test.d.ts +1 -0
  5. package/dist/commands/__tests__/login.test.js +75 -0
  6. package/dist/commands/__tests__/ls.test.d.ts +1 -0
  7. package/dist/commands/__tests__/ls.test.js +84 -0
  8. package/dist/commands/__tests__/request.test.d.ts +1 -0
  9. package/dist/commands/__tests__/request.test.js +94 -0
  10. package/dist/commands/__tests__/ssh.test.d.ts +1 -0
  11. package/dist/commands/__tests__/ssh.test.js +107 -0
  12. package/dist/commands/aws/__tests__/__input__/saml-response.d.ts +11 -0
  13. package/dist/commands/aws/__tests__/__input__/saml-response.js +18 -0
  14. package/dist/commands/aws/__tests__/__input__/sts-response.d.ts +11 -0
  15. package/dist/commands/aws/__tests__/__input__/sts-response.js +37 -0
  16. package/dist/commands/aws/__tests__/role.test.d.ts +1 -0
  17. package/dist/commands/aws/__tests__/role.test.js +98 -0
  18. package/dist/commands/aws/index.d.ts +4 -0
  19. package/dist/commands/aws/index.js +26 -0
  20. package/dist/commands/aws/role.d.ts +27 -0
  21. package/dist/commands/aws/role.js +123 -0
  22. package/dist/commands/index.d.ts +2 -0
  23. package/dist/commands/index.js +49 -0
  24. package/dist/commands/login.d.ts +14 -0
  25. package/dist/commands/login.js +93 -0
  26. package/dist/commands/ls.d.ts +4 -0
  27. package/dist/commands/ls.js +78 -0
  28. package/dist/commands/request.d.ts +12 -0
  29. package/dist/commands/request.js +116 -0
  30. package/dist/commands/ssh.d.ts +11 -0
  31. package/dist/commands/ssh.js +154 -0
  32. package/dist/common/auth/oidc.d.ts +4 -0
  33. package/dist/common/auth/oidc.js +18 -0
  34. package/dist/common/auth/server.d.ts +15 -0
  35. package/dist/common/auth/server.js +73 -0
  36. package/dist/common/fetch.d.ts +16 -0
  37. package/dist/common/fetch.js +39 -0
  38. package/dist/common/mime.d.ts +14 -0
  39. package/dist/common/mime.js +17 -0
  40. package/dist/common/xml.d.ts +21 -0
  41. package/dist/common/xml.js +52 -0
  42. package/dist/drivers/__mocks__/auth.d.ts +30 -0
  43. package/dist/drivers/__mocks__/auth.js +46 -0
  44. package/dist/drivers/api.d.ts +3 -0
  45. package/dist/drivers/api.js +69 -0
  46. package/dist/drivers/auth.d.ts +11 -0
  47. package/dist/drivers/auth.js +122 -0
  48. package/dist/drivers/env.d.ts +15 -0
  49. package/dist/drivers/env.js +38 -0
  50. package/dist/drivers/firestore.d.ts +10 -0
  51. package/dist/drivers/firestore.js +53 -0
  52. package/dist/drivers/stdio.d.ts +25 -0
  53. package/dist/drivers/stdio.js +44 -0
  54. package/dist/index.d.ts +1 -0
  55. package/dist/index.js +23 -0
  56. package/dist/middlewares/version.d.ts +8 -0
  57. package/dist/middlewares/version.js +77 -0
  58. package/dist/plugins/__mocks__/login.d.ts +14 -0
  59. package/dist/plugins/__mocks__/login.js +24 -0
  60. package/dist/plugins/aws/__mocks__/assumeRole.d.ts +12 -0
  61. package/dist/plugins/aws/__mocks__/assumeRole.js +20 -0
  62. package/dist/plugins/aws/api.d.ts +12 -0
  63. package/dist/plugins/aws/api.js +16 -0
  64. package/dist/plugins/aws/assumeRole.d.ts +14 -0
  65. package/dist/plugins/aws/assumeRole.js +55 -0
  66. package/dist/plugins/aws/config.d.ts +5 -0
  67. package/dist/plugins/aws/config.js +38 -0
  68. package/dist/plugins/aws/ssm/index.d.ts +18 -0
  69. package/dist/plugins/aws/ssm/index.js +274 -0
  70. package/dist/plugins/aws/ssm/install.d.ts +7 -0
  71. package/dist/plugins/aws/ssm/install.js +133 -0
  72. package/dist/plugins/aws/types.d.ts +54 -0
  73. package/dist/plugins/aws/types.js +2 -0
  74. package/dist/plugins/google/login.d.ts +2 -0
  75. package/dist/plugins/google/login.js +76 -0
  76. package/dist/plugins/login.d.ts +13 -0
  77. package/dist/plugins/login.js +19 -0
  78. package/dist/plugins/okta/aws.d.ts +5 -0
  79. package/dist/plugins/okta/aws.js +42 -0
  80. package/dist/plugins/okta/login.d.ts +8 -0
  81. package/dist/plugins/okta/login.js +165 -0
  82. package/dist/plugins/ssh/types.d.ts +22 -0
  83. package/dist/plugins/ssh/types.js +2 -0
  84. package/dist/public/favicon.ico +0 -0
  85. package/dist/public/redirect-landing.html +40 -0
  86. package/dist/testing/firestore.d.ts +2 -0
  87. package/dist/testing/firestore.js +16 -0
  88. package/dist/testing/yargs.d.ts +12 -0
  89. package/dist/testing/yargs.js +23 -0
  90. package/dist/types/identity.d.ts +23 -0
  91. package/dist/types/identity.js +2 -0
  92. package/dist/types/index.d.ts +11 -0
  93. package/dist/types/index.js +15 -0
  94. package/dist/types/oidc.d.ts +41 -0
  95. package/dist/types/oidc.js +2 -0
  96. package/dist/types/org.d.ts +20 -0
  97. package/dist/types/org.js +2 -0
  98. package/dist/types/request.d.ts +35 -0
  99. package/dist/types/request.js +20 -0
  100. package/dist/util.d.ts +42 -0
  101. package/dist/util.js +87 -0
  102. package/p0 +16 -0
  103. package/package.json +70 -0
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.fetchCommand = void 0;
36
+ /** Copyright © 2024-present P0 Security
37
+
38
+ This file is part of @p0security/cli
39
+
40
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
41
+
42
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
43
+
44
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
45
+ **/
46
+ const env_1 = require("../drivers/env");
47
+ const path = __importStar(require("node:path"));
48
+ const commandUrl = (tenant) => `${env_1.config.appUrl}/o/${tenant}/command/`;
49
+ const fetchCommand = (authn, args, argv) => __awaiter(void 0, void 0, void 0, function* () {
50
+ const token = yield authn.userCredential.user.getIdToken();
51
+ const response = yield fetch(commandUrl(authn.identity.org.slug), {
52
+ method: "POST",
53
+ headers: {
54
+ authorization: `Bearer ${token}`,
55
+ "Content-Type": "application/json",
56
+ },
57
+ body: JSON.stringify({
58
+ argv,
59
+ scriptName: path.basename(args.$0),
60
+ }),
61
+ });
62
+ const text = yield response.text();
63
+ const data = JSON.parse(text);
64
+ if ("error" in data) {
65
+ throw data.error;
66
+ }
67
+ return data;
68
+ });
69
+ exports.fetchCommand = fetchCommand;
@@ -0,0 +1,11 @@
1
+ import { Authn, Identity } from "../types/identity";
2
+ export declare const IDENTITY_FILE_PATH: string;
3
+ export declare const cached: <T>(name: string, loader: () => Promise<T>, options: {
4
+ duration: number;
5
+ }) => Promise<T>;
6
+ export declare const loadCredentials: (options?: {
7
+ noRefresh?: boolean;
8
+ }) => Promise<Identity>;
9
+ export declare const authenticate: (options?: {
10
+ noRefresh?: boolean;
11
+ }) => Promise<Authn>;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.authenticate = exports.loadCredentials = exports.cached = exports.IDENTITY_FILE_PATH = void 0;
36
+ /** Copyright © 2024-present P0 Security
37
+
38
+ This file is part of @p0security/cli
39
+
40
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
41
+
42
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
43
+
44
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
45
+ **/
46
+ const login_1 = require("../commands/login");
47
+ const util_1 = require("../util");
48
+ const firestore_1 = require("./firestore");
49
+ const stdio_1 = require("./stdio");
50
+ const auth_1 = require("firebase/auth");
51
+ const fs = __importStar(require("fs/promises"));
52
+ const path = __importStar(require("path"));
53
+ exports.IDENTITY_FILE_PATH = path.join(util_1.P0_PATH, "identity.json");
54
+ const cached = (name, loader, options) => __awaiter(void 0, void 0, void 0, function* () {
55
+ var _a;
56
+ const cachePath = path.join(path.dirname(exports.IDENTITY_FILE_PATH), "cache");
57
+ // Following lines sanitize input
58
+ // nosemgrep: javascript.lang.security.audit.path-traversal.path-join-resolve-traversal.path-join-resolve-traversal
59
+ const loc = path.resolve(path.join(cachePath, `${name}.json`));
60
+ if (!loc.startsWith(cachePath)) {
61
+ throw new Error("Illegal path traversal");
62
+ }
63
+ const loadCache = () => __awaiter(void 0, void 0, void 0, function* () {
64
+ const data = yield loader();
65
+ if (!data)
66
+ throw `Could not load credentials for "${name}"`;
67
+ yield fs.mkdir(path.dirname(loc), { recursive: true, mode: "700" });
68
+ yield fs.writeFile(loc, JSON.stringify(data), { mode: "600" });
69
+ return data;
70
+ });
71
+ try {
72
+ const stat = yield fs.stat(loc);
73
+ if (stat.mtime.getTime() < Date.now() - options.duration) {
74
+ yield fs.rm(loc);
75
+ return yield loadCache();
76
+ }
77
+ const data = yield fs.readFile(loc);
78
+ return JSON.parse(data.toString("utf-8"));
79
+ }
80
+ catch (error) {
81
+ if ((error === null || error === void 0 ? void 0 : error.code) !== "ENOENT")
82
+ (0, stdio_1.print2)(`Could not load credentials "${name}" from cache: ${(_a = error.message) !== null && _a !== void 0 ? _a : error}`);
83
+ return yield loadCache();
84
+ }
85
+ });
86
+ exports.cached = cached;
87
+ const loadCredentials = (options) => __awaiter(void 0, void 0, void 0, function* () {
88
+ try {
89
+ const buffer = yield fs.readFile(exports.IDENTITY_FILE_PATH);
90
+ const identity = JSON.parse(buffer.toString());
91
+ if (!(options === null || options === void 0 ? void 0 : options.noRefresh) &&
92
+ identity.credential.expires_at < Date.now() * 1e-3) {
93
+ yield (0, login_1.login)({ org: identity.org.slug }, { skipAuthenticate: true });
94
+ (0, stdio_1.print2)("\u200B"); // Force a new line
95
+ return (0, exports.loadCredentials)({ noRefresh: true });
96
+ }
97
+ return identity;
98
+ }
99
+ catch (error) {
100
+ if ((error === null || error === void 0 ? void 0 : error.code) === "ENOENT") {
101
+ throw "Please run `p0 login <organization>` to use the P0 CLI.";
102
+ }
103
+ throw error;
104
+ }
105
+ });
106
+ exports.loadCredentials = loadCredentials;
107
+ const authenticate = (options) => __awaiter(void 0, void 0, void 0, function* () {
108
+ const identity = yield (0, exports.loadCredentials)(options);
109
+ const { credential } = identity;
110
+ // TODO: Move to map lookup
111
+ const provider = new auth_1.OAuthProvider(identity.org.ssoProvider === "google"
112
+ ? auth_1.SignInMethod.GOOGLE
113
+ : identity.org.providerId);
114
+ const firebaseCredential = provider.credential({
115
+ accessToken: credential.access_token,
116
+ idToken: credential.id_token,
117
+ });
118
+ firestore_1.auth.tenantId = identity.org.tenantId;
119
+ const userCredential = yield (0, auth_1.signInWithCredential)(firestore_1.auth, firebaseCredential);
120
+ return { userCredential, identity };
121
+ });
122
+ exports.authenticate = authenticate;
@@ -0,0 +1,15 @@
1
+ export declare const config: {
2
+ fs: {
3
+ apiKey: string;
4
+ authDomain: string;
5
+ projectId: string;
6
+ storageBucket: string;
7
+ messagingSenderId: string;
8
+ appId: string;
9
+ };
10
+ google: {
11
+ clientId: string;
12
+ clientSecret: string;
13
+ };
14
+ appUrl: string;
15
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.config = void 0;
8
+ /** Copyright © 2024-present P0 Security
9
+
10
+ This file is part of @p0security/cli
11
+
12
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
13
+
14
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
17
+ **/
18
+ const dotenv_1 = __importDefault(require("dotenv"));
19
+ dotenv_1.default.config();
20
+ const { env } = process;
21
+ exports.config = {
22
+ fs: {
23
+ // Falls back to public production Firestore credentials
24
+ apiKey: (_a = env.P0_FS_API_KEY) !== null && _a !== void 0 ? _a : "AIzaSyCaL-Ik_l_5tdmgNUNZ4Nv6NuR4o5_PPfs",
25
+ authDomain: (_b = env.P0_FS_AUTH_DOMAIN) !== null && _b !== void 0 ? _b : "p0-prod.firebaseapp.com",
26
+ projectId: (_c = env.P0_FS_PROJECT_ID) !== null && _c !== void 0 ? _c : "p0-prod",
27
+ storageBucket: (_d = env.P0_FS_STORAGE_BUCKET) !== null && _d !== void 0 ? _d : "p0-prod.appspot.com",
28
+ messagingSenderId: (_e = env.P0_FS_MESSAGING_SENDER_ID) !== null && _e !== void 0 ? _e : "228132571547",
29
+ appId: (_f = env.P0_FS_APP_ID) !== null && _f !== void 0 ? _f : "1:228132571547:web:4da03aeb78add86fe6b93e",
30
+ },
31
+ google: {
32
+ clientId: (_g = env.P0_GOOGLE_OIDC_CLIENT_ID) !== null && _g !== void 0 ? _g : "228132571547-kilcq1er15hlbl6mitghttnacp7u58l8.apps.googleusercontent.com",
33
+ // Despite the name, this is not actually "secret" in any sense of the word.
34
+ // Instead, the client is protected by requiring PKCE and defining the redirect URIs.
35
+ clientSecret: (_h = env.P0_GOOGLE_OIDC_CLIENT_SECRET) !== null && _h !== void 0 ? _h : "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",
36
+ },
37
+ appUrl: (_j = env.P0_APP_URL) !== null && _j !== void 0 ? _j : "https://api.p0.app",
38
+ };
@@ -0,0 +1,10 @@
1
+ import { CollectionReference, DocumentReference } from "firebase/firestore";
2
+ export declare const FIRESTORE: import("@firebase/firestore").Firestore;
3
+ export declare const auth: import("@firebase/auth").Auth;
4
+ export declare const collection: <T>(path: string, ...pathSegments: string[]) => CollectionReference<T, import("@firebase/firestore").DocumentData>;
5
+ export declare const doc: <T>(path: string) => DocumentReference<T, import("@firebase/firestore").DocumentData>;
6
+ /** Ensures that Firestore is shutdown at command termination
7
+ *
8
+ * This prevents Firestore from holding the command on execution completion or failure.
9
+ */
10
+ export declare const guard: <P, T>(cb: (args: P) => Promise<T>) => (args: P) => Promise<void>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.guard = exports.doc = exports.collection = exports.auth = exports.FIRESTORE = void 0;
13
+ /** Copyright © 2024-present P0 Security
14
+
15
+ This file is part of @p0security/cli
16
+
17
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
18
+
19
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20
+
21
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
22
+ **/
23
+ const env_1 = require("./env");
24
+ const app_1 = require("firebase/app");
25
+ const auth_1 = require("firebase/auth");
26
+ const firestore_1 = require("firebase/firestore");
27
+ // Your web app's Firebase configuration
28
+ const firebaseConfig = env_1.config.fs;
29
+ // Initialize Firebase
30
+ const app = (0, app_1.initializeApp)(firebaseConfig);
31
+ exports.FIRESTORE = (0, firestore_1.getFirestore)(app);
32
+ exports.auth = (0, auth_1.getAuth)();
33
+ const collection = (path, ...pathSegments) => {
34
+ return (0, firestore_1.collection)(exports.FIRESTORE, path, ...pathSegments);
35
+ };
36
+ exports.collection = collection;
37
+ const doc = (path) => {
38
+ return (0, firestore_1.doc)(exports.FIRESTORE, path);
39
+ };
40
+ exports.doc = doc;
41
+ /** Ensures that Firestore is shutdown at command termination
42
+ *
43
+ * This prevents Firestore from holding the command on execution completion or failure.
44
+ */
45
+ const guard = (cb) => (args) => __awaiter(void 0, void 0, void 0, function* () {
46
+ try {
47
+ yield cb(args);
48
+ }
49
+ finally {
50
+ void (0, firestore_1.terminate)(exports.FIRESTORE);
51
+ }
52
+ });
53
+ exports.guard = guard;
@@ -0,0 +1,25 @@
1
+ /** Copyright © 2024-present P0 Security
2
+
3
+ This file is part of @p0security/cli
4
+
5
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
6
+
7
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
+
9
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
10
+ **/
11
+ /** Used to output machine-readable text to stdout
12
+ *
13
+ * In general this should not be used for text meant to be consumed
14
+ * only by humans.
15
+ */
16
+ export declare function print1(message: any): void;
17
+ /** Output human-consumable text to stderr
18
+ *
19
+ * In general this should not be used for machine-consumed text.
20
+ */
21
+ export declare function print2(message: any): void;
22
+ export declare const Ansi: {
23
+ readonly Reset: string;
24
+ readonly Dim: string;
25
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /** Copyright © 2024-present P0 Security
3
+
4
+ This file is part of @p0security/cli
5
+
6
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
7
+
8
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9
+
10
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
11
+ **/
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.Ansi = exports.print2 = exports.print1 = void 0;
14
+ /** Functions to handle stdio
15
+ *
16
+ * These are essentially wrappers around console.foo, but allow for
17
+ * - Better testing
18
+ * - Later redirection / duplication
19
+ */
20
+ const lodash_1 = require("lodash");
21
+ /** Used to output machine-readable text to stdout
22
+ *
23
+ * In general this should not be used for text meant to be consumed
24
+ * only by humans.
25
+ */
26
+ function print1(message) {
27
+ // eslint-disable-next-line no-console
28
+ console.log(message);
29
+ }
30
+ exports.print1 = print1;
31
+ /** Output human-consumable text to stderr
32
+ *
33
+ * In general this should not be used for machine-consumed text.
34
+ */
35
+ function print2(message) {
36
+ // eslint-disable-next-line no-console
37
+ console.error(message);
38
+ }
39
+ exports.print2 = print2;
40
+ const AnsiCodes = {
41
+ Reset: "00",
42
+ Dim: "02",
43
+ };
44
+ exports.Ansi = (0, lodash_1.mapValues)(AnsiCodes, (v) => `\u001b[${v}m`);
@@ -0,0 +1 @@
1
+ export declare const main: () => void;
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.main = void 0;
4
+ /** Copyright © 2024-present P0 Security
5
+
6
+ This file is part of @p0security/cli
7
+
8
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
9
+
10
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
+
12
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
13
+ **/
14
+ const commands_1 = require("./commands");
15
+ const lodash_1 = require("lodash");
16
+ const main = () => {
17
+ // We can suppress output here, as .fail() already print1 errors
18
+ void commands_1.cli.parse().catch(lodash_1.noop);
19
+ };
20
+ exports.main = main;
21
+ if (require.main === module) {
22
+ (0, exports.main)();
23
+ }
@@ -0,0 +1,8 @@
1
+ import yargs from "yargs";
2
+ /** Checks if there is a new version of the CLI
3
+ *
4
+ * If there is, prints an upgrade banner.
5
+ *
6
+ * If there is no new version, or if version lookup errors, just pass silently.
7
+ */
8
+ export declare const checkVersion: (_yargs: yargs.ArgumentsCamelCase) => Promise<void>;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.checkVersion = void 0;
16
+ /** Copyright © 2024-present P0 Security
17
+
18
+ This file is part of @p0security/cli
19
+
20
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
21
+
22
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
25
+ **/
26
+ const stdio_1 = require("../drivers/stdio");
27
+ const util_1 = require("../util");
28
+ const promises_1 = __importDefault(require("node:fs/promises"));
29
+ const node_path_1 = __importDefault(require("node:path"));
30
+ const semver_1 = __importDefault(require("semver"));
31
+ const LATEST_VERSION_FILE = "last-version-check";
32
+ // We don't want to add any significant overhead to p0 commands with the version check,
33
+ // so just give up if it takes too long.
34
+ const VERSION_CHECK_TIMEOUT_MILLIS = 1e3;
35
+ const VERSION_CHECK_INTERVAL_MILLIS = 86400e3; // 1 day
36
+ /** Checks if there is a new version of the CLI
37
+ *
38
+ * If there is, prints an upgrade banner.
39
+ *
40
+ * If there is no new version, or if version lookup errors, just pass silently.
41
+ */
42
+ const checkVersion = (_yargs) => __awaiter(void 0, void 0, void 0, function* () {
43
+ try {
44
+ const latestFile = node_path_1.default.join(util_1.P0_PATH, LATEST_VERSION_FILE);
45
+ try {
46
+ const stat = yield promises_1.default.stat(latestFile);
47
+ if (Date.now() - stat.mtime.getTime() <= VERSION_CHECK_INTERVAL_MILLIS)
48
+ return;
49
+ }
50
+ catch (error) {
51
+ if (error.code !== "ENOENT")
52
+ throw error;
53
+ }
54
+ // Write the version-check file first to avoid retrying errors
55
+ yield promises_1.default.writeFile(latestFile, "");
56
+ // Note that package.json is installed one level above "dist"
57
+ // We can't require package.json as it is outside the TypeScript root
58
+ const { name, version } = JSON.parse((yield promises_1.default.readFile(`${__dirname}/../../package.json`)).toString("utf-8"));
59
+ const npmResult = (0, util_1.exec)("npm", ["view", name, "--json"], { check: true });
60
+ const npmPackage = yield (0, util_1.timeout)(npmResult, VERSION_CHECK_TIMEOUT_MILLIS);
61
+ const { "dist-tags": { latest }, } = JSON.parse(npmPackage.stdout);
62
+ if (semver_1.default.lt(version, latest)) {
63
+ (0, stdio_1.print2)(`╔══════════════════════════════════════╗
64
+ ║ A new version of P0 CLI is available ║
65
+ ║ ║
66
+ ║ To install, run ║
67
+ ║ npm -g update ${name.padEnd(20)} ║
68
+ ╚══════════════════════════════════════╝
69
+ `);
70
+ }
71
+ }
72
+ catch (error) {
73
+ // Silently pass errors
74
+ // TODO: log to ~/.p0
75
+ }
76
+ });
77
+ exports.checkVersion = checkVersion;
@@ -0,0 +1,14 @@
1
+ /// <reference types="jest" />
2
+ /** Copyright © 2024-present P0 Security
3
+
4
+ This file is part of @p0security/cli
5
+
6
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
7
+
8
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9
+
10
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
11
+ **/
12
+ export declare const pluginLoginMap: {
13
+ google: jest.Mock<any, any, any>;
14
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pluginLoginMap = void 0;
4
+ /** Copyright © 2024-present P0 Security
5
+
6
+ This file is part of @p0security/cli
7
+
8
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
9
+
10
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
+
12
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
13
+ **/
14
+ exports.pluginLoginMap = {
15
+ google: jest.fn().mockResolvedValue({
16
+ access_token: "test-access-token",
17
+ id_token: "test-id-token",
18
+ token_type: "oidc",
19
+ scope: "oidc",
20
+ expires_in: 3600,
21
+ refresh_token: "test-refresh-token",
22
+ device_secret: "test-device-secret",
23
+ }),
24
+ };
@@ -0,0 +1,12 @@
1
+ /** Copyright © 2024-present P0 Security
2
+
3
+ This file is part of @p0security/cli
4
+
5
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
6
+
7
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
+
9
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
10
+ **/
11
+ import { AwsCredentials } from "../types";
12
+ export declare const assumeRoleWithSaml: () => Promise<AwsCredentials>;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.assumeRoleWithSaml = void 0;
13
+ const assumeRoleWithSaml = () => __awaiter(void 0, void 0, void 0, function* () {
14
+ return ({
15
+ AWS_ACCESS_KEY_ID: "test-access-key-id",
16
+ AWS_SECRET_ACCESS_KEY: "test-secret-access-key",
17
+ AWS_SESSION_TOKEN: "test-session-token",
18
+ });
19
+ });
20
+ exports.assumeRoleWithSaml = assumeRoleWithSaml;
@@ -0,0 +1,12 @@
1
+ /** Copyright © 2024-present P0 Security
2
+
3
+ This file is part of @p0security/cli
4
+
5
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
6
+
7
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
+
9
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
10
+ **/
11
+ export declare const AWS_API_VERSION = "2011-06-15";
12
+ export declare const arnPrefix: (account: string) => string;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.arnPrefix = exports.AWS_API_VERSION = void 0;
4
+ /** Copyright © 2024-present P0 Security
5
+
6
+ This file is part of @p0security/cli
7
+
8
+ @p0security/cli is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.
9
+
10
+ @p0security/cli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
+
12
+ You should have received a copy of the GNU General Public License along with @p0security/cli. If not, see <https://www.gnu.org/licenses/>.
13
+ **/
14
+ exports.AWS_API_VERSION = "2011-06-15";
15
+ const arnPrefix = (account) => `arn:aws:iam::${account}`;
16
+ exports.arnPrefix = arnPrefix;
@@ -0,0 +1,14 @@
1
+ import { AwsCredentials } from "./types";
2
+ /** Assumes an AWS role via SAML login */
3
+ export declare const assumeRoleWithSaml: (args: {
4
+ /** An AWS account identifier */
5
+ account: string;
6
+ /** The account-specific role name requested */
7
+ role: string;
8
+ saml: {
9
+ /** The SAML Identity Provider name in AWS IAM */
10
+ providerName: string;
11
+ /** A base64-encoded SAML response document */
12
+ response: string;
13
+ };
14
+ }) => Promise<AwsCredentials>;