Skip to content
Draft
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
43 changes: 39 additions & 4 deletions packages/pandas-gbq/docs/howto/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ credentials are not found.
credentials = pydata_google_auth.get_user_credentials(
SCOPES,
# Note, this doesn't work if you're running from a notebook on a
# remote sever, such as over SSH or with Google Colab. In those cases,
# remote server, such as over SSH or with Google Colab. In those cases,
# install the gcloud command line interface and authenticate with the
# `gcloud auth application-default login` command and the `--no-browser`
# option.
auth_local_webserver=True,
# `gcloud auth application-default login --no-browser` command.
use_local_webserver=True,
)

df = pandas_gbq.read_gbq(
Expand All @@ -212,15 +211,51 @@ credentials are not found.
SCOPES,
# Use the NOOP cache to avoid writing credentials to disk.
credentials_cache=pydata_google_auth.cache.NOOP,
use_local_webserver=True,
)

Additional information on the user credentials authentication mechanism
can be found in the `Google Cloud authentication guide
<https://cloud.google.com/docs/authentication/end-user>`__.

Authenticating in Headless or Remote Environments
-------------------------------------------------

When running on a remote server, SSH session, or container where a local web
browser cannot reach ``localhost``, use one of the following recommended
authentication methods instead of ``auth_local_webserver=False`` (which is
deprecated and will be removed in a future release):

1. **Application Default Credentials with the gcloud CLI (recommended):**
Run the following command on the remote machine and follow the remote
bootstrap prompts on a machine with a browser:

.. code-block:: shell

$ gcloud auth application-default login --no-browser

``pandas-gbq`` automatically discovers Application Default Credentials
before falling back to interactive user OAuth.

2. **Service accounts or Workload Identity Federation:**
For non-interactive servers, CI/CD, and containers, attach a service
account, configure Workload Identity Federation, set
``GOOGLE_APPLICATION_CREDENTIALS``, or pass explicit ``credentials``.

3. **Google Colab:**
``pandas-gbq`` automatically uses native Colab authentication
(``google.colab.auth.authenticate_user()``) when running in Colab.

Authenticating from Highly Constrained Development Environments
---------------------------------------------------------------

.. warning::

The legacy ``auth_local_webserver=False`` and ``auth_redirect_uri``
workflows described in the constrained-environment guide are deprecated and
will be removed in a future release due to OAuth security risks. Prefer the
methods in `Authenticating in Headless or Remote Environments`_ above.

The instructions above may not be adequate for users who are working in
a *highly constrained development environment*:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ the ``credentials`` argument to explicitly pass in Google

.. _authentication_hce:

.. warning::

The ``auth_local_webserver=False`` and ``auth_redirect_uri`` workflows on
this page are **deprecated** and will be removed in a future release due to
OAuth security risks in the console/webapp redirect flow (including
unvalidated OAuth ``state`` and static redirect page code exposure).

Instead, use one of these recommended alternatives for headless or remote
environments:

* **Application Default Credentials via gcloud CLI:** run
``gcloud auth application-default login --no-browser`` on the remote
machine and complete the remote-bootstrap prompt on a browser-equipped
machine.
* **Service accounts / Workload Identity Federation:** configure ADC or pass
an explicit ``credentials`` object.
* **Google Colab:** use native Colab authentication (automatically detected
by ``pandas-gbq``).

See the `Authentication guide <authentication.html>`_ for details.


Authenticating from Highly Constrained Development Environments
---------------------------------------------------------------
Expand Down Expand Up @@ -47,8 +68,8 @@ page.
you/your institution can create and host an institution-specific OAuth
authentication page and associate a redirect URI to that Cloud Project.

OPTION 1 - Testing Mode
^^^^^^^^^^^^^^^^^^^^^^^
OPTION 1 - Testing Mode (Deprecated)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This approach is for limited use, such as when testing your product. It is not
intended for production use. If you have fewer than 100 users, it is possible to
Expand Down Expand Up @@ -145,8 +166,8 @@ To run this code sample, you will need to have ``python-bigquery-pandas`` instal

print(df)

OPTION 2 - Institution-specific authentication page
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OPTION 2 - Institution-specific authentication page (Deprecated)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To access Bigquery programmatically, you will need your Client ID and your Client Secret, an OAuth authorization page, and an assigned redirect URI.

Expand Down
8 changes: 8 additions & 0 deletions packages/pandas-gbq/docs/oauth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
Sign in to BigQuery
===================

