hammerkit 2.1.0-next.6 → 2.1.0-next.8

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.
@@ -45,7 +45,8 @@ const pushSchema = zod_1.z
45
45
  * The sandboxes of a caller: checkouts on the server that runs work on.
46
46
  *
47
47
  * POST /v1/sandboxes { repo, ref } fetch a branch into a new sandbox
48
- * GET /v1/sandboxes the caller's sandboxes, their limits and usage
48
+ * GET /v1/sandboxes the caller's sandboxes, their limits and usage;
49
+ * `?scope=all` everybody's, with `observe`
49
50
  * GET /v1/sandboxes/<id> one sandbox
50
51
  * DELETE /v1/sandboxes/<id> cancel its runs and remove it
51
52
  * POST /v1/sandboxes/<id>/fetch { ref } move a sandbox without changes to a branch's commit
@@ -53,7 +54,10 @@ const pushSchema = zod_1.z
53
54
  * ... /v1/sandboxes/<id>/tree, files/<path>, search, edits, changes: its files, see
54
55
  * `handleSandboxFiles`
55
56
  *
56
- * A sandbox is its owner's: anybody else gets `404`.
57
+ * A sandbox is its owner's: anybody else gets `404`. Who observes (`observe` on an
58
+ * entitlement) may read every sandbox, as they read every run: list them, and get one, its
59
+ * tree, files, search and changes. Everything that changes a sandbox or runs on it stays
60
+ * its owner's, and is `404` to them too.
57
61
  */
