-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: IMDS support for providing custom endpoint (#2859)
- Loading branch information
1 parent
f84fe47
commit a85127e
Showing
7 changed files
with
509 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ | ||
"type": "feature", | ||
"category": "Credentials", | ||
"description": "Adds support for specifying custom IMDS endpoint when using the InstanceProfileProvider." | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Aws\Credentials; | ||
|
||
final class CredentialsUtils | ||
{ | ||
/** | ||
* Determines whether a given host | ||
* is a loopback address. | ||
* | ||
* @param $host | ||
* | ||
* @return bool | ||
*/ | ||
public static function isLoopBackAddress($host): bool | ||
{ | ||
if (!filter_var($host, FILTER_VALIDATE_IP)) { | ||
return false; | ||
} | ||
|
||
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | ||
if ($host === '::1') { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
$loopbackStart = ip2long('127.0.0.0'); | ||
$loopbackEnd = ip2long('127.255.255.255'); | ||
$ipLong = ip2long($host); | ||
|
||
return ($ipLong >= $loopbackStart && $ipLong <= $loopbackEnd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
namespace Aws\Test\Credentials; | ||
|
||
use Aws\Credentials\CredentialsUtils; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Aws\Credentials\CredentialsUtils | ||
*/ | ||
class CredentialsUtilsTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @param string $host | ||
* @param bool $expectedResult | ||
* | ||
* @dataProvider loopBackAddressCasesProvider | ||
*/ | ||
public function testLoopBackAddressCases(string $host, bool $expectedResult) | ||
{ | ||
$isLoopBack = CredentialsUtils::isLoopBackAddress($host); | ||
$this->assertEquals($expectedResult, $isLoopBack); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function loopBackAddressCasesProvider(): array | ||
{ | ||
return [ | ||
'IPv6_invalid_loopBack' => | ||
[ | ||
'host' => '::2', | ||
'expected' => false | ||
], | ||
'IPv6_valid_loopBack' => | ||
[ | ||
'host' => '::1', | ||
'expected' => true | ||
], | ||
'IPv4_invalid_loopBack' => | ||
[ | ||
'host' => '192.168.0.1', | ||
'expected' => false | ||
], | ||
'IPv4_valid_loopBack' => | ||
[ | ||
'host' => '127.0.0.1', | ||
'expected' => true | ||
], | ||
'IPv4_valid_loopBack_2' => | ||
[ | ||
'host' => '127.0.0.255', | ||
'expected' => true | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.