Skip to content

Commit fb632a0

Browse files
authored
Merge pull request kubernetes#92080 from JonathanSun/master
Log metadata server access for master pods by UID
2 parents daa27c9 + 2f7874b commit fb632a0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

cluster/gce/gci/configure-helper.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ set -o errexit
2525
set -o nounset
2626
set -o pipefail
2727

28+
### Hardcoded constants
29+
METADATA_SERVER_IP="${METADATA_SERVER_IP:-169.254.169.254}"
30+
2831
function convert-manifest-params {
2932
# A helper function to convert the manifest args from a string to a list of
3033
# flag arguments.
@@ -96,6 +99,28 @@ function secure_random {
9699
echo -n "${out}" | xxd -r -p | base64 -w 0
97100
}
98101

102+
# Helper for configuring iptables rules for metadata server.
103+
#
104+
# $1 is the command flag (-I or -D).
105+
# $2 is the firewall action (LOG or REJECT).
106+
# $3 is the prefix for log output.
107+
# $4 is "!" to optionally invert the uid range.
108+
function gce-metadata-fw-helper {
109+
local -r command="$1"
110+
local action="$2"
111+
local -r prefix="$3"
112+
local -r invert="${4:-}"
113+
114+
# Expand rule action to include relevant option flags.
115+
case "${action}" in
116+
LOG)
117+
action="LOG --log-prefix "${prefix}:" --log-uid --log-tcp-options --log-ip-option"
118+
;;
119+
esac
120+
121+
iptables ${command} OUTPUT -p tcp --dport 80 -d ${METADATA_SERVER_IP} -m owner ${invert:-} --uid-owner=${METADATA_SERVER_ALLOWED_UID_RANGE:-0-2999} -j ${action}
122+
}
123+
99124
function config-ip-firewall {
100125
echo "Configuring IP firewall rules"
101126

@@ -146,8 +171,17 @@ function config-ip-firewall {
146171
# node because we don't expect the daemonset to run on this node.
147172
if [[ "${ENABLE_METADATA_CONCEALMENT:-}" == "true" ]] && [[ ! "${METADATA_CONCEALMENT_NO_FIREWALL:-}" == "true" ]]; then
148173
echo "Add rule for metadata concealment"
149-
iptables -w -t nat -I PREROUTING -p tcp -d 169.254.169.254 --dport 80 -m comment --comment "metadata-concealment: bridge traffic to metadata server goes to metadata proxy" -j DNAT --to-destination 127.0.0.1:988
174+
iptables -w -t nat -I PREROUTING -p tcp -d ${METADATA_SERVER_IP} --dport 80 -m comment --comment "metadata-concealment: bridge traffic to metadata server goes to metadata proxy" -j DNAT --to-destination 127.0.0.1:988
150175
fi
176+
177+
# Log all metadata access not from approved processes.
178+
case "${METADATA_SERVER_FIREWALL_MODE:-off}" in
179+
log)
180+
echo "Installing metadata firewall logging rules"
181+
gce-metadata-fw-helper -I LOG "MetadataServerFirewallReject" !
182+
gce-metadata-fw-helper -I LOG "MetadataServerFirewallAccept"
183+
;;
184+
esac
151185
}
152186

153187
function create-dirs {

0 commit comments

Comments
 (0)