.. warning::

This copy-paste OAuth redirect flow (``auth_local_webserver=False`` /
``auth_redirect_uri``) is deprecated and will be removed in a future release
of ``pandas-gbq``. Use ``auth_local_webserver=True`` (the default) locally,
or authenticate headless environments with
``gcloud auth application-default login --no-browser``.

You are seeing this page because you are attempting to access BigQuery via one
of several possible methods, including:

Expand Down
30 changes: 29 additions & 1 deletion packages/pandas-gbq/pandas_gbq/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""Private module for fetching Google BigQuery credentials."""

import logging
import warnings

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,12 +38,39 @@ def get_credentials(
method from the google-auth package."""
)

# TODO(security): Raise NotImplementedError when
# auth_local_webserver=False or auth_redirect_uri is provided, and force
# use_local_webserver=True to avoid pydata-google-auth's _run_webapp() flow.
if not auth_local_webserver:
warnings.warn(
"auth_local_webserver=False is deprecated and will be removed in "
"a future version due to security risks in the console/webapp "
"OAuth redirect flow. Use auth_local_webserver=True (the default) "
"for local development, or authenticate headless environments "
"using Application Default Credentials "
"(`gcloud auth application-default login --no-browser`) or "
"explicit `credentials`.",
FutureWarning,
stacklevel=2,
)

if auth_redirect_uri is not None:
warnings.warn(
"auth_redirect_uri is deprecated and will be removed in a future "
"version along with auth_local_webserver=False. Use Application "
"Default Credentials "
"(`gcloud auth application-default login --no-browser`) or pass "
"explicit `credentials` instead.",
FutureWarning,
stacklevel=2,
)

credentials, default_project_id = pydata_google_auth.default(
SCOPES,
client_id=client_id,
client_secret=client_secret,
credentials_cache=get_credentials_cache(reauth),
auth_local_webserver=auth_local_webserver,
use_local_webserver=auth_local_webserver,
redirect_uri=auth_redirect_uri,
)

