Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 5612076

Browse files
committed
Kubernetes: move part of launch to init container
This allows the main container to run without write access to the host filesystem, except for two specific areas, thus trimming the attack surface. We lose the behaviour that the CNI plugin is only installed if Weave Net gets up and running at least once, so the error reports from kubelet will be slightly different in the case that it never manages to run.
1 parent b7a9947 commit 5612076

6 files changed

+127
-79
lines changed

prog/weave-kube/Dockerfile.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LABEL maintainer="Weaveworks <[email protected]>" \
66
org.opencontainers.image.source="https://github.com/weaveworks/weave" \
77
org.opencontainers.image.vendor="Weaveworks"
88

9-
ADD ./launch.sh ./kube-utils /home/weave/
9+
ADD ./init.sh ./launch.sh ./kube-utils /home/weave/
1010
ENTRYPOINT ["/home/weave/launch.sh"]
1111

1212
# This label will change for every build, and should therefore be the last layer of the image:

prog/weave-kube/init.sh

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/sh
2+
# Initialisation of Weave Net pod: check Linux settings and install CNI plugin
3+
4+
set -e
5+
6+
modprobe_safe() {
7+
modprobe $1 || echo "Ignore the error if \"$1\" is built-in in the kernel" >&2
8+
}
9+
10+
# Check whether xt_set actually exists
11+
xt_set_exists() {
12+
# Clean everything up in advance, in case there's leftovers
13+
iptables -w -F WEAVE-KUBE-TEST 2>/dev/null || true
14+
iptables -w -X WEAVE-KUBE-TEST 2>/dev/null || true
15+
ipset destroy weave-kube-test 2>/dev/null || true
16+
17+
ipset create weave-kube-test hash:ip
18+
iptables -w -t filter -N WEAVE-KUBE-TEST
19+
if ! iptables -w -A WEAVE-KUBE-TEST -m set --match-set weave-kube-test src -j DROP; then
20+
NOT_EXIST=1
21+
fi
22+
iptables -w -F WEAVE-KUBE-TEST
23+
iptables -w -X WEAVE-KUBE-TEST
24+
ipset destroy weave-kube-test
25+
[ -z "$NOT_EXIST" ] || (echo "\"xt_set\" does not exist" >&2 && return 1)
26+
}
27+
28+
# Default for network policy
29+
EXPECT_NPC=${EXPECT_NPC:-1}
30+
31+
# Ensure we have the required modules for NPC
32+
if [ "${EXPECT_NPC}" != "0" ]; then
33+
modprobe_safe br_netfilter
34+
modprobe_safe xt_set
35+
xt_set_exists
36+
fi
37+
38+
# kube-proxy requires that bridged traffic passes through netfilter
39+
if ! BRIDGE_NF_ENABLED=$(cat /proc/sys/net/bridge/bridge-nf-call-iptables); then
40+
echo "Cannot detect bridge-nf support - network policy and iptables mode kubeproxy may not work reliably" >&2
41+
else
42+
if [ "$BRIDGE_NF_ENABLED" != "1" ]; then
43+
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
44+
fi
45+
fi
46+
47+
# This is where we expect the manifest to map host directories
48+
HOST_ROOT=${HOST_ROOT:-/host}
49+
50+
# Install CNI plugin binary to typical CNI bin location
51+
# with fall-back to CNI directory used by kube-up on GCI OS
52+
if ! mkdir -p $HOST_ROOT/opt/cni/bin ; then
53+
if mkdir -p $HOST_ROOT/home/kubernetes/bin ; then
54+
export WEAVE_CNI_PLUGIN_DIR=$HOST_ROOT/home/kubernetes/bin
55+
else
56+
echo "Failed to install the Weave CNI plugin" >&2
57+
exit 1
58+
fi
59+
fi
60+
mkdir -p $HOST_ROOT/etc/cni/net.d
61+
export HOST_ROOT
62+
/home/weave/weave --local setup-cni

prog/weave-kube/launch.sh

+1-54
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#!/bin/sh
2+
# Launch of Weave Net pod - requires that init.sh has been run previously
23

34
set -e
45

