-
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
When using IMDS for fetching credentials, customers should be able to provide their custom endpoint when desired, and that is what this change does. Basically, customer can provide a custom endpoint by doing one of the following options: Please note that a valid URI value needs to be provided as endpoint, otherwise a credential exception will be thrown. - Providing a parameter called 'ec2_metadata_service_endpoint' to the constructor of the InstanceProfileProvider. - By setting an environment variable called AWS_EC2_METADATA_SERVICE_ENDPOINT with the desired custom endpoint. - By defining a key-value config in the config file ~/.aws/config where its key is ec2_metadata_service_endpoint and its value is the desired custom endpoint. This commit also includes customizing the endpoint mode, which is used to change how the default endpoint is resolved. The valid endpoint mode are IPv4 and IPv6. Here are the different options for setting the endpoint mode: - Providing a parameter called 'ec2_metadata_service_endpoint_mode' in the constructor parameters for InstanceProfileProvider. - By setting an environment variable called AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE with the desired custom endpoint mode. - By defining a key-value config in the config file ~/.aws/config where its key is ec2_metadata_service_endpoint_mode and its value is the desired custom endpoint mode. Please note that the reason why the parameter we pass in the constructor is not 'endpoint' is because the parameter name conflicts with service client configurations, and when using the default credential resolution internally we pass the client configuration to the credential provider, and if a custom endpoint was provided for the service client it will also pass this custom endpoint to the IMDS credential provider, which will make it to fail.
- Loading branch information
1 parent
63c7202
commit 21a1125
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.