Skip to content

Commit b28224b

Browse files
fix: revert "feat: add api key support (#826)" (#964)
This reverts commit 3b15092b3461278400e4683060f64a96d50587c4. Co-authored-by: Anthonios Partheniou <partheniou@google.com>
1 parent 07fc160 commit b28224b

File tree

7 files changed

+2
-289
lines changed

7 files changed

+2
-289
lines changed

packages/google-auth/google/auth/_default.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -345,24 +345,6 @@ def _get_external_account_credentials(
345345
return credentials, credentials.get_project_id(request=request)
346346

347347

348-
def _get_api_key_credentials(quota_project_id=None):
349-
"""Gets API key credentials and project ID."""
350-
from google.auth import api_key
351-
352-
api_key_value = os.environ.get(environment_vars.API_KEY)
353-
if api_key_value:
354-
return api_key.Credentials(api_key_value), quota_project_id
355-
else:
356-
return None, None
357-
358-
359-
def get_api_key_credentials(api_key_value):
360-
"""Gets API key credentials using the given api key value."""
361-
from google.auth import api_key
362-
363-
return api_key.Credentials(api_key_value)
364-
365-
366348
def _get_authorized_user_credentials(filename, info, scopes=None):
367349
from google.oauth2 import credentials
368350

@@ -458,14 +440,7 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
458440
This function acquires credentials from the environment in the following
459441
order:
460442
461-
1. If both ``GOOGLE_API_KEY`` and ``GOOGLE_APPLICATION_CREDENTIALS``
462-
environment variables are set, throw an exception.
463-
464-
If ``GOOGLE_API_KEY`` is set, an `API Key`_ credentials will be returned.
465-
The project ID returned is the one defined by ``GOOGLE_CLOUD_PROJECT`` or
466-
``GCLOUD_PROJECT`` environment variables.
467-
468-
If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
443+
1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
469444
to the path of a valid service account JSON private key file, then it is
470445
loaded and returned. The project ID returned is the project ID defined
471446
in the service account file if available (some older files do not
@@ -513,7 +488,6 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
513488
.. _Metadata Service: https://cloud.google.com/compute/docs\
514489
/storing-retrieving-metadata
515490
.. _Cloud Run: https://cloud.google.com/run
516-
.. _API Key: https://cloud.google.com/docs/authentication/api-keys
517491
518492
Example::
519493
@@ -555,19 +529,11 @@ def default(scopes=None, request=None, quota_project_id=None, default_scopes=Non
555529
environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
556530
)
557531

558-
if os.environ.get(environment_vars.API_KEY) and os.environ.get(
559-
environment_vars.CREDENTIALS
560-
):
561-
raise exceptions.DefaultCredentialsError(
562-
"Environment variables GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"
563-
)
564-
565532
checkers = (
566533
# Avoid passing scopes here to prevent passing scopes to user credentials.
567534
# with_scopes_if_required() below will ensure scopes/default scopes are
568535
# safely set on the returned credentials since requires_scopes will
569536
# guard against setting scopes on user credentials.
570-
lambda: _get_api_key_credentials(quota_project_id=quota_project_id),
571537
lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
572538
lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
573539
_get_gae_credentials,

packages/google-auth/google/auth/_default_async.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,6 @@ def _get_gae_credentials():
161161
return _default._get_gae_credentials()
162162

163163

164-
def _get_api_key_credentials(quota_project_id=None):
165-
"""Gets API key credentials and project ID."""
166-
from google.auth import api_key
167-
168-
api_key_value = os.environ.get(environment_vars.API_KEY)
169-
if api_key_value:
170-
return api_key.Credentials(api_key_value), quota_project_id
171-
else:
172-
return None, None
173-
174-
175-
def get_api_key_credentials(api_key_value):
176-
"""Gets API key credentials using the given api key value."""
177-
from google.auth import api_key
178-
179-
return api_key.Credentials(api_key_value)
180-
181-
182164
def _get_gce_credentials(request=None):
183165
"""Gets credentials and project ID from the GCE Metadata Service."""
184166
# Ping requires a transport, but we want application default credentials
@@ -200,14 +182,7 @@ def default_async(scopes=None, request=None, quota_project_id=None):
200182
This function acquires credentials from the environment in the following
201183
order:
202184
203-
1. If both ``GOOGLE_API_KEY`` and ``GOOGLE_APPLICATION_CREDENTIALS``
204-
environment variables are set, throw an exception.
205-
206-
If ``GOOGLE_API_KEY`` is set, an `API Key`_ credentials will be returned.
207-
The project ID returned is the one defined by ``GOOGLE_CLOUD_PROJECT`` or
208-
``GCLOUD_PROJECT`` environment variables.
209-
210-
If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
185+
1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
211186
to the path of a valid service account JSON private key file, then it is
212187
loaded and returned. The project ID returned is the project ID defined
213188
in the service account file if available (some older files do not
@@ -246,7 +221,6 @@ def default_async(scopes=None, request=None, quota_project_id=None):
246221
.. _Metadata Service: https://cloud.google.com/compute/docs\
247222
/storing-retrieving-metadata
248223
.. _Cloud Run: https://cloud.google.com/run
249-
.. _API Key: https://cloud.google.com/docs/authentication/api-keys
250224
251225
Example::
252226
@@ -282,15 +256,7 @@ def default_async(scopes=None, request=None, quota_project_id=None):
282256
environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)
283257
)
284258

285-
if os.environ.get(environment_vars.API_KEY) and os.environ.get(
286-
environment_vars.CREDENTIALS
287-
):
288-
raise exceptions.DefaultCredentialsError(
289-
"GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"
290-
)
291-
292259
checkers = (
293-
lambda: _get_api_key_credentials(quota_project_id=quota_project_id),
294260
lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
295261
lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),
296262
_get_gae_credentials,

packages/google-auth/google/auth/api_key.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

packages/google-auth/google/auth/environment_vars.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
"""Environment variable defining the location of Google application default
3434
credentials."""
3535

36-
API_KEY = "GOOGLE_API_KEY"
37-
"""Environment variable defining the API key value."""
38-
3936
# The environment variable name which can replace ~/.config if set.
4037
CLOUD_SDK_CONFIG_DIR = "CLOUDSDK_CONFIG"
4138
"""Environment variable defines the location of Google Cloud SDK's config

packages/google-auth/tests/test__default.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import pytest # type: ignore
2020

2121
from google.auth import _default
22-
from google.auth import api_key
2322
from google.auth import app_engine
2423
from google.auth import aws
2524
from google.auth import compute_engine
@@ -1141,46 +1140,3 @@ def test_default_impersonated_service_account_set_both_scopes_and_default_scopes
11411140

11421141
credentials, _ = _default.default(scopes=scopes, default_scopes=default_scopes)
11431142
assert credentials._target_scopes == scopes
1144-
1145-
1146-
def test__get_api_key_credentials_no_env_var():
1147-
cred, project_id = _default._get_api_key_credentials(quota_project_id="project-foo")
1148-
assert cred is None
1149-
assert project_id is None
1150-
1151-
1152-
def test__get_api_key_credentials_from_env_var():
1153-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
1154-
cred, project_id = _default._get_api_key_credentials(
1155-
quota_project_id="project-foo"
1156-
)
1157-
assert isinstance(cred, api_key.Credentials)
1158-
assert cred.token == "api-key"
1159-
assert project_id == "project-foo"
1160-
1161-
1162-
def test_exception_with_api_key_and_adc_env_var():
1163-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
1164-
with mock.patch.dict(
1165-
os.environ, {environment_vars.CREDENTIALS: "/path/to/json"}
1166-
):
1167-
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
1168-
_default.default()
1169-
1170-
assert excinfo.match(
1171-
r"GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"
1172-
)
1173-
1174-
1175-
def test_default_api_key_from_env_var():
1176-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
1177-
cred, project_id = _default.default()
1178-
assert isinstance(cred, api_key.Credentials)
1179-
assert cred.token == "api-key"
1180-
assert project_id is None
1181-
1182-
1183-
def test_get_api_key_credentials():
1184-
cred = _default.get_api_key_credentials("api-key")
1185-
assert isinstance(cred, api_key.Credentials)
1186-
assert cred.token == "api-key"

packages/google-auth/tests/test_api_key.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

packages/google-auth/tests_async/test__default_async.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from google.auth import _credentials_async as credentials
2222
from google.auth import _default_async as _default
23-
from google.auth import api_key
2423
from google.auth import app_engine
2524
from google.auth import compute_engine
2625
from google.auth import environment_vars
@@ -562,46 +561,3 @@ def test_default_no_warning_with_quota_project_id_for_user_creds(get_adc_path):
562561
get_adc_path.return_value = test_default.AUTHORIZED_USER_CLOUD_SDK_FILE
563562

564563
credentials, project_id = _default.default_async(quota_project_id="project-foo")
565-
566-
567-
def test__get_api_key_credentials_no_env_var():
568-
cred, project_id = _default._get_api_key_credentials(quota_project_id="project-foo")
569-
assert cred is None
570-
assert project_id is None
571-
572-
573-
def test__get_api_key_credentials_from_env_var():
574-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
575-
cred, project_id = _default._get_api_key_credentials(
576-
quota_project_id="project-foo"
577-
)
578-
assert isinstance(cred, api_key.Credentials)
579-
assert cred.token == "api-key"
580-
assert project_id == "project-foo"
581-
582-
583-
def test_exception_with_api_key_and_adc_env_var():
584-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
585-
with mock.patch.dict(
586-
os.environ, {environment_vars.CREDENTIALS: "/path/to/json"}
587-
):
588-
with pytest.raises(exceptions.DefaultCredentialsError) as excinfo:
589-
_default.default_async()
590-
591-
assert excinfo.match(
592-
r"GOOGLE_API_KEY and GOOGLE_APPLICATION_CREDENTIALS are mutually exclusive"
593-
)
594-
595-
596-
def test_default_api_key_from_env_var():
597-
with mock.patch.dict(os.environ, {environment_vars.API_KEY: "api-key"}):
598-
cred, project_id = _default.default_async()
599-
assert isinstance(cred, api_key.Credentials)
600-
assert cred.token == "api-key"
601-
assert project_id is None
602-
603-
604-
def test_get_api_key_credentials():
605-
cred = _default.get_api_key_credentials("api-key")
606-
assert isinstance(cred, api_key.Credentials)
607-
assert cred.token == "api-key"

0 commit comments

Comments
 (0)