auth: Hoist exception handling in GerritAuthenticator

This moves the handling to the same level as
`raise GitLoginRequiredError()` so we can check for other exit codes
and raise other errors.

Bug: 442666611
Change-Id: Idbb34d6549b47b715bf59d6720362293d5c28039
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6912689
Commit-Queue: Allen Li <ayatane@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
This commit is contained in:
Allen Li
2025-09-03 15:31:20 -07:00
committed by LUCI CQ
parent 05b5e754e4
commit 42cfc1ef22

45
auth.py
View File

@@ -229,7 +229,13 @@ class GerritAuthenticator(object):
Raises: Raises:
GitLoginRequiredError if user interaction is required. GitLoginRequiredError if user interaction is required.
""" """
access_token = self._get_luci_auth_token() try:
access_token = self._get_luci_auth_token()
except subprocess2.CalledProcessError as e:
# subprocess2.CalledProcessError.__str__ nicely formats
# stdout/stderr.
logging.error('git-credential-luci failed: %s', e)
access_token = None
if access_token: if access_token:
return access_token return access_token
logging.debug('Failed to create access token') logging.debug('Failed to create access token')
@@ -237,25 +243,18 @@ class GerritAuthenticator(object):
def _get_luci_auth_token(self, use_id_token=False) -> Optional[str]: def _get_luci_auth_token(self, use_id_token=False) -> Optional[str]:
logging.debug('Running git-credential-luci') logging.debug('Running git-credential-luci')
try: # TODO(crbug.com/442666611): depot_tools doesn't support
# TODO(crbug.com/442666611): depot_tools doesn't support # ReAuth creds from the helper yet.
# ReAuth creds from the helper yet. env = os.environ.copy()
env = os.environ.copy() env['LUCI_ENABLE_REAUTH'] = '0'
env['LUCI_ENABLE_REAUTH'] = '0' out, err = subprocess2.check_call_out(['git-credential-luci', 'get'],
out, err = subprocess2.check_call_out( stdin=subprocess2.DEVNULL,
['git-credential-luci', 'get'], stdout=subprocess2.PIPE,
stdin=subprocess2.DEVNULL, stderr=subprocess2.PIPE,
stdout=subprocess2.PIPE, env=env)
stderr=subprocess2.PIPE, logging.debug('git-credential-luci stderr:\n%s', err)
env=env) for line in out.decode().splitlines():
logging.debug('git-credential-luci stderr:\n%s', err) if line.startswith('password='):
for line in out.decode().splitlines(): return line[len('password='):].rstrip()
if line.startswith('password='): logging.error('git-credential-luci did not return a token')
return line[len('password='):].rstrip() return None
logging.error('git-credential-luci did not return a token')
return None
except subprocess2.CalledProcessError as e:
# subprocess2.CalledProcessError.__str__ nicely formats
# stdout/stderr.
logging.error('git-credential-luci failed: %s', e)
return None