@tsplc/agent 0.5.9 → 0.5.10
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/cli.js +2462 -533
- package/dist/cli.js.map +30 -17
- package/dist/client.js +238 -36
- package/dist/client.js.map +4 -4
- package/dist/index.js +2162 -416
- package/dist/index.js.map +25 -13
- package/package.json +5 -5
- package/src/access.ts +257 -0
- package/src/agent.ts +101 -29
- package/src/audit.ts +64 -0
- package/src/cli.ts +58 -4
- package/src/client.ts +181 -36
- package/src/dashboard/index.ts +116 -0
- package/src/dashboard/ui/access.ts +192 -0
- package/src/dashboard/ui/actions.ts +142 -0
- package/src/dashboard/ui/api.ts +147 -0
- package/src/dashboard/ui/app.ts +41 -0
- package/src/dashboard/ui/approval.ts +132 -0
- package/src/dashboard/ui/audit.ts +115 -0
- package/src/dashboard/ui/contract.ts +16 -0
- package/src/dashboard/ui/dashboard.css +1332 -0
- package/src/dashboard/ui/deployment.ts +432 -0
- package/src/dashboard/ui/deployments.ts +317 -0
- package/src/dashboard/ui/details.ts +240 -0
- package/src/dashboard/ui/dom.ts +91 -0
- package/src/dashboard/ui/environment.ts +224 -0
- package/src/dashboard/ui/format.ts +118 -0
- package/src/dashboard/ui/logs.ts +127 -0
- package/src/dashboard/ui/main.ts +86 -0
- package/src/dashboard/ui/overlay.ts +146 -0
- package/src/dashboard/ui/shell.ts +171 -0
- package/src/dashboard/ui/source.ts +144 -0
- package/src/deploy.ts +138 -144
- package/src/deployments/health.ts +80 -0
- package/src/deployments/retention.ts +45 -0
- package/src/deployments/routes.ts +273 -0
- package/src/deployments/runner.ts +877 -0
- package/src/deployments/store.ts +233 -0
- package/src/environment.ts +205 -0
- package/src/index.ts +4 -0
- package/src/operations.ts +5 -1
- package/src/protocol.ts +218 -2
- package/src/releases.ts +73 -1
- package/src/routes.ts +52 -0
- package/src/secrets.ts +43 -2
- package/src/server-process.ts +33 -16
- package/src/sessions.ts +58 -0
- package/src/state.ts +2 -0
- package/src/tailscale.ts +112 -0
package/src/client.ts
CHANGED
|
@@ -13,7 +13,14 @@ import {
|
|
|
13
13
|
clientHeaders,
|
|
14
14
|
comparisonSchema,
|
|
15
15
|
controllerSchema,
|
|
16
|
+
type DeploymentLogLine,
|
|
17
|
+
type DeploymentMode,
|
|
18
|
+
type DeploymentRecord,
|
|
16
19
|
type DeployOptions,
|
|
20
|
+
deploymentCreatedSchema,
|
|
21
|
+
deploymentListSchema,
|
|
22
|
+
deploymentLogLineSchema,
|
|
23
|
+
deploymentRecordSchema,
|
|
17
24
|
errorSchema,
|
|
18
25
|
eventSchema,
|
|
19
26
|
logoutSchema,
|
|
@@ -59,6 +66,52 @@ export class AgentOperationError extends Error {
|
|
|
59
66
|
}
|
|
60
67
|
}
|
|
61
68
|
|
|
69
|
+
/** The agent can't be reached, or the connection to it broke; trying again may succeed. */
|
|
70
|
+
export class AgentUnreachable extends Error {}
|
|
71
|
+
|
|
72
|
+
/** One of a deployment's server-sent events: the whole record, or one log line. */
|
|
73
|
+
export type DeploymentEvent =
|
|
74
|
+
| { kind: "record"; record: DeploymentRecord }
|
|
75
|
+
| { kind: "log"; line: DeploymentLogLine };
|
|
76
|
+
|
|
77
|
+
/** Each event of a `text/event-stream` body as its name and data; comments and other fields are skipped. */
|
|
78
|
+
async function* serverSentEvents(body: NonNullable<Response["body"]>) {
|
|
79
|
+
const reader = body.pipeThrough(new TextDecoderStream()).getReader();
|
|
80
|
+
let buffered = "";
|
|
81
|
+
let name = "message";
|
|
82
|
+
let data: string[] = [];
|
|
83
|
+
try {
|
|
84
|
+
while (true) {
|
|
85
|
+
const { done, value } = await reader.read().catch((cause: unknown) => {
|
|
86
|
+
throw new AgentUnreachable("The connection to the agent broke", {
|
|
87
|
+
cause,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
if (done) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
buffered += value;
|
|
94
|
+
const lines = buffered.split(/\r?\n/);
|
|
95
|
+
buffered = lines.pop() ?? "";
|
|
96
|
+
for (const line of lines) {
|
|
97
|
+
if (line === "") {
|
|
98
|
+
if (data.length) {
|
|
99
|
+
yield { name, data: data.join("\n") };
|
|
100
|
+
}
|
|
101
|
+
name = "message";
|
|
102
|
+
data = [];
|
|
103
|
+
} else if (line.startsWith("event:")) {
|
|
104
|
+
name = line.slice("event:".length).trim();
|
|
105
|
+
} else if (line.startsWith("data:")) {
|
|
106
|
+
data.push(line.slice("data:".length).replace(/^ /, ""));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} finally {
|
|
111
|
+
await reader.cancel().catch(() => {});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
62
115
|
/** A file to upload, at its path in the release. */
|
|
63
116
|
export interface ReleaseFile {
|
|
64
117
|
path: string;
|
|
@@ -80,8 +133,8 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
80
133
|
[clientHeaders.version]: tsplcVersion,
|
|
81
134
|
...scripts,
|
|
82
135
|
}));
|
|
83
|
-
const
|
|
84
|
-
|
|
136
|
+
const request = async (path: string, init: RequestInit = {}) =>
|
|
137
|
+
fetch(new URL(path, options.url), {
|
|
85
138
|
...init,
|
|
86
139
|
headers: {
|
|
87
140
|
...init.headers,
|
|
@@ -89,10 +142,12 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
89
142
|
Authorization: `Bearer ${options.token}`,
|
|
90
143
|
},
|
|
91
144
|
}).catch((cause: unknown) => {
|
|
92
|
-
throw new
|
|
93
|
-
|
|
94
|
-
|
|
145
|
+
throw new AgentUnreachable(
|
|
146
|
+
`Cannot reach the plant agent at ${options.url}`,
|
|
147
|
+
{ cause },
|
|
148
|
+
);
|
|
95
149
|
});
|
|
150
|
+
const answered = async (response: Response) => {
|
|
96
151
|
if (!response.ok) {
|
|
97
152
|
const body = await response.json().catch(() => ({}));
|
|
98
153
|
throw new AgentOperationError(
|
|
@@ -102,6 +157,8 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
102
157
|
}
|
|
103
158
|
return response;
|
|
104
159
|
};
|
|
160
|
+
const call = async (path: string, init: RequestInit = {}) =>
|
|
161
|
+
answered(await request(path, init));
|
|
105
162
|
const get = async <T>(path: string, schema: z.ZodType<T>) =>
|
|
106
163
|
schema.parse(await (await call(path)).json());
|
|
107
164
|
const send = async <T>(
|
|
@@ -160,49 +217,64 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
160
217
|
const compare = (buckets: string[]) =>
|
|
161
218
|
send("POST", "/releases/compare", { buckets }, comparisonSchema);
|
|
162
219
|
|
|
220
|
+
/**
|
|
221
|
+
* The upload body of `release`, built on what the agent already holds. Refuses an agent of
|
|
222
|
+
* another version first.
|
|
223
|
+
*/
|
|
224
|
+
const upload = async (
|
|
225
|
+
release: { manifest: ReleaseManifest; files: ReleaseFile[] },
|
|
226
|
+
settings: DeployOptions,
|
|
227
|
+
onProgress: (message: string) => void,
|
|
228
|
+
) => {
|
|
229
|
+
// Before uploading: an agent of another version, including one from before version checks,
|
|
230
|
+
// would compile the release with scripts this client doesn't expect.
|
|
231
|
+
const agentVersion = (await call("/status")).headers.get(
|
|
232
|
+
agentVersionHeader,
|
|
233
|
+
);
|
|
234
|
+
if (agentVersion !== tsplcVersion) {
|
|
235
|
+
throw new AgentOperationError(
|
|
236
|
+
versionMismatch({
|
|
237
|
+
agent: agentVersion,
|
|
238
|
+
client: tsplcVersion,
|
|
239
|
+
agentUpgrades: upgradesItself(agentVersion),
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const files = await Promise.all(
|
|
244
|
+
release.files.map(async ({ path, content }) => ({
|
|
245
|
+
path,
|
|
246
|
+
content: await content.bytes(),
|
|
247
|
+
})),
|
|
248
|
+
);
|
|
249
|
+
const digests = files.map(({ path, content }) => ({
|
|
250
|
+
path,
|
|
251
|
+
digest: shortDigest(content),
|
|
252
|
+
}));
|
|
253
|
+
const buckets = bucketDigests(digests);
|
|
254
|
+
return releaseUpload(
|
|
255
|
+
{ manifest: release.manifest, options: settings, buckets },
|
|
256
|
+
files,
|
|
257
|
+
await compare(buckets),
|
|
258
|
+
onProgress,
|
|
259
|
+
);
|
|
260
|
+
};
|
|
261
|
+
|
|
163
262
|
return {
|
|
263
|
+
/** The agent's address, where its dashboard is. */
|
|
264
|
+
url: options.url,
|
|
164
265
|
status: () => get("/status", statusSchema),
|
|
165
266
|
interfaces: () => get("/interfaces", z.array(networkInterfaceSchema)),
|
|
166
267
|
discover: (name: string) =>
|
|
167
268
|
send("POST", "/discover", { name }, z.array(controllerSchema)),
|
|
168
269
|
setTarget: (target: Target) => send("PUT", "/target", target, targetSchema),
|
|
169
270
|
variables: () => get("/variables", z.array(variableSchema)),
|
|
271
|
+
/** Deploys `release` at once, streaming its progress: `POST /deploy`. */
|
|
170
272
|
deploy: async (
|
|
171
273
|
release: { manifest: ReleaseManifest; files: ReleaseFile[] },
|
|
172
274
|
settings: DeployOptions,
|
|
173
275
|
onProgress: (message: string) => void,
|
|
174
276
|
) => {
|
|
175
|
-
|
|
176
|
-
// would compile the release with scripts this client doesn't expect.
|
|
177
|
-
const agentVersion = (await call("/status")).headers.get(
|
|
178
|
-
agentVersionHeader,
|
|
179
|
-
);
|
|
180
|
-
if (agentVersion !== tsplcVersion) {
|
|
181
|
-
throw new AgentOperationError(
|
|
182
|
-
versionMismatch({
|
|
183
|
-
agent: agentVersion,
|
|
184
|
-
client: tsplcVersion,
|
|
185
|
-
agentUpgrades: upgradesItself(agentVersion),
|
|
186
|
-
}),
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
const files = await Promise.all(
|
|
190
|
-
release.files.map(async ({ path, content }) => ({
|
|
191
|
-
path,
|
|
192
|
-
content: await content.bytes(),
|
|
193
|
-
})),
|
|
194
|
-
);
|
|
195
|
-
const digests = files.map(({ path, content }) => ({
|
|
196
|
-
path,
|
|
197
|
-
digest: shortDigest(content),
|
|
198
|
-
}));
|
|
199
|
-
const buckets = bucketDigests(digests);
|
|
200
|
-
const body = await releaseUpload(
|
|
201
|
-
{ manifest: release.manifest, options: settings, buckets },
|
|
202
|
-
files,
|
|
203
|
-
await compare(buckets),
|
|
204
|
-
onProgress,
|
|
205
|
-
);
|
|
277
|
+
const body = await upload(release, settings, onProgress);
|
|
206
278
|
return releaseSchema.parse(
|
|
207
279
|
await operation(
|
|
208
280
|
await call("/deploy", { method: "POST", ...body }),
|
|
@@ -210,6 +282,79 @@ export const createAgentClient = (options: { url: string; token: string }) => {
|
|
|
210
282
|
),
|
|
211
283
|
);
|
|
212
284
|
},
|
|
285
|
+
/**
|
|
286
|
+
* Queues `release` as a deployment the agent runs on its own: `POST /api/deployments`. Null
|
|
287
|
+
* when the agent has no deployments API, so the caller can deploy at once instead.
|
|
288
|
+
*/
|
|
289
|
+
queueDeployment: async (
|
|
290
|
+
release: { manifest: ReleaseManifest; files: ReleaseFile[] },
|
|
291
|
+
settings: DeployOptions,
|
|
292
|
+
onProgress: (message: string) => void,
|
|
293
|
+
) => {
|
|
294
|
+
const body = await upload(release, settings, onProgress);
|
|
295
|
+
const response = await request("/api/deployments", {
|
|
296
|
+
method: "POST",
|
|
297
|
+
...body,
|
|
298
|
+
});
|
|
299
|
+
if (response.status === 404) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
return deploymentCreatedSchema.parse(
|
|
303
|
+
await (await answered(response)).json(),
|
|
304
|
+
);
|
|
305
|
+
},
|
|
306
|
+
deployments: () => get("/api/deployments", deploymentListSchema),
|
|
307
|
+
/**
|
|
308
|
+
* A deployment's events from after log line `after`, until the agent ends the stream once
|
|
309
|
+
* the deployment finishes. A broken connection throws `AgentUnreachable`.
|
|
310
|
+
*/
|
|
311
|
+
deploymentEvents: async function* (
|
|
312
|
+
id: string,
|
|
313
|
+
after: number,
|
|
314
|
+
): AsyncGenerator<DeploymentEvent> {
|
|
315
|
+
const response = await call(
|
|
316
|
+
`/api/deployments/${encodeURIComponent(id)}/events?after=${after}`,
|
|
317
|
+
{ headers: { Accept: "text/event-stream" } },
|
|
318
|
+
);
|
|
319
|
+
if (!response.body) {
|
|
320
|
+
throw new AgentUnreachable("The agent returned no event stream");
|
|
321
|
+
}
|
|
322
|
+
for await (const event of serverSentEvents(response.body)) {
|
|
323
|
+
if (event.name === "record") {
|
|
324
|
+
yield {
|
|
325
|
+
kind: "record",
|
|
326
|
+
record: deploymentRecordSchema.parse(JSON.parse(event.data)),
|
|
327
|
+
};
|
|
328
|
+
} else if (event.name === "log") {
|
|
329
|
+
yield {
|
|
330
|
+
kind: "log",
|
|
331
|
+
line: deploymentLogLineSchema.parse(JSON.parse(event.data)),
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
/** Approves a deployment that waits on a person, as a full download when `download`. */
|
|
337
|
+
approve: async (id: string, approval: { download: boolean }) => {
|
|
338
|
+
await call(`/api/deployments/${encodeURIComponent(id)}/approve`, {
|
|
339
|
+
method: "POST",
|
|
340
|
+
headers: { "Content-Type": "application/json" },
|
|
341
|
+
body: JSON.stringify(approval),
|
|
342
|
+
});
|
|
343
|
+
},
|
|
344
|
+
redeploy: (id: string, mode: DeploymentMode) =>
|
|
345
|
+
send(
|
|
346
|
+
"POST",
|
|
347
|
+
`/api/deployments/${encodeURIComponent(id)}/redeploy`,
|
|
348
|
+
{ mode },
|
|
349
|
+
deploymentCreatedSchema,
|
|
350
|
+
),
|
|
351
|
+
rollback: (id: string) =>
|
|
352
|
+
send(
|
|
353
|
+
"POST",
|
|
354
|
+
`/api/deployments/${encodeURIComponent(id)}/rollback`,
|
|
355
|
+
{},
|
|
356
|
+
deploymentCreatedSchema,
|
|
357
|
+
),
|
|
213
358
|
login: async (
|
|
214
359
|
credentials: Credentials | null,
|
|
215
360
|
onProgress: (message: string) => void,
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { basename, dirname, resolve } from "node:path";
|
|
3
|
+
import { errorMessage } from "../errors";
|
|
4
|
+
import { allows, roles } from "../protocol";
|
|
5
|
+
|
|
6
|
+
/** The pages a browser loads: they hold no data, which they fetch from `/api` as whoever is signed in. */
|
|
7
|
+
const pages = ["/", "/deployments", "/environment", "/audit", "/access"];
|
|
8
|
+
|
|
9
|
+
/** Where the pages' script and stylesheet are served; like the pages, they hold no data. */
|
|
10
|
+
export const dashboardAssets = "/dashboard-assets/";
|
|
11
|
+
|
|
12
|
+
const page = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>tsPLC</title><script>document.documentElement.dataset.theme=localStorage.getItem("tsplc-theme")||""</script><link rel="stylesheet" href="${dashboardAssets}dashboard.css"><script type="module" src="${dashboardAssets}main.js"></script></head><body><main id="app"></main></body></html>`;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What the browser needs from protocol.ts at run time. The bundle can't import protocol.ts,
|
|
16
|
+
* whose other schemas load the agent's Node-only packages, so the bundle is given the owner's
|
|
17
|
+
* answers instead.
|
|
18
|
+
*/
|
|
19
|
+
const contract = {
|
|
20
|
+
roles,
|
|
21
|
+
permissions: Object.fromEntries(
|
|
22
|
+
roles.map((role) => [
|
|
23
|
+
role,
|
|
24
|
+
Object.fromEntries(
|
|
25
|
+
roles.map((required) => [required, allows(role, required)]),
|
|
26
|
+
),
|
|
27
|
+
]),
|
|
28
|
+
),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
interface Asset {
|
|
32
|
+
text: string;
|
|
33
|
+
type: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const bundle = async () => {
|
|
37
|
+
// Resolved through the package, so a published `dist/` bundle finds the shipped sources.
|
|
38
|
+
const sources = resolve(
|
|
39
|
+
dirname(createRequire(import.meta.url).resolve("@tsplc/agent")),
|
|
40
|
+
"../src/dashboard/ui",
|
|
41
|
+
);
|
|
42
|
+
const result = await Bun.build({
|
|
43
|
+
entrypoints: [
|
|
44
|
+
resolve(sources, "main.ts"),
|
|
45
|
+
resolve(sources, "dashboard.css"),
|
|
46
|
+
],
|
|
47
|
+
target: "browser",
|
|
48
|
+
// Workspace packages resolve to their sources, as they do for the host, so no build is needed first.
|
|
49
|
+
conditions: ["bun"],
|
|
50
|
+
minify: true,
|
|
51
|
+
naming: "[name].[ext]",
|
|
52
|
+
define: { TSPLC_CONTRACT: JSON.stringify(contract) },
|
|
53
|
+
});
|
|
54
|
+
if (!result.success) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Bundling the dashboard failed:\n${result.logs.map(String).join("\n")}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const assets = new Map<string, Asset>();
|
|
60
|
+
for (const output of result.outputs) {
|
|
61
|
+
assets.set(basename(output.path), {
|
|
62
|
+
text: await output.text(),
|
|
63
|
+
type: output.type,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return assets;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const isPage = (pathname: string) =>
|
|
70
|
+
pages.some(
|
|
71
|
+
(path) =>
|
|
72
|
+
pathname === path || (path !== "/" && pathname.startsWith(`${path}/`)),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The agent's dashboard: its pages, answered for any path under them, and their script and
|
|
77
|
+
* stylesheet, bundled on first request and then kept.
|
|
78
|
+
*/
|
|
79
|
+
export const createDashboard = () => {
|
|
80
|
+
let assets: Promise<Map<string, Asset>> | undefined;
|
|
81
|
+
return {
|
|
82
|
+
handle: async (request: Request): Promise<Response | undefined> => {
|
|
83
|
+
const { pathname } = new URL(request.url);
|
|
84
|
+
if (request.method !== "GET") {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
if (isPage(pathname)) {
|
|
88
|
+
return new Response(page, {
|
|
89
|
+
headers: {
|
|
90
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
91
|
+
"Cache-Control": "no-cache",
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (!pathname.startsWith(dashboardAssets)) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
assets ??= bundle();
|
|
99
|
+
try {
|
|
100
|
+
const asset = (await assets).get(
|
|
101
|
+
pathname.slice(dashboardAssets.length),
|
|
102
|
+
);
|
|
103
|
+
if (!asset) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
return new Response(asset.text, {
|
|
107
|
+
headers: { "Content-Type": asset.type, "Cache-Control": "no-cache" },
|
|
108
|
+
});
|
|
109
|
+
} catch (error) {
|
|
110
|
+
// A failed bundle is tried again on the next request rather than kept.
|
|
111
|
+
assets = undefined;
|
|
112
|
+
return new Response(errorMessage(error), { status: 500 });
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { errorMessage } from "../../errors";
|
|
2
|
+
import type { AccessConfig, Role } from "../../protocol";
|
|
3
|
+
import { api } from "./api";
|
|
4
|
+
import { type App, attempt } from "./app";
|
|
5
|
+
import { roleNames } from "./contract";
|
|
6
|
+
import { empty, fill, h, pageHeader, type View } from "./dom";
|
|
7
|
+
|
|
8
|
+
const noRole = "";
|
|
9
|
+
|
|
10
|
+
const roleSelect = (selected: Role | null, options: { none: string | null }) =>
|
|
11
|
+
h(
|
|
12
|
+
"select",
|
|
13
|
+
{ class: "input" },
|
|
14
|
+
options.none === null
|
|
15
|
+
? null
|
|
16
|
+
: h(
|
|
17
|
+
"option",
|
|
18
|
+
{ value: noRole, selected: selected === null },
|
|
19
|
+
options.none,
|
|
20
|
+
),
|
|
21
|
+
...roleNames.map((role) =>
|
|
22
|
+
h("option", { value: role, selected: role === selected }, role),
|
|
23
|
+
),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
const chosenRole = (select: HTMLSelectElement) =>
|
|
27
|
+
roleNames.find((role) => role === select.value) ?? null;
|
|
28
|
+
|
|
29
|
+
/** Who has which role: by Tailscale login, and the role any other member of the tailnet gets. */
|
|
30
|
+
export const accessView = (app: App): View => {
|
|
31
|
+
const holder = h("div", {}, empty("Loading access…"));
|
|
32
|
+
if (!app.can("admin")) {
|
|
33
|
+
fill(holder, empty("Only an admin can see and change who has which role."));
|
|
34
|
+
return {
|
|
35
|
+
element: h("div", { class: "page" }, pageHeader("Access", null), holder),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const render = (access: AccessConfig) => {
|
|
40
|
+
const logins = Object.entries(access.roles).sort(([a], [b]) =>
|
|
41
|
+
a.localeCompare(b),
|
|
42
|
+
);
|
|
43
|
+
const rows = logins.map(([login, role]) => ({
|
|
44
|
+
login,
|
|
45
|
+
select: roleSelect(role, { none: null }),
|
|
46
|
+
}));
|
|
47
|
+
const tailnet = roleSelect(access.tailnetDefault, { none: "No access" });
|
|
48
|
+
const newLogin = h("input", {
|
|
49
|
+
class: "input",
|
|
50
|
+
type: "email",
|
|
51
|
+
placeholder: "someone@example.com",
|
|
52
|
+
autocomplete: "off",
|
|
53
|
+
});
|
|
54
|
+
const newRole = roleSelect("viewer", { none: null });
|
|
55
|
+
const save = async (next: AccessConfig) => {
|
|
56
|
+
const saved = await attempt(() => api.saveAccess(next), "Saved access");
|
|
57
|
+
if (saved) {
|
|
58
|
+
render(saved);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const current = (): AccessConfig => ({
|
|
62
|
+
roles: Object.fromEntries(
|
|
63
|
+
rows.flatMap(({ login, select }) => {
|
|
64
|
+
const role = chosenRole(select);
|
|
65
|
+
return role ? [[login, role]] : [];
|
|
66
|
+
}),
|
|
67
|
+
),
|
|
68
|
+
tailnetDefault: chosenRole(tailnet),
|
|
69
|
+
});
|
|
70
|
+
fill(
|
|
71
|
+
holder,
|
|
72
|
+
h(
|
|
73
|
+
"section",
|
|
74
|
+
{ class: "card" },
|
|
75
|
+
h("h3", {}, "Tailnet default"),
|
|
76
|
+
h(
|
|
77
|
+
"p",
|
|
78
|
+
{ class: "muted" },
|
|
79
|
+
"The role of a member of the tailnet without a role of their own.",
|
|
80
|
+
),
|
|
81
|
+
tailnet,
|
|
82
|
+
),
|
|
83
|
+
h(
|
|
84
|
+
"section",
|
|
85
|
+
{ class: "card" },
|
|
86
|
+
h("h3", {}, "Roles by login"),
|
|
87
|
+
h(
|
|
88
|
+
"p",
|
|
89
|
+
{ class: "muted" },
|
|
90
|
+
"A viewer reads deployments, logs, the debugger and the audit log. A deployer also deploys, approves, rolls back and restarts the server. An admin also manages secrets and access.",
|
|
91
|
+
),
|
|
92
|
+
rows.length
|
|
93
|
+
? h(
|
|
94
|
+
"table",
|
|
95
|
+
{ class: "table" },
|
|
96
|
+
h(
|
|
97
|
+
"thead",
|
|
98
|
+
{},
|
|
99
|
+
h(
|
|
100
|
+
"tr",
|
|
101
|
+
{},
|
|
102
|
+
h("th", {}, "Tailscale login"),
|
|
103
|
+
h("th", {}, "Role"),
|
|
104
|
+
h("th", {}, ""),
|
|
105
|
+
),
|
|
106
|
+
),
|
|
107
|
+
h(
|
|
108
|
+
"tbody",
|
|
109
|
+
{},
|
|
110
|
+
...rows.map(({ login, select }) =>
|
|
111
|
+
h(
|
|
112
|
+
"tr",
|
|
113
|
+
{},
|
|
114
|
+
h("td", { class: "mono" }, login),
|
|
115
|
+
h("td", {}, select),
|
|
116
|
+
h(
|
|
117
|
+
"td",
|
|
118
|
+
{ class: "row-actions" },
|
|
119
|
+
h(
|
|
120
|
+
"button",
|
|
121
|
+
{
|
|
122
|
+
class: "button small danger-text",
|
|
123
|
+
onclick: () => {
|
|
124
|
+
const next = current();
|
|
125
|
+
void save({
|
|
126
|
+
...next,
|
|
127
|
+
roles: Object.fromEntries(
|
|
128
|
+
Object.entries(next.roles).filter(
|
|
129
|
+
([name]) => name !== login,
|
|
130
|
+
),
|
|
131
|
+
),
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
"Remove",
|
|
136
|
+
),
|
|
137
|
+
),
|
|
138
|
+
),
|
|
139
|
+
),
|
|
140
|
+
),
|
|
141
|
+
)
|
|
142
|
+
: empty("No login has a role yet."),
|
|
143
|
+
h(
|
|
144
|
+
"form",
|
|
145
|
+
{
|
|
146
|
+
class: "inline-form",
|
|
147
|
+
onsubmit: (event) => {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
const login = newLogin.value.trim();
|
|
150
|
+
const role = chosenRole(newRole);
|
|
151
|
+
if (!login || !role) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const next = current();
|
|
155
|
+
void save({ ...next, roles: { ...next.roles, [login]: role } });
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
newLogin,
|
|
159
|
+
newRole,
|
|
160
|
+
h("button", { class: "button", type: "submit" }, "Add"),
|
|
161
|
+
),
|
|
162
|
+
),
|
|
163
|
+
h(
|
|
164
|
+
"div",
|
|
165
|
+
{ class: "form-actions" },
|
|
166
|
+
h(
|
|
167
|
+
"button",
|
|
168
|
+
{ class: "button primary", onclick: () => void save(current()) },
|
|
169
|
+
"Save changes",
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
void api
|
|
176
|
+
.access()
|
|
177
|
+
.then(render, (error: unknown) =>
|
|
178
|
+
fill(holder, h("p", { class: "error-text" }, errorMessage(error))),
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
element: h(
|
|
183
|
+
"div",
|
|
184
|
+
{ class: "page" },
|
|
185
|
+
pageHeader(
|
|
186
|
+
"Access",
|
|
187
|
+
"Who may view, deploy and administer this agent. The agent's tokens keep their roles: the full token is an admin, the read token a viewer.",
|
|
188
|
+
),
|
|
189
|
+
holder,
|
|
190
|
+
),
|
|
191
|
+
};
|
|
192
|
+
};
|