-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocker_volumes.go
More file actions
235 lines (218 loc) · 5.4 KB
/
Copy pathdocker_volumes.go
File metadata and controls
235 lines (218 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package isobox
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"path"
"strings"
)
func lockDockerImageAndValidateVolumes(ctx context.Context, argv []string) ([]string, error) {
imageIndex, ok := dockerRunImageIndex(argv)
if !ok {
return argv, nil
}
image := argv[imageIndex]
id, volumes, err := inspectDockerImageForRun(ctx, argv[0], image)
if err != nil {
return nil, err
}
if dockerArgvHas(argv, "--read-only") {
violations := dockerReadOnlyVolumeViolations(argv, volumes)
if len(violations) != 0 {
return nil, fmt.Errorf("isobox: docker image %q declares writable VOLUME %q, but this plan uses --read-only; use an image without that VOLUME or explicitly allow the path with Writable/AllowTemp", image, violations[0])
}
}
rewritten := append([]string(nil), argv...)
rewritten[imageIndex] = id
return rewritten, nil
}
type dockerImageInspect struct {
ID string `json:"Id"`
Config struct {
Volumes map[string]json.RawMessage `json:"Volumes"`
} `json:"Config"`
}
func inspectDockerImageForRun(ctx context.Context, docker, image string) (string, []string, error) {
out, err := exec.CommandContext(ctx, docker, "image", "inspect", image).Output()
if err != nil {
return "", nil, fmt.Errorf("isobox: inspecting docker image %q: %w", image, err)
}
id, volumes, err := parseDockerImageInspect(out)
if err != nil {
return "", nil, fmt.Errorf("isobox: parsing docker image inspect for %q: %w", image, err)
}
return id, volumes, nil
}
func parseDockerImageInspect(data []byte) (string, []string, error) {
var images []dockerImageInspect
if err := json.Unmarshal(data, &images); err != nil {
return "", nil, err
}
if len(images) == 0 || images[0].ID == "" {
return "", nil, fmt.Errorf("missing image ID")
}
raw := images[0].Config.Volumes
volumes := make([]string, 0, len(raw))
for volume := range raw {
volumes = append(volumes, cleanDockerContainerPath(volume))
}
return images[0].ID, volumes, nil
}
func dockerReadOnlyVolumeViolations(argv []string, volumes []string) []string {
allowed := dockerWritableDestinations(argv)
violations := make([]string, 0, len(volumes))
for _, volume := range volumes {
volume = cleanDockerContainerPath(volume)
if volume == "" || dockerPathUnderAnyScope(volume, allowed) {
continue
}
violations = append(violations, volume)
}
return violations
}
func dockerWritableDestinations(argv []string) []string {
var out []string
for i := 0; i < len(argv); i++ {
switch argv[i] {
case "--tmpfs":
if i+1 < len(argv) {
out = append(out, dockerMountDestinationValue(argv[i+1]))
i++
}
case "--mount":
if i+1 < len(argv) {
spec := argv[i+1]
if dockerMountIsWritable(spec) {
out = append(out, dockerMountDestination(spec))
}
i++
}
}
}
return out
}
func dockerMountIsWritable(spec string) bool {
readonly := false
typ := ""
for _, part := range strings.Split(spec, ",") {
key, value, ok := strings.Cut(part, "=")
if !ok {
if part == "readonly" || part == "ro" {
readonly = true
}
continue
}
switch key {
case "type":
typ = value
case "readonly", "ro":
if value == "" || value == "true" || value == "1" {
readonly = true
}
}
}
if readonly {
return false
}
return typ == "bind" || typ == "volume" || typ == "tmpfs"
}
func dockerMountDestination(spec string) string {
for _, part := range strings.Split(spec, ",") {
key, value, ok := strings.Cut(part, "=")
if !ok {
continue
}
switch key {
case "dst", "destination", "target":
return cleanDockerContainerPath(value)
}
}
return ""
}
func dockerMountDestinationValue(value string) string {
if before, _, ok := strings.Cut(value, ":"); ok {
value = before
}
return cleanDockerContainerPath(value)
}
func dockerRunImage(argv []string) (string, bool) {
idx, ok := dockerRunImageIndex(argv)
if !ok {
return "", false
}
return argv[idx], true
}
func dockerRunImageIndex(argv []string) (int, bool) {
if len(argv) < 3 || argv[1] != "run" {
return -1, false
}
for i := 2; i < len(argv); i++ {
arg := argv[i]
if arg == "--" {
if i+1 < len(argv) {
return i + 1, true
}
return -1, false
}
if arg == "--rm" || arg == "--read-only" {
continue
}
if strings.HasPrefix(arg, "--") {
if strings.Contains(arg, "=") {
continue
}
if dockerRunOptionTakesValue(arg) && i+1 < len(argv) {
i++
continue
}
continue
}
return i, true
}
return -1, false
}
func dockerRunOptionTakesValue(option string) bool {
switch option {
case "--name", "--ipc", "--runtime", "--tmpfs", "--mount", "--workdir", "--cpus", "--memory", "--memory-swap", "--pids-limit", "--network", "--security-opt":
return true
default:
return false
}
}
func dockerArgvHas(argv []string, flag string) bool {
for _, arg := range argv {
if arg == flag {
return true
}
}
return false
}
func dockerPathUnderAnyScope(path string, scopes []string) bool {
for _, scope := range scopes {
if dockerPathUnderScope(path, scope) {
return true
}
}
return false
}
func dockerPathUnderScope(path, scope string) bool {
path = cleanDockerContainerPath(path)
scope = cleanDockerContainerPath(scope)
if path == "" || scope == "" {
return false
}
if path == scope {
return true
}
if scope == "/" {
return strings.HasPrefix(path, "/")
}
return strings.HasPrefix(path, scope+"/")
}
func cleanDockerContainerPath(p string) string {
if p == "" {
return ""
}
return path.Clean("/" + strings.TrimPrefix(p, "/"))
}