Skip to content

Commit 2067c4f

Browse files
Update webhdfs to support BasicAuth and apply proxy on every call (#1409)
--------- Co-authored-by: Martin Durant <[email protected]>
1 parent 879adfc commit 2067c4f

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

fsspec/implementations/webhdfs.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class WebHDFS(AbstractFileSystem):
2121
"""
2222
Interface to HDFS over HTTP using the WebHDFS API. Supports also HttpFS gateways.
2323
24-
Three auth mechanisms are supported:
24+
Four auth mechanisms are supported:
2525
2626
insecure: no auth is done, and the user is assumed to be whoever they
2727
say they are (parameter ``user``), or a predefined value such as
@@ -34,6 +34,8 @@ class WebHDFS(AbstractFileSystem):
3434
service. Indeed, this client can also generate such tokens when
3535
not insecure. Note that tokens expire, but can be renewed (by a
3636
previously specified user) and may allow for proxying.
37+
basic-auth: used when both parameter ``user`` and parameter ``password``
38+
are provided.
3739
3840
"""
3941

@@ -47,6 +49,7 @@ def __init__(
4749
kerberos=False,
4850
token=None,
4951
user=None,
52+
password=None,
5053
proxy_to=None,
5154
kerb_kwargs=None,
5255
data_proxy=None,
@@ -68,6 +71,9 @@ def __init__(
6871
given
6972
user: str or None
7073
If given, assert the user name to connect with
74+
password: str or None
75+
If given, assert the password to use for basic auth. If password
76+
is provided, user must be provided also
7177
proxy_to: str or None
7278
If given, the user has the authority to proxy, and this value is
7379
the user in who's name actions are taken
@@ -102,8 +108,19 @@ def __init__(
102108
" token"
103109
)
104110
self.pars["delegation"] = token
105-
if user is not None:
106-
self.pars["user.name"] = user
111+
self.user = user
112+
self.password = password
113+
114+
if password is not None:
115+
if user is None:
116+
raise ValueError(
117+
"If passing a password, the user must also be"
118+
"set in order to set up the basic-auth"
119+
)
120+
else:
121+
if user is not None:
122+
self.pars["user.name"] = user
123+
107124
if proxy_to is not None:
108125
self.pars["doas"] = proxy_to
109126
if kerberos and user is not None:
@@ -126,8 +143,13 @@ def _connect(self):
126143

127144
self.session.auth = HTTPKerberosAuth(**self.kerb_kwargs)
128145

146+
if self.user is not None and self.password is not None:
147+
from requests.auth import HTTPBasicAuth
148+
149+
self.session.auth = HTTPBasicAuth(self.user, self.password)
150+
129151
def _call(self, op, method="get", path=None, data=None, redirect=True, **kwargs):
130-
url = self.url + quote(path or "")
152+
url = self._apply_proxy(self.url + quote(path or ""))
131153
args = kwargs.copy()
132154
args.update(self.pars)
133155
args["op"] = op.upper()

0 commit comments

Comments
 (0)