Repository navigation
Backport #1116 to 3.x: Read Arrow results with the connection session's credentials #1119
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,69 +127,18 @@ def __init__( | |
| def __s3_file_system(self): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Independent review (relayed): CLEAN Reviewer: Codex CLI 0.162.1 ( Covered (as reported): both diffs and the factory/test correspondence; the commit message's resolutions against #881, #940, and #885 history; connection credentials for default/static/process/SSO profiles, profile-based roles, role/MFA arguments, supplied sessions, and absent/error paths;
Non-actionable (as reported): |
||
| from pyarrow import fs | ||
|
|
||
| connection = self.connection | ||
|
|
||
| # Build timeout parameters dict | ||
| timeout_kwargs = {} | ||
| kwargs: dict[str, Any] = {"region": self.connection.region_name} | ||
| if self._connect_timeout is not None: | ||
| timeout_kwargs["connect_timeout"] = self._connect_timeout | ||
| kwargs["connect_timeout"] = self._connect_timeout | ||
| if self._request_timeout is not None: | ||
| timeout_kwargs["request_timeout"] = self._request_timeout | ||
|
|
||
| if connection._kwargs.get("role_arn"): | ||
| external_id = connection._kwargs.get("external_id") | ||
| fs = fs.S3FileSystem( | ||
| role_arn=connection._kwargs["role_arn"], | ||
| session_name=connection._kwargs["role_session_name"], | ||
| external_id="" if external_id is None else external_id, | ||
| load_frequency=connection._kwargs["duration_seconds"], | ||
| region=connection.region_name, | ||
| **timeout_kwargs, | ||
| ) | ||
| elif connection.profile_name: | ||
| profile = connection.session._session.full_config["profiles"][connection.profile_name] | ||
| fs = fs.S3FileSystem( | ||
| access_key=profile.get("aws_access_key_id", None), | ||
| secret_key=profile.get("aws_secret_access_key", None), | ||
| session_token=profile.get("aws_session_token", None), | ||
| region=connection.region_name, | ||
| **timeout_kwargs, | ||
| ) | ||
| else: | ||
| # Try explicit credentials first | ||
| explicit_access_key = connection._kwargs.get("aws_access_key_id") | ||
| explicit_secret_key = connection._kwargs.get("aws_secret_access_key") | ||
|
|
||
| if explicit_access_key and explicit_secret_key: | ||
| # Use explicitly provided credentials | ||
| fs = fs.S3FileSystem( | ||
| access_key=explicit_access_key, | ||
| secret_key=explicit_secret_key, | ||
| session_token=connection._kwargs.get("aws_session_token"), | ||
| region=connection.region_name, | ||
| **timeout_kwargs, | ||
| ) | ||
| else: | ||
| # Fall back to dynamic credentials from boto3 session | ||
| # This handles EC2 instance profiles, temporary credentials, etc. | ||
| try: | ||
| credentials = connection.session._session.get_credentials() | ||
| if credentials: | ||
| fs = fs.S3FileSystem( | ||
| access_key=credentials.access_key, | ||
| secret_key=credentials.secret_key, | ||
| session_token=credentials.token, | ||
| region=connection.region_name, | ||
| **timeout_kwargs, | ||
| ) | ||
| else: | ||
| # Fall back to default (no explicit credentials) | ||
| fs = fs.S3FileSystem(region=connection.region_name, **timeout_kwargs) | ||
| except Exception: | ||
| # Fall back to default if credential retrieval fails | ||
| fs = fs.S3FileSystem(region=connection.region_name, **timeout_kwargs) | ||
|
|
||
| return fs | ||
| kwargs["request_timeout"] = self._request_timeout | ||
| credentials = self.connection.session.get_credentials() | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Self-review round one (implementation behavior): FINDINGS (1, repaired) Base Covered: the three changed files against #1116 (
Validation after repair: |
||
| if credentials: | ||
| frozen_credentials = credentials.get_frozen_credentials() | ||
| kwargs["access_key"] = frozen_credentials.access_key | ||
| kwargs["secret_key"] = frozen_credentials.secret_key | ||
| kwargs["session_token"] = frozen_credentials.token | ||
| return fs.S3FileSystem(**kwargs) | ||
|
|
||
| @property | ||
| def timestamp_parsers(self) -> list[str]: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # Copyright 2026 The PyAthena authors | ||
| # | ||
| # Licensed under the MIT License. | ||
| # See LICENSE or https://opensource.org/licenses/MIT. | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| import os | ||
| import sys | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from pyathena.arrow.result_set import AthenaArrowResultSet | ||
| from pyathena.connection import Connection | ||
|
|
||
|
|
||
| class TestAthenaArrowResultSet: | ||
| @pytest.mark.parametrize( | ||
| ("connection_kwargs", "expected_access_key"), | ||
| [ | ||
| ({}, "DEFAULTKEY"), | ||
| ({"profile_name": "static"}, "STATICKEY"), | ||
| ({"profile_name": "process"}, "PROCESSKEY"), | ||
| ( | ||
| {"profile_name": "process", "role_arn": "arn:aws:iam::123456789012:role/r"}, | ||
| "ROLEKEY", | ||
| ), | ||
| ], | ||
| ids=["default", "static", "credential_process", "role_arn"], | ||
| ) | ||
| def test_create_s3_file_system_uses_session_credentials( | ||
| self, monkeypatch, tmp_path, connection_kwargs, expected_access_key | ||
| ): | ||
| """The pyarrow filesystem reads with the credentials of the connection's session. | ||
|
|
||
| No AWS calls; the profiles come from temporary config files, and the role | ||
| assumption is mocked. | ||
| """ | ||
| for key in list(os.environ): | ||
| if key.startswith("AWS_"): | ||
| monkeypatch.delenv(key) | ||
| process = tmp_path / "credential_process.py" | ||
| process.write_text( | ||
| 'print(\'{"Version": 1, "AccessKeyId": "PROCESSKEY", "SecretAccessKey": "secret"}\')\n' | ||
| ) | ||
| config = tmp_path / "config" | ||
| config.write_text( | ||
| "[default]\naws_access_key_id = DEFAULTKEY\naws_secret_access_key = secret\n" | ||
| "[profile static]\naws_access_key_id = STATICKEY\naws_secret_access_key = secret\n" | ||
| f'[profile process]\ncredential_process = "{sys.executable}" "{process}"\n' | ||
| ) | ||
| monkeypatch.setenv("AWS_CONFIG_FILE", str(config)) | ||
| monkeypatch.setenv("AWS_SHARED_CREDENTIALS_FILE", str(tmp_path / "credentials")) | ||
| role_credentials = { | ||
| "AccessKeyId": "ROLEKEY", | ||
| "SecretAccessKey": "secret", | ||
| "SessionToken": "token", | ||
| } | ||
| with patch.object(Connection, "_assume_role", return_value=role_credentials): | ||
| connection = Connection( | ||
| region_name="us-east-1", s3_staging_dir="s3://bucket/path/", **connection_kwargs | ||
| ) | ||
| with connection: | ||
| result_set = AthenaArrowResultSet.__new__(AthenaArrowResultSet) # bypass __init__ | ||
| result_set._connection = connection | ||
| result_set._connect_timeout = None | ||
| result_set._request_timeout = None | ||
| fs = result_set._AthenaArrowResultSet__s3_file_system() | ||
|
|
||
| # pyarrow exposes the filesystem options only through its pickle support. | ||
| options = fs.__reduce__()[1][0] | ||
| assert options["access_key"] == expected_access_key | ||
| assert options["role_arn"] == "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self-review round two (claims, compatibility, operations): CLEAN
Base
7fc7f07310f7f32af08afacb0d5eb8ea80a61746, heade3caedc9bcf214d51588c21b7dcc8f7458bd9b21.Claims checked:
f4cc848b, merged ina86a180e); the paragraph rewrite from Correct user guide claims that contradict the implementation #940 (8bd91260, merged in23ff61f6); the PyArrow 10 note removal from Require pandas 3.0 and pyarrow 22.0 #885 (842f0f05). The cherry-pick source SHA is209549db8ff551dfd66290bdbad19130e129aff3(verified withgit rev-parse; an earlier local draft of the message had a wrong SHA and was corrected before the first push).session_tokenfirst appears in_s3fs.pyxatapache-arrow-2.0.0(absent at 1.0.0);connect_timeout/request_timeoutat 10.0.0 (absent at 9.0.0);access_key/secret_keyat 0.17.0.Connection(same branches as master).docs/usage.md:743-791) describes connection arguments and does not mention Arrow.Deferred (pre-existing, not introduced): 3.x's sentence that the timeouts "apply to all S3 operations performed by the cursor, including HeadObject and GetObject" is inaccurate; master corrected it in #940, which is not part of this backport.
Operations: as on master, one fewer
sts:AssumeRoleper Arrow result set withrole_arn.