Skip to content

Commit

Permalink
fix: ec2 detection not working with imdsv2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgiga1993 committed Sep 7, 2023
1 parent dbf696c commit 09ca557
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions node/windows-packaging/CalicoWindows/libs/calico/calico.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,20 @@ function Get-PlatformType()
# EC2
$restError = $null
Try {
$awsNodeName=Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/local-hostname -ErrorAction Ignore
$awsNodeName = Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/local-hostname -ErrorAction Ignore
} Catch {
$restError = $_
if ($_.Exception.Response.StatusCode.value__ -eq 401) {
# IMDSv2
Try {
$token = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token-ttl-seconds" = "21600"} -Method PUT -Uri http://169.254.169.254/latest/api/token -ErrorAction Ignore
$awsNodeName = Invoke-RestMethod -Headers @{"X-aws-ec2-metadata-token" = $token} -uri http://169.254.169.254/latest/meta-data/local-hostname -ErrorAction Ignore
}
Catch {
$restError = $_
}
} else {
$restError = $_
}
}
if ($restError -eq $null) {
return ("ec2")
Expand Down

0 comments on commit 09ca557

Please sign in to comment.