···11-{ targetRoot, wgetExtraOptions }:
11+{ curl, targetRoot, wgetExtraOptions }:
22+# Note: be very cautious about dependencies, each dependency grows
33+# the closure of the initrd. Ideally we would not even require curl,
44+# but there is no reasonable way to send an HTTP PUT request without
55+# it. Note: do not be fooled: the wget referenced in this script
66+# is busybox's wget, not the fully featured one with --method support.
77+#
88+# Make sure that every package you depend on here is already listed as
99+# a channel blocker for both the full-sized and small channels.
1010+# Otherwise, we risk breaking user deploys in released channels.
211''
312 metaDir=${targetRoot}etc/ec2-metadata
413 mkdir -m 0755 -p "$metaDir"
5141515+ get_imds_token() {
1616+ # retry-delay of 1 selected to give the system a second to get going,
1717+ # but not add a lot to the bootup time
1818+ ${curl}/bin/curl \
1919+ -v \
2020+ --retry 3 \
2121+ --retry-delay 1 \
2222+ --fail \
2323+ -X PUT \
2424+ --connect-timeout 1 \
2525+ -H "X-aws-ec2-metadata-token-ttl-seconds: 600" \
2626+ http://169.254.169.254/latest/api/token
2727+ }
2828+2929+ preflight_imds_token() {
3030+ # retry-delay of 1 selected to give the system a second to get going,
3131+ # but not add a lot to the bootup time
3232+ ${curl}/bin/curl \
3333+ -v \
3434+ --retry 3 \
3535+ --retry-delay 1 \
3636+ --fail \
3737+ --connect-timeout 1 \
3838+ -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
3939+ http://169.254.169.254/1.0/meta-data/instance-id
4040+ }
4141+4242+ try=1
4343+ while [ $try -le 3 ]; do
4444+ echo "(attempt $try/3) getting an EC2 instance metadata service v2 token..."
4545+ IMDS_TOKEN=$(get_imds_token) && break
4646+ try=$((try + 1))
4747+ sleep 1
4848+ done
4949+5050+ if [ "x$IMDS_TOKEN" == "x" ]; then
5151+ echo "failed to fetch an IMDS2v token."
5252+ fi
5353+5454+ try=1
5555+ while [ $try -le 10 ]; do
5656+ echo "(attempt $try/10) validating the EC2 instance metadata service v2 token..."
5757+ preflight_imds_token && break
5858+ try=$((try + 1))
5959+ sleep 1
6060+ done
6161+662 echo "getting EC2 instance metadata..."
763864 if ! [ -e "$metaDir/ami-manifest-path" ]; then
99- wget ${wgetExtraOptions} -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
6565+ wget ${wgetExtraOptions} --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
1066 fi
11671268 if ! [ -e "$metaDir/user-data" ]; then
1313- wget ${wgetExtraOptions} -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data"
6969+ wget ${wgetExtraOptions} --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data && chmod 600 "$metaDir/user-data"
1470 fi
15711672 if ! [ -e "$metaDir/hostname" ]; then
1717- wget ${wgetExtraOptions} -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname
7373+ wget ${wgetExtraOptions} --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname
1874 fi
19752076 if ! [ -e "$metaDir/public-keys-0-openssh-key" ]; then
2121- wget ${wgetExtraOptions} -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
7777+ wget ${wgetExtraOptions} --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
2278 fi
2379''