From 877b1e3cbd01dac192da8fab784d2cad93704d73 Mon Sep 17 00:00:00 2001 From: Allen Li Date: Tue, 21 Jan 2025 13:59:20 -0800 Subject: [PATCH] [auth] Hide global SSO rewrite in local repo If the user uses SSO (and thus has SSO rewrite rules) for their global config, a repo which locally uses an email that doesn't use SSO needs an override. Bug: b/390219533 Change-Id: I639dae4c1a45bbd2c17180855c22260979b0dbc2 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6177780 Reviewed-by: Josip Sokcevic Commit-Queue: Allen Li --- git_auth.py | 7 ++++++- tests/git_auth_test.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/git_auth.py b/git_auth.py index 6cb5a8e39..bd483b207 100644 --- a/git_auth.py +++ b/git_auth.py @@ -36,7 +36,7 @@ class ConfigChanger(object): # # Increment this when making changes to the config, so that reliant # code can determine whether the config needs to be re-applied. - VERSION: int = 3 + VERSION: int = 4 def __init__( self, @@ -166,15 +166,20 @@ class ConfigChanger(object): def _apply_sso(self, cwd: str) -> None: """Apply config changes relating to SSO.""" sso_key: str = f'url.sso://{self._shortname}/.insteadOf' + http_key: str = f'url.{self._remote_url}.insteadOf' if self.mode == ConfigMode.NEW_AUTH: self._set_config(cwd, 'protocol.sso.allow', None) self._set_config(cwd, sso_key, None, modify_all=True) + # Shadow a potential global SSO rewrite rule. + self._set_config(cwd, http_key, self._remote_url, modify_all=True) elif self.mode == ConfigMode.NEW_AUTH_SSO: self._set_config(cwd, 'protocol.sso.allow', 'always') self._set_config(cwd, sso_key, self._base_url, modify_all=True) + self._set_config(cwd, http_key, None, modify_all=True) elif self.mode == ConfigMode.NO_AUTH: self._set_config(cwd, 'protocol.sso.allow', None) self._set_config(cwd, sso_key, None, modify_all=True) + self._set_config(cwd, http_key, None, modify_all=True) else: raise TypeError(f'Invalid mode {self.mode!r}') diff --git a/tests/git_auth_test.py b/tests/git_auth_test.py index 41a5ec4f8..4b450a03c 100755 --- a/tests/git_auth_test.py +++ b/tests/git_auth_test.py @@ -20,6 +20,8 @@ import scm_mock class TestConfigChanger(unittest.TestCase): + maxDiff = None + def setUp(self): self._global_state_view: Iterable[tuple[str, list[str]]] = scm_mock.GIT(self) @@ -39,6 +41,10 @@ class TestConfigChanger(unittest.TestCase): 'credential.https://chromium.googlesource.com/.helper': ['', 'luci'], 'http.cookiefile': [''], + 'url.https://chromium.googlesource.com/chromium/tools/depot_tools.git.insteadof': + [ + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git' + ], }, } self.assertEqual(scm.GIT._dump_config_state(), want) @@ -86,6 +92,10 @@ class TestConfigChanger(unittest.TestCase): 'credential.https://chromium.googlesource.com/.helper': ['', 'luci'], 'http.cookiefile': [''], + 'url.https://chromium.googlesource.com/chromium/tools/depot_tools.git.insteadof': + [ + 'https://chromium.googlesource.com/chromium/tools/depot_tools.git' + ], }, } self.assertEqual(scm.GIT._dump_config_state(), want)