hammerkit 1.27.0-next.30 → 1.27.0-next.31
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/cache/backends/registry/registry-client.js +1 -0
- package/dist/dashboard/assets/index-CjnKm5-3.css +2 -0
- package/dist/dashboard/assets/index-zPXa4klZ.js +13 -0
- package/dist/dashboard/favicon.svg +1 -0
- package/dist/dashboard/index.html +15 -0
- package/dist/kubernetes/kubernetes-instance.js +1 -0
- package/dist/remote/remote-commands.js +3 -0
- package/dist/remote/remote-status.js +112 -0
- package/dist/remote/remote-work-commands.js +126 -7
- package/dist/server/accounts-api.js +1 -0
- package/dist/server/capacity-format.js +36 -0
- package/dist/server/capacity.js +300 -0
- package/dist/server/dashboard.js +196 -0
- package/dist/server/endpoints.js +79 -25
- package/dist/server/kubernetes-backend.js +25 -0
- package/dist/server/overview.js +271 -0
- package/dist/server/policy.js +3 -1
- package/dist/server/proposals-api.js +17 -8
- package/dist/server/run-logs.js +4 -0
- package/dist/server/runs.js +125 -11
- package/dist/server/server-config.js +24 -2
- package/dist/server/server.js +136 -21
- package/dist/server/timings.js +132 -0
- package/dist/server/wait-reason.js +21 -0
- package/package.json +8 -2
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatMemory = exports.formatCpu = exports.HostCapacity = exports.KubernetesCapacity = exports.cachedCapacity = exports.parseQuantity = void 0;
|
|
4
|
+
const os_1 = require("os");
|
|
5
|
+
const log_1 = require("../log");
|
|
6
|
+
const apply_1 = require("../kubernetes/apply");
|
|
7
|
+
const UNITS = {
|
|
8
|
+
n: 1e-9,
|
|
9
|
+
u: 1e-6,
|
|
10
|
+
m: 1e-3,
|
|
11
|
+
'': 1,
|
|
12
|
+
k: 1e3,
|
|
13
|
+
K: 1e3,
|
|
14
|
+
M: 1e6,
|
|
15
|
+
G: 1e9,
|
|
16
|
+
T: 1e12,
|
|
17
|
+
P: 1e15,
|
|
18
|
+
E: 1e18,
|
|
19
|
+
Ki: 1024,
|
|
20
|
+
Mi: 1024 ** 2,
|
|
21
|
+
Gi: 1024 ** 3,
|
|
22
|
+
Ti: 1024 ** 4,
|
|
23
|
+
Pi: 1024 ** 5,
|
|
24
|
+
Ei: 1024 ** 6,
|
|
25
|
+
};
|
|
26
|
+
// A quantity of Kubernetes (`250m`, `1.5`, `512Mi`, `12345678n`, `1e3`), as a number.
|
|
27
|
+
function parseQuantity(value) {
|
|
28
|
+
if (value === undefined) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (typeof value === 'number') {
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
const match = /^([+-]?[0-9.]+(?:[eE][+-]?\d+)?)([a-zA-Z]*)$/.exec(value.trim());
|
|
35
|
+
if (!match || !(match[2] in UNITS)) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const number = Number(match[1]);
|
|
39
|
+
return Number.isFinite(number) ? number * UNITS[match[2]] : undefined;
|
|
40
|
+
}
|
|
41
|
+
exports.parseQuantity = parseQuantity;
|
|
42
|
+
function add(into, cpu, memory) {
|
|
43
|
+
if (cpu !== undefined) {
|
|
44
|
+
into.cpu = (into.cpu ?? 0) + cpu;
|
|
45
|
+
}
|
|
46
|
+
if (memory !== undefined) {
|
|
47
|
+
into.memory = (into.memory ?? 0) + memory;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function addAll(into, from) {
|
|
51
|
+
if (from) {
|
|
52
|
+
add(into, from.cpu, from.memory);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Reads at most once in `ms`, and never twice at the same time: the dashboard reloads. A read
|
|
56
|
+
// that takes longer than `wait` is not waited for: a slow cluster API does not hold up a page,
|
|
57
|
+
// the next one shows what it read.
|
|
58
|
+
function cachedCapacity(reader, ms = 15000, now = Date.now, wait = 3000) {
|
|
59
|
+
let last;
|
|
60
|
+
const failed = (problem) => ({
|
|
61
|
+
backend: 'host',
|
|
62
|
+
at: new Date(now()).toISOString(),
|
|
63
|
+
runs: { requested: {} },
|
|
64
|
+
groups: [],
|
|
65
|
+
problems: [problem],
|
|
66
|
+
});
|
|
67
|
+
return {
|
|
68
|
+
read(runs) {
|
|
69
|
+
if (!last || now() - last.at >= ms) {
|
|
70
|
+
const value = reader.read(runs).catch((e) => failed((0, log_1.getErrorMessage)(e)));
|
|
71
|
+
last = { at: now(), value };
|
|
72
|
+
}
|
|
73
|
+
let timer;
|
|
74
|
+
const late = new Promise((resolve) => {
|
|
75
|
+
timer = setTimeout(() => resolve(failed('the capacity could not be read in time')), wait);
|
|
76
|
+
timer.unref?.();
|
|
77
|
+
});
|
|
78
|
+
return Promise.race([last.value, late]).finally(() => clearTimeout(timer));
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
exports.cachedCapacity = cachedCapacity;
|
|
83
|
+
function why(what, e, rule) {
|
|
84
|
+
const code = (0, apply_1.statusCodeOf)(e);
|
|
85
|
+
if (code === 403 && rule) {
|
|
86
|
+
return `${what}: the server may not read it, it needs ${rule}`;
|
|
87
|
+
}
|
|
88
|
+
if (code === 404 && what.includes('metrics')) {
|
|
89
|
+
return `${what}: the cluster serves no metrics API (metrics-server)`;
|
|
90
|
+
}
|
|
91
|
+
return `${what}: ${(0, log_1.getErrorMessage)(e)}`;
|
|
92
|
+
}
|
|
93
|
+
const ENDED = ['Succeeded', 'Failed'];
|
|
94
|
+
// On Kubernetes: per namespace of the server, its pods' requests, its quota and its usage.
|
|
95
|
+
class KubernetesCapacity {
|
|
96
|
+
constructor(instance, options) {
|
|
97
|
+
this.instance = instance;
|
|
98
|
+
this.options = options;
|
|
99
|
+
}
|
|
100
|
+
async read(runs) {
|
|
101
|
+
const problems = [];
|
|
102
|
+
const groups = [];
|
|
103
|
+
let namespaces = [];
|
|
104
|
+
try {
|
|
105
|
+
namespaces = await this.options.namespaces();
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
problems.push(why('the namespaces of the server', e));
|
|
109
|
+
}
|
|
110
|
+
const said = new Set();
|
|
111
|
+
const once = (text) => {
|
|
112
|
+
if (!said.has(text)) {
|
|
113
|
+
said.add(text);
|
|
114
|
+
problems.push(text);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
await Promise.all(namespaces.map(async ({ name: namespace, kind }) => {
|
|
118
|
+
const group = {
|
|
119
|
+
name: namespace,
|
|
120
|
+
kind,
|
|
121
|
+
...this.options.ownerOf(namespace, runs),
|
|
122
|
+
pods: 0,
|
|
123
|
+
// a namespace whose pods could be listed and that has none asks for nothing
|
|
124
|
+
requested: { cpu: 0, memory: 0 },
|
|
125
|
+
};
|
|
126
|
+
try {
|
|
127
|
+
const pods = await this.instance.coreApi.listNamespacedPod({ namespace });
|
|
128
|
+
for (const pod of pods.items) {
|
|
129
|
+
if (ENDED.includes(pod.status?.phase ?? '')) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
group.pods++;
|
|
133
|
+
for (const container of pod.spec?.containers ?? []) {
|
|
134
|
+
const requests = container.resources?.requests ?? {};
|
|
135
|
+
add(group.requested, parseQuantity(requests.cpu) ?? 0, parseQuantity(requests.memory) ?? 0);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
once(why('the pods of the namespaces', e, 'list on pods in them (runRole)'));
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const quota = await this.instance.coreApi.readNamespacedResourceQuota({
|
|
144
|
+
name: this.options.quotaName,
|
|
145
|
+
namespace,
|
|
146
|
+
});
|
|
147
|
+
const hard = quota.spec?.hard ?? {};
|
|
148
|
+
const cpu = parseQuantity(hard['requests.cpu'] ?? hard.cpu);
|
|
149
|
+
const memory = parseQuantity(hard['requests.memory'] ?? hard.memory);
|
|
150
|
+
if (cpu !== undefined || memory !== undefined) {
|
|
151
|
+
group.quota = { ...(cpu !== undefined ? { cpu } : {}), ...(memory !== undefined ? { memory } : {}) };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
// a namespace without a quota is no problem
|
|
156
|
+
if ((0, apply_1.statusCodeOf)(e) !== 404) {
|
|
157
|
+
once(why('the quotas of the namespaces', e, 'get on resourcequotas in them (runRole)'));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (this.instance.metrics) {
|
|
161
|
+
try {
|
|
162
|
+
const metrics = await this.instance.metrics.getPodMetrics(namespace);
|
|
163
|
+
const used = { cpu: 0, memory: 0 };
|
|
164
|
+
for (const pod of metrics.items) {
|
|
165
|
+
for (const container of pod.containers) {
|
|
166
|
+
add(used, parseQuantity(container.usage.cpu), parseQuantity(container.usage.memory));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
group.used = used;
|
|
170
|
+
}
|
|
171
|
+
catch (e) {
|
|
172
|
+
once(why('what the pods use (metrics)', e, 'list on pods.metrics.k8s.io in them (runRole, see the backend docs)'));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
groups.push(group);
|
|
176
|
+
}));
|
|
177
|
+
groups.sort((a, b) => a.name.localeCompare(b.name));
|
|
178
|
+
const total = { requested: {}, pods: 0 };
|
|
179
|
+
const used = {};
|
|
180
|
+
for (const group of groups) {
|
|
181
|
+
addAll(total.requested, group.requested);
|
|
182
|
+
total.pods = (total.pods ?? 0) + group.pods;
|
|
183
|
+
addAll(used, group.used);
|
|
184
|
+
}
|
|
185
|
+
if (groups.some((group) => group.used)) {
|
|
186
|
+
total.used = used;
|
|
187
|
+
}
|
|
188
|
+
let machines;
|
|
189
|
+
try {
|
|
190
|
+
const nodes = await this.instance.coreApi.listNode();
|
|
191
|
+
const allocatable = {};
|
|
192
|
+
for (const node of nodes.items) {
|
|
193
|
+
const of = node.status?.allocatable ?? {};
|
|
194
|
+
add(allocatable, parseQuantity(of.cpu), parseQuantity(of.memory));
|
|
195
|
+
}
|
|
196
|
+
machines = { count: nodes.items.length, allocatable };
|
|
197
|
+
if (this.instance.metrics) {
|
|
198
|
+
try {
|
|
199
|
+
const metrics = await this.instance.metrics.getNodeMetrics();
|
|
200
|
+
const nodesUsed = {};
|
|
201
|
+
for (const node of metrics.items) {
|
|
202
|
+
add(nodesUsed, parseQuantity(node.usage.cpu), parseQuantity(node.usage.memory));
|
|
203
|
+
}
|
|
204
|
+
machines.used = nodesUsed;
|
|
205
|
+
}
|
|
206
|
+
catch (e) {
|
|
207
|
+
once(why('what the nodes use (metrics)', e, 'list on nodes.metrics.k8s.io (see the backend docs)'));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
// listing nodes is optional, and many clusters do not let a workload do it
|
|
213
|
+
if ((0, apply_1.statusCodeOf)(e) === 403) {
|
|
214
|
+
once('the nodes: not shown, the server may not list nodes (optional, see the backend docs)');
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
once(why('the nodes', e));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
backend: 'kubernetes',
|
|
222
|
+
at: new Date().toISOString(),
|
|
223
|
+
...(machines ? { machines } : {}),
|
|
224
|
+
runs: total,
|
|
225
|
+
groups,
|
|
226
|
+
problems,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
exports.KubernetesCapacity = KubernetesCapacity;
|
|
231
|
+
// On a host: the machine of the docker daemon, and the containers runs started on it.
|
|
232
|
+
class HostCapacity {
|
|
233
|
+
constructor(docker,
|
|
234
|
+
// whether the docker daemon runs on this machine, so its load is the server's
|
|
235
|
+
local = true) {
|
|
236
|
+
this.docker = docker;
|
|
237
|
+
this.local = local;
|
|
238
|
+
}
|
|
239
|
+
async read() {
|
|
240
|
+
const problems = [];
|
|
241
|
+
let machines;
|
|
242
|
+
try {
|
|
243
|
+
const info = (await this.docker.info());
|
|
244
|
+
machines = {
|
|
245
|
+
count: 1,
|
|
246
|
+
allocatable: { cpu: info.NCPU ?? (0, os_1.cpus)().length, memory: info.MemTotal ?? (0, os_1.totalmem)() },
|
|
247
|
+
...(this.local ? { used: { memory: (0, os_1.totalmem)() - (0, os_1.freemem)() }, load: (0, os_1.loadavg)() } : {}),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
catch (e) {
|
|
251
|
+
problems.push(why('the docker daemon', e));
|
|
252
|
+
machines = this.local
|
|
253
|
+
? {
|
|
254
|
+
count: 1,
|
|
255
|
+
allocatable: { cpu: (0, os_1.cpus)().length, memory: (0, os_1.totalmem)() },
|
|
256
|
+
used: { memory: (0, os_1.totalmem)() - (0, os_1.freemem)() },
|
|
257
|
+
load: (0, os_1.loadavg)(),
|
|
258
|
+
}
|
|
259
|
+
: undefined;
|
|
260
|
+
}
|
|
261
|
+
const runs = { requested: {}, used: {}, containers: 0 };
|
|
262
|
+
try {
|
|
263
|
+
const containers = await this.docker.listContainers({ filters: { label: ['app=hammerkit'] } });
|
|
264
|
+
runs.containers = containers.length;
|
|
265
|
+
await Promise.all(containers.map(async (container) => {
|
|
266
|
+
try {
|
|
267
|
+
const stats = (await this.docker.getContainer(container.Id).stats({ stream: false }));
|
|
268
|
+
const cpu = stats.cpu_stats?.cpu_usage?.total_usage ?? 0;
|
|
269
|
+
const before = stats.precpu_stats?.cpu_usage?.total_usage ?? 0;
|
|
270
|
+
const system = (stats.cpu_stats?.system_cpu_usage ?? 0) - (stats.precpu_stats?.system_cpu_usage ?? 0);
|
|
271
|
+
const online = stats.cpu_stats?.online_cpus ?? 1;
|
|
272
|
+
const cores = system > 0 ? ((cpu - before) / system) * online : 0;
|
|
273
|
+
const memory = (stats.memory_stats?.usage ?? 0) -
|
|
274
|
+
(stats.memory_stats?.stats?.inactive_file ?? stats.memory_stats?.stats?.cache ?? 0);
|
|
275
|
+
add(runs.used, Math.max(0, cores), Math.max(0, memory));
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
// a container that just ended
|
|
279
|
+
}
|
|
280
|
+
}));
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
problems.push(why('the containers of runs', e));
|
|
284
|
+
delete runs.used;
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
backend: 'host',
|
|
288
|
+
at: new Date().toISOString(),
|
|
289
|
+
...(machines ? { machines } : {}),
|
|
290
|
+
runs,
|
|
291
|
+
groups: [],
|
|
292
|
+
problems,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
exports.HostCapacity = HostCapacity;
|
|
297
|
+
var capacity_format_1 = require("./capacity-format");
|
|
298
|
+
Object.defineProperty(exports, "formatCpu", { enumerable: true, get: function () { return capacity_format_1.formatCpu; } });
|
|
299
|
+
Object.defineProperty(exports, "formatMemory", { enumerable: true, get: function () { return capacity_format_1.formatMemory; } });
|
|
300
|
+
//# sourceMappingURL=capacity.js.map
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleDashboard = exports.HEADERS = exports.DECIDE_WITHIN_MS = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const http_1 = require("./http");
|
|
8
|
+
const policy_1 = require("./policy");
|
|
9
|
+
const overview_1 = require("./overview");
|
|
10
|
+
// Approving and publishing take a sign-in of the last minutes: the groups of a session are
|
|
11
|
+
// those of its sign-in, and somebody taken out of a group must not decide for hours.
|
|
12
|
+
exports.DECIDE_WITHIN_MS = 15 * 60000;
|
|
13
|
+
const RUN = 'run_[A-Za-z0-9_-]{16}';
|
|
14
|
+
// a hash of a build file version, checked by the API
|
|
15
|
+
const HASH = '[A-Za-z0-9:_-]{1,200}';
|
|
16
|
+
const path = (pattern) => new RegExp(`^/v1/${pattern}$`);
|
|
17
|
+
// what the pages read
|
|
18
|
+
const READS = [
|
|
19
|
+
'me',
|
|
20
|
+
'overview',
|
|
21
|
+
'capacity',
|
|
22
|
+
'tasks',
|
|
23
|
+
'runs',
|
|
24
|
+
`runs/${RUN}`,
|
|
25
|
+
`runs/${RUN}/logs`,
|
|
26
|
+
'approvals',
|
|
27
|
+
`approvals/${RUN}`,
|
|
28
|
+
'proposals',
|
|
29
|
+
`proposals/${HASH}`,
|
|
30
|
+
`proposals/${HASH}/files`,
|
|
31
|
+
].map(path);
|
|
32
|
+
// what the buttons do
|
|
33
|
+
const ACTIONS = [`runs/${RUN}/(cancel|extend)`].map(path);
|
|
34
|
+
// what needs a recent sign-in
|
|
35
|
+
const DECISIONS = [`approvals/${RUN}/(approve|reject)`, `proposals/${HASH}/(approve|reject)`].map(path);
|
|
36
|
+
exports.HEADERS = {
|
|
37
|
+
'cache-control': 'no-store',
|
|
38
|
+
'x-robots-tag': 'noindex',
|
|
39
|
+
// the app is a script and a style sheet of its own host, and talks to its host only
|
|
40
|
+
'content-security-policy': "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'self'; " +
|
|
41
|
+
"font-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'",
|
|
42
|
+
'x-frame-options': 'DENY',
|
|
43
|
+
'referrer-policy': 'no-referrer',
|
|
44
|
+
'x-content-type-options': 'nosniff',
|
|
45
|
+
};
|
|
46
|
+
const TYPES = {
|
|
47
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
48
|
+
'.css': 'text/css; charset=utf-8',
|
|
49
|
+
'.svg': 'image/svg+xml',
|
|
50
|
+
'.woff2': 'font/woff2',
|
|
51
|
+
'.png': 'image/png',
|
|
52
|
+
};
|
|
53
|
+
async function handleDashboard(context, principal, req, res, url) {
|
|
54
|
+
const now = context.now ?? Date.now;
|
|
55
|
+
const grant = (0, policy_1.getGrant)(context.config.entitlements, principal);
|
|
56
|
+
const viewer = { principal, grant, activity: context.config.runs.activity === 'everyone' };
|
|
57
|
+
const pathname = url.pathname;
|
|
58
|
+
for (const [name, value] of Object.entries(exports.HEADERS)) {
|
|
59
|
+
res.setHeader(name, value);
|
|
60
|
+
}
|
|
61
|
+
if (pathname === '/api' || pathname.startsWith('/api/')) {
|
|
62
|
+
if (grant.matched.length === 0) {
|
|
63
|
+
return (0, http_1.send)(res, 403, { error: 'you are not entitled to anything on this server' });
|
|
64
|
+
}
|
|
65
|
+
return api(context, principal, viewer, now(), req, res, url);
|
|
66
|
+
}
|
|
67
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
68
|
+
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET, HEAD' });
|
|
69
|
+
}
|
|
70
|
+
return asset(context, req, res, pathname);
|
|
71
|
+
}
|
|
72
|
+
exports.handleDashboard = handleDashboard;
|
|
73
|
+
async function api(context, principal, viewer, now, req, res, url) {
|
|
74
|
+
const { grant } = viewer;
|
|
75
|
+
const csrf = context.sign(`dashboard\0csrf\0${principal.iss}\0${principal.sub}\0${context.session ?? 'token'}`);
|
|
76
|
+
const method = req.method ?? 'GET';
|
|
77
|
+
if (url.pathname === '/api/session') {
|
|
78
|
+
if (method !== 'GET') {
|
|
79
|
+
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET' });
|
|
80
|
+
}
|
|
81
|
+
const session = {
|
|
82
|
+
iss: principal.iss,
|
|
83
|
+
sub: principal.sub,
|
|
84
|
+
observe: grant.observe,
|
|
85
|
+
seesOthers: grant.observe || (0, overview_1.seesActivity)(viewer),
|
|
86
|
+
publishes: (0, policy_1.mayPublish)(grant),
|
|
87
|
+
csrf,
|
|
88
|
+
...(context.signedInAt !== undefined
|
|
89
|
+
? { decideUntil: new Date(context.signedInAt + exports.DECIDE_WITHIN_MS).toISOString() }
|
|
90
|
+
: {}),
|
|
91
|
+
decideWithinMinutes: exports.DECIDE_WITHIN_MS / 60000,
|
|
92
|
+
};
|
|
93
|
+
return (0, http_1.send)(res, 200, session);
|
|
94
|
+
}
|
|
95
|
+
const download = new RegExp(`^/api/runs/(${RUN})/log\\.txt$`).exec(url.pathname);
|
|
96
|
+
if (download) {
|
|
97
|
+
if (method !== 'GET') {
|
|
98
|
+
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'GET' });
|
|
99
|
+
}
|
|
100
|
+
return logText(context, viewer, res, download[1], url.searchParams.get('task') ?? undefined);
|
|
101
|
+
}
|
|
102
|
+
const target = url.pathname.slice('/api'.length);
|
|
103
|
+
const read = READS.some((pattern) => pattern.test(target));
|
|
104
|
+
const action = ACTIONS.some((pattern) => pattern.test(target));
|
|
105
|
+
const decision = DECISIONS.some((pattern) => pattern.test(target));
|
|
106
|
+
if (!read && !action && !decision) {
|
|
107
|
+
return (0, http_1.send)(res, 404, { error: 'not found' });
|
|
108
|
+
}
|
|
109
|
+
if (method === 'GET' || method === 'HEAD') {
|
|
110
|
+
if (!read) {
|
|
111
|
+
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: 'POST' });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else if (method === 'POST' && (action || decision)) {
|
|
115
|
+
// a page of another site can not set the header, nor read the token
|
|
116
|
+
const given = Buffer.from(String(req.headers['x-hammerkit-csrf'] ?? ''));
|
|
117
|
+
const expected = Buffer.from(csrf);
|
|
118
|
+
if (given.length !== expected.length || !(0, crypto_1.timingSafeEqual)(given, expected)) {
|
|
119
|
+
return (0, http_1.send)(res, 403, { error: 'the page is out of date, open it again' });
|
|
120
|
+
}
|
|
121
|
+
// an API token is new; a sign-in in the browser has to be of the last minutes to decide
|
|
122
|
+
if (decision && context.signedInAt !== undefined && now - context.signedInAt > exports.DECIDE_WITHIN_MS) {
|
|
123
|
+
return (0, http_1.send)(res, 403, {
|
|
124
|
+
error: `approving and rejecting take a sign-in of the last ${exports.DECIDE_WITHIN_MS / 60000} minutes`,
|
|
125
|
+
signInAgain: true,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
context.log(`dashboard ${method} ${target} iss=${principal.iss} sub=${principal.sub}`);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
return (0, http_1.send)(res, 405, { error: 'method not allowed' }, { allow: read ? 'GET' : 'POST' });
|
|
132
|
+
}
|
|
133
|
+
const forwarded = new URL(`${target}${url.search}`, 'http://localhost');
|
|
134
|
+
return context.api(principal, req, res, forwarded);
|
|
135
|
+
}
|
|
136
|
+
// The log as a text file: what the tasks printed, as they printed it.
|
|
137
|
+
function logText(context, viewer, res, id, task) {
|
|
138
|
+
const run = context.registry.peek(id);
|
|
139
|
+
// the log is for the owner of the run, who decides about it and observe
|
|
140
|
+
const logs = run && (0, overview_1.maySee)(viewer, run) && (0, overview_1.mayRead)(viewer, run) ? context.registry.logsOf(id) : undefined;
|
|
141
|
+
if (!run || !logs) {
|
|
142
|
+
return (0, http_1.send)(res, 404, { error: 'not found' });
|
|
143
|
+
}
|
|
144
|
+
const names = logs.index.names();
|
|
145
|
+
const chosen = task !== undefined && names.includes(task) ? task : undefined;
|
|
146
|
+
const lines = [];
|
|
147
|
+
const selected = chosen ? logs.index.lines(chosen) : undefined;
|
|
148
|
+
const total = selected ? selected.length : logs.index.size;
|
|
149
|
+
for (let position = 0; position < total; position++) {
|
|
150
|
+
const line = logs.index.text(logs.events, selected ? selected[position] : position);
|
|
151
|
+
lines.push(chosen ? line.text : `[${line.task}] ${line.text}`);
|
|
152
|
+
}
|
|
153
|
+
if (logs.truncated) {
|
|
154
|
+
lines.push('[hammerkit] the log was cut off at runs.maxLogBytes');
|
|
155
|
+
}
|
|
156
|
+
res.writeHead(200, {
|
|
157
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
158
|
+
'content-disposition': `attachment; filename="${run.id}${chosen ? `-${chosen.replace(/[^A-Za-z0-9._-]/g, '_')}` : ''}.log"`,
|
|
159
|
+
});
|
|
160
|
+
res.end(lines.join('\n') + (lines.length > 0 ? '\n' : ''));
|
|
161
|
+
}
|
|
162
|
+
// The files of the app: its scripts and styles by their hashed names, and index.html for
|
|
163
|
+
// every page, which the app draws.
|
|
164
|
+
async function asset(context, req, res, pathname) {
|
|
165
|
+
const root = context.assets ?? (0, path_1.join)(__dirname, '..', 'dashboard');
|
|
166
|
+
const file = /^\/assets\/[A-Za-z0-9_-][A-Za-z0-9._-]{0,200}$/.test(pathname)
|
|
167
|
+
? pathname.slice(1)
|
|
168
|
+
: pathname === '/favicon.svg'
|
|
169
|
+
? 'favicon.svg'
|
|
170
|
+
: pathname.startsWith('/assets/') || pathname.startsWith('/.')
|
|
171
|
+
? undefined
|
|
172
|
+
: 'index.html';
|
|
173
|
+
if (!file) {
|
|
174
|
+
return (0, http_1.send)(res, 404, { error: 'not found' });
|
|
175
|
+
}
|
|
176
|
+
let content;
|
|
177
|
+
try {
|
|
178
|
+
content = await (0, promises_1.readFile)((0, path_1.join)(root, file));
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
if (file === 'index.html') {
|
|
182
|
+
res.writeHead(503, { 'content-type': 'text/plain; charset=utf-8' });
|
|
183
|
+
res.end('The dashboard of this server was not built (npm run build:dashboard).\n');
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
return (0, http_1.send)(res, 404, { error: 'not found' });
|
|
187
|
+
}
|
|
188
|
+
const extension = /\.[a-z0-9]+$/.exec(file)?.[0] ?? '';
|
|
189
|
+
res.writeHead(200, {
|
|
190
|
+
'content-type': file === 'index.html' ? 'text/html; charset=utf-8' : TYPES[extension] ?? 'application/octet-stream',
|
|
191
|
+
// the names of the scripts and styles change with what they hold
|
|
192
|
+
...(file.startsWith('assets/') ? { 'cache-control': 'public, max-age=31536000, immutable' } : {}),
|
|
193
|
+
});
|
|
194
|
+
res.end(req.method === 'HEAD' ? undefined : content);
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=dashboard.js.map
|
package/dist/server/endpoints.js
CHANGED
|
@@ -10,6 +10,7 @@ const oauth_1 = require("./oauth");
|
|
|
10
10
|
const policy_1 = require("./policy");
|
|
11
11
|
const service_runs_1 = require("./service-runs");
|
|
12
12
|
const units_1 = require("../utils/units");
|
|
13
|
+
const dashboard_1 = require("./dashboard");
|
|
13
14
|
// The cookie of a sign-in at one endpoint host. `__Host-` keeps a page on another host of the
|
|
14
15
|
// domain from setting or overwriting it; it needs https, so plain http (on the loopback
|
|
15
16
|
// interface, for testing) goes without the prefix.
|
|
@@ -115,32 +116,41 @@ class EndpointGateway {
|
|
|
115
116
|
res.end();
|
|
116
117
|
return;
|
|
117
118
|
}
|
|
119
|
+
if (label === this.dashboardLabel() && url.pathname === `${OWN_PATH}signin`) {
|
|
120
|
+
// sign in again, with the groups and claims of now, and go back to a page of the dashboard
|
|
121
|
+
const back = url.searchParams.get('path') ?? '/';
|
|
122
|
+
const path = /^\/[A-Za-z0-9_\-/?=&.,]*$/.test(back) && !back.startsWith('//') ? back : '/';
|
|
123
|
+
return this.signIn(label, req, res, new URL(path, 'http://endpoint'));
|
|
124
|
+
}
|
|
125
|
+
if (label === this.dashboardLabel()) {
|
|
126
|
+
const principal = await this.caller(label, req);
|
|
127
|
+
if (!principal) {
|
|
128
|
+
return this.signIn(label, req, res, url);
|
|
129
|
+
}
|
|
130
|
+
const dashboard = this.options.dashboard;
|
|
131
|
+
if (!dashboard) {
|
|
132
|
+
return page(res, 404, 'Not found', '<p>This server has no dashboard.</p>');
|
|
133
|
+
}
|
|
134
|
+
// the token of what the app sends holds for this sign-in only: it dies with its session
|
|
135
|
+
const session = this.readSession(label, req.headers.cookie);
|
|
136
|
+
return (0, dashboard_1.handleDashboard)({
|
|
137
|
+
config: this.config,
|
|
138
|
+
registry: this.registry,
|
|
139
|
+
sign: (value) => this.sign(value),
|
|
140
|
+
log: this.options.log,
|
|
141
|
+
...(this.options.now ? { now: this.options.now } : {}),
|
|
142
|
+
...dashboard,
|
|
143
|
+
session: session ? `session:${session.exp}` : 'token',
|
|
144
|
+
// a cookie of before the sign-in time was kept counts as an old sign-in
|
|
145
|
+
...(session ? { signedInAt: session.at ?? 0 } : {}),
|
|
146
|
+
}, principal, req, res, url);
|
|
147
|
+
}
|
|
118
148
|
if (!found) {
|
|
119
149
|
return page(res, 404, 'Not found', '<p>There is no endpoint at this address.</p>');
|
|
120
150
|
}
|
|
121
151
|
const principal = await this.caller(label, req);
|
|
122
152
|
if (!principal) {
|
|
123
|
-
|
|
124
|
-
const bind = (0, crypto_1.randomBytes)(18).toString('base64url');
|
|
125
|
-
const start = new URL(this.url(exports.LOGIN_HOST, '/start'));
|
|
126
|
-
start.searchParams.set('host', label);
|
|
127
|
-
start.searchParams.set('path', url.pathname + url.search);
|
|
128
|
-
start.searchParams.set('bind', bind);
|
|
129
|
-
res.writeHead(302, {
|
|
130
|
-
location: start.toString(),
|
|
131
|
-
'set-cookie': this.cookie(this.bindCookieName(bind), bind, LOGIN_TIMEOUT_MS),
|
|
132
|
-
'cache-control': 'no-store',
|
|
133
|
-
});
|
|
134
|
-
res.end();
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
res.writeHead(401, {
|
|
138
|
-
'content-type': 'text/plain; charset=utf-8',
|
|
139
|
-
'www-authenticate': 'Bearer realm="hammerkit"',
|
|
140
|
-
'cache-control': 'no-store',
|
|
141
|
-
});
|
|
142
|
-
res.end('sign in: send a token in Hammerkit-Authorization: Bearer <token>\n');
|
|
143
|
-
return;
|
|
153
|
+
return this.signIn(label, req, res, url);
|
|
144
154
|
}
|
|
145
155
|
if (!this.mayOpen(principal, found.run)) {
|
|
146
156
|
this.options.log(`deny endpoint ${label} iss=${principal.iss} sub=${principal.sub}`);
|
|
@@ -207,6 +217,48 @@ class EndpointGateway {
|
|
|
207
217
|
socket.on('close', () => upstream.destroy());
|
|
208
218
|
upstream.on('close', () => socket.destroy());
|
|
209
219
|
}
|
|
220
|
+
// The host of the dashboard, when there is one: it needs the sign-in of a browser.
|
|
221
|
+
dashboardLabel() {
|
|
222
|
+
const label = this.config.endpoints?.dashboard;
|
|
223
|
+
return label && this.loginVerifier ? label : undefined;
|
|
224
|
+
}
|
|
225
|
+
// The address of the dashboard, for a browser that opens the address of the API.
|
|
226
|
+
dashboardUrl() {
|
|
227
|
+
const label = this.dashboardLabel();
|
|
228
|
+
return label ? this.url(label, '/') : undefined;
|
|
229
|
+
}
|
|
230
|
+
// A person in a browser is sent to sign in, anything else is told how to.
|
|
231
|
+
signIn(label, req, res, url) {
|
|
232
|
+
if (this.loginVerifier && req.method === 'GET' && acceptsHtml(req.headers) && isPageLoad(req.headers)) {
|
|
233
|
+
const bind = (0, crypto_1.randomBytes)(18).toString('base64url');
|
|
234
|
+
const start = new URL(this.url(exports.LOGIN_HOST, '/start'));
|
|
235
|
+
start.searchParams.set('host', label);
|
|
236
|
+
start.searchParams.set('path', url.pathname + url.search);
|
|
237
|
+
start.searchParams.set('bind', bind);
|
|
238
|
+
res.writeHead(302, {
|
|
239
|
+
location: start.toString(),
|
|
240
|
+
'set-cookie': this.cookie(this.bindCookieName(bind), bind, LOGIN_TIMEOUT_MS),
|
|
241
|
+
'cache-control': 'no-store',
|
|
242
|
+
});
|
|
243
|
+
res.end();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
res.writeHead(401, {
|
|
247
|
+
'content-type': 'text/plain; charset=utf-8',
|
|
248
|
+
'www-authenticate': 'Bearer realm="hammerkit"',
|
|
249
|
+
'cache-control': 'no-store',
|
|
250
|
+
});
|
|
251
|
+
res.end('sign in: send a token in Hammerkit-Authorization: Bearer <token>\n');
|
|
252
|
+
}
|
|
253
|
+
// Who may come back from the sign-in to a host: to an endpoint who may open it, to the
|
|
254
|
+
// dashboard anybody with an entitlement.
|
|
255
|
+
mayEnter(principal, host) {
|
|
256
|
+
if (host === this.dashboardLabel()) {
|
|
257
|
+
return (0, policy_1.getGrant)(this.config.entitlements, principal).matched.length > 0;
|
|
258
|
+
}
|
|
259
|
+
const found = this.registry.endpoint(host);
|
|
260
|
+
return !!found && this.mayOpen(principal, found.run);
|
|
261
|
+
}
|
|
210
262
|
// The owner of the run, or who has an `open` level for its repository, with the
|
|
211
263
|
// entitlements as they are now.
|
|
212
264
|
mayOpen(principal, run) {
|
|
@@ -381,7 +433,9 @@ ${run.state === 'up'
|
|
|
381
433
|
const host = url.searchParams.get('host') ?? '';
|
|
382
434
|
const path = url.searchParams.get('path') ?? '/';
|
|
383
435
|
const bind = url.searchParams.get('bind') ?? '';
|
|
384
|
-
if (!/^[a-z0-9-]+$/.test(host) ||
|
|
436
|
+
if (!/^[a-z0-9-]+$/.test(host) ||
|
|
437
|
+
host === exports.LOGIN_HOST ||
|
|
438
|
+
(host !== this.dashboardLabel() && !this.registry.endpoint(host))) {
|
|
385
439
|
return page(res, 404, 'Not found', '<p>There is no endpoint at this address.</p>');
|
|
386
440
|
}
|
|
387
441
|
if (!BIND_PATTERN.test(bind)) {
|
|
@@ -444,10 +498,9 @@ ${run.state === 'up'
|
|
|
444
498
|
this.options.log(`endpoint sign-in failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
445
499
|
return page(res, 400, 'Not signed in', '<p>The sign-in could not be checked, open the endpoint again.</p>');
|
|
446
500
|
}
|
|
447
|
-
|
|
448
|
-
if (!found || !this.mayOpen(principal, found.run)) {
|
|
501
|
+
if (!this.mayEnter(principal, pending.host)) {
|
|
449
502
|
this.options.log(`deny endpoint ${pending.host} iss=${principal.iss} sub=${principal.sub}`);
|
|
450
|
-
return page(res, 403, 'Not allowed', `<p>You signed in as <code>${escape(principal.sub)}</code>, which may not open this endpoint.</p>`);
|
|
503
|
+
return page(res, 403, 'Not allowed', `<p>You signed in as <code>${escape(principal.sub)}</code>, which may not open ${pending.host === this.dashboardLabel() ? 'the dashboard of this server' : 'this endpoint'}.</p>`);
|
|
451
504
|
}
|
|
452
505
|
const issued = (0, crypto_1.randomBytes)(24).toString('base64url');
|
|
453
506
|
this.codes.set(issued, {
|
|
@@ -601,6 +654,7 @@ ${run.state === 'up'
|
|
|
601
654
|
groups: issued.principal.groups,
|
|
602
655
|
claims: this.matchedClaims(issued.principal),
|
|
603
656
|
exp,
|
|
657
|
+
at: this.now(),
|
|
604
658
|
}),
|
|
605
659
|
],
|
|
606
660
|
'cache-control': 'no-store',
|