@@ -25,6 +25,9 @@ set -o errexit
25
25
set -o nounset
26
26
set -o pipefail
27
27
28
+ # ## Hardcoded constants
29
+ METADATA_SERVER_IP=" ${METADATA_SERVER_IP:- 169.254.169.254} "
30
+
28
31
function convert-manifest-params {
29
32
# A helper function to convert the manifest args from a string to a list of
30
33
# flag arguments.
@@ -96,6 +99,28 @@ function secure_random {
96
99
echo -n " ${out} " | xxd -r -p | base64 -w 0
97
100
}
98
101
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
+
99
124
function config-ip-firewall {
100
125
echo " Configuring IP firewall rules"
101
126
@@ -146,8 +171,17 @@ function config-ip-firewall {
146
171
# node because we don't expect the daemonset to run on this node.
147
172
if [[ " ${ENABLE_METADATA_CONCEALMENT:- } " == " true" ]] && [[ ! " ${METADATA_CONCEALMENT_NO_FIREWALL:- } " == " true" ]]; then
148
173
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
150
175
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
151
185
}
152
186
153
187
function create-dirs {
0 commit comments