-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
172 lines (162 loc) · 4.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"os"
"log"
"github.com/zpatrick/go-config"
"github.com/codegangsta/cli"
"github.com/qnib/doxy/proxy"
"strings"
)
var (
dockerSocketFlag = cli.StringFlag{
Name: "docker-socket",
Value: proxy.DOCKER_SOCKET,
Usage: "Docker host to connect to.",
EnvVar: "DOXY_DOCKER_SOCKET",
}
proxySocketFlag = cli.StringFlag{
Name: "proxy-socket",
Value: proxy.PROXY_SOCKET,
Usage: "Proxy socket to be created",
EnvVar: "DOXY_PROXY_SOCKET",
}
proxyPatternKey = cli.StringFlag{
Name: "pattern-key",
Value: "default",
Usage: "pattern key predefined",
EnvVar: "DOXY_PATTERN_KEY",
}
gpuEnabled = cli.BoolFlag{
Name: "gpu",
Usage: "Map devices, bind-mounts and environment into each container to allow GPU usage",
EnvVar: "DOXY_GPU_ENABLED",
}
debugFlag = cli.BoolFlag{
Name: "debug",
Usage: "Print proxy requests",
EnvVar: "DOXY_DEBUG",
}
bindAddFlag = cli.StringFlag{
Name: "add-binds",
Usage: "Comma separated list of bind-mounts to add",
EnvVar: "DOXY_ADDITIONAL_BINDS",
}
devMapFlag = cli.StringFlag{
Name: "device-mappings",
Usage: "Comma separated list of device mappings",
EnvVar: "DOXY_DEVICE_MAPPINGS",
}
patternFileFlag = cli.StringFlag{
Name: "pattern-file",
Value: proxy.PATTERN_FILE,
Usage: "File holding line-separated regex-patterns to be allowed (comments allowed, use #)",
EnvVar: "DOXY_PATTERN_FILE",
}
pinUserBool = cli.BoolFlag{
Name: "pin-user",
Usage: "Pin user within container to the UID calling the command",
EnvVar: "DOXY_USER_PINNING_ENABLED",
}
pinUserFlag = cli.StringFlag{
Name: "user",
Usage: "Overwrite `--user` with given value (if pin-user is set)",
EnvVar: "DOXY_USER",
}
cudaLibPathFlag = cli.StringFlag{
Name: "cuda-lib-path",
Usage: "Path to cuda libraries.",
EnvVar: "DOXY_CUDA_LIB_PATH",
}
deviceFileFlag = cli.StringFlag{
Name: "device-file",
Value: proxy.DEVICE_FILE,
Usage: "File holding line-separated devices to be mapped in when in (GPU|HPC) mode (comments allowed, use #)",
EnvVar: "DOXY_DEVICE_FILE",
}
)
func EvalOptions(cfg *config.Config) (po []proxy.ProxyOption) {
proxySock, _ := cfg.String("proxy-socket")
po = append(po, proxy.WithProxySocket(proxySock))
dockerSock, _ := cfg.String("docker-socket")
po = append(po, proxy.WithDockerSocket(dockerSock))
debug, _ := cfg.Bool("debug")
po = append(po, proxy.WithDebugValue(debug))
gpu, _ := cfg.Bool("gpu")
po = append(po, proxy.WithGpuValue(gpu))
devMaps, _ := cfg.String("device-mappings")
po = append(po, proxy.WithDevMappings(strings.Split(devMaps,",")))
pinUser, _ := cfg.String("user")
pinUserBool, _ := cfg.Bool("pin-user")
po = append(po, proxy.WithPinUser(pinUserBool, pinUser))
cudalibPath, _ := cfg.String("cuda-lib-path")
po = append(po, proxy.WithCudaLibPath(cudalibPath))
return
}
func EvalPatternOpts(cfg *config.Config) (proxy.ProxyOption) {
patternsFile, _ := cfg.String("pattern-file")
reader, err := os.Open(patternsFile)
defer reader.Close()
patterns := []string{}
if err != nil {
patternsKey, _ := cfg.String("pattern-key")
if patterns, ok := proxy.PATTERNS[patternsKey]; ok {
log.Printf("Error reading patterns file '%s', using %s patterns\n", err.Error(), patternsKey)
return proxy.WithPatterns(patterns)
}
log.Printf("Could not find pattern-key '%s'\n", patternsKey)
os.Exit(1)
}
patterns, err = proxy.ReadLineFile(reader)
log.Printf("Patterns read from '%s':\n%s\n", patternsFile, strings.Join(patterns,"\n "))
return proxy.WithPatterns(patterns)
}
func EvalDevicesOpts(cfg *config.Config) (proxy.ProxyOption) {
deviceFile, _ := cfg.String("device-file")
reader, err := os.Open(deviceFile)
defer reader.Close()
devices := []string{}
if err != nil {
return proxy.WithDevMappings([]string{})
}
devices, err = proxy.ReadLineFile(reader)
if err != nil {
log.Printf("Error while reading device file: %s", err.Error())
}
return proxy.WithDevMappings(devices)
}
func EvalBindMountOpts(cfg *config.Config) (proxy.ProxyOption) {
bindStr, _ := cfg.String("add-binds")
bindMounts := strings.Split(bindStr,",")
return proxy.WithBindMounts(bindMounts)
}
func RunApp(ctx *cli.Context) {
log.Printf("[II] Start Version: %s", ctx.App.Version)
cfg := config.NewConfig([]config.Provider{config.NewCLI(ctx, true)})
po := EvalOptions(cfg)
po = append(po, EvalPatternOpts(cfg))
po = append(po, EvalDevicesOpts(cfg))
po = append(po, EvalBindMountOpts(cfg))
p := proxy.NewProxy(po...)
p.Run()
}
func main() {
app := cli.NewApp()
app.Name = "Proxy Docker unix socket to filter out insecure, harmful requests."
app.Usage = "doxy [options]"
app.Version = "0.2.4"
app.Flags = []cli.Flag{
dockerSocketFlag,
proxySocketFlag,
debugFlag,
gpuEnabled,
deviceFileFlag,
patternFileFlag,
proxyPatternKey,
bindAddFlag,
pinUserBool,
pinUserFlag,
cudaLibPathFlag,
}
app.Action = RunApp
app.Run(os.Args)
}