-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
340 lines (297 loc) · 8.32 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"strconv"
"strings"
"github.com/coreos/go-iptables/iptables"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
func main() {
IncomingInterface := flag.String("iface", "eth0", "Incoming network interface on host")
Subnet := flag.String("network", "::/0", "Created chain just cares about this subnet")
DefaultDenyRules := flag.Bool("defaults", false, "Add some default deny rules")
ChainName := flag.String("chain", "WHALEGUARD", "Set name of iptables chain to manage")
DockerLabelFilter := flag.String("label", "whaleguard=true", "Discovery only container attached with this label")
flag.Parse()
// check interface exists before starting
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
interfaceError := true
for _, i := range interfaces {
if i.Name == *IncomingInterface {
interfaceError = false
}
}
if interfaceError {
log.Fatal(fmt.Sprintf("Network interface not found: %s", *IncomingInterface))
}
_, ipnet, err := net.ParseCIDR(*Subnet)
if err != nil {
log.Fatal(fmt.Sprintf("Could not parse network %s", *Subnet))
}
ipt, err := iptables.NewWithProtocol(iptables.ProtocolIPv6)
if err != nil {
panic(err)
}
fw := Firewall{
IPTables: ipt,
ChainName: *ChainName,
Table: "filter",
Interface: *IncomingInterface, // incoming interface before routing
Network: ipnet,
}
// enforce older api version
// os.Setenv("DOCKER_API_VERSION", "1.26")
cl, err := client.NewEnvClient()
if err != nil {
panic(err)
}
containerFilterArgs := filters.NewArgs()
if *DockerLabelFilter != "" {
containerFilterArgs.Add("label", *DockerLabelFilter)
}
if err = fw.InitializeChain(*DefaultDenyRules); err != nil {
panic(err)
}
log.Println("Synchronize with docker host")
container, err := cl.ContainerList(context.Background(), types.ContainerListOptions{
Filters: containerFilterArgs,
})
if err != nil {
panic(err)
}
for _, c := range container {
err := fw.AddContainer(c.ID, cl)
if err != nil {
log.Println(err)
}
}
ctx := context.Background()
containerFilterArgs.Add("event", "start")
containerFilterArgs.Add("event", "die")
containerFilterArgs.Add("type", "container")
chanEvents, chanErrors := cl.Events(ctx, types.EventsOptions{
Filters: containerFilterArgs,
})
log.Println("Watch for container events")
for {
select {
case event := <-chanEvents:
logInfo := fmt.Sprintf("container_id=%s event_action=%s", event.Actor.ID, event.Action)
switch event.Action {
case "start":
err := fw.AddContainer(event.ID, cl)
if err != nil {
log.Printf("Failed to add container: %v %s\n", err, logInfo)
}
case "die":
log.Printf("Remove rules for container %s\n", logInfo)
err := fw.DropRules(event.Actor.ID)
if err != nil {
log.Printf("Failed to remove container: %v %s\n", err, logInfo)
}
default:
log.Printf("Unhandled event action %s\n", logInfo)
}
case errors := <-chanErrors:
log.Panic(errors.Error())
return
}
}
}
// PortProto represents a Port with Protocol (udp/tcp)
type PortProto struct {
Port string
Proto string
}
// ParsePortProto parses a string combination (Port/Protocol) to create a new PortProto
// Examples: 123/udp, 123
func ParsePortProto(p string) (*PortProto, error) {
ppraw := strings.SplitN(p, "/", 2)
proto := "tcp"
if len(ppraw) == 2 {
proto = strings.ToLower(strings.TrimSpace(ppraw[1]))
}
if proto != "tcp" && proto != "udp" {
return nil, fmt.Errorf("unsupported protocol provided in label: %s", proto)
}
// ensure we got an integer
pi, err := strconv.Atoi(strings.TrimSpace(ppraw[0]))
if err != nil {
return nil, fmt.Errorf("could not parse port '%s' in label: %v", p, err)
}
return &PortProto{
Port: strconv.Itoa(pi),
Proto: proto,
}, nil
}
// Firewall represents an iptables chain
type Firewall struct {
IPTables *iptables.IPTables
ChainName string
Table string
Interface string
Network *net.IPNet
}
// InitializeChain ensures the working chain is correctly setuped and clears old rules in the working chain
func (f *Firewall) InitializeChain(defaults bool) error {
var err error
chains, err := f.IPTables.ListChains(f.Table)
if err != nil {
return err
}
exists := false
for _, a := range chains {
if a == f.ChainName {
exists = true
}
}
if !exists {
if err = f.IPTables.NewChain(f.Table, f.ChainName); err != nil {
return err
}
} else {
// Cleanup the chain to start fresh
if err = f.IPTables.ClearChain(f.Table, f.ChainName); err != nil {
return err
}
}
rulespec := []string{"-j", f.ChainName, "-i", f.Interface}
if f.Network != nil {
rulespec = append(rulespec, "--dst", f.Network.String())
}
// Ensure new chain will be used as first
err = f.InsertUnique("INPUT", rulespec...)
if err != nil {
return err
}
// Attach to FORWARD to catch routed packets
err = f.InsertUnique("FORWARD", rulespec...)
if err != nil {
return err
}
// initialize some default deny rules here?
if defaults {
// allow related traffic
err := f.IPTables.Append(f.Table, f.ChainName, "-m", "state", "--state", "ESTABLISHED,RELATED", "-j", "ACCEPT")
if err != nil {
return err
}
// allow v6 icmp packages
err = f.IPTables.Append(f.Table, f.ChainName, "-p", "ipv6-icmp", "-j", "ACCEPT")
if err != nil {
return err
}
// last but not least, reject everything else
err = f.IPTables.Append(f.Table, f.ChainName, "-j", "REJECT", "--reject-with", "icmp6-adm-prohibited")
if err != nil {
return err
}
}
return nil
}
// AddContainer retrieves the container information (ip and ports) and adds the specific allow rules
func (f *Firewall) AddContainer(containerID string, client *client.Client) error {
info, err := client.ContainerInspect(context.Background(), containerID)
if err != nil {
return err
}
var ports []PortProto
if labelports, ok := info.Config.Labels["whaleguard.port"]; ok {
for _, p := range strings.Split(labelports, ",") {
pp, err := ParsePortProto(p)
if err != nil {
log.Print(err.Error())
continue
}
ports = append(ports, *pp)
}
} else {
// use exposed port
for k := range info.NetworkSettings.Ports {
ports = append(ports, PortProto{
Port: k.Port(),
Proto: k.Proto(),
})
}
}
var ips []string
if info.NetworkSettings.GlobalIPv6Address != "" {
ips = append(ips, info.NetworkSettings.GlobalIPv6Address)
}
// add additional networks
for _, network := range info.NetworkSettings.Networks {
if network.GlobalIPv6Address != "" {
ips = append(ips, network.GlobalIPv6Address)
}
}
if len(ports) < 1 || len(ips) < 1 {
log.Printf("No Ipv6 address or port assigned to container '%s', '%v', '%v'", containerID, ips, ports)
return nil
}
for _, ip := range ips {
for _, pp := range ports {
err := f.AddRule(info.ID, ip, pp)
if err != nil {
log.Print(err)
}
}
}
return nil
}
// InsertUnique acts like IPTables.Insert except that it won't add a duplicate
func (f *Firewall) InsertUnique(chain string, rulespec ...string) error {
exists, err := f.IPTables.Exists(f.Table, chain, rulespec...)
if err != nil {
return err
}
if !exists {
return f.IPTables.Insert(f.Table, chain, 1, rulespec...)
}
return nil
}
// AddRule adds a new allow rule for an container
func (f *Firewall) AddRule(containerID string, ip string, pp PortProto) error {
rulespec := []string{
"-j", "ACCEPT",
"-p", pp.Proto,
"-d", ip,
"--dport", pp.Port,
"-m", "comment", "--comment", containerID,
}
log.Printf("Ensure rule: %v", rulespec)
return f.InsertUnique(f.ChainName, rulespec...)
}
// DropRules removes all allow rules for this container in the chain
func (f *Firewall) DropRules(containerID string) error {
rules, err := f.IPTables.List(f.Table, f.ChainName)
if err != nil {
return err
}
failed := []string{}
for _, r := range rules {
// just grep for the correct container
if strings.Contains(r, containerID) {
// lets make an rulespec out of it, first to parts is "-A Chainname"
rulespec := strings.Split(r, " ")
rulespec = rulespec[2:]
log.Printf("Delete rule: %v", rulespec)
err := f.IPTables.Delete(f.Table, f.ChainName, rulespec...)
if err != nil {
failed = append(failed, err.Error())
}
}
}
if len(failed) > 0 {
return fmt.Errorf("iptables delete failed with errors: %s", strings.Join(failed, ","))
}
return nil
}