summary refs log tree commit diff
path: root/nixos/modules/virtualisation/ec2-metadata-fetcher.nix
blob: 760f024f33fbd03d527832e70111356cb3d2b666 (plain) (blame)
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
{ curl, targetRoot, wgetExtraOptions }:
# Note: be very cautious about dependencies, each dependency grows
# the closure of the initrd. Ideally we would not even require curl,
# but there is no reasonable way to send an HTTP PUT request without
# it. Note: do not be fooled: the wget referenced in this script
# is busybox's wget, not the fully featured one with --method support.
#
# Make sure that every package you depend on here is already listed as
# a channel blocker for both the full-sized and small channels.
# Otherwise, we risk breaking user deploys in released channels.
#
# Also note: OpenStack's metadata service for its instances aims to be
# compatible with the EC2 IMDS. Where possible, try to keep the set of
# fetched metadata in sync with ./openstack-metadata-fetcher.nix .
''
  metaDir=${targetRoot}etc/ec2-metadata
  mkdir -m 0755 -p "$metaDir"
  rm -f "$metaDir/*"

  get_imds_token() {
    # retry-delay of 1 selected to give the system a second to get going,
    # but not add a lot to the bootup time
    ${curl}/bin/curl \
      -v \
      --retry 3 \
      --retry-delay 1 \
      --fail \
      -X PUT \
      --connect-timeout 1 \
      -H "X-aws-ec2-metadata-token-ttl-seconds: 600" \
      http://169.254.169.254/latest/api/token
  }

  preflight_imds_token() {
    # retry-delay of 1 selected to give the system a second to get going,
    # but not add a lot to the bootup time
    ${curl}/bin/curl \
      -v \
      --retry 3 \
      --retry-delay 1 \
      --fail \
      --connect-timeout 1 \
      -H "X-aws-ec2-metadata-token: $IMDS_TOKEN" \
      http://169.254.169.254/1.0/meta-data/instance-id
  }

  try=1
  while [ $try -le 3 ]; do
    echo "(attempt $try/3) getting an EC2 instance metadata service v2 token..."
    IMDS_TOKEN=$(get_imds_token) && break
    try=$((try + 1))
    sleep 1
  done

  if [ "x$IMDS_TOKEN" == "x" ]; then
    echo "failed to fetch an IMDS2v token."
  fi

  try=1
  while [ $try -le 10 ]; do
    echo "(attempt $try/10) validating the EC2 instance metadata service v2 token..."
    preflight_imds_token && break
    try=$((try + 1))
    sleep 1
  done

  echo "getting EC2 instance metadata..."

  wget_imds() {
    wget ${wgetExtraOptions} --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" "$@";
  }

  wget_imds -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path
  (umask 077 && wget_imds -O "$metaDir/user-data" http://169.254.169.254/1.0/user-data)
  wget_imds -O "$metaDir/hostname" http://169.254.169.254/1.0/meta-data/hostname
  wget_imds -O "$metaDir/public-keys-0-openssh-key" http://169.254.169.254/1.0/meta-data/public-keys/0/openssh-key
''