hammerkit 2.0.0-next.12 → 2.0.0-next.13

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.
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isRunSourceEnv = exports.RUN_SOURCE_ENVS = void 0;
4
+ // The variables a server sets for the process of a run, from what the run is from (see
5
+ // src/server/run-source-environment.ts). Nothing else sets them: the operator's `runs.env`
6
+ // can't name them, and a run that isn't from that source has none of them.
7
+ exports.RUN_SOURCE_ENVS = [
8
+ 'HAMMERKIT_RUN_REPO',
9
+ 'HAMMERKIT_RUN_REF',
10
+ 'HAMMERKIT_RUN_COMMIT',
11
+ 'HAMMERKIT_RUN_SANDBOX',
12
+ ];
13
+ // without case: Windows doesn't tell `hammerkit_run_ref` from `HAMMERKIT_RUN_REF`
14
+ function isRunSourceEnv(name) {
15
+ return exports.RUN_SOURCE_ENVS.includes(name.toUpperCase());
16
+ }
17
+ exports.isRunSourceEnv = isRunSourceEnv;
18
+ //# sourceMappingURL=run-source-envs.js.map
@@ -5,6 +5,7 @@ const crypto_1 = require("crypto");
5
5
  const state_key_1 = require("../executer/scheduler/state-key");
6
6
  const get_work_cache_stats_1 = require("./get-work-cache-stats");
7
7
  const work_task_cache_description_1 = require("./work-task-cache-description");
