From d1b60771a20c36fcf6ddf59a7f48a5ee053e2177 Mon Sep 17 00:00:00 2001 From: Anne Macedo Date: Wed, 28 Aug 2024 17:43:50 -0300 Subject: [PATCH 1/2] Add --url flag to ec2-metadata to override metadata-server Add --url flag to override METADATA_BASEURL so that one can use another metadata server instead of the default one. This is useful for situations where you want to use a mock metadata server, such as https://github.com/aws/amazon-ec2-metadata-mock This also allows to simulate EC2 environments locally, using https://github.com/retpolanne/cloud-vm-sim for example. Signed-off-by: Anne Macedo --- ec2-metadata | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ec2-metadata b/ec2-metadata index 8405682..ad619e3 100755 --- a/ec2-metadata +++ b/ec2-metadata @@ -41,7 +41,8 @@ Options: -s/--security-groups Names of the security groups the instance is launched in. Only available if supplied at instance launch time -d/--user-data User-supplied data.Only available if supplied at instance launch time. -g/--tags Tags assigned to this instance. ---quiet Suppress tag keys from the output." +--quiet Suppress tag keys from the output. +--url Override base URL for metadata server (default http://169.254.169.254)" } METADATA_BASEURL="http://169.254.169.254" @@ -169,7 +170,7 @@ shortopts=almnbithokzPcpvuresdgR longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone partition product-codes public-hostname public-ipv4 public-keys ramdisk-id - reservation-id security-groups user-data tags region help all quiet) + reservation-id security-groups user-data tags region help all quiet url) oldIFS="$IFS" IFS=, @@ -192,6 +193,9 @@ while true; do --quiet) QUIET=1 ; shift ;; + --url) + METADATA_BASEURL=$2; shift; shift + ;; --) shift ; break ;; From 474123bc1515c0f144014c1d2da853c09cc7a469 Mon Sep 17 00:00:00 2001 From: Anne Macedo Date: Thu, 29 Aug 2024 10:31:08 -0300 Subject: [PATCH 2/2] Fix url parsing and check imds later Check imds after getting the metadata baseurl from the parameters. Signed-off-by: Anne Macedo --- ec2-metadata | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ec2-metadata b/ec2-metadata index ad619e3..ca84f96 100755 --- a/ec2-metadata +++ b/ec2-metadata @@ -157,9 +157,6 @@ function print_all() print_tags } -#check if run inside an EC2 instance -set_imds_token - #command called in default mode if [ "$#" -eq 0 ]; then print_all @@ -170,7 +167,7 @@ shortopts=almnbithokzPcpvuresdgR longopts=(ami-id ami-launch-index ami-manifest-path ancestor-ami-ids block-device-mapping instance-id instance-type local-hostname local-ipv4 kernel-id availability-zone partition product-codes public-hostname public-ipv4 public-keys ramdisk-id - reservation-id security-groups user-data tags region help all quiet url) + reservation-id security-groups user-data tags region help all quiet url:) oldIFS="$IFS" IFS=, @@ -194,7 +191,8 @@ while true; do QUIET=1 ; shift ;; --url) - METADATA_BASEURL=$2; shift; shift + shift + METADATA_BASEURL=$1; shift ;; --) shift ; break @@ -211,6 +209,9 @@ while true; do esac done +#check if run inside an EC2 instance +set_imds_token + #start processing command line arguments for action in "${actions[@]}"; do case "$action" in