hammerkit 2.0.0-next.16 → 2.0.0-next.18

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.
@@ -284,14 +284,19 @@ up) {
284
284
  if (input.kubernetes.server?.serviceRun) {
285
285
  // a run that moved here from another commit: what it made for items it has no more
286
286
  const ids = [...Object.values(workTree.tasks), ...Object.values(workTree.services)].map((item) => item.id());
287
- await (0, remove_stale_items_1.removeStaleItems)((0, kubernetes_instance_1.createKubernetesInstances)(input.kubernetes), input.kubernetes, ids).catch((e) => emit({
287
+ await (0, remove_stale_items_1.removeStaleItems)((0, kubernetes_instance_1.createKubernetesInstances)(input.kubernetes), input.kubernetes, ids, {
288
+ signal: abortCtrl.signal,
289
+ }).catch((e) => emit({
288
290
  type: 'log',
289
291
  task: 'hammerkit',
290
292
  stream: 'stderr',
291
293
  text: `what the run made at its previous commit could not be removed: ${(0, log_1.getErrorMessage)(e)}`,
292
294
  }));
293
295
  }
294
- await new Promise((resolve) => abortCtrl.signal.addEventListener('abort', () => resolve(), { once: true }));
296
+ // stopped while the old pods ended, or up until it is stopped
297
+ if (!abortCtrl.signal.aborted) {
298
+ await new Promise((resolve) => abortCtrl.signal.addEventListener('abort', () => resolve(), { once: true }));
299
+ }
295
300
  return;
296
301
  }
297
302
  if (input.up && ready) {
@@ -299,6 +299,15 @@ const kubernetesBackendSchema = zod_1.z
299
299
  // build file's environment does: hammerkit can't tell from the API, and without it a
300
300
  // task with a limited network fails rather than runs open
301
301
  networkPolicy: zod_1.z.literal('enforced').optional(),
302
+ // what the pods with an open network wait to no longer reach before they start, under
303
+ // `networkPolicy: enforced`: an address and port `deniedEgress` covers that answers without
304
+ // the policy (the metadata endpoint by default), or `false` for no wait
305
+ openNetworkGate: zod_1.z
306
+ .union([
307
+ zod_1.z.literal(false),
308
+ zod_1.z.object({ host: zod_1.z.string().ip({ version: 'v4' }), port: zod_1.z.number().int().min(1).max(65535) }).strict(),
309
+ ])
310
+ .optional(),
302
311
  // `runtimeClass` is a RuntimeClass of a sandbox, such as gvisor; the pods of tasks that
303
312
  // add capabilities run in it
304
313
  capabilities: zod_1.z.object({ allow: capabilitiesAllowSchema, runtimeClass: kubernetesNameSchema }).strict().optional(),
@@ -585,8 +594,9 @@ exports.serverConfigSchema = zod_1.z
585
594
  // the most open network a task or service of a run on a sandbox gets, written as
586
595
  // a task's `network`: only the services it needs by default
587
596
  network: network_schema_1.networkSchema.default('none'),
588
- // the variables of `runs.env.allow` an `env:` secret of a run on a sandbox may
589
- // read: none by default, a sandbox holds code nobody reviewed
597
+ // the variables of runs (`runs.env.allow` and `runs.env.set`) an `env:` secret and the
598
+ // `envs` of a build file of a run on a sandbox may read, besides PATH, HOME and CI:
599
+ // none by default, a sandbox holds code nobody reviewed
590
600
  env: zod_1.z
591
601
  .object({ allow: zod_1.z.array(zod_1.z.string().regex(/^[A-Za-z_][A-Za-z0-9_()]*$/)).default([]) })
592
602
  .strict()
@@ -779,11 +789,11 @@ exports.serverConfigSchema = zod_1.z
779
789
  });
780
790
  }
781
791
  config.sandboxes.env.allow.forEach((name, index) => {
782
- if (!config.runs.env.allow.includes(name)) {
792
+ if (!config.runs.env.allow.includes(name) && !Object.prototype.hasOwnProperty.call(config.runs.env.set, name)) {
783
793
  ctx.addIssue({
784
794
  code: zod_1.z.ZodIssueCode.custom,
785
795
  path: ['sandboxes', 'env', 'allow', index],
786
- message: `${name} is not in runs.env.allow, no run can read it`,
796
+ message: `${name} is not in runs.env.allow or runs.env.set, no run can read it`,
787
797
  });
788
798
  }
789
799
  });
@@ -38,6 +38,7 @@ const sandbox_storage_1 = require("./sandbox-storage");
38
38
  const kubernetes_sandbox_storage_1 = require("./kubernetes-sandbox-storage");
39
39
  const sandbox_docker_1 = require("./sandbox-docker");
40
40
  const sandboxes_api_1 = require("./sandboxes-api");
41
+ const plan_1 = require("./plan");
41
42
  const endpoints_1 = require("./endpoints");
42
43
  const service_runs_1 = require("./service-runs");
43
44
  const units_1 = require("../utils/units");
@@ -152,7 +153,7 @@ async function handleRuns(config, registry, definitions, sources, sandboxes, pla
152
153
  const grant = (0, policy_1.getGrant)(config.entitlements, principal);
153
154
  const run = registry.cancel(principal, id, accountsOf(config, principal), (up) => (0, service_runs_1.mayPublishEndpoints)(grant, up));
154
155
  log(`cancel iss=${principal.iss} sub=${principal.sub} run=${id} found=${Boolean(run)}`);
155
- run ? (0, http_2.send)(res, 200, toRunResponse(run)) : (0, http_2.send)(res, 404, { error: 'not found' });
156
+ run ? (0, http_2.send)(res, 200, answerRun(config, principal, run)) : (0, http_2.send)(res, 404, { error: 'not found' });
156
157
  }
157
158
  else if (action === 'extend') {
158
159
  if (method !== 'POST') {
@@ -180,7 +181,7 @@ async function handleRuns(config, registry, definitions, sources, sandboxes, pla
180
181
  (0, http_2.send)(res, 409, { error: result.reason });
181
182
  }
182
183
  else {
183
- (0, http_2.send)(res, 200, toRunResponse(result.run));
184
+ (0, http_2.send)(res, 200, answerRun(config, principal, result.run));
184
185
  }
185
186
  }
186
187
  else if (method !== 'GET') {
@@ -268,9 +269,18 @@ async function extendRun(config, registry, log, principal, id, req, res) {
268
269
  (0, http_2.send)(res, 409, { error: `the run is ${result.state}, only a service run that is up can be extended` });
269
270
  }
270
271
  else {
271
- (0, http_2.send)(res, 200, toRunResponse(result.run));
272
+ (0, http_2.send)(res, 200, answerRun(config, principal, result.run));
272
273
  }
273
274
  }
275
+ // What a stop, an extend or a wake answers: the run whole for who may read it (its owner, who
276
+ // decides about it, `observe`), else what the list shows of it. Who may only open or publish
277
+ // endpoints of its repository is told how it stands, not its arguments, plan or log.
278
+ function answerRun(config, principal, run) {
279
+ const viewer = (0, overview_1.getViewer)(config, principal);
280
+ return (0, overview_1.mayRead)(viewer, run)
281
+ ? toRunResponse(run, undefined, !(0, overview_1.owns)(viewer, run), viewer.nameOf)
282
+ : (0, overview_1.toListedRun)(viewer, run);
283
+ }
274
284
  // Why the owner of a service run that sleeps may no longer run it, with the entitlements of
275
285
  // the moment: a wake is started by who opens an endpoint, but runs as the owner, as a new
276
286
  // run of theirs would.
@@ -816,9 +826,14 @@ async function startServer(config, environment, options = {}) {
816
826
  },
817
827
  };
818
828
  const storage = config.storage.directory ?? (0, path_1.join)((0, get_cache_directory_1.getHammerkitDirectory)(), 'server');
819
- const definitions = new definitions_1.DefinitionStore((0, path_1.join)(storage, 'definitions'), planning.environment, {
820
- policy: planning.policy,
821
- });
829
+ // a build file nobody ran yet is read with the names of the variables, not their values
830
+ const definitions = new definitions_1.DefinitionStore((0, path_1.join)(storage, 'definitions'), {
831
+ ...planning.environment,
832
+ hosted: {
833
+ ...planning.environment.hosted,
834
+ buildFileEnvs: (0, plan_1.withSymbolicValues)(planning.environment.processEnvs),
835
+ },
836
+ }, { policy: planning.policy });
822
837
  await definitions.load();
823
838
  const repos = new repo_access_1.RepoAccess(config, planning.environment, options.repos);
824
839
  const sandboxRuntimes = (0, server_config_1.getSandboxRuntimes)(config);
@@ -847,6 +862,8 @@ async function startServer(config, environment, options = {}) {
847
862
  ...planning.policy,
848
863
  envs: config.sandboxes.env.allow,
849
864
  envsFrom: 'sandboxes.env.allow',
865
+ // and what its build files read of the run's variables, `$NAME` in its `envs`
866
+ readableEnvs: config.sandboxes.env.allow,
850
867
  runtimes: sandboxRuntimes,
851
868
  capabilities: config.sandboxes.capabilities,
852
869
  capabilitiesFrom: 'sandboxes.capabilities',
@@ -38,7 +38,9 @@ function containerPorts(plan) {
38
38
  * Why the endpoints of a service run can't be published, or null. Each is a port its
39
39
  * service declares, of a service the run starts. Under a `runs.secretNetwork` below `open`
40
40
  * a service behind an endpoint may not read a secret or a credential of the server: a page
41
- * is a way out that no mask of the log covers.
41
+ * is a way out that no mask of the log covers. Nor may what it is made from or talks to: a
42
+ * task it depends on (its init task too) can leave a value in a file the service serves,
43
+ * and a service it needs can hand one over the connection it answers on.
42
44
  */
43
45
  function refuseEndpoints(request, plan, secretNetworkOpen) {
44
46
  for (const endpoint of request.endpoints) {
@@ -50,14 +52,50 @@ function refuseEndpoints(request, plan, secretNetworkOpen) {
50
52
  const ports = containerPorts(item);
51
53
  return `${endpoint.service} has no port ${endpoint.port}${ports.length > 0 ? `, its ports are ${ports.join(', ')}` : ', it declares none'}`;
52
54
  }
53
- const reads = (0, plan_1.getCredentialReads)(item);
54
- if (!secretNetworkOpen && reads.length > 0) {
55
- return `${endpoint.service} reads ${reads.join(', ')}, it can not be behind an endpoint on this server (runs.secretNetwork): a page could show it`;
55
+ if (secretNetworkOpen) {
56
+ continue;
57
+ }
58
+ for (const { name, path } of reachedFrom(endpoint.service, plan)) {
59
+ const reads = (0, plan_1.getCredentialReads)(plan[name]);
60
+ if (reads.length === 0) {
61
+ continue;
62
+ }
63
+ const what = reads.join(', ');
64
+ if (path.length === 0) {
65
+ return `${name} reads ${what}, it can not be behind an endpoint on this server (runs.secretNetwork): a page could show it`;
66
+ }
67
+ // the way from the service to what reads it: `web needs api, which needs vault`
68
+ const way = path.map((step, index) => `${index === 0 ? '' : ', which '}${step.relation} ${step.name}`).join('');
69
+ return (`${endpoint.service} ${way}, which reads ${what}: ` +
70
+ 'it can not be behind an endpoint on this server (runs.secretNetwork), a page could show it');
56
71
  }
57
72
  }
58
73
  return null;
59
74
  }
60
75
  exports.refuseEndpoints = refuseEndpoints;
76
+ function reachedFrom(service, plan) {
77
+ if (!plan[service]) {
78
+ return [];
79
+ }
80
+ const reached = [{ name: service, path: [] }];
81
+ const seen = new Set([service]);
82
+ for (let index = 0; index < reached.length; index++) {
83
+ const { name, path } = reached[index];
84
+ const item = plan[name];
85
+ const next = [
86
+ ...(item.init ? [{ relation: 'is initialized by', name: item.init }] : []),
87
+ ...(item.deps ?? []).map((dep) => ({ relation: 'depends on', name: dep })),
88
+ ...(item.needs ?? []).map((need) => ({ relation: 'needs', name: need })),
89
+ ];
90
+ for (const step of next) {
91
+ if (plan[step.name] && !seen.has(step.name)) {
92
+ seen.add(step.name);
93
+ reached.push({ name: step.name, path: [...path, step] });
94
+ }
95
+ }
96
+ }
97
+ return reached;
98
+ }
61
99
  function toRunEndpoints(endpoints, hosts, url) {
62
100
  return endpoints.map((endpoint, index) => ({
63
101
  service: endpoint.service,
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lineSplitter = exports.MAX_LINE_CHARS = void 0;
4
+ const string_decoder_1 = require("string_decoder");
5
+ // The most of a line that is held back for its end: a longer one is passed on in parts.
6
+ exports.MAX_LINE_CHARS = 64 * 1024;
7
+ /**
8
+ * The lines of output that arrives in chunks, split where getLogs splits a chunk: at a line
9
+ * feed, with a CR before it. A line that two chunks (or two frames of docker) split is passed
10
+ * on whole once it ends, so the mask of the secrets of a run (SecretRegistry.redact, which
11
+ * works per line) covers a value that was split between them. Empty lines are left out, as
12
+ * getLogs does. A line of more than `maxChars` is passed on in parts of that length.
13
+ */
14
+ function lineSplitter(onLine, maxChars = exports.MAX_LINE_CHARS) {
15
+ // a character split between two chunks stays whole
16
+ const decoder = new string_decoder_1.StringDecoder('utf8');
17
+ let pending = '';
18
+ const take = (text) => {
19
+ const lines = (pending + text).split(/\r?\n/);
20
+ pending = lines.pop() ?? '';
21
+ for (const line of lines) {
22
+ if (line) {
23
+ onLine(line);
24
+ }
25
+ }
26
+ while (pending.length > maxChars) {
27
+ onLine(pending.slice(0, maxChars));
28
+ pending = pending.slice(maxChars);
29
+ }
30
+ };
31
+ let ended = false;
32
+ return {
33
+ write(chunk) {
34
+ if (!ended) {
35
+ take(typeof chunk === 'string' ? chunk : decoder.write(chunk));
36
+ }
37
+ },
38
+ end() {
39
+ if (ended) {
40
+ return;
41
+ }
42
+ ended = true;
43
+ take(decoder.end());
44
+ if (pending) {
45
+ onLine(pending);
46
+ pending = '';
47
+ }
48
+ },
49
+ };
50
+ }
51
+ exports.lineSplitter = lineSplitter;
52
+ //# sourceMappingURL=line-splitter.js.map
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.0.0-next.16",
4
+ "version": "2.0.0-next.18",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",