Skip to content

Commit 3dad9c1

Browse files
committed
Add IMDSv2 support
1 parent 3b1ea03 commit 3dad9c1

2 files changed

Lines changed: 39 additions & 27 deletions

File tree

ec2-metadata

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,33 @@ Options:
4040
-d/--user-data User-supplied data.Only available if supplied at instance launch time."
4141
}
4242

43-
#check some basic configurations before running the code
44-
function chk_config()
43+
METADATA_BASEURL="http://169.254.169.254"
44+
METADATA_TOKEN_PATH="latest/api/token"
45+
46+
function set_imds_token()
4547
{
46-
#check if run inside an ec2-instance
47-
x=$(curl -sq http://169.254.169.254/)
48-
if [ $? -gt 0 ]; then
49-
echo '[ERROR] Command not valid outside EC2 instance. Please run this command within a running EC2 instance.'
50-
exit 1
48+
if [ -z "${IMDS_TOKEN}" ];then
49+
IMDS_TOKEN=$(curl -s -f -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 900" ${METADATA_BASEURL}/${METADATA_TOKEN_PATH})
50+
if [ "${?}" -gt 0 ] || [ -z "${IMDS_TOKEN}" ]; then
51+
echo '[ERROR] Could not get IMDSv2 token. Instance Metadata might have been disabled or this is not an EC2 instance.'
52+
exit 1
53+
fi
5154
fi
5255
}
5356

57+
# param1 = query
58+
function get_meta()
59+
{
60+
local imds_out=$(curl -s -q -H "X-aws-ec2-metadata-token:${IMDS_TOKEN}" -f ${METADATA_BASEURL}/latest/${1})
61+
echo -n ${imds_out}
62+
}
63+
5464
#print standard metric
5565
function print_normal_metric() {
5666
metric_path=$2
5767
echo -n $1": "
58-
RESPONSE=$(curl -fsq http://169.254.169.254/latest/${metric_path}/)
59-
if [ $? == 0 ]; then
68+
RESPONSE=$(get_meta ${metric_path})
69+
if [ -n "${RESPONSE}" ]; then
6070
echo "$RESPONSE"
6171
else
6272
echo not available
@@ -66,39 +76,38 @@ function print_normal_metric() {
6676
#print block-device-mapping
6777
function print_block-device-mapping()
6878
{
69-
echo 'block-device-mapping: '
70-
x=$(curl -fsq http://169.254.169.254/latest/meta-data/block-device-mapping/)
71-
if [ $? -eq 0 ]; then
72-
for i in $x; do
73-
echo -e '\t' $i: $(curl -sq http://169.254.169.254/latest/meta-data/block-device-mapping/$i)
74-
done
75-
else
76-
echo not available
77-
fi
79+
echo 'block-device-mapping: '
80+
x=$(get_meta meta-data/block-device-mapping/)
81+
if [ -n "${x}" ]; then
82+
for i in $x; do
83+
echo -e '\t' $i: $(get_meta meta-data/block-device-mapping/$i)
84+
done
85+
else
86+
echo not available
87+
fi
7888
}
7989

8090
#print public-keys
8191
function print_public-keys()
8292
{
8393
echo 'public-keys: '
84-
x=$(curl -fsq http://169.254.169.254/latest/meta-data/public-keys/)
85-
if [ $? -eq 0 ]; then
94+
x=$(get_meta meta-data/public-keys/)
95+
if [ -n "${x}" ]; then
8696
for i in $x; do
8797
index=$(echo $i|cut -d = -f 1)
8898
keyname=$(echo $i|cut -d = -f 2)
8999
echo keyname:$keyname
90100
echo index:$index
91-
format=$(curl -sq http://169.254.169.254/latest/meta-data/public-keys/$index/)
101+
format=$(get_meta meta-data/public-keys/$index/)
92102
echo format:$format
93103
echo 'key:(begins from next line)'
94-
echo $(curl -sq http://169.254.169.254/latest/meta-data/public-keys/$index/$format)
104+
echo $(get_meta meta-data/public-keys/$index/$format)
95105
done
96106
else
97107
echo not available
98108
fi
99109
}
100110

101-
102111
function print_all()
103112
{
104113
print_normal_metric ami-id meta-data/ami-id
@@ -123,7 +132,7 @@ function print_all()
123132
}
124133

125134
#check if run inside an EC2 instance
126-
chk_config
135+
set_imds_token
127136

128137
#command called in default mode
129138
if [ "$#" -eq 0 ]; then

ec2-utils.spec

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Name: ec2-utils
22
Summary: A set of tools for running in EC2
3-
Version: 1.0
4-
Release: 2%{?dist}
3+
Version: 1.1
4+
Release: 1%{?dist}
55
License: MIT
66
Group: System Tools
77

@@ -80,7 +80,10 @@ rm -rf $RPM_BUILD_ROOT
8080
%{_sysconfdir}/udev/rules.d/70-ec2-nvme-devices.rules
8181

8282
%changelog
83-
* Tue Aug 27 2019 Anchal Agarwal <anchalag@amazon.com>
83+
* Wed Jan 15 2020 Frederick Lefebvre <fredlef@amazon.com> 1.1-1
84+
- Add IMDSv2 support
85+
86+
* Tue Aug 27 2019 Anchal Agarwal <anchalag@amazon.com> 1.0-2
8487
- Add udev rule to define lower timeout for instance storage volumes
8588

8689
* Wed Sep 22 2010 Nathan Blackham <blackham@amazon.com>

0 commit comments

Comments
 (0)