Skip to content

Commit 1b059e9

Browse files
Fix st2 cli client auth in st2auth proxy mode
1 parent 8f6cb46 commit 1b059e9

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

st2auth/st2auth/handlers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ def handle_auth(
130130
remote_addr = headers.get("x-forwarded-for", remote_addr)
131131
extra = {"remote_addr": remote_addr}
132132

133+
# Needed to support st2client which does not connect via st2web
134+
if authorization and not remote_user:
135+
try:
136+
auth_value = base64.b64decode(authorization[1])
137+
except Exception:
138+
LOG.audit("Invalid authorization header", extra=extra)
139+
abort_request()
140+
return
141+
142+
split = auth_value.split(b":", 1)
143+
if len(split) != 2:
144+
LOG.audit("Invalid authorization header", extra=extra)
145+
abort_request()
146+
return
147+
148+
remote_user = split[0]
149+
if six.PY3 and isinstance(remote_user, six.binary_type):
150+
remote_user = remote_user.decode("utf-8")
151+
133152
if remote_user:
134153
ttl = getattr(request, "ttl", None)
135154
username = self._get_username_for_request(remote_user, request)

st2auth/tests/unit/test_handlers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,31 @@ def test_proxy_handler(self):
4848
)
4949
self.assertEqual(token.user, "test_proxy_handler")
5050

51+
def test_proxy_handler_no_remote_user(self):
52+
h = handlers.ProxyAuthHandler()
53+
request = {}
54+
token = h.handle_auth(
55+
request,
56+
headers={},
57+
remote_addr=None,
58+
remote_user=None,
59+
authorization=("basic", DUMMY_CREDS),
60+
)
61+
self.assertEqual(token.user, "auser")
62+
63+
def test_proxy_handler_bad_auth(self):
64+
h = handlers.ProxyAuthHandler()
65+
request = {}
66+
67+
with self.assertRaises(exc.HTTPUnauthorized):
68+
h.handle_auth(
69+
request,
70+
headers={},
71+
remote_addr=None,
72+
remote_user=None,
73+
authorization=None,
74+
)
75+
5176
def test_standalone_bad_auth_type(self):
5277
h = handlers.StandaloneAuthHandler()
5378
request = {}

0 commit comments

Comments
 (0)