Skip to content

Commit

Permalink
Add auth exception handling for expired token
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGlobus committed Oct 23, 2023
1 parent ac540fd commit 69df2db
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
19 changes: 19 additions & 0 deletions compute_endpoint/globus_compute_endpoint/exception_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import annotations

import functools
import json
import logging
import os
import sys
Expand Down Expand Up @@ -86,6 +87,24 @@ def wrapper(*args, **kwargs):
message: {e.text}
"""
)

# This specific Auth error has a common cause
if e.http_status == 400 and e.code == "Error":
try:
error_info = json.loads(e.text)
if "invalid_grant" == error_info.get("error"):
msg += (
"\nYour Globus Compute credentials might have expired."
" You can clear them with `globus-compute-endpoint logout`"
" before retrying the command.\n"
"Note that Globus Connect (Transfer) credentials are managed "
" via the `globus` command, separately from the Globus "
" Compute ones."
)
except Exception:
# Shouldn't get here unless Globus Auth changes its response
pass

log.warning(msg)
click.echo(msg)
sys.exit(os.EX_NOPERM)
Expand Down
23 changes: 18 additions & 5 deletions compute_endpoint/tests/unit/test_cli_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,11 @@ def test_get_endpoint_by_name_or_uuid_error_message(run_line, data):
@pytest.mark.parametrize(
"data",
[
("start", "start_endpoint"),
("stop", "stop_endpoint"),
("delete --yes", "delete_endpoint"),
("start", "start_endpoint", '{"error":"invalid_grant"}'),
("start", "start_endpoint", '{"error":"something else"}'),
("start", "start_endpoint", ""),
("stop", "stop_endpoint", "err_msg"),
("delete --yes", "delete_endpoint", "err_msg"),
],
)
def test_handle_globus_auth_error(
Expand All @@ -679,15 +681,20 @@ def test_handle_globus_auth_error(
ep_name,
data: tuple[str, str],
):
cmd, ep_method = data
cmd, ep_method, auth_err_msg = data
mock_ep, _ = mock_cli_state
make_endpoint_dir()

mock_log = mocker.patch("globus_compute_endpoint.exception_handling.log")
mock_resp = mock.MagicMock(
status_code=400,
reason="Bad Request",
text=auth_err_msg,
)
mocker.patch.object(
mock_ep,
ep_method,
side_effect=globus_sdk.AuthAPIError(r=mock.MagicMock(status_code=400)),
side_effect=globus_sdk.AuthAPIError(r=mock_resp),
)

res = run_line(f"{cmd} {ep_name}", assert_exit_code=os.EX_NOPERM)
Expand All @@ -699,3 +706,9 @@ def test_handle_globus_auth_error(
assert err_msg in a[0]
assert "400" in res.stdout
assert "400" in a[0]

additional_details = "You can clear them with"
if "invalid_grant" in auth_err_msg:
assert additional_details in res.stdout
else:
assert additional_details not in res.stdout

0 comments on commit 69df2db

Please sign in to comment.