Skip to content

Conversation

kevinrudde
Copy link
Contributor

Added support for EKS Pod Identity, see https://docs.aws.amazon.com/eks/latest/userguide/pod-identities.html

It is similar to the ContainerProvider, but uses the injected environment variables from the Pod Identity Agent.

@kevinrudde kevinrudde force-pushed the add-support-for-pod-identity branch 4 times, most recently from 0e0a48b to 6d2adb6 Compare October 16, 2024 08:12
Copy link
Member

@jderusse jderusse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this shiny documented PR 😍

I've just some concerns about introducing a new Provider / Versus reusing the ContainerProvider like the AWS-SDK does 🤔

*
* @see https://docs.aws.amazon.com/eks/latest/userguide/pod-identities.html
*/
final class PodIdentityProvider implements CredentialProvider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's about reusing the existing ContainerProvider instead?

It looks like this is what the official AWS SDK does: https://github.com/aws/aws-sdk-php/blob/master/src/Credentials/EcsCredentialProvider.php

In that way, we could leverage the AuthToken in both full / relative URI

Endpoint is one of:

  • http://169.254.170.2 . AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
  • pr AWS_CONTAINER_CREDENTIALS_FULL_URI

Auth Token is one of:

  • AWS_CONTAINER_AUTHORIZATION_TOKEN
  • content of AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE
  • nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed my implementation to reuse the ContainerProvider like the AWS SDK does 😄


use AsyncAws\Core\Exception\RuntimeException;

final class TokenFileLoader
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this class could be a trait.

Comment on lines 80 to 81
new ContainerProvider($httpClient, $logger),
new PodIdentityProvider($httpClient, $logger),
new InstanceProvider($httpClient, $logger),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to your PR, but I discovered that AWS uses either EscProvider or InstanceProvider in the default chain:

        if (self::shouldUseEcs()) {
            $defaultChain['ecs'] = self::ecsCredentials($config);
        } else {
            $defaultChain['instance'] = self::instanceProfile($config);
        } 

I think we could safely do the same 🤔


// fetch credentials from pod identity agent endpoint
try {
$response = $this->httpClient->request('GET', $fullUri, ['headers' => ['Authorization' => $tokenFileContent]]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinrudde kevinrudde force-pushed the add-support-for-pod-identity branch 3 times, most recently from 3e1cce1 to 18c06e5 Compare October 22, 2024 07:00
Copy link
Member

@jderusse jderusse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, just few minor comment for my side

}

$tokenFile = $configuration->get(Configuration::OPTION_POD_IDENTITY_AUTHORIZATION_TOKEN_FILE);
if (false === empty($tokenFile)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (false === empty($tokenFile)) {
if (!empty($tokenFile)) {

}
}

if (!$this->isUriValid($fullUri)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be move earlier (at line 60 for instance)

private function getFullUri(Configuration $configuration): ?string
{
$relativeUri = $configuration->get(Configuration::OPTION_CONTAINER_CREDENTIALS_RELATIVE_URI);
$fullUri = $configuration->get(Configuration::OPTION_POD_IDENTITY_CREDENTIALS_FULL_URI);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be moved at line 144 (no need to instanciate the fullUri variable when the relativeUri is not null

*
* @return bool true if the IP is a loopback address, false otherwise
*/
public static function isLoopBackAddress(string $host)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to be static, nor to be public.
lets avoid exposing ( = maintaining) new methods

if ('https' !== $parsedUri['scheme']) {
$host = trim($parsedUri['host'] ?? '', '[]');

$ecsHost = parse_url(self::ECS_ENDPOINT)['host'] ?? '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to parse the entier url, when you are looking only for the host

Suggested change
$ecsHost = parse_url(self::ECS_ENDPOINT)['host'] ?? '';
$ecsHost = parse_url(self::ECS_ENDPOINT, \PHP_URL_HOST) ?: '';

@kevinrudde kevinrudde force-pushed the add-support-for-pod-identity branch from 18c06e5 to 8d07181 Compare October 22, 2024 08:47
@kevinrudde
Copy link
Contributor Author

Hey @jderusse 👋
is there an ETA for merging this? 😄

@jderusse jderusse merged commit 74b61ed into async-aws:master Oct 29, 2024
16 checks passed
@jderusse
Copy link
Member

thank you @kevinrudde

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants