hammerkit 2.0.0-next.19 → 2.0.0-next.20
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/docker/server-tools.js +96 -0
- package/dist/executer/check-container-binds.js +158 -0
- package/dist/executer/docker-service.js +35 -5
- package/dist/executer/docker-task.js +18 -7
- package/dist/executer/print-container-options.js +10 -10
- package/dist/file/stat-beneath.js +66 -0
- package/package.json +1 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isServerToolsPath = exports.hideServerTools = exports.openGateArguments = exports.gatedCommand = exports.withServerBusybox = exports.serviceGateTmpfs = exports.serverToolsBinds = exports.serverBusybox = exports.ensureServerBusybox = exports.createServerTools = void 0;
|
|
4
|
+
const promises_1 = require("fs/promises");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const promises_2 = require("stream/promises");
|
|
7
|
+
const tar_1 = require("tar");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const get_cache_directory_1 = require("../optimizer/get-cache-directory");
|
|
10
|
+
const transfer_tools_1 = require("../kubernetes/transfer-tools");
|
|
11
|
+
const pull_1 = require("./pull");
|
|
12
|
+
const remove_container_1 = require("./remove-container");
|
|
13
|
+
function createServerTools(hostBusybox) {
|
|
14
|
+
return { folder: `/.hammerkit-${(0, crypto_1.randomBytes)(16).toString('hex')}`, hostBusybox };
|
|
15
|
+
}
|
|
16
|
+
exports.createServerTools = createServerTools;
|
|
17
|
+
let busybox;
|
|
18
|
+
// The busybox on the docker host, copied out of the pinned image once per process.
|
|
19
|
+
function ensureServerBusybox(docker, status, environment) {
|
|
20
|
+
busybox ?? (busybox = copyBusybox(docker, status, environment).catch((e) => {
|
|
21
|
+
busybox = undefined;
|
|
22
|
+
throw e;
|
|
23
|
+
}));
|
|
24
|
+
return busybox;
|
|
25
|
+
}
|
|
26
|
+
exports.ensureServerBusybox = ensureServerBusybox;
|
|
27
|
+
async function copyBusybox(docker, status, environment) {
|
|
28
|
+
const name = (0, crypto_1.createHash)('sha256').update(transfer_tools_1.HELPER_TOOLS_IMAGE).digest('hex').slice(0, 16);
|
|
29
|
+
const folder = (0, path_1.join)((0, get_cache_directory_1.getHammerkitDirectory)(), 'tools');
|
|
30
|
+
const file = (0, path_1.join)(folder, `busybox-${name}`);
|
|
31
|
+
if (await (0, promises_1.stat)(file).catch(() => undefined)) {
|
|
32
|
+
return file;
|
|
33
|
+
}
|
|
34
|
+
await (0, pull_1.pull)(status, docker, transfer_tools_1.HELPER_TOOLS_IMAGE, environment);
|
|
35
|
+
await (0, promises_1.mkdir)(folder, { recursive: true, mode: 0o755 });
|
|
36
|
+
const temporary = await (0, promises_1.mkdtemp)((0, path_1.join)(folder, '.busybox-'));
|
|
37
|
+
const container = await docker.createContainer({ Image: transfer_tools_1.HELPER_TOOLS_IMAGE, Cmd: ['true'] });
|
|
38
|
+
try {
|
|
39
|
+
const archive = await container.getArchive({ path: '/bin/busybox' });
|
|
40
|
+
await (0, promises_2.pipeline)(archive, (0, tar_1.extract)({ cwd: temporary, filter: (path) => path === 'busybox' }));
|
|
41
|
+
await (0, promises_1.chmod)((0, path_1.join)(temporary, 'busybox'), 0o755);
|
|
42
|
+
await (0, promises_1.rename)((0, path_1.join)(temporary, 'busybox'), file);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
await (0, remove_container_1.removeContainer)(container).catch(() => undefined);
|
|
46
|
+
await (0, promises_1.rm)(temporary, { recursive: true, force: true });
|
|
47
|
+
}
|
|
48
|
+
return file;
|
|
49
|
+
}
|
|
50
|
+
// the busybox in the container
|
|
51
|
+
function serverBusybox(tools) {
|
|
52
|
+
return `${tools.folder}/busybox`;
|
|
53
|
+
}
|
|
54
|
+
exports.serverBusybox = serverBusybox;
|
|
55
|
+
function serverToolsBinds(tools) {
|
|
56
|
+
return [`${tools.hostBusybox}:${serverBusybox(tools)}:ro`];
|
|
57
|
+
}
|
|
58
|
+
exports.serverToolsBinds = serverToolsBinds;
|
|
59
|
+
// a tmpfs of a service's container: the server writes `go` into it once the binds are checked
|
|
60
|
+
function serviceGateTmpfs(tools) {
|
|
61
|
+
return { [`${tools.folder}/run`]: 'rw,size=64k,mode=755' };
|
|
62
|
+
}
|
|
63
|
+
exports.serviceGateTmpfs = serviceGateTmpfs;
|
|
64
|
+
function gateFile(tools) {
|
|
65
|
+
return `${tools.folder}/run/go`;
|
|
66
|
+
}
|
|
67
|
+
// `command` run by the server's busybox in a container
|
|
68
|
+
function withServerBusybox(tools, command) {
|
|
69
|
+
return [serverBusybox(tools), ...command];
|
|
70
|
+
}
|
|
71
|
+
exports.withServerBusybox = withServerBusybox;
|
|
72
|
+
// Runs `command` (the service's own, as docker would) once the server opened the gate.
|
|
73
|
+
function gatedCommand(tools, command) {
|
|
74
|
+
const busybox = serverBusybox(tools);
|
|
75
|
+
const wait = `until ${busybox} test -e ${gateFile(tools)}; do ${busybox} sleep 0.1; done; exec "$@"`;
|
|
76
|
+
return [busybox, 'sh', '-c', wait, 'hammerkit-gate', ...command];
|
|
77
|
+
}
|
|
78
|
+
exports.gatedCommand = gatedCommand;
|
|
79
|
+
// what the server's busybox runs to open the gate
|
|
80
|
+
function openGateArguments(tools) {
|
|
81
|
+
return ['touch', gateFile(tools)];
|
|
82
|
+
}
|
|
83
|
+
exports.openGateArguments = openGateArguments;
|
|
84
|
+
// `text` (a log line, an error) without the name of the tools' folder
|
|
85
|
+
function hideServerTools(text, tools) {
|
|
86
|
+
return tools ? text.split(tools.folder).join('/.hammerkit-…') : text;
|
|
87
|
+
}
|
|
88
|
+
exports.hideServerTools = hideServerTools;
|
|
89
|
+
// A path of a container where an item's source, output, mount, volume or secret would
|
|
90
|
+
// hide or replace the server's tools: `/`, or at or in a folder `.hammerkit…` of `/`.
|
|
91
|
+
function isServerToolsPath(containerPath) {
|
|
92
|
+
const normalized = path_1.posix.normalize(containerPath).replace(/\/+$/, '') || '/';
|
|
93
|
+
return normalized === '/' || /^\/\.hammerkit($|[-/])/.test(normalized);
|
|
94
|
+
}
|
|
95
|
+
exports.isServerToolsPath = isServerToolsPath;
|
|
96
|
+
//# sourceMappingURL=server-tools.js.map
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.openServiceGate = exports.readIdentity = exports.refuseUncheckableBinds = exports.refuseServerToolsPaths = exports.assertContainerBinds = void 0;
|
|
4
|
+
const path_1 = require("path");
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
const work_task_1 = require("../planner/work-task");
|
|
7
|
+
const execute_docker_1 = require("./execute-docker");
|
|
8
|
+
const execution_steps_1 = require("./execution-steps");
|
|
9
|
+
const stat_beneath_1 = require("../file/stat-beneath");
|
|
10
|
+
const server_tools_1 = require("../docker/server-tools");
|
|
11
|
+
const get_container_binds_1 = require("./get-container-binds");
|
|
12
|
+
/**
|
|
13
|
+
* On a server, what a started container binds is what the run's files hold. The engine
|
|
14
|
+
* resolves a bind's path as the container starts and says nothing about what it found, so
|
|
15
|
+
* once it started, every folder and file the container sees at a bind is compared with the
|
|
16
|
+
* one of the run's files by device and inode. The host side is read from the run's root
|
|
17
|
+
* one name at a time without following a link (statBeneath); the container side by the
|
|
18
|
+
* server's own busybox (server-tools.ts), which is also all that has run in the container
|
|
19
|
+
* so far. A container that sees anything else is refused. Off a server nothing is checked:
|
|
20
|
+
* the files are the user's own.
|
|
21
|
+
*/
|
|
22
|
+
async function assertContainerBinds(item, environment, container, binds, tools) {
|
|
23
|
+
const root = (0, execution_steps_1.hostedRoot)(environment);
|
|
24
|
+
if (!root) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!tools) {
|
|
28
|
+
throw new Error(`${item.name}: the server's tools are not in the container`);
|
|
29
|
+
}
|
|
30
|
+
await (0, execution_steps_1.refuseHostLinks)(item, environment);
|
|
31
|
+
const checks = binds
|
|
32
|
+
.filter((b) => (0, path_1.isAbsolute)(b.localPath))
|
|
33
|
+
.map(async (bind) => {
|
|
34
|
+
const expected = await (0, stat_beneath_1.statBeneath)(root, bind.localPath).catch((e) => {
|
|
35
|
+
throw new Error(`${item.name}: ${e.message}`);
|
|
36
|
+
});
|
|
37
|
+
const seen = await readIdentity(container, tools, (0, execute_docker_1.convertToPosixPath)(bind.containerPath));
|
|
38
|
+
if (seen.error !== undefined) {
|
|
39
|
+
throw new Error(`${item.name}: can not check what the container binds at ${bind.containerPath}: ${seen.error}`);
|
|
40
|
+
}
|
|
41
|
+
if (seen.identity !== `${expected.dev} ${expected.ino}`) {
|
|
42
|
+
throw new Error(`${item.name}: the container does not bind the files of the run at ${bind.containerPath}`);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const results = await Promise.allSettled(checks);
|
|
46
|
+
const failed = results.find((r) => r.status === 'rejected');
|
|
47
|
+
if (failed) {
|
|
48
|
+
throw failed.reason;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.assertContainerBinds = assertContainerBinds;
|
|
52
|
+
/**
|
|
53
|
+
* On a server, no source, output, mount, volume or secret file of an item may be where the
|
|
54
|
+
* server's tools are in a container (isServerToolsPath), where it would hide or replace them.
|
|
55
|
+
*/
|
|
56
|
+
function refuseServerToolsPaths(item, environment) {
|
|
57
|
+
if (!(0, execution_steps_1.hostedRoot)(environment)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const data = item.data;
|
|
61
|
+
const paths = [
|
|
62
|
+
...((0, work_task_1.isContainerWorkTask)(data)
|
|
63
|
+
? (0, get_container_binds_1.getContainerBinds)(item).map((b) => b.containerPath)
|
|
64
|
+
: [
|
|
65
|
+
...data.src.map((s) => s.absolutePath),
|
|
66
|
+
...data.mounts.map((m) => m.containerPath),
|
|
67
|
+
...data.volumes.map((v) => v.containerPath),
|
|
68
|
+
]),
|
|
69
|
+
...data.secrets.flatMap((secret) => (secret.target.type === 'file' ? [secret.target.path] : [])),
|
|
70
|
+
];
|
|
71
|
+
const over = paths.find((path) => (0, server_tools_1.isServerToolsPath)((0, execute_docker_1.convertToPosixPath)(path)));
|
|
72
|
+
if (over !== undefined) {
|
|
73
|
+
throw new Error(`${item.name}: ${over} is where the server keeps its tools in a container, nothing can be there`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.refuseServerToolsPaths = refuseServerToolsPaths;
|
|
77
|
+
/**
|
|
78
|
+
* On a server's docker host, a task that adds capabilities runs in a sandboxed runtime
|
|
79
|
+
* (`backend.capabilities.runtime`, such as gVisor) that reports its own device numbers, so
|
|
80
|
+
* what such a container binds can not be compared with the run's files (assertContainerBinds).
|
|
81
|
+
* Until it can, such a task may bind no files of the run: it is refused before its container
|
|
82
|
+
* is created.
|
|
83
|
+
*/
|
|
84
|
+
function refuseUncheckableBinds(item, environment) {
|
|
85
|
+
if (!(0, execution_steps_1.hostedRoot)(environment) || item.data.capAdd.length === 0) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const bound = (0, get_container_binds_1.getContainerBinds)(item).find((b) => (0, path_1.isAbsolute)(b.localPath));
|
|
89
|
+
if (bound) {
|
|
90
|
+
throw new Error(`${item.name}: adds capabilities and binds ${bound.containerPath}; on a server's docker host a task that adds capabilities can not bind files of the run`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.refuseUncheckableBinds = refuseUncheckableBinds;
|
|
94
|
+
// The device and inode of `path` inside the container, as the server's busybox reads them.
|
|
95
|
+
async function readIdentity(container, tools, path) {
|
|
96
|
+
const result = await runServerCommand(container, tools, ['stat', '-c', '%d %i', '--', path]);
|
|
97
|
+
if (result.error !== undefined) {
|
|
98
|
+
return { error: result.error };
|
|
99
|
+
}
|
|
100
|
+
const match = result.stdout.match(/^(\d+) (\d+)\s*$/);
|
|
101
|
+
return match
|
|
102
|
+
? { identity: `${BigInt(match[1])} ${BigInt(match[2])}` }
|
|
103
|
+
: { error: `unexpected answer ${result.stdout.trim()}` };
|
|
104
|
+
}
|
|
105
|
+
exports.readIdentity = readIdentity;
|
|
106
|
+
// Lets the command of a service's container run, once its binds are checked (see gatedCommand).
|
|
107
|
+
async function openServiceGate(container, tools) {
|
|
108
|
+
const result = await runServerCommand(container, tools, (0, server_tools_1.openGateArguments)(tools));
|
|
109
|
+
if (result.error !== undefined) {
|
|
110
|
+
throw new Error(`the service could not be started: ${result.error}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.openServiceGate = openServiceGate;
|
|
114
|
+
// Runs a tool of the server's busybox in the container, as root.
|
|
115
|
+
// What it says is kept free of the name of the tools' folder.
|
|
116
|
+
async function runServerCommand(container, tools, command, timeoutMs = 10000) {
|
|
117
|
+
try {
|
|
118
|
+
const exec = await container.exec({
|
|
119
|
+
Cmd: (0, server_tools_1.withServerBusybox)(tools, command),
|
|
120
|
+
AttachStdout: true,
|
|
121
|
+
AttachStderr: true,
|
|
122
|
+
User: '0',
|
|
123
|
+
});
|
|
124
|
+
const stream = await exec.start({ hijack: true, stdin: false });
|
|
125
|
+
const stdout = new stream_1.PassThrough();
|
|
126
|
+
const stderr = new stream_1.PassThrough();
|
|
127
|
+
const chunks = [];
|
|
128
|
+
const errors = [];
|
|
129
|
+
stdout.on('data', (chunk) => chunks.push(chunk));
|
|
130
|
+
stderr.on('data', (chunk) => errors.push(chunk));
|
|
131
|
+
container.modem.demuxStream(stream, stdout, stderr);
|
|
132
|
+
let timer;
|
|
133
|
+
const finished = await Promise.race([
|
|
134
|
+
new Promise((resolve) => {
|
|
135
|
+
stream.on('end', () => resolve(true));
|
|
136
|
+
stream.on('error', () => resolve(false));
|
|
137
|
+
}),
|
|
138
|
+
new Promise((resolve) => {
|
|
139
|
+
timer = setTimeout(() => resolve(false), timeoutMs);
|
|
140
|
+
}),
|
|
141
|
+
]);
|
|
142
|
+
clearTimeout(timer);
|
|
143
|
+
if (!finished) {
|
|
144
|
+
stream.destroy();
|
|
145
|
+
return { error: `no answer within ${timeoutMs / 1000}s` };
|
|
146
|
+
}
|
|
147
|
+
const result = await exec.inspect();
|
|
148
|
+
if (result.ExitCode !== 0) {
|
|
149
|
+
const error = Buffer.concat(errors).toString('utf8').trim() || `exit code ${result.ExitCode}`;
|
|
150
|
+
return { error: (0, server_tools_1.hideServerTools)(error, tools) };
|
|
151
|
+
}
|
|
152
|
+
return { stdout: Buffer.concat(chunks).toString('utf8') };
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
return { error: (0, server_tools_1.hideServerTools)(e.message, tools) };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=check-container-binds.js.map
|
|
@@ -14,6 +14,8 @@ const remove_container_1 = require("../docker/remove-container");
|
|
|
14
14
|
const check_readiness_1 = require("./check-readiness");
|
|
15
15
|
const main_process_user_1 = require("./main-process-user");
|
|
16
16
|
const execution_steps_1 = require("./execution-steps");
|
|
17
|
+
const check_container_binds_1 = require("./check-container-binds");
|
|
18
|
+
const server_tools_1 = require("../docker/server-tools");
|
|
17
19
|
const item_network_1 = require("../docker/item-network");
|
|
18
20
|
const container_secrets_1 = require("./container-secrets");
|
|
19
21
|
const replace_env_variables_1 = require("../environment/replace-env-variables");
|
|
@@ -44,9 +46,12 @@ async function readPublishedPorts(container, ports) {
|
|
|
44
46
|
});
|
|
45
47
|
}
|
|
46
48
|
exports.readPublishedPorts = readPublishedPorts;
|
|
47
|
-
function buildServiceCreateOptions(item, options, network, secrets, resources = {}
|
|
49
|
+
function buildServiceCreateOptions(item, options, network, secrets, resources = {},
|
|
50
|
+
// on a server: the server's tools, and the command the service runs once the server
|
|
51
|
+
// checked its binds (gatedCommand)
|
|
52
|
+
gate) {
|
|
48
53
|
const envs = (0, replace_env_variables_1.getEnvironmentVariables)(item.data.envs);
|
|
49
|
-
const command = (0, work_service_1.getServiceCommand)(item.data);
|
|
54
|
+
const command = gate ? { entrypoint: (0, server_tools_1.gatedCommand)(gate.tools, gate.command), cmd: [] } : (0, work_service_1.getServiceCommand)(item.data);
|
|
50
55
|
return {
|
|
51
56
|
Image: item.data.image,
|
|
52
57
|
Hostname: (0, service_hostname_1.getServiceHostname)(item.name),
|
|
@@ -74,7 +79,9 @@ function buildServiceCreateOptions(item, options, network, secrets, resources =
|
|
|
74
79
|
...item.data.mounts.map((v) => `${v.localPath}:${(0, execute_docker_1.convertToPosixPath)(v.containerPath)}${v.readOnly ? ':ro' : ''}`),
|
|
75
80
|
...item.data.volumes.map((v) => `${v.name}:${(0, execute_docker_1.convertToPosixPath)(v.containerPath)}${v.readOnly ? ':ro' : ''}`),
|
|
76
81
|
...secrets.binds,
|
|
82
|
+
...(gate ? (0, server_tools_1.serverToolsBinds)(gate.tools) : []),
|
|
77
83
|
],
|
|
84
|
+
...(gate ? { Tmpfs: (0, server_tools_1.serviceGateTmpfs)(gate.tools) } : {}),
|
|
78
85
|
// host ports are for `hammerkit up` and local tasks; container tasks
|
|
79
86
|
// reach the service over its link, and a run publishing them would
|
|
80
87
|
// collide with another run on the same host. A limited service's are
|
|
@@ -89,6 +96,15 @@ function buildServiceCreateOptions(item, options, network, secrets, resources =
|
|
|
89
96
|
};
|
|
90
97
|
}
|
|
91
98
|
exports.buildServiceCreateOptions = buildServiceCreateOptions;
|
|
99
|
+
// What docker would run in the service's container: its own command, or the image's.
|
|
100
|
+
async function getServiceProcess(docker, service) {
|
|
101
|
+
const { entrypoint, cmd } = (0, work_service_1.getServiceCommand)(service);
|
|
102
|
+
if (entrypoint) {
|
|
103
|
+
return [...entrypoint, ...(cmd ?? [])];
|
|
104
|
+
}
|
|
105
|
+
const config = (await docker.getImage(service.image).inspect()).Config;
|
|
106
|
+
return [...(config.Entrypoint ?? []), ...(cmd ?? config.Cmd ?? [])];
|
|
107
|
+
}
|
|
92
108
|
async function dockerService(docker, item, options, environment) {
|
|
93
109
|
let container = null;
|
|
94
110
|
let network = null;
|
|
@@ -107,16 +123,30 @@ async function dockerService(docker, item, options, environment) {
|
|
|
107
123
|
item.status.write('debug', `create container with image ${item.data.image}`);
|
|
108
124
|
const secrets = await (0, container_secrets_1.prepareContainerSecrets)(item.data.secrets, (0, work_instance_id_1.getWorkInstanceId)(item), environment);
|
|
109
125
|
const resources = await (0, docker_resources_1.resolveDockerResources)(docker, item.data.resources, item.status);
|
|
126
|
+
(0, check_container_binds_1.refuseServerToolsPaths)(item, environment);
|
|
127
|
+
const gate = (0, execution_steps_1.hostedRoot)(environment)
|
|
128
|
+
? {
|
|
129
|
+
tools: (0, server_tools_1.createServerTools)(await (0, server_tools_1.ensureServerBusybox)(docker, item.status, environment)),
|
|
130
|
+
command: await getServiceProcess(docker, item.data),
|
|
131
|
+
}
|
|
132
|
+
: undefined;
|
|
110
133
|
await (0, execution_steps_1.refuseHostLinks)(item, environment);
|
|
111
|
-
container = await docker.createContainer(buildServiceCreateOptions(item, options, network.options, secrets, resources));
|
|
134
|
+
container = await docker.createContainer(buildServiceCreateOptions(item, options, network.options, secrets, resources, gate));
|
|
112
135
|
const relay = await network.connect(container);
|
|
113
136
|
const stream = await container.attach({ stream: true, stdout: true, stderr: true });
|
|
114
137
|
(0, stream_1.logStream)(item.status, stream);
|
|
115
|
-
// the engine resolves the paths it binds when the container starts:
|
|
138
|
+
// the engine resolves the paths it binds when the container starts: on a server the
|
|
139
|
+
// service's own command waits for the server to check them (gatedCommand)
|
|
116
140
|
await (0, execution_steps_1.refuseHostLinks)(item, environment);
|
|
117
141
|
await container.start();
|
|
118
142
|
const started = container;
|
|
119
|
-
|
|
143
|
+
const binds = [
|
|
144
|
+
...item.data.src.map((s) => ({ localPath: s.absolutePath, containerPath: s.absolutePath })),
|
|
145
|
+
...item.data.mounts,
|
|
146
|
+
];
|
|
147
|
+
await (0, check_container_binds_1.assertContainerBinds)(item, environment, started, binds, gate?.tools)
|
|
148
|
+
.then(() => (gate ? (0, check_container_binds_1.openServiceGate)(started, gate.tools) : undefined))
|
|
149
|
+
.catch(async (e) => {
|
|
120
150
|
// not kept, not even for a daemon
|
|
121
151
|
container = null;
|
|
122
152
|
await (0, remove_container_1.removeContainer)(started);
|
|
@@ -8,6 +8,8 @@ const abort_1 = require("./abort");
|
|
|
8
8
|
const log_1 = require("../log");
|
|
9
9
|
const execution_steps_1 = require("./execution-steps");
|
|
10
10
|
const using_container_1 = require("../docker/using-container");
|
|
11
|
+
const check_container_binds_1 = require("./check-container-binds");
|
|
12
|
+
const server_tools_1 = require("../docker/server-tools");
|
|
11
13
|
const print_container_options_1 = require("./print-container-options");
|
|
12
14
|
const tar_1 = require("tar");
|
|
13
15
|
const replace_env_variables_1 = require("../environment/replace-env-variables");
|
|
@@ -18,7 +20,9 @@ const get_outputs_to_reset_1 = require("../planner/utils/get-outputs-to-reset");
|
|
|
18
20
|
const container_secrets_1 = require("./container-secrets");
|
|
19
21
|
const container_task_state_1 = require("./container-task-state");
|
|
20
22
|
const docker_resources_1 = require("./docker-resources");
|
|
21
|
-
function buildCreateOptions(item, stateKey, network, environment, secrets, resources = {}
|
|
23
|
+
function buildCreateOptions(item, stateKey, network, environment, secrets, resources = {},
|
|
24
|
+
// on a server: the server's tools, whose busybox is the container's first process
|
|
25
|
+
tools) {
|
|
22
26
|
const binds = (0, get_container_binds_1.getContainerBinds)(item);
|
|
23
27
|
const envs = (0, replace_env_variables_1.getEnvironmentVariables)(item.data.envs);
|
|
24
28
|
// Running as the host's uid (Linux), the user has no home in the image and
|
|
@@ -29,8 +33,9 @@ function buildCreateOptions(item, stateKey, network, environment, secrets, resou
|
|
|
29
33
|
abortSignal: environment.abortCtrl.signal,
|
|
30
34
|
Image: item.data.image,
|
|
31
35
|
Tty: true,
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
// on a server nothing of the image runs before its binds are checked (check-container-binds.ts)
|
|
37
|
+
Entrypoint: tools ? [(0, server_tools_1.serverBusybox)(tools)] : [item.data.shell],
|
|
38
|
+
Cmd: tools ? ['sleep', '3600'] : ['-c', 'sleep 3600'],
|
|
34
39
|
Env: Object.entries({ ...envs, ...home, ...network.env, ...secrets.env }).map(([key, value]) => `${key}=${value}`),
|
|
35
40
|
WorkingDir: (0, execute_docker_1.convertToPosixPath)(item.data.cwd),
|
|
36
41
|
Labels: {
|
|
@@ -44,6 +49,7 @@ function buildCreateOptions(item, stateKey, network, environment, secrets, resou
|
|
|
44
49
|
Binds: [
|
|
45
50
|
...binds.map((b) => `${b.localPath}:${(0, execute_docker_1.convertToPosixPath)(b.containerPath)}${b.readOnly ? ':ro' : ''}`),
|
|
46
51
|
...secrets.binds,
|
|
52
|
+
...(tools ? (0, server_tools_1.serverToolsBinds)(tools) : []),
|
|
47
53
|
],
|
|
48
54
|
...(0, item_network_1.getNetworkHostConfig)(network),
|
|
49
55
|
AutoRemove: true,
|
|
@@ -94,13 +100,18 @@ async function dockerTask(docker, item, environment, options) {
|
|
|
94
100
|
(0, abort_1.checkForAbort)(options.abort);
|
|
95
101
|
const secrets = await (0, container_secrets_1.prepareContainerSecrets)(item.data.secrets, (0, work_instance_id_1.getWorkInstanceId)(item), environment);
|
|
96
102
|
const resources = await (0, docker_resources_1.resolveDockerResources)(docker, item.data.resources, item.status);
|
|
97
|
-
|
|
98
|
-
(0,
|
|
103
|
+
(0, check_container_binds_1.refuseServerToolsPaths)(item, environment);
|
|
104
|
+
(0, check_container_binds_1.refuseUncheckableBinds)(item, environment);
|
|
105
|
+
const tools = (0, execution_steps_1.hostedRoot)(environment)
|
|
106
|
+
? (0, server_tools_1.createServerTools)(await (0, server_tools_1.ensureServerBusybox)(docker, item.status, environment))
|
|
107
|
+
: undefined;
|
|
108
|
+
const containerOptions = buildCreateOptions(item, options.stateKey, network.options, environment, secrets, resources, tools);
|
|
109
|
+
(0, print_container_options_1.printContainerOptions)(item.status, containerOptions, tools);
|
|
99
110
|
await (0, execution_steps_1.refuseHostLinks)(item, environment);
|
|
100
111
|
const itemNetwork = network;
|
|
101
112
|
const succeeded = await (0, using_container_1.usingContainer)(docker, item, containerOptions, async (container) => {
|
|
102
|
-
// the engine resolved the paths it binds as the container started:
|
|
103
|
-
await (0,
|
|
113
|
+
// the engine resolved the paths it binds as the container started: before anything runs in it
|
|
114
|
+
await (0, check_container_binds_1.assertContainerBinds)(item, environment, container, (0, get_container_binds_1.getContainerBinds)(item), tools);
|
|
104
115
|
await itemNetwork.connect(container);
|
|
105
116
|
await (0, execution_steps_1.setUserPermissions)(item, container, environment);
|
|
106
117
|
for (const generate of outputsToReset.filter((g) => !g.isFile)) {
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.printContainerOptions = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const server_tools_1 = require("../docker/server-tools");
|
|
5
|
+
// `tools`: the server's tools in the container, whose folder is not printed
|
|
6
|
+
function printContainerOptions(status, containerOptions, tools) {
|
|
7
|
+
const write = (text) => status.write('debug', (0, server_tools_1.hideServerTools)(text, tools));
|
|
8
|
+
write(`create container with image ${containerOptions.Image} with ${containerOptions.Entrypoint}`);
|
|
6
9
|
for (const mount of containerOptions.HostConfig?.Binds || []) {
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
for (const mount of containerOptions.HostConfig?.Binds || []) {
|
|
10
|
-
status.write('debug', `bind ${mount}`);
|
|
10
|
+
write(`bind ${mount}`);
|
|
11
11
|
}
|
|
12
12
|
if (containerOptions.HostConfig?.NanoCpus) {
|
|
13
|
-
|
|
13
|
+
write(`limit cpus to ${containerOptions.HostConfig.NanoCpus / 1e9}`);
|
|
14
14
|
}
|
|
15
15
|
if (containerOptions.HostConfig?.Memory) {
|
|
16
|
-
|
|
16
|
+
write(`limit memory to ${containerOptions.HostConfig.Memory} bytes`);
|
|
17
17
|
}
|
|
18
18
|
if (containerOptions.HostConfig?.MemoryReservation) {
|
|
19
|
-
|
|
19
|
+
write(`reserve memory ${containerOptions.HostConfig.MemoryReservation} bytes`);
|
|
20
20
|
}
|
|
21
21
|
for (const link of containerOptions.HostConfig?.Links || []) {
|
|
22
|
-
|
|
22
|
+
write(`link ${link}`);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.printContainerOptions = printContainerOptions;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.statBeneath = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const promises_1 = require("fs/promises");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
/**
|
|
8
|
+
* What `path`, inside `root`, is: its device and inode, reached from the root one name at a
|
|
9
|
+
* time without following a link. On Linux each folder is opened with O_NOFOLLOW through the
|
|
10
|
+
* descriptor of the one above (/proc/self/fd/<n>/<name>), and the last name is looked at
|
|
11
|
+
* (lstat) through the descriptor of its folder, so a folder swapped for a link at any moment
|
|
12
|
+
* is either refused or not looked at: the answer is never what a link led to.
|
|
13
|
+
* Elsewhere (a machine of a developer) the names are checked for links, then the path read.
|
|
14
|
+
* Throws when the path leaves the root, goes through a link, or can not be opened.
|
|
15
|
+
*/
|
|
16
|
+
async function statBeneath(root, path) {
|
|
17
|
+
const inside = (0, path_1.relative)(root, path);
|
|
18
|
+
if (inside === '..' || inside.startsWith(`..${path_1.sep}`) || (0, path_1.isAbsolute)(inside)) {
|
|
19
|
+
throw new Error(`${path} is outside of ${root}`);
|
|
20
|
+
}
|
|
21
|
+
const parts = inside === '' ? [] : inside.split(path_1.sep);
|
|
22
|
+
if (process.platform !== 'linux') {
|
|
23
|
+
let current = root;
|
|
24
|
+
for (const part of parts) {
|
|
25
|
+
current = (0, path_1.join)(current, part);
|
|
26
|
+
if ((await (0, promises_1.lstat)(current)).isSymbolicLink()) {
|
|
27
|
+
throw new Error(`${path} goes through the symbolic link ${(0, path_1.relative)(root, current)}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return (0, promises_1.stat)(path, { bigint: true });
|
|
31
|
+
}
|
|
32
|
+
let handle = await (0, promises_1.open)(root, fs_1.constants.O_RDONLY | fs_1.constants.O_DIRECTORY);
|
|
33
|
+
try {
|
|
34
|
+
if ((await (0, promises_1.readlink)(`/proc/self/fd/${handle.fd}`).catch(() => undefined)) === undefined) {
|
|
35
|
+
throw new Error(`${path} can not be checked: /proc/self/fd is not readable`);
|
|
36
|
+
}
|
|
37
|
+
// the folders on the way are opened, the last name is only looked at, from the folder
|
|
38
|
+
// above it, which is pinned by its descriptor: so a file the server may not read still
|
|
39
|
+
// has an identity
|
|
40
|
+
for (const [index, part] of parts.entries()) {
|
|
41
|
+
const through = `/proc/self/fd/${handle.fd}/${part}`;
|
|
42
|
+
if (index === parts.length - 1) {
|
|
43
|
+
const stats = await (0, promises_1.lstat)(through, { bigint: true });
|
|
44
|
+
if (stats.isSymbolicLink()) {
|
|
45
|
+
throw new Error(`${path} goes through a symbolic link at ${parts.join('/')}`);
|
|
46
|
+
}
|
|
47
|
+
return stats;
|
|
48
|
+
}
|
|
49
|
+
const next = await (0, promises_1.open)(through, fs_1.constants.O_RDONLY | fs_1.constants.O_NOFOLLOW | fs_1.constants.O_DIRECTORY).catch((e) => {
|
|
50
|
+
const code = e.code;
|
|
51
|
+
if (code === 'ELOOP' || code === 'ENOTDIR') {
|
|
52
|
+
throw new Error(`${path} goes through a symbolic link at ${parts.slice(0, index + 1).join('/')}`);
|
|
53
|
+
}
|
|
54
|
+
throw e;
|
|
55
|
+
});
|
|
56
|
+
await handle.close();
|
|
57
|
+
handle = next;
|
|
58
|
+
}
|
|
59
|
+
return await handle.stat({ bigint: true });
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
await handle.close();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.statBeneath = statBeneath;
|
|
66
|
+
//# sourceMappingURL=stat-beneath.js.map
|
package/package.json
CHANGED