@gadicc/loom 1.25.0 → 1.26.1
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 +4 -3
- package/esm/src/cli/mod.d.ts +5 -0
- package/esm/src/cli/mod.d.ts.map +1 -1
- package/esm/src/cli/mod.js +65 -145
- package/esm/src/cli/releaseCredentials.d.ts +32 -0
- package/esm/src/cli/releaseCredentials.d.ts.map +1 -0
- package/esm/src/cli/releaseCredentials.js +388 -0
- package/esm/src/cli/releaseTrust.d.ts +39 -0
- package/esm/src/cli/releaseTrust.d.ts.map +1 -0
- package/esm/src/cli/releaseTrust.js +125 -0
- package/esm/src/cli/releaseTrustCommand.d.ts +18 -0
- package/esm/src/cli/releaseTrustCommand.d.ts.map +1 -0
- package/esm/src/cli/releaseTrustCommand.js +142 -0
- package/esm/src/cli/vercelBuildEnv.d.ts +6 -2
- package/esm/src/cli/vercelBuildEnv.d.ts.map +1 -1
- package/esm/src/cli/vercelBuildEnv.js +130 -14
- package/package.json +2 -2
- package/script/src/cli/mod.d.ts +5 -0
- package/script/src/cli/mod.d.ts.map +1 -1
- package/script/src/cli/mod.js +65 -145
- package/script/src/cli/releaseCredentials.d.ts +32 -0
- package/script/src/cli/releaseCredentials.d.ts.map +1 -0
- package/script/src/cli/releaseCredentials.js +392 -0
- package/script/src/cli/releaseTrust.d.ts +39 -0
- package/script/src/cli/releaseTrust.d.ts.map +1 -0
- package/script/src/cli/releaseTrust.js +130 -0
- package/script/src/cli/releaseTrustCommand.d.ts +18 -0
- package/script/src/cli/releaseTrustCommand.d.ts.map +1 -0
- package/script/src/cli/releaseTrustCommand.js +145 -0
- package/script/src/cli/vercelBuildEnv.d.ts +6 -2
- package/script/src/cli/vercelBuildEnv.d.ts.map +1 -1
- package/script/src/cli/vercelBuildEnv.js +128 -12
- package/skills/loom/SKILL.md +3 -1
- package/skills/loom-db/SKILL.md +22 -10
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VERCEL_TOKEN_SCOPE_VARIABLE = exports.VERCEL_TOKEN_ID_VARIABLE = exports.VERCEL_TOKEN_EXPIRY_VARIABLE = exports.DEFAULT_VERCEL_TOKEN_EXPIRY_DAYS = void 0;
|
|
4
|
+
exports.configureGithubVercelReleaseCredential = configureGithubVercelReleaseCredential;
|
|
5
|
+
exports.DEFAULT_VERCEL_TOKEN_EXPIRY_DAYS = 90;
|
|
6
|
+
exports.VERCEL_TOKEN_EXPIRY_VARIABLE = "LOOM_VERCEL_TOKEN_EXPIRES_AT";
|
|
7
|
+
exports.VERCEL_TOKEN_ID_VARIABLE = "LOOM_VERCEL_TOKEN_ID";
|
|
8
|
+
exports.VERCEL_TOKEN_SCOPE_VARIABLE = "LOOM_VERCEL_TOKEN_SCOPE";
|
|
9
|
+
function parseJsonOutput(text) {
|
|
10
|
+
const trimmed = text.trim();
|
|
11
|
+
if (!trimmed)
|
|
12
|
+
return null;
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(trimmed);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
// A CLI may emit progress before its JSON response.
|
|
18
|
+
}
|
|
19
|
+
const starts = [trimmed.indexOf("{"), trimmed.indexOf("[")]
|
|
20
|
+
.filter((index) => index >= 0)
|
|
21
|
+
.sort((left, right) => left - right);
|
|
22
|
+
for (const start of starts) {
|
|
23
|
+
const close = trimmed[start] === "{" ? "}" : "]";
|
|
24
|
+
const end = trimmed.lastIndexOf(close);
|
|
25
|
+
if (end <= start)
|
|
26
|
+
continue;
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(trimmed.slice(start, end + 1));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// Try the next JSON-looking range.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
function isObject(value) {
|
|
37
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
38
|
+
}
|
|
39
|
+
function parseGithubNamedValues(text) {
|
|
40
|
+
const parsed = parseJsonOutput(text);
|
|
41
|
+
if (!Array.isArray(parsed))
|
|
42
|
+
return null;
|
|
43
|
+
const values = [];
|
|
44
|
+
for (const item of parsed) {
|
|
45
|
+
if (!isObject(item) || typeof item.name !== "string")
|
|
46
|
+
return null;
|
|
47
|
+
values.push({
|
|
48
|
+
name: item.name,
|
|
49
|
+
value: typeof item.value === "string" ? item.value : undefined,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
return values;
|
|
53
|
+
}
|
|
54
|
+
function isMissingGithubEnvironment(result) {
|
|
55
|
+
return /(?:HTTP\s*)?404\b|not found/i.test(`${result.stderr}\n${result.stdout}`);
|
|
56
|
+
}
|
|
57
|
+
async function requireSafeCommand(description, options, command, args, input) {
|
|
58
|
+
let result;
|
|
59
|
+
try {
|
|
60
|
+
result = await options.commandRunner(command, args, {
|
|
61
|
+
cwd: options.cwd,
|
|
62
|
+
env: options.env,
|
|
63
|
+
input,
|
|
64
|
+
stdio: "pipe",
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new Error(`${description} did not complete; provider output was suppressed.`);
|
|
69
|
+
}
|
|
70
|
+
if (result.code !== 0) {
|
|
71
|
+
throw new Error(`${description} failed; provider output was suppressed.`);
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
async function inspectGithubEnvironment(options) {
|
|
76
|
+
const endpoint = `repos/:owner/:repo/environments/${encodeURIComponent(options.environment)}`;
|
|
77
|
+
let result;
|
|
78
|
+
try {
|
|
79
|
+
result = await options.commandRunner(options.githubCommand, ["api", endpoint], { cwd: options.cwd, env: options.env, stdio: "pipe" });
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
throw new Error(`Could not inspect GitHub environment \`${options.environment}\`; provider output was suppressed.`);
|
|
83
|
+
}
|
|
84
|
+
if (result.code !== 0) {
|
|
85
|
+
if (!isMissingGithubEnvironment(result)) {
|
|
86
|
+
throw new Error(`Could not inspect GitHub environment \`${options.environment}\`; provider output was suppressed.`);
|
|
87
|
+
}
|
|
88
|
+
await requireSafeCommand(`Create GitHub environment \`${options.environment}\``, options, options.githubCommand, ["api", "--method", "PUT", endpoint, "--input", "-"], JSON.stringify({
|
|
89
|
+
deployment_branch_policy: {
|
|
90
|
+
custom_branch_policies: true,
|
|
91
|
+
protected_branches: false,
|
|
92
|
+
},
|
|
93
|
+
}));
|
|
94
|
+
return { created: true, protectionRuleTypes: [] };
|
|
95
|
+
}
|
|
96
|
+
const parsed = parseJsonOutput(result.stdout);
|
|
97
|
+
if (!isObject(parsed)) {
|
|
98
|
+
throw new Error(`GitHub returned an unreadable definition for environment \`${options.environment}\`; provider output was suppressed.`);
|
|
99
|
+
}
|
|
100
|
+
const environment = parsed;
|
|
101
|
+
const branchPolicy = environment.deployment_branch_policy;
|
|
102
|
+
if (!branchPolicy || branchPolicy.custom_branch_policies !== true ||
|
|
103
|
+
branchPolicy.protected_branches !== false) {
|
|
104
|
+
throw new Error(`GitHub environment \`${options.environment}\` does not use custom deployment branch policies. Loom refused to replace its existing protected-branch or all-branch policy mode.`);
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
created: false,
|
|
108
|
+
protectionRuleTypes: (environment.protection_rules ?? [])
|
|
109
|
+
.flatMap((rule) => typeof rule.type === "string" ? [rule.type] : []),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async function readGithubCredentialState(options) {
|
|
113
|
+
const secretResult = await requireSafeCommand(`List GitHub environment secrets for \`${options.environment}\``, options, options.githubCommand, ["secret", "list", "--env", options.environment, "--json", "name"]);
|
|
114
|
+
const variableResult = await requireSafeCommand(`List GitHub environment variables for \`${options.environment}\``, options, options.githubCommand, [
|
|
115
|
+
"variable",
|
|
116
|
+
"list",
|
|
117
|
+
"--env",
|
|
118
|
+
options.environment,
|
|
119
|
+
"--json",
|
|
120
|
+
"name,value",
|
|
121
|
+
]);
|
|
122
|
+
const secrets = parseGithubNamedValues(secretResult.stdout);
|
|
123
|
+
const variables = parseGithubNamedValues(variableResult.stdout);
|
|
124
|
+
if (!secrets || !variables) {
|
|
125
|
+
throw new Error(`GitHub returned unreadable release credential metadata for environment \`${options.environment}\`; provider output was suppressed.`);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
secretNames: new Set(secrets.map(({ name }) => name)),
|
|
129
|
+
variables: new Map(variables.flatMap(({ name, value }) => value === undefined ? [] : [[name, value]])),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function assertExistingTokenAssociation(options, variables) {
|
|
133
|
+
const existingOrgId = variables.get("VERCEL_ORG_ID");
|
|
134
|
+
const existingProjectId = variables.get("VERCEL_PROJECT_ID");
|
|
135
|
+
if (!existingOrgId || !existingProjectId) {
|
|
136
|
+
throw new Error(`GitHub environment \`${options.environment}\` already has VERCEL_TOKEN, but its VERCEL_ORG_ID and VERCEL_PROJECT_ID association is incomplete or unreadable. Loom refused to bind that secret to the currently linked project.`);
|
|
137
|
+
}
|
|
138
|
+
if (existingOrgId !== options.orgId || existingProjectId !== options.projectId) {
|
|
139
|
+
throw new Error(`GitHub environment \`${options.environment}\` already has VERCEL_TOKEN associated with a different Vercel org or project. Loom refused to rebind the existing credential.`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async function ensureGithubBranchPolicy(options) {
|
|
143
|
+
const endpoint = `repos/:owner/:repo/environments/${encodeURIComponent(options.environment)}/deployment-branch-policies`;
|
|
144
|
+
const result = await requireSafeCommand(`List GitHub deployment branch policies for \`${options.environment}\``, options, options.githubCommand, ["api", `${endpoint}?per_page=100`]);
|
|
145
|
+
const parsed = parseJsonOutput(result.stdout);
|
|
146
|
+
if (!isObject(parsed) || !Array.isArray(parsed.branch_policies)) {
|
|
147
|
+
throw new Error(`GitHub returned unreadable deployment branch policies for environment \`${options.environment}\`; provider output was suppressed.`);
|
|
148
|
+
}
|
|
149
|
+
const policies = parsed.branch_policies.filter(isObject);
|
|
150
|
+
if (typeof parsed.total_count === "number" &&
|
|
151
|
+
parsed.total_count > policies.length) {
|
|
152
|
+
throw new Error(`GitHub environment \`${options.environment}\` has more than 100 deployment branch policies. Loom refused to change an incomplete policy audit.`);
|
|
153
|
+
}
|
|
154
|
+
const sameNamePolicies = policies.filter((policy) => policy.name === options.productionBranch);
|
|
155
|
+
if (sameNamePolicies.some((policy) => policy.type !== "branch")) {
|
|
156
|
+
throw new Error(`GitHub environment \`${options.environment}\` has a non-branch deployment policy named \`${options.productionBranch}\`. Loom refused to treat it as the production branch constraint.`);
|
|
157
|
+
}
|
|
158
|
+
if (!sameNamePolicies.some((policy) => policy.type === "branch")) {
|
|
159
|
+
await requireSafeCommand(`Allow branch \`${options.productionBranch}\` to deploy to GitHub environment \`${options.environment}\``, options, options.githubCommand, ["api", "--method", "POST", endpoint, "--input", "-"], JSON.stringify({ name: options.productionBranch, type: "branch" }));
|
|
160
|
+
}
|
|
161
|
+
return policies.filter((policy) => policy.name !== options.productionBranch || policy.type !== "branch");
|
|
162
|
+
}
|
|
163
|
+
async function setGithubVariable(options, name, value) {
|
|
164
|
+
await requireSafeCommand(`Set GitHub environment variable ${name}`, options, options.githubCommand, ["variable", "set", name, "--env", options.environment, "--body", value]);
|
|
165
|
+
}
|
|
166
|
+
function resolveTokenSettings(options) {
|
|
167
|
+
const requestedScope = options.tokenScope ?? "project";
|
|
168
|
+
if (requestedScope !== "project" && requestedScope !== "team") {
|
|
169
|
+
throw new Error("--token-scope must be `project` or `team`.");
|
|
170
|
+
}
|
|
171
|
+
const tokenScope = requestedScope;
|
|
172
|
+
const expiryDays = options.tokenExpiryDays === undefined
|
|
173
|
+
? exports.DEFAULT_VERCEL_TOKEN_EXPIRY_DAYS
|
|
174
|
+
: typeof options.tokenExpiryDays === "number"
|
|
175
|
+
? options.tokenExpiryDays
|
|
176
|
+
: Number(options.tokenExpiryDays);
|
|
177
|
+
if (!Number.isSafeInteger(expiryDays) || expiryDays <= 0) {
|
|
178
|
+
throw new Error("--token-expiry-days must be a positive integer.");
|
|
179
|
+
}
|
|
180
|
+
const now = (options.now ?? (() => new Date()))();
|
|
181
|
+
if (!Number.isFinite(now.getTime())) {
|
|
182
|
+
throw new Error("Could not determine a valid Vercel token expiry time.");
|
|
183
|
+
}
|
|
184
|
+
const expiresAt = now.getTime() + expiryDays * 24 * 60 * 60 * 1000;
|
|
185
|
+
if (!Number.isSafeInteger(expiresAt)) {
|
|
186
|
+
throw new Error("The requested Vercel token expiry is out of range.");
|
|
187
|
+
}
|
|
188
|
+
const expiry = new Date(expiresAt);
|
|
189
|
+
if (!Number.isFinite(expiry.getTime())) {
|
|
190
|
+
throw new Error("The requested Vercel token expiry is out of range.");
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
expiresAt,
|
|
194
|
+
expiresAtIso: expiry.toISOString(),
|
|
195
|
+
tokenScope,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function parseTimestamp(value) {
|
|
199
|
+
if (typeof value === "number" && Number.isSafeInteger(value))
|
|
200
|
+
return value;
|
|
201
|
+
if (typeof value !== "string" || !/^\d+$/.test(value))
|
|
202
|
+
return null;
|
|
203
|
+
const parsed = Number(value);
|
|
204
|
+
return Number.isSafeInteger(parsed) ? parsed : null;
|
|
205
|
+
}
|
|
206
|
+
function parseCreatedToken(text) {
|
|
207
|
+
const parsed = parseJsonOutput(text);
|
|
208
|
+
if (!isObject(parsed))
|
|
209
|
+
return null;
|
|
210
|
+
const metadata = isObject(parsed.token) ? parsed.token : parsed;
|
|
211
|
+
const bearerToken = typeof parsed.bearerToken === "string"
|
|
212
|
+
? parsed.bearerToken
|
|
213
|
+
: typeof metadata.bearerToken === "string"
|
|
214
|
+
? metadata.bearerToken
|
|
215
|
+
: null;
|
|
216
|
+
const expiresAt = parseTimestamp(metadata.expiresAt);
|
|
217
|
+
const id = typeof metadata.id === "string" ? metadata.id : null;
|
|
218
|
+
return bearerToken && expiresAt !== null && id
|
|
219
|
+
? { bearerToken, expiresAt, id }
|
|
220
|
+
: null;
|
|
221
|
+
}
|
|
222
|
+
function isAuthorizationDenial(result) {
|
|
223
|
+
if (result.code === 0)
|
|
224
|
+
return false;
|
|
225
|
+
const parsed = parseJsonOutput(`${result.stdout}\n${result.stderr}`);
|
|
226
|
+
const error = isObject(parsed) && isObject(parsed.error)
|
|
227
|
+
? parsed.error
|
|
228
|
+
: parsed;
|
|
229
|
+
const code = isObject(error) && typeof error.code === "string"
|
|
230
|
+
? error.code
|
|
231
|
+
: "";
|
|
232
|
+
return /unauthori[sz]ed|forbidden|access_denied/i.test(code) ||
|
|
233
|
+
/(?:HTTP\s*)?(?:401|403)\b|unauthori[sz]ed|forbidden/i.test(`${result.stderr}\n${result.stdout}`);
|
|
234
|
+
}
|
|
235
|
+
async function createAndValidateVercelToken(options, tokenScope, expiresAt) {
|
|
236
|
+
const body = {
|
|
237
|
+
expiresAt,
|
|
238
|
+
name: options.tokenName,
|
|
239
|
+
};
|
|
240
|
+
if (tokenScope === "project")
|
|
241
|
+
body.projectId = options.projectId;
|
|
242
|
+
let createResult;
|
|
243
|
+
try {
|
|
244
|
+
createResult = await options.commandRunner(options.vercelCommand, [
|
|
245
|
+
"api",
|
|
246
|
+
`/v3/user/tokens?teamId=${encodeURIComponent(options.orgId)}`,
|
|
247
|
+
"--method",
|
|
248
|
+
"POST",
|
|
249
|
+
"--input",
|
|
250
|
+
"-",
|
|
251
|
+
"--raw",
|
|
252
|
+
], {
|
|
253
|
+
cwd: options.cwd,
|
|
254
|
+
env: options.env,
|
|
255
|
+
input: JSON.stringify(body),
|
|
256
|
+
stdio: "pipe",
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
throw new Error(`Vercel did not confirm creation of token \`${options.tokenName}\`; provider output was suppressed. Review that token name in Vercel before retrying. Loom did not revoke any token.`);
|
|
261
|
+
}
|
|
262
|
+
if (createResult.code !== 0) {
|
|
263
|
+
if (/only user authentication tokens can be used to create new tokens/i.test(`${createResult.stderr}\n${createResult.stdout}`)) {
|
|
264
|
+
throw new Error(`Vercel token creation requires user-token authentication, but the current Vercel login cannot mint tokens. Create a bounded project-scoped token in the Vercel dashboard and install it through GitHub's hidden secret input for environment \`${options.environment}\`; Loom did not replace VERCEL_TOKEN or print provider output.`);
|
|
265
|
+
}
|
|
266
|
+
throw new Error(`Vercel did not confirm creation of token \`${options.tokenName}\`; provider output was suppressed. Review that token name in Vercel before retrying. Loom did not revoke any token.`);
|
|
267
|
+
}
|
|
268
|
+
const token = parseCreatedToken(createResult.stdout);
|
|
269
|
+
if (!token) {
|
|
270
|
+
throw new Error(`Vercel created token \`${options.tokenName}\`, but Loom could not read its one-time bearer value. Review or revoke that token in Vercel before retrying. Loom did not revoke it automatically.`);
|
|
271
|
+
}
|
|
272
|
+
if (token.expiresAt !== expiresAt) {
|
|
273
|
+
throw new Error(`Vercel created token \`${options.tokenName}\` with expiry metadata that did not match the request. Review or revoke it in Vercel before retrying. Loom did not install or revoke the token.`);
|
|
274
|
+
}
|
|
275
|
+
const validationEnv = { ...options.env, VERCEL_TOKEN: token.bearerToken };
|
|
276
|
+
let validationResult;
|
|
277
|
+
try {
|
|
278
|
+
validationResult = await options.commandRunner(options.vercelCommand, [
|
|
279
|
+
"api",
|
|
280
|
+
`/v9/projects/${encodeURIComponent(options.projectId)}?teamId=${encodeURIComponent(options.orgId)}`,
|
|
281
|
+
"--raw",
|
|
282
|
+
], {
|
|
283
|
+
cwd: options.cwd,
|
|
284
|
+
env: validationEnv,
|
|
285
|
+
stdio: "pipe",
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
throw new Error(`Vercel token \`${options.tokenName}\` was created but its linked-project access check did not complete. Review or revoke it in Vercel before retrying. Provider output was suppressed, and Loom did not revoke the token.`);
|
|
290
|
+
}
|
|
291
|
+
const validationBody = parseJsonOutput(validationResult.stdout);
|
|
292
|
+
if (validationResult.code !== 0 || !isObject(validationBody) ||
|
|
293
|
+
validationBody.id !== options.projectId) {
|
|
294
|
+
throw new Error(`Vercel token \`${options.tokenName}\` was created but did not validate access to the linked project. Review or revoke it in Vercel before retrying. Provider output was suppressed, and Loom did not revoke the token.`);
|
|
295
|
+
}
|
|
296
|
+
if (tokenScope === "project") {
|
|
297
|
+
let accountProbe;
|
|
298
|
+
try {
|
|
299
|
+
accountProbe = await options.commandRunner(options.vercelCommand, [
|
|
300
|
+
"api",
|
|
301
|
+
`/v2/teams/${encodeURIComponent(options.orgId)}`,
|
|
302
|
+
"--raw",
|
|
303
|
+
], {
|
|
304
|
+
cwd: options.cwd,
|
|
305
|
+
env: validationEnv,
|
|
306
|
+
stdio: "pipe",
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
throw new Error(`Vercel token \`${options.tokenName}\` was created, but Loom could not prove that team-level access is denied. Review or revoke it in Vercel before retrying. Provider output was suppressed, and Loom did not revoke the token.`);
|
|
311
|
+
}
|
|
312
|
+
if (!isAuthorizationDenial(accountProbe)) {
|
|
313
|
+
throw new Error(`Vercel token \`${options.tokenName}\` was created, but Loom could not prove that team-level access is denied. Review or revoke it in Vercel before retrying. Provider output was suppressed, and Loom did not revoke the token.`);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return token;
|
|
317
|
+
}
|
|
318
|
+
function formatExtraPolicies(policies) {
|
|
319
|
+
return policies.map((policy) => `${policy.type ?? "unknown"} \`${policy.name ?? "unnamed"}\``).join(", ");
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Audits GitHub release protections and installs a bounded Vercel credential
|
|
323
|
+
* without exposing token values in arguments or provider failures.
|
|
324
|
+
*/
|
|
325
|
+
async function configureGithubVercelReleaseCredential(options) {
|
|
326
|
+
const tokenSettings = resolveTokenSettings(options);
|
|
327
|
+
const environmentState = await inspectGithubEnvironment(options);
|
|
328
|
+
const credentialState = await readGithubCredentialState(options);
|
|
329
|
+
const hasExistingToken = credentialState.secretNames.has("VERCEL_TOKEN");
|
|
330
|
+
if (hasExistingToken) {
|
|
331
|
+
assertExistingTokenAssociation(options, credentialState.variables);
|
|
332
|
+
}
|
|
333
|
+
const extraPolicies = await ensureGithubBranchPolicy(options);
|
|
334
|
+
const notes = [];
|
|
335
|
+
notes.push(environmentState.created
|
|
336
|
+
? `Created GitHub environment \`${options.environment}\` with a custom deployment branch policy.`
|
|
337
|
+
: `Preserved the existing GitHub environment \`${options.environment}\` protection settings.`);
|
|
338
|
+
if (environmentState.protectionRuleTypes.length) {
|
|
339
|
+
notes.push(`Preserved GitHub environment protection rules: ${environmentState.protectionRuleTypes.join(", ")}.`);
|
|
340
|
+
}
|
|
341
|
+
notes.push(`Ensured branch \`${options.productionBranch}\` can deploy to GitHub environment \`${options.environment}\`.`);
|
|
342
|
+
if (extraPolicies.length) {
|
|
343
|
+
notes.push(`Additional GitHub deployment policies remain active: ${formatExtraPolicies(extraPolicies)}.`);
|
|
344
|
+
}
|
|
345
|
+
if (hasExistingToken && !options.rotateToken) {
|
|
346
|
+
const id = credentialState.variables.get(exports.VERCEL_TOKEN_ID_VARIABLE) ??
|
|
347
|
+
"unknown";
|
|
348
|
+
const scope = credentialState.variables.get(exports.VERCEL_TOKEN_SCOPE_VARIABLE) ??
|
|
349
|
+
"unknown";
|
|
350
|
+
const expiry = credentialState.variables.get(exports.VERCEL_TOKEN_EXPIRY_VARIABLE) ?? "unknown";
|
|
351
|
+
notes.push("Preserved the existing GitHub environment secret `VERCEL_TOKEN`; GitHub does not expose its value, so Loom did not claim that the token is valid.");
|
|
352
|
+
if (options.hasTokenOverrides) {
|
|
353
|
+
notes.push("Ignored token creation settings because --rotate-token was not provided.");
|
|
354
|
+
}
|
|
355
|
+
notes.push(`Recorded token metadata: id ${id}; scope ${scope}; expiry ${expiry}.`);
|
|
356
|
+
return { notes };
|
|
357
|
+
}
|
|
358
|
+
await setGithubVariable(options, "VERCEL_ORG_ID", options.orgId);
|
|
359
|
+
await setGithubVariable(options, "VERCEL_PROJECT_ID", options.projectId);
|
|
360
|
+
const token = await createAndValidateVercelToken(options, tokenSettings.tokenScope, tokenSettings.expiresAt);
|
|
361
|
+
let secretResult;
|
|
362
|
+
try {
|
|
363
|
+
secretResult = await options.commandRunner(options.githubCommand, ["secret", "set", "VERCEL_TOKEN", "--env", options.environment], {
|
|
364
|
+
cwd: options.cwd,
|
|
365
|
+
env: options.env,
|
|
366
|
+
input: token.bearerToken,
|
|
367
|
+
stdio: "pipe",
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
catch {
|
|
371
|
+
throw new Error(`GitHub did not confirm the VERCEL_TOKEN update for environment \`${options.environment}\`. GitHub may have accepted the write, so Loom left the new Vercel token active and did not revoke it. Check GitHub and Vercel before retrying; provider output was suppressed.`);
|
|
372
|
+
}
|
|
373
|
+
if (secretResult.code !== 0) {
|
|
374
|
+
throw new Error(`GitHub did not confirm the VERCEL_TOKEN update for environment \`${options.environment}\`. GitHub may have accepted the write, so Loom left the new Vercel token active and did not revoke it. Check GitHub and Vercel before retrying; provider output was suppressed.`);
|
|
375
|
+
}
|
|
376
|
+
try {
|
|
377
|
+
await setGithubVariable(options, exports.VERCEL_TOKEN_SCOPE_VARIABLE, tokenSettings.tokenScope);
|
|
378
|
+
await setGithubVariable(options, exports.VERCEL_TOKEN_EXPIRY_VARIABLE, tokenSettings.expiresAtIso);
|
|
379
|
+
await setGithubVariable(options, exports.VERCEL_TOKEN_ID_VARIABLE, token.id);
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
throw new Error(`GitHub accepted VERCEL_TOKEN, but Loom could not finish its non-secret token metadata. Loom left the new token installed and active; set ${exports.VERCEL_TOKEN_ID_VARIABLE}, ${exports.VERCEL_TOKEN_SCOPE_VARIABLE}, and ${exports.VERCEL_TOKEN_EXPIRY_VARIABLE} in GitHub environment \`${options.environment}\` before retrying.`);
|
|
383
|
+
}
|
|
384
|
+
notes.push(tokenSettings.tokenScope === "project"
|
|
385
|
+
? "Created and verified a project-scoped Vercel token, validated its access to the linked project and denial of team-level access, and stored it as GitHub environment secret `VERCEL_TOKEN`."
|
|
386
|
+
: "Created a Vercel token using the explicit team-scope compatibility request, validated its access to the linked project, and stored it as GitHub environment secret `VERCEL_TOKEN`.");
|
|
387
|
+
notes.push(`Recorded non-secret token metadata with requested scope ${tokenSettings.tokenScope} and expiry ${tokenSettings.expiresAtIso}.`);
|
|
388
|
+
if (hasExistingToken) {
|
|
389
|
+
notes.push("The previous Vercel token remains active. Revoke it manually only after a full release succeeds with the replacement.");
|
|
390
|
+
}
|
|
391
|
+
return { notes };
|
|
392
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Verified provider identities and the app-owned workflow to authorize. */
|
|
2
|
+
export interface GithubReleaseTrustTarget {
|
|
3
|
+
projectId: string;
|
|
4
|
+
teamId: string;
|
|
5
|
+
/** Canonical GitHub owner/repository, resolving any repository redirects. */
|
|
6
|
+
repository: string;
|
|
7
|
+
/** Immutable GitHub repository ID from the authenticated GitHub API. */
|
|
8
|
+
repositoryId: string;
|
|
9
|
+
productionBranch: string;
|
|
10
|
+
githubEnvironment: string;
|
|
11
|
+
workflowPath: string;
|
|
12
|
+
}
|
|
13
|
+
/** Reviewable, credential-free proposal; only the production HTTP gate changes. */
|
|
14
|
+
export interface GithubReleaseTrustPlan {
|
|
15
|
+
profile: "loom-github-release-trust-v1";
|
|
16
|
+
target: GithubReleaseTrustTarget;
|
|
17
|
+
expectedTrustedSourcesSha256: string;
|
|
18
|
+
/** Digest of the complete target, expected provider state and proposed patch. */
|
|
19
|
+
approvalSha256: string;
|
|
20
|
+
patch: {
|
|
21
|
+
trustedSources: Record<string, unknown>;
|
|
22
|
+
};
|
|
23
|
+
changed: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Add one exact GitHub identity rule while preserving every existing field/rule.
|
|
27
|
+
* This grants HTTP Deployment Protection access, not Vercel management access.
|
|
28
|
+
* Callers must obtain the project and canonical GitHub identity independently.
|
|
29
|
+
*/
|
|
30
|
+
export declare function prepareGithubReleaseTrust(project: unknown, target: GithubReleaseTrustTarget): GithubReleaseTrustPlan;
|
|
31
|
+
/**
|
|
32
|
+
* Rebuild immediately before PATCH and reject stale state or an altered plan.
|
|
33
|
+
* Vercel provides no atomic conditional update for this field: callers must also
|
|
34
|
+
* reread after PATCH and keep unrelated operators out of that apply window.
|
|
35
|
+
*/
|
|
36
|
+
export declare function verifyGithubReleaseTrustPlan(project: unknown, plan: GithubReleaseTrustPlan, reviewedPlanSha256: string): void;
|
|
37
|
+
/** Confirm exact provider state and identity after a reviewed update. */
|
|
38
|
+
export declare function verifyGithubReleaseTrustApplied(project: unknown, plan: GithubReleaseTrustPlan): void;
|
|
39
|
+
//# sourceMappingURL=releaseTrust.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"releaseTrust.d.ts","sourceRoot":"","sources":["../../../src/src/cli/releaseTrust.ts"],"names":[],"mappings":"AAKA,4EAA4E;AAC5E,MAAM,WAAW,wBAAwB;IACvC,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,UAAU,EAAE,MAAM,CAAC;IACnB,wEAAwE;IACxE,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,EAAE,MAAM,CAAC;CACtB;AAED,mFAAmF;AACnF,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,8BAA8B,CAAC;IACxC,MAAM,EAAE,wBAAwB,CAAC;IACjC,4BAA4B,EAAE,MAAM,CAAC;IACrC,iFAAiF;IACjF,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE;QAAE,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC;IACnD,OAAO,EAAE,OAAO,CAAC;CAClB;AA2ED;;;;GAIG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,wBAAwB,GAC/B,sBAAsB,CA8CxB;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,sBAAsB,EAC5B,kBAAkB,EAAE,MAAM,QAO3B;AAED,yEAAyE;AACzE,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,sBAAsB,QAO7B"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.prepareGithubReleaseTrust = prepareGithubReleaseTrust;
|
|
4
|
+
exports.verifyGithubReleaseTrustPlan = verifyGithubReleaseTrustPlan;
|
|
5
|
+
exports.verifyGithubReleaseTrustApplied = verifyGithubReleaseTrustApplied;
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const releaseConfig_js_1 = require("./releaseConfig.js");
|
|
8
|
+
const ISSUER = "https://token.actions.githubusercontent.com";
|
|
9
|
+
function record(value) {
|
|
10
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
11
|
+
}
|
|
12
|
+
function ordered(value) {
|
|
13
|
+
if (Array.isArray(value))
|
|
14
|
+
return value.map(ordered);
|
|
15
|
+
if (record(value)) {
|
|
16
|
+
return Object.fromEntries(Object.keys(value).sort().map((key) => [key, ordered(value[key])]));
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function digest(value) {
|
|
21
|
+
return (0, node_crypto_1.createHash)("sha256").update(JSON.stringify(ordered(value))).digest("hex");
|
|
22
|
+
}
|
|
23
|
+
function approvalDigest(plan) {
|
|
24
|
+
return digest({
|
|
25
|
+
profile: plan.profile,
|
|
26
|
+
target: plan.target,
|
|
27
|
+
expectedTrustedSourcesSha256: plan.expectedTrustedSourcesSha256,
|
|
28
|
+
patch: plan.patch,
|
|
29
|
+
changed: plan.changed,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function validateTarget(target) {
|
|
33
|
+
if (!record(target) ||
|
|
34
|
+
Object.keys(target).sort().join(",") !==
|
|
35
|
+
"githubEnvironment,productionBranch,projectId,repository,repositoryId,teamId,workflowPath" ||
|
|
36
|
+
Object.values(target).some((value) => typeof value !== "string" || /\p{Cc}/u.test(value)) ||
|
|
37
|
+
!/^[A-Za-z0-9_-]+$/.test(target.projectId) ||
|
|
38
|
+
!/^[A-Za-z0-9_-]+$/.test(target.teamId) ||
|
|
39
|
+
!/^[A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9_.-]+$/.test(target.repository) ||
|
|
40
|
+
[".", ".."].includes(target.repository.split("/")[1]) ||
|
|
41
|
+
!/^[1-9][0-9]*$/.test(target.repositoryId) ||
|
|
42
|
+
!target.githubEnvironment ||
|
|
43
|
+
target.githubEnvironment !== target.githubEnvironment.trim() ||
|
|
44
|
+
target.githubEnvironment.length > 255 ||
|
|
45
|
+
target.githubEnvironment.includes(",") ||
|
|
46
|
+
!/^\.github\/workflows\/[A-Za-z0-9_-][A-Za-z0-9_.-]*\.ya?ml$/.test(target.workflowPath))
|
|
47
|
+
throw new Error("Invalid GitHub release trust target.");
|
|
48
|
+
(0, releaseConfig_js_1.resolveProductionBranch)({ productionBranch: target.productionBranch });
|
|
49
|
+
}
|
|
50
|
+
function checkedProject(project, target) {
|
|
51
|
+
validateTarget(target);
|
|
52
|
+
if (!record(project) || project.id !== target.projectId ||
|
|
53
|
+
project.accountId !== target.teamId || !record(project.link) ||
|
|
54
|
+
project.link.type !== "github" ||
|
|
55
|
+
`${project.link.org}/${project.link.repo}` !== target.repository ||
|
|
56
|
+
String(project.link.repoId) !== target.repositoryId ||
|
|
57
|
+
project.link.productionBranch !== target.productionBranch) {
|
|
58
|
+
throw new Error("Vercel project identity or GitHub release link changed.");
|
|
59
|
+
}
|
|
60
|
+
const current = project.trustedSources ?? null;
|
|
61
|
+
if (current !== null && !record(current)) {
|
|
62
|
+
throw new Error("Invalid Vercel Trusted Sources configuration.");
|
|
63
|
+
}
|
|
64
|
+
return current;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Add one exact GitHub identity rule while preserving every existing field/rule.
|
|
68
|
+
* This grants HTTP Deployment Protection access, not Vercel management access.
|
|
69
|
+
* Callers must obtain the project and canonical GitHub identity independently.
|
|
70
|
+
*/
|
|
71
|
+
function prepareGithubReleaseTrust(project, target) {
|
|
72
|
+
const current = checkedProject(project, target);
|
|
73
|
+
const trustedSources = structuredClone(current ?? {});
|
|
74
|
+
const providers = trustedSources.oidcProviders ?? {};
|
|
75
|
+
if (!record(providers))
|
|
76
|
+
throw new Error("Invalid Trusted Sources providers.");
|
|
77
|
+
const rules = providers[ISSUER] ?? [];
|
|
78
|
+
if (!Array.isArray(rules) || rules.some((rule) => !record(rule))) {
|
|
79
|
+
throw new Error("Invalid GitHub Trusted Sources rules.");
|
|
80
|
+
}
|
|
81
|
+
const rule = {
|
|
82
|
+
claims: {
|
|
83
|
+
aud: [`https://github.com/${target.repository.split("/")[0]}`],
|
|
84
|
+
environment: [target.githubEnvironment],
|
|
85
|
+
ref: [`refs/heads/${target.productionBranch}`],
|
|
86
|
+
repository: [target.repository],
|
|
87
|
+
repository_id: [target.repositoryId],
|
|
88
|
+
workflow_ref: [
|
|
89
|
+
`${target.repository}/${target.workflowPath}@refs/heads/${target.productionBranch}`,
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
label: `GitHub ${target.githubEnvironment} release (${target.repository})`,
|
|
93
|
+
to: { slugs: ["production"] },
|
|
94
|
+
};
|
|
95
|
+
const ruleHash = digest(rule);
|
|
96
|
+
if (rules.some((existing) => existing.label === rule.label && digest(existing) !== ruleHash)) {
|
|
97
|
+
throw new Error("A different Trusted Sources rule uses this release label.");
|
|
98
|
+
}
|
|
99
|
+
if (!rules.some((existing) => digest(existing) === ruleHash)) {
|
|
100
|
+
providers[ISSUER] = [...rules, rule];
|
|
101
|
+
}
|
|
102
|
+
trustedSources.oidcProviders = providers;
|
|
103
|
+
const patch = { trustedSources };
|
|
104
|
+
const proposal = {
|
|
105
|
+
profile: "loom-github-release-trust-v1",
|
|
106
|
+
target: structuredClone(target),
|
|
107
|
+
expectedTrustedSourcesSha256: digest(current),
|
|
108
|
+
patch,
|
|
109
|
+
changed: digest(current) !== digest(trustedSources),
|
|
110
|
+
};
|
|
111
|
+
return { ...proposal, approvalSha256: approvalDigest(proposal) };
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Rebuild immediately before PATCH and reject stale state or an altered plan.
|
|
115
|
+
* Vercel provides no atomic conditional update for this field: callers must also
|
|
116
|
+
* reread after PATCH and keep unrelated operators out of that apply window.
|
|
117
|
+
*/
|
|
118
|
+
function verifyGithubReleaseTrustPlan(project, plan, reviewedPlanSha256) {
|
|
119
|
+
const rebuilt = prepareGithubReleaseTrust(project, plan.target);
|
|
120
|
+
if (reviewedPlanSha256 !== rebuilt.approvalSha256 ||
|
|
121
|
+
digest(plan) !== digest(rebuilt))
|
|
122
|
+
throw new Error("Trusted Sources state or reviewed plan changed.");
|
|
123
|
+
}
|
|
124
|
+
/** Confirm exact provider state and identity after a reviewed update. */
|
|
125
|
+
function verifyGithubReleaseTrustApplied(project, plan) {
|
|
126
|
+
const current = checkedProject(project, plan.target);
|
|
127
|
+
if (approvalDigest(plan) !== plan.approvalSha256 ||
|
|
128
|
+
digest(current) !== digest(plan.patch.trustedSources))
|
|
129
|
+
throw new Error("Vercel did not retain the exact reviewed trust state.");
|
|
130
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CommandRunner } from "./mod.js";
|
|
2
|
+
/** CLI transport dependencies; commands use the operator's existing CLI login. */
|
|
3
|
+
export interface GithubReleaseTrustCommandOptions {
|
|
4
|
+
cwd: string;
|
|
5
|
+
commandRunner: CommandRunner;
|
|
6
|
+
vercelCli: string;
|
|
7
|
+
env: Record<string, string | undefined>;
|
|
8
|
+
/** Required for planning; an explicit value must match a saved proposal. */
|
|
9
|
+
environment?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Plan, apply, or verify one reviewed GitHub production HTTP trust rule.
|
|
13
|
+
* Planning only reads providers and writes an exclusive local proposal file.
|
|
14
|
+
* Apply checks current state twice, requires the reviewed proposal digest, and
|
|
15
|
+
* verifies the exact resulting provider state. It never creates a deploy token.
|
|
16
|
+
*/
|
|
17
|
+
export declare function runGithubReleaseTrustCommand(action: "plan" | "apply" | "check", args: string[], options: GithubReleaseTrustCommandOptions): Promise<string[]>;
|
|
18
|
+
//# sourceMappingURL=releaseTrustCommand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"releaseTrustCommand.d.ts","sourceRoot":"","sources":["../../../src/src/cli/releaseTrustCommand.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAU9C,kFAAkF;AAClF,MAAM,WAAW,gCAAgC;IAC/C,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IACxC,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAgBD;;;;;GAKG;AACH,wBAAsB,4BAA4B,CAChD,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,EAClC,IAAI,EAAE,MAAM,EAAE,EACd,OAAO,EAAE,gCAAgC,GACxC,OAAO,CAAC,MAAM,EAAE,CAAC,CA4JnB"}
|