hammerkit 1.27.0-next.3 → 1.27.0-next.30

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.
Files changed (119) hide show
  1. package/build.schema.json +188 -0
  2. package/dist/cache/backends/local-cache-backend.js +6 -0
  3. package/dist/cache/backends/registry-cache-backend.js +2 -2
  4. package/dist/cache/explain.js +17 -0
  5. package/dist/cli.js +2 -0
  6. package/dist/docker/item-network.js +13 -5
  7. package/dist/executer/deadline.js +17 -3
  8. package/dist/executer/deferred-restore.js +36 -0
  9. package/dist/executer/docker-service.js +30 -8
  10. package/dist/executer/docker-task.js +21 -1
  11. package/dist/executer/event-cache.js +20 -2
  12. package/dist/executer/execute-work-service.js +2 -0
  13. package/dist/executer/execute-work-task.js +10 -3
  14. package/dist/executer/execution-steps.js +25 -4
  15. package/dist/executer/reset-work-tree.js +4 -0
  16. package/dist/executer/scheduler/enqueue-next.js +57 -6
  17. package/dist/executer/scheduler/state-key.js +42 -5
  18. package/dist/executer/watch-loop.js +4 -1
  19. package/dist/harness/connect.js +20 -6
  20. package/dist/harness/mcp-command.js +22 -6
  21. package/dist/harness/mcp.js +63 -114
  22. package/dist/harness/opencode.js +158 -50
  23. package/dist/harness/pi.js +29 -0
  24. package/dist/harness/sandbox-client.js +168 -17
  25. package/dist/harness/service-tools.js +141 -0
  26. package/dist/harness/task-tools.js +221 -0
  27. package/dist/kubernetes/apply.js +27 -16
  28. package/dist/kubernetes/await-running-state.js +59 -13
  29. package/dist/kubernetes/claim-layout.js +71 -0
  30. package/dist/kubernetes/cluster-state.js +245 -0
  31. package/dist/kubernetes/ensure-http-route.js +1 -1
  32. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +35 -12
  33. package/dist/kubernetes/ensure-kubernetes-persistent-volume-claim-exists.js +1 -1
  34. package/dist/kubernetes/ensure-kubernetes-service-exists.js +6 -9
  35. package/dist/kubernetes/ensure-persistent-data.js +30 -20
  36. package/dist/kubernetes/follow-pod-logs.js +182 -0
  37. package/dist/kubernetes/get-service-ip.js +2 -2
  38. package/dist/kubernetes/job-placement.js +36 -12
  39. package/dist/kubernetes/job-secrets.js +33 -19
  40. package/dist/kubernetes/kept-claims.js +22 -10
  41. package/dist/kubernetes/network-policy.js +85 -27
  42. package/dist/kubernetes/remove-persistent-data.js +3 -1
  43. package/dist/kubernetes/resources.js +25 -3
  44. package/dist/kubernetes/restore-kubernetes-data.js +9 -3
  45. package/dist/kubernetes/sandbox-runner.js +628 -0
  46. package/dist/kubernetes/secrets.js +7 -11
  47. package/dist/kubernetes/volumes.js +6 -3
  48. package/dist/main.js +2 -0
  49. package/dist/network/egress-proxy.js +45 -6
  50. package/dist/optimizer/calculate-checksum.js +3 -2
  51. package/dist/optimizer/check-key.js +94 -0
  52. package/dist/optimizer/check-keys.js +64 -0
  53. package/dist/optimizer/get-work-cache-stats.js +17 -3
  54. package/dist/optimizer/work-task-cache-description.js +51 -1
  55. package/dist/planner/utils/append-work-dependencies.js +10 -5
  56. package/dist/planner/utils/append-work-service.js +19 -1
  57. package/dist/planner/utils/append-work-task.js +18 -3
  58. package/dist/planner/utils/get-work-runtime.js +6 -1
  59. package/dist/planner/utils/normalize-path.js +19 -1
  60. package/dist/planner/utils/parse-work-mount.js +7 -1
  61. package/dist/planner/utils/parse-work-port.js +4 -1
  62. package/dist/planner/utils/task-arguments.js +3 -0
  63. package/dist/planner/validate.js +22 -0
  64. package/dist/planner/work-network.js +3 -1
  65. package/dist/planner/work-runtime-aggregate.js +32 -0
  66. package/dist/planner/work-runtime-docker.js +2 -1
  67. package/dist/planner/work-runtime-kubernetes.js +124 -48
  68. package/dist/planner/work-runtime-local.js +2 -1
  69. package/dist/planner/work-task.js +6 -1
  70. package/dist/program.js +31 -10
  71. package/dist/remote/device-login.js +7 -2
  72. package/dist/remote/host-config.js +69 -3
  73. package/dist/remote/remote-commands.js +30 -7
  74. package/dist/remote/remote-work-commands.js +426 -45
  75. package/dist/remote/server-client.js +15 -2
  76. package/dist/remote/token-source.js +64 -26
  77. package/dist/run-program.js +9 -0
  78. package/dist/schema/build-file-base-task-schema.js +8 -1
  79. package/dist/schema/build-file-container-service-schema.js +12 -0
  80. package/dist/schema/build-file-container-task-schema.js +16 -1
  81. package/dist/schema/build-file-include-schema.js +18 -4
  82. package/dist/schema/build-file-kubernetes-service-schema.js +2 -0
  83. package/dist/schema/build-file-task-schema.js +5 -1
  84. package/dist/schema/capabilities-schema.js +56 -0
  85. package/dist/schema/reference-parser.js +120 -11
  86. package/dist/schema/schema-parser.js +92 -41
  87. package/dist/schema/work-scope-parser.js +33 -10
  88. package/dist/secrets/command-secret-provider.js +3 -1
  89. package/dist/secrets/github-app-token-command.js +3 -1
  90. package/dist/secrets/secret-catalog.js +43 -19
  91. package/dist/server/accounts-api.js +3 -1
  92. package/dist/server/approvals-api.js +1 -1
  93. package/dist/server/checks-api.js +83 -0
  94. package/dist/server/checks.js +116 -0
  95. package/dist/server/definitions-api.js +66 -3
  96. package/dist/server/endpoints.js +739 -0
  97. package/dist/server/executor.js +57 -5
  98. package/dist/server/kubernetes-backend.js +367 -65
  99. package/dist/server/kubernetes-sandbox-storage.js +48 -3
  100. package/dist/server/oauth.js +12 -1
  101. package/dist/server/plan.js +188 -103
  102. package/dist/server/policy.js +32 -2
  103. package/dist/server/repo-access.js +5 -0
  104. package/dist/server/run-plan.js +50 -19
  105. package/dist/server/run-request.js +58 -3
  106. package/dist/server/run-worker.js +245 -12
  107. package/dist/server/runs.js +410 -27
  108. package/dist/server/sandbox-pod.js +28 -0
  109. package/dist/server/sandbox-storage.js +23 -0
  110. package/dist/server/sandboxes-api.js +10 -4
  111. package/dist/server/sandboxes.js +8 -0
  112. package/dist/server/server-command.js +39 -20
  113. package/dist/server/server-config.js +255 -14
  114. package/dist/server/server.js +269 -15
  115. package/dist/server/service-runs.js +82 -0
  116. package/dist/utils/abort-on-signals.js +46 -6
  117. package/dist/utils/tar.js +36 -0
  118. package/dist/utils/terminal-prompt.js +53 -0
  119. package/package.json +1 -1