8
+ const run_source_envs_1 = require("../environment/run-source-envs");
8
9
  /**
9
10
  * What a passed check is recorded under, and what `remote verify` looks it up by: the
10
11
  * inputs of the task's cache key, so a cached result counts for the same key it was
@@ -46,6 +47,12 @@ function getNeedKey(item, environment, memo, path) {
46
47
  });
47
48
  }
48
49
  async function getInputKeys(item, environment, memo, path) {
50
+ // what a run is from differs from run to run, and `remote verify` never has it: a result
51
+ // that read it says nothing about the files (a CI job keyed the same task on the default)
52
+ const runSource = ('envs' in item.data ? item.data.envs.processNames ?? [] : []).find(run_source_envs_1.isRunSourceEnv);
53
+ if (runSource) {
54
+ throw new Error(`${item.name} reads ${runSource}, which differs from run to run`);
55
+ }
49
56
  const deps = [];
50
57
  for (const dep of item.deps) {
51
58
  const key = await getCheckKey(dep, environment, memo, path);
@@ -13,6 +13,7 @@ const git_checkout_1 = require("./git-checkout");
13
13
  const run_plan_1 = require("./run-plan");
14
14
  const work_network_1 = require("../planner/work-network");
15
15
  const execute_docker_1 = require("../executer/execute-docker");
16
+ const run_source_environment_1 = require("./run-source-environment");
16
17
  // `.git` as a file system may read it: any case, and a name Windows reads without its
17
18
  // trailing dots and spaces, or by its short name
18
19
  function isGitDirectory(segment) {
@@ -202,7 +203,7 @@ class HostExecutor {
202
203
  // what was approved is what runs: the accounts are read again from the files with
203
204
  // the environment of the run, and the run may use no more than it was admitted for
204
205
  const policy = source.type === 'sandbox' && sandboxes ? sandboxes.policy : planning.policy;
205
- const planned = await (0, run_plan_1.planRun)(bundle, run, planning.environment, policy);
206
+ const planned = await (0, run_plan_1.planRun)(bundle, run, (0, run_source_environment_1.runPlanningEnvironment)(planning.environment, run.source), policy);
206
207
  const extra = planned.accounts.filter((account) => !run.accounts.includes(account));
207
208
  if (extra.length > 0) {
208
209
  throw new Error(`the run would use ${extra.join(', ')}, which it was not admitted for`);
@@ -355,7 +356,7 @@ class HostExecutor {
355
356
  : {}),
356
357
  ...(placed ? { kubernetes: placed.kubernetes } : {}),
357
358
  ...(!placed && this.options.capabilityRuntime ? { capabilityRuntime: this.options.capabilityRuntime } : {}),
358
- }, published, { ...this.processEnvironment(home, cache), ...placed?.env }, context, abortCtrl.signal);
359
+ }, published, { ...(0, run_source_environment_1.withRunSource)(this.processEnvironment(home, cache), run.source), ...placed?.env }, context, abortCtrl.signal);
359
360
  }
360
361
  finally {
361
362
  if (placement && placed) {
@@ -18,12 +18,13 @@ const log_1 = require("../log");
18
18
  const parse_error_1 = require("../schema/parse-error");
19
19
  const task_arguments_1 = require("../planner/utils/task-arguments");
20
20
  const work_network_1 = require("../planner/work-network");
21
+ const run_source_envs_1 = require("../environment/run-source-envs");
21
22
  class DefinitionError extends Error {
22
23
  }
23
24
  exports.DefinitionError = DefinitionError;
24
25
  // Variables of a server that hold no credential (FR-008): a task may read these and
25
- // still take arguments, or reach any network.
26
- exports.PLAIN_SERVER_ENVS = ['PATH', 'HOME', 'CI'];
26
+ // still take arguments, or reach any network. The ones of the run's source are set for each run.
27
+ exports.PLAIN_SERVER_ENVS = ['PATH', 'HOME', 'CI', ...run_source_envs_1.RUN_SOURCE_ENVS];
27
28
  // What an item holds that its command must not hand out: its secrets, and the variables
28
29
  // of the server its `envs` read, but the plain ones.
29
30
  function getCredentialReads(item) {
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runEnvironment = void 0;
4
+ const run_source_envs_1 = require("../environment/run-source-envs");
4
5
  // What a run sees of the server's environment: the variables the operator listed
5
6
  // in `runs.env.allow`, and the fixed ones of `runs.env.set`. The credentials of a
6
- // run are not among them, they exist only as the secrets of an account.
7
+ // run are not among them, they exist only as the secrets of an account. The variables of
8
+ // what a run is from are the run's own (the config refuses them, see run-source-envs.ts).
7
9
  function runEnvironment(config, processEnvs) {
8
10
  const passed = {};
9
11
  for (const name of config.runs.env.allow) {
@@ -14,7 +16,8 @@ function runEnvironment(config, processEnvs) {
14
16
  passed[name] = processEnvs[key];
15
17
  }
16
18
  }
17
- return { ...passed, ...config.runs.env.set };
19
+ const all = { ...passed, ...config.runs.env.set };
20
+ return Object.fromEntries(Object.entries(all).filter(([name]) => !(0, run_source_envs_1.isRunSourceEnv)(name)));
18
21
  }
19
22
  exports.runEnvironment = runEnvironment;
20
23
  //# sourceMappingURL=run-environment.js.map
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runPlanningEnvironment = exports.withRunSource = exports.runSourceEnvironment = exports.RUN_SOURCE_ENVS = void 0;
4
+ const run_source_envs_1 = require("../environment/run-source-envs");
5
+ Object.defineProperty(exports, "RUN_SOURCE_ENVS", { enumerable: true, get: function () { return run_source_envs_1.RUN_SOURCE_ENVS; } });
6
+ // a ref may name a commit, which is no branch or tag (see git-checkout.ts)
7
+ const COMMIT = /^[0-9a-f]{40}([0-9a-f]{24})?$/i;
8
+ // What a run is from, for the `envs` of its tasks and services (`$HAMMERKIT_RUN_REF`): the
9
+ // repository, and the branch or tag and the commit it was pinned to, or the repository and the
10
+ // sandbox. None of them is a credential, so a task that reads them still takes arguments and
11
+ // keeps its network (see PLAIN_SERVER_ENVS). A sandbox has no commit: its files may hold
12
+ // changes. A run from an upload has none of them.
13
+ function runSourceEnvironment(source) {
14
+ if (source.type === 'ref') {
15
+ return {
16
+ HAMMERKIT_RUN_REPO: source.repo,
17
+ ...(COMMIT.test(source.ref) ? {} : { HAMMERKIT_RUN_REF: source.ref }),
18
+ ...(source.commit ? { HAMMERKIT_RUN_COMMIT: source.commit } : {}),
19
+ };
20
+ }
21
+ if (source.type === 'sandbox') {
22
+ return { HAMMERKIT_RUN_REPO: source.repo, HAMMERKIT_RUN_SANDBOX: source.id };
23
+ }
24
+ return {};
25
+ }
26
+ exports.runSourceEnvironment = runSourceEnvironment;
27
+ // The variables of a process, and the run's own in place of any of the same name, in any case.
28
+ function withRunSource(envs, source) {
29
+ const kept = Object.fromEntries(Object.entries(envs).filter(([name]) => !(0, run_source_envs_1.isRunSourceEnv)(name)));
30
+ return { ...kept, ...runSourceEnvironment(source) };
31
+ }
32
+ exports.withRunSource = withRunSource;
33
+ // The environment a run's files are read with, on the server, before its process starts: so
34
+ // the plan an approver reads shows what the tasks will see.
35
+ function runPlanningEnvironment(environment, source) {
36
+ return { ...environment, processEnvs: withRunSource(environment.processEnvs, source) };
37
+ }
38
+ exports.runPlanningEnvironment = runPlanningEnvironment;
39
+ //# sourceMappingURL=run-source-environment.js.map
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.detectPlatform = exports.getSecretCatalog = exports.readServerConfig = exports.getServerAccounts = exports.getSandboxRuntimes = exports.serverConfigSchema = exports.sourceSchema = exports.entitlementSchema = exports.endpointGrantSchema = exports.endpointLevelSchema = exports.sandboxGrantSchema = exports.sandboxBranchTypeSchema = exports.buildsLevelSchema = exports.accountLevelSchema = exports.getMaxVolumeSize = exports.DEFAULT_MAX_VOLUME_SIZE = exports.DEFAULT_DENIED_EGRESS = exports.kubernetesPoolSchema = exports.accountSchema = exports.issuerSchema = exports.serverPlatformSchema = void 0;
4
+ const run_source_envs_1 = require("../environment/run-source-envs");
4
5
  const net_1 = require("net");
5
6
  const zod_1 = require("zod");
6
7
  const yaml_1 = require("yaml");
@@ -512,6 +513,15 @@ exports.serverConfigSchema = zod_1.z
512
513
  set: zod_1.z.record(zod_1.z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/), zod_1.z.string()).default({}),
513
514
  })
514
515
  .strict()
516
+ // what a run is from is the run's: an operator's value would pass for it
517
+ .superRefine((env, ctx) => {
518
+ for (const name of [...env.allow, ...Object.keys(env.set)].filter(run_source_envs_1.isRunSourceEnv)) {
519
+ ctx.addIssue({
520
+ code: zod_1.z.ZodIssueCode.custom,
521
+ message: `${name} is set by the server for each run, from what the run is from`,
522
+ });
523
+ }
524
+ })
515
525
  .default({}),
516
526
  // the most any task or service of a run reaches besides its needs: none, open
517
527
  // or { allow: [hosts] }. A version that asks for more is refused at publish.
@@ -20,6 +20,7 @@ const execute_docker_1 = require("../executer/execute-docker");
20
20
  const timings_1 = require("./timings");
21
21
  const kubernetes_backend_1 = require("./kubernetes-backend");
22
22
  const run_plan_1 = require("./run-plan");
23
+ const run_source_environment_1 = require("./run-source-environment");
23
24
  const work_network_1 = require("../planner/work-network");
24
25
  const run_environment_1 = require("./run-environment");
25
26
  const accounts_api_1 = require("./accounts-api");
@@ -465,7 +466,13 @@ async function submitRun(config, registry, definitions, sources, sandboxes, plan
465
466
  let planned;
466
467
  try {
467
468
  // a run on a sandbox may only use the runtimes of `sandboxes.runtimes`
468
- planned = await (0, run_plan_1.planRun)(bundle, request, planning.environment, sandbox ? sandboxes.policy : planning.policy);
469
+ // with what the run is from, as its tasks read it; the commit of a ref follows below
470
+ const from = request.source.type === 'ref'
471
+ ? { type: 'ref', repo: request.source.repo, ref: request.source.ref }
472
+ : sandbox
473
+ ? { type: 'sandbox', id: sandbox.id, repo: sandbox.repo }
474
+ : { type: 'upload' };
475
+ planned = await (0, run_plan_1.planRun)(bundle, request, (0, run_source_environment_1.runPlanningEnvironment)(planning.environment, from), sandbox ? sandboxes.policy : planning.policy);
469
476
  }
470
477
  catch (e) {
471
478
  (0, http_2.send)(res, 400, { error: `the run does not load: ${e instanceof Error ? e.message : String(e)}` });
@@ -528,6 +535,25 @@ async function submitRun(config, registry, definitions, sources, sandboxes, plan
528
535
  else if (request.source.type === 'upload') {
529
536
  source = request.source;
530
537
  }
538
+ if (source?.type === 'ref') {
539
+ // what the approver reads is what the tasks see, the commit included; who may run it was
540
+ // decided without it and can't change with it
541
+ let pinned;
542
+ try {
543
+ pinned = await (0, run_plan_1.planRun)(bundle, request, (0, run_source_environment_1.runPlanningEnvironment)(planning.environment, source), planning.policy);
544
+ }
545
+ catch (e) {
546
+ (0, http_2.send)(res, 400, { error: `the run does not load: ${e instanceof Error ? e.message : String(e)}` });
547
+ return;
548
+ }
549
+ if (pinned.accounts.join(',') !== planned.accounts.join(',')) {
550
+ const error = `the accounts of the run change with its commit (${pinned.accounts.join(', ')})`;
551
+ log(`refuse run iss=${principal.iss} sub=${principal.sub}: ${error}`);
552
+ (0, http_2.send)(res, 400, { error });
553
+ return;
554
+ }
555
+ planned = pinned;
556
+ }
531
557
  // a service run is named after its branch unless it says, and its endpoints after that
532
558
  const serviceName = request.services.length > 0
533
559
  ? request.name ??
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.12",
4
+ "version": "2.0.0-next.13",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",