This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
forked from cloudfoundry/guardian
-
Notifications
You must be signed in to change notification settings - Fork 1
/
global_chains.go
284 lines (231 loc) · 9.75 KB
/
global_chains.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
package iptables
import (
"fmt"
"os"
"os/exec"
)
const SetupScript = `
set -o xtrace
set -o nounset
set -o errexit
shopt -s nullglob
filter_input_chain="${GARDEN_IPTABLES_FILTER_INPUT_CHAIN}"
filter_forward_chain="${GARDEN_IPTABLES_FILTER_FORWARD_CHAIN}"
filter_default_chain="${GARDEN_IPTABLES_FILTER_DEFAULT_CHAIN}"
filter_instance_prefix="${GARDEN_IPTABLES_FILTER_INSTANCE_PREFIX}"
nat_prerouting_chain="${GARDEN_IPTABLES_NAT_PREROUTING_CHAIN}"
nat_postrouting_chain="${GARDEN_IPTABLES_NAT_POSTROUTING_CHAIN}"
nat_instance_prefix="${GARDEN_IPTABLES_NAT_INSTANCE_PREFIX}"
iptables_bin="${GARDEN_IPTABLES_BIN}"
function teardown_deprecated_rules() {
# Remove jump to garden-dispatch from INPUT
rules=$(${iptables_bin} -w -S INPUT 2> /dev/null) || true
echo "$rules" |
grep " -j garden-dispatch" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w
# Remove jump to garden-dispatch from FORWARD
rules=$(${iptables_bin} -w -S FORWARD 2> /dev/null) || true
echo "$rules" |
grep " -j garden-dispatch" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w
# Prune garden-dispatch
${iptables_bin} -w -F garden-dispatch 2> /dev/null || true
# Delete garden-dispatch
${iptables_bin} -w -X garden-dispatch 2> /dev/null || true
}
function teardown_filter() {
teardown_deprecated_rules
# Prune garden-forward chain
rules=$(${iptables_bin} -w -S ${filter_forward_chain} 2> /dev/null) || true
echo "$rules" |
grep "\-g ${filter_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w
# Prune per-instance chains
rules=$(${iptables_bin} -w -S 2> /dev/null) || true
echo "$rules" |
grep "^-A ${filter_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w
# Delete per-instance chains
rules=$(${iptables_bin} -w -S 2> /dev/null) || true
echo "$rules" |
grep "^-N ${filter_instance_prefix}" |
sed -e "s/-N/-X/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w || true
# Remove jump to garden-forward from FORWARD
rules=$(${iptables_bin} -w -S FORWARD 2> /dev/null) || true
echo "$rules" |
grep " -j ${filter_forward_chain}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w || true
${iptables_bin} -w -F ${filter_forward_chain} 2> /dev/null || true
${iptables_bin} -w -F ${filter_default_chain} 2> /dev/null || true
# Remove jump to filter input chain from INPUT
rules=$(${iptables_bin} -w -S INPUT 2> /dev/null) || true
echo "$rules" |
grep " -j ${filter_input_chain}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w || true
# Empty and delete filter input chain
${iptables_bin} -w -F ${filter_input_chain} 2> /dev/null || true
${iptables_bin} -w -X ${filter_input_chain} 2> /dev/null || true
}
function setup_filter() {
teardown_filter
# Determine interface device to the outside
default_interface=$(ip route show | grep default | cut -d' ' -f5 | head -1)
# Create, or empty existing, filter input chain
${iptables_bin} -w -N ${filter_input_chain} 2> /dev/null || ${iptables_bin} -w -F ${filter_input_chain}
# Accept inbound packets if default interface is matched by filter prefix
${iptables_bin} -w -I ${filter_input_chain} -i $default_interface --jump ACCEPT
# Put connection tracking rule in filter input chain
# to accept packets related to previously established connections
${iptables_bin} -w -A ${filter_input_chain} -m conntrack --ctstate ESTABLISHED,RELATED --jump ACCEPT
if [ "${GARDEN_IPTABLES_ALLOW_HOST_ACCESS}" != "true" ]; then
${iptables_bin} -w -A ${filter_input_chain} --jump REJECT --reject-with icmp-host-prohibited
else
${iptables_bin} -w -A ${filter_input_chain} --jump ACCEPT
fi
# Forward input traffic via ${filter_input_chain}
${iptables_bin} -w -A INPUT -i ${GARDEN_NETWORK_INTERFACE_PREFIX}+ --jump ${filter_input_chain}
# Create or flush forward chain
${iptables_bin} -w -N ${filter_forward_chain} 2> /dev/null || ${iptables_bin} -w -F ${filter_forward_chain}
${iptables_bin} -w -A ${filter_forward_chain} -j DROP
# Create or flush default chain
${iptables_bin} -w -N ${filter_default_chain} 2> /dev/null || ${iptables_bin} -w -F ${filter_default_chain}
# Always allow established connections to containers
${iptables_bin} -w -A ${filter_default_chain} -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Forward outbound traffic via ${filter_forward_chain}
${iptables_bin} -w -A FORWARD -i ${GARDEN_NETWORK_INTERFACE_PREFIX}+ --jump ${filter_forward_chain}
# Forward inbound traffic immediately
${iptables_bin} -w -I ${filter_forward_chain} -i $default_interface --jump ACCEPT
}
function teardown_nat() {
# Prune prerouting chain
rules=$(${iptables_bin} -w -t nat -S ${nat_prerouting_chain} 2> /dev/null) || true
echo "$rules" |
grep "\-j ${nat_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w -t nat
# Prune per-instance chains
rules=$(${iptables_bin} -w -t nat -S 2> /dev/null) || true
echo "$rules" |
grep "^-A ${nat_instance_prefix}" |
sed -e "s/-A/-D/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w -t nat
# Delete per-instance chains
rules=$(${iptables_bin} -w -t nat -S 2> /dev/null) || true
echo "$rules" |
grep "^-N ${nat_instance_prefix}" |
sed -e "s/-N/-X/" -e "s/\s\+\$//" |
xargs --no-run-if-empty --max-lines=1 ${iptables_bin} -w -t nat || true
# Flush prerouting chain
${iptables_bin} -w -t nat -F ${nat_prerouting_chain} 2> /dev/null || true
# Flush postrouting chain
${iptables_bin} -w -t nat -F ${nat_postrouting_chain} 2> /dev/null || true
}
function setup_nat() {
teardown_nat
# Create prerouting chain
${iptables_bin} -w -t nat -N ${nat_prerouting_chain} 2> /dev/null || true
# Bind chain to PREROUTING
(${iptables_bin} -w -t nat -S PREROUTING | grep -q "\-j ${nat_prerouting_chain}\b") ||
${iptables_bin} -w -t nat -A PREROUTING \
--jump ${nat_prerouting_chain}
# Bind chain to OUTPUT (for traffic originating from same host)
(${iptables_bin} -w -t nat -S OUTPUT | grep -q "\-j ${nat_prerouting_chain}\b") ||
${iptables_bin} -w -t nat -A OUTPUT \
--out-interface "lo" \
--jump ${nat_prerouting_chain}
# Create postrouting chain
${iptables_bin} -w -t nat -N ${nat_postrouting_chain} 2> /dev/null || true
# Bind chain to POSTROUTING
(${iptables_bin} -w -t nat -S POSTROUTING | grep -q "\-j ${nat_postrouting_chain}\b") ||
${iptables_bin} -w -t nat -A POSTROUTING \
--jump ${nat_postrouting_chain}
}
case "${ACTION}" in
setup)
setup_filter
setup_nat
# Enable forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
;;
teardown)
teardown_filter
teardown_nat
;;
*)
echo "Unknown command: ${1}" 1>&2
exit 1
;;
esac
`
type Starter struct {
iptables *IPTablesController
allowHostAccess bool
destroyContainersOnStartup bool
nicPrefix string
denyNetworks []string
}
func NewStarter(iptables *IPTablesController, allowHostAccess bool, nicPrefix string, denyNetworks []string, destroyContainersOnStartup bool) *Starter {
return &Starter{
iptables: iptables,
allowHostAccess: allowHostAccess,
destroyContainersOnStartup: destroyContainersOnStartup,
nicPrefix: nicPrefix,
denyNetworks: denyNetworks,
}
}
func (s Starter) Start() error {
if s.destroyContainersOnStartup || !s.chainExists(s.iptables.inputChain) {
cmd := exec.Command("bash", "-c", SetupScript)
cmd.Env = []string{
fmt.Sprintf("PATH=%s", os.Getenv("PATH")),
"ACTION=setup",
fmt.Sprintf("GARDEN_IPTABLES_BIN=%s", s.iptables.iptablesBinPath),
fmt.Sprintf("GARDEN_IPTABLES_FILTER_INPUT_CHAIN=%s", s.iptables.inputChain),
fmt.Sprintf("GARDEN_IPTABLES_FILTER_FORWARD_CHAIN=%s", s.iptables.forwardChain),
fmt.Sprintf("GARDEN_IPTABLES_FILTER_DEFAULT_CHAIN=%s", s.iptables.defaultChain),
fmt.Sprintf("GARDEN_IPTABLES_FILTER_INSTANCE_PREFIX=%s", s.iptables.instanceChainPrefix),
fmt.Sprintf("GARDEN_IPTABLES_NAT_PREROUTING_CHAIN=%s", s.iptables.preroutingChain),
fmt.Sprintf("GARDEN_IPTABLES_NAT_POSTROUTING_CHAIN=%s", s.iptables.postroutingChain),
fmt.Sprintf("GARDEN_IPTABLES_NAT_INSTANCE_PREFIX=%s", s.iptables.instanceChainPrefix),
fmt.Sprintf("GARDEN_NETWORK_INTERFACE_PREFIX=%s", s.nicPrefix),
fmt.Sprintf("GARDEN_IPTABLES_ALLOW_HOST_ACCESS=%t", s.allowHostAccess),
}
if err := s.iptables.run("setup-global-chains", cmd); err != nil {
return fmt.Errorf("setting up default chains: %s", err)
}
}
if err := s.resetDenyNetworks(); err != nil {
return err
}
for _, n := range s.denyNetworks {
if err := s.iptables.appendRule(s.iptables.defaultChain, rejectRule(n)); err != nil {
return err
}
}
return nil
}
func (s Starter) chainExists(chainName string) bool {
cmd := exec.Command(s.iptables.iptablesBinPath, "-w", "-L", chainName)
cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s", os.Getenv("PATH")))
return s.iptables.run("checking-chain-exists", cmd) == nil
}
func (s Starter) resetDenyNetworks() error {
cmd := exec.Command(s.iptables.iptablesBinPath, "-w", "-F", s.iptables.defaultChain)
cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s", os.Getenv("PATH")))
if err := s.iptables.run("flushing-default-chain", cmd); err != nil {
return err
}
cmd = exec.Command(s.iptables.iptablesBinPath, "-w", "-A", s.iptables.defaultChain, "-m", "conntrack", "--ctstate", "ESTABLISHED,RELATED", "--jump", "ACCEPT")
cmd.Env = append(cmd.Env, fmt.Sprintf("PATH=%s", os.Getenv("PATH")))
if err := s.iptables.run("appending-default-chain", cmd); err != nil {
return err
}
return nil
}