diff --git a/packages/aws-credentials-sts/pyproject.toml b/packages/aws-credentials-sts/pyproject.toml index fe18d2b5..ce8267d3 100644 --- a/packages/aws-credentials-sts/pyproject.toml +++ b/packages/aws-credentials-sts/pyproject.toml @@ -50,6 +50,9 @@ WebIdentityTokenEnv = "aws_credentials_sts.aio.providers:AsyncWebIdentityTokenEn [dependency-groups] test = [ + "aws-credentials-http~=0.4.0", + "aws-credentials-imds~=0.4.0", + "aws-sdk-s3~=0.12.0", "pytest>=9.0.3,<10.0.0", "pytest-asyncio>=1.3.0,<1.4.0", ] @@ -58,6 +61,9 @@ test = [ asyncio_mode = "auto" [tool.uv.sources] +aws-credentials-http = { path = "../aws-credentials-http", editable = true } +aws-credentials-imds = { path = "../aws-credentials-imds", editable = true } +aws-sdk-s3 = { path = "../../clients/aws-sdk-s3", editable = true } aws-sdk-sts = { path = "../../clients/aws-sdk-sts", editable = true } [build-system] diff --git a/packages/aws-credentials-sts/tests/functional/__init__.py b/packages/aws-credentials-sts/tests/functional/__init__.py new file mode 100644 index 00000000..04f8b7b7 --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/packages/aws-credentials-sts/tests/functional/_transports.py b/packages/aws-credentials-sts/tests/functional/_transports.py new file mode 100644 index 00000000..e0759437 --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/_transports.py @@ -0,0 +1,101 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import re +from urllib.parse import parse_qs + +from smithy_core.aio.utils import read_streaming_blob_async +from smithy_core.utils import read_streaming_blob +from smithy_http import HTTPResponse +from smithy_http.aio.testing import AsyncMockHTTPClient +from smithy_http.interfaces import HTTPRequest, HTTPRequestConfiguration +from smithy_http.testing import MockHTTPClient + +_LIST_BUCKETS_RESPONSE = ( + b"owner" + b"" +) +_ECS_RESPONSE = ( + b'{"AccessKeyId": "ECS_AKID", "SecretAccessKey": "SECRET", "Token": "TOKEN",' + b' "Expiration": "2999-01-01T00:00:00Z"}' +) +_IMDS_RESPONSE = ( + b'{"Code": "Success", "AccessKeyId": "IMDS_AKID", "SecretAccessKey": "SECRET",' + b' "Token": "TOKEN", "Expiration": "2999-01-01T00:00:00Z"}' +) + + +class CredentialSourceTransport(MockHTTPClient): + """Answers every credential source, and S3, with a recognizable access key.""" + + def send( + self, + request: HTTPRequest, + *, + request_config: HTTPRequestConfiguration | None = None, + ) -> HTTPResponse: + body = read_streaming_blob(request.body) + # Reading consumed the body, and the pipeline still sends this request. + request.body = body + self.add_response(*_answer(request, body)) + return super().send(request, request_config=request_config) + + +class AsyncCredentialSourceTransport(AsyncMockHTTPClient): + """Answers every credential source, and S3, with a recognizable access key.""" + + async def send( + self, + request: HTTPRequest, + *, + request_config: HTTPRequestConfiguration | None = None, + ) -> HTTPResponse: + body = await read_streaming_blob_async(request.body) + # Reading consumed the body, and the pipeline still sends this request. + request.body = body + self.add_response(*_answer(request, body)) + return await super().send(request, request_config=request_config) + + +def signing_access_key(transport: MockHTTPClient | AsyncMockHTTPClient) -> str: + """Return the access key the last request was signed with.""" + request = transport.captured_requests[-1] + for field in request.fields: + if field.name.lower() == "authorization": + match = re.search(r"Credential=([^/]+)/", field.values[0]) + assert match is not None, field.values[0] + return match.group(1) + raise AssertionError("the last request wasn't signed") + + +def _answer( + request: HTTPRequest, body: bytes +) -> tuple[int, list[tuple[str, str]], bytes]: + host = request.destination.host + path = request.destination.path or "" + if host.startswith("sts."): + form = parse_qs(body.decode()) + action = form["Action"][0] + role_name = form["RoleArn"][0].rsplit("/", 1)[-1] + # The action and role name tell apart the sources that call STS. + return 200, [("content-type", "text/xml")], _sts_response(action, role_name) + if host == "169.254.169.254": + if request.method == "PUT": + return 200, [], b"imds-session-token" + if path.endswith("security-credentials/"): + return 200, [], b"imds-role" + return 200, [], _IMDS_RESPONSE + if host == "127.0.0.1": + return 200, [("content-type", "application/json")], _ECS_RESPONSE + return 200, [("content-type", "application/xml")], _LIST_BUCKETS_RESPONSE + + +def _sts_response(action: str, role_name: str) -> bytes: + return ( + f'<{action}Response xmlns="https://sts.amazonaws.com/doc/2011-06-15/">' + f"<{action}Result>{action}.{role_name}" + "SECRET" + "TOKEN" + "2999-01-01T00:00:00Z" + f"" + ).encode() diff --git a/packages/aws-credentials-sts/tests/functional/aio/__init__.py b/packages/aws-credentials-sts/tests/functional/aio/__init__.py new file mode 100644 index 00000000..04f8b7b7 --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/aio/__init__.py @@ -0,0 +1,2 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 diff --git a/packages/aws-credentials-sts/tests/functional/aio/test_credential_chain.py b/packages/aws-credentials-sts/tests/functional/aio/test_credential_chain.py new file mode 100644 index 00000000..f36bf6c9 --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/aio/test_credential_chain.py @@ -0,0 +1,117 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import sys +from pathlib import Path + +import pytest +from aws_sdk_s3.aio.client import AsyncS3Client +from aws_sdk_s3.aio.config import AsyncS3Config +from aws_sdk_s3.models import ListBucketsInput +from smithy_aws_core.identity.chain import IdentityChainError + +from .._transports import AsyncCredentialSourceTransport, signing_access_key + +# CAUTION: these tests pin the order credential sources are tried in. Customers rely +# on it staying the same across upgrades, whatever the specification says. + + +async def test_each_source_is_used_until_it_is_gone( + isolated_environment: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Removing each credential source in chain order hands over to the next.""" + process = tmp_path / "credential_process.py" + process.write_text( + "import json\n" + "print(json.dumps({'Version': 1, 'AccessKeyId': 'PROCESS_AKID'," + " 'SecretAccessKey': 'SECRET'}))\n" + ) + token_file = tmp_path / "web_identity_token" + token_file.write_text("web-identity-token") + env = isolated_environment + + env.setenv("AWS_ACCESS_KEY_ID", "ENV_AKID") + env.setenv("AWS_SECRET_ACCESS_KEY", "SECRET") + env.setenv("AWS_ROLE_ARN", "arn:aws:iam::123456789012:role/env-web-role") + env.setenv("AWS_WEB_IDENTITY_TOKEN_FILE", str(token_file)) + env.setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://127.0.0.1/credentials") + (tmp_path / "config").write_text( + "[default]\n" + "region = us-west-2\n" + "role_arn = arn:aws:iam::123456789012:role/profile-role\n" + "source_profile = base\n" + f"web_identity_token_file = {token_file}\n" + f"credential_process = {sys.executable} {process}\n" + "[profile base]\n" + "region = us-west-2\n" + ) + (tmp_path / "credentials").write_text( + "[default]\n" + "aws_access_key_id = SESSION_AKID\n" + "aws_secret_access_key = SECRET\n" + "aws_session_token = TOKEN\n" + "[base]\n" + "aws_access_key_id = BASE_AKID\n" + "aws_secret_access_key = SECRET\n" + ) + + # Environment variables + assert await _signing_access_key() == "ENV_AKID" + + # Web identity from environment variables + env.delenv("AWS_ACCESS_KEY_ID") + env.delenv("AWS_SECRET_ACCESS_KEY") + assert await _signing_access_key() == "AssumeRoleWithWebIdentity.env-web-role" + + # Profile role assumption with a source profile + env.delenv("AWS_ROLE_ARN") + env.delenv("AWS_WEB_IDENTITY_TOKEN_FILE") + assert await _signing_access_key() == "AssumeRole.profile-role" + + # Profile web identity + (tmp_path / "config").write_text( + "[default]\n" + "region = us-west-2\n" + "role_arn = arn:aws:iam::123456789012:role/profile-role\n" + f"web_identity_token_file = {token_file}\n" + f"credential_process = {sys.executable} {process}\n" + ) + assert await _signing_access_key() == "AssumeRoleWithWebIdentity.profile-role" + + # Profile session keys. A profile with role_arn never uses its own keys, so + # they only take effect once the role is gone. + (tmp_path / "config").write_text( + f"[default]\nregion = us-west-2\ncredential_process = {sys.executable} {process}\n" + ) + assert await _signing_access_key() == "SESSION_AKID" + + # Profile static keys + (tmp_path / "credentials").write_text( + "[default]\naws_access_key_id = STATIC_AKID\naws_secret_access_key = SECRET\n" + ) + assert await _signing_access_key() == "STATIC_AKID" + + # Profile credential process + (tmp_path / "credentials").write_text("") + assert await _signing_access_key() == "PROCESS_AKID" + + # Container credentials + (tmp_path / "config").write_text("[default]\nregion = us-west-2\n") + assert await _signing_access_key() == "ECS_AKID" + + # EC2 instance metadata + env.delenv("AWS_CONTAINER_CREDENTIALS_FULL_URI") + assert await _signing_access_key() == "IMDS_AKID" + + # No source left + env.setenv("AWS_EC2_METADATA_DISABLED", "true") + with pytest.raises(IdentityChainError): + await _signing_access_key() + + +async def _signing_access_key() -> str: + transport = AsyncCredentialSourceTransport() + config = await AsyncS3Config.resolve(region="us-west-2", transport=transport) + async with AsyncS3Client(config) as client: + await client.list_buckets(ListBucketsInput()) + return signing_access_key(transport) diff --git a/packages/aws-credentials-sts/tests/functional/conftest.py b/packages/aws-credentials-sts/tests/functional/conftest.py new file mode 100644 index 00000000..bb12225e --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/conftest.py @@ -0,0 +1,21 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import os +from pathlib import Path + +import pytest + + +@pytest.fixture(autouse=True) +def isolated_environment( + monkeypatch: pytest.MonkeyPatch, tmp_path: Path +) -> pytest.MonkeyPatch: + """Hide the developer's AWS configuration, so only the test's sources exist.""" + for name in os.environ: + if name.startswith("AWS_"): + monkeypatch.delenv(name) + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("AWS_CONFIG_FILE", str(tmp_path / "config")) + monkeypatch.setenv("AWS_SHARED_CREDENTIALS_FILE", str(tmp_path / "credentials")) + return monkeypatch diff --git a/packages/aws-credentials-sts/tests/functional/test_credential_chain.py b/packages/aws-credentials-sts/tests/functional/test_credential_chain.py new file mode 100644 index 00000000..65765638 --- /dev/null +++ b/packages/aws-credentials-sts/tests/functional/test_credential_chain.py @@ -0,0 +1,116 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +import sys +from pathlib import Path + +import pytest +from aws_sdk_s3.client import S3Client +from aws_sdk_s3.config import S3Config +from aws_sdk_s3.models import ListBucketsInput +from smithy_aws_core.identity.chain import IdentityChainError + +from ._transports import CredentialSourceTransport, signing_access_key + +# CAUTION: these tests pin the order credential sources are tried in. Customers rely +# on it staying the same across upgrades, whatever the specification says. + + +def test_each_source_is_used_until_it_is_gone( + isolated_environment: pytest.MonkeyPatch, tmp_path: Path +) -> None: + """Removing each credential source in chain order hands over to the next.""" + process = tmp_path / "credential_process.py" + process.write_text( + "import json\n" + "print(json.dumps({'Version': 1, 'AccessKeyId': 'PROCESS_AKID'," + " 'SecretAccessKey': 'SECRET'}))\n" + ) + token_file = tmp_path / "web_identity_token" + token_file.write_text("web-identity-token") + env = isolated_environment + + env.setenv("AWS_ACCESS_KEY_ID", "ENV_AKID") + env.setenv("AWS_SECRET_ACCESS_KEY", "SECRET") + env.setenv("AWS_ROLE_ARN", "arn:aws:iam::123456789012:role/env-web-role") + env.setenv("AWS_WEB_IDENTITY_TOKEN_FILE", str(token_file)) + env.setenv("AWS_CONTAINER_CREDENTIALS_FULL_URI", "http://127.0.0.1/credentials") + (tmp_path / "config").write_text( + "[default]\n" + "region = us-west-2\n" + "role_arn = arn:aws:iam::123456789012:role/profile-role\n" + "source_profile = base\n" + f"web_identity_token_file = {token_file}\n" + f"credential_process = {sys.executable} {process}\n" + "[profile base]\n" + "region = us-west-2\n" + ) + (tmp_path / "credentials").write_text( + "[default]\n" + "aws_access_key_id = SESSION_AKID\n" + "aws_secret_access_key = SECRET\n" + "aws_session_token = TOKEN\n" + "[base]\n" + "aws_access_key_id = BASE_AKID\n" + "aws_secret_access_key = SECRET\n" + ) + + # Environment variables + assert _signing_access_key() == "ENV_AKID" + + # Web identity from environment variables + env.delenv("AWS_ACCESS_KEY_ID") + env.delenv("AWS_SECRET_ACCESS_KEY") + assert _signing_access_key() == "AssumeRoleWithWebIdentity.env-web-role" + + # Profile role assumption with a source profile + env.delenv("AWS_ROLE_ARN") + env.delenv("AWS_WEB_IDENTITY_TOKEN_FILE") + assert _signing_access_key() == "AssumeRole.profile-role" + + # Profile web identity + (tmp_path / "config").write_text( + "[default]\n" + "region = us-west-2\n" + "role_arn = arn:aws:iam::123456789012:role/profile-role\n" + f"web_identity_token_file = {token_file}\n" + f"credential_process = {sys.executable} {process}\n" + ) + assert _signing_access_key() == "AssumeRoleWithWebIdentity.profile-role" + + # Profile session keys. A profile with role_arn never uses its own keys, so + # they only take effect once the role is gone. + (tmp_path / "config").write_text( + f"[default]\nregion = us-west-2\ncredential_process = {sys.executable} {process}\n" + ) + assert _signing_access_key() == "SESSION_AKID" + + # Profile static keys + (tmp_path / "credentials").write_text( + "[default]\naws_access_key_id = STATIC_AKID\naws_secret_access_key = SECRET\n" + ) + assert _signing_access_key() == "STATIC_AKID" + + # Profile credential process + (tmp_path / "credentials").write_text("") + assert _signing_access_key() == "PROCESS_AKID" + + # Container credentials + (tmp_path / "config").write_text("[default]\nregion = us-west-2\n") + assert _signing_access_key() == "ECS_AKID" + + # EC2 instance metadata + env.delenv("AWS_CONTAINER_CREDENTIALS_FULL_URI") + assert _signing_access_key() == "IMDS_AKID" + + # No source left + env.setenv("AWS_EC2_METADATA_DISABLED", "true") + with pytest.raises(IdentityChainError): + _signing_access_key() + + +def _signing_access_key() -> str: + transport = CredentialSourceTransport() + client = S3Client(S3Config.resolve(region="us-west-2", transport=transport)) + client.list_buckets(ListBucketsInput()) + return signing_access_key(transport)