Skip to content

Commit

Permalink
Merge pull request #74 from tarosky/issue/73
Browse files Browse the repository at this point in the history
Fix bug related to IMDSv2
Close #73.
  • Loading branch information
harai authored May 8, 2024
2 parents 5c02b5f + 5c6a4f7 commit 09aacd0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion inc/healthcheck-eip.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,29 @@ function init_healthcheck($option)

$renew_token = function () use (&$token) {
$ch = curl_init(IMDSV2_TOKEN_URL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-aws-ec2-metadata-token-ttl-seconds: 21600']);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$content = substr($response, $header_size);
curl_close($ch);

if ($status !== 200) {
return;
}

$token = $content;
};

$get = null;
$get = function ($url, $retry_count = 0) use (&$get, &$token, $renew_token) {
if ($token === null) {
throw new MetadataError('failed to get token');
}

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-aws-ec2-metadata-token: $token"]);
Expand Down

0 comments on commit 09aacd0

Please sign in to comment.