Skip to content

Commit f192d8a

Browse files
committed
Migrate IMDSv1 request to IMDSv2
1 parent 31a309c commit f192d8a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

certs/templates/user_data.tpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#cloud-config
22
runcmd:
3-
- export INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id`
3+
# Fetch IMDSv2 token
4+
- |
5+
TOKEN=$(curl -H "X-aws-ec2-metadata-token-ttl-seconds: 60" -X PUT "http://169.254.169.254/latest/api/token")
6+
echo "IMDSv2 token fetched: $TOKEN"
7+
8+
# Use the token to make a metadata request
9+
- |
10+
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/instance-id")
11+
echo "Instance ID: $INSTANCE_ID"
412
- echo "OPENVPN_CERT_SOURCE=s3://${replace(s3_bucket,"/(/)+$/","")}/${replace(s3_bucket_prefix,"/^(/)+|(/)+$/","")}" > /etc/openvpn/get-openvpn-certs.env
513
- if [ -n "${vpc_dns_ip}" ]; then echo "push \"dhcp-option DNS ${vpc_dns_ip}\"" >> /etc/openvpn/server.conf;fi
614
- echo 'crl-verify /etc/openvpn/keys/crl.pem' >> /etc/openvpn/server.conf

docker-openvpn-server/cluster/templates/user_data.tpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ runcmd:
66
- curl -s -O https://bootstrap.pypa.io/get-pip.py && python get-pip.py
77
- /usr/local/bin/pip install awscli && ln -sf /usr/local/bin/aws /usr/bin/
88

9-
- export INSTANCE_ID=`curl http://169.254.169.254/latest/meta-data/instance-id`
9+
# Fetch IMDSv2 token
10+
- |
11+
TOKEN=$(curl -H "X-aws-ec2-metadata-token-ttl-seconds: 60" -X PUT "http://169.254.169.254/latest/api/token")
12+
echo "IMDSv2 token fetched: $TOKEN"
13+
14+
# Use the token to make a metadata request
15+
- |
16+
INSTANCE_ID=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s "http://169.254.169.254/latest/meta-data/instance-id")
1017
- docker pull ${openvpn_docker_image}:${openvpn_docker_tag}
1118
- mkdir -p /opt/openvpn
1219
- touch /opt/openvpn/.env && chmod 700 /opt/openvpn/.env
@@ -27,5 +34,4 @@ runcmd:
2734

2835
- docker run -d --name openvpn --env-file=/opt/openvpn/.env --cap-add=NET_ADMIN --device=/dev/net/tun -v /opt/openvpn/:/etc/openvpn/ -v /var/run/openvpn/:/var/run/openvpn -p 1194:1194/tcp ${openvpn_docker_image}:${openvpn_docker_tag} /start_server.sh
2936
- if [ ${assign_eip} = 'true' ]; then for eip in `aws ec2 describe-tags --region=${region} --filters "Name=resource-type,Values=elastic-ip" "Name=value,Values=${stack_item_label}" | jq -r '.Tags[].ResourceId'`; do if [ `aws ec2 describe-addresses --allocation-id $${eip} --region=${region} | jq -r '.Addresses[].InstanceId'` = 'null' ]; then echo "$${eip} is available, assigning it to current instance";aws ec2 associate-address --instance-id "$${INSTANCE_ID}" --allocation-id $${eip} --region=${region};else echo "$${eip} is taken";fi; done;fi
30-
3137
output : { all : '| tee -a /var/log/cloud-init-output.log' }

0 commit comments

Comments
 (0)