Skip to content

Commit 1ca0233

Browse files
authored
Revert "feat: add support for use_auth_w_custom_endpoint (#901)" (#940)
This reverts commit 4862a9c.
1 parent 4862a9c commit 1ca0233

File tree

3 files changed

+41
-77
lines changed

3 files changed

+41
-77
lines changed

google/cloud/storage/_helpers.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@
3333
STORAGE_EMULATOR_ENV_VAR = "STORAGE_EMULATOR_HOST"
3434
"""Environment variable defining host for Storage emulator."""
3535

36-
_BASE_STORAGE_URI = "https://storage.googleapis.com"
37-
"""Base request endpoint URI for JSON API."""
38-
39-
_DEFAULT_STORAGE_HOST = os.getenv("API_ENDPOINT_OVERRIDE", _BASE_STORAGE_URI)
36+
_DEFAULT_STORAGE_HOST = os.getenv(
37+
"API_ENDPOINT_OVERRIDE", "https://storage.googleapis.com"
38+
)
4039
"""Default storage host for JSON API."""
4140

4241
_API_VERSION = os.getenv("API_VERSION_OVERRIDE", "v1")
4342
"""API version of the default storage host"""
4443

44+
_BASE_STORAGE_URI = "storage.googleapis.com"
45+
"""Base request endpoint URI for JSON API."""
46+
4547
# etag match parameters in snake case and equivalent header
4648
_ETAG_MATCH_PARAMETERS = (
4749
("if_etag_match", "If-Match"),

google/cloud/storage/client.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ class Client(ClientWithProject):
9696
:type client_options: :class:`~google.api_core.client_options.ClientOptions` or :class:`dict`
9797
:param client_options: (Optional) Client options used to set user options on the client.
9898
API Endpoint should be set through client_options.
99-
100-
:type use_auth_w_custom_endpoint: bool
101-
:param use_auth_w_custom_endpoint:
102-
(Optional) Whether authentication is required under custom endpoints.
103-
If false, uses AnonymousCredentials and bypasses authentication.
104-
Defaults to True. Note this is only used when a custom endpoint is set in conjunction.
10599
"""
106100

107101
SCOPE = (
@@ -118,7 +112,6 @@ def __init__(
118112
_http=None,
119113
client_info=None,
120114
client_options=None,
121-
use_auth_w_custom_endpoint=True,
122115
):
123116
self._base_connection = None
124117

@@ -139,7 +132,7 @@ def __init__(
139132
# then mTLS logic will be applied to decide which endpoint will be used.
140133
storage_host = _get_storage_host()
141134
kw_args["api_endpoint"] = (
142-
storage_host if storage_host != _BASE_STORAGE_URI else None
135+
storage_host if storage_host != _DEFAULT_STORAGE_HOST else None
143136
)
144137

145138
if client_options:
@@ -151,23 +144,19 @@ def __init__(
151144
api_endpoint = client_options.api_endpoint
152145
kw_args["api_endpoint"] = api_endpoint
153146

154-
# If a custom endpoint is set, the client checks for credentials
155-
# or finds the default credentials based on the current environment.
156-
# Authentication may be bypassed under certain conditions:
157-
# (1) STORAGE_EMULATOR_HOST is set (for backwards compatibility), OR
158-
# (2) use_auth_w_custom_endpoint is set to False.
159-
if kw_args["api_endpoint"] is not None:
160-
if (
161-
kw_args["api_endpoint"] == storage_host
162-
or not use_auth_w_custom_endpoint
163-
):
164-
if credentials is None:
165-
credentials = AnonymousCredentials()
166-
if project is None:
167-
project = _get_environ_project()
168-
if project is None:
169-
no_project = True
170-
project = "<none>"
147+
# Use anonymous credentials and no project when
148+
# STORAGE_EMULATOR_HOST or a non-default api_endpoint is set.
149+
if (
150+
kw_args["api_endpoint"] is not None
151+
and _BASE_STORAGE_URI not in kw_args["api_endpoint"]
152+
):
153+
if credentials is None:
154+
credentials = AnonymousCredentials()
155+
if project is None:
156+
project = _get_environ_project()
157+
if project is None:
158+
no_project = True
159+
project = "<none>"
171160

172161
super(Client, self).__init__(
173162
project=project,
@@ -908,7 +897,7 @@ def create_bucket(
908897
project = self.project
909898

910899
# Use no project if STORAGE_EMULATOR_HOST is set
911-
if _get_storage_host() != _DEFAULT_STORAGE_HOST:
900+
if _BASE_STORAGE_URI not in _get_storage_host():
912901
if project is None:
913902
project = _get_environ_project()
914903
if project is None:
@@ -1338,7 +1327,7 @@ def list_buckets(
13381327
project = self.project
13391328

13401329
# Use no project if STORAGE_EMULATOR_HOST is set
1341-
if _get_storage_host() != _DEFAULT_STORAGE_HOST:
1330+
if _BASE_STORAGE_URI not in _get_storage_host():
13421331
if project is None:
13431332
project = _get_environ_project()
13441333
if project is None:

tests/unit/test_client.py

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
from google.auth.credentials import AnonymousCredentials
2929
from google.oauth2.service_account import Credentials
3030

31-
from google.cloud.storage import _helpers
3231
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR
3332
from google.cloud.storage._helpers import _get_default_headers
34-
from google.cloud.storage._http import Connection
33+
from google.cloud.storage import _helpers
3534
from google.cloud.storage.retry import DEFAULT_RETRY
3635
from google.cloud.storage.retry import DEFAULT_RETRY_IF_GENERATION_SPECIFIED
3736
from tests.unit.test__helpers import GCCL_INVOCATION_TEST_CONST
@@ -120,6 +119,7 @@ def _make_one(self, *args, **kw):
120119

121120
def test_ctor_connection_type(self):
122121
from google.cloud._http import ClientInfo
122+
from google.cloud.storage._http import Connection
123123

124124
PROJECT = "PROJECT"
125125
credentials = _make_credentials()
@@ -179,6 +179,8 @@ def test_ctor_w_client_options_object(self):
179179
)
180180

181181
def test_ctor_wo_project(self):
182+
from google.cloud.storage._http import Connection
183+
182184
PROJECT = "PROJECT"
183185
credentials = _make_credentials(project=PROJECT)
184186

@@ -191,6 +193,8 @@ def test_ctor_wo_project(self):
191193
self.assertEqual(list(client._batch_stack), [])
192194

193195
def test_ctor_w_project_explicit_none(self):
196+
from google.cloud.storage._http import Connection
197+
194198
credentials = _make_credentials()
195199

196200
client = self._make_one(project=None, credentials=credentials)
@@ -203,6 +207,7 @@ def test_ctor_w_project_explicit_none(self):
203207

204208
def test_ctor_w_client_info(self):
205209
from google.cloud._http import ClientInfo
210+
from google.cloud.storage._http import Connection
206211

207212
credentials = _make_credentials()
208213
client_info = ClientInfo()
@@ -234,40 +239,8 @@ def test_ctor_mtls(self):
234239
self.assertEqual(client._connection.ALLOW_AUTO_SWITCH_TO_MTLS_URL, False)
235240
self.assertEqual(client._connection.API_BASE_URL, "http://foo")
236241

237-
def test_ctor_w_custom_endpoint_use_auth(self):
238-
custom_endpoint = "storage-example.p.googleapis.com"
239-
client = self._make_one(client_options={"api_endpoint": custom_endpoint})
240-
self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
241-
self.assertIsNotNone(client.project)
242-
self.assertIsInstance(client._connection, Connection)
243-
self.assertIsNotNone(client._connection.credentials)
244-
self.assertNotIsInstance(client._connection.credentials, AnonymousCredentials)
245-
246-
def test_ctor_w_custom_endpoint_bypass_auth(self):
247-
custom_endpoint = "storage-example.p.googleapis.com"
248-
client = self._make_one(
249-
client_options={"api_endpoint": custom_endpoint},
250-
use_auth_w_custom_endpoint=False,
251-
)
252-
self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
253-
self.assertEqual(client.project, None)
254-
self.assertIsInstance(client._connection, Connection)
255-
self.assertIsInstance(client._connection.credentials, AnonymousCredentials)
256-
257-
def test_ctor_w_custom_endpoint_w_credentials(self):
258-
PROJECT = "PROJECT"
259-
custom_endpoint = "storage-example.p.googleapis.com"
260-
credentials = _make_credentials(project=PROJECT)
261-
client = self._make_one(
262-
credentials=credentials, client_options={"api_endpoint": custom_endpoint}
263-
)
264-
self.assertEqual(client._connection.API_BASE_URL, custom_endpoint)
265-
self.assertEqual(client.project, PROJECT)
266-
self.assertIsInstance(client._connection, Connection)
267-
self.assertIs(client._connection.credentials, credentials)
268-
269242
def test_ctor_w_emulator_wo_project(self):
270-
# bypasses authentication if STORAGE_EMULATOR_ENV_VAR is set
243+
# avoids authentication if STORAGE_EMULATOR_ENV_VAR is set
271244
host = "http://localhost:8080"
272245
environ = {STORAGE_EMULATOR_ENV_VAR: host}
273246
with mock.patch("os.environ", environ):
@@ -277,8 +250,16 @@ def test_ctor_w_emulator_wo_project(self):
277250
self.assertEqual(client._connection.API_BASE_URL, host)
278251
self.assertIsInstance(client._connection.credentials, AnonymousCredentials)
279252

253+
# avoids authentication if storage emulator is set through api_endpoint
254+
client = self._make_one(
255+
client_options={"api_endpoint": "http://localhost:8080"}
256+
)
257+
self.assertIsNone(client.project)
258+
self.assertEqual(client._connection.API_BASE_URL, host)
259+
self.assertIsInstance(client._connection.credentials, AnonymousCredentials)
260+
280261
def test_ctor_w_emulator_w_environ_project(self):
281-
# bypasses authentication and infers the project from the environment
262+
# avoids authentication and infers the project from the environment
282263
host = "http://localhost:8080"
283264
environ_project = "environ-project"
284265
environ = {
@@ -308,17 +289,9 @@ def test_ctor_w_emulator_w_project_arg(self):
308289
self.assertEqual(client._connection.API_BASE_URL, host)
309290
self.assertIsInstance(client._connection.credentials, AnonymousCredentials)
310291

311-
def test_ctor_w_emulator_w_credentials(self):
312-
host = "http://localhost:8080"
313-
environ = {STORAGE_EMULATOR_ENV_VAR: host}
314-
credentials = _make_credentials()
315-
with mock.patch("os.environ", environ):
316-
client = self._make_one(credentials=credentials)
317-
318-
self.assertEqual(client._connection.API_BASE_URL, host)
319-
self.assertIs(client._connection.credentials, credentials)
320-
321292
def test_create_anonymous_client(self):
293+
from google.cloud.storage._http import Connection
294+
322295
klass = self._get_target_class()
323296
client = klass.create_anonymous_client()
324297

0 commit comments

Comments
 (0)