-
Notifications
You must be signed in to change notification settings - Fork 7
/
docker-proxy-acl.go
157 lines (131 loc) · 3.82 KB
/
docker-proxy-acl.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
package main
import "github.com/namsral/flag"
import "fmt"
import "os"
import "os/signal"
import "syscall"
import "net"
import "net/http"
import "github.com/gorilla/mux"
import "github.com/titpetric/docker-proxy-acl/proxy"
type stringSlice []string;
func (s *stringSlice) String() string {
return fmt.Sprintf("%d", *s)
}
func (s *stringSlice) Set(value string) error {
fmt.Sprintf("Allowing endpont: %s\n", value);
*s = append(*s, value);
return nil;
}
func main() {
fs := flag.NewFlagSetWithEnvPrefix(os.Args[0], "GO", 0)
var (
allowed stringSlice;
allowedMap map[string]bool = make(map[string]bool);
filename = fs.String("filename", "/tmp/docker-proxy-acl/docker.sock", "Location of socket file");
)
fs.Var(&allowed, "a", "Allowed location pattern prefix");
fs.Parse(os.Args[1:])
if len(allowed) < 1 {
fmt.Println("Need at least 1 argument for -a: [containers, networks, version, info, ping]");
os.Exit(0);
}
for _, s := range allowed {
allowedMap[s] = true;
}
var routers [2]*mux.Router;
routers[0] = mux.NewRouter();
routers[1] = routers[0].PathPrefix("/{version:[v][0-9]+[.][0-9]+}").Subrouter();
upstream := proxy.NewSocket("/var/run/docker.sock");
if allowedMap["containers"] {
fmt.Printf("Registering container handlers\n");
for _, m := range routers {
containers := m.PathPrefix("/containers").Subrouter();
containers.HandleFunc("/json", upstream.Pass());
containers.HandleFunc("/{name}/json", upstream.Pass());
}
}
if allowedMap["images"] {
fmt.Printf("Registering images handlers\n");
for _, m := range routers {
containers := m.PathPrefix("/images").Subrouter();
containers.HandleFunc("/json", upstream.Pass());
containers.HandleFunc("/{name}/json", upstream.Pass());
containers.HandleFunc("/{name}/history", upstream.Pass());
}
}
if allowedMap["volumes"] {
fmt.Printf("Registering volumes handlers\n");
for _, m := range routers {
m.HandleFunc("/volumes", upstream.Pass());
m.HandleFunc("/volumes/{name}", upstream.Pass());
}
}
if allowedMap["networks"] {
fmt.Printf("Registering networks handlers\n");
for _, m := range routers {
m.HandleFunc("/networks", upstream.Pass());
m.HandleFunc("/networks/{name}", upstream.Pass());
}
}
if allowedMap["services"] {
fmt.Printf("Registering services handlers\n");
for _, m := range routers {
m.HandleFunc("/services", upstream.Pass());
m.HandleFunc("/services/{name}", upstream.Pass());
}
}
if allowedMap["tasks"] {
fmt.Printf("Registering tasks handlers\n");
for _, m := range routers {
m.HandleFunc("/tasks", upstream.Pass());
m.HandleFunc("/tasks/{name}", upstream.Pass());
}
}
if allowedMap["events"] {
fmt.Printf("Registering events handlers\n");
for _, m := range routers {
m.HandleFunc("/events", upstream.PassStream());
}
}
if allowedMap["version"] {
fmt.Printf("Registering version handlers\n");
for _, m := range routers {
m.HandleFunc("/version", upstream.Pass());
}
}
if allowedMap["info"] {
fmt.Printf("Registering info handlers\n");
for _, m := range routers {
m.HandleFunc("/info", upstream.Pass());
}
}
if allowedMap["ping"] {
fmt.Printf("Registering ping handlers\n");
for _, m := range routers {
m.HandleFunc("/_ping", upstream.Pass());
}
}
http.Handle("/", routers[0]);
l, err := net.Listen("unix", *filename)
os.Chmod(*filename, 0666);
// Looking up group ids coming up for Go 1.7
// https://github.com/golang/go/issues/2617
fmt.Println("[docker-proxy-acl] Listening on " + *filename);
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c chan os.Signal) {
sig := <-c
fmt.Printf("[docker-proxy-acl] Caught signal %s: shutting down.\n", sig)
l.Close()
os.Exit(0)
}(sigc)
if err != nil {
panic(err)
} else {
err := http.Serve(l, nil)
if err != nil {
panic(err)
}
}
}