5-
modprobe_safe() {
6-
modprobe $1 || echo "Ignore the error if \"$1\" is built-in in the kernel" >&2
7-
}
8-
96
# Setup iptables backend to be legacy or nftable
107
setup_iptables_backend() {
118
if [ -n "${IPTABLES_BACKEND}" ]; then
@@ -34,24 +31,6 @@ setup_iptables_backend() {
3431
fi
3532
}
3633

37-
# Check whether xt_set actually exists
38-
xt_set_exists() {
39-
# Clean everything up in advance, in case there's leftovers
40-
iptables -w -F WEAVE-KUBE-TEST 2>/dev/null || true
41-
iptables -w -X WEAVE-KUBE-TEST 2>/dev/null || true
42-
ipset destroy weave-kube-test 2>/dev/null || true
43-
44-
ipset create weave-kube-test hash:ip
45-
iptables -w -t filter -N WEAVE-KUBE-TEST
46-
if ! iptables -w -A WEAVE-KUBE-TEST -m set --match-set weave-kube-test src -j DROP; then
47-
NOT_EXIST=1
48-
fi
49-
iptables -w -F WEAVE-KUBE-TEST
50-
iptables -w -X WEAVE-KUBE-TEST
51-
ipset destroy weave-kube-test
52-
[ -z "$NOT_EXIST" ] || (echo "\"xt_set\" does not exist" >&2 && return 1)
53-
}
54-
5534
setup_iptables_backend
5635

5736
# Default if not supplied - same as weave net default
@@ -67,23 +46,6 @@ DB_PREFIX=${DB_PREFIX:-/weavedb/weave-net}
6746

6847
# Default for network policy
6948
EXPECT_NPC=${EXPECT_NPC:-1}
70-
NO_MASQ_LOCAL=${NO_MASQ_LOCAL:-1}
71-
72-
# Ensure we have the required modules for NPC
73-
if [ "${EXPECT_NPC}" != "0" ]; then
74-
modprobe_safe br_netfilter
75-
modprobe_safe xt_set
76-
xt_set_exists
77-
fi
78-
79-
# kube-proxy requires that bridged traffic passes through netfilter
80-
if ! BRIDGE_NF_ENABLED=$(cat /proc/sys/net/bridge/bridge-nf-call-iptables); then
81-
echo "Cannot detect bridge-nf support - network policy and iptables mode kubeproxy may not work reliably" >&2
82-
else
83-
if [ "$BRIDGE_NF_ENABLED" != "1" ]; then
84-
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
85-
fi
86-
fi
8749

8850
STATUS_OPTS="--metrics-addr=$METRICS_ADDR"
8951
# --status-addr exposes internal information, so only turn it on if asked to.
@@ -160,20 +122,6 @@ post_start_actions() {
160122
sleep 1
161123
done
162124

163-
# Install CNI plugin binary to typical CNI bin location
164-
# with fall-back to CNI directory used by kube-up on GCI OS
165-
if ! mkdir -p $HOST_ROOT/opt/cni/bin ; then
166-
if mkdir -p $HOST_ROOT/home/kubernetes/bin ; then
167-
export WEAVE_CNI_PLUGIN_DIR=$HOST_ROOT/home/kubernetes/bin
168-
else
169-
echo "Failed to install the Weave CNI plugin" >&2
170-
exit 1
171-
fi
172-
fi
173-
mkdir -p $HOST_ROOT/etc/cni/net.d
174-
export HOST_ROOT
175-
/home/weave/weave --local setup-cni
176-
177125
# Attempt to run the reclaim process, but don't halt the script if it fails
178126
/home/weave/kube-utils -reclaim -node-name="$HOSTNAME" -peer-name="$PEERNAME" -log-level=debug || true
179127

@@ -190,7 +138,6 @@ post_start_actions &
190138

191139
/home/weave/weaver $EXTRA_ARGS --port=6783 $(router_bridge_opts) \
192140
--name="$PEERNAME" \
193-
--host-root=$HOST_ROOT \
194141
--http-addr=$HTTP_ADDR $STATUS_OPTS --docker-api='' --no-dns \
195142
--db-prefix="$DB_PREFIX" \
196143
--ipalloc-range=$IPALLOC_RANGE $NICKNAME_ARG \

prog/weave-kube/weave-daemonset-k8s-1.11.yaml

+21-8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ items:
118118
labels:
119119
name: weave-net
120120
spec:
121+
initContainers:
122+
- name: weave-init
123+
image: 'weaveworks/weave-kube:latest'
124+
command:
125+
- /home/weave/init.sh
126+
env:
127+
securityContext:
128+
privileged: true
129+
volumeMounts:
130+
- name: cni-bin
131+
mountPath: /host/opt
132+
- name: cni-bin2
133+
mountPath: /host/home
134+
- name: cni-conf
135+
mountPath: /host/etc
136+
- name: lib-modules
137+
mountPath: /lib/modules
138+
- name: xtables-lock
139+
mountPath: /run/xtables.lock
140+
readOnly: false
121141
containers:
122142
- name: weave
123143
command:
@@ -143,16 +163,9 @@ items:
143163
volumeMounts:
144164
- name: weavedb
145165
mountPath: /weavedb
146-
- name: cni-bin
147-
mountPath: /host/opt
148-
- name: cni-bin2
149-
mountPath: /host/home
150-
- name: cni-conf
151-
mountPath: /host/etc
152166
- name: dbus
153167
mountPath: /host/var/lib/dbus
154-
- name: lib-modules
155-
mountPath: /lib/modules
168+
readOnly: true
156169
- name: xtables-lock
157170
mountPath: /run/xtables.lock
158171
readOnly: false

prog/weave-kube/weave-daemonset-k8s-1.8.yaml

+21-8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ items:
115115
labels:
116116
name: weave-net
117117
spec:
118+
initContainers:
119+
- name: weave-init
120+
image: 'weaveworks/weave-kube:latest'
121+
command:
122+
- /home/weave/init.sh
123+
env:
124+
securityContext:
125+
privileged: true
126+
volumeMounts:
127+
- name: cni-bin
128+
mountPath: /host/opt
129+
- name: cni-bin2
130+
mountPath: /host/home
131+
- name: cni-conf
132+
mountPath: /host/etc
133+
- name: lib-modules
134+
mountPath: /lib/modules
135+
- name: xtables-lock
136+
mountPath: /run/xtables.lock
137+
readOnly: false
118138
containers:
119139
- name: weave
120140
command:
@@ -140,16 +160,9 @@ items:
140160
volumeMounts:
141161
- name: weavedb
142162
mountPath: /weavedb
143-
- name: cni-bin
144-
mountPath: /host/opt
145-
- name: cni-bin2
146-
mountPath: /host/home
147-
- name: cni-conf
148-
mountPath: /host/etc
149163
- name: dbus
150164
mountPath: /host/var/lib/dbus
151-
- name: lib-modules
152-
mountPath: /lib/modules
165+
readOnly: true
153166
- name: xtables-lock
154167
mountPath: /run/xtables.lock
155168
readOnly: false

prog/weave-kube/weave-daemonset-k8s-1.9.yaml

+21-8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ items:
118118
labels:
119119
name: weave-net
120120
spec:
121+
initContainers:
122+
- name: weave-init
123+
image: 'weaveworks/weave-kube:latest'
124+
command:
125+
- /home/weave/init.sh
126+
env:
127+
securityContext:
128+
privileged: true
129+
volumeMounts:
130+
- name: cni-bin
131+
mountPath: /host/opt
132+
- name: cni-bin2
133+
mountPath: /host/home
134+
- name: cni-conf
135+
mountPath: /host/etc
136+
- name: lib-modules
137+
mountPath: /lib/modules
138+
- name: xtables-lock
139+
mountPath: /run/xtables.lock
140+
readOnly: false
121141
containers:
122142
- name: weave
123143
command:
@@ -143,16 +163,9 @@ items:
143163
volumeMounts:
144164
- name: weavedb
145165
mountPath: /weavedb
146-
- name: cni-bin
147-
mountPath: /host/opt
148-
- name: cni-bin2
149-
mountPath: /host/home
150-
- name: cni-conf
151-
mountPath: /host/etc
152166
- name: dbus
153167
mountPath: /host/var/lib/dbus
154-
- name: lib-modules
155-
mountPath: /lib/modules
168+
readOnly: true
156169
- name: xtables-lock
157170
mountPath: /run/xtables.lock
158171
readOnly: false

0 commit comments

Comments
 (0)