From 863573b6650a8d32b14c5485eb6201cd71b1d9b4 Mon Sep 17 00:00:00 2001 From: Robert Iannucci Date: Thu, 25 Jul 2024 20:31:34 +0000 Subject: [PATCH] [gerrit_util] Automatically re-warm SSO cookies. In the event that `git-remote-sso` doesn't yield the expected cookies, run `git ls-remote` on chromium/All-Projects. This operation is fairly quick and robust - this fallback code should only execute at most 1/day. R=ayatane, yiwzhang Change-Id: I5290f47e56341e9c4fd3b295ae117650f592dc7c Bug: 342644760 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5741848 Auto-Submit: Robbie Iannucci Reviewed-by: Yiwei Zhang Commit-Queue: Yiwei Zhang --- gerrit_util.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/gerrit_util.py b/gerrit_util.py index ef7ad27a9..89767679e 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -495,9 +495,22 @@ class SSOAuthenticator(_Authenticator): @classmethod def _get_sso_info(cls) -> SSOInfo: with cls._sso_info_lock: + info = cls._sso_info if not info: info = cls._launch_sso_helper() + # HACK: `git-remote-sso` doesn't always properly warm up the + # cookies - in this case, run a canned git operation against + # a public repo to cause `git-remote-sso` to warm the cookies + # up, and then try pulling config again. + # + # BUG: b/342644760 + if not any(c.domain == '.google.com' for c in info.cookies): + LOGGER.debug('SSO: Refreshing .google.com cookies.') + scm.GIT.Capture(['ls-remote', 'sso://chromium/All-Projects']) + info = cls._launch_sso_helper() + if not any(c.domain == '.google.com' for c in info.cookies): + raise ValueError('Unable to extract .google.com cookie.') cls._sso_info = info return info @@ -525,9 +538,7 @@ class SSOAuthenticator(_Authenticator): # Finally, add cookies sso_info.cookies.add_cookie_header(conn) assert 'Cookie' in conn.req_headers, ( - 'sso_info.cookies.add_cookie_header failed to add Cookie' - ' (try running `git ls-remote sso://chromium/All-Projects` and retrying)' - ) + 'sso_info.cookies.add_cookie_header failed to add Cookie.') def debug_summary_state(self) -> str: return ''