@tsplc/tmp-agent 0.0.0-stage → 0.6.2
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/README.md +26 -2
- package/dist/cli.js +4888 -0
- package/dist/cli.js.map +53 -0
- package/dist/client.js +514 -0
- package/dist/client.js.map +12 -0
- package/dist/index.js +3680 -0
- package/dist/index.js.map +41 -0
- package/package.json +61 -4
- package/src/access.ts +260 -0
- package/src/agent.ts +461 -0
- package/src/audit.ts +74 -0
- package/src/cli.ts +290 -0
- package/src/client.ts +407 -0
- package/src/codesys.ts +195 -0
- package/src/dashboard/index.ts +159 -0
- package/src/dashboard/ui/access.ts +191 -0
- package/src/dashboard/ui/actions.ts +207 -0
- package/src/dashboard/ui/api.ts +138 -0
- package/src/dashboard/ui/app.ts +41 -0
- package/src/dashboard/ui/approval.ts +131 -0
- package/src/dashboard/ui/audit.ts +115 -0
- package/src/dashboard/ui/contract.ts +19 -0
- package/src/dashboard/ui/dashboard.css +1332 -0
- package/src/dashboard/ui/deployment.ts +431 -0
- package/src/dashboard/ui/deployments.ts +319 -0
- package/src/dashboard/ui/details.ts +214 -0
- package/src/dashboard/ui/dom.ts +87 -0
- package/src/dashboard/ui/environment.ts +345 -0
- package/src/dashboard/ui/format.ts +124 -0
- package/src/dashboard/ui/logs.ts +126 -0
- package/src/dashboard/ui/main.ts +88 -0
- package/src/dashboard/ui/overlay.ts +144 -0
- package/src/dashboard/ui/shell.ts +185 -0
- package/src/dashboard/ui/source.ts +144 -0
- package/src/debugging.ts +142 -0
- package/src/deploy.ts +203 -0
- package/src/deployments/health.ts +98 -0
- package/src/deployments/retention.ts +44 -0
- package/src/deployments/routes.ts +281 -0
- package/src/deployments/runner.ts +900 -0
- package/src/deployments/store.ts +209 -0
- package/src/deployments/submission.ts +78 -0
- package/src/discovery.ts +90 -0
- package/src/environment.ts +214 -0
- package/src/errors.ts +12 -0
- package/src/index.ts +36 -0
- package/src/instance.ts +51 -0
- package/src/login.ts +267 -0
- package/src/logs.ts +115 -0
- package/src/maintenance.ts +303 -0
- package/src/operations.ts +149 -0
- package/src/processes.ts +41 -0
- package/src/protocol.ts +557 -0
- package/src/releases.ts +414 -0
- package/src/restarts.ts +96 -0
- package/src/routes.ts +128 -0
- package/src/secrets.ts +174 -0
- package/src/server-process.ts +264 -0
- package/src/service.ts +194 -0
- package/src/sessions.ts +58 -0
- package/src/state.ts +126 -0
- package/src/tailscale.ts +112 -0
- package/src/terminal.ts +57 -0
- package/src/transfer.ts +94 -0
- package/src/upgrade.ts +153 -0
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsplc/tmp-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.6.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"description": "The tsPLC plant agent: deploys a program and its server to a controller on the plant network, and holds an engineering login for reads, writes and forces.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"plc",
|
|
9
|
+
"codesys",
|
|
10
|
+
"deployment"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/rip-technologies/tsplc#readme",
|
|
13
|
+
"bugs": "https://github.com/rip-technologies/tsplc/issues",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/rip-technologies/tsplc.git",
|
|
17
|
+
"directory": "packages/agent"
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./src/index.ts",
|
|
22
|
+
"bun": "./src/index.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./client": {
|
|
26
|
+
"types": "./src/client.ts",
|
|
27
|
+
"bun": "./src/client.ts",
|
|
28
|
+
"default": "./dist/client.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"src",
|
|
34
|
+
"!**/*.test.ts",
|
|
35
|
+
"!**/*.fixtures.ts",
|
|
36
|
+
"!**/test_*.py",
|
|
37
|
+
"!**/__pycache__/**",
|
|
38
|
+
"!**/*.pyc",
|
|
39
|
+
"!**/tests/**"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"bun": ">=1.4.3"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@tsplc/build": "npm:@tsplc/tmp-build@0.6.2",
|
|
46
|
+
"@tsplc/config": "npm:@tsplc/tmp-config@0.6.2",
|
|
47
|
+
"@tsplc/debug": "npm:@tsplc/tmp-debug@0.6.2",
|
|
48
|
+
"@tsplc/iec": "npm:@tsplc/tmp-iec@0.6.2",
|
|
49
|
+
"@tsplc/server": "npm:@tsplc/tmp-server@0.6.2",
|
|
50
|
+
"zod": "4.3.6"
|
|
51
|
+
},
|
|
52
|
+
"bin": {
|
|
53
|
+
"tsplc-agent": "./dist/cli.js"
|
|
54
|
+
},
|
|
55
|
+
"buildEntries": {
|
|
56
|
+
"./dist/cli.js": {
|
|
57
|
+
"source": "./src/cli.ts"
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
"publishConfig": {
|
|
61
|
+
"access": "public"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/access.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
import { dashboardHeader, tokenQueryParameter } from "@tsplc/debug/api";
|
|
4
|
+
import { controllerRestartPath } from "@tsplc/server/plant-agent";
|
|
5
|
+
import { type AuditLog, authorOf } from "./audit";
|
|
6
|
+
import { errorMessage } from "./errors";
|
|
7
|
+
import {
|
|
8
|
+
type AccessConfig,
|
|
9
|
+
accessConfigSchema,
|
|
10
|
+
debuggerMount,
|
|
11
|
+
type Identity,
|
|
12
|
+
sessionRequestSchema,
|
|
13
|
+
} from "./protocol";
|
|
14
|
+
import type { ApiRoute } from "./routes";
|
|
15
|
+
import { createSessions } from "./sessions";
|
|
16
|
+
import { readJsonFile, writeJsonFile } from "./state";
|
|
17
|
+
import { peerKind, type Whois } from "./tailscale";
|
|
18
|
+
|
|
19
|
+
const noAccess: AccessConfig = { roles: {}, tailnetDefault: null };
|
|
20
|
+
|
|
21
|
+
export const readAccessConfig = async (file: string) =>
|
|
22
|
+
(await readJsonFile(file, accessConfigSchema)) ?? noAccess;
|
|
23
|
+
|
|
24
|
+
/** Compares digests, so the time taken says nothing about the token, not even its length. */
|
|
25
|
+
const matches = (presented: string, token: string) =>
|
|
26
|
+
timingSafeEqual(
|
|
27
|
+
createHash("sha256").update(presented).digest(),
|
|
28
|
+
createHash("sha256").update(token).digest(),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
/** Where a request came from, as the HTTP server reports it. */
|
|
32
|
+
export interface Peer {
|
|
33
|
+
address: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const refuse = (status: 401 | 403, error: string) =>
|
|
37
|
+
Response.json({ error }, { status });
|
|
38
|
+
const unauthorized = () => refuse(401, "Unauthorized");
|
|
39
|
+
|
|
40
|
+
/** A proxy on the agent's PC would make every request it forwards look local. */
|
|
41
|
+
const forwardingHeaders = [
|
|
42
|
+
"forwarded",
|
|
43
|
+
"x-forwarded-for",
|
|
44
|
+
"tailscale-user-login",
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether the request names the agent by an address, a single-label name or a MagicDNS name.
|
|
49
|
+
* Another site that points its own name at the agent (DNS rebinding) names it by that, and gets
|
|
50
|
+
* no identity from where the browser runs.
|
|
51
|
+
*/
|
|
52
|
+
const namesAgentDirectly = (request: Request) => {
|
|
53
|
+
const host = new URL(request.url).hostname.replace(/^\[(.*)\]$/, "$1");
|
|
54
|
+
return (
|
|
55
|
+
isIP(host) !== 0 ||
|
|
56
|
+
!host.includes(".") ||
|
|
57
|
+
host.endsWith(".ts.net") ||
|
|
58
|
+
host === "localhost"
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Anything but `GET` may change something, so another site must not be able to send it. */
|
|
63
|
+
const changes = (request: Request) => request.method !== "GET";
|
|
64
|
+
const fromDashboard = (request: Request) =>
|
|
65
|
+
request.headers.get(dashboardHeader) === "1";
|
|
66
|
+
const crossSite = () =>
|
|
67
|
+
refuse(
|
|
68
|
+
403,
|
|
69
|
+
`A browser's request that changes something needs the ${dashboardHeader}: 1 header`,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Who a request is from, and the role that gives them. The full token is an admin and the read
|
|
74
|
+
* token a viewer, as a bearer token or a browser session signed in with one. The deployed server's
|
|
75
|
+
* token restarts the controller and does nothing else; no browser signs in with it. Under `/api` and
|
|
76
|
+
* `/debug`, a request from the agent's own PC is an admin, and one from a tailnet member has the
|
|
77
|
+
* role `access.json` gives their login.
|
|
78
|
+
*/
|
|
79
|
+
export const createAccess = (options: {
|
|
80
|
+
token: string;
|
|
81
|
+
readToken: string | null;
|
|
82
|
+
/** The token the agent starts the deployed server with, which may only restart the controller. */
|
|
83
|
+
serverToken: string | null;
|
|
84
|
+
/** `access.json`, read on each tailnet request so `tsplc-agent access` applies at once. */
|
|
85
|
+
file: string;
|
|
86
|
+
whois: Whois;
|
|
87
|
+
audit: AuditLog;
|
|
88
|
+
now: () => number;
|
|
89
|
+
log: (message: string) => void;
|
|
90
|
+
}) => {
|
|
91
|
+
const sessions = createSessions(options.now);
|
|
92
|
+
|
|
93
|
+
const tokenIdentity = (presented: string): Identity | null => {
|
|
94
|
+
if (matches(presented, options.token)) {
|
|
95
|
+
return { login: null, name: "Agent token", role: "admin", via: "token" };
|
|
96
|
+
}
|
|
97
|
+
if (options.readToken !== null && matches(presented, options.readToken)) {
|
|
98
|
+
return {
|
|
99
|
+
login: null,
|
|
100
|
+
name: "Read token",
|
|
101
|
+
role: "viewer",
|
|
102
|
+
via: "token",
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const serverIdentity: Identity = {
|
|
109
|
+
login: null,
|
|
110
|
+
name: "Line server",
|
|
111
|
+
role: "deployer",
|
|
112
|
+
via: "server",
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const peerIdentity = async (
|
|
116
|
+
request: Request,
|
|
117
|
+
peer: Peer | null,
|
|
118
|
+
): Promise<Identity | null> => {
|
|
119
|
+
if (
|
|
120
|
+
!peer ||
|
|
121
|
+
!namesAgentDirectly(request) ||
|
|
122
|
+
forwardingHeaders.some((name) => request.headers.has(name))
|
|
123
|
+
) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const kind = peerKind(peer.address);
|
|
127
|
+
if (kind === "loopback") {
|
|
128
|
+
return { login: null, name: "This PC", role: "admin", via: "local" };
|
|
129
|
+
}
|
|
130
|
+
if (kind !== "tailnet") {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const user = await options.whois(peer.address);
|
|
134
|
+
if (!user) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
// A hand-edited file that doesn't parse gives no one a role until it is fixed.
|
|
138
|
+
const config = await readAccessConfig(options.file).catch(
|
|
139
|
+
(error: unknown) => {
|
|
140
|
+
options.log(`Can't read ${options.file}: ${errorMessage(error)}`);
|
|
141
|
+
return noAccess;
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
const role = config.roles[user.login] ?? config.tailnetDefault;
|
|
145
|
+
return (
|
|
146
|
+
role && { login: user.login, name: user.name, role, via: "tailscale" }
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The request's identity, or the refusal to answer it with. A route a browser calls also takes
|
|
152
|
+
* a browser's session or where the request comes from; the CLI's take only a token.
|
|
153
|
+
*/
|
|
154
|
+
const identify = async (
|
|
155
|
+
request: Request,
|
|
156
|
+
peer: Peer | null,
|
|
157
|
+
route: { browser: boolean },
|
|
158
|
+
): Promise<Identity | Response> => {
|
|
159
|
+
const { searchParams, pathname } = new URL(request.url);
|
|
160
|
+
// EventSource can't send headers, so the debugger also takes the token in the query.
|
|
161
|
+
const presented =
|
|
162
|
+
/^Bearer (.+)$/.exec(request.headers.get("authorization") ?? "")?.[1] ??
|
|
163
|
+
(pathname.startsWith(debuggerMount)
|
|
164
|
+
? searchParams.get(tokenQueryParameter)
|
|
165
|
+
: null);
|
|
166
|
+
if (presented !== null) {
|
|
167
|
+
if (
|
|
168
|
+
options.serverToken !== null &&
|
|
169
|
+
matches(presented, options.serverToken)
|
|
170
|
+
) {
|
|
171
|
+
return request.method === "POST" && pathname === controllerRestartPath
|
|
172
|
+
? serverIdentity
|
|
173
|
+
: refuse(403, "The server's token only restarts the controller");
|
|
174
|
+
}
|
|
175
|
+
return tokenIdentity(presented) ?? unauthorized();
|
|
176
|
+
}
|
|
177
|
+
if (!route.browser) {
|
|
178
|
+
return unauthorized();
|
|
179
|
+
}
|
|
180
|
+
const identity =
|
|
181
|
+
sessions.find(request) ?? (await peerIdentity(request, peer));
|
|
182
|
+
if (!identity) {
|
|
183
|
+
return unauthorized();
|
|
184
|
+
}
|
|
185
|
+
return changes(request) && !fromDashboard(request) ? crossSite() : identity;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
/** Signs a browser in and out, which needs no identity: the answer, or null for another route. */
|
|
189
|
+
const handle = async (request: Request): Promise<Response | null> => {
|
|
190
|
+
const { pathname } = new URL(request.url);
|
|
191
|
+
if (
|
|
192
|
+
pathname !== "/api/session" ||
|
|
193
|
+
(request.method !== "POST" && request.method !== "DELETE")
|
|
194
|
+
) {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
if (!fromDashboard(request)) {
|
|
198
|
+
return crossSite();
|
|
199
|
+
}
|
|
200
|
+
if (request.method === "DELETE") {
|
|
201
|
+
return new Response(null, {
|
|
202
|
+
status: 204,
|
|
203
|
+
headers: { "Set-Cookie": sessions.close(request) },
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const body = sessionRequestSchema.safeParse(
|
|
207
|
+
await request.json().catch(() => null),
|
|
208
|
+
);
|
|
209
|
+
if (!body.success) {
|
|
210
|
+
return Response.json({ error: "Send { token }" }, { status: 400 });
|
|
211
|
+
}
|
|
212
|
+
const identity = tokenIdentity(body.data.token);
|
|
213
|
+
if (!identity) {
|
|
214
|
+
return unauthorized();
|
|
215
|
+
}
|
|
216
|
+
return Response.json(identity, {
|
|
217
|
+
headers: { "Set-Cookie": sessions.open(identity) },
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const routes: ApiRoute[] = [
|
|
222
|
+
{
|
|
223
|
+
method: "GET",
|
|
224
|
+
pattern: "/api/session",
|
|
225
|
+
role: "viewer",
|
|
226
|
+
handle: async (_request, { identity }) => Response.json(identity),
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
method: "GET",
|
|
230
|
+
pattern: "/api/access",
|
|
231
|
+
role: "admin",
|
|
232
|
+
handle: async () => Response.json(await readAccessConfig(options.file)),
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
method: "PUT",
|
|
236
|
+
pattern: "/api/access",
|
|
237
|
+
role: "admin",
|
|
238
|
+
handle: async (request, { identity }) => {
|
|
239
|
+
const config = accessConfigSchema.parse(await request.json());
|
|
240
|
+
const previous = await readAccessConfig(options.file);
|
|
241
|
+
writeJsonFile(options.file, config);
|
|
242
|
+
await options.audit.record({
|
|
243
|
+
by: authorOf(identity),
|
|
244
|
+
command: "access",
|
|
245
|
+
values: auditedAccess(config),
|
|
246
|
+
previous: auditedAccess(previous),
|
|
247
|
+
error: null,
|
|
248
|
+
});
|
|
249
|
+
return Response.json(config);
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
];
|
|
253
|
+
return { identify, handle, routes };
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/** Who has which role, as the audit log lists it: each login's role, and the tailnet default. */
|
|
257
|
+
const auditedAccess = (config: AccessConfig) => ({
|
|
258
|
+
...config.roles,
|
|
259
|
+
"(tailnet default)": config.tailnetDefault ?? "none",
|
|
260
|
+
});
|