Skip to content

Commit

Permalink
updated to IMDSv2 (#5783)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvr authored Oct 30, 2024
1 parent a728aa1 commit 29dc85d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-linux/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ runs:
# Pulled from instance metadata endpoint for EC2
# see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
category=$1
curl -fsSL "http://169.254.169.254/latest/meta-data/${category}"
curl -H "X-aws-ec2-metadata-token: $(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30")" -fsSL "http://169.254.169.254/latest/meta-data/${category}"
}
echo "ami-id: $(get_ec2_metadata ami-id)"
echo "instance-id: $(get_ec2_metadata instance-id)"
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/setup-windows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ runs:
# Pulled from instance metadata endpoint for EC2
# see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
category=$1
curl -fsSL "http://169.254.169.254/latest/meta-data/${category}"
curl -H "X-aws-ec2-metadata-token: $(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30")" -fsSL "http://169.254.169.254/latest/meta-data/${category}"
}
echo "ami-id: $(get_ec2_metadata ami-id)"
echo "instance-id: $(get_ec2_metadata instance-id)"
Expand Down
21 changes: 20 additions & 1 deletion setup-ssh/src/ec2-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@ export async function getEC2Metadata(category: string): Promise<string> {
allowRetries: true,
maxRetries
})
// convert these two curls:
// curl -H "X-aws-ec2-metadata-token: $(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 30")" -fsSL "http://169.254.169.254/latest/meta-data/${category}"
const tokenResponse = await http.put(
`http://169.254.169.254/latest/api/token`, undefined, {
headers: {
'X-aws-ec2-metadata-token-ttl-seconds': '30'
}
}
)

if (tokenResponse.message.statusCode !== 200) {
return ''
}

const resp = await http.get(
`http://169.254.169.254/latest/meta-data/${category}`
`http://169.254.169.254/latest/meta-data/${category}`, {
headers: {
'X-aws-ec2-metadata-token': tokenResponse.result
}
}
)

if (resp.message.statusCode !== 200) {
return ''
}
Expand Down

0 comments on commit 29dc85d

Please sign in to comment.