Unverified Commit 80a4ce9a authored by Johnny Walker's avatar Johnny Walker Committed by Philip Taron
Browse files

nixos/ec2-metadata-fetcher: decompress gzip-compressed user data

parent 1acbb176
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -96,7 +96,12 @@ in
      wantedBy = [ "multi-user.target" ];
      wants = [ "network-online.target" ];
      after = [ "network-online.target" ];
      path = [ pkgs.curl ];
      path = with pkgs; [
        curl
        file
        gzip
        mktemp
      ];
      script = builtins.readFile ./ec2-metadata-fetcher.sh;
      serviceConfig.Type = "oneshot";
      serviceConfig.StandardOutput = "journal+console";
+13 −1
Original line number Diff line number Diff line
@@ -61,7 +61,19 @@ get_imds() {
  curl --silent --show-error --fail --header "X-aws-ec2-metadata-token: $IMDS_TOKEN" "$@" || true
}

try_decompress() {
  local temp
  ftype=$(file --brief "$1")
  case $ftype in
    gzip*)
      echo "decompressing: $1"
      temp=$(mktemp)
      zcat "$1" > "$temp"
      mv "$temp" "$1"
  esac
}

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