Skip to content

Commit 085f177

Browse files
enhance user-pinning
1 parent 9873d7c commit 085f177

File tree

4 files changed

+66
-34
lines changed

4 files changed

+66
-34
lines changed

main.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ var (
3333
Usage: "Map devices, bind-mounts and environment into each container to allow GPU usage",
3434
EnvVar: "DOXY_GPU_ENABLED",
3535
}
36-
constrainUser = cli.BoolFlag{
37-
Name: "user-pinning",
38-
Usage: "Pin user within container to the UID calling the command",
39-
EnvVar: "DOXY_USER_PINNING_ENABLED",
40-
}
4136
debugFlag = cli.BoolFlag{
4237
Name: "debug",
4338
Usage: "Print proxy requests",
@@ -59,10 +54,15 @@ var (
5954
Usage: "File holding line-separated regex-patterns to be allowed (comments allowed, use #)",
6055
EnvVar: "DOXY_PATTERN_FILE",
6156
}
57+
pinUserBool = cli.BoolFlag{
58+
Name: "pin-user",
59+
Usage: "Pin user within container to the UID calling the command",
60+
EnvVar: "DOXY_USER_PINNING_ENABLED",
61+
}
6262
pinUserFlag = cli.StringFlag{
63-
Name: "pin-user",
64-
Usage: "Overwrite `--user` with given value",
65-
EnvVar: "DOXY_PIN_USER",
63+
Name: "user",
64+
Usage: "Overwrite `--user` with given value (if pin-user is set)",
65+
EnvVar: "DOXY_USER",
6666
}
6767
deviceFileFlag = cli.StringFlag{
6868
Name: "device-file",
@@ -83,8 +83,9 @@ func EvalOptions(cfg *config.Config) (po []proxy.ProxyOption) {
8383
po = append(po, proxy.WithGpuValue(gpu))
8484
devMaps, _ := cfg.String("device-mappings")
8585
po = append(po, proxy.WithDevMappings(strings.Split(devMaps,",")))
86-
pinUser, _ := cfg.String("pin-user")
87-
po = append(po, proxy.WithPinUserValue(pinUser))
86+
pinUser, _ := cfg.String("user")
87+
pinUserBool, _ := cfg.Bool("pin-user")
88+
po = append(po, proxy.WithPinUser(pinUserBool, pinUser))
8889
return
8990
}
9091

@@ -155,6 +156,7 @@ func main() {
155156
patternFileFlag,
156157
proxyPatternKey,
157158
bindAddFlag,
159+
pinUserBool,
158160
pinUserFlag,
159161
}
160162
app.Action = RunApp

proxy/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ var (
4848
)
4949

5050
type Proxy struct {
51+
po ProxyOptions
5152
dockerSocket, newSocket, pinUser string
52-
debug, gpu bool
53+
debug, gpu, pinUserEnabled bool
5354
patterns []string
5455
bindMounts,devMappings []string
5556
}
@@ -65,6 +66,8 @@ func NewProxy(opts ...ProxyOption) Proxy {
6566
debug: options.Debug,
6667
gpu: options.Gpu,
6768
pinUser: options.PinUser,
69+
pinUserEnabled: options.PinUserEnabled,
70+
po: options,
6871
patterns: options.Patterns,
6972
bindMounts: options.BindMounts,
7073
devMappings: options.DevMappings,
@@ -77,12 +80,13 @@ func (p *Proxy) GetOptions() map[string]interface{} {
7780
"proxy-socket": p.newSocket,
7881
"debug": p.debug,
7982
"patterns": p.patterns,
83+
"pin-user": p.pinUserEnabled,
8084
}
8185
return opt
8286
}
8387

8488
func (p *Proxy) Run() {
85-
upstream := NewUpstream(p.dockerSocket, p.patterns, p.bindMounts, p.devMappings, p.gpu, p.pinUser)
89+
upstream := NewUpstream(p.dockerSocket, p.patterns, p.bindMounts, p.devMappings, p.gpu, p.pinUser, p.pinUserEnabled)
8690
sigc := make(chan os.Signal, 1)
8791
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
8892
l, err := ListenToNewSock(p.newSocket, sigc)

proxy/options.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package proxy
22

3+
34
type ProxyOptions struct {
4-
DockerSocket string
5-
ProxySocket string
6-
PinUser string
7-
Debug,Gpu bool
8-
Patterns []string
9-
BindMounts []string
10-
DevMappings []string
5+
DockerSocket,ProxySocket,PinUser string
6+
Debug,Gpu,PinUserEnabled bool
7+
Patterns,BindMounts,DevMappings []string
118
}
129

1310
var defaultProxyOptions = ProxyOptions{
1411
DockerSocket: DOCKER_SOCKET,
1512
ProxySocket: PROXY_SOCKET,
1613
PinUser: "",
14+
PinUserEnabled: false,
1715
Debug: false,
1816
Gpu: false,
1917
Patterns: []string{},
@@ -23,9 +21,10 @@ var defaultProxyOptions = ProxyOptions{
2321

2422
type ProxyOption func(*ProxyOptions)
2523

26-
func WithPinUserValue(pu string) ProxyOption {
24+
func WithPinUser(pub bool, pu string) ProxyOption {
2725
return func(o *ProxyOptions) {
2826
o.PinUser = pu
27+
o.PinUserEnabled = pub
2928
}
3029
}
3130
func WithDockerSocket(s string) ProxyOption {

proxy/proxy.go

+41-14
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121

2222
// UpStream creates upstream handler struct
2323
type UpStream struct {
24-
Name string
25-
proxy http.Handler
24+
Name string
25+
proxy http.Handler
2626
// TODO: Kick out separat config options and use more generic one
27-
allowed []*regexp.Regexp
28-
bindMounts []string
29-
devMappings []string
30-
gpu bool
31-
pinUser string
27+
allowed []*regexp.Regexp
28+
bindMounts []string
29+
devMappings []string
30+
gpu bool
31+
pinUser string
32+
pinUserEnabled bool
3233
}
3334

3435
// UnixSocket just provides the path, so that I can test it
@@ -64,8 +65,27 @@ func newReverseProxy(dial func(network, addr string) (net.Conn, error)) *httputi
6465
}
6566
}
6667

68+
func NewUpstreamPO(po ProxyOptions) *UpStream {
69+
us := NewUnixSocket(po.ProxySocket)
70+
a := []*regexp.Regexp{}
71+
for _, r := range po.Patterns {
72+
p, _ := regexp.Compile(r)
73+
a = append(a, p)
74+
}
75+
upstream := &UpStream{
76+
Name: po.ProxySocket,
77+
proxy: newReverseProxy(us.connectSocket),
78+
allowed: a,
79+
bindMounts: po.BindMounts,
80+
devMappings: po.DevMappings,
81+
gpu: po.Gpu,
82+
pinUser: po.PinUser,
83+
pinUserEnabled: po.PinUserEnabled,
84+
}
85+
return upstream
86+
}
6787
// NewUpstream returns a new socket (magic)
68-
func NewUpstream(socket string, regs []string, binds []string, devs []string, gpu bool, pinUser string) *UpStream {
88+
func NewUpstream(socket string, regs []string, binds []string, devs []string, gpu bool, pinUser string, pinUserB bool) *UpStream {
6989
us := NewUnixSocket(socket)
7090
a := []*regexp.Regexp{}
7191
for _, r := range regs {
@@ -80,6 +100,7 @@ func NewUpstream(socket string, regs []string, binds []string, devs []string, gp
80100
devMappings: devs,
81101
gpu: gpu,
82102
pinUser: pinUser,
103+
pinUserEnabled: pinUserB,
83104
}
84105
return upstream
85106
}
@@ -141,14 +162,20 @@ func (u *UpStream) ServeHTTP(w http.ResponseWriter, req *http.Request) {
141162
config.Env = append(config.Env, "PATH=/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
142163
config.Env = append(config.Env, "LD_LIBRARY_PATH=/usr/local/nvidia/")
143164
}
144-
if u.pinUser != "" {
165+
if u.pinUserEnabled {
166+
fmt.Print("Alter User setting ")
145167
// TODO: Should depend on calling user from syscall.GetsockoptUcred()
146-
if config.User != "" {
147-
fmt.Printf("Overwrite User with '%s', was '%s'\n", u.pinUser, config.User)
148-
} else {
149-
fmt.Printf("Overwrite User with '%s'\n", u.pinUser)
168+
switch {
169+
case config.User != "" && u.pinUser == "":
170+
fmt.Printf(" - Remove setting User, was '%s'\n", config.User)
171+
config.User = ""
172+
case config.User != "" && u.pinUser != "":
173+
fmt.Printf(" - Overwrite User with '%s', was '%s'\n", u.pinUser, config.User)
174+
config.User = u.pinUser
175+
default:
176+
fmt.Printf(" - Set User to '%s'\n", u.pinUser)
177+
config.User = u.pinUser
150178
}
151-
config.User = u.pinUser
152179
}
153180
for _, bMount := range u.bindMounts {
154181
if bMount == "" {

0 commit comments

Comments
 (0)