@@ -9,11 +9,9 @@ package main
99
1010import (
1111 "archive/tar"
12- "bufio"
1312 "compress/gzip"
1413 "context"
1514 "embed"
16- "encoding/json"
1715 "fmt"
1816 "io"
1917 "os"
@@ -25,6 +23,7 @@ import (
2523 "syscall"
2624 "time"
2725
26+ "github.com/gitpod-io/gitpod/docker-up/dockerd"
2827 "github.com/rootless-containers/rootlesskit/pkg/sigproxy"
2928 sigproxysignal "github.com/rootless-containers/rootlesskit/pkg/sigproxy/signal"
3029 "github.com/sirupsen/logrus"
@@ -45,6 +44,7 @@ var opts struct {
4544 UserAccessibleSocket bool
4645 Verbose bool
4746 DontWrapNetNS bool
47+ AutoLogin bool
4848}
4949
5050//go:embed docker.tgz
@@ -58,6 +58,7 @@ var aptUpdated = false
5858const (
5959 dockerSocketFN = "/var/run/docker.sock"
6060 gitpodUserId = 33333
61+ gitpodGroupId = 33333
6162 containerIf = "eth0"
6263)
6364
@@ -73,6 +74,7 @@ func main() {
7374 pflag .BoolVar (& opts .AutoInstall , "auto-install" , true , "auto-install prerequisites (docker)" )
7475 pflag .BoolVar (& opts .UserAccessibleSocket , "user-accessible-socket" , true , "chmod the Docker socket to make it user accessible" )
7576 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" )
7678 pflag .Parse ()
7779
7880 logger := logrus .New ()
@@ -118,7 +120,8 @@ func runWithinNetns() (err error) {
118120 )
119121 }
120122
121- userArgs , err := userArgs ()
123+ userArgsValue , _ := os .LookupEnv (DaemonArgs )
124+ userArgs , err := dockerd .ParseUserArgs (log , userArgsValue )
122125 if err != nil {
123126 return xerrors .Errorf ("cannot add user supplied docker args: %w" , err )
124127 }
@@ -192,98 +195,6 @@ func runWithinNetns() (err error) {
192195 return nil
193196}
194197
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-
287198var prerequisites = map [string ]func () error {
288199 "dockerd" : installDocker ,
289200 "docker-compose" : installDockerCompose ,
@@ -353,7 +264,8 @@ func installDocker() error {
353264 }
354265
355266 switch hdr .Typeflag {
356- case tar .TypeReg , tar .TypeRegA :
267+
268+ case tar .TypeReg , tar .TypeRegA : //lint:ignore SA1019 backwards compatibility
357269 file , err := os .OpenFile (dstpath , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , mode )
358270 if err != nil {
359271 return xerrors .Errorf ("unable to create file: %v" , err )
@@ -480,12 +392,12 @@ func detectRuncVersion(output string) (major, minor int, err error) {
480392
481393 major , err = strconv .Atoi (n [0 ])
482394 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 )
484396 }
485397
486398 minor , err = strconv .Atoi (n [1 ])
487399 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 )
489401 }
490402
491403 return major , minor , nil
0 commit comments