58
62
  async function handleSandboxes(config, sandboxes, repos, log, principal, req, res, url) {
59
63
  const match = /^\/v1\/sandboxes(?:\/([^/]+)(?:\/(fetch|push|tree|search|edits|changes)|\/(files)\/(.+))?)?$/.exec(url.pathname);
@@ -64,12 +68,23 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
64
68
  const action = named ?? files;
65
69
  const method = req.method;
66
70
  const who = `iss=${principal.iss} sub=${principal.sub}`;
71
+ const show = (sandbox) => view(config, sandboxes, principal, sandbox);
67
72
  try {
68
73
  if (!id) {
69
74
  if (method === 'GET') {
75
+ const scope = url.searchParams.get('scope') ?? 'mine';
76
+ if (scope !== 'mine' && scope !== 'all') {
77
+ (0, http_1.send)(res, 400, { error: 'scope: mine or all' });
78
+ return true;
79
+ }
80
+ if (scope === 'all' && !(0, policy_1.getGrant)(config.entitlements, principal).observe) {
81
+ (0, http_1.send)(res, 403, { error: 'scope all needs an entitlement with observe' });
82
+ return true;
83
+ }
70
84
  const limits = sandboxes.limits;
85
+ // the limits and the usage are the caller's, whichever sandboxes they list
71
86
  (0, http_1.send)(res, 200, {
72
- sandboxes: sandboxes.list(principal).map((sandbox) => view(sandboxes, sandbox)),
87
+ sandboxes: (scope === 'all' ? sandboxes.all() : sandboxes.list(principal)).map(show),
73
88
  limits: { count: limits.count, bytes: limits.bytes },
74
89
  used: sandboxes.usage(principal),
75
90
  });
@@ -84,21 +99,29 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
84
99
  }
85
100
  const sandbox = await sandboxes.create(principal, body.repo, body.ref);
86
101
  log(`sandbox create ${sandbox.id} ${who} repo=${sandbox.repo} commit=${sandbox.commit} bytes=${sandbox.bytes}`);
87
- (0, http_1.send)(res, 201, view(sandboxes, sandbox));
102
+ (0, http_1.send)(res, 201, show(sandbox));
88
103
  }
89
104
  else {
90
105
  (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET, POST' });
91
106
  }
92
107
  return true;
93
108
  }
94
- const sandbox = runs_1.SANDBOX_ID_PATTERN.test(id) ? sandboxes.get(principal, id) : undefined;
109
+ const known = runs_1.SANDBOX_ID_PATTERN.test(id);
110
+ const own = known ? sandboxes.get(principal, id) : undefined;
111
+ const observes = (0, policy_1.getGrant)(config.entitlements, principal).observe;
112
+ const reading = isRead(action, method);
113
+ // who observes reads the sandbox of anybody; a change is for its owner, and else not found
114
+ const sandbox = own ?? (known && observes && reading ? sandboxes.peek(id) : undefined);
95
115
  if (!sandbox) {
96
116
  (0, http_1.send)(res, 404, { error: 'not found' });
97
117
  return true;
98
118
  }
99
119
  if (action && action !== 'fetch' && action !== 'push') {
100
- // the caller may have lost the repository, or the sandbox's branch, since they created it
101
- const refused = (0, policy_1.decideSandbox)((0, policy_1.getGrant)(config.entitlements, principal), sandbox.repo, sandbox.ref, (repo) => repos.readable(repo));
120
+ // the owner may have lost the repository, or the sandbox's branch, since they created
121
+ // it; who observes reads it all the same
122
+ const refused = own && !(observes && reading)
123
+ ? (0, policy_1.decideSandbox)((0, policy_1.getGrant)(config.entitlements, principal), sandbox.repo, sandbox.ref, (repo) => repos.readable(repo))
124
+ : undefined;
102
125
  if (refused) {
103
126
  log(`refuse sandbox ${action} ${id} ${who}: ${refused.error}`);
104
127
  (0, http_1.send)(res, refused.status, { error: refused.error });
@@ -133,15 +156,15 @@ async function handleSandboxes(config, sandboxes, repos, log, principal, req, re
133
156
  if ('branch' in body) {
134
157
  const pushed = await sandboxes.push(principal, id, body.branch, { ...author(principal), message: body.message }, (0, policy_1.sandboxPushBranches)(grant, sandbox.repo));
135
158
  log(`sandbox push ${id} ${who} branch=${body.branch} commit=${pushed.commit}`);
136
- (0, http_1.send)(res, 200, { branch: body.branch, commit: pushed.commit, sandbox: view(sandboxes, pushed) });
159
+ (0, http_1.send)(res, 200, { branch: body.branch, commit: pushed.commit, sandbox: show(pushed) });
137
160
  return true;
138
161
  }
139
162
  const fetched = await sandboxes.fetch(principal, id, body.ref);
140
163
  log(`sandbox fetch ${id} ${who} ref=${body.ref} commit=${fetched.commit}`);
141
- (0, http_1.send)(res, 200, view(sandboxes, fetched));
164
+ (0, http_1.send)(res, 200, show(fetched));
142
165
  }
143
166
  else if (method === 'GET') {
144
- (0, http_1.send)(res, 200, view(sandboxes, sandbox));
167
+ (0, http_1.send)(res, 200, show(sandbox));
145
168
  }
146
169
  else if (method === 'DELETE') {
147
170
  // a caller who lost the entitlement can still delete what they keep
@@ -206,11 +229,39 @@ function author(principal) {
206
229
  return { name, email: `${local}@${host}` };
207
230
  }
208
231
  exports.author = author;
209
- // What the owner sees: everything but the owner, and the run that holds it.
210
- function view(sandboxes, sandbox) {
232
+ /**
233
+ * Whether a request only reads a sandbox, which who observes may: the sandbox, its tree, a
234
+ * file, a search and its changes. Edits, a write of a file, a fetch, a push and a delete
235
+ * change it.
236
+ */
237
+ function isRead(action, method) {
238
+ switch (action) {
239
+ case undefined:
240
+ return method === 'GET';
241
+ case 'tree':
242
+ case 'changes':
243
+ return method === 'GET';
244
+ case 'files':
245
+ return method === 'GET' || method === 'HEAD';
246
+ case 'search':
247
+ // ripgrep's query is a body
248
+ return method === 'POST';
249
+ default:
250
+ return false;
251
+ }
252
+ }
253
+ // A sandbox as the caller sees it, and the run that holds it: their own without its owner,
254
+ // one of somebody else (who observes) with whose it is, as a run of somebody else names it.
255
+ function view(config, sandboxes, principal, sandbox) {
211
256
  const { owner, ...visible } = sandbox;
212
- void owner;
213
257
  const run = sandboxes.holder(sandbox.id);
214
- return { ...visible, busy: run !== undefined, run: run ?? null };
258
+ const mine = owner.iss === principal.iss && owner.sub === principal.sub;
259
+ const name = owner.name ?? (0, policy_1.subjectName)(config.entitlements, owner);
260
+ return {
261
+ ...visible,
262
+ ...(mine ? {} : { owner: { iss: owner.iss, sub: owner.sub, ...(name ? { name } : {}) } }),
263
+ busy: run !== undefined,
264
+ run: run ?? null,
265
+ };
215
266
  }
216
267
  //# sourceMappingURL=sandboxes-api.js.map
@@ -6,6 +6,7 @@ const promises_1 = require("fs/promises");
6
6
  const path_1 = require("path");
7
7
  const units_1 = require("../utils/units");
8
8
  const git_checkout_1 = require("./git-checkout");
9
+ const oauth_1 = require("./oauth");
9
10
  const runs_1 = require("./runs");
10
11
  const repo_1 = require("./repo");
11
12
  const sandbox_storage_1 = require("./sandbox-storage");
@@ -22,7 +23,8 @@ class SandboxError extends Error {
22
23
  exports.SandboxError = SandboxError;
23
24
  /**
24
25
  * The sandboxes of a server: who owns which, the limits, and one change at a time per
25
- * sandbox. Everything is scoped to the owner: somebody else's sandbox is "not found".
26
+ * sandbox. Everything is scoped to the owner: somebody else's sandbox is "not found". Only
27
+ * `all` and `peek` show the sandboxes of everybody, for who may read them (`observe`).
26
28
  */
27
29
  class SandboxRegistry {
28
30
  constructor(options) {
@@ -111,6 +113,18 @@ class SandboxRegistry {
111
113
  const sandbox = this.sandboxes.get(id);
112
114
  return sandbox && owns(owner, sandbox) && !this.deleting.has(id) ? copy(sandbox) : undefined;
113
115
  }
116
+ // The sandboxes of everybody, oldest first: what who observes sees.
117
+ all() {
118
+ return [...this.sandboxes.values()]
119
+ .filter((sandbox) => !this.deleting.has(sandbox.id))
120
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt))
121
+ .map(copy);
122
+ }
123
+ // A sandbox of anybody, for who observes: to read, never to change.
124
+ peek(id) {
125
+ const sandbox = this.sandboxes.get(id);
126
+ return sandbox && !this.deleting.has(id) ? copy(sandbox) : undefined;
127
+ }
114
128
  holder(id) {
115
129
  return this.runs.holder(id);
116
130
  }
@@ -188,9 +202,10 @@ class SandboxRegistry {
188
202
  }
189
203
  this.creating.set(id, { owner: { iss: owner.iss, sub: owner.sub }, bytes });
190
204
  const at = this.timestamp();
205
+ const name = (0, oauth_1.displayName)(owner);
191
206
  const sandbox = {
192
207
  id,
193
- owner: { iss: owner.iss, sub: owner.sub },
208
+ owner: { iss: owner.iss, sub: owner.sub, ...(name ? { name } : {}) },
194
209
  repo,
195
210
  ref,
196
211
  commit,
@@ -548,8 +563,9 @@ function owns(owner, sandbox) {
548
563
  const of = 'owner' in sandbox ? sandbox.owner : sandbox;
549
564
  return of.iss === owner.iss && of.sub === owner.sub;
550
565
  }
566
+ // what the storage keeps a sandbox by: its owner's identity, not the name they had
551
567
  function keyOf(sandbox) {
552
- return { id: sandbox.id, owner: sandbox.owner };
568
+ return { id: sandbox.id, owner: { iss: sandbox.owner.iss, sub: sandbox.owner.sub } };
553
569
  }
554
570
  function copy(sandbox) {
555
571
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "2.1.0-next.6",
4
+ "version": "2.1.0-next.8",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",