Expand Down
34 changes: 24 additions & 10 deletions packages/pandas-gbq/pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ def read_gbq(
auth_local_webserver : bool, default True
Use the `local webserver flow
<https://googleapis.dev/python/google-auth-oauthlib/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_local_server>`_
instead of the `console flow
<https://googleapis.dev/python/google-auth-oauthlib/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_console>`_
when getting user credentials. Your code must run on the same machine
as your web browser and your web browser can access your application
via ``localhost:808X``.

Setting ``auth_local_webserver=False`` is deprecated and will be
removed in a future version due to OAuth security risks in the
console/webapp redirect flow. For headless or remote environments,
authenticate with Application Default Credentials
(``gcloud auth application-default login --no-browser``) or pass
explicit ``credentials``.

.. versionadded:: 0.2.0
dialect : str, default 'standard'
Note: The default value changed to 'standard' in version 0.10.0.
Expand Down Expand Up @@ -269,9 +274,11 @@ def read_gbq(
or
:func:`google.oauth2.service_account.Credentials.from_service_account_file`
instead.
auth_redirect_uri : str
Path to the authentication page for organization-specific authentication
workflows. Used when ``auth_local_webserver=False``.
auth_redirect_uri : str, deprecated
Deprecated. Will be removed in a future version along with
``auth_local_webserver=False``. Path to the authentication page for
organization-specific authentication workflows. Used when
``auth_local_webserver=False``.
client_id : str
The Client ID for the Google Cloud Project the user is attempting to
connect to.
Expand Down Expand Up @@ -470,12 +477,17 @@ def to_gbq(
auth_local_webserver : bool, default True
Use the `local webserver flow
<https://googleapis.dev/python/google-auth-oauthlib/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_local_server>`_
instead of the `console flow
<https://googleapis.dev/python/google-auth-oauthlib/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_console>`_
when getting user credentials. Your code must run on the same machine
as your web browser and your web browser can access your application
via ``localhost:808X``.

Setting ``auth_local_webserver=False`` is deprecated and will be
removed in a future version due to OAuth security risks in the
console/webapp redirect flow. For headless or remote environments,
authenticate with Application Default Credentials
(``gcloud auth application-default login --no-browser``) or pass
explicit ``credentials``.

.. versionadded:: 0.2.0
table_schema : list of dicts, optional
List of BigQuery table fields to which according DataFrame
Expand Down Expand Up @@ -533,9 +545,11 @@ def to_gbq(
or
:func:`google.oauth2.service_account.Credentials.from_service_account_file`
instead.
auth_redirect_uri : str
Path to the authentication page for organization-specific authentication
workflows. Used when ``auth_local_webserver=False``.
auth_redirect_uri : str, deprecated
Deprecated. Will be removed in a future version along with
``auth_local_webserver=False``. Path to the authentication page for
organization-specific authentication workflows. Used when
``auth_local_webserver=False``.
client_id : str
The Client ID for the Google Cloud Project the user is attempting to
connect to.
Expand Down
4 changes: 2 additions & 2 deletions packages/pandas-gbq/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"pyarrow >= 12.0.0",
# See https://arrow.apache.org/release/22.0.0.html
"pyarrow >= 22.0.0; python_version >= '3.14'",
"pydata-google-auth >=1.5.0",
"pydata-google-auth >=1.9.1",
"psutil >=5.9.8",
# Note: google-api-core and google-auth are also included via transitive
# dependency on google-cloud-bigquery, but this library also uses them
# directly.
"google-api-core >= 2.34.0, <3.0.0",
"google-auth >=2.62.0",
"google-auth-oauthlib >=0.7.0",
"google-auth-oauthlib >=1.5.0",
# Please also update the minimum version in pandas_gbq/features.py to
# allow pandas-gbq to detect invalid package versions at runtime.
"google-cloud-bigquery >=3.43.0,<4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/pandas-gbq/testing/constraints-3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ db-dtypes==1.1.1
numpy==1.26.4
pandas==1.5.3
pyarrow==12.0.0
pydata-google-auth==1.5.0
pydata-google-auth==1.9.1
psutil==5.9.8
google-api-core==2.34.0
google-auth==2.62.0
google-auth-oauthlib==0.7.0
google-auth-oauthlib==1.5.0
google-cloud-bigquery==3.43.0
packaging==24.2.0
# Extras
Expand Down
69 changes: 69 additions & 0 deletions packages/pandas-gbq/tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# -*- coding: utf-8 -*-

import json
import warnings
from unittest import mock

import pytest
Expand Down Expand Up @@ -65,3 +66,71 @@ def test_get_credentials_cache_w_reauth():

cache = auth.get_credentials_cache(True)
assert isinstance(cache, pydata_google_auth.cache.WriteOnlyCredentialsCache)


def test_get_credentials_uses_local_webserver_kwarg(monkeypatch):
import google.auth.credentials
import pydata_google_auth

mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials)
captured_kwargs = {}

def mock_default_credentials(scopes, **kwargs):
captured_kwargs.update(kwargs)
return (mock_user_credentials, "test-project")

monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials)

with warnings.catch_warnings(record=True) as recorded:
warnings.simplefilter("always", FutureWarning)
credentials, project = auth.get_credentials()

assert credentials is mock_user_credentials
assert project == "test-project"
assert captured_kwargs["use_local_webserver"] is True
assert "auth_local_webserver" not in captured_kwargs
assert captured_kwargs["redirect_uri"] is None
assert recorded == []


def test_get_credentials_auth_local_webserver_false_warns_deprecated(monkeypatch):
import google.auth.credentials
import pydata_google_auth

mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials)
captured_kwargs = {}

def mock_default_credentials(scopes, **kwargs):
captured_kwargs.update(kwargs)
return (mock_user_credentials, "test-project")

monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials)

with pytest.warns(FutureWarning, match="auth_local_webserver=False is deprecated"):
credentials, project = auth.get_credentials(auth_local_webserver=False)

assert credentials is mock_user_credentials
assert project == "test-project"
assert captured_kwargs["use_local_webserver"] is False


def test_get_credentials_auth_redirect_uri_warns_deprecated(monkeypatch):
import google.auth.credentials
import pydata_google_auth

mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials)
captured_kwargs = {}

def mock_default_credentials(scopes, **kwargs):
captured_kwargs.update(kwargs)
return (mock_user_credentials, "test-project")

monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials)

redirect_uri = "https://example.com/oauth.html"
with pytest.warns(FutureWarning, match="auth_redirect_uri is deprecated"):
credentials, project = auth.get_credentials(auth_redirect_uri=redirect_uri)

assert credentials is mock_user_credentials
assert project == "test-project"
assert captured_kwargs["redirect_uri"] == redirect_uri
Loading
Loading