@capsulos/provider-docker 0.2.0 → 0.8.0
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/index.d.ts +4 -1
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/docker.e2e.test.ts +137 -0
- package/src/index.ts +33 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { type CapsuleHandle, type CapsuleInfo, type CapsuleSpec, type Connection, type CreateOptions, type Provider, type SnapshotResult } from '@capsulos/sdk';
|
|
1
|
+
import { type CapsuleHandle, type CapsuleInfo, type CapsuleSpec, type Connection, type CreateOptions, type Provider, type ProviderCapabilities, type SnapshotResult } from '@capsulos/sdk';
|
|
2
2
|
export interface DockerProviderOptions {
|
|
3
3
|
/** Docker binary; defaults to `docker` on PATH. */
|
|
4
4
|
docker?: string;
|
|
5
5
|
/** Label value used to find capsules among other containers. */
|
|
6
6
|
label?: string;
|
|
7
7
|
}
|
|
8
|
+
/** What a container on this machine gives a capsule. Development and CI: a container is not the isolation capsulos is designed around. */
|
|
9
|
+
export declare const DOCKER_CAPABILITIES: ProviderCapabilities;
|
|
8
10
|
export declare class DockerProvider implements Provider {
|
|
9
11
|
readonly name = "docker";
|
|
12
|
+
readonly capabilities: ProviderCapabilities;
|
|
10
13
|
private readonly docker;
|
|
11
14
|
private readonly label;
|
|
12
15
|
constructor(opts?: DockerProviderOptions);
|
package/dist/index.js
CHANGED
|
@@ -7,8 +7,24 @@ import { promisify } from 'node:util';
|
|
|
7
7
|
import { CapsuleError, } from '@capsulos/sdk';
|
|
8
8
|
const run = promisify(execFile);
|
|
9
9
|
const CAPSULOD_PORT = 8080;
|
|
10
|
+
/** What a container on this machine gives a capsule. Development and CI: a container is not the isolation capsulos is designed around. */
|
|
11
|
+
export const DOCKER_CAPABILITIES = {
|
|
12
|
+
os: ['linux'],
|
|
13
|
+
arch: [process.arch === 'arm64' ? 'arm64' : 'amd64'],
|
|
14
|
+
isolation: 'container',
|
|
15
|
+
gpu: false,
|
|
16
|
+
pause: true,
|
|
17
|
+
snapshot: true,
|
|
18
|
+
mounts: true,
|
|
19
|
+
networkPolicy: true,
|
|
20
|
+
agentHarness: true,
|
|
21
|
+
// A version is a tag of the image: `capsulos/<name>:<version>` is the template's name.
|
|
22
|
+
templateVersions: false,
|
|
23
|
+
typicalStartMs: 1000,
|
|
24
|
+
};
|
|
10
25
|
export class DockerProvider {
|
|
11
26
|
name = 'docker';
|
|
27
|
+
capabilities = DOCKER_CAPABILITIES;
|
|
12
28
|
docker;
|
|
13
29
|
label;
|
|
14
30
|
constructor(opts = {}) {
|
|
@@ -31,8 +47,25 @@ export class DockerProvider {
|
|
|
31
47
|
// Mounts work with real kernel privileges, same as on Lambda with additionalOsCapabilities=ALL.
|
|
32
48
|
if (spec.mounts && Object.keys(spec.mounts).length)
|
|
33
49
|
args.push('--privileged');
|
|
50
|
+
// The network policy locks the container's routes from inside (CAP_NET_ADMIN)
|
|
51
|
+
// and needs reverse-path filtering off, or the kernel drops the replies to
|
|
52
|
+
// the proxy's connections; /proc/sys is read-only in the container, so
|
|
53
|
+
// the sysctls are set here.
|
|
54
|
+
if (spec.network?.internet === 'deny') {
|
|
55
|
+
args.push('--cap-add', 'NET_ADMIN');
|
|
56
|
+
args.push('--sysctl', 'net.ipv4.conf.all.rp_filter=0', '--sysctl', 'net.ipv4.conf.default.rp_filter=0');
|
|
57
|
+
}
|
|
34
58
|
for (const [k, v] of Object.entries(spec.metadata ?? {}))
|
|
35
59
|
args.push('-e', `CAPSULE_${k.toUpperCase()}=${v}`);
|
|
60
|
+
// Docker has no boot hook: the capsule's token goes in the daemon's own
|
|
61
|
+
// environment. capsulod reads it once and removes it before it starts
|
|
62
|
+
// anything, so no process of the capsule inherits it (it stays visible
|
|
63
|
+
// to `docker inspect` on the host, and to root inside).
|
|
64
|
+
if (spec.token)
|
|
65
|
+
args.push('-e', `CAPSULOD_TOKEN=${spec.token}`);
|
|
66
|
+
// The signed mode's key the same way; it is not a secret.
|
|
67
|
+
if (spec.access)
|
|
68
|
+
args.push('-e', `CAPSULOD_ACCESS=${JSON.stringify(spec.access)}`);
|
|
36
69
|
if (spec.clientToken)
|
|
37
70
|
args.push('--name', `capsule-${spec.clientToken}`);
|
|
38
71
|
args.push(spec.template);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qEAAqE;AACrE,8EAA8E;AAC9E,gEAAgE;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,YAAY,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,qEAAqE;AACrE,8EAA8E;AAC9E,gEAAgE;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,YAAY,GAUb,MAAM,eAAe,CAAC;AAEvB,MAAM,GAAG,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAShC,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,0IAA0I;AAC1I,MAAM,CAAC,MAAM,mBAAmB,GAAyB;IACvD,EAAE,EAAE,CAAC,OAAO,CAAC;IACb,IAAI,EAAE,CAAC,OAAO,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC;IACpD,SAAS,EAAE,WAAW;IACtB,GAAG,EAAE,KAAK;IACV,KAAK,EAAE,IAAI;IACX,QAAQ,EAAE,IAAI;IACd,MAAM,EAAE,IAAI;IACZ,aAAa,EAAE,I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AAI,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,aAAa,MAAM,CAAC,CAAC;QAChE,iEAAiE;QACjE,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QACxC,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;QACxC,IAAI,CAAC,QAAQ;YAAE,MAAM,IAAI,YAAY,CAAC,aAAa,EAAE,0BAA0B,aAAa,EAAE,CAAC,CAAC;QAChG,OAAO,EAAE,OAAO,EAAE,oBAAoB,QAAQ,EAAE,EAAE,OAAO,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;IAChF,CAAC;CACF;AAED,SAAS,OAAO,CAAC,CAAS;IACxB,QAAQ,CAAC,EAAE,CAAC;QACV,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;QACnB,KAAK,SAAS;YACZ,OAAO,SAAS,CAAC;QACnB,KAAK,QAAQ;YACX,OAAO,WAAW,CAAC;QACrB,KAAK,QAAQ,CAAC;QACd,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACb,OAAO,YAAY,CAAC;QACtB;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capsulos/provider-docker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "capsulos provider for local Docker: the development and CI bench. Not an isolation boundary.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"keywords": [
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"access": "public"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@capsulos/sdk": "0.
|
|
46
|
+
"@capsulos/sdk": "0.8.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^22.20.1",
|
package/src/docker.e2e.test.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// templates/base with the capsulod from .artifacts). Set
|
|
4
4
|
// CAPSULOS_DOCKER_IMAGE to use another image. Skipped otherwise.
|
|
5
5
|
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { mkdtempSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { join } from 'node:path';
|
|
6
9
|
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
7
10
|
import { Capsule } from '@capsulos/sdk';
|
|
8
11
|
import { DockerProvider } from './index.js';
|
|
@@ -77,6 +80,34 @@ describe.skipIf(!dockerHasImage(IMAGE))('docker provider e2e', () => {
|
|
|
77
80
|
}
|
|
78
81
|
});
|
|
79
82
|
|
|
83
|
+
it('enforces a network policy: allow list through the proxy, everything else unreachable', async () => {
|
|
84
|
+
const locked = await Capsule.create(
|
|
85
|
+
provider,
|
|
86
|
+
{ template: IMAGE!, network: { internet: 'deny', allow: ['registry.npmjs.org'] } },
|
|
87
|
+
{ readyTimeoutMs: 60_000 },
|
|
88
|
+
);
|
|
89
|
+
try {
|
|
90
|
+
const h = await locked.health();
|
|
91
|
+
expect(h.network).toMatchObject({ internet: 'deny', enforced: true, proxy: '127.0.0.1:3128' });
|
|
92
|
+
// Allowed host, through HTTPS_PROXY (inherited by every exec).
|
|
93
|
+
const ok = await locked.commands.run('curl -fsS -m 20 -o /dev/null -w "%{http_code}" https://registry.npmjs.org/-/ping');
|
|
94
|
+
expect(ok.stdout, ok.stderr).toBe('200');
|
|
95
|
+
// Blocked host: the proxy answers 403 (curl -f → exit 22).
|
|
96
|
+
const blocked = await locked.commands.run('curl -fsS -m 20 https://example.com/ 2>&1; echo "exit=$?"');
|
|
97
|
+
expect(blocked.stdout).toMatch(/403/);
|
|
98
|
+
expect(blocked.stdout).toMatch(/exit=22/);
|
|
99
|
+
// Bypassing the proxy variables does not help: the default route is a
|
|
100
|
+
// blackhole, so even the DNS lookup has nowhere to go (curl 6: could not
|
|
101
|
+
// resolve; 7 / 28 would be the connect failing or timing out).
|
|
102
|
+
const direct = await locked.commands.run('curl -sS -m 5 --noproxy "*" https://example.com/ 2>&1; echo "exit=$?"');
|
|
103
|
+
expect(direct.stdout).toMatch(/exit=(6|7|28)/);
|
|
104
|
+
expect(direct.stdout).not.toMatch(/exit=0/);
|
|
105
|
+
expect((await locked.health()).network?.blocked).toBeGreaterThanOrEqual(1);
|
|
106
|
+
} finally {
|
|
107
|
+
await locked.kill();
|
|
108
|
+
}
|
|
109
|
+
}, 120_000);
|
|
110
|
+
|
|
80
111
|
it('lists and terminates capsules', async () => {
|
|
81
112
|
const before = await provider.list();
|
|
82
113
|
expect(before.map((c) => c.id)).toContain(capsule.id);
|
|
@@ -85,3 +116,109 @@ describe.skipIf(!dockerHasImage(IMAGE))('docker provider e2e', () => {
|
|
|
85
116
|
expect(await provider.get(capsule.id)).toBeNull();
|
|
86
117
|
});
|
|
87
118
|
});
|
|
119
|
+
|
|
120
|
+
// A capsule that runs code it does not trust: a service with its own user,
|
|
121
|
+
// which tries to drive the daemon from inside. What it finds is in its log.
|
|
122
|
+
describe.skipIf(!dockerHasImage(IMAGE))('docker provider e2e: a service that is not root', () => {
|
|
123
|
+
const provider = new DockerProvider({ label: 'capsulos-e2e-agent' });
|
|
124
|
+
const AGENT_IMAGE = 'capsulos-e2e-agent:latest';
|
|
125
|
+
let capsule: Capsule;
|
|
126
|
+
let findings: Record<string, string>;
|
|
127
|
+
|
|
128
|
+
const probe = `#!/bin/sh
|
|
129
|
+
code() { curl -s -o /dev/null -m 5 -w '%{http_code}' "$@"; }
|
|
130
|
+
until [ "$(code http://127.0.0.1:8080/health)" = 200 ]; do sleep 0.1; done
|
|
131
|
+
hooks=http://127.0.0.1:9000/aws/lambda-microvms/runtime/v1
|
|
132
|
+
echo "uid=$(id -u)"
|
|
133
|
+
echo "caps=$(grep CapEff /proc/self/status | cut -f2)"
|
|
134
|
+
echo "health=$(code http://127.0.0.1:8080/health)"
|
|
135
|
+
echo "exec=$(code -X POST -d '{"cmd":"id"}' http://127.0.0.1:8080/exec)"
|
|
136
|
+
echo "exec-other-address=$(code -X POST -d '{"cmd":"id"}' http://$(hostname -i):8080/exec)"
|
|
137
|
+
echo "files=$(code 'http://127.0.0.1:8080/files/read?path=/etc/shadow')"
|
|
138
|
+
echo "configure=$(code -X POST -d '{"metadata":{"owner":"agent"}}' http://127.0.0.1:8080/configure)"
|
|
139
|
+
echo "services=$(code http://127.0.0.1:8080/services)"
|
|
140
|
+
echo "proxy-own-port=$(code -X POST -d '{"cmd":"id"}' http://127.0.0.1:8080/proxy/8080/exec)"
|
|
141
|
+
echo "hook-run=$(code -X POST -d '{"runHookPayload":"{\\"token\\":\\"a-token-the-agent-chose-0001\\"}"}' $hooks/run)"
|
|
142
|
+
echo "hook-suspend=$(code -X POST -d '{}' $hooks/suspend)"
|
|
143
|
+
echo "token-in-env=[$CAPSULOD_TOKEN]"
|
|
144
|
+
echo "daemon-environ=$(cat /proc/1/environ 2>/dev/null | tr '\\0' '\\n' | grep -c CAPSULOD_TOKEN)"
|
|
145
|
+
echo "metadata-file-writable=$( [ -w /run/capsulos/metadata.json ] && echo yes || echo no )"
|
|
146
|
+
echo probed
|
|
147
|
+
exec sleep infinity
|
|
148
|
+
`;
|
|
149
|
+
|
|
150
|
+
beforeAll(async () => {
|
|
151
|
+
const ctx = mkdtempSync(join(tmpdir(), 'capsulos-agent-'));
|
|
152
|
+
writeFileSync(join(ctx, 'probe.sh'), probe, { mode: 0o755 });
|
|
153
|
+
writeFileSync(
|
|
154
|
+
join(ctx, 'services.json'),
|
|
155
|
+
JSON.stringify({ services: [{ name: 'agent', run: '/opt/probe.sh', user: 'agent' }, { name: 'root-side', run: 'echo "side uid=$(id -u)"; exec sleep infinity' }] }),
|
|
156
|
+
);
|
|
157
|
+
writeFileSync(
|
|
158
|
+
join(ctx, 'Dockerfile'),
|
|
159
|
+
`FROM ${IMAGE}\nRUN id agent >/dev/null 2>&1 || useradd -m agent\nCOPY --chmod=0755 probe.sh /opt/probe.sh\nCOPY services.json /etc/capsulos/services.json\n`,
|
|
160
|
+
);
|
|
161
|
+
execFileSync('docker', ['build', '-q', '-t', AGENT_IMAGE, ctx], { stdio: 'ignore' });
|
|
162
|
+
capsule = await Capsule.create(provider, { template: AGENT_IMAGE, metadata: { owner: 'platform' } }, { readyTimeoutMs: 60_000 });
|
|
163
|
+
for (let i = 0; i < 200; i++) {
|
|
164
|
+
const { lines } = await capsule.services.logs('agent');
|
|
165
|
+
findings = Object.fromEntries(lines.map((l) => l.text.split(/=(.*)/s).slice(0, 2) as [string, string]));
|
|
166
|
+
if (lines.some((l) => l.text === 'probed')) break;
|
|
167
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
168
|
+
}
|
|
169
|
+
}, 120_000);
|
|
170
|
+
|
|
171
|
+
afterAll(async () => {
|
|
172
|
+
await capsule?.kill();
|
|
173
|
+
execFileSync('docker', ['rmi', '-f', AGENT_IMAGE], { stdio: 'ignore' });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('runs each service as the user it names, without capabilities', async () => {
|
|
177
|
+
const list = await capsule.services.list();
|
|
178
|
+
expect(list.map((s) => [s.name, s.user ?? 'root', s.running])).toEqual([
|
|
179
|
+
['agent', 'agent', true],
|
|
180
|
+
['root-side', 'root', true],
|
|
181
|
+
]);
|
|
182
|
+
expect(Number(findings.uid)).toBe(list[0]!.uid);
|
|
183
|
+
expect(Number(findings.uid)).toBeGreaterThan(0);
|
|
184
|
+
expect(findings.caps).toBe('0000000000000000');
|
|
185
|
+
expect((await capsule.services.logs('root-side')).lines.map((l) => l.text)).toEqual(['side uid=0']);
|
|
186
|
+
expect((await capsule.health()).auth).toEqual({ token: true, mode: 'token', refusedUids: [list[0]!.uid] });
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("refuses that user on the daemon's API, on /proxy to the daemon and on the hooks", () => {
|
|
190
|
+
expect(findings).toMatchObject({
|
|
191
|
+
health: '200', // open to everyone: a service reads the network policy there
|
|
192
|
+
exec: '403',
|
|
193
|
+
'exec-other-address': '403', // the capsule's own address is no way around loopback
|
|
194
|
+
files: '403',
|
|
195
|
+
configure: '403',
|
|
196
|
+
services: '403',
|
|
197
|
+
'proxy-own-port': '403',
|
|
198
|
+
'hook-run': '403',
|
|
199
|
+
'hook-suspend': '403',
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('keeps the token where that user cannot read it', async () => {
|
|
204
|
+
expect(findings).toMatchObject({ 'token-in-env': '[]', 'daemon-environ': '0', 'metadata-file-writable': 'no' });
|
|
205
|
+
// Nothing the daemon starts inherits it either, root included.
|
|
206
|
+
expect((await capsule.commands.run('env | grep -c CAPSULOD_TOKEN; true')).stdout).toBe('0\n');
|
|
207
|
+
const meta = await capsule.files.read('/run/capsulos/metadata.json').catch(() => '');
|
|
208
|
+
expect(meta).not.toContain(capsule.token!);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('refuses that user even when it has the token, and anyone else without it', async () => {
|
|
212
|
+
const curl = (as: string, header: string) =>
|
|
213
|
+
capsule.commands.run(`${as} curl -s -o /dev/null -m 5 -w '%{http_code}' ${header} -X POST -d '{"cmd":"id"}' http://127.0.0.1:8080/exec`, {
|
|
214
|
+
env: { T: capsule.token! },
|
|
215
|
+
});
|
|
216
|
+
const asAgent = 'setpriv --reuid=agent --regid=agent --clear-groups';
|
|
217
|
+
expect((await curl(asAgent, '-H "X-Capsulos-Token: $T"')).stdout).toBe('403');
|
|
218
|
+
expect((await curl('', '')).stdout).toBe('401'); // root inside, no token
|
|
219
|
+
expect((await curl('', '-H "X-Capsulos-Token: $T"')).stdout).toBe('200');
|
|
220
|
+
// The agent's attempts changed nothing.
|
|
221
|
+
expect((await capsule.health()).hooks).toBeUndefined();
|
|
222
|
+
expect(await capsule.files.read('/run/capsulos/metadata.json').catch(() => 'no file')).not.toContain('agent');
|
|
223
|
+
});
|
|
224
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type Connection,
|
|
14
14
|
type CreateOptions,
|
|
15
15
|
type Provider,
|
|
16
|
+
type ProviderCapabilities,
|
|
16
17
|
type SnapshotResult,
|
|
17
18
|
} from '@capsulos/sdk';
|
|
18
19
|
|
|
@@ -27,8 +28,25 @@ export interface DockerProviderOptions {
|
|
|
27
28
|
|
|
28
29
|
const CAPSULOD_PORT = 8080;
|
|
29
30
|
|
|
31
|
+
/** What a container on this machine gives a capsule. Development and CI: a container is not the isolation capsulos is designed around. */
|
|
32
|
+
export const DOCKER_CAPABILITIES: ProviderCapabilities = {
|
|
33
|
+
os: ['linux'],
|
|
34
|
+
arch: [process.arch === 'arm64' ? 'arm64' : 'amd64'],
|
|
35
|
+
isolation: 'container',
|
|
36
|
+
gpu: false,
|
|
37
|
+
pause: true,
|
|
38
|
+
snapshot: true,
|
|
39
|
+
mounts: true,
|
|
40
|
+
networkPolicy: true,
|
|
41
|
+
agentHarness: true,
|
|
42
|
+
// A version is a tag of the image: `capsulos/<name>:<version>` is the template's name.
|
|
43
|
+
templateVersions: false,
|
|
44
|
+
typicalStartMs: 1000,
|
|
45
|
+
};
|
|
46
|
+
|
|
30
47
|
export class DockerProvider implements Provider {
|
|
31
48
|
readonly name = 'docker';
|
|
49
|
+
readonly capabilities = DOCKER_CAPABILITIES;
|
|
32
50
|
private readonly docker: string;
|
|
33
51
|
private readonly label: string;
|
|
34
52
|
|
|
@@ -52,7 +70,22 @@ export class DockerProvider implements Provider {
|
|
|
52
70
|
const args = ['run', '-d', '--label', `capsulos=${this.label}`, '-p', `127.0.0.1::${CAPSULOD_PORT}`];
|
|
53
71
|
// Mounts work with real kernel privileges, same as on Lambda with additionalOsCapabilities=ALL.
|
|
54
72
|
if (spec.mounts && Object.keys(spec.mounts).length) args.push('--privileged');
|
|
73
|
+
// The network policy locks the container's routes from inside (CAP_NET_ADMIN)
|
|
74
|
+
// and needs reverse-path filtering off, or the kernel drops the replies to
|
|
75
|
+
// the proxy's connections; /proc/sys is read-only in the container, so
|
|
76
|
+
// the sysctls are set here.
|
|
77
|
+
if (spec.network?.internet === 'deny') {
|
|
78
|
+
args.push('--cap-add', 'NET_ADMIN');
|
|
79
|
+
args.push('--sysctl', 'net.ipv4.conf.all.rp_filter=0', '--sysctl', 'net.ipv4.conf.default.rp_filter=0');
|
|
80
|
+
}
|
|
55
81
|
for (const [k, v] of Object.entries(spec.metadata ?? {})) args.push('-e', `CAPSULE_${k.toUpperCase()}=${v}`);
|
|
82
|
+
// Docker has no boot hook: the capsule's token goes in the daemon's own
|
|
83
|
+
// environment. capsulod reads it once and removes it before it starts
|
|
84
|
+
// anything, so no process of the capsule inherits it (it stays visible
|
|
85
|
+
// to `docker inspect` on the host, and to root inside).
|
|
86
|
+
if (spec.token) args.push('-e', `CAPSULOD_TOKEN=${spec.token}`);
|
|
87
|
+
// The signed mode's key the same way; it is not a secret.
|
|
88
|
+
if (spec.access) args.push('-e', `CAPSULOD_ACCESS=${JSON.stringify(spec.access)}`);
|
|
56
89
|
if (spec.clientToken) args.push('--name', `capsule-${spec.clientToken}`);
|
|
57
90
|
args.push(spec.template);
|
|
58
91
|
const id = (await this.cli(...args)).slice(0, 12);
|