Skip to content

Commit

Permalink
Fix st2 cli client auth in st2auth proxy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
floatingstatic committed Oct 25, 2023
1 parent 8f6cb46 commit 1b059e9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions st2auth/st2auth/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ def handle_auth(
remote_addr = headers.get("x-forwarded-for", remote_addr)
extra = {"remote_addr": remote_addr}

# Needed to support st2client which does not connect via st2web
if authorization and not remote_user:
try:
auth_value = base64.b64decode(authorization[1])
except Exception:
LOG.audit("Invalid authorization header", extra=extra)
abort_request()
return

split = auth_value.split(b":", 1)
if len(split) != 2:
LOG.audit("Invalid authorization header", extra=extra)
abort_request()
return

remote_user = split[0]
if six.PY3 and isinstance(remote_user, six.binary_type):
remote_user = remote_user.decode("utf-8")

if remote_user:
ttl = getattr(request, "ttl", None)
username = self._get_username_for_request(remote_user, request)
Expand Down
25 changes: 25 additions & 0 deletions st2auth/tests/unit/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ def test_proxy_handler(self):
)
self.assertEqual(token.user, "test_proxy_handler")

def test_proxy_handler_no_remote_user(self):
h = handlers.ProxyAuthHandler()
request = {}
token = h.handle_auth(
request,
headers={},
remote_addr=None,
remote_user=None,
authorization=("basic", DUMMY_CREDS),
)
self.assertEqual(token.user, "auser")

def test_proxy_handler_bad_auth(self):
h = handlers.ProxyAuthHandler()
request = {}

with self.assertRaises(exc.HTTPUnauthorized):
h.handle_auth(
request,
headers={},
remote_addr=None,
remote_user=None,
authorization=None,
)

def test_standalone_bad_auth_type(self):
h = handlers.StandaloneAuthHandler()
request = {}
Expand Down

0 comments on commit 1b059e9

Please sign in to comment.