OLD | NEW |
1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Utilities for requesting information for a gerrit server via https. | 6 Utilities for requesting information for a gerrit server via https. |
7 | 7 |
8 https://gerrit-review.googlesource.com/Documentation/rest-api.html | 8 https://gerrit-review.googlesource.com/Documentation/rest-api.html |
9 """ | 9 """ |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 | 76 |
77 @staticmethod | 77 @staticmethod |
78 def get(): | 78 def get(): |
79 """Returns: (Authenticator) The identified Authenticator to use. | 79 """Returns: (Authenticator) The identified Authenticator to use. |
80 | 80 |
81 Probes the local system and its environment and identifies the | 81 Probes the local system and its environment and identifies the |
82 Authenticator instance to use. | 82 Authenticator instance to use. |
83 """ | 83 """ |
84 if GceAuthenticator.is_gce(): | 84 if GceAuthenticator.is_gce(): |
85 return GceAuthenticator() | 85 return GceAuthenticator() |
86 return NetrcAuthenticator() | 86 return CookiesAuthenticator() |
87 | 87 |
88 | 88 |
89 class NetrcAuthenticator(Authenticator): | 89 class CookiesAuthenticator(Authenticator): |
90 """Authenticator implementation that uses ".netrc" for token. | 90 """Authenticator implementation that uses ".netrc" or ".gitcookies" for token. |
| 91 |
| 92 Expected case for developer workstations. |
91 """ | 93 """ |
92 | 94 |
93 def __init__(self): | 95 def __init__(self): |
94 self.netrc = self._get_netrc() | 96 self.netrc = self._get_netrc() |
95 self.gitcookies = self._get_gitcookies() | 97 self.gitcookies = self._get_gitcookies() |
96 | 98 |
97 @staticmethod | 99 @classmethod |
98 def _get_netrc(): | 100 def get_new_password_message(cls, host): |
| 101 assert not host.startswith('http') |
| 102 # Assume *.googlesource.com pattern. |
| 103 parts = host.split('.') |
| 104 if not parts[0].endswith('-review'): |
| 105 parts[0] += '-review' |
| 106 url = 'https://%s/new-password' % ('.'.join(parts)) |
| 107 return 'You can (re)generate your credentails by visiting %s' % url |
| 108 |
| 109 @classmethod |
| 110 def get_netrc_path(cls): |
99 path = '_netrc' if sys.platform.startswith('win') else '.netrc' | 111 path = '_netrc' if sys.platform.startswith('win') else '.netrc' |
100 path = os.path.expanduser(os.path.join('~', path)) | 112 return os.path.expanduser(os.path.join('~', path)) |
| 113 |
| 114 @classmethod |
| 115 def _get_netrc(cls): |
| 116 path = cls.get_netrc_path() |
| 117 if not os.path.exists(path): |
| 118 return netrc.netrc(os.devnull) |
| 119 |
101 try: | 120 try: |
102 return netrc.netrc(path) | 121 return netrc.netrc(path) |
103 except IOError: | 122 except IOError: |
104 print >> sys.stderr, 'WARNING: Could not read netrc file %s' % path | 123 print >> sys.stderr, 'WARNING: Could not read netrc file %s' % path |
105 return netrc.netrc(os.devnull) | 124 return netrc.netrc(os.devnull) |
106 except netrc.NetrcParseError as e: | 125 except netrc.NetrcParseError: |
107 st = os.stat(e.path) | 126 st = os.stat(path) |
108 if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): | 127 if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): |
109 print >> sys.stderr, ( | 128 print >> sys.stderr, ( |
110 'WARNING: netrc file %s cannot be used because its file ' | 129 'WARNING: netrc file %s cannot be used because its file ' |
111 'permissions are insecure. netrc file permissions should be ' | 130 'permissions are insecure. netrc file permissions should be ' |
112 '600.' % path) | 131 '600.' % path) |
113 else: | 132 else: |
114 print >> sys.stderr, ('ERROR: Cannot use netrc file %s due to a ' | 133 print >> sys.stderr, ('ERROR: Cannot use netrc file %s due to a ' |
115 'parsing error.' % path) | 134 'parsing error.' % path) |
116 raise | 135 raise |
117 return netrc.netrc(os.devnull) | 136 return netrc.netrc(os.devnull) |
118 | 137 |
119 @staticmethod | 138 @classmethod |
120 def _get_gitcookies(): | 139 def get_gitcookies_path(cls): |
| 140 return os.path.join(os.environ['HOME'], '.gitcookies') |
| 141 |
| 142 @classmethod |
| 143 def _get_gitcookies(cls): |
121 gitcookies = {} | 144 gitcookies = {} |
122 path = os.path.join(os.environ['HOME'], '.gitcookies') | 145 path = cls.get_gitcookies_path() |
| 146 if not os.path.exists(path): |
| 147 return gitcookies |
| 148 |
123 try: | 149 try: |
124 f = open(path, 'rb') | 150 f = open(path, 'rb') |
125 except IOError: | 151 except IOError: |
126 return gitcookies | 152 return gitcookies |
127 | 153 |
128 with f: | 154 with f: |
129 for line in f: | 155 for line in f: |
130 try: | 156 try: |
131 fields = line.strip().split('\t') | 157 fields = line.strip().split('\t') |
132 if line.strip().startswith('#') or len(fields) != 7: | 158 if line.strip().startswith('#') or len(fields) != 7: |
(...skipping 13 matching lines...) Expand all Loading... |
146 if cookielib.domain_match(host, domain): | 172 if cookielib.domain_match(host, domain): |
147 auth = (creds[0], None, creds[1]) | 173 auth = (creds[0], None, creds[1]) |
148 break | 174 break |
149 | 175 |
150 if not auth: | 176 if not auth: |
151 auth = self.netrc.authenticators(host) | 177 auth = self.netrc.authenticators(host) |
152 if auth: | 178 if auth: |
153 return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2]))) | 179 return 'Basic %s' % (base64.b64encode('%s:%s' % (auth[0], auth[2]))) |
154 return None | 180 return None |
155 | 181 |
| 182 # Backwards compatibility just in case somebody imports this outside of |
| 183 # depot_tools. |
| 184 NetrcAuthenticator = CookiesAuthenticator |
| 185 |
156 | 186 |
157 class GceAuthenticator(Authenticator): | 187 class GceAuthenticator(Authenticator): |
158 """Authenticator implementation that uses GCE metadata service for token. | 188 """Authenticator implementation that uses GCE metadata service for token. |
159 """ | 189 """ |
160 | 190 |
161 _INFO_URL = 'http://metadata.google.internal' | 191 _INFO_URL = 'http://metadata.google.internal' |
162 _ACQUIRE_URL = ('http://metadata/computeMetadata/v1/instance/' | 192 _ACQUIRE_URL = ('http://metadata/computeMetadata/v1/instance/' |
163 'service-accounts/default/token') | 193 'service-accounts/default/token') |
164 _ACQUIRE_HEADERS = {"Metadata-Flavor": "Google"} | 194 _ACQUIRE_HEADERS = {"Metadata-Flavor": "Google"} |
165 | 195 |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 username = review.get('email', jmsg.get('name', '')) | 635 username = review.get('email', jmsg.get('name', '')) |
606 raise GerritError(200, 'Unable to set %s label for user "%s"' | 636 raise GerritError(200, 'Unable to set %s label for user "%s"' |
607 ' on change %s.' % (label, username, change)) | 637 ' on change %s.' % (label, username, change)) |
608 jmsg = GetChangeCurrentRevision(host, change) | 638 jmsg = GetChangeCurrentRevision(host, change) |
609 if not jmsg: | 639 if not jmsg: |
610 raise GerritError( | 640 raise GerritError( |
611 200, 'Could not get review information for change "%s"' % change) | 641 200, 'Could not get review information for change "%s"' % change) |
612 elif jmsg[0]['current_revision'] != revision: | 642 elif jmsg[0]['current_revision'] != revision: |
613 raise GerritError(200, 'While resetting labels on change "%s", ' | 643 raise GerritError(200, 'While resetting labels on change "%s", ' |
614 'a new patchset was uploaded.' % change) | 644 'a new patchset was uploaded.' % change) |
OLD | NEW |