From 5e5802d7a244d01039076113b3d91b953105c58b Mon Sep 17 00:00:00 2001 From: Aravind Vasudevan Date: Thu, 12 Jan 2023 22:29:42 +0000 Subject: [PATCH] Update log when luci-auth is not logged-in This change updates gsutil.py to capture when luci-auth is not logged in and prints useful log for signing in. This change couple enables luci-auth UI when luci-auth is enabled. Bug: 959170, 1359383 Change-Id: Ic636bb362df388073e1fd11d736a47f35b749016 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/4154550 Reviewed-by: Gavin Mak Commit-Queue: Aravind Vasudevan --- gsutil.py | 55 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/gsutil.py b/gsutil.py index 3bff1888c..66095d78c 100755 --- a/gsutil.py +++ b/gsutil.py @@ -169,32 +169,63 @@ def luci_context(cmd): def luci_login(): """Helper to run `luci-auth login`.""" - _luci_auth_cmd('login') + # luci-auth requires interactive shell. + return _luci_auth_cmd('login', interactive=True) -def _luci_auth_cmd(luci_cmd, wrapped_cmds=None): +def _luci_auth_cmd(luci_cmd, wrapped_cmds=None, interactive=False): """Helper to call luci-auth command.""" - print('WARNING: OOB authentication flow has been deprecated.') - print('Using luci-auth login instead.') - print('Override luci-auth by setting `BOTO_CONFIG` or ' - '`AWS_CREDENTIAL_FILE` in your env.\n') + print('Using luci-auth login since OOB is deprecated.') + print('Override luci-auth by setting `BOTO_CONFIG` in your env.\n') cmd = ['luci-auth', luci_cmd, '-scopes', ' '.join(LUCI_AUTH_SCOPES)] if wrapped_cmds: cmd += ['--'] + wrapped_cmds - return _run_subprocess(cmd) + if interactive: + return _run_subprocess(cmd, interactive=True, env=_enable_luci_auth_ui()) + p = _run_subprocess(cmd, env=_enable_luci_auth_ui()) -def _run_subprocess(cmd): + # If luci-auth is not logged in. + if 'Not logged in.' in p.stderr: + print('Not logged in.\n') + print('Login by running:') + print('\t$ gsutil.py config') + else: + if p.stdout: + print(p.stdout) + + if p.stderr: + print(p.stderr, file=sys.stderr) + + return p + + +def _enable_luci_auth_ui(): + """Returns environment variables to enable luci-auth""" + # TODO(aravindvasudev): clean up after luci-auth UI is released. + return {'LUCI_AUTH_LOGIN_SESSIONS_HOST': 'ci.chromium.org'} + + +def _run_subprocess(cmd, interactive=False, env=None): """Wrapper to run the given command within a subprocess.""" - return subprocess.call(cmd, shell=IS_WINDOWS) + kwargs = {'shell': IS_WINDOWS, 'encoding': 'utf-8'} + + if env: + kwargs['env'] = dict(os.environ, **env) + + if not interactive: + kwargs['stdout'] = subprocess.PIPE + kwargs['stderr'] = subprocess.PIPE + + return subprocess.run(cmd, **kwargs) def run_gsutil(target, args, clean=False): # Redirect gsutil config calls to luci-auth. if os.getenv(GSUTIL_ENABLE_LUCI_AUTH) == '1' and 'config' in args: - return luci_login() + return luci_login().returncode gsutil_bin = ensure_gsutil(VERSION, target, clean) args_opt = ['-o', 'GSUtil:software_update_check_period=0'] @@ -229,9 +260,9 @@ def run_gsutil(target, args, clean=False): if (os.getenv(GSUTIL_ENABLE_LUCI_AUTH) != '1' or _is_luci_context() or os.getenv('SWARMING_HEADLESS') == '1' or os.getenv('BOTO_CONFIG') or os.getenv('AWS_CREDENTIAL_FILE')): - return _run_subprocess(cmd) + return _run_subprocess(cmd, interactive=True).returncode - return luci_context(cmd) + return luci_context(cmd).returncode def parse_args():