Skip to content

Adding support for Google AuthManager #2072

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion pyiceberg/catalog/rest/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import base64
import importlib
import logging
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, Type
from typing import Any, Dict, List, Optional, Type

from requests import HTTPError, PreparedRequest, Session
from requests.auth import AuthBase
Expand All @@ -27,6 +28,7 @@
from pyiceberg.exceptions import OAuthError

COLON = ":"
logger = logging.getLogger(__name__)


class AuthManager(ABC):
Expand Down Expand Up @@ -119,6 +121,35 @@ def auth_header(self) -> str:
return f"Bearer {self._token}"


class GoogleAuthManager(AuthManager):
"""An auth manager that is responsible for handling Google credentials."""

def __init__(self, credentials_path: Optional[str] = None, scopes: Optional[List[str]] = None):
"""
Initialize GoogleAuthManager.

Args:
credentials_path: Optional path to Google credentials JSON file.
scopes: Optional list of OAuth2 scopes.
"""
try:
import google.auth
import google.auth.transport.requests
except ImportError as e:
raise ImportError("Google Auth libraries not found. Please install 'google-auth'.") from e

if credentials_path:
self.credentials, _ = google.auth.load_credentials_from_file(credentials_path, scopes=scopes)
else:
logger.info("Using Google Default Application Credentials")
self.credentials, _ = google.auth.default(scopes=scopes)
self._auth_request = google.auth.transport.requests.Request()

def auth_header(self) -> str:
self.credentials.refresh(self._auth_request)
return f"Bearer {self.credentials.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.

Expand Down Expand Up @@ -197,3 +228,4 @@ def create(cls, class_or_name: str, config: Dict[str, Any]) -> AuthManager:
AuthManagerFactory.register("noop", NoopAuthManager)
AuthManagerFactory.register("basic", BasicAuthManager)
AuthManagerFactory.register("legacyoauth2", LegacyOAuth2AuthManager)
AuthManagerFactory.register("google", GoogleAuthManager)
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ zstandard = ">=0.13.0,<1.0.0"
tenacity = ">=8.2.3,<10.0.0"
pyroaring = ">=1.0.0,<2.0.0"
pyarrow = { version = ">=17.0.0,<21.0.0", optional = true }
google-auth = { version = ">=2.4.0", optional = true }
pandas = { version = ">=1.0.0,<3.0.0", optional = true }
duckdb = { version = ">=0.5.0,<2.0.0", optional = true }
ray = [
Expand Down Expand Up @@ -127,6 +128,10 @@ ignore_missing_imports = true
module = "pyarrow.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "google.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "pandas.*"
ignore_missing_imports = true
Expand Down Expand Up @@ -317,6 +322,7 @@ rest-sigv4 = ["boto3"]
hf = ["huggingface-hub"]
pyiceberg-core = ["pyiceberg-core"]
datafusion = ["datafusion"]
gcp-auth = ["google-auth"]

[tool.pytest.ini_options]
testpaths = ["tests"]
Expand Down
Loading