Skip to content

Commit

Permalink
Merge pull request #7970 from davidgiga1993/imdsv2-fix
Browse files Browse the repository at this point in the history
fix: ec2 detection not working with imdsv2
  • Loading branch information
coutinhop authored Sep 15, 2023
2 parents 7b9f6ce + 09ca557 commit 39796b7
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 39796b7

Please sign in to comment.