@@ -9,11 +9,9 @@ package main
9
9
10
10
import (
11
11
"archive/tar"
12
- "bufio"
13
12
"compress/gzip"
14
13
"context"
15
14
"embed"
16
- "encoding/json"
17
15
"fmt"
18
16
"io"
19
17
"os"
@@ -25,6 +23,7 @@ import (
25
23
"syscall"
26
24
"time"
27
25
26
+ "github.com/gitpod-io/gitpod/docker-up/dockerd"
28
27
"github.com/rootless-containers/rootlesskit/pkg/sigproxy"
29
28
sigproxysignal "github.com/rootless-containers/rootlesskit/pkg/sigproxy/signal"
30
29
"github.com/sirupsen/logrus"
@@ -45,6 +44,7 @@ var opts struct {
45
44
UserAccessibleSocket bool
46
45
Verbose bool
47
46
DontWrapNetNS bool
47
+ AutoLogin bool
48
48
}
49
49
50
50
//go:embed docker.tgz
@@ -58,6 +58,7 @@ var aptUpdated = false
58
58
const (
59
59
dockerSocketFN = "/var/run/docker.sock"
60
60
gitpodUserId = 33333
61
+ gitpodGroupId = 33333
61
62
containerIf = "eth0"
62
63
)
63
64
@@ -73,6 +74,7 @@ func main() {
73
74
pflag .BoolVar (& opts .AutoInstall , "auto-install" , true , "auto-install prerequisites (docker)" )
74
75
pflag .BoolVar (& opts .UserAccessibleSocket , "user-accessible-socket" , true , "chmod the Docker socket to make it user accessible" )
75
76
pflag .BoolVar (& opts .DontWrapNetNS , "dont-wrap-netns" , os .Getenv ("WORKSPACEKIT_WRAP_NETNS" ) == "true" , "wrap the Docker daemon in a network namespace" )
77
+ pflag .BoolVar (& opts .AutoLogin , "auto-login" , false , "use content of GITPOD_IMAGE_AUTH to automatically login with the docker daemon" )
76
78
pflag .Parse ()
77
79
78
80
logger := logrus .New ()
@@ -118,7 +120,8 @@ func runWithinNetns() (err error) {
118
120
)
119
121
}
120
122
121
- userArgs , err := userArgs ()
123
+ userArgsValue , _ := os .LookupEnv (DaemonArgs )
124
+ userArgs , err := dockerd .ParseUserArgs (log , userArgsValue )
122
125
if err != nil {
123
126
return xerrors .Errorf ("cannot add user supplied docker args: %w" , err )
124
127
}
@@ -192,98 +195,6 @@ func runWithinNetns() (err error) {
192
195
return nil
193
196
}
194
197
195
- type ConvertUserArg func (arg , value string ) ([]string , error )
196
-
197
- var allowedDockerArgs = map [string ]ConvertUserArg {
198
- "remap-user" : convertRemapUser ,
199
- }
200
-
201
- func userArgs () ([]string , error ) {
202
- userArgs , exists := os .LookupEnv (DaemonArgs )
203
- args := []string {}
204
- if ! exists {
205
- return args , nil
206
- }
207
-
208
- var providedDockerArgs map [string ]string
209
- if err := json .Unmarshal ([]byte (userArgs ), & providedDockerArgs ); err != nil {
210
- return nil , xerrors .Errorf ("unable to deserialize docker args: %w" , err )
211
- }
212
-
213
- for userArg , userValue := range providedDockerArgs {
214
- converter , exists := allowedDockerArgs [userArg ]
215
- if ! exists {
216
- continue
217
- }
218
-
219
- if converter != nil {
220
- cargs , err := converter (userArg , userValue )
221
- if err != nil {
222
- return nil , xerrors .Errorf ("could not convert %v - %v: %w" , userArg , userValue , err )
223
- }
224
- args = append (args , cargs ... )
225
-
226
- } else {
227
- args = append (args , "--" + userArg , userValue )
228
- }
229
- }
230
-
231
- return args , nil
232
- }
233
-
234
- func convertRemapUser (arg , value string ) ([]string , error ) {
235
- id , err := strconv .Atoi (value )
236
- if err != nil {
237
- return nil , err
238
- }
239
-
240
- for _ , f := range []string {"/etc/subuid" , "/etc/subgid" } {
241
- err := adaptSubid (f , id )
242
- if err != nil {
243
- return nil , xerrors .Errorf ("could not adapt subid files: %w" , err )
244
- }
245
- }
246
-
247
- return []string {"--userns-remap" , "gitpod" }, nil
248
- }
249
-
250
- func adaptSubid (oldfile string , id int ) error {
251
- uid , err := os .Open (oldfile )
252
- if err != nil {
253
- return err
254
- }
255
-
256
- newfile , err := os .Create (oldfile + ".new" )
257
- if err != nil {
258
- return err
259
- }
260
-
261
- mappingFmt := func (username string , id int , size int ) string { return fmt .Sprintf ("%s:%d:%d\n " , username , id , size ) }
262
-
263
- if id != 0 {
264
- newfile .WriteString (mappingFmt ("gitpod" , 1 , id ))
265
- newfile .WriteString (mappingFmt ("gitpod" , gitpodUserId , 1 ))
266
- } else {
267
- newfile .WriteString (mappingFmt ("gitpod" , gitpodUserId , 1 ))
268
- newfile .WriteString (mappingFmt ("gitpod" , 1 , gitpodUserId - 1 ))
269
- newfile .WriteString (mappingFmt ("gitpod" , gitpodUserId + 1 , 32200 )) // map rest of user ids in the user namespace
270
- }
271
-
272
- uidScanner := bufio .NewScanner (uid )
273
- for uidScanner .Scan () {
274
- l := uidScanner .Text ()
275
- if ! strings .HasPrefix (l , "gitpod" ) {
276
- newfile .WriteString (l + "\n " )
277
- }
278
- }
279
-
280
- if err = os .Rename (newfile .Name (), oldfile ); err != nil {
281
- return err
282
- }
283
-
284
- return nil
285
- }
286
-
287
198
var prerequisites = map [string ]func () error {
288
199
"dockerd" : installDocker ,
289
200
"docker-compose" : installDockerCompose ,
@@ -353,7 +264,8 @@ func installDocker() error {
353
264
}
354
265
355
266
switch hdr .Typeflag {
356
- case tar .TypeReg , tar .TypeRegA :
267
+
268
+ case tar .TypeReg , tar .TypeRegA : //lint:ignore SA1019 backwards compatibility
357
269
file , err := os .OpenFile (dstpath , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , mode )
358
270
if err != nil {
359
271
return xerrors .Errorf ("unable to create file: %v" , err )
@@ -480,12 +392,12 @@ func detectRuncVersion(output string) (major, minor int, err error) {
480
392
481
393
major , err = strconv .Atoi (n [0 ])
482
394
if err != nil {
483
- return 0 , 0 , xerrors .Errorf ("could not parse major %s: %w" , n [0 ])
395
+ return 0 , 0 , xerrors .Errorf ("could not parse major %s: %w" , n [0 ], err )
484
396
}
485
397
486
398
minor , err = strconv .Atoi (n [1 ])
487
399
if err != nil {
488
- return 0 , 0 , xerrors .Errorf ("could not parse minor %s: %w" , n [1 ])
400
+ return 0 , 0 , xerrors .Errorf ("could not parse minor %s: %w" , n [1 ], err )
489
401
}
490
402
491
403
return major , minor , nil
0 commit comments