-
Notifications
You must be signed in to change notification settings - Fork 414
Introduce AuthManager #1908
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
Introduce AuthManager #1908
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
File renamed without changes.
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,82 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import base64 | ||
| from abc import ABC, abstractmethod | ||
| from typing import Optional | ||
|
|
||
| from requests import PreparedRequest | ||
| from requests.auth import AuthBase | ||
|
|
||
|
|
||
| class AuthManager(ABC): | ||
| """ | ||
| Abstract base class for Authentication Managers used to supply authorization headers to HTTP clients (e.g. requests.Session). | ||
|
|
||
| Subclasses must implement the `auth_header` method to return an Authorization header value. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def auth_header(self) -> Optional[str]: | ||
| """Return the Authorization header value, or None if not applicable.""" | ||
|
|
||
|
|
||
| class NoopAuthManager(AuthManager): | ||
| def auth_header(self) -> Optional[str]: | ||
| return None | ||
|
|
||
|
|
||
| class BasicAuthManager(AuthManager): | ||
| def __init__(self, username: str, password: str): | ||
| credentials = f"{username}:{password}" | ||
| self._token = base64.b64encode(credentials.encode()).decode() | ||
|
|
||
| def auth_header(self) -> str: | ||
| return f"Basic {self._token}" | ||
|
|
||
|
|
||
| class AuthManagerAdapter(AuthBase): | ||
| """A `requests.auth.AuthBase` adapter that integrates an `AuthManager` into a `requests.Session` to automatically attach the appropriate Authorization header to every request. | ||
sungwy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| This adapter is useful when working with `requests.Session.auth` | ||
| and allows reuse of authentication strategies defined by `AuthManager`. | ||
| This AuthManagerAdapter is only intended to be used against the REST Catalog | ||
| Server that expects the Authorization Header. | ||
| """ | ||
|
|
||
| def __init__(self, auth_manager: AuthManager): | ||
| """ | ||
| Initialize AuthManagerAdapter. | ||
|
|
||
| Args: | ||
| auth_manager (AuthManager): An instance of an AuthManager subclass. | ||
| """ | ||
| self.auth_manager = auth_manager | ||
|
|
||
| def __call__(self, request: PreparedRequest) -> PreparedRequest: | ||
| """ | ||
| Modify the outgoing request to include the Authorization header. | ||
|
|
||
| Args: | ||
| request (requests.PreparedRequest): The HTTP request being prepared. | ||
|
|
||
| Returns: | ||
| requests.PreparedRequest: The modified request with Authorization header. | ||
| """ | ||
| if auth_header := self.auth_manager.auth_header(): | ||
| request.headers["Authorization"] = auth_header | ||
| return request | ||
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,65 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| import base64 | ||
|
|
||
| import pytest | ||
| import requests | ||
| from requests_mock import Mocker | ||
|
|
||
| from pyiceberg.catalog.rest.auth import AuthManagerAdapter, BasicAuthManager, NoopAuthManager | ||
|
|
||
| TEST_URI = "https://iceberg-test-catalog/" | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def rest_mock(requests_mock: Mocker) -> Mocker: | ||
| requests_mock.get( | ||
| TEST_URI, | ||
| json={}, | ||
| status_code=200, | ||
| ) | ||
| return requests_mock | ||
|
|
||
|
|
||
| def test_noop_auth_header(rest_mock: Mocker) -> None: | ||
| auth_manager = NoopAuthManager() | ||
| session = requests.Session() | ||
| session.auth = AuthManagerAdapter(auth_manager) | ||
|
|
||
| session.get(TEST_URI) | ||
| history = rest_mock.request_history | ||
| assert len(history) == 1 | ||
| actual_headers = history[0].headers | ||
| assert "Authorization" not in actual_headers | ||
|
|
||
|
|
||
| def test_basic_auth_header(rest_mock: Mocker) -> None: | ||
| username = "testuser" | ||
| password = "testpassword" | ||
| expected_token = base64.b64encode(f"{username}:{password}".encode()).decode() | ||
| expected_header = f"Basic {expected_token}" | ||
|
|
||
| auth_manager = BasicAuthManager(username=username, password=password) | ||
| session = requests.Session() | ||
| session.auth = AuthManagerAdapter(auth_manager) | ||
|
|
||
| session.get(TEST_URI) | ||
| history = rest_mock.request_history | ||
| assert len(history) == 1 | ||
| actual_headers = history[0].headers | ||
| assert actual_headers["Authorization"] == expected_header |
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.
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.
The older version of the iceberg-aws-bundle looks like it was removed. Updating the pin to fix issues with the integration tests: https://repository.apache.org/content/groups/snapshots/org/apache/iceberg/iceberg-aws-bundle/1.9.0-SNAPSHOT/