@@ -13,60 +13,79 @@ async function createParseContext(fileName, environment, options) {
13
13
  files: {},
14
14
  refreshed: options?.refresh ? new Map() : undefined,
15
15
  directory: options?.directory,
16
+ checkGitSource: options?.checkGitSource,
16
17
  };
17
18
  const scope = await appendBuildFile(workDirectory(ctx, (0, path_1.dirname)(fileName)), ctx, environment, fileName, []);
18
19
  return { ctx, scope };
19
20
  }
20
21
  exports.createParseContext = createParseContext;
21
- async function appendBuildFile(cwd, ctx, environment, fileName, namePrefix, remote, inputs) {
22
+ async function appendBuildFile(cwd, ctx, environment, fileName, namePrefix, remote, inputs,
23
+ // on a server: the file is included with `publish: false`, or below such an include
24
+ leftOut = false) {
22
25
  const relativeName = namePrefix.join(':');
23
- // the same file included with other inputs is another set of tasks
24
- const key = inputs ? `${cwd}:${fileName}:${JSON.stringify(inputs)}` : `${cwd}:${fileName}`;
26
+ // the same file included with other inputs is another set of tasks, and on a server a
27
+ // file left out is another one than the same file published (its git sources differ)
28
+ const key = `${cwd}:${fileName}${inputs ? `:${JSON.stringify(inputs)}` : ''}${leftOut ? ':left-out' : ''}`;
25
29
  if (ctx.files[key]) {
26
30
  return ctx.files[key];
27
31
  }
28
32
  const input = await (0, read_build_file_1.read)(fileName, environment);
29
33
  const result = await build_file_schema_1.buildFileSchema.safeParseAsync(input);
30
- if (result.success) {
31
- const scope = {
32
- fileName,
33
- cwd,
34
- schema: inputs ? applyInputs(result.data, inputs, namePrefix, fileName) : result.data,
35
- namePrefix: relativeName,
36
- remote,
37
- inputs,
38
- references: {},
39
- };
40
- ctx.files[key] = scope;
41
- if (scope.schema.references) {
42
- for (const referenceName of Object.keys(scope.schema.references)) {
43
- const target = await locateInclude(ctx, scope, scope.schema.references[referenceName],
44
- // a remote file's paths resolve within its repository
45
- remote ? (0, path_1.dirname)(scope.fileName) : readDirectory(ctx, scope.cwd), environment);
46
- const referenceScope = await appendBuildFile(workDirectory(ctx, (0, path_1.dirname)(target.fileName)), ctx, environment, target.fileName, [...namePrefix, referenceName], target.remote, target.inputs);
47
- if (scope.references[referenceName]) {
48
- throw new Error(referenceName + ' already exists');
49
- }
50
- scope.references[referenceName] = { type: 'reference', scope: referenceScope };
34
+ if (!result.success) {
35
+ throw new parse_error_1.ParseError(result.error, fileName);
36
+ }
37
+ const scope = {
38
+ fileName,
39
+ cwd,
40
+ schema: inputs ? applyInputs(result.data, inputs, namePrefix, fileName) : result.data,
41
+ namePrefix: relativeName,
42
+ remote,
43
+ inputs,
44
+ references: {},
45
+ };
46
+ ctx.files[key] = scope;
47
+ for (const block of ['references', 'includes']) {
48
+ for (const [name, include] of Object.entries(scope.schema[block] ?? {})) {
49
+ if (scope.references[name]) {
50
+ throw new Error(name + ' already exists');
51
51
  }
52
- }
53
- if (scope.schema.includes) {
54
- for (const includeName of Object.keys(scope.schema.includes)) {
55
- const target = await locateInclude(ctx, scope, scope.schema.includes[includeName], (0, path_1.dirname)(scope.fileName), environment);
56
- const includeScope = await appendBuildFile(cwd, ctx, environment, target.fileName, [...namePrefix, includeName], target.remote, target.inputs);
57
- if (scope.references[includeName]) {
58
- throw new Error(includeName + ' already exists');
59
- }
60
- scope.references[includeName] = { type: 'include', scope: includeScope };
52
+ const publish = (0, build_file_include_schema_1.isPublished)(include);
53
+ const type = block === 'references' ? 'reference' : 'include';
54
+ // a server does not fetch what it leaves out: it doesn't run, and isn't checked
55
+ const leaveOut = !!environment.hosted && (leftOut || !publish);
56
+ if ((0, build_file_include_schema_1.isGitSource)(include) && leaveOut) {
57
+ scope.references[name] = { type, publish, scope: notFetched(cwd, [...namePrefix, name], include) };
58
+ continue;
61
59
  }
60
+ if ((0, build_file_include_schema_1.isGitSource)(include)) {
61
+ ctx.checkGitSource?.(include, scope, block, name);
62
+ }
63
+ const target = await locateInclude(ctx, scope, include, block === 'references'
64
+ ? // a remote file's paths resolve within its repository
65
+ remote
66
+ ? (0, path_1.dirname)(scope.fileName)
67
+ : readDirectory(ctx, scope.cwd)
68
+ : (0, path_1.dirname)(scope.fileName), environment);
69
+ const targetScope = await appendBuildFile(
70
+ // a reference works in its own directory, an include in the one of the including file
71
+ block === 'references' ? workDirectory(ctx, (0, path_1.dirname)(target.fileName)) : cwd, ctx, environment, target.fileName, [...namePrefix, name], target.remote, target.inputs, leaveOut);
72
+ scope.references[name] = { type, scope: targetScope, publish };
62
73
  }
63
- return scope;
64
- }
65
- else {
66
- throw new parse_error_1.ParseError(result.error, fileName);
67
74
  }
75
+ return scope;
68
76
  }
69
77
  exports.appendBuildFile = appendBuildFile;
78
+ // The scope of a git source a server does not fetch, see ParseScope.notFetched
79
+ function notFetched(cwd, namePrefix, include) {
80
+ return {
81
+ fileName: `${include.git}${include.path ? ` ${include.path}` : ''}`,
82
+ cwd,
83
+ schema: {},
84
+ namePrefix: namePrefix.join(':'),
85
+ references: {},
86
+ notFetched: true,
87
+ };
88
+ }
70
89
  // The directory a build file works in, for the directory it was read from: the same
71
90
  // one, unless the files were read from elsewhere (see BuildFileDirectory).
72
91
  function workDirectory(ctx, directory) {
@@ -89,13 +108,45 @@ async function locateInclude(ctx, scope, include, base, environment) {
89
108
  if (!(0, resolve_git_source_1.isWithin)(remote.root, path)) {
90
109
  throw new Error(`${include.path} in ${include.git} points outside the repository`);
91
110
  }
92
- return { fileName: await (0, default_build_file_1.getBuildFilename)(path, environment), remote, inputs: include.with };
111
+ return { fileName: await (0, default_build_file_1.getBuildFilename)(path, environment), remote, inputs: passEnvs(include.with, scope) };
93
112
  }
94
- const path = (0, path_1.join)(base, include);
113
+ const localPath = typeof include === 'string' ? include : include.path;
114
+ const path = (0, path_1.join)(base, localPath);
95
115
  if (scope.remote && !(0, resolve_git_source_1.isWithin)(scope.remote.root, path)) {
96
- throw new Error(`${include} in ${scope.fileName} points outside ${scope.remote.git}`);
116
+ throw new Error(`${localPath} in ${scope.fileName} points outside ${scope.remote.git}`);
117
+ }
118
+ const inputs = typeof include === 'string' ? undefined : passEnvs(include.with, scope);
119
+ return { fileName: await (0, default_build_file_1.getBuildFilename)(path, environment), remote: scope.remote, inputs };
120
+ }
121
+ // `${NAME}` (or `${NAME:-default}`) in a `with` value reads as it does in the including
122
+ // file: the environment or a `.env` file first, then that file's own `envs` value. So one
123
+ // file holds a value such as NODE_VERSION for all it includes, and CI can still set it. A
124
+ // name the file does not declare is left to the included file, which reads it the same way.
125
+ function passEnvs(inputs, scope) {
126
+ if (!inputs) {
127
+ return undefined;
128
+ }
129
+ const envs = scope.schema.envs ?? {};
130
+ const result = {};
131
+ for (const [key, value] of Object.entries(inputs)) {
132
+ result[key] =
133
+ typeof value === 'string'
134
+ ? value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g, (match, name, fallback) => name in envs ? passEnv(name, `${envs[name]}`, fallback) : match)
135
+ : value;
136
+ }
137
+ return result;
138
+ }
139
+ // The including file's value as an interpolation the included file resolves in the same
140
+ // order. A value built from other variables is passed on as it is written.
141
+ function passEnv(name, value, fallback) {
142
+ const reference = /^\$([A-Za-z_][A-Za-z0-9_]*)$/.exec(value);
143
+ if (reference) {
144
+ return fallback === undefined ? `\${${reference[1]}}` : `\${${reference[1]}:-${fallback}}`;
145
+ }
146
+ if (value.includes('$') || value.includes('}')) {
147
+ return value;
97
148
  }
98
- return { fileName: await (0, default_build_file_1.getBuildFilename)(path, environment), remote: scope.remote };
149
+ return `\${${name}:-${value}}`;
99
150
  }
100
151
  // `includes pull` fetches each source once, however often the build files name it.
101
152
  async function resolveRemote(ctx, source, environment) {
@@ -47,19 +47,27 @@ function getWorkContext(context, scope, environment) {
47
47
  // its init task's need on it, which narrowing follows
48
48
  const tasks = Object.values(context.tasks).map((task) => (0, append_work_task_1.appendWorkTask)(workTree, task.cwd, task, environment, context));
49
49
  const services = Object.values(context.services).map((service) => (0, append_work_service_1.appendWorkService)(workTree, service, environment, context));
50
+ const apply = { servicesAlone: !(0, work_scope_1.isContextTaskFilter)(scope) && !!scope.names && !!scope.stop };
50
51
  const selectedTasks = [];
51
52
  const selectedServices = [];
52
53
  for (const item of tasks) {
53
54
  if (matchesTaskWorkScope(item, scope)) {
54
55
  requested.push(item.name);
55
56
  selectedTasks.push(item.data);
56
- applyTask(filteredWorkTree, item);
57
+ applyTask(filteredWorkTree, item, apply);
57
58
  }
58
59
  }
59
60
  for (const item of services) {
60
61
  if (matchesServiceWorkScope(item.data, scope)) {
61
62
  selectedServices.push(item.data);
62
- applyService(filteredWorkTree, item);
63
+ applyService(filteredWorkTree, item, apply);
64
+ }
65
+ }
66
+ if (!(0, work_scope_1.isContextTaskFilter)(scope) && scope.names) {
67
+ const known = new Set([...tasks.map((task) => task.name), ...services.map((service) => service.data.name)]);
68
+ const unknown = scope.names.filter((name) => !known.has(name));
69
+ if (unknown.length > 0) {
70
+ throw new Error(`no task or service named ${unknown.join(', ')}, see hammerkit ls`);
63
71
  }
64
72
  }
65
73
  if (scope.args) {
@@ -86,43 +94,58 @@ function keepRequirersInScope(workTree) {
86
94
  }
87
95
  }
88
96
  }
89
- function applyTask(workTree, item) {
97
+ function applyTask(workTree, item, options) {
90
98
  if (workTree.tasks[item.name]) {
91
99
  return;
92
100
  }
93
101
  workTree.tasks[item.name] = item;
94
- applyNeedsAndDeps(workTree, item);
102
+ applyNeedsAndDeps(workTree, item, options);
95
103
  }
96
- function applyService(workTree, need) {
104
+ function applyService(workTree, need, options) {
97
105
  const item = (0, append_work_service_1.getPlannedService)(need);
98
106
  if (workTree.services[item.name]) {
99
107
  return;
100
108
  }
101
109
  workTree.services[item.name] = item;
102
- applyNeedsAndDeps(workTree, item);
110
+ if (options.servicesAlone) {
111
+ return;
112
+ }
113
+ applyNeedsAndDeps(workTree, item, options);
103
114
  // a service in the run brings its init task, which runs once it is healthy
104
115
  if (item.data.type === 'container-service' && item.data.init) {
105
- applyTask(workTree, item.data.init.task);
116
+ applyTask(workTree, item.data.init.task, options);
106
117
  }
107
118
  }
108
- function applyNeedsAndDeps(workTree, item) {
119
+ function applyNeedsAndDeps(workTree, item, options) {
109
120
  for (const dep of item.deps) {
110
- applyTask(workTree, dep);
121
+ applyTask(workTree, dep, options);
111
122
  }
112
123
  for (const need of item.needs) {
113
- applyService(workTree, need.service);
124
+ applyService(workTree, need.service, options);
114
125
  }
115
126
  }
116
127
  function matchesTaskWorkScope(task, scope) {
117
128
  if ((0, work_scope_1.isContextTaskFilter)(scope)) {
118
129
  return task.name === scope.taskName;
119
130
  }
131
+ if (scope.serviceNames) {
132
+ return false;
133
+ }
134
+ if (scope.names) {
135
+ return scope.names.includes(task.name);
136
+ }
120
137
  return (0, label_values_1.appliesToLabels)(task.data.labels, scope);
121
138
  }
122
139
  function matchesServiceWorkScope(service, scope) {
123
140
  if ((0, work_scope_1.isContextTaskFilter)(scope)) {
124
141
  return false;
125
142
  }
143
+ if (scope.serviceNames) {
144
+ return scope.serviceNames.includes(service.name);
145
+ }
146
+ if (scope.names) {
147
+ return scope.names.includes(service.name);
148
+ }
126
149
  return (0, label_values_1.appliesToLabels)(service.labels, scope);
127
150
  }
128
151
  //# sourceMappingURL=work-scope-parser.js.map
@@ -150,9 +150,11 @@ function candidates(command, path) {
150
150
  .map((dir) => (0, path_1.resolve)(dir, command));
151
151
  return bases.flatMap((base) => extensions.map((extension) => base + extension));
152
152
  }
153
+ // bigint: a Windows file id has more digits than a number keeps, and two files whose ids
154
+ // round to the same number would be the same file
153
155
  function statOf(path) {
154
156
  try {
155
- const stat = (0, fs_1.statSync)(path);
157
+ const stat = (0, fs_1.statSync)(path, { bigint: true });
156
158
  return stat.isFile() ? stat : null;
157
159
  }
158
160
  catch {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.registerGithubAppTokenCommand = void 0;
4
+ const commander_1 = require("commander");
4
5
  const log_1 = require("../log");
5
6
  const github_app_token_1 = require("./github-app-token");
6
7
  // `hammerkit github-app-token <owner/repo>`: a secret provider command for the token a
@@ -18,7 +19,8 @@ function registerGithubAppTokenCommand(program, environment) {
18
19
  }
19
20
  catch (e) {
20
21
  environment.stderr.write(`github-app-token: ${(0, log_1.getErrorMessage)(e)}\n`);
21
- throw e;
22
+ // said already: the CLI does not print it again
23
+ throw new commander_1.CommanderError(1, 'hammerkit.githubAppToken', (0, log_1.getErrorMessage)(e));
22
24
  }
23
25
  });
24
26
  }
@@ -11,10 +11,12 @@ function emptySecretCatalog() {
11
11
  exports.emptySecretCatalog = emptySecretCatalog;
12
12
  const FULL_COMMIT = /^[0-9a-f]{40}$/i;
13
13
  // Providers and accounts are global names, like caches, collected from every
14
- // loaded build file. A name declared twice is an error rather than a silent
15
- // override: which definition runs with a service account's credential must not
16
- // depend on include order.
17
- function collectSecretCatalog(files, environment, context) {
14
+ // loaded build file. A name declared twice is an error rather than a silent override:
15
+ // which definition runs with a service account's credential must not depend on include
16
+ // order. The host config then replaces what the build files declare, name by name (and
17
+ // its default account replaces theirs): the machine that runs a task says how it reads
18
+ // secrets, and a build file that still declares them loads as it is.
19
+ function collectSecretCatalog(files, environment, context, host) {
18
20
  const catalog = emptySecretCatalog();
19
21
  for (const file of files) {
20
22
  const providers = Object.entries(file.schema.secretProviders ?? {});
@@ -28,25 +30,13 @@ function collectSecretCatalog(files, environment, context) {
28
30
  if (catalog.providers[name]) {
29
31
  throw new Error(`secret provider ${name} is declared in ${catalog.providers[name].declaredIn} and in ${file.fileName}`);
30
32
  }
31
- catalog.providers[name] = {
32
- name,
33
- type: 'command',
34
- command: provider.command.map((part) => applyEnvs(part, envs)),
35
- env: provider.env ?? {},
36
- timeoutMs: (0, units_1.parseDuration)(provider.timeout ?? DEFAULT_TIMEOUT),
37
- declaredIn: file.fileName,
38
- };
33
+ catalog.providers[name] = toProvider(name, provider, envs, file.fileName);
39
34
  }
40
35
  for (const [name, account] of accounts) {
41
36
  if (catalog.accounts[name]) {
42
37
  throw new Error(`secret account ${name} is declared in ${catalog.accounts[name].declaredIn} and in ${file.fileName}`);
43
38
  }
44
- catalog.accounts[name] = {
45
- name,
46
- default: account.default ?? false,
47
- providers: account.providers,
48
- declaredIn: file.fileName,
49
- };
39
+ catalog.accounts[name] = toAccount(name, account, file.fileName);
50
40
  }
51
41
  }
52
42
  const defaults = Object.values(catalog.accounts).filter((account) => account.default);
@@ -55,14 +45,48 @@ function collectSecretCatalog(files, environment, context) {
55
45
  }
56
46
  for (const account of Object.values(catalog.accounts)) {
57
47
  for (const provider of Object.keys(account.providers)) {
58
- if (!catalog.providers[provider]) {
48
+ if (!catalog.providers[provider] && !host?.secretProviders?.[provider]) {
59
49
  throw new Error(`secret account ${account.name} (${account.declaredIn}) names provider ${provider}, which is not declared`);
60
50
  }
61
51
  }
62
52
  }
53
+ if (host) {
54
+ // the host config has no envs of its own: `${NAME}` is read from the process
55
+ for (const [name, provider] of Object.entries(host.secretProviders ?? {})) {
56
+ catalog.providers[name] = toProvider(name, provider, {}, host.fileName);
57
+ }
58
+ const accounts = Object.entries(host.secretAccounts ?? {});
59
+ const hostDefaults = accounts.filter(([, account]) => account.default);
60
+ if (hostDefaults.length > 1) {
61
+ throw new Error(`more than one default secret account: ${hostDefaults.map(([name]) => name).join(', ')} (${host.fileName})`);
62
+ }
63
+ if (hostDefaults.length > 0) {
64
+ for (const account of Object.values(catalog.accounts)) {
65
+ account.default = false;
66
+ }
67
+ }
68
+ // a provider an account of the host names is only checked when a secret uses it:
69
+ // a project that doesn't declare it, and reads no secret, loads anyway
70
+ for (const [name, account] of accounts) {
71
+ catalog.accounts[name] = toAccount(name, account, host.fileName);
72
+ }
73
+ }
63
74
  return catalog;
64
75
  }
65
76
  exports.collectSecretCatalog = collectSecretCatalog;
77
+ function toProvider(name, provider, envs, declaredIn) {
78
+ return {
79
+ name,
80
+ type: 'command',
81
+ command: provider.command.map((part) => applyEnvs(part, envs)),
82
+ env: provider.env ?? {},
83
+ timeoutMs: (0, units_1.parseDuration)(provider.timeout ?? DEFAULT_TIMEOUT),
84
+ declaredIn,
85
+ };
86
+ }
87
+ function toAccount(name, account, declaredIn) {
88
+ return { name, default: account.default ?? false, providers: account.providers, declaredIn };
89
+ }
66
90
  // A tag or branch can be moved, and a moved file runs with the credentials of a
67
91
  // service account: providers and accounts from a git include are pinned.
68
92
  function requirePinnedSource(file) {
@@ -6,7 +6,7 @@ const policy_1 = require("./policy");
6
6
  /**
7
7
  * Who the caller is and what they may do:
8
8
  *
9
- * GET /v1/me identity, accounts with their level, repositories, the builds level and the sandbox grants
9
+ * GET /v1/me identity, accounts with their level, repositories, the builds level the sandbox and endpoint grants
10
10
  * GET /v1/accounts the accounts the caller has a level on, with what the operator says about them
11
11
  */
12
12
  async function handleAccounts(config, log, principal, req, res, url) {
@@ -31,7 +31,9 @@ async function handleAccounts(config, log, principal, req, res, url) {
31
31
  accounts,
32
32
  repos: grant.repos,
33
33
  builds: grant.builds,
34
+ checks: grant.checks || grant.builds !== null ? 'read' : null,
34
35
  sandboxes: grant.sandboxes,
36
+ endpoints: grant.endpoints,
35
37
  });
36
38
  return true;
37
39
  }
@@ -13,7 +13,7 @@ function toApprovalView(run) {
13
13
  return {
14
14
  id: run.id,
15
15
  state: run.state,
16
- requestedBy: run.owner,
16
+ requestedBy: { iss: run.owner.iss, sub: run.owner.sub },
17
17
  createdAt: run.createdAt,
18
18
  definition: run.definition,
19
19
  tasks: run.tasks,
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleChecks = exports.MAX_LOOKUP_KEYS = void 0;
4
+ const zod_1 = require("zod");
5
+ const http_1 = require("./http");
6
+ const policy_1 = require("./policy");
7
+ const checks_1 = require("./checks");
8
+ const repo_1 = require("./repo");
9
+ // the tasks one lookup may ask about
10
+ exports.MAX_LOOKUP_KEYS = 100;
11
+ const lookupSchema = zod_1.z
12
+ .object({
13
+ repo: zod_1.z.string().transform((value, ctx) => {
14
+ const repo = (0, repo_1.normalizeRepo)(value);
15
+ if (!repo) {
16
+ ctx.addIssue({ code: zod_1.z.ZodIssueCode.custom, message: 'expected a repository like github.com/corp/payments' });
17
+ return zod_1.z.NEVER;
18
+ }
19
+ return repo;
20
+ }),
21
+ keys: zod_1.z
22
+ .array(zod_1.z
23
+ .object({
24
+ task: zod_1.z.string().min(1).max(200),
25
+ key: zod_1.z.string().regex(checks_1.CHECK_KEY_PATTERN, 'expected a check key, 64 hex digits'),
26
+ })
27
+ .strict())
28
+ .min(1)
29
+ .max(exports.MAX_LOOKUP_KEYS),
30
+ })
31
+ .strict();
32
+ /**
33
+ * What runs passed, for CI that verifies instead of running (`hammerkit remote verify`):
34
+ *
35
+ * POST /v1/checks/lookup { repo, keys: [{ task, key }] } the newest passed run per key
36
+ *
37
+ * The caller computed the keys from its own checkout (check-key.ts); a key nothing passed
38
+ * for, or whose version of the build file was yanked since, is answered with none.
39
+ */
40
+ async function handleChecks(config, checks, definitions, log, principal, req, res, url) {
41
+ if (url.pathname !== '/v1/checks/lookup') {
42
+ return false;
43
+ }
44
+ if (req.method !== 'POST') {
45
+ (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'POST' });
46
+ return true;
47
+ }
48
+ const parsed = lookupSchema.safeParse(await (0, http_1.readJson)(req));
49
+ if (!parsed.success) {
50
+ const issue = parsed.error.issues[0];
51
+ (0, http_1.send)(res, 400, { error: `${issue.path.join('.') || 'request'}: ${issue.message}` });
52
+ return true;
53
+ }
54
+ const { repo, keys } = parsed.data;
55
+ const grant = (0, policy_1.getGrant)(config.entitlements, principal);
56
+ if (!(0, policy_1.mayReadChecks)(grant, repo)) {
57
+ log(`refuse checks iss=${principal.iss} sub=${principal.sub} repo=${repo}`);
58
+ (0, http_1.send)(res, 403, { error: `you may not read the checks of ${repo}` });
59
+ return true;
60
+ }
61
+ const answers = await Promise.all(keys.map(async ({ task, key }) => {
62
+ const record = await checks.find(repo, key);
63
+ const yanked = record ? definitions.find(record.definition)?.yanked : undefined;
64
+ if (!record || yanked) {
65
+ return {
66
+ task,
67
+ key,
68
+ passed: null,
69
+ ...(yanked ? { reason: `it passed with ${record?.definition}, which was yanked since` } : {}),
70
+ };
71
+ }
72
+ const { repo: _repo, key: _key, accounts: _accounts, ...view } = record;
73
+ void _repo;
74
+ void _key;
75
+ void _accounts;
76
+ return { task, key, passed: view };
77
+ }));
78
+ log(`checks iss=${principal.iss} sub=${principal.sub} repo=${repo} passed=${answers.filter((a) => a.passed).length}/${answers.length}`);
79
+ (0, http_1.send)(res, 200, { checks: answers });
80
+ return true;
81
+ }
82
+ exports.handleChecks = handleChecks;
83
+ //# sourceMappingURL=checks-api.js.map
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckStore = exports.getCheckRecords = exports.isCheckRun = exports.CHECK_KEY_PATTERN = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ exports.CHECK_KEY_PATTERN = /^[0-9a-f]{64}$/;
8
+ // The runs whose results are checks: those of a commit the server checked out, and those
9
+ // on a copy of the files of a sandbox, which hold nothing a commit of them would not. A run
10
+ // with arguments ran something other than the task, and is no check of it.
11
+ function isCheckRun(run) {
12
+ // a run of services keeps them up, it passes nothing
13
+ if ((run.options.args ?? []).length > 0 || run.up) {
14
+ return false;
15
+ }
16
+ return run.source.type === 'ref' || (run.source.type === 'sandbox' && run.source.check === true);
17
+ }
18
+ exports.isCheckRun = isCheckRun;
19
+ // What a run that ended passed, with the keys its executor reported before the tasks ran.
20
+ function getCheckRecords(run, keys) {
21
+ if (!isCheckRun(run) || run.source.type === 'upload') {
22
+ return [];
23
+ }
24
+ const source = run.source.type === 'ref'
25
+ ? { type: 'ref', ref: run.source.ref, commit: run.source.commit }
26
+ : { type: 'sandbox', sandbox: run.source.id, commit: run.source.commit, tree: run.source.tree };
27
+ const records = [];
28
+ for (const [task, result] of Object.entries(run.results ?? {})) {
29
+ const key = keys[task];
30
+ if (!key || !exports.CHECK_KEY_PATTERN.test(key) || (result.status !== 'succeeded' && result.status !== 'cached')) {
31
+ continue;
32
+ }
33
+ records.push({
34
+ repo: run.source.repo,
35
+ key,
36
+ task,
37
+ status: result.status,
38
+ run: run.id,
39
+ by: { iss: run.owner.iss, sub: run.owner.sub },
40
+ source,
41
+ definition: run.definition,
42
+ accounts: [...run.accounts],
43
+ finishedAt: result.finishedAt ?? run.finishedAt ?? new Date().toISOString(),
44
+ ...(result.durationMs !== undefined ? { durationMs: result.durationMs } : {}),
45
+ });
46
+ }
47
+ return records;
48
+ }
49
+ exports.getCheckRecords = getCheckRecords;
50
+ /**
51
+ * One file per repository and key, the newest run's: `<directory>/<repo hash>/<key>.json`,
52
+ * written to a temporary file and renamed. A record older than `keep` is not found and is
53
+ * removed by `sweep`.
54
+ */
55
+ class CheckStore {
56
+ constructor(directory, options) {
57
+ this.directory = directory;
58
+ this.options = options;
59
+ this.now = options.now ?? Date.now;
60
+ }
61
+ async save(records) {
62
+ for (const record of records) {
63
+ const folder = (0, path_1.join)(this.directory, repoFolder(record.repo));
64
+ await (0, promises_1.mkdir)(folder, { recursive: true, mode: 0o700 });
65
+ const file = (0, path_1.join)(folder, `${record.key}.json`);
66
+ const temporary = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
67
+ await (0, promises_1.writeFile)(temporary, JSON.stringify(record), { mode: 0o600 });
68
+ await (0, promises_1.rename)(temporary, file);
69
+ }
70
+ }
71
+ async find(repo, key) {
72
+ if (!exports.CHECK_KEY_PATTERN.test(key)) {
73
+ return undefined;
74
+ }
75
+ let record;
76
+ try {
77
+ record = JSON.parse(await (0, promises_1.readFile)((0, path_1.join)(this.directory, repoFolder(repo), `${key}.json`), 'utf8'));
78
+ }
79
+ catch {
80
+ return undefined;
81
+ }
82
+ if (record.repo !== repo || record.key !== key || this.expired(record)) {
83
+ return undefined;
84
+ }
85
+ return record;
86
+ }
87
+ // Removes the records that are older than `keep`.
88
+ async sweep() {
89
+ let folders;
90
+ try {
91
+ folders = await (0, promises_1.readdir)(this.directory);
92
+ }
93
+ catch {
94
+ return;
95
+ }
96
+ for (const folder of folders) {
97
+ const path = (0, path_1.join)(this.directory, folder);
98
+ for (const name of await (0, promises_1.readdir)(path).catch(() => [])) {
99
+ const file = (0, path_1.join)(path, name);
100
+ const stats = await (0, promises_1.stat)(file).catch(() => undefined);
101
+ if (stats && this.now() - stats.mtimeMs > this.options.keep) {
102
+ await (0, promises_1.rm)(file, { force: true });
103
+ }
104
+ }
105
+ }
106
+ }
107
+ expired(record) {
108
+ const at = Date.parse(record.finishedAt);
109
+ return !Number.isFinite(at) || this.now() - at > this.options.keep;
110
+ }
111
+ }
112
+ exports.CheckStore = CheckStore;
113
+ function repoFolder(repo) {
114
+ return (0, crypto_1.createHash)('sha256').update(repo).digest('hex').slice(0, 32);
115
+ }
116
+ //# sourceMappingURL=checks.js.map