Skip to content

Commit 2e6ab9d

Browse files
authored
Add SimpleAuth implementation (#106)
* Add SimpleAuth implementation * Add logic to preserve hadoop.auth * Always set auth token * Fix cookies passing ... without losing them from session * Add default user "yarn"
1 parent 26520b2 commit 2e6ab9d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

yarn_api_client/auth.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import requests
2+
3+
class SimpleAuth(requests.auth.AuthBase):
4+
def __init__(self, username="yarn"):
5+
self.username = username
6+
self.auth_token = None
7+
self.auth_done = False
8+
9+
def __call__(self, request):
10+
if not self.auth_done:
11+
_session = requests.Session()
12+
r = _session.get(request.url, params={"user.name": self.username})
13+
r.raise_for_status()
14+
self.auth_token = _session.cookies.get_dict()['hadoop.auth']
15+
self.auth_done = True
16+
17+
# Borrowed from https://github.com/psf/requests/issues/2532#issuecomment-90126896
18+
if 'Cookie' in request.headers:
19+
old_cookies = request.headers['Cookie']
20+
all_cookies = '; '.join([old_cookies, "{0}={1}".format("hadoop.auth", self.auth_token)])
21+
request.headers['Cookie'] = all_cookies
22+
else:
23+
request.prepare_cookies({"hadoop.auth": self.auth_token})
24+
return request

0 commit comments

Comments
 (0)