Skip to content

Commit

Permalink
fix: ec2 check to support IMDSv1 and IMDSv2 + moving env vars to conf…
Browse files Browse the repository at this point in the history
… file fixes #206

* Add support for IMDV2 in the ec2 startup script (thank you @orensharoni)
* Fix a typo in the ec2 startup script (thank you @HighOnMikey)

---------

Co-authored-by: Oren Sharoni <oren.sharoni@techmahindra.com>
  • Loading branch information
orensharoni and Oren Sharoni committed Feb 16, 2024
1 parent c687663 commit 626d29d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions standalone_ubuntu_oss_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ required=("S3_BUCKET_NAME" "S3_SERVER" "S3_SERVER_PORT" "S3_SERVER_PROTO"
if [ ! -z ${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI+x} ]; then
echo "Running inside an ECS task, using container credentials"
uses_iam_creds=1
elif TOKEN=$(curl -X PUT --silent --fail --connect-timeout 2 --max-time 2 "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") && \
curl -H "X-aws-ec2-metadata-token: $TOKEN" --output /dev/null --silent --head --fail --connect-timeout 2 --max-time 5 "http://169.254.169.254"; then
echo "Running inside an EC2 instance, using IMDSv2 for credentials"
uses_iam_creds=1
elif curl --output /dev/null --silent --head --fail --connect-timeout 2 "http://169.254.169.254"; then
echo "Running inside an EC2 instance, using IMDS for credentials"
echo "Running inside an EC2 instance, using IMDSv1 for credentials"
uses_iam_creds=1
else
required+=("AWS_ACCESS_KEY_ID" "AWS_SECRET_ACCESS_KEY")
Expand Down Expand Up @@ -312,12 +316,12 @@ EOF
# Only include these env vars if we are not using a instance profile credential
# to obtain S3 permissions.
if [ $uses_iam_creds -eq 0 ]; then
cat >> "/etc/nginx/environment" << EOF
cat >> "/etc/nginx/nginx.conf" << EOF
env AWS_ACCESS_KEY_ID;
env AWS_SECRET_ACCESS_KEY;
EOF
if [[ -v AWS_SESSION_TOKEN ]]; then
cat >> "/etc/nginx/environment" << EOF
cat >> "/etc/nginx/nginx.conf" << EOF
env AWS_SESSION_TOKEN;
EOF
fi
Expand Down

0 comments on commit 626d29d

Please sign in to comment.