newauth.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 common conditions for the new auth stack migration."""
  5. from __future__ import annotations
  6. import os
  7. import sys
  8. import scm
  9. def Enabled() -> bool:
  10. """Returns True if new auth stack is enabled."""
  11. if not SwitchedOn():
  12. return False
  13. if _HasGitcookies():
  14. _PrintGitcookiesWarning()
  15. return False
  16. return True
  17. def SwitchedOn() -> bool:
  18. """Returns True if new auth stack is "switched on".
  19. Note that this does not necessarily mean that new auth is enabled.
  20. In particular, we still disable new auth if a .gitcookies file is
  21. present, to protect bots that haven't been migrated yet.
  22. """
  23. if Default():
  24. return not ExplicitlyDisabled()
  25. return ExplicitlyEnabled()
  26. def Default() -> bool:
  27. "Returns default enablement status for new auth stack."
  28. return True
  29. def _HasGitcookies() -> bool:
  30. """Returns True if user has gitcookies file."""
  31. return os.path.exists(os.path.expanduser('~/.gitcookies'))
  32. _warning_printed = False
  33. def _PrintGitcookiesWarning() -> None:
  34. global _warning_printed
  35. if _warning_printed:
  36. return
  37. _warning_printed = True
  38. sys.stderr.write('''
  39. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Warning !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  40. depot_tools will soon stop using the .gitcookies file for authentication.
  41. To silence this warning, please run `git cl creds-check` which will help you fix this.
  42. If you encounter any issues, please report them using:
  43. https://issues.chromium.org/issues/new?component=1456702&template=2076315
  44. --------------------------------------------------------------------------------
  45. ''')
  46. def ExplicitlyEnabled() -> bool:
  47. """Returns True if new auth stack is explicitly enabled.
  48. Directly checks config and doesn't do gitcookie check.
  49. """
  50. return scm.GIT.GetConfig(os.getcwd(),
  51. 'depot-tools.usenewauthstack') in ('yes', 'on',
  52. 'true', '1')
  53. def ExplicitlyDisabled() -> bool:
  54. """Returns True if new auth stack is explicitly disabled."""
  55. return scm.GIT.GetConfig(os.getcwd(),
  56. 'depot-tools.usenewauthstack') in ('no', 'off',
  57. 'false', '0')