Skip to content

Commit 13f12b6

Browse files
committed
fixup! Don't send bearer tokens to DRS servers outside of Terra (#7660)
1 parent 37073c6 commit 13f12b6

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/azul/http.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
from collections.abc import (
2+
Callable,
3+
)
14
import logging
25
import sys
36
import time
47
from typing import (
58
Any,
69
ClassVar,
710
Self,
11+
Sequence,
812
)
913

1014
import certifi
1115
from furl import (
1216
furl,
1317
)
18+
from google.auth.transport.urllib3 import (
19+
AuthorizedHttp,
20+
)
1421
import urllib3
1522
import urllib3.connection
1623
import urllib3.connectionpool
@@ -322,6 +329,28 @@ def urlopen(self, method, url, *args, **kwargs) -> urllib3.HTTPResponse:
322329
return response
323330

324331

332+
class ConditionalAuthHttpClient(HttpClientDecorator):
333+
"""
334+
Must directly wrap an instance of AuthorizedHttp. An HTTP client that only
335+
authenticates if a condition is met. The arguments to the predicate function
336+
are the same as to :meth:`urlopen`.
337+
"""
338+
339+
def __init__(self,
340+
inner: AuthorizedHttp,
341+
condition: Callable[[str, str, Sequence[Any], dict[str, Any]], bool],
342+
headers: dict | None = None):
343+
super().__init__(inner, headers)
344+
self.condition = condition
345+
346+
def urlopen(self, method, url, *args, **kwargs) -> urllib3.HTTPResponse:
347+
assert isinstance(self._inner, AuthorizedHttp), self._inner
348+
if self.condition(method, url, *args, **kwargs):
349+
return super().urlopen(method, url, *args, **kwargs)
350+
else:
351+
return self._inner.http.request(method, url, *args, **kwargs)
352+
353+
325354
class HasCachedHttpClient:
326355
"""
327356
A convenience mixin that provides a cached instance property referring to an

src/azul/plugins/repository/tdr.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
from azul import (
2525
cache_per_thread,
26-
config,
2726
require,
2827
)
2928
from azul.auth import (
@@ -271,12 +270,6 @@ def update(self,
271270
assert self.location is None, self
272271
assert self.retry_after is None, self
273272
else:
274-
# Compact DRS URIs cannot be parsed by furl
275-
_, _, drs_host, *_ = self.file.drs_uri.split('/')
276-
if drs_host != config.tdr_service_url.host:
277-
# External DRS URis can point to servers outside of Terra, which
278-
# don't accept our OAuth tokens.
279-
authentication = None
280273
drs_client = plugin.drs_client(authentication)
281274
access = drs_client.get_object(self.file.drs_uri,
282275
access_method=AccessMethod.gs)

src/azul/terra.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
from google.auth.transport.requests import (
3737
Request,
3838
)
39+
from google.auth.transport.urllib3 import (
40+
AuthorizedHttp,
41+
)
3942
from google.cloud import (
4043
bigquery,
4144
)
@@ -79,6 +82,7 @@
7982
DRSClient,
8083
)
8184
from azul.http import (
85+
ConditionalAuthHttpClient,
8286
LimitedRetryHttpClient,
8387
LimitedTimeoutException,
8488
Propagate429HttpClient,
@@ -289,9 +293,20 @@ class TerraClient(OAuth2Client):
289293
credentials_provider: TerraCredentialsProvider
290294

291295
def _create_http_client(self) -> urllib3.request.RequestMethods:
296+
297+
authorized_http = super()._create_http_client()
298+
assert isinstance(authorized_http, AuthorizedHttp), authorized_http
299+
300+
def should_authenticate(method: str, url: str, *args, **kwargs) -> bool:
301+
url = furl(url)
302+
return url.host in (config.tdr_service_url.host, config.sam_service_url.host)
303+
292304
return Propagate429HttpClient(
293305
LimitedRetryHttpClient(
294-
super()._create_http_client()
306+
ConditionalAuthHttpClient(
307+
authorized_http,
308+
should_authenticate,
309+
)
295310
)
296311
)
297312

0 commit comments

Comments
 (0)