git_auth.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. # Copyright (c) 2024 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Defines utilities for setting up Git authentication."""
  5. from __future__ import annotations
  6. import enum
  7. from collections.abc import Collection
  8. import contextlib
  9. import functools
  10. import logging
  11. import os
  12. from typing import TYPE_CHECKING, Callable, NamedTuple, TextIO
  13. import urllib.parse
  14. import gerrit_util
  15. import newauth
  16. import scm
  17. if TYPE_CHECKING:
  18. # Causes import cycle if imported normally
  19. import git_cl
  20. class ConfigMode(enum.Enum):
  21. """Modes to pass to ConfigChanger"""
  22. NO_AUTH = 1
  23. NEW_AUTH = 2
  24. NEW_AUTH_SSO = 3
  25. class ConfigChanger(object):
  26. """Changes Git auth config as needed for Gerrit."""
  27. # Can be used to determine whether this version of the config has
  28. # been applied to a Git repo.
  29. #
  30. # Increment this when making changes to the config, so that reliant
  31. # code can determine whether the config needs to be re-applied.
  32. VERSION: int = 6
  33. def __init__(
  34. self,
  35. *,
  36. mode: ConfigMode,
  37. remote_url: str,
  38. set_config_func: Callable[..., None] = scm.GIT.SetConfig,
  39. ):
  40. """Create a new ConfigChanger.
  41. Args:
  42. mode: How to configure auth
  43. remote_url: Git repository's remote URL, e.g.,
  44. https://chromium.googlesource.com/chromium/tools/depot_tools.git
  45. set_config_func: Function used to set configuration. Used
  46. for testing.
  47. """
  48. self.mode: ConfigMode = mode
  49. self._remote_url: str = remote_url
  50. self._set_config_func: Callable[..., None] = set_config_func
  51. @functools.cached_property
  52. def _shortname(self) -> str:
  53. # Example: chromium
  54. parts: urllib.parse.SplitResult = urllib.parse.urlsplit(
  55. self._remote_url)
  56. return _url_shortname(parts)
  57. @functools.cached_property
  58. def _host_url(self) -> str:
  59. # Example: https://chromium.googlesource.com
  60. # Example: https://chromium-review.googlesource.com
  61. parts: urllib.parse.SplitResult = urllib.parse.urlsplit(
  62. self._remote_url)
  63. return _url_host_url(parts)
  64. @functools.cached_property
  65. def _root_url(self) -> str:
  66. # Example: https://chromium.googlesource.com/
  67. # Example: https://chromium-review.googlesource.com/
  68. parts: urllib.parse.SplitResult = urllib.parse.urlsplit(
  69. self._remote_url)
  70. return _url_root_url(parts)
  71. @classmethod
  72. def new_from_env(cls, cwd: str, cl: git_cl.Changelist) -> ConfigChanger:
  73. """Create a ConfigChanger by inferring from env.
  74. The Gerrit host is inferred from the current repo/branch.
  75. The user, which is used to determine the mode, is inferred using
  76. git-config(1) in the given `cwd`.
  77. """
  78. # This is determined either from the branch or repo config.
  79. #
  80. # Example: chromium-review.googlesource.com
  81. gerrit_host = cl.GetGerritHost()
  82. # This depends on what the user set for their remote.
  83. # There are a couple potential variations for the same host+repo.
  84. #
  85. # Example:
  86. # https://chromium.googlesource.com/chromium/tools/depot_tools.git
  87. remote_url = cl.GetRemoteUrl()
  88. if gerrit_host is None or remote_url is None:
  89. raise Exception(
  90. 'Error Git auth settings inferring from environment:'
  91. f' {gerrit_host=} {remote_url=}')
  92. assert gerrit_host is not None
  93. assert remote_url is not None
  94. return cls(
  95. mode=cls._infer_mode(cwd, gerrit_host),
  96. remote_url=remote_url,
  97. )
  98. @classmethod
  99. def new_for_remote(cls, cwd: str, remote_url: str) -> ConfigChanger:
  100. """Create a ConfigChanger for the given Gerrit host.
  101. The user, which is used to determine the mode, is inferred using
  102. git-config(1) in the given `cwd`.
  103. """
  104. c = cls(
  105. mode=ConfigMode.NEW_AUTH,
  106. remote_url=remote_url,
  107. )
  108. assert c._shortname, "Short name is empty"
  109. c.mode = cls._infer_mode(cwd, c._shortname + '-review.googlesource.com')
  110. return c
  111. @staticmethod
  112. def _infer_mode(cwd: str, gerrit_host: str) -> ConfigMode:
  113. """Infer default mode to use."""
  114. if not newauth.Enabled():
  115. return ConfigMode.NO_AUTH
  116. email: str = scm.GIT.GetConfig(cwd, 'user.email') or ''
  117. if gerrit_util.ShouldUseSSO(gerrit_host, email):
  118. return ConfigMode.NEW_AUTH_SSO
  119. if not gerrit_util.GitCredsAuthenticator.gerrit_account_exists(
  120. gerrit_host):
  121. return ConfigMode.NO_AUTH
  122. return ConfigMode.NEW_AUTH
  123. def apply(self, cwd: str) -> None:
  124. """Apply config changes to the Git repo directory."""
  125. self._apply_cred_helper(cwd)
  126. self._apply_sso(cwd)
  127. self._apply_gitcookies(cwd)
  128. def apply_global(self, cwd: str) -> None:
  129. """Apply config changes to the global (user) Git config.
  130. This will make the instance's mode (e.g., SSO or not) the global
  131. default for the Gerrit host, if not overridden by a specific Git repo.
  132. """
  133. self._apply_global_cred_helper(cwd)
  134. self._apply_global_sso(cwd)
  135. def _apply_cred_helper(self, cwd: str) -> None:
  136. """Apply config changes relating to credential helper."""
  137. cred_key: str = f'credential.{self._host_url}.helper'
  138. if self.mode == ConfigMode.NEW_AUTH:
  139. self._set_config(cwd, cred_key, '', modify_all=True)
  140. self._set_config(cwd, cred_key, 'luci', append=True)
  141. elif self.mode == ConfigMode.NEW_AUTH_SSO:
  142. self._set_config(cwd, cred_key, None, modify_all=True)
  143. elif self.mode == ConfigMode.NO_AUTH:
  144. self._set_config(cwd, cred_key, None, modify_all=True)
  145. else:
  146. raise TypeError(f'Invalid mode {self.mode!r}')
  147. # Cleanup old from version 4
  148. old_key: str = f'credential.{self._root_url}.helper'
  149. self._set_config(cwd, old_key, None, modify_all=True)
  150. def _apply_sso(self, cwd: str) -> None:
  151. """Apply config changes relating to SSO."""
  152. sso_key: str = f'url.sso://{self._shortname}/.insteadOf'
  153. http_key: str = f'url.{self._remote_url}.insteadOf'
  154. if self.mode == ConfigMode.NEW_AUTH:
  155. self._set_config(cwd, 'protocol.sso.allow', None)
  156. self._set_config(cwd, sso_key, None, modify_all=True)
  157. # Shadow a potential global SSO rewrite rule.
  158. self._set_config(cwd, http_key, self._remote_url, modify_all=True)
  159. elif self.mode == ConfigMode.NEW_AUTH_SSO:
  160. self._set_config(cwd, 'protocol.sso.allow', 'always')
  161. self._set_config(cwd, sso_key, self._root_url, modify_all=True)
  162. self._set_config(cwd, http_key, None, modify_all=True)
  163. elif self.mode == ConfigMode.NO_AUTH:
  164. self._set_config(cwd, 'protocol.sso.allow', None)
  165. self._set_config(cwd, sso_key, None, modify_all=True)
  166. self._set_config(cwd, http_key, None, modify_all=True)
  167. else:
  168. raise TypeError(f'Invalid mode {self.mode!r}')
  169. def _apply_gitcookies(self, cwd: str) -> None:
  170. """Apply config changes relating to gitcookies."""
  171. if self.mode == ConfigMode.NEW_AUTH:
  172. # Override potential global setting
  173. self._set_config(cwd, 'http.cookieFile', '', modify_all=True)
  174. elif self.mode == ConfigMode.NEW_AUTH_SSO:
  175. # Override potential global setting
  176. self._set_config(cwd, 'http.cookieFile', '', modify_all=True)
  177. elif self.mode == ConfigMode.NO_AUTH:
  178. self._set_config(cwd, 'http.cookieFile', None, modify_all=True)
  179. else:
  180. raise TypeError(f'Invalid mode {self.mode!r}')
  181. def _apply_global_cred_helper(self, cwd: str) -> None:
  182. """Apply config changes relating to credential helper."""
  183. cred_key: str = f'credential.{self._host_url}.helper'
  184. if self.mode == ConfigMode.NEW_AUTH:
  185. self._set_config(cwd, cred_key, '', scope='global', modify_all=True)
  186. self._set_config(cwd, cred_key, 'luci', scope='global', append=True)
  187. elif self.mode == ConfigMode.NEW_AUTH_SSO:
  188. # Avoid editing the user's config in case they manually
  189. # configured something.
  190. pass
  191. elif self.mode == ConfigMode.NO_AUTH:
  192. # Avoid editing the user's config in case they manually
  193. # configured something.
  194. pass
  195. else:
  196. raise TypeError(f'Invalid mode {self.mode!r}')
  197. # Cleanup old from version 4
  198. old_key: str = f'credential.{self._root_url}.helper'
  199. self._set_config(cwd, old_key, None, modify_all=True, scope='global')
  200. def _apply_global_sso(self, cwd: str) -> None:
  201. """Apply config changes relating to SSO."""
  202. sso_key: str = f'url.sso://{self._shortname}/.insteadOf'
  203. if self.mode == ConfigMode.NEW_AUTH:
  204. # Do not unset protocol.sso.allow because it may be used by
  205. # other hosts.
  206. self._set_config(cwd,
  207. sso_key,
  208. None,
  209. scope='global',
  210. modify_all=True)
  211. elif self.mode == ConfigMode.NEW_AUTH_SSO:
  212. self._set_config(cwd,
  213. 'protocol.sso.allow',
  214. 'always',
  215. scope='global')
  216. self._set_config(cwd,
  217. sso_key,
  218. self._root_url,
  219. scope='global',
  220. modify_all=True)
  221. elif self.mode == ConfigMode.NO_AUTH:
  222. # Avoid editing the user's config in case they manually
  223. # configured something.
  224. pass
  225. else:
  226. raise TypeError(f'Invalid mode {self.mode!r}')
  227. def _set_config(self, *args, **kwargs) -> None:
  228. self._set_config_func(*args, **kwargs)
  229. def AutoConfigure(cwd: str, cl: git_cl.Changelist) -> None:
  230. """Configure Git authentication automatically.
  231. This tracks when the config that has already been applied and skips
  232. doing anything if so.
  233. This may modify the global Git config and the local repo config as
  234. needed.
  235. """
  236. latestVer: int = ConfigChanger.VERSION
  237. v: int = 0
  238. try:
  239. v = int(
  240. scm.GIT.GetConfig(cwd, 'depot-tools.gitauthautoconfigured') or '0')
  241. except ValueError:
  242. v = 0
  243. if v < latestVer:
  244. logging.debug(
  245. 'Automatically configuring Git repo authentication'
  246. ' (current version: %r, latest: %r)', v, latestVer)
  247. Configure(cwd, cl)
  248. scm.GIT.SetConfig(cwd, 'depot-tools.gitAuthAutoConfigured',
  249. str(latestVer))
  250. def Configure(cwd: str, cl: git_cl.Changelist) -> None:
  251. """Configure Git authentication.
  252. This may modify the global Git config and the local repo config as
  253. needed.
  254. """
  255. logging.debug('Configuring Git authentication...')
  256. logging.debug('Configuring global Git authentication...')
  257. # We want the user's global config.
  258. # We can probably assume the root directory doesn't have any local
  259. # Git configuration.
  260. c = ConfigChanger.new_from_env('/', cl)
  261. c.apply_global(os.path.expanduser('~'))
  262. c2 = ConfigChanger.new_from_env(cwd, cl)
  263. if c2.mode == c.mode:
  264. logging.debug(
  265. 'Local user wants same mode %s as global;'
  266. ' clearing local repo auth config', c2.mode)
  267. c2.mode = ConfigMode.NO_AUTH
  268. c2.apply(cwd)
  269. return
  270. logging.debug('Local user wants mode %s while global user wants mode %s',
  271. c2.mode, c.mode)
  272. logging.debug('Configuring current Git repo authentication...')
  273. c2.apply(cwd)
  274. def ConfigureGlobal(cwd: str, remote_url: str) -> None:
  275. """Configure global/user Git authentication."""
  276. logging.debug('Configuring global Git authentication for %s', remote_url)
  277. # Checks to ensure this doesn't error when called with "bad" URLs.
  278. #
  279. # Don't try to configure auth for local files.
  280. if remote_url.startswith('file://'):
  281. return
  282. # Skip for local files that aren't even URIs.
  283. if '://' not in remote_url:
  284. return
  285. ConfigChanger.new_for_remote(cwd, remote_url).apply_global(cwd)
  286. def ClearRepoConfig(cwd: str, cl: git_cl.Changelist) -> None:
  287. """Clear the current Git repo authentication."""
  288. logging.debug('Clearing current Git repo authentication...')
  289. c = ConfigChanger.new_from_env(cwd, cl)
  290. c.mode = ConfigMode.NO_AUTH
  291. c.apply(cwd)
  292. class _ConfigError(Exception):
  293. """Subclass for errors raised by ConfigWizard.
  294. This may be unused, but keep this around so that anyone who needs it
  295. when tweaking ConfigWizard can use it.
  296. """
  297. class _ConfigMethod(enum.Enum):
  298. """Enum used in _ConfigInfo."""
  299. OAUTH = 1
  300. SSO = 2
  301. class _ConfigInfo(NamedTuple):
  302. """Result for ConfigWizard._configure."""
  303. method: _ConfigMethod
  304. class _GitcookiesSituation(NamedTuple):
  305. """Result for ConfigWizard._check_gitcookies."""
  306. gitcookies_exists: bool
  307. cookiefile: str
  308. cookiefile_exists: bool
  309. divergent_cookiefiles: bool
  310. _InputChecker = Callable[['UserInterface', str], bool]
  311. def _check_any(ui: UserInterface, line: str) -> bool:
  312. """Allow any input."""
  313. return True
  314. def _check_nonempty(ui: UserInterface, line: str) -> bool:
  315. """Reject nonempty input."""
  316. if line:
  317. return True
  318. ui.write('Input cannot be empty.\n')
  319. return False
  320. def _check_choice(choices: Collection[str]) -> _InputChecker:
  321. """Allow specified choices."""
  322. def func(ui: UserInterface, line: str) -> bool:
  323. if line in choices:
  324. return True
  325. ui.write('Invalid choice.\n')
  326. return False
  327. return func
  328. class UserInterface(object):
  329. """Abstracts user interaction for ConfigWizard.
  330. This implementation supports regular terminals.
  331. """
  332. _prompts = {
  333. None: 'y/n',
  334. True: 'Y/n',
  335. False: 'y/N',
  336. }
  337. def __init__(self, stdin: TextIO, stdout: TextIO):
  338. self._stdin = stdin
  339. self._stdout = stdout
  340. def read_yn(self, prompt: str, *, default: bool | None = None) -> bool:
  341. """Reads a yes/no response.
  342. The prompt should end in '?'.
  343. """
  344. prompt = f'{prompt} [{self._prompts[default]}]: '
  345. while True:
  346. self._stdout.write(prompt)
  347. self._stdout.flush()
  348. response = self._stdin.readline().strip().lower()
  349. if response in ('y', 'yes'):
  350. return True
  351. if response in ('n', 'no'):
  352. return False
  353. if not response and default is not None:
  354. return default
  355. self._stdout.write('Type y or n.\n')
  356. def read_line(self,
  357. prompt: str,
  358. *,
  359. check: _InputChecker = _check_any) -> str:
  360. """Reads a line of input.
  361. Trailing whitespace is stripped from the read string.
  362. The prompt should not end in any special indicator like a colon.
  363. Optionally, an input check function may be provided. This
  364. method will continue to prompt for input until it passes the
  365. check. The check should print some explanation for rejected
  366. inputs.
  367. """
  368. while True:
  369. self._stdout.write(f'{prompt}: ')
  370. self._stdout.flush()
  371. s = self._stdin.readline().rstrip()
  372. if check(self, s):
  373. return s
  374. def read_enter(self, text: str = 'Press Enter to proceed.') -> None:
  375. """Reads an Enter.
  376. Used to interactively proceed.
  377. """
  378. self._stdout.write(text)
  379. self._stdout.flush()
  380. self._stdin.readline()
  381. def write(self, s: str) -> None:
  382. """Write string as-is.
  383. The string should usually end in a newline.
  384. """
  385. self._stdout.write(s)
  386. RemoteURLFunc = Callable[[], str]
  387. class ConfigWizard(object):
  388. """Wizard for setting up user's Git config Gerrit authentication.
  389. Instances carry internal state, so cannot be reused.
  390. """
  391. def __init__(self, *, ui: UserInterface, remote_url_func: RemoteURLFunc):
  392. self._ui = ui
  393. self._remote_url_func = remote_url_func
  394. # Internal state
  395. self._user_actions = []
  396. def run(self, *, force_global: bool) -> None:
  397. with self._handle_config_errors():
  398. self._run(force_global=force_global)
  399. def _run(self, *, force_global: bool) -> None:
  400. self._println('This tool will help check your Gerrit authentication.')
  401. self._println(
  402. '(Report any issues to https://issues.chromium.org/issues/new?component=1456702&template=2076315)'
  403. )
  404. self._println()
  405. self._fix_gitcookies()
  406. self._println()
  407. self._println('Checking for SSO helper...')
  408. if self._check_sso_helper():
  409. self._println('SSO helper is available.')
  410. self._set_config('protocol.sso.allow', 'always', scope='global')
  411. self._check_gce()
  412. self._println()
  413. self._run_gerrit_host_configuration(force_global=force_global)
  414. self._println()
  415. self._println('Successfully finished auth configuration check.')
  416. self._print_actions_for_user()
  417. def _run_gerrit_host_configuration(self, *, force_global: bool) -> None:
  418. remote_url = self._remote_url_func()
  419. if _is_gerrit_url(remote_url):
  420. if force_global:
  421. self._println(
  422. 'We will pretend to be running outside of a Gerrit repository'
  423. )
  424. self._println(
  425. 'and check your global Git configuration since you passed --global.'
  426. )
  427. self._run_outside_repo()
  428. else:
  429. self._println(
  430. 'Looks like we are running inside a Gerrit repository,')
  431. self._println('so we will check your Git configuration for it.')
  432. self._run_inside_repo()
  433. else:
  434. self._println(
  435. 'Looks like we are running outside of a Gerrit repository,')
  436. self._println('so we will check your global Git configuration.')
  437. self._run_outside_repo()
  438. def _run_outside_repo(self) -> None:
  439. global_email = self._check_global_email()
  440. self._println()
  441. self._println('Since we are not running in a Gerrit repository,')
  442. self._println('we do not know which Gerrit host to check.')
  443. self._println(
  444. 'You can re-run this command inside a Gerrit repository to check a specific host,'
  445. )
  446. self._println('or we can set up some commonly used Gerrit hosts.')
  447. self._println()
  448. self._println(
  449. "(If you haven't already set up auth for these Gerrit hosts,")
  450. self._println(
  451. "and you skip this, then you won't be able to auth to those hosts.")
  452. self._println(
  453. "This means lots of things will fail, like gclient sync.)")
  454. self._println()
  455. if not self._read_yn('Set up commonly used Gerrit hosts?',
  456. default=True):
  457. self._println('Okay, skipping Gerrit host setup.')
  458. self._println(
  459. 'You can re-run this command later or follow the instructions for manual configuration.'
  460. )
  461. self._print_manual_instructions()
  462. return
  463. hosts = [
  464. 'android.googlesource.com',
  465. 'aomedia.googlesource.com',
  466. 'beto-core.googlesource.com',
  467. 'boringssl.googlesource.com',
  468. 'chromium.googlesource.com',
  469. 'chrome-internal.googlesource.com',
  470. 'dawn.googlesource.com',
  471. 'pdfium.googlesource.com',
  472. 'quiche.googlesource.com',
  473. 'skia.googlesource.com',
  474. 'swiftshader.googlesource.com',
  475. 'webrtc.googlesource.com',
  476. ]
  477. self._println()
  478. self._println('We will set up auth for the following hosts:')
  479. for host in hosts:
  480. self._println(f'- {host}')
  481. self._println()
  482. self._read_enter()
  483. used_oauth = False
  484. for host in hosts:
  485. self._println()
  486. self._println(f'Checking authentication config for {host}')
  487. parts = urllib.parse.urlsplit(f'https://{host}/')
  488. info = self._configure_host(parts, global_email, scope='global')
  489. if info.method == _ConfigMethod.OAUTH:
  490. used_oauth = True
  491. if used_oauth:
  492. self._print_oauth_instructions()
  493. self._println()
  494. self._println(
  495. "If you need to set up any uncommonly used hosts that we didn't set up above,"
  496. )
  497. self._println('you can set them up manually.')
  498. self._print_manual_instructions()
  499. def _run_inside_repo(self) -> None:
  500. global_email = self._check_global_email()
  501. info = self._configure_repo(global_email=global_email)
  502. # This repo should be confirmed to be Gerrit by this point.
  503. assert info is not None
  504. if info.method == _ConfigMethod.OAUTH:
  505. self._print_oauth_instructions()
  506. dirs = list(scm.GIT.ListSubmodules(os.getcwd()))
  507. if dirs:
  508. self._println()
  509. self._println('This repository appears to have submodules.')
  510. self._println(
  511. 'These may use different Gerrit hosts and need to be configured separately.'
  512. )
  513. self._println_action(
  514. "If you haven't yet, run `git cl creds-check --global` to configure common Gerrit hosts."
  515. )
  516. # Configuring Git for Gerrit auth
  517. def _configure_repo(self, *, global_email: str) -> _ConfigInfo | None:
  518. """Configure current Git repo for Gerrit auth.
  519. Returns None if current Git repo doesn't have Gerrit remote.
  520. """
  521. self._println()
  522. self._println(f'Configuring Gerrit auth for {os.getcwd()}')
  523. remote_url = self._remote_url_func()
  524. if not _is_gerrit_url(remote_url):
  525. self._println(
  526. f"Current repo remote {remote_url} doesn't look like Gerrit, so skipping"
  527. )
  528. return None
  529. self._println(f"Repo remote is {remote_url}")
  530. local_email = self._check_local_email()
  531. email = global_email
  532. scope = 'global'
  533. if local_email and local_email != global_email:
  534. self._println()
  535. self._println(
  536. 'You have different emails configured locally vs globally.')
  537. self._println(
  538. 'We will configure Gerrit authentication for your local repo only.'
  539. )
  540. email = local_email
  541. scope = 'local'
  542. self._println()
  543. parts = urllib.parse.urlsplit(remote_url)
  544. return self._configure_host(parts, email, scope=scope)
  545. def _configure_host(self, parts: urllib.parse.SplitResult, email: str, *,
  546. scope: scm.GitConfigScope) -> _ConfigInfo:
  547. """Configure auth for one Gerrit host."""
  548. use_sso = self._check_use_sso(parts, email)
  549. if use_sso:
  550. self._configure_sso(parts, scope=scope)
  551. return _ConfigInfo(method=_ConfigMethod.SSO)
  552. self._configure_oauth(parts, scope=scope)
  553. return _ConfigInfo(method=_ConfigMethod.OAUTH)
  554. def _configure_sso(self, parts: urllib.parse.SplitResult, *,
  555. scope: scm.GitConfigScope) -> None:
  556. if parts.scheme == 'sso':
  557. self._println(f'Your remote URL {parts.geturl()} already uses SSO')
  558. else:
  559. self._set_sso_rewrite(parts, scope=scope)
  560. self._clear_url_rewrite_override(parts, scope=scope)
  561. def _configure_oauth(self, parts: urllib.parse.SplitResult, *,
  562. scope: scm.GitConfigScope) -> None:
  563. self._set_oauth_helper(parts, scope=scope)
  564. if scope == 'local':
  565. # Override a potential SSO rewrite set in the global config
  566. self._set_url_rewrite_override(parts, scope=scope)
  567. self._clear_sso_rewrite(parts, scope=scope)
  568. # Fixing gitcookies
  569. def _fix_gitcookies(self):
  570. sit = self._check_gitcookies()
  571. if not sit.cookiefile:
  572. self._println(
  573. "You don't have a cookie file configured in Git (good).")
  574. if sit.gitcookies_exists:
  575. self._println(
  576. 'However, you have a .gitcookies file (which is not configured for Git).'
  577. )
  578. self._println(
  579. 'This won"t affect Git authentication, but may cause issues for'
  580. )
  581. self._println('other Gerrit operations in depot_tools.')
  582. if self._read_yn(
  583. 'Shall we move your .gitcookies file (to a backup location)?',
  584. default=True):
  585. self._move_file(self._gitcookies())
  586. self._println(
  587. 'Note that some tools may still use the (legacy) .gitcookies file.'
  588. )
  589. self._println(
  590. 'If you encounter an issue, please report it.')
  591. return
  592. self._println('You appear to have a cookie file configured for Git.')
  593. self._println(f'http.cookiefile={sit.cookiefile!r}')
  594. if not sit.cookiefile_exists:
  595. self._println('However, this file does not exist.')
  596. self._println(
  597. 'This will not affect anything, but we suggest removing the http.cookiefile from your Git config.'
  598. )
  599. if self._read_yn('Shall we remove it for you?', default=True):
  600. self._set_config('http.cookiefile', None, scope='global')
  601. return
  602. if sit.divergent_cookiefiles:
  603. self._println()
  604. self._println(
  605. 'You also have a .gitcookies file, which is different from the cookefile in your Git config.'
  606. )
  607. self._println('We cannot handle this unusual case right now.')
  608. raise _ConfigError('unusual gitcookie setup')
  609. with open(sit.cookiefile, 'r') as f:
  610. info = _parse_cookiefile(f)
  611. if not info.contains_gerrit:
  612. self._println(
  613. "The cookie file doesn't appear to contain any Gerrit cookies,")
  614. self._println('so we will ignore it.')
  615. return
  616. if info.contains_nongerrit:
  617. self._println(
  618. 'The cookie file contains Gerrit cookies and non-Gerrit cookies.'
  619. )
  620. self._println(
  621. 'Cookie auth is deprecated, and these cookies may interfere with Gerrit authentication.'
  622. )
  623. self._println(
  624. "Since you have non-Gerrit cookies too, we won't try to fix it for you."
  625. )
  626. self._println_action(
  627. f'Please remove the Gerrit cookies (lines containing .googlesource.com) from {sit.cookiefile}'
  628. )
  629. return
  630. self._println('The cookie file contains Gerrit cookies.')
  631. self._println(
  632. 'Cookie auth is deprecated, and these cookies may interfere with Gerrit authentication.'
  633. )
  634. if not self._read_yn(
  635. 'Shall we move your cookie file (to a backup location)?',
  636. default=True):
  637. self._println(
  638. 'Okay, we recommend that you move (or remove) it later to avoid issues.'
  639. )
  640. return
  641. self._move_file(sit.cookiefile)
  642. self._set_config('http.cookiefile', None, scope='global')
  643. # Self-contained checks for specific things
  644. def _check_gitcookies(self) -> _GitcookiesSituation:
  645. """Checks various things about the user's gitcookies situation."""
  646. gitcookies = self._gitcookies()
  647. gitcookies_exists = os.path.exists(gitcookies)
  648. cookiefile = scm.GIT.GetConfig(
  649. os.getcwd(), 'http.cookiefile', scope='global') or ''
  650. cookiefile_exists = os.path.exists(cookiefile)
  651. divergent_cookiefiles = gitcookies_exists and cookiefile_exists and not os.path.samefile(
  652. gitcookies, cookiefile)
  653. return _GitcookiesSituation(
  654. gitcookies_exists=gitcookies_exists,
  655. cookiefile=cookiefile,
  656. cookiefile_exists=cookiefile_exists,
  657. divergent_cookiefiles=divergent_cookiefiles,
  658. )
  659. def _check_gce(self):
  660. if not gerrit_util.GceAuthenticator.is_applicable():
  661. return
  662. self._println()
  663. self._println('This appears to be a GCE VM.')
  664. self._println('You will need to add `export SKIP_GCE_AUTH_FOR_GIT=1`')
  665. self._println('to your .bashrc or similar.')
  666. fallback_msg = 'Add `export SKIP_GCE_AUTH_FOR_GIT=1` to your .bashrc or similar.'
  667. if os.name == 'nt':
  668. # Can't automatically handle Windows yet.
  669. self._println_action(fallback_msg)
  670. return
  671. rcfile: str | None = None
  672. options = [
  673. os.path.expanduser('~/.bashrc'),
  674. ]
  675. for p in options:
  676. if os.path.exists(p):
  677. self._println(f'We found {p!r}')
  678. rcfile = p
  679. break
  680. if not rcfile:
  681. self._println("We couldn't automatically detect your rcfile")
  682. self._println("so you'll need to do it manually.")
  683. self._println_action(fallback_msg)
  684. return
  685. if self._read_yn(f'Shall we add this line to {rcfile}?', default=True):
  686. self._append_to_file(rcfile, 'export SKIP_GCE_AUTH_FOR_GIT=1')
  687. self._println_action(
  688. 'Restart your shell/terminal to use the updated environment.')
  689. return
  690. self._println_action(fallback_msg)
  691. def _check_global_email(self) -> str:
  692. """Checks and returns user's global Git email.
  693. Prompts the user to set it if it isn't set.
  694. """
  695. email = scm.GIT.GetConfig(os.getcwd(), 'user.email',
  696. scope='global') or ''
  697. if email:
  698. self._println(f'Your global Git email is: {email}')
  699. return email
  700. self._println()
  701. self._println(
  702. 'You do not have an email configured in your global Git config.')
  703. if not self._read_yn('Do you want to set one now?', default=True):
  704. self._println('Will attempt to continue without a global email.')
  705. return ''
  706. name = scm.GIT.GetConfig(os.getcwd(), 'user.name', scope='global') or ''
  707. if not name:
  708. name = self._read_line('Enter your name (e.g., John Doe)',
  709. check=_check_nonempty)
  710. self._set_config('user.name', name, scope='global')
  711. email = self._read_line('Enter your email', check=_check_nonempty)
  712. self._set_config('user.email', email, scope='global')
  713. return email
  714. def _check_local_email(self) -> str:
  715. """Checks and returns the user's local Git email."""
  716. email = scm.GIT.GetConfig(os.getcwd(), 'user.email',
  717. scope='local') or ''
  718. if email:
  719. self._println(
  720. f'You have an email configured in your local repo: {email}')
  721. return email
  722. def _check_use_sso(self, parts: urllib.parse.SplitResult,
  723. email: str) -> bool:
  724. """Checks whether SSO is needed for the given user and host."""
  725. if not self._check_sso_helper():
  726. return False
  727. host = _url_review_host(parts)
  728. self._println(f'Checking SSO requirement for {email!r} on {host}')
  729. self._println(
  730. '(Note that this check may require SSO; if you get an error,')
  731. self._println('you will need to login to SSO and re-run this command.)')
  732. result = gerrit_util.CheckShouldUseSSO(host, email)
  733. text = 'use' if result.status else 'not use'
  734. self._println(f'Decided we should {text} SSO for {email!r} on {host}')
  735. self._println(f'Reason: {result.reason}')
  736. self._println()
  737. return result.status
  738. def _check_sso_helper(self) -> bool:
  739. """Checks and returns whether SSO helper is available."""
  740. return bool(gerrit_util.ssoHelper.find_cmd())
  741. # Reused instruction printing
  742. def _print_manual_instructions(self) -> None:
  743. """Prints manual instructions for setting up auth."""
  744. self._println()
  745. self._println(
  746. 'Instructions for manually configuring Gerrit authentication:')
  747. self._println(
  748. 'https://commondatastorage.googleapis.com/chrome-infra-docs/flat/depot_tools/docs/html/depot_tools_gerrit_auth.html'
  749. )
  750. def _print_oauth_instructions(self) -> None:
  751. """Prints instructions for setting up OAuth helper."""
  752. self._println()
  753. self._println('We have configured Git to use an OAuth helper.')
  754. self._println('The OAuth helper requires its own login.')
  755. self._println_action(
  756. "If you haven't yet, run `git credential-luci login` using the same email as Git."
  757. )
  758. self._println(
  759. "(If you have already done this, you don't need to do it again.)")
  760. self._println(
  761. '(However, if you changed your email, you should do this again')
  762. self._println("to ensure you're using the right account.)")
  763. # Low level Git config manipulation
  764. def _set_oauth_helper(self, parts: urllib.parse.SplitResult, *,
  765. scope: scm.GitConfigScope) -> None:
  766. cred_key = _creds_helper_key(parts)
  767. self._set_config(cred_key, '', modify_all=True, scope=scope)
  768. self._set_config(cred_key, 'luci', append=True, scope=scope)
  769. def _set_sso_rewrite(self, parts: urllib.parse.SplitResult, *,
  770. scope: scm.GitConfigScope) -> None:
  771. sso_key = _sso_rewrite_key(parts)
  772. self._set_config(sso_key,
  773. _url_root_url(parts),
  774. modify_all=True,
  775. scope=scope)
  776. def _clear_sso_rewrite(self, parts: urllib.parse.SplitResult, *,
  777. scope: scm.GitConfigScope) -> None:
  778. sso_key = _sso_rewrite_key(parts)
  779. self._set_config(sso_key, None, modify_all=True, scope=scope)
  780. def _set_url_rewrite_override(self, parts: urllib.parse.SplitResult, *,
  781. scope: scm.GitConfigScope) -> None:
  782. url_key = _url_rewrite_key(parts)
  783. self._set_config(url_key, parts.geturl(), modify_all=True, scope=scope)
  784. def _clear_url_rewrite_override(self, parts: urllib.parse.SplitResult, *,
  785. scope: scm.GitConfigScope) -> None:
  786. url_key = _url_rewrite_key(parts)
  787. self._set_config(url_key, None, scope=scope, modify_all=True)
  788. def _set_config(self,
  789. key: str,
  790. value: str | None,
  791. *,
  792. scope: scm.GitConfigScope,
  793. modify_all: bool = False,
  794. append: bool = False) -> None:
  795. scope_msg = f'In your {scope} Git config,'
  796. if append:
  797. assert value is not None
  798. self._println_notify(
  799. f'{scope_msg} we appended {key}={value!r} to existing values')
  800. else:
  801. if value is None:
  802. action = f"we cleared {'all values' if modify_all else 'the value'} for {key}"
  803. else:
  804. action = f'we set {key}={value!r}'
  805. if modify_all:
  806. action += ', replacing any existing values'
  807. self._println_notify(f'{scope_msg} {action}')
  808. scm.GIT.SetConfig(os.getcwd(),
  809. key,
  810. value,
  811. scope=scope,
  812. modify_all=modify_all,
  813. append=append)
  814. # Low level misc helpers
  815. def _append_to_file(self, path: str, line: str) -> None:
  816. """Append line to file.
  817. One newline is written before and after the given line string.
  818. """
  819. with open(path, 'a') as f:
  820. f.write('\n')
  821. f.write(line)
  822. f.write('\n')
  823. self._println_notify(f'Added {line!r} to {path!r}')
  824. def _move_file(self, path: str) -> None:
  825. """Move file to a backup path."""
  826. backup = f'{path}.bak'
  827. n = 1
  828. while os.path.exists(backup):
  829. n += 1
  830. backup = f'{path}.bak{n}'
  831. os.rename(path, backup)
  832. self._println_notify(f'Moved {path!r} to {backup!r}')
  833. @contextlib.contextmanager
  834. def _handle_config_errors(self):
  835. try:
  836. yield None
  837. except _ConfigError as e:
  838. self._println(f'ConfigError: {e!s}')
  839. def _print_actions_for_user(self) -> None:
  840. """Print manual actions requested from user.
  841. Aggregates any actions printed throughout the wizard run so it's
  842. easier for the user.
  843. """
  844. if not self._user_actions:
  845. return
  846. self._println()
  847. self._println(
  848. "However, there are some manual actions that are suggested")
  849. self._println("(you don't have to re-run this command afterward):")
  850. for s in self._user_actions:
  851. self._println(f'- {s}')
  852. def _println_action(self, s: str) -> None:
  853. """Print a notification about a manual action request from user.
  854. Also queues up the action for _print_actions_for_user.
  855. """
  856. self._println(f'!!! {s}')
  857. self._user_actions.append(s)
  858. def _println_notify(self, s: str) -> None:
  859. """Print a notification about a change we made."""
  860. self._println(f'>>> {s}')
  861. def _println(self, s: str = '') -> None:
  862. self._ui.write(s)
  863. self._ui.write('\n')
  864. def _read_yn(self, prompt: str, *, default: bool | None = None) -> bool:
  865. ret = self._ui.read_yn(prompt, default=default)
  866. self._ui.write('\n')
  867. return ret
  868. def _read_line(self,
  869. prompt: str,
  870. *,
  871. check: _InputChecker = _check_any) -> str:
  872. ret = self._ui.read_line(prompt, check=check)
  873. self._ui.write('\n')
  874. return ret
  875. def _read_enter(self) -> None:
  876. self._ui.read_enter()
  877. self._ui.write('\n')
  878. @staticmethod
  879. def _gitcookies() -> str:
  880. """Path to user's gitcookies.
  881. Can be mocked for testing.
  882. """
  883. return os.path.expanduser('~/.gitcookies')
  884. class _CookiefileInfo(NamedTuple):
  885. """Result for _parse_cookiefile."""
  886. contains_gerrit: bool
  887. contains_nongerrit: bool
  888. def _parse_cookiefile(f: TextIO) -> _CookiefileInfo:
  889. """Checks cookie file contents.
  890. Used to guide auth configuration.
  891. """
  892. contains_gerrit = False
  893. contains_nongerrit = False
  894. for line in f:
  895. if line.lstrip().startswith('#'):
  896. continue
  897. if not line.strip():
  898. continue
  899. if '.googlesource.com' in line:
  900. contains_gerrit = True
  901. else:
  902. contains_nongerrit = True
  903. return _CookiefileInfo(
  904. contains_gerrit=contains_gerrit,
  905. contains_nongerrit=contains_nongerrit,
  906. )
  907. def _is_gerrit_url(url: str) -> bool:
  908. """Checks if URL is for a Gerrit host."""
  909. if not url:
  910. return False
  911. parts = urllib.parse.urlsplit(url)
  912. if parts.netloc.endswith('.googlesource.com') or parts.netloc.endswith(
  913. '.git.corp.google.com'):
  914. return True
  915. return False
  916. def _creds_helper_key(parts: urllib.parse.SplitResult) -> str:
  917. """Return Git config key for credential helpers."""
  918. return f'credential.{_url_host_url(parts)}.helper'
  919. def _sso_rewrite_key(parts: urllib.parse.SplitResult) -> str:
  920. """Return Git config key for SSO URL rewrites."""
  921. return f'url.sso://{_url_shortname(parts)}/.insteadOf'
  922. def _url_rewrite_key(parts: urllib.parse.SplitResult) -> str:
  923. """Return Git config key for rewriting the full URL."""
  924. return f'url.{parts.geturl()}.insteadOf'
  925. def _url_review_host(parts: urllib.parse.SplitResult) -> str:
  926. """Format URL as Gerrit review host.
  927. Example: chromium-review.googlesource.com
  928. """
  929. return f'{_url_shortname(parts)}-review.googlesource.com'
  930. def _url_shortname(parts: urllib.parse.SplitResult) -> str:
  931. """Format URL as Gerrit host shortname.
  932. Example: chromium
  933. """
  934. name: str = parts.netloc.split('.')[0]
  935. if name.endswith('-review'):
  936. name = name[:-len('-review')]
  937. return name
  938. def _url_host_url(parts: urllib.parse.SplitResult) -> str:
  939. """Format URL with host only (no path).
  940. Example: https://chromium.googlesource.com
  941. Example: https://chromium-review.googlesource.com
  942. """
  943. return parts._replace(path='', query='', fragment='').geturl()
  944. def _url_root_url(parts: urllib.parse.SplitResult) -> str:
  945. """Format URL with root path.
  946. Example: https://chromium.googlesource.com/
  947. Example: https://chromium-review.googlesource.com/
  948. """
  949. return parts._replace(path='/', query='', fragment='').geturl()