generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 24
IMDS Credentials Resolver #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0b21719
Add basic IMDS credentials resolver
jonathan343 3d7489e
Use old `security-credentials` instead of `security-credentials-exten…
jonathan343 a6cee60
Use async lock
jonathan343 df131f3
Address initial feedback
jonathan343 3f79e6f
Move ttl validation to Config. Make config a parameter for TokenCache.
jonathan343 28cc2d5
Store Token value as a string instead of bytes
jonathan343 878e7b2
Add tests for Config, Token, and TokenCache
jonathan343 5fb2c43
whitespace linter fix
jonathan343 f7daf20
Add tests for EC2Metadata and IMDSCredentialsResolver classes
jonathan343 1b920a5
Add todo for aws shared config and env var support
jonathan343 b0622c3
Only use reuse connections in the pool if they're open
jonathan343 8db26d2
Remove unnecessary doc strings
jonathan343 dfac021
testing
jonathan343 690de0e
Remove port config option and use endpoint_uri.port instead
jonathan343 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
234 changes: 234 additions & 0 deletions
234
packages/smithy-aws-core/src/smithy_aws_core/credentials_resolvers/imds.py
This file contains hidden or 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,234 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import json | ||
| import asyncio | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timedelta, timezone | ||
| from typing import Literal | ||
|
|
||
| from smithy_core import URI | ||
| from smithy_core.aio.interfaces.identity import IdentityResolver | ||
| from smithy_core.exceptions import SmithyIdentityException | ||
| from smithy_core.interfaces.identity import IdentityProperties | ||
| from smithy_core.interfaces.retries import RetryStrategy | ||
| from smithy_core.retries import SimpleRetryStrategy | ||
| from smithy_http import Field, Fields | ||
| from smithy_http.aio import HTTPRequest | ||
| from smithy_http.aio.interfaces import HTTPClient | ||
|
|
||
| from smithy_aws_core.identity import AWSCredentialsIdentity | ||
|
|
||
|
|
||
| @dataclass(init=False) | ||
| class Config: | ||
| """Configuration for EC2Metadata.""" | ||
|
|
||
| _HOST_MAPPING = {"IPv4": "169.254.169.254", "IPv6": "[fd00:ec2::254]"} | ||
| _MIN_TTL = 5 | ||
| _MAX_TTL = 21600 | ||
|
|
||
| retry_strategy: RetryStrategy | ||
| endpoint_uri: URI | ||
| endpoint_mode: Literal["IPv4", "IPv6"] | ||
| port: int | ||
| token_ttl: int | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| retry_strategy: RetryStrategy | None = None, | ||
| endpoint_uri: URI | None = None, | ||
| endpoint_mode: Literal["IPv4", "IPv6"] = "IPv4", | ||
| port: int = 80, | ||
| token_ttl: int = _MAX_TTL, | ||
| ec2_instance_profile_name: str | None = None, | ||
| ): | ||
| # TODO: Implement retries. | ||
| self.retry_strategy = retry_strategy or SimpleRetryStrategy(max_attempts=3) | ||
| self.endpoint_mode = endpoint_mode | ||
| self.endpoint_uri = self._resolve_endpoint(endpoint_uri, endpoint_mode) | ||
| self.port = port | ||
| self.token_ttl = self._validate_token_ttl(token_ttl) | ||
| self.ec2_instance_profile_name = ec2_instance_profile_name | ||
|
|
||
| def _validate_token_ttl(self, ttl: int) -> int: | ||
| """Validates the token TTL value.""" | ||
| if not self._MIN_TTL <= ttl <= self._MAX_TTL: | ||
| raise ValueError( | ||
| f"Token TTL must be between {self._MIN_TTL} and {self._MAX_TTL} seconds." | ||
| ) | ||
| return ttl | ||
|
|
||
| def _resolve_endpoint( | ||
| self, endpoint_uri: URI | None, endpoint_mode: Literal["IPv4", "IPv6"] | ||
| ) -> URI: | ||
| if endpoint_uri is not None: | ||
| return endpoint_uri | ||
|
|
||
| return URI( | ||
| scheme="http", | ||
| host=self._HOST_MAPPING.get(endpoint_mode, self._HOST_MAPPING["IPv4"]), | ||
| ) | ||
|
|
||
|
|
||
| class Token: | ||
| """Represents an IMDSv2 session token with a value and method for checking | ||
| expiration.""" | ||
|
|
||
| def __init__(self, value: str, ttl: int): | ||
| self._value = value | ||
| self._ttl = ttl | ||
| self._created_time = datetime.now() | ||
|
|
||
| def is_expired(self) -> bool: | ||
| """Check if the token has expired.""" | ||
| return datetime.now() - self._created_time >= timedelta(seconds=self._ttl) | ||
|
|
||
| @property | ||
| def value(self) -> str: | ||
| return self._value | ||
|
|
||
|
|
||
| class TokenCache: | ||
| """Holds the token needed to fetch instance metadata. | ||
|
|
||
| In addition, it knows how to refresh itself. | ||
| """ | ||
|
|
||
| _TOKEN_PATH = "/latest/api/token" | ||
|
|
||
| def __init__(self, http_client: HTTPClient, config: Config): | ||
| self._http_client = http_client | ||
| self._config = config | ||
| self._base_uri = config.endpoint_uri | ||
| self._refresh_lock = asyncio.Lock() | ||
| self._token = None | ||
|
|
||
| def _should_refresh(self) -> bool: | ||
| """Determines if the token should be refreshed.""" | ||
| return self._token is None or self._token.is_expired() | ||
|
|
||
| async def _refresh(self) -> None: | ||
| """Refreshes the token if needed, with thread safety.""" | ||
| async with self._refresh_lock: | ||
| if not self._should_refresh(): | ||
| return | ||
| headers = Fields( | ||
| [ | ||
| # TODO: Add user-agent | ||
| Field( | ||
| name="x-aws-ec2-metadata-token-ttl-seconds", | ||
| values=[str(self._config.token_ttl)], | ||
| ), | ||
| ] | ||
| ) | ||
| request = HTTPRequest( | ||
| method="PUT", | ||
| destination=URI( | ||
| scheme=self._base_uri.scheme, | ||
| host=self._base_uri.host, | ||
| path=self._TOKEN_PATH, | ||
| ), | ||
| fields=headers, | ||
| ) | ||
| response = await self._http_client.send(request) | ||
| token_value = await response.consume_body_async() | ||
| self._token = Token(token_value.decode("utf-8"), self._config.token_ttl) | ||
|
|
||
| async def get_token(self) -> Token: | ||
| """Get the current token, refreshing it if expired.""" | ||
| if self._should_refresh(): | ||
| await self._refresh() | ||
| assert self._token is not None | ||
| return self._token | ||
|
|
||
|
|
||
| class EC2Metadata: | ||
| def __init__(self, http_client: HTTPClient, config: Config | None = None): | ||
| self._http_client = http_client | ||
| self._config = config or Config() | ||
| self._token_cache = TokenCache( | ||
| http_client=self._http_client, config=self._config | ||
| ) | ||
|
|
||
| async def get(self, *, path: str) -> str: | ||
| token = await self._token_cache.get_token() | ||
| headers = Fields( | ||
| [ | ||
| # TODO: Add user-agent | ||
jonathan343 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Field( | ||
| name="x-aws-ec2-metadata-token", | ||
| values=[token.value], | ||
| ) | ||
| ] | ||
| ) | ||
| request = HTTPRequest( | ||
| method="GET", | ||
| destination=URI( | ||
| scheme=self._config.endpoint_uri.scheme, | ||
| host=self._config.endpoint_uri.host, | ||
| port=self._config.port, | ||
| path=path, | ||
| ), | ||
| fields=headers, | ||
| ) | ||
| response = await self._http_client.send(request=request) | ||
| body = await response.consume_body_async() | ||
| return body.decode("utf-8") | ||
|
|
||
|
|
||
| class IMDSCredentialsResolver( | ||
| IdentityResolver[AWSCredentialsIdentity, IdentityProperties] | ||
| ): | ||
| """Resolves AWS Credentials from an EC2 Instance Metadata Service (IMDS) client.""" | ||
|
|
||
| _METADATA_PATH_BASE = "/latest/meta-data/iam/security-credentials" | ||
|
|
||
| def __init__(self, http_client: HTTPClient, config: Config | None = None): | ||
| # TODO: Respect IMDS specific config values from aws shared config file and environment. | ||
| self._http_client = http_client | ||
| self._ec2_metadata_client = EC2Metadata(http_client=http_client, config=config) | ||
| self._config = config or Config() | ||
| self._credentials = None | ||
| self._profile_name = self._config.ec2_instance_profile_name | ||
|
|
||
| async def get_identity( | ||
| self, *, identity_properties: IdentityProperties | ||
| ) -> AWSCredentialsIdentity: | ||
| if ( | ||
| self._credentials is not None | ||
| and self._credentials.expiration | ||
| and datetime.now(timezone.utc) < self._credentials.expiration | ||
| ): | ||
| return self._credentials | ||
|
|
||
| profile = self._profile_name | ||
| if profile is None: | ||
| profile = await self._ec2_metadata_client.get(path=self._METADATA_PATH_BASE) | ||
|
|
||
| creds_str = await self._ec2_metadata_client.get( | ||
| path=f"{self._METADATA_PATH_BASE}/{profile}" | ||
| ) | ||
| creds = json.loads(creds_str) | ||
|
|
||
| access_key_id = creds.get("AccessKeyId") | ||
| secret_access_key = creds.get("SecretAccessKey") | ||
| session_token = creds.get("Token") | ||
| account_id = creds.get("AccountId") | ||
| expiration = creds.get("Expiration") | ||
| if expiration is not None: | ||
| expiration = datetime.fromisoformat(expiration).replace(tzinfo=timezone.utc) | ||
|
|
||
| if access_key_id is None or secret_access_key is None: | ||
| raise SmithyIdentityException( | ||
| "AccessKeyId and SecretAccessKey are required" | ||
| ) | ||
|
|
||
| self._credentials = AWSCredentialsIdentity( | ||
| access_key_id=access_key_id, | ||
| secret_access_key=secret_access_key, | ||
| session_token=session_token, | ||
| expiration=expiration, | ||
| account_id=account_id, | ||
| ) | ||
| return self._credentials | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.