This repository was archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-metadata
More file actions
executable file
·58 lines (54 loc) · 1.76 KB
/
get-metadata
File metadata and controls
executable file
·58 lines (54 loc) · 1.76 KB
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
#!/bin/bash
# Taken from https://gist.github.com/xuwang/c42a77e9f833d263d040. Replaced some grep with jq.
info=${1^^}
meta_data_url=http://169.254.169.254/latest/meta-data/
roleProfile=$(curl -s http://169.254.169.254/latest/meta-data/iam/info | grep -Eo 'instance-profile/([a-zA-Z.-]+)' | sed 's#instance-profile/##')
# auth values
get_sts_value() {
echo -n $(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$roleProfile/ \
| grep "$1" \
| awk -F":" '{print $2}' \
| sed 's/^[ ^t]*//;s/"//g;s/,//g')
}
case $info in
ACCOUNT)
result=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq .accountId | sed 's/"//g')
;;
HOSTNAME)
result=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
;;
ID|INSTANCEID)
result=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
;;
PRIVATEIP)
result=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
;;
PUBLICIP)
result=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
;;
ROLE)
result=$roleProfile
;;
STSCRED)
result=$(curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/$roleProfile)
;;
STSTOKEN)
result=$(get_sts_value "Token")
;;
STSKEY)
result=$(get_sts_value "AccessKeyId")
;;
S3SECRET)
result=$(get_sts_value "SecretAccessKey")
;;
ZONE)
result=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone/ | sed -e 's/.$//')
;;
*)
echo "Usage: $(basename $0) <argument>"
grep -Eo '([A-Z.]+\))' $0 | sed 's/)//'
;;
esac
if [ ! -z "$result" ]; then
echo "$result"
fi