hammerkit 2.2.0-next.4 → 2.2.0-next.6
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/dist/harness/sandbox-client.js +51 -5
- package/dist/kubernetes/sync-changes.js +9 -2
- package/dist/server/git-checkout.js +74 -2
- package/dist/server/policy.js +46 -1
- package/dist/server/repo-access.js +48 -1
- package/dist/server/sandbox-files-api.js +15 -1
- package/dist/server/sandbox-files.js +21 -8
- package/dist/server/server-config.js +16 -0
- package/dist/server/server.js +43 -6
- package/package.json +1 -1
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.request = exports.quotePosix = exports.fetchFailure = exports.toSandboxPath = exports.checkSandboxPath = exports.exitCodeOf = exports.readRunLog = exports.downloadRunArtifact = exports.getRunArtifacts = exports.getRun = exports.followRun = exports.getMoveTarget = exports.lookupChecks = exports.submitRun = exports.FINAL_RUN_STATES = exports.readTextUpTo = exports.oneAtATime = exports.SandboxClient = exports.listPublishedTasks = exports.runCheck = exports.CHECK_DESCRIPTION = exports.getServerConnection = exports.SandboxClientError = exports.MAX_ENDPOINT_BYTES = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const path_1 = require("path");
|
|
6
|
+
const sandbox_patch_1 = require("../server/sandbox-patch");
|
|
6
7
|
const token_source_1 = require("../remote/token-source");
|
|
7
8
|
const reconnect_1 = require("./reconnect");
|
|
8
9
|
const fs_1 = require("fs");
|
|
@@ -339,20 +340,38 @@ class SandboxClient {
|
|
|
339
340
|
// The ops of an edit as they go to the server, with the paths the server checks. The first op on
|
|
340
341
|
// a file carries the hash the agent read of it. The server checks a file as the edit has changed
|
|
341
342
|
// it so far, so an op after one that changed its file, a move's destination included, or after a
|
|
342
|
-
// patch
|
|
343
|
+
// patch that changed it, carries none. A patch carries the hash of each file it reads, and a
|
|
344
|
+
// patch this client cannot read leaves the files of the batch unknown, so the ops after it carry
|
|
345
|
+
// none. An invalid path goes as it is.
|
|
343
346
|
withHashes(ops) {
|
|
344
347
|
const changed = new Set();
|
|
345
|
-
let
|
|
348
|
+
let unknown = false;
|
|
346
349
|
return ops.map((op) => {
|
|
347
350
|
if (op.op === 'patch') {
|
|
348
|
-
|
|
349
|
-
|
|
351
|
+
const files = patchFiles(op.patch);
|
|
352
|
+
if (files === undefined) {
|
|
353
|
+
unknown = true;
|
|
354
|
+
return op;
|
|
355
|
+
}
|
|
356
|
+
const hashes = {};
|
|
357
|
+
for (const key of files.read) {
|
|
358
|
+
const hash = !unknown && !changed.has(key) ? this.seen.get(key) : undefined;
|
|
359
|
+
if (hash !== undefined) {
|
|
360
|
+
hashes[key] = hash;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
for (const key of files.changed) {
|
|
364
|
+
changed.add(key);
|
|
365
|
+
}
|
|
366
|
+
// the hashes the caller gave win over the ones read here
|
|
367
|
+
const merged = { ...hashes, ...op.ifHash };
|
|
368
|
+
return Object.keys(merged).length > 0 ? { ...op, ifHash: merged } : op;
|
|
350
369
|
}
|
|
351
370
|
const key = fileKey(op.path);
|
|
352
371
|
if (key === undefined) {
|
|
353
372
|
return op;
|
|
354
373
|
}
|
|
355
|
-
const hash = !
|
|
374
|
+
const hash = !unknown && !changed.has(key) ? this.seen.get(key) : undefined;
|
|
356
375
|
changed.add(key);
|
|
357
376
|
const to = op.op === 'move' ? fileKey(op.to) : undefined;
|
|
358
377
|
if (to !== undefined) {
|
|
@@ -390,6 +409,33 @@ function fileKey(path) {
|
|
|
390
409
|
return undefined;
|
|
391
410
|
}
|
|
392
411
|
}
|
|
412
|
+
// The keys of the files a patch reads and of the files it changes; undefined when the patch does
|
|
413
|
+
// not parse. A copy leaves its source as it was, so the source is read but not changed.
|
|
414
|
+
function patchFiles(text) {
|
|
415
|
+
let files;
|
|
416
|
+
try {
|
|
417
|
+
files = (0, sandbox_patch_1.parsePatch)(text);
|
|
418
|
+
}
|
|
419
|
+
catch {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
const read = [];
|
|
423
|
+
const changed = [];
|
|
424
|
+
for (const file of files) {
|
|
425
|
+
const from = file.from === undefined ? undefined : fileKey(file.from);
|
|
426
|
+
const to = file.to === undefined ? undefined : fileKey(file.to);
|
|
427
|
+
if (from !== undefined) {
|
|
428
|
+
read.push(from);
|
|
429
|
+
if (!file.copy) {
|
|
430
|
+
changed.push(from);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (to !== undefined) {
|
|
434
|
+
changed.push(to);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return { read, changed };
|
|
438
|
+
}
|
|
393
439
|
const SHA256 = /^[0-9a-f]{64}$/;
|
|
394
440
|
function sha256Of(content) {
|
|
395
441
|
return (0, crypto_1.createHash)('sha256').update(content).digest('hex');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.restartPods = exports.syncChangedFiles = exports.podFolderOf = exports.SYNC_CHANGES = exports.APPLY_PATHS = exports.getChangeTarget = exports.CHANGES_STAGING_DIR = void 0;
|
|
3
|
+
exports.restartPods = exports.syncChangedFiles = exports.readArchive = exports.podFolderOf = exports.SYNC_CHANGES = exports.APPLY_PATHS = exports.getChangeTarget = exports.CHANGES_STAGING_DIR = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const promises_1 = require("fs/promises");
|
|
6
6
|
const os_1 = require("os");
|
|
@@ -196,7 +196,13 @@ async function readArchive(archive) {
|
|
|
196
196
|
await new Promise((resolve, reject) => {
|
|
197
197
|
const parser = new tar_1.Parser({
|
|
198
198
|
onReadEntry: (entry) => {
|
|
199
|
-
|
|
199
|
+
// the mtime is kept, so the files are packed again with the times the sandbox gave them
|
|
200
|
+
const file = {
|
|
201
|
+
path: entry.path,
|
|
202
|
+
mode: entry.mode ?? 0o644,
|
|
203
|
+
content: Buffer.alloc(0),
|
|
204
|
+
mtime: entry.mtime,
|
|
205
|
+
};
|
|
200
206
|
files.push(file);
|
|
201
207
|
const chunks = [];
|
|
202
208
|
entry.on('data', (chunk) => chunks.push(chunk));
|
|
@@ -209,6 +215,7 @@ async function readArchive(archive) {
|
|
|
209
215
|
});
|
|
210
216
|
return files;
|
|
211
217
|
}
|
|
218
|
+
exports.readArchive = readArchive;
|
|
212
219
|
// `data` as a stream of pieces of PIECE_BYTES: an exec's standard input takes them as they come
|
|
213
220
|
function piecesOf(data) {
|
|
214
221
|
return stream_1.Readable.from((function* () {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.git = exports.remoteBranch = exports.isValidBranch = exports.assertBranchName = exports.resolveRef = exports.GitOutputTooLarge = exports.RefError = exports.checkoutRef = exports.isValidRef = void 0;
|
|
3
|
+
exports.git = exports.commitOnBranch = exports.remoteBranch = exports.isValidBranch = exports.assertBranchName = exports.resolveRef = exports.GitOutputTooLarge = exports.RefError = exports.checkoutRef = exports.isValidRef = void 0;
|
|
4
4
|
const child_process_1 = require("child_process");
|
|
5
5
|
const promises_1 = require("fs/promises");
|
|
6
|
+
const os_1 = require("os");
|
|
7
|
+
const path_1 = require("path");
|
|
6
8
|
const util_1 = require("util");
|
|
7
9
|
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
8
10
|
// Anything but plain network and local transports is refused: `ext::` runs a command.
|
|
@@ -130,6 +132,76 @@ async function remoteBranch(repository, branch, env, signal) {
|
|
|
130
132
|
return undefined;
|
|
131
133
|
}
|
|
132
134
|
exports.remoteBranch = remoteBranch;
|
|
135
|
+
// What git says when a commit is not in the repository (its exit code is 128)
|
|
136
|
+
const MISSING_COMMIT = /Not a valid (object|commit) name/;
|
|
137
|
+
// A failed git command: its exit code and the last line git printed
|
|
138
|
+
class GitFailure extends Error {
|
|
139
|
+
constructor(message, exitCode, reason) {
|
|
140
|
+
super(message);
|
|
141
|
+
this.exitCode = exitCode;
|
|
142
|
+
this.reason = reason;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Runs git for the answer in its exit code. `answer` says which exit codes, with git's last
|
|
146
|
+
// line, are answers; any other failure throws as `git` does.
|
|
147
|
+
async function gitCode(args, env, signal, what, answer) {
|
|
148
|
+
try {
|
|
149
|
+
await git(args, env, signal, what, 'check');
|
|
150
|
+
return { code: 0, reason: '' };
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
if (e instanceof GitFailure && e.exitCode !== undefined && answer(e.exitCode, e.reason)) {
|
|
154
|
+
return { code: e.exitCode, reason: e.reason };
|
|
155
|
+
}
|
|
156
|
+
throw e;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether `commit` is in the history of `branch` of a repository. The history of the branch is
|
|
161
|
+
* fetched once, without trees or blobs, into a scratch repository. A commit that is not there
|
|
162
|
+
* after that fetch is not on the branch. A branch that is missing, or a fetch that fails,
|
|
163
|
+
* throws: the caller refuses then, it never reads that as "not on the branch".
|
|
164
|
+
*/
|
|
165
|
+
async function commitOnBranch(repository, commit, branch, env, signal) {
|
|
166
|
+
assertBranchName(branch);
|
|
167
|
+
if (!COMMIT.test(commit)) {
|
|
168
|
+
throw new RefError(`${JSON.stringify(commit.slice(0, 80))} is not a commit hash`);
|
|
169
|
+
}
|
|
170
|
+
// a commit the scratch repository lacks must not make git fetch it from a promisor remote
|
|
171
|
+
const gitEnv = { ...env, GIT_NO_LAZY_FETCH: '1' };
|
|
172
|
+
const directory = await (0, promises_1.mkdtemp)((0, path_1.join)((0, os_1.tmpdir)(), 'hammerkit-commit-on-'));
|
|
173
|
+
const what = `the history of ${repository} at ${branch}`;
|
|
174
|
+
try {
|
|
175
|
+
await git(['init', '--quiet', '--bare', directory], gitEnv, signal, 'a scratch repository', 'prepare');
|
|
176
|
+
await git([
|
|
177
|
+
'-C',
|
|
178
|
+
directory,
|
|
179
|
+
'fetch',
|
|
180
|
+
'--quiet',
|
|
181
|
+
'--no-tags',
|
|
182
|
+
// commits only: the history is enough to tell whether one is in it
|
|
183
|
+
'--filter=tree:0',
|
|
184
|
+
'--',
|
|
185
|
+
repository,
|
|
186
|
+
`refs/heads/${branch}:refs/hammerkit/branch`,
|
|
187
|
+
], gitEnv, signal, `${repository} at ${branch}`);
|
|
188
|
+
// exit 0: an ancestor, exit 1: not one. Exit 128 with git saying the commit is not there
|
|
189
|
+
// is not on the branch, once cat-file says the same; any other failure throws
|
|
190
|
+
const ancestor = await gitCode(['-C', directory, 'merge-base', '--is-ancestor', commit, 'refs/hammerkit/branch'], gitEnv, signal, what, (code, reason) => code === 0 || code === 1 || (code === 128 && MISSING_COMMIT.test(reason)));
|
|
191
|
+
if (ancestor.code !== 128) {
|
|
192
|
+
return ancestor.code === 0;
|
|
193
|
+
}
|
|
194
|
+
const present = await gitCode(['-C', directory, 'cat-file', '-e', `${commit}^{commit}`], gitEnv, signal, what, (code, reason) => code === 0 || (code === 128 && MISSING_COMMIT.test(reason)));
|
|
195
|
+
if (present.code === 0) {
|
|
196
|
+
throw new Error(`unable to check ${what}: git could not compare ${commit} with the branch`);
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
await (0, promises_1.rm)(directory, { recursive: true, force: true });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.commitOnBranch = commitOnBranch;
|
|
133
205
|
// Runs git without a prompt and with the network transports only. Errors say what could
|
|
134
206
|
// not be done (`unable to <action> <what>`) with the last line git printed.
|
|
135
207
|
async function git(args, baseEnv, signal, what, action = 'fetch',
|
|
@@ -172,7 +244,7 @@ options = {}) {
|
|
|
172
244
|
.map((line) => line.trim())
|
|
173
245
|
.filter((line) => line.length > 0)
|
|
174
246
|
.pop() ?? error.message;
|
|
175
|
-
throw new
|
|
247
|
+
throw new GitFailure(`unable to ${action} ${what}: ${reason}`, typeof error.code === 'number' ? error.code : undefined, reason);
|
|
176
248
|
}
|
|
177
249
|
}
|
|
178
250
|
exports.git = git;
|
package/dist/server/policy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.decideRun = exports.sandboxRepos = exports.decideSandbox = exports.sandboxPushBranches = exports.sandboxBranchType = exports.endpointLevel = exports.getGrant = exports.usedAccounts = exports.mayYank = exports.accountLevel = exports.canUse = exports.maySeeDefinitions = exports.mayPublish = exports.mayPropose = exports.mayReadChecks = exports.mayRun = exports.matches = exports.subjectName = void 0;
|
|
3
|
+
exports.decideRun = exports.commitOnBranches = exports.sandboxRepos = exports.decideSandbox = exports.sandboxPushBranches = exports.sandboxBranchType = exports.endpointLevel = exports.getGrant = exports.usedAccounts = exports.mayYank = exports.accountLevel = exports.canUse = exports.maySeeDefinitions = exports.mayPublish = exports.mayPropose = exports.mayReadChecks = exports.mayRun = exports.matches = exports.subjectName = void 0;
|
|
4
4
|
const repo_1 = require("./repo");
|
|
5
5
|
const BUILDS_ORDER = ['run', 'propose', 'publish'];
|
|
6
6
|
// The name an entitlement gives a subject, for the pages to name its runs by when its tokens
|
|
@@ -214,6 +214,44 @@ function checkRepo(repos, repo, readable, what) {
|
|
|
214
214
|
return null;
|
|
215
215
|
}
|
|
216
216
|
const COMMIT = /^[0-9a-f]{40}([0-9a-f]{24})?$/i;
|
|
217
|
+
// The branches a run from a ref must have its commit on: the `commitOn` of the accounts it uses.
|
|
218
|
+
// Its gates mirror decideRun's, so the server reads no repository for a run it refuses. Empty
|
|
219
|
+
// when no account names branches.
|
|
220
|
+
function commitOnBranches(input) {
|
|
221
|
+
const { grant, accounts, catalog, source, readable } = input;
|
|
222
|
+
if (source.type !== 'ref' || !(0, exports.mayRun)(grant) || !(0, repo_1.matchesAnyRepo)(grant.repos, source.repo) || !readable(source.repo)) {
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
const branches = new Set();
|
|
226
|
+
for (const account of accounts) {
|
|
227
|
+
const rules = Object.prototype.hasOwnProperty.call(catalog, account) ? catalog[account] : undefined;
|
|
228
|
+
if (accountLevel(grant, account) !== undefined && rules?.commitOn) {
|
|
229
|
+
rules.commitOn.forEach((branch) => branches.add(branch));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return [...branches];
|
|
233
|
+
}
|
|
234
|
+
exports.commitOnBranches = commitOnBranches;
|
|
235
|
+
// An account with `commitOn` takes a run only when the commit is on one of its branches. A check
|
|
236
|
+
// that failed or was not made refuses too: it never passes for want of an answer.
|
|
237
|
+
function commitOnProblem(account, branches, onBranches) {
|
|
238
|
+
const checks = branches.map((branch) => onBranches?.[branch]);
|
|
239
|
+
if (checks.includes(true)) {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
const reason = checks.find((check) => typeof check === 'string');
|
|
243
|
+
if (reason !== undefined || checks.includes(undefined)) {
|
|
244
|
+
const why = reason ?? 'the server could not check the branch';
|
|
245
|
+
return {
|
|
246
|
+
status: 409,
|
|
247
|
+
error: `the commit of the run could not be checked to be on ${branches.join(', ')} for the account ${account}: ${why}`,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
status: 403,
|
|
252
|
+
error: `the account ${account} is only used by runs from commits on ${branches.join(', ')}, this commit is on none of them`,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
217
255
|
// The single place that decides what happens to a run that was submitted: it
|
|
218
256
|
// starts, waits for an approval, or is refused with the reason.
|
|
219
257
|
function decideRun(input) {
|
|
@@ -259,6 +297,13 @@ function decideRun(input) {
|
|
|
259
297
|
};
|
|
260
298
|
}
|
|
261
299
|
}
|
|
300
|
+
// the config takes `commitOn` only with `source: [ref]`, so this run is from a ref and has a commit
|
|
301
|
+
if (rules?.commitOn && !input.commitPinned) {
|
|
302
|
+
const problem = commitOnProblem(account, rules.commitOn, input.onBranches);
|
|
303
|
+
if (problem) {
|
|
304
|
+
return { type: 'refuse', ...problem };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
262
307
|
if (level === 'request') {
|
|
263
308
|
if (!approvable(account)) {
|
|
264
309
|
return { type: 'refuse', status: 409, error: `nobody can approve ${account}, no entitlement uses it` };
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RepoAccess = void 0;
|
|
3
|
+
exports.RepoAccess = exports.BRANCH_CHECK_FAILED = void 0;
|
|
4
|
+
const limit_1 = require("../utils/limit");
|
|
4
5
|
const redact_1 = require("../utils/redact");
|
|
5
6
|
const provider_values_1 = require("../secrets/provider-values");
|
|
6
7
|
const secret_catalog_1 = require("../secrets/secret-catalog");
|
|
7
8
|
const git_checkout_1 = require("./git-checkout");
|
|
8
9
|
const repo_1 = require("./repo");
|
|
10
|
+
function errorText(error) {
|
|
11
|
+
return error instanceof Error ? error.message : String(error);
|
|
12
|
+
}
|
|
9
13
|
// A token is read again after this long, so a short-lived one (a GitHub App
|
|
10
14
|
// installation token minted by the provider command) is not used up and a revoked one
|
|
11
15
|
// stops working soon.
|
|
12
16
|
const TOKEN_TTL_MS = 60 * 1000;
|
|
13
17
|
// A run waits for the repository at submit, not forever
|
|
14
18
|
const RESOLVE_TIMEOUT_MS = 30 * 1000;
|
|
19
|
+
exports.BRANCH_CHECK_FAILED = 'the branch check failed';
|
|
20
|
+
// Branch checks that run at once on the server: each fetches a branch's history
|
|
21
|
+
const BRANCH_CHECKS_AT_ONCE = 2;
|
|
22
|
+
const branchChecks = (0, limit_1.createLimit)(BRANCH_CHECKS_AT_ONCE);
|
|
15
23
|
/**
|
|
16
24
|
* The repositories a server can run code from. What may be read is the operator's
|
|
17
25
|
* `sources` list; who may name which of them is the entitlement's `repos`. The read
|
|
@@ -57,6 +65,45 @@ class RepoAccess {
|
|
|
57
65
|
throw this.scrub(e, credentials.values);
|
|
58
66
|
}
|
|
59
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* The commit a ref points to now, and for each of `branches` whether that commit is on it. A
|
|
70
|
+
* check that fails is `BRANCH_CHECK_FAILED`, with its detail in `errors` for the log. Each
|
|
71
|
+
* branch gets its own timeout, and the checks of the server run a few at once.
|
|
72
|
+
*/
|
|
73
|
+
async commitBranches(repo, ref, branches, signal) {
|
|
74
|
+
const commit = await this.resolve(repo, ref, signal);
|
|
75
|
+
const on = Object.create(null);
|
|
76
|
+
const errors = Object.create(null);
|
|
77
|
+
for (const branch of branches) {
|
|
78
|
+
const checked = await this.checkBranch(repo, commit, branch, signal);
|
|
79
|
+
if ('error' in checked) {
|
|
80
|
+
on[branch] = exports.BRANCH_CHECK_FAILED;
|
|
81
|
+
errors[branch] = checked.error;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
on[branch] = checked.on;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { commit, on, errors };
|
|
88
|
+
}
|
|
89
|
+
checkBranch(repo, commit, branch, signal) {
|
|
90
|
+
return branchChecks(async () => {
|
|
91
|
+
const timeout = AbortSignal.timeout(RESOLVE_TIMEOUT_MS);
|
|
92
|
+
const abort = signal ? AbortSignal.any([signal, timeout]) : timeout;
|
|
93
|
+
if (this.options.onBranch) {
|
|
94
|
+
return this.options.onBranch(repo, commit, branch, abort).then((on) => ({ on }), (e) => ({ error: errorText(e) }));
|
|
95
|
+
}
|
|
96
|
+
let values = [];
|
|
97
|
+
try {
|
|
98
|
+
const credentials = await this.credentials(repo);
|
|
99
|
+
values = credentials.values;
|
|
100
|
+
return { on: await (0, git_checkout_1.commitOnBranch)(this.url(repo), commit, branch, credentials.env, abort) };
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
return { error: errorText(this.scrub(e, values)) };
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
60
107
|
/**
|
|
61
108
|
* The environment a git process needs to read the repository: the allowlisted
|
|
62
109
|
* variables of the server, and the token as a header of requests to that host. The
|
|
@@ -8,6 +8,8 @@ const sandbox_files_1 = require("./sandbox-files");
|
|
|
8
8
|
const sandboxes_1 = require("./sandboxes");
|
|
9
9
|
// a batch of edits carries the content it writes
|
|
10
10
|
exports.MAX_EDITS_BODY_BYTES = 16 * 1024 * 1024;
|
|
11
|
+
// the files one patch may name in its ifHash
|
|
12
|
+
const MAX_IF_HASH_FILES = 1000;
|
|
11
13
|
const hashSchema = zod_1.z.string().regex(/^[0-9a-fA-F]{64}$/, 'expected the sha256 of the file, 64 hex digits');
|
|
12
14
|
const opSchema = zod_1.z.discriminatedUnion('op', [
|
|
13
15
|
zod_1.z
|
|
@@ -31,7 +33,19 @@ const opSchema = zod_1.z.discriminatedUnion('op', [
|
|
|
31
33
|
.strict(),
|
|
32
34
|
zod_1.z.object({ op: zod_1.z.literal('delete'), path: zod_1.z.string(), ifHash: hashSchema.optional() }).strict(),
|
|
33
35
|
zod_1.z.object({ op: zod_1.z.literal('move'), path: zod_1.z.string(), to: zod_1.z.string(), ifHash: hashSchema.optional() }).strict(),
|
|
34
|
-
zod_1.z
|
|
36
|
+
zod_1.z
|
|
37
|
+
.object({
|
|
38
|
+
op: zod_1.z.literal('patch'),
|
|
39
|
+
patch: zod_1.z.string().min(1),
|
|
40
|
+
// the files a patch reads, at most this many, each at most a path of checkPath
|
|
41
|
+
ifHash: zod_1.z
|
|
42
|
+
.record(zod_1.z.string().max(sandbox_files_1.MAX_PATH_LENGTH), hashSchema)
|
|
43
|
+
.refine((map) => Object.keys(map).length <= MAX_IF_HASH_FILES, {
|
|
44
|
+
message: `at most ${MAX_IF_HASH_FILES} files`,
|
|
45
|
+
})
|
|
46
|
+
.optional(),
|
|
47
|
+
})
|
|
48
|
+
.strict(),
|
|
35
49
|
]);
|
|
36
50
|
const editsSchema = zod_1.z.object({ dryRun: zod_1.z.boolean().optional(), ops: zod_1.z.array(opSchema).min(1).max(1000) }).strict();
|
|
37
51
|
// grep tools name the flags `-i`, `-A`: both spellings are taken
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HostSandboxFiles = exports.isRunState = exports.RUN_STATE_DIRECTORY = exports.deniedPath = exports.checkPath = exports.SandboxFileError = exports.MAX_EDIT_SERVER_BYTES = exports.MAX_EDIT_BATCH_WORK = exports.MAX_EDIT_BATCH_BYTES = exports.MAX_CHANGES_BYTES = exports.READ_LINES = exports.MAX_FILE_BYTES = void 0;
|
|
3
|
+
exports.HostSandboxFiles = exports.isRunState = exports.RUN_STATE_DIRECTORY = exports.deniedPath = exports.checkPath = exports.MAX_PATH_LENGTH = exports.SandboxFileError = exports.MAX_EDIT_SERVER_BYTES = exports.MAX_EDIT_BATCH_WORK = exports.MAX_EDIT_BATCH_BYTES = exports.MAX_CHANGES_BYTES = exports.READ_LINES = exports.MAX_FILE_BYTES = void 0;
|
|
4
4
|
const child_process_1 = require("child_process");
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
6
|
const fs_1 = require("fs");
|
|
@@ -51,12 +51,14 @@ class SandboxFileError extends Error {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
exports.SandboxFileError = SandboxFileError;
|
|
54
|
+
// the longest path of a file the API takes
|
|
55
|
+
exports.MAX_PATH_LENGTH = 4096;
|
|
54
56
|
/**
|
|
55
57
|
* A path as the API takes it: relative, `/` between names, no `.` or `..`, and nothing the
|
|
56
58
|
* deny list of uploads names (`.git/`, `.env`, keys).
|
|
57
59
|
*/
|
|
58
60
|
function checkPath(path) {
|
|
59
|
-
if (!(0, project_path_1.isRelativePath)(path,
|
|
61
|
+
if (!(0, project_path_1.isRelativePath)(path, exports.MAX_PATH_LENGTH)) {
|
|
60
62
|
throw new SandboxFileError(400, `${JSON.stringify(path.slice(0, 200))} is not a path in the sandbox: relative, / between names, no . or ..`);
|
|
61
63
|
}
|
|
62
64
|
const denied = deniedPath(path);
|
|
@@ -1156,9 +1158,7 @@ class EditBatch {
|
|
|
1156
1158
|
const state = await this.current(path);
|
|
1157
1159
|
const actual = state.kind === 'file' ? sha256(await this.content(path, state)) : undefined;
|
|
1158
1160
|
if (actual !== ifHash.toLowerCase()) {
|
|
1159
|
-
throw new SandboxFileError(412, actual
|
|
1160
|
-
? `${path} changed since you read it (sha256 ${actual}), read it again`
|
|
1161
|
-
: `${path} does not exist any more, read it again`);
|
|
1161
|
+
throw new SandboxFileError(412, actual ? `${path} changed since you read it, read it again` : `${path} does not exist any more, read it again`);
|
|
1162
1162
|
}
|
|
1163
1163
|
}
|
|
1164
1164
|
// A path a file may be written to: nothing but directories above it, no directory there.
|
|
@@ -1192,7 +1192,7 @@ class EditBatch {
|
|
|
1192
1192
|
}
|
|
1193
1193
|
async apply(op) {
|
|
1194
1194
|
if (op.op === 'patch') {
|
|
1195
|
-
return this.patch(op.patch);
|
|
1195
|
+
return this.patch(op.patch, op.ifHash);
|
|
1196
1196
|
}
|
|
1197
1197
|
const path = checkPath(op.path);
|
|
1198
1198
|
await this.checkHash(path, op.ifHash);
|
|
@@ -1261,8 +1261,21 @@ class EditBatch {
|
|
|
1261
1261
|
this.set(to, content, state.exec);
|
|
1262
1262
|
}
|
|
1263
1263
|
}
|
|
1264
|
-
async patch(text) {
|
|
1265
|
-
|
|
1264
|
+
async patch(text, ifHash) {
|
|
1265
|
+
const files = (0, sandbox_patch_1.parsePatch)(text);
|
|
1266
|
+
// the files the patch reads: a hash is only for one of them
|
|
1267
|
+
const read = new Set(files.flatMap((file) => (file.from === undefined ? [] : [checkPath(file.from)])));
|
|
1268
|
+
const hashes = Object.entries(ifHash ?? {}).map(([path, hash]) => [checkPath(path), hash]);
|
|
1269
|
+
for (const [path] of hashes) {
|
|
1270
|
+
if (!read.has(path)) {
|
|
1271
|
+
throw new sandbox_patch_1.PatchError(`${path} is in ifHash, but the patch does not read it`);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
// all the hashes are checked before the first file changes, so a refused patch changes none
|
|
1275
|
+
for (const [path, hash] of hashes) {
|
|
1276
|
+
await this.checkHash(path, hash);
|
|
1277
|
+
}
|
|
1278
|
+
for (const file of files) {
|
|
1266
1279
|
await this.patchFile(file);
|
|
1267
1280
|
}
|
|
1268
1281
|
}
|
|
@@ -95,6 +95,15 @@ exports.accountSchema = zod_1.z
|
|
|
95
95
|
// the refs (branches or tags, `release/*` for a level) a run using this account
|
|
96
96
|
// may be started from; a commit is not accepted then, it names no branch
|
|
97
97
|
refs: zod_1.z.array(zod_1.z.string().min(1).max(200)).min(1).optional(),
|
|
98
|
+
// the branches a run from a ref is taken only when its commit is in the history of one of
|
|
99
|
+
// them: a tag or a commit that was never reviewed on these is refused. A name, not a pattern.
|
|
100
|
+
commitOn: zod_1.z
|
|
101
|
+
.array(zod_1.z
|
|
102
|
+
.string()
|
|
103
|
+
.regex(/^[A-Za-z0-9][A-Za-z0-9._/-]*$/, 'a branch name, not a pattern')
|
|
104
|
+
.max(200))
|
|
105
|
+
.min(1)
|
|
106
|
+
.optional(),
|
|
98
107
|
// On a Kubernetes backend: the credential the commands of the account's secrets
|
|
99
108
|
// get in the Job of a task, under /var/run/hammerkit/account. The server holds
|
|
100
109
|
// none of the secret manager's credentials.
|
|
@@ -123,6 +132,13 @@ exports.accountSchema = zod_1.z
|
|
|
123
132
|
message: 'refs only restricts runs from a git ref, set source: [ref] too, an upload names no ref',
|
|
124
133
|
});
|
|
125
134
|
}
|
|
135
|
+
if (account.commitOn && (account.source?.length !== 1 || account.source[0] !== 'ref')) {
|
|
136
|
+
ctx.addIssue({
|
|
137
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
138
|
+
path: ['commitOn'],
|
|
139
|
+
message: 'commitOn only restricts runs from a git ref, set source: [ref] too, a sandbox or an upload names no commit',
|
|
140
|
+
});
|
|
141
|
+
}
|
|
126
142
|
});
|
|
127
143
|
// The capabilities (`capAdd`) a task of a run may add, and the sandboxed runtime its
|
|
128
144
|
// container then runs in, so they hold in the sandbox's own kernel and never in the host's
|
package/dist/server/server.js
CHANGED
|
@@ -348,6 +348,8 @@ function refuseWake(config, repos, run) {
|
|
|
348
348
|
// an account it may only request is refused, the wake has nobody to approve it
|
|
349
349
|
approvable: () => true,
|
|
350
350
|
readable: (repo) => repos.readable(repo),
|
|
351
|
+
// a run with a commit was checked at submit and stays pinned to it: no second check here
|
|
352
|
+
commitPinned: run.source.commit !== undefined,
|
|
351
353
|
});
|
|
352
354
|
if (decision.type === 'refuse') {
|
|
353
355
|
return `its owner ${run.owner.sub} may no longer run it: ${decision.error.replace(/^you /, 'they ')}`;
|
|
@@ -611,17 +613,52 @@ async function startRun(config, registry, definitions, sources, sandboxes, plann
|
|
|
611
613
|
(0, http_2.send)(res, 403, { error });
|
|
612
614
|
return;
|
|
613
615
|
}
|
|
616
|
+
const submitted = request.source.type === 'ref'
|
|
617
|
+
? { type: 'ref', repo: request.source.repo, ref: request.source.ref }
|
|
618
|
+
: sandbox
|
|
619
|
+
? { type: 'sandbox', repo: sandbox.repo, branch: sandbox.ref }
|
|
620
|
+
: { type: 'upload' };
|
|
621
|
+
// the commit of a run from a ref is looked for on the branches its accounts name in `commitOn`,
|
|
622
|
+
// here where the repository is, and the run pins that same commit below
|
|
623
|
+
const commitOn = (0, policy_1.commitOnBranches)({
|
|
624
|
+
grant,
|
|
625
|
+
accounts: planned.accounts,
|
|
626
|
+
catalog: config.accounts,
|
|
627
|
+
source: submitted,
|
|
628
|
+
readable: (repo) => repos.readable(repo),
|
|
629
|
+
});
|
|
630
|
+
let checkedCommit;
|
|
631
|
+
let onBranches;
|
|
632
|
+
if (commitOn.length > 0 && request.source.type === 'ref') {
|
|
633
|
+
// a caller that hangs up frees its place in the queue of the branch checks
|
|
634
|
+
const hungUp = new AbortController();
|
|
635
|
+
res.on('close', () => hungUp.abort());
|
|
636
|
+
try {
|
|
637
|
+
const checked = await repos.commitBranches(request.source.repo, request.source.ref, commitOn, hungUp.signal);
|
|
638
|
+
checkedCommit = checked.commit;
|
|
639
|
+
onBranches = checked.on;
|
|
640
|
+
// the detail of a failed check, for the log: the run is refused with the fixed text only
|
|
641
|
+
for (const [branch, detail] of Object.entries(checked.errors)) {
|
|
642
|
+
log(`commitOn check of ${request.source.repo} at ${checked.commit} on ${branch} failed: ${detail}`);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
catch (e) {
|
|
646
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
647
|
+
log(`refuse run iss=${principal.iss} sub=${principal.sub}: ${message}`);
|
|
648
|
+
(0, http_2.send)(res, e instanceof git_checkout_1.RefError ? 400 : 502, {
|
|
649
|
+
error: e instanceof git_checkout_1.RefError ? message : `could not resolve ${request.source.ref}: ${message}`,
|
|
650
|
+
});
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
614
654
|
const decision = (0, policy_1.decideRun)({
|
|
615
655
|
grant,
|
|
616
656
|
accounts: planned.accounts,
|
|
617
657
|
catalog: config.accounts,
|
|
618
|
-
source:
|
|
619
|
-
? { type: 'ref', repo: request.source.repo, ref: request.source.ref }
|
|
620
|
-
: sandbox
|
|
621
|
-
? { type: 'sandbox', repo: sandbox.repo, branch: sandbox.ref }
|
|
622
|
-
: { type: 'upload' },
|
|
658
|
+
source: submitted,
|
|
623
659
|
approvable: (account) => config.entitlements.some((entitlement) => entitlement.accounts[account] === 'use'),
|
|
624
660
|
readable: (repo) => repos.readable(repo),
|
|
661
|
+
onBranches,
|
|
625
662
|
});
|
|
626
663
|
if (decision.type === 'refuse') {
|
|
627
664
|
log(`refuse run iss=${principal.iss} sub=${principal.sub}: ${decision.error}`);
|
|
@@ -633,7 +670,7 @@ async function startRun(config, registry, definitions, sources, sandboxes, plann
|
|
|
633
670
|
let source;
|
|
634
671
|
if (request.source.type === 'ref') {
|
|
635
672
|
try {
|
|
636
|
-
const commit = await repos.resolve(request.source.repo, request.source.ref);
|
|
673
|
+
const commit = checkedCommit ?? (await repos.resolve(request.source.repo, request.source.ref));
|
|
637
674
|
if (request.source.commit && commit !== request.source.commit) {
|
|
638
675
|
const error = retryOf
|
|
639
676
|
? `${request.source.ref} moved to ${commit} since ${retryOf.id} ran ${request.source.commit}, start a new run from ${request.source.ref}`
|
package/package.json
CHANGED