@@ -35,7 +35,7 @@ import (
35
35
36
36
var log * logrus.Entry
37
37
38
- const DaemonArgs = "DOCKER_DAEMON_ARGS "
38
+ const DaemonArgs = "DOCKERD_ARGS "
39
39
40
40
var opts struct {
41
41
RuncFacade bool
@@ -56,6 +56,7 @@ var aptUpdated = false
56
56
57
57
const (
58
58
dockerSocketFN = "/var/run/docker.sock"
59
+ gitpodUserId = 33333
59
60
)
60
61
61
62
func main () {
@@ -115,10 +116,11 @@ func runWithinNetns() (err error) {
115
116
)
116
117
}
117
118
118
- args , err = setUserArgs ( args )
119
+ userArgs , err := userArgs ( )
119
120
if err != nil {
120
121
return xerrors .Errorf ("cannot add user supplied docker args: %w" , err )
121
122
}
123
+ args = append (args , userArgs ... )
122
124
123
125
if listenFDs > 0 {
124
126
os .Setenv ("LISTEN_PID" , strconv .Itoa (os .Getpid ()))
@@ -175,12 +177,15 @@ func runWithinNetns() (err error) {
175
177
return nil
176
178
}
177
179
178
- var allowedDockerArgs = map [string ]string {
179
- "remap-user" : "userns-remap" ,
180
+ type ConvertUserArg func (arg , value string ) ([]string , error )
181
+
182
+ var allowedDockerArgs = map [string ]ConvertUserArg {
183
+ "remap-user" : convertRemapUser ,
180
184
}
181
185
182
- func setUserArgs ( args [] string ) ([]string , error ) {
186
+ func userArgs ( ) ([]string , error ) {
183
187
userArgs , exists := os .LookupEnv (DaemonArgs )
188
+ args := []string {}
184
189
if ! exists {
185
190
return args , nil
186
191
}
@@ -191,25 +196,18 @@ func setUserArgs(args []string) ([]string, error) {
191
196
}
192
197
193
198
for userArg , userValue := range providedDockerArgs {
194
- mapped , exists := allowedDockerArgs [userArg ]
199
+ converter , exists := allowedDockerArgs [userArg ]
195
200
if ! exists {
196
201
continue
197
202
}
198
203
199
- if userArg == "remap-user" {
200
- id , err := strconv . Atoi ( userValue )
204
+ if converter != nil {
205
+ cargs , err := converter ( userArg , userValue )
201
206
if err != nil {
202
- return nil , err
203
- }
204
-
205
- for _ , f := range []string {"/etc/subuid" , "/etc/subgid" } {
206
- err := adaptSubid (f , id )
207
- if err != nil {
208
- return nil , xerrors .Errorf ("could not adapt subid files: %w" , err )
209
- }
207
+ return nil , xerrors .Errorf ("could not convert %v - %v: %w" , userArg , userValue , err )
210
208
}
209
+ args = append (args , cargs ... )
211
210
212
- args = append (args , "--" + mapped , "gitpod" )
213
211
} else {
214
212
args = append (args , "--" + userArg , userValue )
215
213
}
@@ -218,6 +216,22 @@ func setUserArgs(args []string) ([]string, error) {
218
216
return args , nil
219
217
}
220
218
219
+ func convertRemapUser (arg , value string ) ([]string , error ) {
220
+ id , err := strconv .Atoi (value )
221
+ if err != nil {
222
+ return nil , err
223
+ }
224
+
225
+ for _ , f := range []string {"/etc/subuid" , "/etc/subgid" } {
226
+ err := adaptSubid (f , id )
227
+ if err != nil {
228
+ return nil , xerrors .Errorf ("could not adapt subid files: %w" , err )
229
+ }
230
+ }
231
+
232
+ return []string {"--userns-remap" , "gitpod" }, nil
233
+ }
234
+
221
235
func adaptSubid (oldfile string , id int ) error {
222
236
uid , err := os .Open (oldfile )
223
237
if err != nil {
@@ -229,14 +243,15 @@ func adaptSubid(oldfile string, id int) error {
229
243
return err
230
244
}
231
245
232
- if id != 0 {
233
- newfile .WriteString (fmt .Sprintf ("gitpod:%d:%d\n " , 1 , id ))
234
- newfile .WriteString ("gitpod:33333:1\n " )
246
+ mappingFmt := func (username string , id int , size int ) string { return fmt .Sprintf ("%s:%d:%d\n " , username , id , size ) }
235
247
248
+ if id != 0 {
249
+ newfile .WriteString (mappingFmt ("gitpod" , 1 , id ))
250
+ newfile .WriteString (mappingFmt ("gitpod" , gitpodUserId , 1 ))
236
251
} else {
237
- newfile .WriteString ("gitpod:33333:1 \n " )
238
- newfile .WriteString (fmt . Sprintf ("gitpod:%d:%d \n " , 1 , 33332 ))
239
- newfile .WriteString (fmt . Sprintf ("gitpod:%d:%d \n " , 33334 , 32200 ))
252
+ newfile .WriteString (mappingFmt ( "gitpod" , gitpodUserId , 1 ) )
253
+ newfile .WriteString (mappingFmt ("gitpod" , 1 , gitpodUserId - 1 ))
254
+ newfile .WriteString (mappingFmt ("gitpod" , gitpodUserId + 1 , 32200 )) // map rest of user ids in the user namespace
240
255
}
241
256
242
257
uidScanner := bufio .NewScanner (uid )
0 commit comments