Skip to content

Commit b13aec0

Browse files
simonlskefioppre-commit-ci[bot]
authored
[6478] Fix 401 unauthorized for directories in basic auth protected http remote (#6479)
* added auth to iter_url and created custom open for http fs * added default value to auth * Update dvc/utils/http.py Co-authored-by: Ruslan Kuprieiev <[email protected]> * Update dvc/fs/http.py Co-authored-by: Ruslan Kuprieiev <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * pr fixes Co-authored-by: Ruslan Kuprieiev <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 13d1276 commit b13aec0

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

dvc/fs/base.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from dvc.progress import Tqdm
1010
from dvc.utils import tmp_fname
1111
from dvc.utils.fs import makedirs, move
12-
from dvc.utils.http import open_url
1312

1413
logger = logging.getLogger(__name__)
1514

@@ -126,12 +125,6 @@ def _check_requires(self, **kwargs):
126125
)
127126

128127
def open(self, path_info, mode: str = "r", encoding: str = None, **kwargs):
129-
if hasattr(self, "_generate_download_url"):
130-
# pylint:disable=no-member
131-
func = self._generate_download_url # type: ignore[attr-defined]
132-
get_url = partial(func, path_info)
133-
return open_url(get_url, mode=mode, encoding=encoding, **kwargs)
134-
135128
raise RemoteActionNotImplemented("open", self.scheme)
136129

137130
def exists(self, path_info) -> bool:

dvc/fs/http.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ def _auth_method(self, url):
6565
self.headers.update({self.custom_auth_header: self.password})
6666
return None
6767

68-
def _generate_download_url(self, path_info):
69-
return path_info.url
70-
7168
@wrap_prop(threading.Lock())
7269
@cached_property
7370
def _session(self):
@@ -190,6 +187,17 @@ def _upload(
190187
desc=name or to_info.url,
191188
)
192189

190+
def open(self, path_info, mode: str = "r", encoding: str = None, **kwargs):
191+
from dvc.utils.http import open_url
192+
193+
return open_url(
194+
path_info.url,
195+
mode=mode,
196+
encoding=encoding,
197+
auth=self._auth_method(path_info),
198+
**kwargs,
199+
)
200+
193201
@staticmethod
194202
def _content_length(response) -> Optional[int]:
195203
res = response.headers.get("Content-Length")

dvc/utils/http.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ def open_url(url, mode="r", encoding=None, **iter_opts):
2424

2525

2626
@contextmanager
27-
def iter_url(url, chunk_size=io.DEFAULT_BUFFER_SIZE):
27+
def iter_url(url, chunk_size=io.DEFAULT_BUFFER_SIZE, auth=None):
2828
"""Iterate over chunks requested from url."""
2929
import requests
3030

3131
def request(headers=None):
3232
the_url = url() if callable(url) else url
33-
response = requests.get(the_url, stream=True, headers=headers)
33+
response = requests.get(
34+
the_url, stream=True, headers=headers, auth=auth
35+
)
3436
if response.status_code == 404:
3537
raise FileNotFoundError(f"Can't open {the_url}")
3638
response.raise_for_status()

0 commit comments

Comments
 (0)