Skip to content

Commit 3fd86ad

Browse files
author
OpenShift Bot
committed
Merge pull request #471 from csrwng/user_spec
Merged by openshift-bot
2 parents 46477e0 + 5b3f5ca commit 3fd86ad

File tree

3 files changed

+72
-23
lines changed

3 files changed

+72
-23
lines changed

pkg/docker/util.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package docker
22

33
import (
4+
"bufio"
45
"fmt"
56
"io"
67
"os"
78
"path/filepath"
9+
"regexp"
810
"strings"
911

10-
"bufio"
11-
1212
client "github.com/fsouza/go-dockerclient"
1313
"github.com/golang/glog"
1414
"github.com/openshift/source-to-image/pkg/api"
@@ -202,10 +202,11 @@ func CheckAllowedUser(d Docker, imageName string, uids user.RangeList, isOnbuild
202202
if uids == nil || uids.Empty() {
203203
return nil
204204
}
205-
imageUser, err := d.GetImageUser(imageName)
205+
imageUserSpec, err := d.GetImageUser(imageName)
206206
if err != nil {
207207
return err
208208
}
209+
imageUser := extractUser(imageUserSpec)
209210
if !user.IsUserAllowed(imageUser, &uids) {
210211
return errors.NewBuilderUserNotAllowedError(imageName, false)
211212
}
@@ -214,13 +215,41 @@ func CheckAllowedUser(d Docker, imageName string, uids user.RangeList, isOnbuild
214215
if err != nil {
215216
return err
216217
}
217-
if !user.IsOnbuildAllowed(cmds, &uids) {
218+
if !isOnbuildAllowed(cmds, &uids) {
218219
return errors.NewBuilderUserNotAllowedError(imageName, true)
219220
}
220221
}
221222
return nil
222223
}
223224

225+
var dockerLineDelim = regexp.MustCompile(`[\t\v\f\r ]+`)
226+
227+
// isOnbuildAllowed checks a list of Docker ONBUILD instructions for
228+
// user directives. It ensures that any users specified by the directives
229+
// falls within the specified range list of users.
230+
func isOnbuildAllowed(directives []string, allowed *user.RangeList) bool {
231+
for _, line := range directives {
232+
parts := dockerLineDelim.Split(line, 2)
233+
if strings.ToLower(parts[0]) != "user" {
234+
continue
235+
}
236+
uname := extractUser(parts[1])
237+
if !user.IsUserAllowed(uname, allowed) {
238+
return false
239+
}
240+
}
241+
return true
242+
}
243+
244+
func extractUser(userSpec string) string {
245+
user := userSpec
246+
if strings.Contains(user, ":") {
247+
parts := strings.SplitN(userSpec, ":", 2)
248+
user = parts[0]
249+
}
250+
return strings.TrimSpace(user)
251+
}
252+
224253
// IsReachable returns true if the Docker daemon is reachable from s2i
225254
func IsReachable(config *api.Config) bool {
226255
d, err := New(config.DockerConfig, config.PullAuthentication)

pkg/docker/util_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,45 @@ func TestCheckAllowedUser(t *testing.T) {
7878
onbuild: []string{"USER 501", "VOLUME /data"},
7979
expectErr: false,
8080
},
81+
{
82+
name: "AllowedUIDs is set, numeric user with group",
83+
allowedUIDs: rangeList("1-"),
84+
user: "5:5000",
85+
expectErr: false,
86+
},
87+
{
88+
name: "AllowedUIDs is set, numeric user with named group",
89+
allowedUIDs: rangeList("1-"),
90+
user: "5:group",
91+
expectErr: false,
92+
},
93+
{
94+
name: "AllowedUIDs is set, named user with group",
95+
allowedUIDs: rangeList("1-"),
96+
user: "root:wheel",
97+
expectErr: true,
98+
},
99+
{
100+
name: "AllowedUIDs is set, numeric user, onbuild user with group",
101+
allowedUIDs: rangeList("1-"),
102+
user: "200",
103+
onbuild: []string{"RUN echo \"hello world\"", "USER 10:100"},
104+
expectErr: false,
105+
},
106+
{
107+
name: "AllowedUIDs is set, numeric user, onbuild named user with group",
108+
allowedUIDs: rangeList("1-"),
109+
user: "200",
110+
onbuild: []string{"RUN echo \"hello world\"", "USER root:wheel"},
111+
expectErr: true,
112+
},
113+
{
114+
name: "AllowedUIDs is set, numeric user, onbuild user with named group",
115+
allowedUIDs: rangeList("1-"),
116+
user: "200",
117+
onbuild: []string{"RUN echo \"hello world\"", "USER 10:wheel"},
118+
expectErr: false,
119+
},
81120
}
82121

83122
for _, tc := range tests {

pkg/util/user/rangelist.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package user
22

33
import (
4-
"regexp"
54
"strconv"
65
"strings"
76
)
@@ -82,21 +81,3 @@ func IsUserAllowed(user string, allowed *RangeList) bool {
8281
}
8382
return allowed.Contains(uid)
8483
}
85-
86-
var dockerLineDelim = regexp.MustCompile(`[\t\v\f\r ]+`)
87-
88-
// IsOnbuildAllowed checks a list of Docker ONBUILD instructions for
89-
// user directives. It ensures that any users specified by the directives
90-
// falls within the specified range list of users.
91-
func IsOnbuildAllowed(directives []string, allowed *RangeList) bool {
92-
for _, line := range directives {
93-
parts := dockerLineDelim.Split(line, 2)
94-
if strings.ToLower(parts[0]) != "user" {
95-
continue
96-
}
97-
if !IsUserAllowed(parts[1], allowed) {
98-
return false
99-
}
100-
}
101-
return true
102-
}

0 commit comments

Comments
 (0)