@capsulos/provider-docker 0.11.5 → 0.13.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 +8 -0
- package/dist/index.js +38 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/docker.test.ts +97 -0
- package/src/index.ts +34 -2
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,15 @@ export declare class DockerProvider implements Provider {
|
|
|
12
12
|
readonly capabilities: ProviderCapabilities;
|
|
13
13
|
private readonly docker;
|
|
14
14
|
private readonly label;
|
|
15
|
+
/** False once this Docker refused CAP_BPF: capsules are created without it from then on. */
|
|
16
|
+
private bpf;
|
|
15
17
|
constructor(opts?: DockerProviderOptions);
|
|
18
|
+
/**
|
|
19
|
+
* `docker run`. With CAP_BPF asked for and refused (a Docker or a kernel that does not know it), once
|
|
20
|
+
* more without it, and never again with it for this provider. The container a refused start may have
|
|
21
|
+
* left is removed (its id is in the cidfile).
|
|
22
|
+
*/
|
|
23
|
+
private start;
|
|
16
24
|
private cli;
|
|
17
25
|
/** `template` is a local image name that runs capsulod (see daemon/Dockerfile and templates/). */
|
|
18
26
|
create(spec: CapsuleSpec, _opts?: CreateOptions): Promise<CapsuleHandle>;
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// container is NOT the isolation boundary capsulos is designed around; use it
|
|
4
4
|
// for development and CI, not for untrusted code in production.
|
|
5
5
|
import { execFile } from 'node:child_process';
|
|
6
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { join } from 'node:path';
|
|
6
9
|
import { promisify } from 'node:util';
|
|
7
10
|
import { CapsuleError, } from '@capsulos/sdk';
|
|
8
11
|
const run = promisify(execFile);
|
|
@@ -27,10 +30,39 @@ export class DockerProvider {
|
|
|
27
30
|
capabilities = DOCKER_CAPABILITIES;
|
|
28
31
|
docker;
|
|
29
32
|
label;
|
|
33
|
+
/** False once this Docker refused CAP_BPF: capsules are created without it from then on. */
|
|
34
|
+
bpf;
|
|
30
35
|
constructor(opts = {}) {
|
|
31
36
|
this.docker = opts.docker ?? 'docker';
|
|
32
37
|
this.label = opts.label ?? 'capsulos';
|
|
33
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* `docker run`. With CAP_BPF asked for and refused (a Docker or a kernel that does not know it), once
|
|
41
|
+
* more without it, and never again with it for this provider. The container a refused start may have
|
|
42
|
+
* left is removed (its id is in the cidfile).
|
|
43
|
+
*/
|
|
44
|
+
async start(args, bpf, image) {
|
|
45
|
+
if (bpf.length === 0)
|
|
46
|
+
return (await this.cli(...args, image)).slice(0, 12);
|
|
47
|
+
const dir = mkdtempSync(join(tmpdir(), 'capsulos-docker-'));
|
|
48
|
+
const cidfile = join(dir, 'cid');
|
|
49
|
+
try {
|
|
50
|
+
return (await this.cli(...args, ...bpf, '--cidfile', cidfile, image)).slice(0, 12);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
// Only when the refusal is about the capability: anything else is the caller's error.
|
|
54
|
+
if (!(err instanceof CapsuleError) || !/capabilit|CAP_BPF|cap_bpf|CapAdd/i.test(err.message))
|
|
55
|
+
throw err;
|
|
56
|
+
const left = existsSync(cidfile) ? readFileSync(cidfile, 'utf8').trim() : '';
|
|
57
|
+
if (left)
|
|
58
|
+
await this.cli('rm', '-f', left).catch(() => undefined);
|
|
59
|
+
this.bpf = false;
|
|
60
|
+
return (await this.cli(...args, image)).slice(0, 12);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
rmSync(dir, { recursive: true, force: true });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
34
66
|
async cli(...args) {
|
|
35
67
|
try {
|
|
36
68
|
const { stdout } = await run(this.docker, args, { maxBuffer: 16 << 20 });
|
|
@@ -55,6 +87,11 @@ export class DockerProvider {
|
|
|
55
87
|
args.push('--cap-add', 'NET_ADMIN');
|
|
56
88
|
args.push('--sysctl', 'net.ipv4.conf.all.rp_filter=0', '--sysctl', 'net.ipv4.conf.default.rp_filter=0');
|
|
57
89
|
}
|
|
90
|
+
// capsulod also filters the sockets of the services' users by their owner, with small eBPF programs it
|
|
91
|
+
// loads itself. That takes CAP_BPF, which Docker knows since 20.10 and Linux since 5.8: where either
|
|
92
|
+
// is older, the capsule is created without it and its policy stands on routes alone (/health says
|
|
93
|
+
// which: sockets.egress is "filter" or "routes").
|
|
94
|
+
const bpf = spec.network?.internet === 'deny' && this.bpf !== false ? ['--cap-add', 'BPF'] : [];
|
|
58
95
|
for (const [k, v] of Object.entries(spec.metadata ?? {}))
|
|
59
96
|
args.push('-e', `CAPSULE_${k.toUpperCase()}=${v}`);
|
|
60
97
|
// Docker has no boot hook: the capsule's token goes in the daemon's own
|
|
@@ -68,8 +105,7 @@ export class DockerProvider {
|
|
|
68
105
|
args.push('-e', `CAPSULOD_ACCESS=${JSON.stringify(spec.access)}`);
|
|
69
106
|
if (spec.clientToken)
|
|
70
107
|
args.push('--name', `capsule-${spec.clientToken}`);
|
|
71
|
-
|
|
72
|
-
const id = (await this.cli(...args)).slice(0, 12);
|
|
108
|
+
const id = await this.start(args, bpf, spec.template);
|
|
73
109
|
if (_opts.signal?.aborted) {
|
|
74
110
|
await this.terminate(id);
|
|
75
111
|
throw new CapsuleError(`creation of container ${id} aborted; removed it`, undefined, { capsuleId: id });
|
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,GAUb,MAAM,eAAe,CAAC;AAEvB,MAAM,GAAG,GAAG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ACV,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.13.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.13.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@types/node": "^22.20.1",
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// The provider's `docker run`, against a docker that is a script: what it asks for under a network
|
|
2
|
+
// policy, and what it does when that Docker does not know CAP_BPF.
|
|
3
|
+
import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
7
|
+
import { DockerProvider } from './index.js';
|
|
8
|
+
|
|
9
|
+
const dirs: string[] = [];
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
/** A docker that writes down each call and answers `run` as told: "ok", or the refusal of a capability. */
|
|
15
|
+
function fakeDocker(refusal: string | null): { docker: string; calls: () => string[][] } {
|
|
16
|
+
const dir = mkdtempSync(join(tmpdir(), 'capsulos-fake-docker-'));
|
|
17
|
+
dirs.push(dir);
|
|
18
|
+
const log = join(dir, 'calls');
|
|
19
|
+
const docker = join(dir, 'docker');
|
|
20
|
+
writeFileSync(
|
|
21
|
+
docker,
|
|
22
|
+
`#!/bin/sh
|
|
23
|
+
printf '%s\\n' "$*" >> ${log}
|
|
24
|
+
case "$1" in
|
|
25
|
+
run)
|
|
26
|
+
cid=""; prev=""; for a in "$@"; do [ "$prev" = --cidfile ] && cid=$a; prev=$a; done
|
|
27
|
+
case " $* " in
|
|
28
|
+
*" --cap-add BPF "*)
|
|
29
|
+
${refusal === null ? ':' : `[ -n "$cid" ] && printf 'leftover1234' > "$cid"; echo '${refusal}' >&2; exit 125`}
|
|
30
|
+
;;
|
|
31
|
+
esac
|
|
32
|
+
echo c0ffee123456789 ;;
|
|
33
|
+
inspect) echo running ;;
|
|
34
|
+
port) echo 127.0.0.1:49153 ;;
|
|
35
|
+
rm) : ;;
|
|
36
|
+
esac
|
|
37
|
+
`,
|
|
38
|
+
);
|
|
39
|
+
chmodSync(docker, 0o755);
|
|
40
|
+
return {
|
|
41
|
+
docker,
|
|
42
|
+
calls: () =>
|
|
43
|
+
readFileSync(log, 'utf8')
|
|
44
|
+
.trim()
|
|
45
|
+
.split('\n')
|
|
46
|
+
.map((line) => line.split(' ')),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('docker run under a network policy', () => {
|
|
51
|
+
it('asks for NET_ADMIN and BPF, so capsulod can filter the sockets of the services’ users', async () => {
|
|
52
|
+
const fake = fakeDocker(null);
|
|
53
|
+
const provider = new DockerProvider({ docker: fake.docker });
|
|
54
|
+
const capsule = await provider.create({ template: 'capsulos/base', network: { internet: 'deny' } });
|
|
55
|
+
expect(capsule.id).toBe('c0ffee123456');
|
|
56
|
+
const run = fake.calls()[0]!;
|
|
57
|
+
expect(run.join(' ')).toContain('--cap-add NET_ADMIN');
|
|
58
|
+
expect(run.join(' ')).toContain('--cap-add BPF');
|
|
59
|
+
expect(run.at(-1)).toBe('capsulos/base');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('asks for neither without a policy', async () => {
|
|
63
|
+
const fake = fakeDocker(null);
|
|
64
|
+
await new DockerProvider({ docker: fake.docker }).create({ template: 'capsulos/base' });
|
|
65
|
+
expect(fake.calls()[0]!.join(' ')).not.toContain('--cap-add');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
for (const refusal of [
|
|
69
|
+
'docker: Error response from daemon: invalid CapAdd: unknown capability: "CAP_BPF".',
|
|
70
|
+
'docker: Error response from daemon: OCI runtime create failed: unable to apply caps: capability cap_bpf is not supported by the kernel: unknown.',
|
|
71
|
+
]) {
|
|
72
|
+
it(`creates the capsule without BPF when Docker refuses it: ${refusal.slice(40, 90)}`, async () => {
|
|
73
|
+
const fake = fakeDocker(refusal);
|
|
74
|
+
const provider = new DockerProvider({ docker: fake.docker });
|
|
75
|
+
const capsule = await provider.create({ template: 'capsulos/base', network: { internet: 'deny' } });
|
|
76
|
+
expect(capsule.id).toBe('c0ffee123456');
|
|
77
|
+
const calls = fake.calls().map((c) => c.join(' '));
|
|
78
|
+
// Refused with BPF; the container that start left is removed; then again without it.
|
|
79
|
+
expect(calls[0]).toContain('--cap-add BPF');
|
|
80
|
+
expect(calls[1]).toBe('rm -f leftover1234');
|
|
81
|
+
expect(calls[2]).toContain('--cap-add NET_ADMIN');
|
|
82
|
+
expect(calls[2]).not.toContain('BPF');
|
|
83
|
+
// And this provider does not ask for it again.
|
|
84
|
+
await provider.create({ template: 'capsulos/base', network: { internet: 'deny' } });
|
|
85
|
+
const later = fake.calls().map((c) => c.join(' ')).filter((c) => c.startsWith('run '));
|
|
86
|
+
expect(later).toHaveLength(3);
|
|
87
|
+
expect(later[2]).not.toContain('BPF');
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
it('does not hide another failure of docker run behind that', async () => {
|
|
92
|
+
const fake = fakeDocker('docker: Error response from daemon: pull access denied for capsulos/base.');
|
|
93
|
+
const provider = new DockerProvider({ docker: fake.docker });
|
|
94
|
+
await expect(provider.create({ template: 'capsulos/base', network: { internet: 'deny' } })).rejects.toThrow(/pull access denied/);
|
|
95
|
+
expect(fake.calls().filter((c) => c[0] === 'run')).toHaveLength(1);
|
|
96
|
+
});
|
|
97
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
// container is NOT the isolation boundary capsulos is designed around; use it
|
|
4
4
|
// for development and CI, not for untrusted code in production.
|
|
5
5
|
import { execFile } from 'node:child_process';
|
|
6
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { join } from 'node:path';
|
|
6
9
|
import { promisify } from 'node:util';
|
|
7
10
|
import {
|
|
8
11
|
CapsuleError,
|
|
@@ -49,12 +52,37 @@ export class DockerProvider implements Provider {
|
|
|
49
52
|
readonly capabilities = DOCKER_CAPABILITIES;
|
|
50
53
|
private readonly docker: string;
|
|
51
54
|
private readonly label: string;
|
|
55
|
+
/** False once this Docker refused CAP_BPF: capsules are created without it from then on. */
|
|
56
|
+
private bpf: boolean | undefined;
|
|
52
57
|
|
|
53
58
|
constructor(opts: DockerProviderOptions = {}) {
|
|
54
59
|
this.docker = opts.docker ?? 'docker';
|
|
55
60
|
this.label = opts.label ?? 'capsulos';
|
|
56
61
|
}
|
|
57
62
|
|
|
63
|
+
/**
|
|
64
|
+
* `docker run`. With CAP_BPF asked for and refused (a Docker or a kernel that does not know it), once
|
|
65
|
+
* more without it, and never again with it for this provider. The container a refused start may have
|
|
66
|
+
* left is removed (its id is in the cidfile).
|
|
67
|
+
*/
|
|
68
|
+
private async start(args: string[], bpf: string[], image: string): Promise<string> {
|
|
69
|
+
if (bpf.length === 0) return (await this.cli(...args, image)).slice(0, 12);
|
|
70
|
+
const dir = mkdtempSync(join(tmpdir(), 'capsulos-docker-'));
|
|
71
|
+
const cidfile = join(dir, 'cid');
|
|
72
|
+
try {
|
|
73
|
+
return (await this.cli(...args, ...bpf, '--cidfile', cidfile, image)).slice(0, 12);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
// Only when the refusal is about the capability: anything else is the caller's error.
|
|
76
|
+
if (!(err instanceof CapsuleError) || !/capabilit|CAP_BPF|cap_bpf|CapAdd/i.test(err.message)) throw err;
|
|
77
|
+
const left = existsSync(cidfile) ? readFileSync(cidfile, 'utf8').trim() : '';
|
|
78
|
+
if (left) await this.cli('rm', '-f', left).catch(() => undefined);
|
|
79
|
+
this.bpf = false;
|
|
80
|
+
return (await this.cli(...args, image)).slice(0, 12);
|
|
81
|
+
} finally {
|
|
82
|
+
rmSync(dir, { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
58
86
|
private async cli(...args: string[]): Promise<string> {
|
|
59
87
|
try {
|
|
60
88
|
const { stdout } = await run(this.docker, args, { maxBuffer: 16 << 20 });
|
|
@@ -78,6 +106,11 @@ export class DockerProvider implements Provider {
|
|
|
78
106
|
args.push('--cap-add', 'NET_ADMIN');
|
|
79
107
|
args.push('--sysctl', 'net.ipv4.conf.all.rp_filter=0', '--sysctl', 'net.ipv4.conf.default.rp_filter=0');
|
|
80
108
|
}
|
|
109
|
+
// capsulod also filters the sockets of the services' users by their owner, with small eBPF programs it
|
|
110
|
+
// loads itself. That takes CAP_BPF, which Docker knows since 20.10 and Linux since 5.8: where either
|
|
111
|
+
// is older, the capsule is created without it and its policy stands on routes alone (/health says
|
|
112
|
+
// which: sockets.egress is "filter" or "routes").
|
|
113
|
+
const bpf = spec.network?.internet === 'deny' && this.bpf !== false ? ['--cap-add', 'BPF'] : [];
|
|
81
114
|
for (const [k, v] of Object.entries(spec.metadata ?? {})) args.push('-e', `CAPSULE_${k.toUpperCase()}=${v}`);
|
|
82
115
|
// Docker has no boot hook: the capsule's token goes in the daemon's own
|
|
83
116
|
// environment. capsulod reads it once and removes it before it starts
|
|
@@ -87,8 +120,7 @@ export class DockerProvider implements Provider {
|
|
|
87
120
|
// The signed mode's key the same way; it is not a secret.
|
|
88
121
|
if (spec.access) args.push('-e', `CAPSULOD_ACCESS=${JSON.stringify(spec.access)}`);
|
|
89
122
|
if (spec.clientToken) args.push('--name', `capsule-${spec.clientToken}`);
|
|
90
|
-
|
|
91
|
-
const id = (await this.cli(...args)).slice(0, 12);
|
|
123
|
+
const id = await this.start(args, bpf, spec.template);
|
|
92
124
|
if (_opts.signal?.aborted) {
|
|
93
125
|
await this.terminate(id);
|
|
94
126
|
throw new CapsuleError(`creation of container ${id} aborted; removed it`, undefined, { capsuleId: id });
|