@vercel/sandbox 2.0.0-beta.10 → 2.0.0-beta.12
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/README.md +8 -0
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/api-client/api-client.cjs +456 -0
- package/dist/api-client/api-client.cjs.map +1 -0
- package/dist/api-client/api-client.d.cts +788 -0
- package/dist/api-client/api-client.d.ts +782 -777
- package/dist/api-client/api-client.js +445 -471
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/api-client/api-error.cjs +32 -0
- package/dist/api-client/api-error.cjs.map +1 -0
- package/dist/api-client/api-error.d.cts +29 -0
- package/dist/api-client/api-error.d.ts +21 -18
- package/dist/api-client/api-error.js +28 -33
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.cjs +125 -0
- package/dist/api-client/base-client.cjs.map +1 -0
- package/dist/api-client/base-client.d.cts +38 -0
- package/dist/api-client/base-client.d.ts +31 -36
- package/dist/api-client/base-client.js +110 -130
- package/dist/api-client/base-client.js.map +1 -1
- package/dist/api-client/file-writer.cjs +62 -0
- package/dist/api-client/file-writer.cjs.map +1 -0
- package/dist/api-client/file-writer.d.cts +66 -0
- package/dist/api-client/file-writer.d.ts +56 -52
- package/dist/api-client/file-writer.js +57 -61
- package/dist/api-client/file-writer.js.map +1 -1
- package/dist/api-client/index.cjs +2 -0
- package/dist/api-client/index.d.ts +2 -2
- package/dist/api-client/index.js +4 -21
- package/dist/api-client/validators.cjs +181 -0
- package/dist/api-client/validators.cjs.map +1 -0
- package/dist/api-client/validators.d.cts +1469 -0
- package/dist/api-client/validators.d.ts +1282 -3699
- package/dist/api-client/validators.js +154 -170
- package/dist/api-client/validators.js.map +1 -1
- package/dist/api-client/with-retry.cjs +89 -0
- package/dist/api-client/with-retry.cjs.map +1 -0
- package/dist/api-client/with-retry.d.cts +10 -0
- package/dist/api-client/with-retry.d.ts +9 -13
- package/dist/api-client/with-retry.js +81 -102
- package/dist/api-client/with-retry.js.map +1 -1
- package/dist/auth/api.cjs +29 -0
- package/dist/auth/api.cjs.map +1 -0
- package/dist/auth/api.js +26 -25
- package/dist/auth/api.js.map +1 -1
- package/dist/auth/error.cjs +13 -0
- package/dist/auth/error.cjs.map +1 -0
- package/dist/auth/error.js +11 -11
- package/dist/auth/error.js.map +1 -1
- package/dist/auth/file.cjs +64 -0
- package/dist/auth/file.cjs.map +1 -0
- package/dist/auth/file.d.cts +26 -0
- package/dist/auth/file.d.ts +19 -15
- package/dist/auth/file.js +49 -64
- package/dist/auth/file.js.map +1 -1
- package/dist/auth/index.cjs +12 -0
- package/dist/auth/index.d.cts +5 -0
- package/dist/auth/index.d.ts +5 -6
- package/dist/auth/index.js +6 -27
- package/dist/auth/linked-project.cjs +38 -0
- package/dist/auth/linked-project.cjs.map +1 -0
- package/dist/auth/linked-project.js +30 -64
- package/dist/auth/linked-project.js.map +1 -1
- package/dist/auth/oauth.cjs +205 -0
- package/dist/auth/oauth.cjs.map +1 -0
- package/dist/auth/oauth.d.cts +135 -0
- package/dist/auth/oauth.d.ts +113 -109
- package/dist/auth/oauth.js +185 -252
- package/dist/auth/oauth.js.map +1 -1
- package/dist/auth/poll-for-token.cjs +82 -0
- package/dist/auth/poll-for-token.cjs.map +1 -0
- package/dist/auth/poll-for-token.d.cts +28 -0
- package/dist/auth/poll-for-token.d.ts +23 -15
- package/dist/auth/poll-for-token.js +79 -64
- package/dist/auth/poll-for-token.js.map +1 -1
- package/dist/auth/project.cjs +80 -0
- package/dist/auth/project.cjs.map +1 -0
- package/dist/auth/project.d.cts +44 -0
- package/dist/auth/project.d.ts +12 -8
- package/dist/auth/project.js +70 -72
- package/dist/auth/project.js.map +1 -1
- package/dist/auth/zod.cjs +22 -0
- package/dist/auth/zod.cjs.map +1 -0
- package/dist/auth/zod.js +18 -17
- package/dist/auth/zod.js.map +1 -1
- package/dist/command.cjs +328 -0
- package/dist/command.cjs.map +1 -0
- package/dist/command.d.cts +289 -0
- package/dist/command.d.ts +265 -171
- package/dist/command.js +323 -226
- package/dist/command.js.map +1 -1
- package/dist/constants.d.cts +5 -0
- package/dist/constants.d.ts +5 -1
- package/dist/index.cjs +13 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +7 -17
- package/dist/network-policy.d.cts +100 -0
- package/dist/network-policy.d.ts +32 -28
- package/dist/sandbox.cjs +688 -0
- package/dist/sandbox.cjs.map +1 -0
- package/dist/sandbox.d.cts +780 -0
- package/dist/sandbox.d.ts +767 -711
- package/dist/sandbox.js +680 -612
- package/dist/sandbox.js.map +1 -1
- package/dist/session.cjs +520 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +406 -0
- package/dist/session.d.ts +398 -367
- package/dist/session.js +517 -505
- package/dist/session.js.map +1 -1
- package/dist/snapshot.cjs +116 -0
- package/dist/snapshot.cjs.map +1 -0
- package/dist/snapshot.d.cts +107 -0
- package/dist/snapshot.d.ts +98 -91
- package/dist/snapshot.js +114 -115
- package/dist/snapshot.js.map +1 -1
- package/dist/utils/array.cjs +17 -0
- package/dist/utils/array.cjs.map +1 -0
- package/dist/utils/array.js +12 -15
- package/dist/utils/array.js.map +1 -1
- package/dist/utils/consume-readable.cjs +18 -0
- package/dist/utils/consume-readable.cjs.map +1 -0
- package/dist/utils/consume-readable.js +13 -12
- package/dist/utils/consume-readable.js.map +1 -1
- package/dist/utils/decode-base64-url.cjs +15 -0
- package/dist/utils/decode-base64-url.cjs.map +1 -0
- package/dist/utils/decode-base64-url.js +10 -9
- package/dist/utils/decode-base64-url.js.map +1 -1
- package/dist/utils/dev-credentials.cjs +142 -0
- package/dist/utils/dev-credentials.cjs.map +1 -0
- package/dist/utils/dev-credentials.js +126 -184
- package/dist/utils/dev-credentials.js.map +1 -1
- package/dist/utils/get-credentials.cjs +123 -0
- package/dist/utils/get-credentials.cjs.map +1 -0
- package/dist/utils/get-credentials.d.cts +21 -0
- package/dist/utils/get-credentials.d.ts +19 -61
- package/dist/utils/get-credentials.js +106 -140
- package/dist/utils/get-credentials.js.map +1 -1
- package/dist/utils/log.cjs +25 -0
- package/dist/utils/log.cjs.map +1 -0
- package/dist/utils/log.js +15 -17
- package/dist/utils/log.js.map +1 -1
- package/dist/utils/network-policy.cjs +65 -0
- package/dist/utils/network-policy.cjs.map +1 -0
- package/dist/utils/network-policy.js +58 -77
- package/dist/utils/network-policy.js.map +1 -1
- package/dist/utils/normalizePath.cjs +27 -0
- package/dist/utils/normalizePath.cjs.map +1 -0
- package/dist/utils/normalizePath.js +21 -28
- package/dist/utils/normalizePath.js.map +1 -1
- package/dist/utils/resolveSignal.cjs +20 -0
- package/dist/utils/resolveSignal.cjs.map +1 -0
- package/dist/utils/resolveSignal.d.cts +15 -0
- package/dist/utils/resolveSignal.d.ts +12 -10
- package/dist/utils/resolveSignal.js +14 -17
- package/dist/utils/resolveSignal.js.map +1 -1
- package/dist/utils/sandbox-snapshot.cjs +14 -0
- package/dist/utils/sandbox-snapshot.cjs.map +1 -0
- package/dist/utils/sandbox-snapshot.d.cts +10 -0
- package/dist/utils/sandbox-snapshot.d.ts +11 -0
- package/dist/utils/sandbox-snapshot.js +14 -0
- package/dist/utils/sandbox-snapshot.js.map +1 -0
- package/dist/utils/types.cjs +13 -0
- package/dist/utils/types.cjs.map +1 -0
- package/dist/utils/types.d.cts +11 -0
- package/dist/utils/types.d.ts +5 -7
- package/dist/utils/types.js +8 -8
- package/dist/utils/types.js.map +1 -1
- package/dist/version.cjs +7 -0
- package/dist/version.cjs.map +1 -0
- package/dist/version.js +5 -5
- package/dist/version.js.map +1 -1
- package/package.json +23 -3
- package/dist/api-client/index.js.map +0 -1
- package/dist/auth/api.d.ts +0 -6
- package/dist/auth/error.d.ts +0 -11
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/linked-project.d.ts +0 -10
- package/dist/auth/zod.d.ts +0 -5
- package/dist/constants.js +0 -3
- package/dist/constants.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/network-policy.js +0 -3
- package/dist/network-policy.js.map +0 -1
- package/dist/utils/array.d.ts +0 -9
- package/dist/utils/consume-readable.d.ts +0 -5
- package/dist/utils/convert-sandbox.d.ts +0 -6
- package/dist/utils/convert-sandbox.js +0 -14
- package/dist/utils/convert-sandbox.js.map +0 -1
- package/dist/utils/decode-base64-url.d.ts +0 -7
- package/dist/utils/dev-credentials.d.ts +0 -37
- package/dist/utils/log.d.ts +0 -2
- package/dist/utils/network-policy.d.ts +0 -7
- package/dist/utils/normalizePath.d.ts +0 -17
- package/dist/version.d.ts +0 -1
|
@@ -1,156 +1,122 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const zod_1 = require("zod");
|
|
8
|
-
const dev_credentials_1 = require("./dev-credentials");
|
|
1
|
+
import { decodeBase64Url } from "./decode-base64-url.js";
|
|
2
|
+
import { cachedGenerateCredentials, shouldPromptForCredentials } from "./dev-credentials.js";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { getVercelOidcToken } from "@vercel/oidc";
|
|
5
|
+
|
|
6
|
+
//#region src/utils/get-credentials.ts
|
|
9
7
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
exports.LocalOidcContextError = LocalOidcContextError;
|
|
8
|
+
* Error thrown when OIDC context is not available in local development,
|
|
9
|
+
* therefore we should guide how to ensure it is set up by linking a project
|
|
10
|
+
*/
|
|
11
|
+
var LocalOidcContextError = class extends Error {
|
|
12
|
+
constructor(cause) {
|
|
13
|
+
const message = [
|
|
14
|
+
"Could not get credentials from OIDC context.",
|
|
15
|
+
"Please link your Vercel project using `npx vercel link`.",
|
|
16
|
+
"Then, pull an initial OIDC token with `npx vercel env pull`",
|
|
17
|
+
"and retry.",
|
|
18
|
+
"╰▶ Make sure you are loading `.env.local` correctly, or passing $VERCEL_OIDC_TOKEN directly."
|
|
19
|
+
].join("\n");
|
|
20
|
+
super(message, { cause });
|
|
21
|
+
this.name = "LocalOidcContextError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
27
24
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
exports.VercelOidcContextError = VercelOidcContextError;
|
|
25
|
+
* Error thrown when OIDC context is not available in Vercel environment,
|
|
26
|
+
* therefore we should guide how to set it up.
|
|
27
|
+
*/
|
|
28
|
+
var VercelOidcContextError = class extends Error {
|
|
29
|
+
constructor(cause) {
|
|
30
|
+
const message = [
|
|
31
|
+
"Could not get credentials from OIDC context.",
|
|
32
|
+
"Please make sure OIDC is set up for your project",
|
|
33
|
+
"╰▶ Docs: https://vercel.com/docs/oidc"
|
|
34
|
+
].join("\n");
|
|
35
|
+
super(message, { cause });
|
|
36
|
+
this.name = "VercelOidcContextError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
43
39
|
async function getVercelToken(opts) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
throw new VercelOidcContextError(error);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
throw new LocalOidcContextError(error);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return await (0, dev_credentials_1.cachedGenerateCredentials)(opts);
|
|
61
|
-
}
|
|
40
|
+
try {
|
|
41
|
+
return getCredentialsFromOIDCToken(await getVercelOidcToken({
|
|
42
|
+
team: opts.teamId,
|
|
43
|
+
project: opts.projectId
|
|
44
|
+
}));
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (!shouldPromptForCredentials()) if (process.env.VERCEL_URL) throw new VercelOidcContextError(error);
|
|
47
|
+
else throw new LocalOidcContextError(error);
|
|
48
|
+
return await cachedGenerateCredentials(opts);
|
|
49
|
+
}
|
|
62
50
|
}
|
|
63
51
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
52
|
+
* Allow to get credentials to access the Vercel API. Credentials can be
|
|
53
|
+
* provided in two different ways:
|
|
54
|
+
*
|
|
55
|
+
* 1. By passing an object with the `teamId`, `token`, and `projectId` properties.
|
|
56
|
+
* 2. By using an environment variable VERCEL_OIDC_TOKEN.
|
|
57
|
+
*
|
|
58
|
+
* If both methods are used, the object properties take precedence over the
|
|
59
|
+
* environment variable. If neither method is used, an error is thrown.
|
|
60
|
+
*/
|
|
73
61
|
async function getCredentials(params) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
typeof params === "object" &&
|
|
81
|
-
"teamId" in params &&
|
|
82
|
-
typeof params.teamId === "string"
|
|
83
|
-
? params.teamId
|
|
84
|
-
: undefined,
|
|
85
|
-
projectId: params &&
|
|
86
|
-
typeof params === "object" &&
|
|
87
|
-
"projectId" in params &&
|
|
88
|
-
typeof params.projectId === "string"
|
|
89
|
-
? params.projectId
|
|
90
|
-
: undefined,
|
|
91
|
-
});
|
|
92
|
-
return oidcToken;
|
|
62
|
+
const credentials = getCredentialsFromParams(params ?? {});
|
|
63
|
+
if (credentials) return credentials;
|
|
64
|
+
return await getVercelToken({
|
|
65
|
+
teamId: params && typeof params === "object" && "teamId" in params && typeof params.teamId === "string" ? params.teamId : void 0,
|
|
66
|
+
projectId: params && typeof params === "object" && "projectId" in params && typeof params.projectId === "string" ? params.projectId : void 0
|
|
67
|
+
});
|
|
93
68
|
}
|
|
94
69
|
/**
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
70
|
+
* Attempt to extract credentials from the provided parameters. Either all
|
|
71
|
+
* required fields (`token`, `teamId`, and `projectId`) must be present
|
|
72
|
+
* or none of them, otherwise an error is thrown.
|
|
73
|
+
*/
|
|
99
74
|
function getCredentialsFromParams(params) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
token: params.token,
|
|
114
|
-
projectId: params.projectId,
|
|
115
|
-
teamId: params.teamId,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
if (missing.length < 3) {
|
|
119
|
-
throw new Error(`Missing credentials parameters to access the Vercel API: ${missing
|
|
120
|
-
.filter((value) => value !== null)
|
|
121
|
-
.join(", ")}`);
|
|
122
|
-
}
|
|
123
|
-
return null;
|
|
75
|
+
if (!params || typeof params !== "object") return null;
|
|
76
|
+
const missing = [
|
|
77
|
+
"token" in params && typeof params.token === "string" ? null : "token",
|
|
78
|
+
"teamId" in params && typeof params.teamId === "string" ? null : "teamId",
|
|
79
|
+
"projectId" in params && typeof params.projectId === "string" ? null : "projectId"
|
|
80
|
+
].filter((value) => value !== null);
|
|
81
|
+
if (missing.length === 0) return {
|
|
82
|
+
token: params.token,
|
|
83
|
+
projectId: params.projectId,
|
|
84
|
+
teamId: params.teamId
|
|
85
|
+
};
|
|
86
|
+
if (missing.length < 3) throw new Error(`Missing credentials parameters to access the Vercel API: ${missing.filter((value) => value !== null).join(", ")}`);
|
|
87
|
+
return null;
|
|
124
88
|
}
|
|
125
89
|
/**
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
90
|
+
* Schema to validate the payload of the Vercel OIDC token where we expect
|
|
91
|
+
* to find the `teamId` and `projectId`.
|
|
92
|
+
*/
|
|
93
|
+
const schema = z.object({
|
|
94
|
+
exp: z.number().optional().describe("Expiry timestamp (seconds since epoch)"),
|
|
95
|
+
iat: z.number().optional().describe("Issued at timestamp"),
|
|
96
|
+
owner_id: z.string(),
|
|
97
|
+
project_id: z.string()
|
|
134
98
|
});
|
|
135
99
|
/**
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
100
|
+
* Extracts credentials from a Vercel OIDC token. The token is expected to be
|
|
101
|
+
* a JWT with a payload that contains `project_id` and `owner_id`.
|
|
102
|
+
*
|
|
103
|
+
* @param token - The Vercel OIDC token.
|
|
104
|
+
* @returns An object containing the token, projectId, and teamId.
|
|
105
|
+
* @throws If the token is invalid or does not contain the required fields.
|
|
106
|
+
*/
|
|
143
107
|
function getCredentialsFromOIDCToken(token) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
108
|
+
try {
|
|
109
|
+
const payload = schema.parse(decodeBase64Url(token.split(".")[1]));
|
|
110
|
+
return {
|
|
111
|
+
token,
|
|
112
|
+
projectId: payload.project_id,
|
|
113
|
+
teamId: payload.owner_id
|
|
114
|
+
};
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw new Error(`Invalid Vercel OIDC token: ${error instanceof Error ? error.message : String(error)}`);
|
|
117
|
+
}
|
|
155
118
|
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { getCredentials };
|
|
156
122
|
//# sourceMappingURL=get-credentials.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-credentials.js","
|
|
1
|
+
{"version":3,"file":"get-credentials.js","names":[],"sources":["../../src/utils/get-credentials.ts"],"sourcesContent":["import { getVercelOidcToken } from \"@vercel/oidc\";\nimport { decodeBase64Url } from \"./decode-base64-url.js\";\nimport { z } from \"zod\";\nimport {\n cachedGenerateCredentials,\n shouldPromptForCredentials,\n} from \"./dev-credentials.js\";\n\nexport interface Credentials {\n /**\n * Authentication token for the Vercel API. It could be an OIDC token\n * or a personal access token.\n */\n token: string;\n /**\n * The ID of the project to associate Sandbox operations.\n */\n projectId: string;\n /**\n * The ID of the team to associate Sandbox operations.\n */\n teamId: string;\n}\n\n/**\n * Error thrown when OIDC context is not available in local development,\n * therefore we should guide how to ensure it is set up by linking a project\n */\nexport class LocalOidcContextError extends Error {\n name = \"LocalOidcContextError\";\n constructor(cause: unknown) {\n const message = [\n \"Could not get credentials from OIDC context.\",\n \"Please link your Vercel project using `npx vercel link`.\",\n \"Then, pull an initial OIDC token with `npx vercel env pull`\",\n \"and retry.\",\n \"╰▶ Make sure you are loading `.env.local` correctly, or passing $VERCEL_OIDC_TOKEN directly.\",\n ].join(\"\\n\");\n super(message, { cause });\n }\n}\n\n/**\n * Error thrown when OIDC context is not available in Vercel environment,\n * therefore we should guide how to set it up.\n */\nexport class VercelOidcContextError extends Error {\n name = \"VercelOidcContextError\";\n constructor(cause: unknown) {\n const message = [\n \"Could not get credentials from OIDC context.\",\n \"Please make sure OIDC is set up for your project\",\n \"╰▶ Docs: https://vercel.com/docs/oidc\",\n ].join(\"\\n\");\n super(message, { cause });\n }\n}\n\nasync function getVercelToken(opts: {\n teamId?: string;\n projectId?: string;\n}): Promise<Credentials> {\n try {\n const token = await getVercelOidcToken({\n team: opts.teamId,\n project: opts.projectId,\n });\n return getCredentialsFromOIDCToken(token);\n } catch (error) {\n if (!shouldPromptForCredentials()) {\n if (process.env.VERCEL_URL) {\n throw new VercelOidcContextError(error);\n } else {\n throw new LocalOidcContextError(error);\n }\n }\n return await cachedGenerateCredentials(opts);\n }\n}\n\n/**\n * Allow to get credentials to access the Vercel API. Credentials can be\n * provided in two different ways:\n *\n * 1. By passing an object with the `teamId`, `token`, and `projectId` properties.\n * 2. By using an environment variable VERCEL_OIDC_TOKEN.\n *\n * If both methods are used, the object properties take precedence over the\n * environment variable. If neither method is used, an error is thrown.\n */\nexport async function getCredentials(params?: unknown): Promise<Credentials> {\n const credentials = getCredentialsFromParams(params ?? {});\n if (credentials) {\n return credentials;\n }\n\n const oidcToken = await getVercelToken({\n teamId:\n params &&\n typeof params === \"object\" &&\n \"teamId\" in params &&\n typeof params.teamId === \"string\"\n ? params.teamId\n : undefined,\n projectId:\n params &&\n typeof params === \"object\" &&\n \"projectId\" in params &&\n typeof params.projectId === \"string\"\n ? params.projectId\n : undefined,\n });\n\n return oidcToken;\n}\n\n/**\n * Attempt to extract credentials from the provided parameters. Either all\n * required fields (`token`, `teamId`, and `projectId`) must be present\n * or none of them, otherwise an error is thrown.\n */\nfunction getCredentialsFromParams(params: unknown): Credentials | null {\n // Type guard: params must be an object\n if (!params || typeof params !== \"object\") {\n return null;\n }\n\n const missing = [\n \"token\" in params && typeof params.token === \"string\" ? null : \"token\",\n \"teamId\" in params && typeof params.teamId === \"string\" ? null : \"teamId\",\n \"projectId\" in params && typeof params.projectId === \"string\"\n ? null\n : \"projectId\",\n ].filter((value) => value !== null);\n\n if (missing.length === 0) {\n return {\n token: (params as any).token,\n projectId: (params as any).projectId,\n teamId: (params as any).teamId,\n };\n }\n\n if (missing.length < 3) {\n throw new Error(\n `Missing credentials parameters to access the Vercel API: ${missing\n .filter((value) => value !== null)\n .join(\", \")}`,\n );\n }\n\n return null;\n}\n\n/**\n * Schema to validate the payload of the Vercel OIDC token where we expect\n * to find the `teamId` and `projectId`.\n */\nexport const schema = z.object({\n exp: z.number().optional().describe(\"Expiry timestamp (seconds since epoch)\"),\n iat: z.number().optional().describe(\"Issued at timestamp\"),\n owner_id: z.string(),\n project_id: z.string(),\n});\n\n/**\n * Extracts credentials from a Vercel OIDC token. The token is expected to be\n * a JWT with a payload that contains `project_id` and `owner_id`.\n *\n * @param token - The Vercel OIDC token.\n * @returns An object containing the token, projectId, and teamId.\n * @throws If the token is invalid or does not contain the required fields.\n */\nfunction getCredentialsFromOIDCToken(token: string): Credentials {\n try {\n const payload = schema.parse(decodeBase64Url(token.split(\".\")[1]));\n return {\n token,\n projectId: payload.project_id,\n teamId: payload.owner_id,\n };\n } catch (error) {\n throw new Error(\n `Invalid Vercel OIDC token: ${error instanceof Error ? error.message : String(error)}`,\n );\n }\n}\n"],"mappings":";;;;;;;;;;AA4BA,IAAa,wBAAb,cAA2C,MAAM;CAE/C,YAAY,OAAgB;EAC1B,MAAM,UAAU;GACd;GACA;GACA;GACA;GACA;GACD,CAAC,KAAK,KAAK;AACZ,QAAM,SAAS,EAAE,OAAO,CAAC;OAT3B,OAAO;;;;;;;AAiBT,IAAa,yBAAb,cAA4C,MAAM;CAEhD,YAAY,OAAgB;EAC1B,MAAM,UAAU;GACd;GACA;GACA;GACD,CAAC,KAAK,KAAK;AACZ,QAAM,SAAS,EAAE,OAAO,CAAC;OAP3B,OAAO;;;AAWT,eAAe,eAAe,MAGL;AACvB,KAAI;AAKF,SAAO,4BAJO,MAAM,mBAAmB;GACrC,MAAM,KAAK;GACX,SAAS,KAAK;GACf,CAAC,CACuC;UAClC,OAAO;AACd,MAAI,CAAC,4BAA4B,CAC/B,KAAI,QAAQ,IAAI,WACd,OAAM,IAAI,uBAAuB,MAAM;MAEvC,OAAM,IAAI,sBAAsB,MAAM;AAG1C,SAAO,MAAM,0BAA0B,KAAK;;;;;;;;;;;;;AAchD,eAAsB,eAAe,QAAwC;CAC3E,MAAM,cAAc,yBAAyB,UAAU,EAAE,CAAC;AAC1D,KAAI,YACF,QAAO;AAoBT,QAjBkB,MAAM,eAAe;EACrC,QACE,UACA,OAAO,WAAW,YAClB,YAAY,UACZ,OAAO,OAAO,WAAW,WACrB,OAAO,SACP;EACN,WACE,UACA,OAAO,WAAW,YAClB,eAAe,UACf,OAAO,OAAO,cAAc,WACxB,OAAO,YACP;EACP,CAAC;;;;;;;AAUJ,SAAS,yBAAyB,QAAqC;AAErE,KAAI,CAAC,UAAU,OAAO,WAAW,SAC/B,QAAO;CAGT,MAAM,UAAU;EACd,WAAW,UAAU,OAAO,OAAO,UAAU,WAAW,OAAO;EAC/D,YAAY,UAAU,OAAO,OAAO,WAAW,WAAW,OAAO;EACjE,eAAe,UAAU,OAAO,OAAO,cAAc,WACjD,OACA;EACL,CAAC,QAAQ,UAAU,UAAU,KAAK;AAEnC,KAAI,QAAQ,WAAW,EACrB,QAAO;EACL,OAAQ,OAAe;EACvB,WAAY,OAAe;EAC3B,QAAS,OAAe;EACzB;AAGH,KAAI,QAAQ,SAAS,EACnB,OAAM,IAAI,MACR,4DAA4D,QACzD,QAAQ,UAAU,UAAU,KAAK,CACjC,KAAK,KAAK,GACd;AAGH,QAAO;;;;;;AAOT,MAAa,SAAS,EAAE,OAAO;CAC7B,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,SAAS,yCAAyC;CAC7E,KAAK,EAAE,QAAQ,CAAC,UAAU,CAAC,SAAS,sBAAsB;CAC1D,UAAU,EAAE,QAAQ;CACpB,YAAY,EAAE,QAAQ;CACvB,CAAC;;;;;;;;;AAUF,SAAS,4BAA4B,OAA4B;AAC/D,KAAI;EACF,MAAM,UAAU,OAAO,MAAM,gBAAgB,MAAM,MAAM,IAAI,CAAC,GAAG,CAAC;AAClE,SAAO;GACL;GACA,WAAW,QAAQ;GACnB,QAAQ,QAAQ;GACjB;UACM,OAAO;AACd,QAAM,IAAI,MACR,8BAA8B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM,GACrF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let picocolors = require("picocolors");
|
|
3
|
+
picocolors = require_rolldown_runtime.__toESM(picocolors);
|
|
4
|
+
|
|
5
|
+
//#region src/utils/log.ts
|
|
6
|
+
const colors = {
|
|
7
|
+
warn: picocolors.default.yellow,
|
|
8
|
+
error: picocolors.default.red,
|
|
9
|
+
success: picocolors.default.green,
|
|
10
|
+
info: picocolors.default.blue
|
|
11
|
+
};
|
|
12
|
+
const logPrefix = picocolors.default.dim("[vercel/sandbox]");
|
|
13
|
+
function write(level, text) {
|
|
14
|
+
text = Array.isArray(text) ? text.join("\n") : text;
|
|
15
|
+
const prefixed = text.replace(/^/gm, `${logPrefix} `);
|
|
16
|
+
console.error(colors[level](prefixed));
|
|
17
|
+
}
|
|
18
|
+
function code(text) {
|
|
19
|
+
return picocolors.default.italic(picocolors.default.dim("`") + text + picocolors.default.dim("`"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.code = code;
|
|
24
|
+
exports.write = write;
|
|
25
|
+
//# sourceMappingURL=log.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.cjs","names":["pico"],"sources":["../../src/utils/log.ts"],"sourcesContent":["import pico from \"picocolors\";\nconst colors = {\n warn: pico.yellow,\n error: pico.red,\n success: pico.green,\n info: pico.blue,\n};\nconst logPrefix = pico.dim(\"[vercel/sandbox]\");\nexport function write(\n level: \"warn\" | \"error\" | \"info\" | \"success\",\n text: string | string[],\n) {\n text = Array.isArray(text) ? text.join(\"\\n\") : text;\n const prefixed = text.replace(/^/gm, `${logPrefix} `);\n console.error(colors[level](prefixed));\n}\n\nexport function code(text: string) {\n return pico.italic(pico.dim(\"`\") + text + pico.dim(\"`\"));\n}\n"],"mappings":";;;;;AACA,MAAM,SAAS;CACb,MAAMA,mBAAK;CACX,OAAOA,mBAAK;CACZ,SAASA,mBAAK;CACd,MAAMA,mBAAK;CACZ;AACD,MAAM,YAAYA,mBAAK,IAAI,mBAAmB;AAC9C,SAAgB,MACd,OACA,MACA;AACA,QAAO,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK,KAAK,GAAG;CAC/C,MAAM,WAAW,KAAK,QAAQ,OAAO,GAAG,UAAU,GAAG;AACrD,SAAQ,MAAM,OAAO,OAAO,SAAS,CAAC;;AAGxC,SAAgB,KAAK,MAAc;AACjC,QAAOA,mBAAK,OAAOA,mBAAK,IAAI,IAAI,GAAG,OAAOA,mBAAK,IAAI,IAAI,CAAC"}
|
package/dist/utils/log.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.write = write;
|
|
7
|
-
exports.code = code;
|
|
8
|
-
const picocolors_1 = __importDefault(require("picocolors"));
|
|
1
|
+
import pico from "picocolors";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/log.ts
|
|
9
4
|
const colors = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
warn: pico.yellow,
|
|
6
|
+
error: pico.red,
|
|
7
|
+
success: pico.green,
|
|
8
|
+
info: pico.blue
|
|
14
9
|
};
|
|
15
|
-
const logPrefix =
|
|
10
|
+
const logPrefix = pico.dim("[vercel/sandbox]");
|
|
16
11
|
function write(level, text) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
text = Array.isArray(text) ? text.join("\n") : text;
|
|
13
|
+
const prefixed = text.replace(/^/gm, `${logPrefix} `);
|
|
14
|
+
console.error(colors[level](prefixed));
|
|
20
15
|
}
|
|
21
16
|
function code(text) {
|
|
22
|
-
|
|
17
|
+
return pico.italic(pico.dim("`") + text + pico.dim("`"));
|
|
23
18
|
}
|
|
19
|
+
|
|
20
|
+
//#endregion
|
|
21
|
+
export { code, write };
|
|
24
22
|
//# sourceMappingURL=log.js.map
|
package/dist/utils/log.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.js","
|
|
1
|
+
{"version":3,"file":"log.js","names":[],"sources":["../../src/utils/log.ts"],"sourcesContent":["import pico from \"picocolors\";\nconst colors = {\n warn: pico.yellow,\n error: pico.red,\n success: pico.green,\n info: pico.blue,\n};\nconst logPrefix = pico.dim(\"[vercel/sandbox]\");\nexport function write(\n level: \"warn\" | \"error\" | \"info\" | \"success\",\n text: string | string[],\n) {\n text = Array.isArray(text) ? text.join(\"\\n\") : text;\n const prefixed = text.replace(/^/gm, `${logPrefix} `);\n console.error(colors[level](prefixed));\n}\n\nexport function code(text: string) {\n return pico.italic(pico.dim(\"`\") + text + pico.dim(\"`\"));\n}\n"],"mappings":";;;AACA,MAAM,SAAS;CACb,MAAM,KAAK;CACX,OAAO,KAAK;CACZ,SAAS,KAAK;CACd,MAAM,KAAK;CACZ;AACD,MAAM,YAAY,KAAK,IAAI,mBAAmB;AAC9C,SAAgB,MACd,OACA,MACA;AACA,QAAO,MAAM,QAAQ,KAAK,GAAG,KAAK,KAAK,KAAK,GAAG;CAC/C,MAAM,WAAW,KAAK,QAAQ,OAAO,GAAG,UAAU,GAAG;AACrD,SAAQ,MAAM,OAAO,OAAO,SAAS,CAAC;;AAGxC,SAAgB,KAAK,MAAc;AACjC,QAAO,KAAK,OAAO,KAAK,IAAI,IAAI,GAAG,OAAO,KAAK,IAAI,IAAI,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/utils/network-policy.ts
|
|
3
|
+
function toAPINetworkPolicy(policy) {
|
|
4
|
+
if (policy === "allow-all") return { mode: "allow-all" };
|
|
5
|
+
if (policy === "deny-all") return { mode: "deny-all" };
|
|
6
|
+
if (policy.allow && !Array.isArray(policy.allow)) {
|
|
7
|
+
const allowedDomains = Object.keys(policy.allow);
|
|
8
|
+
const injectionRules = [];
|
|
9
|
+
for (const [domain, rules] of Object.entries(policy.allow)) {
|
|
10
|
+
const merged = {};
|
|
11
|
+
for (const rule of rules) for (const t of rule.transform ?? []) Object.assign(merged, t.headers);
|
|
12
|
+
if (Object.keys(merged).length > 0) injectionRules.push({
|
|
13
|
+
domain,
|
|
14
|
+
headers: merged
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
mode: "custom",
|
|
19
|
+
...allowedDomains.length > 0 && { allowedDomains },
|
|
20
|
+
...injectionRules.length > 0 && { injectionRules },
|
|
21
|
+
...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
|
|
22
|
+
...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
mode: "custom",
|
|
27
|
+
...policy.allow && { allowedDomains: policy.allow },
|
|
28
|
+
...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
|
|
29
|
+
...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function fromAPINetworkPolicy(api) {
|
|
33
|
+
if (api.mode === "allow-all") return "allow-all";
|
|
34
|
+
if (api.mode === "deny-all") return "deny-all";
|
|
35
|
+
const subnets = api.allowedCIDRs || api.deniedCIDRs ? { subnets: {
|
|
36
|
+
...api.allowedCIDRs && { allow: api.allowedCIDRs },
|
|
37
|
+
...api.deniedCIDRs && { deny: api.deniedCIDRs }
|
|
38
|
+
} } : void 0;
|
|
39
|
+
if (api.injectionRules && api.injectionRules.length > 0) {
|
|
40
|
+
const rulesByDomain = new Map(api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]));
|
|
41
|
+
const allow = {};
|
|
42
|
+
for (const domain of api.allowedDomains ?? []) {
|
|
43
|
+
const headerNames = rulesByDomain.get(domain);
|
|
44
|
+
if (headerNames && headerNames.length > 0) allow[domain] = [{ transform: [{ headers: Object.fromEntries(headerNames.map((n) => [n, "<redacted>"])) }] }];
|
|
45
|
+
else allow[domain] = [];
|
|
46
|
+
}
|
|
47
|
+
for (const rule of api.injectionRules) if (!(rule.domain in allow)) {
|
|
48
|
+
const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
|
|
49
|
+
allow[rule.domain] = [{ transform: [{ headers }] }];
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
allow,
|
|
53
|
+
...subnets
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
...api.allowedDomains && { allow: api.allowedDomains },
|
|
58
|
+
...subnets
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
exports.fromAPINetworkPolicy = fromAPINetworkPolicy;
|
|
64
|
+
exports.toAPINetworkPolicy = toAPINetworkPolicy;
|
|
65
|
+
//# sourceMappingURL=network-policy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network-policy.cjs","names":["injectionRules: z.infer<typeof InjectionRuleValidator>[]","merged: Record<string, string>","allow: Record<string, NetworkPolicyRule[]>"],"sources":["../../src/utils/network-policy.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { NetworkPolicy, NetworkPolicyRule } from \"../network-policy.js\";\nimport {\n NetworkPolicyValidator,\n InjectionRuleValidator,\n} from \"../api-client/validators.js\";\n\ntype APINetworkPolicy = z.infer<typeof NetworkPolicyValidator>;\n\nexport function toAPINetworkPolicy(policy: NetworkPolicy): APINetworkPolicy {\n if (policy === \"allow-all\") return { mode: \"allow-all\" };\n if (policy === \"deny-all\") return { mode: \"deny-all\" };\n\n if (policy.allow && !Array.isArray(policy.allow)) {\n const allowedDomains = Object.keys(policy.allow);\n const injectionRules: z.infer<typeof InjectionRuleValidator>[] = [];\n\n for (const [domain, rules] of Object.entries(policy.allow)) {\n const merged: Record<string, string> = {};\n for (const rule of rules) {\n for (const t of rule.transform ?? []) {\n Object.assign(merged, t.headers);\n }\n }\n if (Object.keys(merged).length > 0) {\n injectionRules.push({ domain, headers: merged });\n }\n }\n\n return {\n mode: \"custom\",\n ...(allowedDomains.length > 0 && { allowedDomains }),\n ...(injectionRules.length > 0 && { injectionRules }),\n ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),\n ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),\n };\n }\n\n return {\n mode: \"custom\",\n ...(policy.allow && { allowedDomains: policy.allow }),\n ...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),\n ...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),\n };\n}\n\nexport function fromAPINetworkPolicy(api: APINetworkPolicy): NetworkPolicy {\n if (api.mode === \"allow-all\") return \"allow-all\";\n if (api.mode === \"deny-all\") return \"deny-all\";\n\n const subnets =\n api.allowedCIDRs || api.deniedCIDRs\n ? {\n subnets: {\n ...(api.allowedCIDRs && { allow: api.allowedCIDRs }),\n ...(api.deniedCIDRs && { deny: api.deniedCIDRs }),\n },\n }\n : undefined;\n\n // If injectionRules are present, reconstruct the record form.\n // The API returns headerNames (secret values are stripped), so we\n // populate each header value with \"<redacted>\".\n if (api.injectionRules && api.injectionRules.length > 0) {\n const rulesByDomain = new Map(\n api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]),\n );\n\n const allow: Record<string, NetworkPolicyRule[]> = {};\n for (const domain of api.allowedDomains ?? []) {\n const headerNames = rulesByDomain.get(domain);\n if (headerNames && headerNames.length > 0) {\n const headers = Object.fromEntries(\n headerNames.map((n) => [n, \"<redacted>\"]),\n );\n allow[domain] = [{ transform: [{ headers }] }];\n } else {\n allow[domain] = [];\n }\n }\n // Include injection rules for domains not in allowedDomains\n for (const rule of api.injectionRules) {\n if (!(rule.domain in allow)) {\n const headers = Object.fromEntries(\n (rule.headerNames ?? []).map((n) => [n, \"<redacted>\"]),\n );\n allow[rule.domain] = [{ transform: [{ headers }] }];\n }\n }\n\n return { allow, ...subnets };\n }\n\n return {\n ...(api.allowedDomains && { allow: api.allowedDomains }),\n ...subnets,\n };\n}\n"],"mappings":";;AASA,SAAgB,mBAAmB,QAAyC;AAC1E,KAAI,WAAW,YAAa,QAAO,EAAE,MAAM,aAAa;AACxD,KAAI,WAAW,WAAY,QAAO,EAAE,MAAM,YAAY;AAEtD,KAAI,OAAO,SAAS,CAAC,MAAM,QAAQ,OAAO,MAAM,EAAE;EAChD,MAAM,iBAAiB,OAAO,KAAK,OAAO,MAAM;EAChD,MAAMA,iBAA2D,EAAE;AAEnE,OAAK,MAAM,CAAC,QAAQ,UAAU,OAAO,QAAQ,OAAO,MAAM,EAAE;GAC1D,MAAMC,SAAiC,EAAE;AACzC,QAAK,MAAM,QAAQ,MACjB,MAAK,MAAM,KAAK,KAAK,aAAa,EAAE,CAClC,QAAO,OAAO,QAAQ,EAAE,QAAQ;AAGpC,OAAI,OAAO,KAAK,OAAO,CAAC,SAAS,EAC/B,gBAAe,KAAK;IAAE;IAAQ,SAAS;IAAQ,CAAC;;AAIpD,SAAO;GACL,MAAM;GACN,GAAI,eAAe,SAAS,KAAK,EAAE,gBAAgB;GACnD,GAAI,eAAe,SAAS,KAAK,EAAE,gBAAgB;GACnD,GAAI,OAAO,SAAS,SAAS,EAAE,cAAc,OAAO,QAAQ,OAAO;GACnE,GAAI,OAAO,SAAS,QAAQ,EAAE,aAAa,OAAO,QAAQ,MAAM;GACjE;;AAGH,QAAO;EACL,MAAM;EACN,GAAI,OAAO,SAAS,EAAE,gBAAgB,OAAO,OAAO;EACpD,GAAI,OAAO,SAAS,SAAS,EAAE,cAAc,OAAO,QAAQ,OAAO;EACnE,GAAI,OAAO,SAAS,QAAQ,EAAE,aAAa,OAAO,QAAQ,MAAM;EACjE;;AAGH,SAAgB,qBAAqB,KAAsC;AACzE,KAAI,IAAI,SAAS,YAAa,QAAO;AACrC,KAAI,IAAI,SAAS,WAAY,QAAO;CAEpC,MAAM,UACJ,IAAI,gBAAgB,IAAI,cACpB,EACE,SAAS;EACP,GAAI,IAAI,gBAAgB,EAAE,OAAO,IAAI,cAAc;EACnD,GAAI,IAAI,eAAe,EAAE,MAAM,IAAI,aAAa;EACjD,EACF,GACD;AAKN,KAAI,IAAI,kBAAkB,IAAI,eAAe,SAAS,GAAG;EACvD,MAAM,gBAAgB,IAAI,IACxB,IAAI,eAAe,KAAK,MAAM,CAAC,EAAE,QAAQ,EAAE,eAAe,EAAE,CAAC,CAAC,CAC/D;EAED,MAAMC,QAA6C,EAAE;AACrD,OAAK,MAAM,UAAU,IAAI,kBAAkB,EAAE,EAAE;GAC7C,MAAM,cAAc,cAAc,IAAI,OAAO;AAC7C,OAAI,eAAe,YAAY,SAAS,EAItC,OAAM,UAAU,CAAC,EAAE,WAAW,CAAC,EAAE,SAHjB,OAAO,YACrB,YAAY,KAAK,MAAM,CAAC,GAAG,aAAa,CAAC,CAC1C,EACyC,CAAC,EAAE,CAAC;OAE9C,OAAM,UAAU,EAAE;;AAItB,OAAK,MAAM,QAAQ,IAAI,eACrB,KAAI,EAAE,KAAK,UAAU,QAAQ;GAC3B,MAAM,UAAU,OAAO,aACpB,KAAK,eAAe,EAAE,EAAE,KAAK,MAAM,CAAC,GAAG,aAAa,CAAC,CACvD;AACD,SAAM,KAAK,UAAU,CAAC,EAAE,WAAW,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC;;AAIvD,SAAO;GAAE;GAAO,GAAG;GAAS;;AAG9B,QAAO;EACL,GAAI,IAAI,kBAAkB,EAAE,OAAO,IAAI,gBAAgB;EACvD,GAAG;EACJ"}
|
|
@@ -1,82 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toAPINetworkPolicy = toAPINetworkPolicy;
|
|
4
|
-
exports.fromAPINetworkPolicy = fromAPINetworkPolicy;
|
|
1
|
+
//#region src/utils/network-policy.ts
|
|
5
2
|
function toAPINetworkPolicy(policy) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
mode: "custom",
|
|
34
|
-
...(policy.allow && { allowedDomains: policy.allow }),
|
|
35
|
-
...(policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow }),
|
|
36
|
-
...(policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }),
|
|
37
|
-
};
|
|
3
|
+
if (policy === "allow-all") return { mode: "allow-all" };
|
|
4
|
+
if (policy === "deny-all") return { mode: "deny-all" };
|
|
5
|
+
if (policy.allow && !Array.isArray(policy.allow)) {
|
|
6
|
+
const allowedDomains = Object.keys(policy.allow);
|
|
7
|
+
const injectionRules = [];
|
|
8
|
+
for (const [domain, rules] of Object.entries(policy.allow)) {
|
|
9
|
+
const merged = {};
|
|
10
|
+
for (const rule of rules) for (const t of rule.transform ?? []) Object.assign(merged, t.headers);
|
|
11
|
+
if (Object.keys(merged).length > 0) injectionRules.push({
|
|
12
|
+
domain,
|
|
13
|
+
headers: merged
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
mode: "custom",
|
|
18
|
+
...allowedDomains.length > 0 && { allowedDomains },
|
|
19
|
+
...injectionRules.length > 0 && { injectionRules },
|
|
20
|
+
...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
|
|
21
|
+
...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
mode: "custom",
|
|
26
|
+
...policy.allow && { allowedDomains: policy.allow },
|
|
27
|
+
...policy.subnets?.allow && { allowedCIDRs: policy.subnets.allow },
|
|
28
|
+
...policy.subnets?.deny && { deniedCIDRs: policy.subnets.deny }
|
|
29
|
+
};
|
|
38
30
|
}
|
|
39
31
|
function fromAPINetworkPolicy(api) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
// Include injection rules for domains not in allowedDomains
|
|
69
|
-
for (const rule of api.injectionRules) {
|
|
70
|
-
if (!(rule.domain in allow)) {
|
|
71
|
-
const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
|
|
72
|
-
allow[rule.domain] = [{ transform: [{ headers }] }];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return { allow, ...subnets };
|
|
76
|
-
}
|
|
77
|
-
return {
|
|
78
|
-
...(api.allowedDomains && { allow: api.allowedDomains }),
|
|
79
|
-
...subnets,
|
|
80
|
-
};
|
|
32
|
+
if (api.mode === "allow-all") return "allow-all";
|
|
33
|
+
if (api.mode === "deny-all") return "deny-all";
|
|
34
|
+
const subnets = api.allowedCIDRs || api.deniedCIDRs ? { subnets: {
|
|
35
|
+
...api.allowedCIDRs && { allow: api.allowedCIDRs },
|
|
36
|
+
...api.deniedCIDRs && { deny: api.deniedCIDRs }
|
|
37
|
+
} } : void 0;
|
|
38
|
+
if (api.injectionRules && api.injectionRules.length > 0) {
|
|
39
|
+
const rulesByDomain = new Map(api.injectionRules.map((r) => [r.domain, r.headerNames ?? []]));
|
|
40
|
+
const allow = {};
|
|
41
|
+
for (const domain of api.allowedDomains ?? []) {
|
|
42
|
+
const headerNames = rulesByDomain.get(domain);
|
|
43
|
+
if (headerNames && headerNames.length > 0) allow[domain] = [{ transform: [{ headers: Object.fromEntries(headerNames.map((n) => [n, "<redacted>"])) }] }];
|
|
44
|
+
else allow[domain] = [];
|
|
45
|
+
}
|
|
46
|
+
for (const rule of api.injectionRules) if (!(rule.domain in allow)) {
|
|
47
|
+
const headers = Object.fromEntries((rule.headerNames ?? []).map((n) => [n, "<redacted>"]));
|
|
48
|
+
allow[rule.domain] = [{ transform: [{ headers }] }];
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
allow,
|
|
52
|
+
...subnets
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
...api.allowedDomains && { allow: api.allowedDomains },
|
|
57
|
+
...subnets
|
|
58
|
+
};
|
|
81
59
|
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { fromAPINetworkPolicy, toAPINetworkPolicy };
|
|
82
63
|
//# sourceMappingURL=network-policy.js.map
|