@mark1russell7/stack-setup 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +155 -2
- package/build/add-on.d.ts +12 -0
- package/build/add-on.d.ts.map +1 -0
- package/build/add-on.js +23 -0
- package/build/add-on.js.map +1 -0
- package/build/index.d.ts +148 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +511 -0
- package/build/index.js.map +1 -0
- package/build/join-DS3ZZ5hb.js +447 -0
- package/build/join-DS3ZZ5hb.js.map +1 -0
- package/build/layer-DWfBSz3H.js +58 -0
- package/build/layer-DWfBSz3H.js.map +1 -0
- package/build/layer-Db50psxJ.d.ts +151 -0
- package/build/layer-Db50psxJ.d.ts.map +1 -0
- package/build/schema.d.ts +21 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +117 -0
- package/build/schema.js.map +1 -0
- package/package.json +75 -3
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import { a as stepPaths, i as stepAddress, o as stepTexts, s as textParts, t as SETUP_SCHEMA } from "./layer-DWfBSz3H.js";
|
|
2
|
+
import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
|
|
3
|
+
import { joinRecords, neededParts, partsById } from "@mark1russell7/system-stack";
|
|
4
|
+
// src/order.ts
|
|
5
|
+
/** This function gives each host of a layer once, in the order of its first profile. */
|
|
6
|
+
function setupHosts(layer) {
|
|
7
|
+
const hosts = [];
|
|
8
|
+
for (const profile of layer.profiles) for (const host of profile.hosts) if (!hosts.includes(host)) hosts.push(host);
|
|
9
|
+
return hosts;
|
|
10
|
+
}
|
|
11
|
+
/** This function gives the profiles of a layer that name a host, in the layer order. */
|
|
12
|
+
function hostProfiles(layer, host) {
|
|
13
|
+
return layer.profiles.filter((profile) => profile.hosts.includes(host));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* This function gives, for each profile of a list, the positions of the profiles of the list that come before it.
|
|
17
|
+
* A profile comes after each other profile whose part is a needed part of its part. Two profiles of one part have no order.
|
|
18
|
+
*
|
|
19
|
+
* @param profiles - The profiles of one host.
|
|
20
|
+
* @param stack - The stack. `neededParts` of the core gives the needs.
|
|
21
|
+
* @returns For each position of the list, the positions that come before it.
|
|
22
|
+
*/
|
|
23
|
+
function profilesBefore(profiles, stack) {
|
|
24
|
+
return profiles.map((profile, at) => {
|
|
25
|
+
const needed = neededParts(stack, profile.part);
|
|
26
|
+
const before = [];
|
|
27
|
+
profiles.forEach((other, position) => {
|
|
28
|
+
if (position !== at && other.part !== profile.part && needed.has(other.part)) before.push(position);
|
|
29
|
+
});
|
|
30
|
+
return before;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* This function gives the profiles of a host in the order of the setup.
|
|
35
|
+
*
|
|
36
|
+
* - A profile comes after each profile of the host whose part its part needs.
|
|
37
|
+
* - Where the needs give no order, the layer order holds. Thus the order is stable.
|
|
38
|
+
* - When the needs of two or more profiles make a cycle, the result has no order and names each cycle.
|
|
39
|
+
*
|
|
40
|
+
* `stronglyConnectedComponents` of `@mark1russell7/collections/graph` finds the cycles.
|
|
41
|
+
*
|
|
42
|
+
* @param layer - The setup layer.
|
|
43
|
+
* @param stack - The stack.
|
|
44
|
+
* @param host - The `id` of the host.
|
|
45
|
+
* @returns The order, or the cycles.
|
|
46
|
+
*/
|
|
47
|
+
function profileOrder(layer, stack, host) {
|
|
48
|
+
const profiles = hostProfiles(layer, host);
|
|
49
|
+
const before = profilesBefore(profiles, stack);
|
|
50
|
+
const positions = profiles.map((_, at) => at);
|
|
51
|
+
const cycles = stronglyConnectedComponents(positions, (at) => before[at] ?? []).filter((component) => component.length > 1).map((component) => [...component].sort((a, b) => a - b).map((at) => profiles[at]?.name ?? String(at)));
|
|
52
|
+
if (cycles.length > 0) return {
|
|
53
|
+
profiles: [],
|
|
54
|
+
cycles
|
|
55
|
+
};
|
|
56
|
+
const placed = /* @__PURE__ */ new Set();
|
|
57
|
+
const order = [];
|
|
58
|
+
while (order.length < profiles.length) {
|
|
59
|
+
const next = positions.find((at) => !placed.has(at) && (before[at] ?? []).every((other) => placed.has(other)));
|
|
60
|
+
if (next === void 0) break;
|
|
61
|
+
placed.add(next);
|
|
62
|
+
const profile = profiles[next];
|
|
63
|
+
if (profile !== void 0) order.push(profile);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
profiles: order,
|
|
67
|
+
cycles: []
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// src/forms.ts
|
|
71
|
+
/** The form of the name of a profile and of the `id` of a step. */
|
|
72
|
+
const NAME_FORM = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
73
|
+
/** The form of the name of a variable. */
|
|
74
|
+
const VARIABLE_FORM = /^[A-Z][A-Z0-9_]*$/;
|
|
75
|
+
/** These names belong to the shell, to the login or to `sudo`. A variable of a layer cannot have them. */
|
|
76
|
+
const SHELL_NAMES = [
|
|
77
|
+
"BASH_ENV",
|
|
78
|
+
"CDPATH",
|
|
79
|
+
"DEBIAN_FRONTEND",
|
|
80
|
+
"ENV",
|
|
81
|
+
"HISTFILE",
|
|
82
|
+
"HOME",
|
|
83
|
+
"IFS",
|
|
84
|
+
"LANG",
|
|
85
|
+
"LANGUAGE",
|
|
86
|
+
"LINENO",
|
|
87
|
+
"LOGNAME",
|
|
88
|
+
"MAIL",
|
|
89
|
+
"MAILPATH",
|
|
90
|
+
"OLDPWD",
|
|
91
|
+
"OPTARG",
|
|
92
|
+
"OPTIND",
|
|
93
|
+
"PATH",
|
|
94
|
+
"PPID",
|
|
95
|
+
"PS1",
|
|
96
|
+
"PS2",
|
|
97
|
+
"PS4",
|
|
98
|
+
"PWD",
|
|
99
|
+
"SHELL",
|
|
100
|
+
"TERM",
|
|
101
|
+
"TMPDIR",
|
|
102
|
+
"TZ",
|
|
103
|
+
"USER"
|
|
104
|
+
];
|
|
105
|
+
/** These prefixes belong to the locale or to `sudo`. A variable of a layer cannot start with them. */
|
|
106
|
+
const SHELL_PREFIXES = ["LC_", "SUDO_"];
|
|
107
|
+
/** The form of the name of a user or of a group. */
|
|
108
|
+
const USER_FORM = /^[a-z_][a-z0-9_-]*\$?$/;
|
|
109
|
+
/** The form of the name of a service of systemd, with no `.service`. */
|
|
110
|
+
const SERVICE_FORM = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
111
|
+
/** The form of the name of an `apt` package. */
|
|
112
|
+
const PACKAGE_FORM = /^[a-z0-9][a-z0-9+.-]+$/;
|
|
113
|
+
/** The characters of a path. */
|
|
114
|
+
const PATH_FORM = /^\/[A-Za-z0-9._@+/-]+$/;
|
|
115
|
+
/** The form of a mode in octal. */
|
|
116
|
+
const MODE_FORM = /^[0-7]{3,4}$/;
|
|
117
|
+
/** The form of a SHA-256. */
|
|
118
|
+
const SHA256_FORM = /^[0-9a-f]{64}$/;
|
|
119
|
+
/** The form of a program, the first argument of a command. It has no `=`, and it does not start with `-`. */
|
|
120
|
+
const PROGRAM_FORM = /^[A-Za-z0-9_./+][A-Za-z0-9_./+-]*$/;
|
|
121
|
+
/** The characters of an address: printable ASCII with no space. */
|
|
122
|
+
const URL_FORM = /^[\x21-\x7e]+$/;
|
|
123
|
+
/** The login shell of a user when a step has none. */
|
|
124
|
+
const DEFAULT_SHELL = "/bin/bash";
|
|
125
|
+
/** The largest size of a swap file in MiB. */
|
|
126
|
+
const SWAP_MIB_LIMIT = 65536;
|
|
127
|
+
/** This function tells if a text is the name of a variable that a layer can have. */
|
|
128
|
+
function isVariableName(name) {
|
|
129
|
+
return VARIABLE_FORM.test(name) && !SHELL_NAMES.includes(name) && !SHELL_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
130
|
+
}
|
|
131
|
+
/** This function tells if a text is the name of a user or of a group. */
|
|
132
|
+
function isUserName(name) {
|
|
133
|
+
return name.length <= 32 && USER_FORM.test(name);
|
|
134
|
+
}
|
|
135
|
+
/** This function tells if a text is the name of a service. The name has no `.service` at its end. */
|
|
136
|
+
function isServiceName(name) {
|
|
137
|
+
return SERVICE_FORM.test(name) && !name.endsWith(".service");
|
|
138
|
+
}
|
|
139
|
+
/** This function tells if a text is an absolute path with no `.` or `..` segment, no `//` and no `/` at its end. */
|
|
140
|
+
function isPath(path) {
|
|
141
|
+
if (!PATH_FORM.test(path) || path.endsWith("/") || path.includes("//")) return false;
|
|
142
|
+
return path.split("/").slice(1).every((segment) => segment !== "." && segment !== "..");
|
|
143
|
+
}
|
|
144
|
+
/** The form of the host of an address, with an optional port. It has no user and no password. */
|
|
145
|
+
const HOST_FORM = /^[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]{1,5})?$/;
|
|
146
|
+
/**
|
|
147
|
+
* This function tells if a text is an HTTPS address: `https://`, a host name with an optional port, and an optional path.
|
|
148
|
+
* An address with a user or a password is not an HTTPS address of a layer, because the script shows each address.
|
|
149
|
+
*/
|
|
150
|
+
function isHttpsUrl(url) {
|
|
151
|
+
if (!URL_FORM.test(url) || !url.startsWith("https://")) return false;
|
|
152
|
+
const rest = url.slice(8);
|
|
153
|
+
const end = rest.search(/[/?#]/);
|
|
154
|
+
return HOST_FORM.test(end === -1 ? rest : rest.slice(0, end));
|
|
155
|
+
}
|
|
156
|
+
/** This function gives a mode in the form of `stat -c %a`: octal digits with no zero at the start, for example `644`. */
|
|
157
|
+
function modeText(mode) {
|
|
158
|
+
return Number.parseInt(mode, 8).toString(8);
|
|
159
|
+
}
|
|
160
|
+
/** This function tells if a mode lets each user of the host read the file. The mode has the form `MODE_FORM`. */
|
|
161
|
+
function isOpenMode(mode) {
|
|
162
|
+
return Number.parseInt(mode, 8) % 8 !== 0;
|
|
163
|
+
}
|
|
164
|
+
/** This function gives the text of a literal: the joined parts, or `undefined` when a part is a variable. */
|
|
165
|
+
function literalText(text) {
|
|
166
|
+
let literal = "";
|
|
167
|
+
for (const part of textParts(text)) {
|
|
168
|
+
if (typeof part !== "string") return void 0;
|
|
169
|
+
literal += part;
|
|
170
|
+
}
|
|
171
|
+
return literal;
|
|
172
|
+
}
|
|
173
|
+
// src/check.ts
|
|
174
|
+
/** These are the codes of the faults of a setup layer. */
|
|
175
|
+
const SETUP_CODES = [
|
|
176
|
+
"setup-name",
|
|
177
|
+
"setup-variable-twice",
|
|
178
|
+
"setup-profile-twice",
|
|
179
|
+
"setup-step-twice",
|
|
180
|
+
"setup-part",
|
|
181
|
+
"setup-host",
|
|
182
|
+
"setup-variable",
|
|
183
|
+
"setup-variable-unused",
|
|
184
|
+
"setup-secret-exposed",
|
|
185
|
+
"setup-text",
|
|
186
|
+
"setup-path",
|
|
187
|
+
"setup-path-twice",
|
|
188
|
+
"setup-user-twice",
|
|
189
|
+
"setup-mode",
|
|
190
|
+
"setup-url",
|
|
191
|
+
"setup-sha256",
|
|
192
|
+
"setup-swap-size",
|
|
193
|
+
"setup-program",
|
|
194
|
+
"setup-empty",
|
|
195
|
+
"setup-cycle"
|
|
196
|
+
];
|
|
197
|
+
/** This function gives a list of names for a message, for example `"a", "b"`. */
|
|
198
|
+
function names(items) {
|
|
199
|
+
return items.map((item) => `"${item}"`).join(", ");
|
|
200
|
+
}
|
|
201
|
+
function faultList() {
|
|
202
|
+
const list = [];
|
|
203
|
+
const add = (level, code, at, message, more = {}) => {
|
|
204
|
+
list.push({
|
|
205
|
+
level,
|
|
206
|
+
code,
|
|
207
|
+
at,
|
|
208
|
+
message,
|
|
209
|
+
...more
|
|
210
|
+
});
|
|
211
|
+
};
|
|
212
|
+
return {
|
|
213
|
+
list,
|
|
214
|
+
add,
|
|
215
|
+
error: (code, at, message, more) => add("error", code, at, message, more)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/** This function examines the first argument of a command: a literal program. */
|
|
219
|
+
function checkProgram(faults, at, label, command) {
|
|
220
|
+
const first = command[0];
|
|
221
|
+
if (first === void 0) {
|
|
222
|
+
faults.error("setup-program", at, `The ${label} command has no argument.`);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const program = literalText(first);
|
|
226
|
+
if (program === void 0 || !PROGRAM_FORM.test(program)) faults.error("setup-program", at, `The first argument of the ${label} command is not the name or the path of a program. It is a literal with no "=", and it does not start with "-".`);
|
|
227
|
+
}
|
|
228
|
+
/** This function examines the fields of one step that are not texts. */
|
|
229
|
+
function checkStep(faults, at, step) {
|
|
230
|
+
const mode = (value) => {
|
|
231
|
+
if (value !== void 0 && !MODE_FORM.test(value)) faults.error("setup-mode", at, `The mode "${value}" is not a mode in octal, for example "644".`);
|
|
232
|
+
};
|
|
233
|
+
const path = (value, label = "path") => {
|
|
234
|
+
if (!isPath(value)) faults.error("setup-path", at, `The ${label} "${value}" is not an absolute path of letters, digits and "._@+-", with no "." or ".." segment.`);
|
|
235
|
+
};
|
|
236
|
+
const user = (value, label) => {
|
|
237
|
+
if (value !== void 0 && !isUserName(value)) faults.error("setup-name", at, `The ${label} "${value}" does not have the form of the name of a user or of a group.`);
|
|
238
|
+
};
|
|
239
|
+
switch (step.kind) {
|
|
240
|
+
case "package":
|
|
241
|
+
if (step.names.length === 0) faults.add("warning", "setup-empty", at, "The step names no package.");
|
|
242
|
+
for (const name of step.names) if (!PACKAGE_FORM.test(name)) faults.error("setup-name", at, `The name "${name}" does not have the form of the name of a package.`);
|
|
243
|
+
return;
|
|
244
|
+
case "user":
|
|
245
|
+
user(step.name, "user");
|
|
246
|
+
for (const group of step.groups ?? []) user(group, "group");
|
|
247
|
+
if (step.shell !== void 0) path(step.shell, "shell");
|
|
248
|
+
for (const key of step.keys ?? []) if (key.trim() === "" || /[\0\r\n]/.test(key)) faults.error("setup-text", at, "A key is empty, or it has a line break or the character NUL. Each key is one line of `authorized_keys`.");
|
|
249
|
+
return;
|
|
250
|
+
case "file":
|
|
251
|
+
path(step.path);
|
|
252
|
+
mode(step.mode);
|
|
253
|
+
user(step.owner, "owner");
|
|
254
|
+
user(step.group, "group");
|
|
255
|
+
return;
|
|
256
|
+
case "download":
|
|
257
|
+
if (!isHttpsUrl(step.url)) faults.error("setup-url", at, "The address is not an HTTPS address with a host name, and with no user and no password.");
|
|
258
|
+
if (!SHA256_FORM.test(step.sha256)) faults.error("setup-sha256", at, "The SHA-256 is not 64 lowercase hexadecimal digits.");
|
|
259
|
+
path(step.path);
|
|
260
|
+
mode(step.mode);
|
|
261
|
+
return;
|
|
262
|
+
case "swap":
|
|
263
|
+
path(step.path);
|
|
264
|
+
if (!Number.isInteger(step.mib) || step.mib < 1 || step.mib > 65536) faults.error("setup-swap-size", at, `The size ${String(step.mib)} MiB is not a whole number from 1 to ${String(SWAP_MIB_LIMIT)}.`);
|
|
265
|
+
return;
|
|
266
|
+
case "service":
|
|
267
|
+
if (!isServiceName(step.name)) faults.error("setup-name", at, `The name "${step.name}" does not have the form of the name of a service, with no ".service".`);
|
|
268
|
+
return;
|
|
269
|
+
case "command":
|
|
270
|
+
checkProgram(faults, at, "run", step.run);
|
|
271
|
+
checkProgram(faults, at, "check", step.check);
|
|
272
|
+
user(step.as, "user");
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* This function tells why a sensitive variable cannot be in a text of a step, or gives `undefined` when it can.
|
|
278
|
+
* A secret can be only in a file that no other user can read.
|
|
279
|
+
*/
|
|
280
|
+
function secretPlace(step) {
|
|
281
|
+
switch (step.kind) {
|
|
282
|
+
case "file": {
|
|
283
|
+
const mode = step.mode ?? "644";
|
|
284
|
+
return MODE_FORM.test(mode) && isOpenMode(mode) ? `the mode ${mode} lets each user read the file` : void 0;
|
|
285
|
+
}
|
|
286
|
+
case "service": return "each user can read the unit of a service";
|
|
287
|
+
case "command": return "each user can read the arguments of a process";
|
|
288
|
+
default: return "the step has no place for a secret";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** This function examines the texts of a step: the character NUL and the variables. */
|
|
292
|
+
function checkTexts(faults, at, step, variables, used) {
|
|
293
|
+
let nul = false;
|
|
294
|
+
for (const text of stepTexts(step)) for (const part of textParts(text)) {
|
|
295
|
+
if (typeof part === "string") {
|
|
296
|
+
nul ||= part.includes("\0");
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
used.add(part.$var);
|
|
300
|
+
const variable = variables.get(part.$var);
|
|
301
|
+
if (variable === void 0) faults.error("setup-variable", at, `The step reads the variable "${part.$var}", and the layer has no variable with that name.`);
|
|
302
|
+
else if (variable.sensitive === true) {
|
|
303
|
+
const place = secretPlace(step);
|
|
304
|
+
if (place !== void 0) faults.error("setup-secret-exposed", at, `The step has the sensitive variable "${part.$var}", and ${place}.`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (nul) faults.error("setup-text", at, "A text of the step has the character NUL. An argument of the shell cannot hold it.");
|
|
308
|
+
}
|
|
309
|
+
/** This function examines the steps of each host together: the paths, the users and the order of the profiles. */
|
|
310
|
+
function checkHosts(faults, layer, stack) {
|
|
311
|
+
for (const host of setupHosts(layer)) {
|
|
312
|
+
const writers = /* @__PURE__ */ new Map();
|
|
313
|
+
const users = /* @__PURE__ */ new Map();
|
|
314
|
+
let swap;
|
|
315
|
+
let fstab;
|
|
316
|
+
for (const profile of layer.profiles) {
|
|
317
|
+
if (!profile.hosts.includes(host)) continue;
|
|
318
|
+
for (const step of profile.steps) {
|
|
319
|
+
const at = stepAddress(profile, step);
|
|
320
|
+
for (const path of stepPaths(step)) {
|
|
321
|
+
const first = writers.get(path);
|
|
322
|
+
if (first === void 0) writers.set(path, at);
|
|
323
|
+
else faults.error("setup-path-twice", at, `The steps "${first}" and "${at}" both write "${path}" on the host "${host}".`, {
|
|
324
|
+
other: first,
|
|
325
|
+
host
|
|
326
|
+
});
|
|
327
|
+
if (path === "/etc/fstab") fstab ??= at;
|
|
328
|
+
}
|
|
329
|
+
if (step.kind === "swap") swap ??= at;
|
|
330
|
+
if (step.kind === "user") {
|
|
331
|
+
const first = users.get(step.name);
|
|
332
|
+
if (first === void 0) users.set(step.name, at);
|
|
333
|
+
else faults.error("setup-user-twice", at, `The steps "${first}" and "${at}" both set up the user "${step.name}" on the host "${host}".`, {
|
|
334
|
+
other: first,
|
|
335
|
+
host
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (swap !== void 0 && fstab !== void 0) faults.error("setup-path-twice", fstab, `The step writes "/etc/fstab", and the swap step "${swap}" adds a line to it on the host "${host}".`, {
|
|
341
|
+
other: swap,
|
|
342
|
+
host
|
|
343
|
+
});
|
|
344
|
+
for (const cycle of profileOrder(layer, stack, host).cycles) faults.error("setup-cycle", cycle[0] ?? host, `On the host "${host}", the needs of the parts of the profiles ${names(cycle)} make a cycle.`, { host });
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/** This function examines a profile and its steps. */
|
|
348
|
+
function checkProfile(faults, profile, parts, variables, used) {
|
|
349
|
+
if (!NAME_FORM.test(profile.name)) faults.error("setup-name", profile.name, `The name of the profile "${profile.name}" does not have the form of a name: letters, digits, "_" and "-".`);
|
|
350
|
+
if (!parts.has(profile.part)) faults.error("setup-part", profile.name, `The part "${profile.part}" of the profile is not a part of the stack.`);
|
|
351
|
+
if (profile.hosts.length === 0) faults.error("setup-host", profile.name, "The profile names no host.");
|
|
352
|
+
for (const host of profile.hosts) if (!parts.has(host)) faults.error("setup-host", profile.name, `The host "${host}" of the profile is not a part of the stack.`, { host });
|
|
353
|
+
if (profile.steps.length === 0) faults.add("warning", "setup-empty", profile.name, "The profile has no step.");
|
|
354
|
+
const ids = /* @__PURE__ */ new Set();
|
|
355
|
+
for (const step of profile.steps) {
|
|
356
|
+
const at = stepAddress(profile, step);
|
|
357
|
+
if (!NAME_FORM.test(step.id)) faults.error("setup-name", at, `The id of the step "${step.id}" does not have the form of a name: letters, digits, "_" and "-".`);
|
|
358
|
+
if (ids.has(step.id)) faults.error("setup-step-twice", at, `The profile "${profile.name}" has two steps with the id "${step.id}".`);
|
|
359
|
+
ids.add(step.id);
|
|
360
|
+
checkStep(faults, at, step);
|
|
361
|
+
checkTexts(faults, at, step, variables, used);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* This function gives each fault of a setup layer for a stack. A list with no error tells that the gate passes.
|
|
366
|
+
*
|
|
367
|
+
* The errors:
|
|
368
|
+
* - `setup-name`: the name of a profile, a variable, a user, a group, a service or a package does not have its form. Or the `id` of a step does not have its form.
|
|
369
|
+
* - `setup-variable-twice`, `setup-profile-twice`, `setup-step-twice`: two records have one name.
|
|
370
|
+
* - `setup-part`: the part of a profile is not a part of the stack.
|
|
371
|
+
* - `setup-host`: a profile names no host, or a host that is not a part of the stack.
|
|
372
|
+
* - `setup-variable`: a text reads a variable that the layer does not have.
|
|
373
|
+
* - `setup-secret-exposed`: a sensitive variable is in a unit, in a command, or in a file that each user can read.
|
|
374
|
+
* - `setup-text`: a text has the character NUL, or a key is not one line.
|
|
375
|
+
* - `setup-path`: a path is not an absolute path of the safe characters.
|
|
376
|
+
* - `setup-path-twice`: two steps of one host write one path.
|
|
377
|
+
* - `setup-user-twice`: two steps of one host set up one user.
|
|
378
|
+
* - `setup-mode`, `setup-url`, `setup-sha256`, `setup-swap-size`: a mode, an address, a SHA-256 or a size does not have its form.
|
|
379
|
+
* - `setup-program`: a command is empty, or its first argument is not a literal program.
|
|
380
|
+
* - `setup-cycle`: the needs of the parts of the profiles of one host make a cycle.
|
|
381
|
+
*
|
|
382
|
+
* The warnings:
|
|
383
|
+
* - `setup-variable-unused`: no step reads a variable.
|
|
384
|
+
* - `setup-empty`: a profile has no step, or a package step names no package.
|
|
385
|
+
*
|
|
386
|
+
* @param layer - The setup layer.
|
|
387
|
+
* @param stack - The stack. Its parts and their needs give the hosts and the order.
|
|
388
|
+
* @returns The faults.
|
|
389
|
+
*/
|
|
390
|
+
function checkSetup(layer, stack) {
|
|
391
|
+
const faults = faultList();
|
|
392
|
+
const parts = partsById(stack);
|
|
393
|
+
const variables = /* @__PURE__ */ new Map();
|
|
394
|
+
for (const variable of layer.variables ?? []) {
|
|
395
|
+
const at = `var.${variable.name}`;
|
|
396
|
+
if (!isVariableName(variable.name)) faults.error("setup-name", at, `The name "${variable.name}" does not have the form of a variable: capital letters, digits and "_", with a letter first, and not a name of the shell.`);
|
|
397
|
+
if (variables.has(variable.name)) faults.error("setup-variable-twice", at, `The layer has two variables with the name "${variable.name}".`);
|
|
398
|
+
else variables.set(variable.name, variable);
|
|
399
|
+
}
|
|
400
|
+
const used = /* @__PURE__ */ new Set();
|
|
401
|
+
const profiles = /* @__PURE__ */ new Set();
|
|
402
|
+
for (const profile of layer.profiles) {
|
|
403
|
+
if (profiles.has(profile.name)) faults.error("setup-profile-twice", profile.name, `The layer has two profiles with the name "${profile.name}".`);
|
|
404
|
+
profiles.add(profile.name);
|
|
405
|
+
checkProfile(faults, profile, parts, variables, used);
|
|
406
|
+
}
|
|
407
|
+
for (const variable of variables.values()) if (!used.has(variable.name)) faults.add("warning", "setup-variable-unused", `var.${variable.name}`, `No step reads the variable "${variable.name}".`);
|
|
408
|
+
checkHosts(faults, layer, stack);
|
|
409
|
+
return faults.list;
|
|
410
|
+
}
|
|
411
|
+
/** This function tells if a list of faults has an error. */
|
|
412
|
+
function hasSetupError(faults) {
|
|
413
|
+
return faults.some((fault) => fault.level === "error");
|
|
414
|
+
}
|
|
415
|
+
// src/join.ts
|
|
416
|
+
/**
|
|
417
|
+
* This function joins the setup layers of the fragments of a stack into one layer.
|
|
418
|
+
*
|
|
419
|
+
* - The variables and the profiles are in the order of the fragments.
|
|
420
|
+
* - A variable name or a profile name in two or more fragments is a fault, also when the records are equal.
|
|
421
|
+
*
|
|
422
|
+
* A profile of one fragment can read a variable of a different fragment. The joined layer has the field `variables` when a fragment has it.
|
|
423
|
+
* The function is pure, and it changes no layer. The gate `checkSetup` examines the joined layer afterward.
|
|
424
|
+
*
|
|
425
|
+
* @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.
|
|
426
|
+
* @returns The joined layer, or the faults of the join.
|
|
427
|
+
*/
|
|
428
|
+
function joinSetup(pieces) {
|
|
429
|
+
const variables = joinRecords(pieces, (layer) => layer.variables, (variable) => variable.name, "variable");
|
|
430
|
+
const profiles = joinRecords(pieces, (layer) => layer.profiles, (profile) => profile.name, "profile");
|
|
431
|
+
const errors = [...variables.errors, ...profiles.errors];
|
|
432
|
+
if (errors.length > 0) return {
|
|
433
|
+
ok: false,
|
|
434
|
+
errors
|
|
435
|
+
};
|
|
436
|
+
return {
|
|
437
|
+
ok: true,
|
|
438
|
+
layer: {
|
|
439
|
+
schema: SETUP_SCHEMA,
|
|
440
|
+
...pieces.some((piece) => piece.layer.variables !== void 0) ? { variables: variables.items } : {},
|
|
441
|
+
profiles: profiles.items
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
export { DEFAULT_SHELL as a, profileOrder as c, hasSetupError as i, profilesBefore as l, SETUP_CODES as n, modeText as o, checkSetup as r, hostProfiles as s, joinSetup as t, setupHosts as u };
|
|
446
|
+
|
|
447
|
+
//# sourceMappingURL=join-DS3ZZ5hb.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"join-DS3ZZ5hb.js","names":[],"sources":["../src/order.ts","../src/forms.ts","../src/check.ts","../src/join.ts"],"sourcesContent":["import { stronglyConnectedComponents } from \"@mark1russell7/collections/graph\";\nimport { neededParts, type Stack } from \"@mark1russell7/system-stack\";\nimport type { Profile, SetupLayer } from \"./layer.ts\";\n\n// The order of the profiles of a host. The needs of the stack give it: a profile comes after each profile\n// of the same host whose part its part needs, directly or through other parts. Where the needs give no order, the layer order holds.\n\n/** The order of the profiles of one host. */\nexport interface ProfileOrder {\n /** The profiles of the host in the order of the setup. It is empty when the needs have a cycle. */\n readonly profiles: readonly Profile[];\n /** The names of the profiles in each cycle of the needs. Each cycle has two or more profiles. */\n readonly cycles: readonly (readonly string[])[];\n}\n\n/** This function gives each host of a layer once, in the order of its first profile. */\nexport function setupHosts(layer: SetupLayer): string[] {\n const hosts: string[] = [];\n for (const profile of layer.profiles) for (const host of profile.hosts) if (!hosts.includes(host)) hosts.push(host);\n return hosts;\n}\n\n/** This function gives the profiles of a layer that name a host, in the layer order. */\nexport function hostProfiles(layer: SetupLayer, host: string): Profile[] {\n return layer.profiles.filter((profile) => profile.hosts.includes(host));\n}\n\n/**\n * This function gives, for each profile of a list, the positions of the profiles of the list that come before it.\n * A profile comes after each other profile whose part is a needed part of its part. Two profiles of one part have no order.\n *\n * @param profiles - The profiles of one host.\n * @param stack - The stack. `neededParts` of the core gives the needs.\n * @returns For each position of the list, the positions that come before it.\n */\nexport function profilesBefore(profiles: readonly Profile[], stack: Stack): number[][] {\n return profiles.map((profile, at) => {\n const needed = neededParts(stack, profile.part);\n const before: number[] = [];\n profiles.forEach((other, position) => {\n if (position !== at && other.part !== profile.part && needed.has(other.part)) before.push(position);\n });\n return before;\n });\n}\n\n/**\n * This function gives the profiles of a host in the order of the setup.\n *\n * - A profile comes after each profile of the host whose part its part needs.\n * - Where the needs give no order, the layer order holds. Thus the order is stable.\n * - When the needs of two or more profiles make a cycle, the result has no order and names each cycle.\n *\n * `stronglyConnectedComponents` of `@mark1russell7/collections/graph` finds the cycles.\n *\n * @param layer - The setup layer.\n * @param stack - The stack.\n * @param host - The `id` of the host.\n * @returns The order, or the cycles.\n */\nexport function profileOrder(layer: SetupLayer, stack: Stack, host: string): ProfileOrder {\n const profiles = hostProfiles(layer, host);\n const before = profilesBefore(profiles, stack);\n const positions = profiles.map((_, at) => at);\n const cycles = stronglyConnectedComponents(positions, (at) => before[at] ?? [])\n .filter((component) => component.length > 1)\n .map((component) => [...component].sort((a, b) => a - b).map((at) => profiles[at]?.name ?? String(at)));\n if (cycles.length > 0) return { profiles: [], cycles };\n const placed = new Set<number>();\n const order: Profile[] = [];\n while (order.length < profiles.length) {\n const next = positions.find((at) => !placed.has(at) && (before[at] ?? []).every((other) => placed.has(other)));\n // With no cycle, a free profile is always there. The guard stops a loop that has none.\n if (next === undefined) break;\n placed.add(next);\n const profile = profiles[next];\n if (profile !== undefined) order.push(profile);\n }\n return { profiles: order, cycles: [] };\n}\n","import { textParts, type SetupText } from \"./layer.ts\";\n\n// The forms of the values of a setup layer. The gate and the script use them. The root entry does not export this module.\n\n/** The form of the name of a profile and of the `id` of a step. */\nexport const NAME_FORM: RegExp = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;\n\n/** The form of the name of a variable. */\nexport const VARIABLE_FORM: RegExp = /^[A-Z][A-Z0-9_]*$/;\n\n/** These names belong to the shell, to the login or to `sudo`. A variable of a layer cannot have them. */\nexport const SHELL_NAMES: readonly string[] = [\n \"BASH_ENV\",\n \"CDPATH\",\n \"DEBIAN_FRONTEND\",\n \"ENV\",\n \"HISTFILE\",\n \"HOME\",\n \"IFS\",\n \"LANG\",\n \"LANGUAGE\",\n \"LINENO\",\n \"LOGNAME\",\n \"MAIL\",\n \"MAILPATH\",\n \"OLDPWD\",\n \"OPTARG\",\n \"OPTIND\",\n \"PATH\",\n \"PPID\",\n \"PS1\",\n \"PS2\",\n \"PS4\",\n \"PWD\",\n \"SHELL\",\n \"TERM\",\n \"TMPDIR\",\n \"TZ\",\n \"USER\",\n];\n\n/** These prefixes belong to the locale or to `sudo`. A variable of a layer cannot start with them. */\nexport const SHELL_PREFIXES: readonly string[] = [\"LC_\", \"SUDO_\"];\n\n/** The form of the name of a user or of a group. */\nexport const USER_FORM: RegExp = /^[a-z_][a-z0-9_-]*\\$?$/;\n\n/** The largest length of the name of a user or of a group. */\nexport const USER_LENGTH = 32;\n\n/** The form of the name of a service of systemd, with no `.service`. */\nexport const SERVICE_FORM: RegExp = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;\n\n/** The form of the name of an `apt` package. */\nexport const PACKAGE_FORM: RegExp = /^[a-z0-9][a-z0-9+.-]+$/;\n\n/** The characters of a path. */\nexport const PATH_FORM: RegExp = /^\\/[A-Za-z0-9._@+/-]+$/;\n\n/** The form of a mode in octal. */\nexport const MODE_FORM: RegExp = /^[0-7]{3,4}$/;\n\n/** The form of a SHA-256. */\nexport const SHA256_FORM: RegExp = /^[0-9a-f]{64}$/;\n\n/** The form of a program, the first argument of a command. It has no `=`, and it does not start with `-`. */\nexport const PROGRAM_FORM: RegExp = /^[A-Za-z0-9_./+][A-Za-z0-9_./+-]*$/;\n\n/** The characters of an address: printable ASCII with no space. */\nexport const URL_FORM: RegExp = /^[\\x21-\\x7e]+$/;\n\n/** The mode of a file and of a download when a step has none. */\nexport const DEFAULT_MODE = \"644\";\n\n/** The login shell of a user when a step has none. */\nexport const DEFAULT_SHELL = \"/bin/bash\";\n\n/** The largest size of a swap file in MiB. */\nexport const SWAP_MIB_LIMIT = 65536;\n\n/** This function tells if a text is the name of a variable that a layer can have. */\nexport function isVariableName(name: string): boolean {\n return VARIABLE_FORM.test(name) && !SHELL_NAMES.includes(name) && !SHELL_PREFIXES.some((prefix) => name.startsWith(prefix));\n}\n\n/** This function tells if a text is the name of a user or of a group. */\nexport function isUserName(name: string): boolean {\n return name.length <= USER_LENGTH && USER_FORM.test(name);\n}\n\n/** This function tells if a text is the name of a service. The name has no `.service` at its end. */\nexport function isServiceName(name: string): boolean {\n return SERVICE_FORM.test(name) && !name.endsWith(\".service\");\n}\n\n/** This function tells if a text is an absolute path with no `.` or `..` segment, no `//` and no `/` at its end. */\nexport function isPath(path: string): boolean {\n if (!PATH_FORM.test(path) || path.endsWith(\"/\") || path.includes(\"//\")) return false;\n return path\n .split(\"/\")\n .slice(1)\n .every((segment) => segment !== \".\" && segment !== \"..\");\n}\n\n/** The form of the host of an address, with an optional port. It has no user and no password. */\nexport const HOST_FORM: RegExp = /^[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]{1,5})?$/;\n\n/**\n * This function tells if a text is an HTTPS address: `https://`, a host name with an optional port, and an optional path.\n * An address with a user or a password is not an HTTPS address of a layer, because the script shows each address.\n */\nexport function isHttpsUrl(url: string): boolean {\n if (!URL_FORM.test(url) || !url.startsWith(\"https://\")) return false;\n const rest = url.slice(\"https://\".length);\n const end = rest.search(/[/?#]/);\n return HOST_FORM.test(end === -1 ? rest : rest.slice(0, end));\n}\n\n/** This function gives a mode in the form of `stat -c %a`: octal digits with no zero at the start, for example `644`. */\nexport function modeText(mode: string): string {\n return Number.parseInt(mode, 8).toString(8);\n}\n\n/** This function tells if a mode lets each user of the host read the file. The mode has the form `MODE_FORM`. */\nexport function isOpenMode(mode: string): boolean {\n // The last octal digit is the access of the other users.\n return Number.parseInt(mode, 8) % 8 !== 0;\n}\n\n/** This function gives the text of a literal: the joined parts, or `undefined` when a part is a variable. */\nexport function literalText(text: SetupText): string | undefined {\n let literal = \"\";\n for (const part of textParts(text)) {\n if (typeof part !== \"string\") return undefined;\n literal += part;\n }\n return literal;\n}\n","import { partsById, type Fault, type FaultLevel, type Stack } from \"@mark1russell7/system-stack\";\nimport {\n DEFAULT_MODE,\n isHttpsUrl,\n isOpenMode,\n isPath,\n isServiceName,\n isUserName,\n isVariableName,\n literalText,\n MODE_FORM,\n NAME_FORM,\n PACKAGE_FORM,\n PROGRAM_FORM,\n SHA256_FORM,\n SWAP_MIB_LIMIT,\n} from \"./forms.ts\";\nimport { stepAddress, stepPaths, stepTexts, textParts, type Profile, type SetupLayer, type SetupText, type Step, type Variable } from \"./layer.ts\";\nimport { profileOrder, setupHosts } from \"./order.ts\";\n\n// The gate of the setup layer. A gate with no error tells that the script of each host is safe to write:\n// each record has its form, each variable is known, no secret is in a place that each user can read, and the needs have no cycle.\n\n/** These are the codes of the faults of a setup layer. */\nexport const SETUP_CODES: readonly [\n \"setup-name\",\n \"setup-variable-twice\",\n \"setup-profile-twice\",\n \"setup-step-twice\",\n \"setup-part\",\n \"setup-host\",\n \"setup-variable\",\n \"setup-variable-unused\",\n \"setup-secret-exposed\",\n \"setup-text\",\n \"setup-path\",\n \"setup-path-twice\",\n \"setup-user-twice\",\n \"setup-mode\",\n \"setup-url\",\n \"setup-sha256\",\n \"setup-swap-size\",\n \"setup-program\",\n \"setup-empty\",\n \"setup-cycle\",\n] = [\n \"setup-name\",\n \"setup-variable-twice\",\n \"setup-profile-twice\",\n \"setup-step-twice\",\n \"setup-part\",\n \"setup-host\",\n \"setup-variable\",\n \"setup-variable-unused\",\n \"setup-secret-exposed\",\n \"setup-text\",\n \"setup-path\",\n \"setup-path-twice\",\n \"setup-user-twice\",\n \"setup-mode\",\n \"setup-url\",\n \"setup-sha256\",\n \"setup-swap-size\",\n \"setup-program\",\n \"setup-empty\",\n \"setup-cycle\",\n];\n\n/** A code of a fault of a setup layer. */\nexport type SetupCode = (typeof SETUP_CODES)[number];\n\n/**\n * A fault of a setup layer. Its field `at` is the place of the fault.\n *\n * - For a step, `at` is the address of the step: the name of its profile, a point and its `id`.\n * - For a profile, `at` is the name of the profile.\n * - For a variable, `at` is `var.` and its name.\n * - For a host with no profile, `at` is the `id` of the host.\n */\nexport interface SetupFault extends Fault {\n readonly code: SetupCode;\n /** The `id` of the host, for a fault of the steps of one host. */\n readonly host?: string | undefined;\n}\n\n/** This function gives a list of names for a message, for example `\"a\", \"b\"`. */\nfunction names(items: readonly string[]): string {\n return items.map((item) => `\"${item}\"`).join(\", \");\n}\n\n/** The writer of the faults of one gate. */\ninterface Faults {\n readonly list: SetupFault[];\n add(level: FaultLevel, code: SetupCode, at: string, message: string, more?: { readonly other?: string; readonly host?: string }): void;\n error(code: SetupCode, at: string, message: string, more?: { readonly other?: string; readonly host?: string }): void;\n}\n\nfunction faultList(): Faults {\n const list: SetupFault[] = [];\n const add: Faults[\"add\"] = (level, code, at, message, more = {}) => {\n list.push({ level, code, at, message, ...more });\n };\n return { list, add, error: (code, at, message, more) => add(\"error\", code, at, message, more) };\n}\n\n/** This function examines the first argument of a command: a literal program. */\nfunction checkProgram(faults: Faults, at: string, label: string, command: readonly SetupText[]): void {\n const first = command[0];\n if (first === undefined) {\n faults.error(\"setup-program\", at, `The ${label} command has no argument.`);\n return;\n }\n const program = literalText(first);\n if (program === undefined || !PROGRAM_FORM.test(program)) {\n faults.error(\"setup-program\", at, `The first argument of the ${label} command is not the name or the path of a program. It is a literal with no \"=\", and it does not start with \"-\".`);\n }\n}\n\n/** This function examines the fields of one step that are not texts. */\nfunction checkStep(faults: Faults, at: string, step: Step): void {\n const mode = (value: string | undefined): void => {\n if (value !== undefined && !MODE_FORM.test(value)) faults.error(\"setup-mode\", at, `The mode \"${value}\" is not a mode in octal, for example \"644\".`);\n };\n const path = (value: string, label = \"path\"): void => {\n if (!isPath(value)) faults.error(\"setup-path\", at, `The ${label} \"${value}\" is not an absolute path of letters, digits and \"._@+-\", with no \".\" or \"..\" segment.`);\n };\n const user = (value: string | undefined, label: string): void => {\n if (value !== undefined && !isUserName(value)) faults.error(\"setup-name\", at, `The ${label} \"${value}\" does not have the form of the name of a user or of a group.`);\n };\n switch (step.kind) {\n case \"package\":\n if (step.names.length === 0) faults.add(\"warning\", \"setup-empty\", at, \"The step names no package.\");\n for (const name of step.names) if (!PACKAGE_FORM.test(name)) faults.error(\"setup-name\", at, `The name \"${name}\" does not have the form of the name of a package.`);\n return;\n case \"user\":\n user(step.name, \"user\");\n for (const group of step.groups ?? []) user(group, \"group\");\n if (step.shell !== undefined) path(step.shell, \"shell\");\n for (const key of step.keys ?? []) {\n if (key.trim() === \"\" || /[\\0\\r\\n]/.test(key)) faults.error(\"setup-text\", at, \"A key is empty, or it has a line break or the character NUL. Each key is one line of `authorized_keys`.\");\n }\n return;\n case \"file\":\n path(step.path);\n mode(step.mode);\n user(step.owner, \"owner\");\n user(step.group, \"group\");\n return;\n case \"download\":\n // The message does not repeat the address: an address with a password would put the password in a log.\n if (!isHttpsUrl(step.url)) faults.error(\"setup-url\", at, \"The address is not an HTTPS address with a host name, and with no user and no password.\");\n if (!SHA256_FORM.test(step.sha256)) faults.error(\"setup-sha256\", at, \"The SHA-256 is not 64 lowercase hexadecimal digits.\");\n path(step.path);\n mode(step.mode);\n return;\n case \"swap\":\n path(step.path);\n if (!Number.isInteger(step.mib) || step.mib < 1 || step.mib > SWAP_MIB_LIMIT) faults.error(\"setup-swap-size\", at, `The size ${String(step.mib)} MiB is not a whole number from 1 to ${String(SWAP_MIB_LIMIT)}.`);\n return;\n case \"service\":\n if (!isServiceName(step.name)) faults.error(\"setup-name\", at, `The name \"${step.name}\" does not have the form of the name of a service, with no \".service\".`);\n return;\n case \"command\":\n checkProgram(faults, at, \"run\", step.run);\n checkProgram(faults, at, \"check\", step.check);\n user(step.as, \"user\");\n return;\n }\n}\n\n/**\n * This function tells why a sensitive variable cannot be in a text of a step, or gives `undefined` when it can.\n * A secret can be only in a file that no other user can read.\n */\nfunction secretPlace(step: Step): string | undefined {\n switch (step.kind) {\n case \"file\": {\n const mode = step.mode ?? DEFAULT_MODE;\n return MODE_FORM.test(mode) && isOpenMode(mode) ? `the mode ${mode} lets each user read the file` : undefined;\n }\n case \"service\":\n return \"each user can read the unit of a service\";\n case \"command\":\n return \"each user can read the arguments of a process\";\n default:\n return \"the step has no place for a secret\";\n }\n}\n\n/** This function examines the texts of a step: the character NUL and the variables. */\nfunction checkTexts(faults: Faults, at: string, step: Step, variables: ReadonlyMap<string, Variable>, used: Set<string>): void {\n let nul = false;\n for (const text of stepTexts(step)) {\n for (const part of textParts(text)) {\n if (typeof part === \"string\") {\n nul ||= part.includes(\"\\0\");\n continue;\n }\n used.add(part.$var);\n const variable = variables.get(part.$var);\n if (variable === undefined) {\n faults.error(\"setup-variable\", at, `The step reads the variable \"${part.$var}\", and the layer has no variable with that name.`);\n } else if (variable.sensitive === true) {\n const place = secretPlace(step);\n if (place !== undefined) faults.error(\"setup-secret-exposed\", at, `The step has the sensitive variable \"${part.$var}\", and ${place}.`);\n }\n }\n }\n if (nul) faults.error(\"setup-text\", at, \"A text of the step has the character NUL. An argument of the shell cannot hold it.\");\n}\n\n/** This function examines the steps of each host together: the paths, the users and the order of the profiles. */\nfunction checkHosts(faults: Faults, layer: SetupLayer, stack: Stack): void {\n for (const host of setupHosts(layer)) {\n const writers = new Map<string, string>();\n const users = new Map<string, string>();\n let swap: string | undefined;\n let fstab: string | undefined;\n for (const profile of layer.profiles) {\n if (!profile.hosts.includes(host)) continue;\n for (const step of profile.steps) {\n const at = stepAddress(profile, step);\n for (const path of stepPaths(step)) {\n const first = writers.get(path);\n if (first === undefined) writers.set(path, at);\n else faults.error(\"setup-path-twice\", at, `The steps \"${first}\" and \"${at}\" both write \"${path}\" on the host \"${host}\".`, { other: first, host });\n if (path === \"/etc/fstab\") fstab ??= at;\n }\n if (step.kind === \"swap\") swap ??= at;\n if (step.kind === \"user\") {\n const first = users.get(step.name);\n if (first === undefined) users.set(step.name, at);\n else faults.error(\"setup-user-twice\", at, `The steps \"${first}\" and \"${at}\" both set up the user \"${step.name}\" on the host \"${host}\".`, { other: first, host });\n }\n }\n }\n if (swap !== undefined && fstab !== undefined) {\n faults.error(\"setup-path-twice\", fstab, `The step writes \"/etc/fstab\", and the swap step \"${swap}\" adds a line to it on the host \"${host}\".`, { other: swap, host });\n }\n for (const cycle of profileOrder(layer, stack, host).cycles) {\n faults.error(\"setup-cycle\", cycle[0] ?? host, `On the host \"${host}\", the needs of the parts of the profiles ${names(cycle)} make a cycle.`, { host });\n }\n }\n}\n\n/** This function examines a profile and its steps. */\nfunction checkProfile(faults: Faults, profile: Profile, parts: ReadonlyMap<string, unknown>, variables: ReadonlyMap<string, Variable>, used: Set<string>): void {\n if (!NAME_FORM.test(profile.name)) faults.error(\"setup-name\", profile.name, `The name of the profile \"${profile.name}\" does not have the form of a name: letters, digits, \"_\" and \"-\".`);\n if (!parts.has(profile.part)) faults.error(\"setup-part\", profile.name, `The part \"${profile.part}\" of the profile is not a part of the stack.`);\n if (profile.hosts.length === 0) faults.error(\"setup-host\", profile.name, \"The profile names no host.\");\n for (const host of profile.hosts) if (!parts.has(host)) faults.error(\"setup-host\", profile.name, `The host \"${host}\" of the profile is not a part of the stack.`, { host });\n if (profile.steps.length === 0) faults.add(\"warning\", \"setup-empty\", profile.name, \"The profile has no step.\");\n const ids = new Set<string>();\n for (const step of profile.steps) {\n const at = stepAddress(profile, step);\n if (!NAME_FORM.test(step.id)) faults.error(\"setup-name\", at, `The id of the step \"${step.id}\" does not have the form of a name: letters, digits, \"_\" and \"-\".`);\n if (ids.has(step.id)) faults.error(\"setup-step-twice\", at, `The profile \"${profile.name}\" has two steps with the id \"${step.id}\".`);\n ids.add(step.id);\n checkStep(faults, at, step);\n checkTexts(faults, at, step, variables, used);\n }\n}\n\n/**\n * This function gives each fault of a setup layer for a stack. A list with no error tells that the gate passes.\n *\n * The errors:\n * - `setup-name`: the name of a profile, a variable, a user, a group, a service or a package does not have its form. Or the `id` of a step does not have its form.\n * - `setup-variable-twice`, `setup-profile-twice`, `setup-step-twice`: two records have one name.\n * - `setup-part`: the part of a profile is not a part of the stack.\n * - `setup-host`: a profile names no host, or a host that is not a part of the stack.\n * - `setup-variable`: a text reads a variable that the layer does not have.\n * - `setup-secret-exposed`: a sensitive variable is in a unit, in a command, or in a file that each user can read.\n * - `setup-text`: a text has the character NUL, or a key is not one line.\n * - `setup-path`: a path is not an absolute path of the safe characters.\n * - `setup-path-twice`: two steps of one host write one path.\n * - `setup-user-twice`: two steps of one host set up one user.\n * - `setup-mode`, `setup-url`, `setup-sha256`, `setup-swap-size`: a mode, an address, a SHA-256 or a size does not have its form.\n * - `setup-program`: a command is empty, or its first argument is not a literal program.\n * - `setup-cycle`: the needs of the parts of the profiles of one host make a cycle.\n *\n * The warnings:\n * - `setup-variable-unused`: no step reads a variable.\n * - `setup-empty`: a profile has no step, or a package step names no package.\n *\n * @param layer - The setup layer.\n * @param stack - The stack. Its parts and their needs give the hosts and the order.\n * @returns The faults.\n */\nexport function checkSetup(layer: SetupLayer, stack: Stack): SetupFault[] {\n const faults = faultList();\n const parts = partsById(stack);\n const variables = new Map<string, Variable>();\n for (const variable of layer.variables ?? []) {\n const at = `var.${variable.name}`;\n if (!isVariableName(variable.name)) faults.error(\"setup-name\", at, `The name \"${variable.name}\" does not have the form of a variable: capital letters, digits and \"_\", with a letter first, and not a name of the shell.`);\n if (variables.has(variable.name)) faults.error(\"setup-variable-twice\", at, `The layer has two variables with the name \"${variable.name}\".`);\n else variables.set(variable.name, variable);\n }\n const used = new Set<string>();\n const profiles = new Set<string>();\n for (const profile of layer.profiles) {\n if (profiles.has(profile.name)) faults.error(\"setup-profile-twice\", profile.name, `The layer has two profiles with the name \"${profile.name}\".`);\n profiles.add(profile.name);\n checkProfile(faults, profile, parts, variables, used);\n }\n for (const variable of variables.values()) {\n if (!used.has(variable.name)) faults.add(\"warning\", \"setup-variable-unused\", `var.${variable.name}`, `No step reads the variable \"${variable.name}\".`);\n }\n checkHosts(faults, layer, stack);\n return faults.list;\n}\n\n/** This function tells if a list of faults has an error. */\nexport function hasSetupError(faults: readonly SetupFault[]): boolean {\n return faults.some((fault) => fault.level === \"error\");\n}\n","import { joinRecords, type LayerParse, type LayerPiece } from \"@mark1russell7/system-stack\";\nimport { SETUP_SCHEMA, type SetupLayer } from \"./layer.ts\";\n\n// The join of the setup layers of the fragments of a stack, for example one file of profiles for each owner.\n\n/**\n * This function joins the setup layers of the fragments of a stack into one layer.\n *\n * - The variables and the profiles are in the order of the fragments.\n * - A variable name or a profile name in two or more fragments is a fault, also when the records are equal.\n *\n * A profile of one fragment can read a variable of a different fragment. The joined layer has the field `variables` when a fragment has it.\n * The function is pure, and it changes no layer. The gate `checkSetup` examines the joined layer afterward.\n *\n * @param pieces - The layers, with the name of the fragment of each one, in the order of the fragments.\n * @returns The joined layer, or the faults of the join.\n */\nexport function joinSetup(pieces: readonly LayerPiece<SetupLayer>[]): LayerParse<SetupLayer> {\n const variables = joinRecords(\n pieces,\n (layer) => layer.variables,\n (variable) => variable.name,\n \"variable\",\n );\n const profiles = joinRecords(\n pieces,\n (layer) => layer.profiles,\n (profile) => profile.name,\n \"profile\",\n );\n const errors = [...variables.errors, ...profiles.errors];\n if (errors.length > 0) return { ok: false, errors };\n return {\n ok: true,\n layer: {\n schema: SETUP_SCHEMA,\n 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WAAW,IAAI;CACtD;CACA,KAAK,MAAM,YAAY,UAAU,OAAO,GACtC,IAAI,CAAC,KAAK,IAAI,SAAS,IAAI,GAAG,OAAO,IAAI,WAAW,yBAAyB,OAAO,SAAS,QAAQ,+BAA+B,SAAS,KAAK,GAAG;CAEvJ,WAAW,QAAQ,OAAO,KAAK;CAC/B,OAAO,OAAO;AAChB;;AAGA,SAAgB,cAAc,QAAwC;CACpE,OAAO,OAAO,MAAM,UAAU,MAAM,UAAU,OAAO;AACvD;;;;;;;;;;;;;;AC3SA,SAAgB,UAAU,QAAmE;CAC3F,MAAM,YAAY,YAChB,SACC,UAAU,MAAM,YAChB,aAAa,SAAS,MACvB,UACF;CACA,MAAM,WAAW,YACf,SACC,UAAU,MAAM,WAChB,YAAY,QAAQ,MACrB,SACF;CACA,MAAM,SAAS,CAAC,GAAG,UAAU,QAAQ,GAAG,SAAS,MAAM;CACvD,IAAI,OAAO,SAAS,GAAG,OAAO;EAAE,IAAI;EAAO;CAAO;CAClD,OAAO;EACL,IAAI;EACJ,OAAO;GACL,QAAQ;GACR,GAAI,OAAO,MAAM,UAAU,MAAM,MAAM,cAAc,KAAA,CAAS,IAAI,EAAE,WAAW,UAAU,MAAM,IAAI,CAAC;GACpG,UAAU,SAAS;EACrB;CACF;AACF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/layer.ts
|
|
2
|
+
/** The name of the format of a setup layer. It is the key of the layer in `Stack.layers`. */
|
|
3
|
+
const SETUP_SCHEMA = "setup/1";
|
|
4
|
+
/** The kinds of a step. */
|
|
5
|
+
const STEP_KINDS = [
|
|
6
|
+
"package",
|
|
7
|
+
"user",
|
|
8
|
+
"file",
|
|
9
|
+
"download",
|
|
10
|
+
"swap",
|
|
11
|
+
"service",
|
|
12
|
+
"command"
|
|
13
|
+
];
|
|
14
|
+
/** This function gives the address of a step: the name of its profile, a point and its `id`. */
|
|
15
|
+
function stepAddress(profile, step) {
|
|
16
|
+
return `${profile.name}.${step.id}`;
|
|
17
|
+
}
|
|
18
|
+
/** This function tells if a value is a variable read: an object with a text field `$var` that is not a list. */
|
|
19
|
+
function isVariableRead(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.$var === "string";
|
|
21
|
+
}
|
|
22
|
+
/** This function gives the parts of a text: each string and each variable read, in order. */
|
|
23
|
+
function textParts(text) {
|
|
24
|
+
if (typeof text === "string" || isVariableRead(text)) return [text];
|
|
25
|
+
return text;
|
|
26
|
+
}
|
|
27
|
+
/** This function gives the names of the variables that a text reads, in order, with no repeat. */
|
|
28
|
+
function textVariables(text) {
|
|
29
|
+
const names = [];
|
|
30
|
+
for (const part of textParts(text)) if (typeof part !== "string" && !names.includes(part.$var)) names.push(part.$var);
|
|
31
|
+
return names;
|
|
32
|
+
}
|
|
33
|
+
/** This function gives each text of a step, for a walk over its variables. */
|
|
34
|
+
function stepTexts(step) {
|
|
35
|
+
switch (step.kind) {
|
|
36
|
+
case "file": return [step.text];
|
|
37
|
+
case "service": return [step.unit];
|
|
38
|
+
case "command": return [...step.run, ...step.check];
|
|
39
|
+
default: return [];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** This function gives the paths that a step writes. Two steps of one host cannot write one path. */
|
|
43
|
+
function stepPaths(step) {
|
|
44
|
+
switch (step.kind) {
|
|
45
|
+
case "file":
|
|
46
|
+
case "download":
|
|
47
|
+
case "swap": return [step.path];
|
|
48
|
+
case "service": return [unitPath(step.name)];
|
|
49
|
+
default: return [];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** This function gives the path of the unit file of a service. */
|
|
53
|
+
function unitPath(name) {
|
|
54
|
+
return `/etc/systemd/system/${name}.service`;
|
|
55
|
+
}
|
|
56
|
+
export { stepPaths as a, textVariables as c, stepAddress as i, unitPath as l, STEP_KINDS as n, stepTexts as o, isVariableRead as r, textParts as s, SETUP_SCHEMA as t };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=layer-DWfBSz3H.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer-DWfBSz3H.js","names":[],"sources":["../src/layer.ts"],"sourcesContent":["// The records of the setup add-on. They are one layer of a stack, with the key `setup/1` in `Stack.layers`.\n// Each record tells a step of the setup inside a host: a package, a user, a file, a download, a swap file, a service or a command.\n// The add-on knows no provider and no cloud. The host is a part of the stack, and the steps are data.\n\n/** The name of the format of a setup layer. It is the key of the layer in `Stack.layers`. */\nexport const SETUP_SCHEMA = \"setup/1\";\n\n/** A value that the script reads from its environment at its start, for example a key. No record holds its value. */\nexport interface VariableRead {\n /** The name of a variable of the layer. */\n readonly $var: string;\n}\n\n/**\n * A text of a step.\n *\n * - A string is literal. The script quotes it, so the shell expands nothing in it.\n * - `{ \"$var\": \"NAME\" }` is the value of a variable of the layer, from the environment when the script runs.\n * - A list joins its parts into one text, for example `[\"token=\", { \"$var\": \"TOKEN\" }, \"\\n\"]`.\n */\nexport type SetupText = string | VariableRead | readonly (string | VariableRead)[];\n\n/** A variable of the layer. A person or a program gives its value in the environment of the script. A variable has no default. */\nexport interface Variable {\n /** The name: capital letters, digits and `_`, with a letter first. The script itself uses only lowercase names. */\n readonly name: string;\n /** `true`: the value is a secret. The gate refuses it in a file that each user can read, and in a unit of a service. */\n readonly sensitive?: boolean | undefined;\n readonly description?: string | undefined;\n}\n\n/** The fields of each step. */\ninterface StepBase {\n /** The name of the step in its profile. The address of a step is the name of the profile, a point and this name. */\n readonly id: string;\n readonly description?: string | undefined;\n}\n\n/** Packages of the system package manager. The step installs each package that is not installed. */\nexport interface PackageStep extends StepBase {\n readonly kind: \"package\";\n /** The package manager. Only `apt` is known. */\n readonly manager: \"apt\";\n /** The names of the packages. */\n readonly names: readonly string[];\n}\n\n/** A user account of the host. */\nexport interface UserStep extends StepBase {\n readonly kind: \"user\";\n /** The login name. */\n readonly name: string;\n /** The extra groups of the user. Each group exists before the step. The step adds the user to each group, and removes none. */\n readonly groups?: readonly string[] | undefined;\n /** The login shell. The default is `/bin/bash`. */\n readonly shell?: string | undefined;\n /**\n * The public SSH keys, each one a line of `authorized_keys`. The step writes the file with these keys only.\n * With no field, the step does not change the file.\n */\n readonly keys?: readonly string[] | undefined;\n}\n\n/** A file with a text. The step writes the file when its text, its mode or its owner is different. */\nexport interface FileStep extends StepBase {\n readonly kind: \"file\";\n /** The absolute path. */\n readonly path: string;\n /** The text of the file. */\n readonly text: SetupText;\n /** The mode in octal, for example `644`. The default is `644`. */\n readonly mode?: string | undefined;\n /** The user that owns the file. The default is `root`. */\n readonly owner?: string | undefined;\n /** The group that owns the file. The default is `root`. */\n readonly group?: string | undefined;\n}\n\n/** A file from an HTTPS address, with its checksum. The step keeps a file only when its SHA-256 agrees. */\nexport interface DownloadStep extends StepBase {\n readonly kind: \"download\";\n /** The HTTPS address of the file. */\n readonly url: string;\n /** The SHA-256 of the file, as 64 lowercase hexadecimal digits. */\n readonly sha256: string;\n /** The absolute path. */\n readonly path: string;\n /** The mode in octal. The default is `644`. */\n readonly mode?: string | undefined;\n}\n\n/** A swap file. The step makes the file, turns it on and adds it to `/etc/fstab`. */\nexport interface SwapStep extends StepBase {\n readonly kind: \"swap\";\n /** The absolute path. */\n readonly path: string;\n /** The size in MiB, from 1 to 65536. */\n readonly mib: number;\n}\n\n/** A service of systemd. The step writes its unit, and starts the service at each boot. */\nexport interface ServiceStep extends StepBase {\n readonly kind: \"service\";\n /** The name of the unit with no `.service`, for example `example-runtime`. */\n readonly name: string;\n /** The text of the unit file. Each user can read a unit, so it reads no secret. */\n readonly unit: SetupText;\n /** `false`: the step writes the unit only. The default is `true`: the service starts with the script and at each boot. */\n readonly enable?: boolean | undefined;\n}\n\n/**\n * A command with its check. The step starts `run` only when `check` fails, and then `check` must pass.\n * Each command is a list of arguments, with no shell. The first argument is a literal program.\n */\nexport interface CommandStep extends StepBase {\n readonly kind: \"command\";\n /** The command that makes the change. */\n readonly run: readonly SetupText[];\n /** The command that tells if the change is there: exit code 0 tells that it is. */\n readonly check: readonly SetupText[];\n /** The user of the two commands. The default is `root`. */\n readonly as?: string | undefined;\n}\n\n/** A step of the setup. */\nexport type Step = PackageStep | UserStep | FileStep | DownloadStep | SwapStep | ServiceStep | CommandStep;\n\n/** The kinds of a step. */\nexport const STEP_KINDS: readonly [\"package\", \"user\", \"file\", \"download\", \"swap\", \"service\", \"command\"] = [\"package\", \"user\", \"file\", \"download\", \"swap\", \"service\", \"command\"];\n\n/** A kind of a step. */\nexport type StepKind = Step[\"kind\"];\n\n/**\n * The setup of one part of the stack on its hosts, as steps in order.\n * The setup of a host has the steps of each profile of the host. A profile comes after each profile whose part its part needs.\n */\nexport interface Profile {\n /** The name of the profile. */\n readonly name: string;\n /** The `id` of the part of the stack that the steps set up, for example a runtime. */\n readonly part: string;\n /** The `id` of each host part that runs the steps. A host can be the part itself. */\n readonly hosts: readonly string[];\n /** The steps, in order. */\n readonly steps: readonly Step[];\n}\n\n/** The setup layer of a stack. */\nexport interface SetupLayer {\n readonly schema: typeof SETUP_SCHEMA;\n readonly variables?: readonly Variable[] | undefined;\n readonly profiles: readonly Profile[];\n}\n\n/** This function gives the address of a step: the name of its profile, a point and its `id`. */\nexport function stepAddress(profile: Profile, step: Step): string {\n return `${profile.name}.${step.id}`;\n}\n\n/** This function tells if a value is a variable read: an object with a text field `$var` that is not a list. */\nexport function isVariableRead(value: unknown): value is VariableRead {\n return typeof value === \"object\" && value !== null && !Array.isArray(value) && typeof (value as { readonly $var?: unknown }).$var === \"string\";\n}\n\n/** This function gives the parts of a text: each string and each variable read, in order. */\nexport function textParts(text: SetupText): readonly (string | VariableRead)[] {\n if (typeof text === \"string\" || isVariableRead(text)) return [text];\n return text;\n}\n\n/** This function gives the names of the variables that a text reads, in order, with no repeat. */\nexport function textVariables(text: SetupText): string[] {\n const names: string[] = [];\n for (const part of textParts(text)) if (typeof part !== \"string\" && !names.includes(part.$var)) names.push(part.$var);\n return names;\n}\n\n/** This function gives each text of a step, for a walk over its variables. */\nexport function stepTexts(step: Step): readonly SetupText[] {\n switch (step.kind) {\n case \"file\":\n return [step.text];\n case \"service\":\n return [step.unit];\n case \"command\":\n return [...step.run, ...step.check];\n default:\n return [];\n }\n}\n\n/** This function gives the paths that a step writes. Two steps of one host cannot write one path. */\nexport function stepPaths(step: Step): string[] {\n switch (step.kind) {\n case \"file\":\n case \"download\":\n case \"swap\":\n return [step.path];\n case \"service\":\n return [unitPath(step.name)];\n default:\n return [];\n }\n}\n\n/** This function gives the path of the unit file of a service. */\nexport function unitPath(name: string): string {\n return `/etc/systemd/system/${name}.service`;\n}\n"],"mappings":";;AAKA,MAAa,eAAe;;AA4H5B,MAAa,aAA6F;CAAC;CAAW;CAAQ;CAAQ;CAAY;CAAQ;CAAW;AAAS;;AA4B9K,SAAgB,YAAY,SAAkB,MAAoB;CAChE,OAAO,GAAG,QAAQ,KAAK,GAAG,KAAK;AACjC;;AAGA,SAAgB,eAAe,OAAuC;CACpE,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK,KAAK,OAAQ,MAAsC,SAAS;AACxI;;AAGA,SAAgB,UAAU,MAAqD;CAC7E,IAAI,OAAO,SAAS,YAAY,eAAe,IAAI,GAAG,OAAO,CAAC,IAAI;CAClE,OAAO;AACT;;AAGA,SAAgB,cAAc,MAA2B;CACvD,MAAM,QAAkB,CAAC;CACzB,KAAK,MAAM,QAAQ,UAAU,IAAI,GAAG,IAAI,OAAO,SAAS,YAAY,CAAC,MAAM,SAAS,KAAK,IAAI,GAAG,MAAM,KAAK,KAAK,IAAI;CACpH,OAAO;AACT;;AAGA,SAAgB,UAAU,MAAkC;CAC1D,QAAQ,KAAK,MAAb;EACE,KAAK,QACH,OAAO,CAAC,KAAK,IAAI;EACnB,KAAK,WACH,OAAO,CAAC,KAAK,IAAI;EACnB,KAAK,WACH,OAAO,CAAC,GAAG,KAAK,KAAK,GAAG,KAAK,KAAK;EACpC,SACE,OAAO,CAAC;CACZ;AACF;;AAGA,SAAgB,UAAU,MAAsB;CAC9C,QAAQ,KAAK,MAAb;EACE,KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO,CAAC,KAAK,IAAI;EACnB,KAAK,WACH,OAAO,CAAC,SAAS,KAAK,IAAI,CAAC;EAC7B,SACE,OAAO,CAAC;CACZ;AACF;;AAGA,SAAgB,SAAS,MAAsB;CAC7C,OAAO,uBAAuB,KAAK;AACrC"}
|