szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 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 |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ |
| 6 | Utilities for requesting information for a gerrit server via https. |
| 7 | |
| 8 | https://gerrit-review.googlesource.com/Documentation/rest-api.html |
| 9 | """ |
| 10 | |
| 11 | import base64 |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 12 | import contextlib |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 13 | import cookielib |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 14 | import httplib # Still used for its constants. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 15 | import json |
| 16 | import logging |
| 17 | import netrc |
| 18 | import os |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame^] | 19 | import random |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 20 | import re |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 21 | import socket |
szager@chromium.org | f202a25 | 2014-05-27 18:55:52 +0000 | [diff] [blame] | 22 | import stat |
| 23 | import sys |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 24 | import tempfile |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 25 | import time |
| 26 | import urllib |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 27 | import urlparse |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 28 | from cStringIO import StringIO |
| 29 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 30 | import auth |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 31 | import gclient_utils |
Aaron Gable | 8797cab | 2018-03-06 13:55:00 -0800 | [diff] [blame] | 32 | import subprocess2 |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 33 | from third_party import httplib2 |
szager@chromium.org | f202a25 | 2014-05-27 18:55:52 +0000 | [diff] [blame] | 34 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 35 | LOGGER = logging.getLogger() |
Steve Kobes | 5611772 | 2018-09-13 18:18:35 +0000 | [diff] [blame] | 36 | # With a starting sleep time of 1.5 seconds, 2^n exponential backoff, and seven |
| 37 | # total tries, the sleep time between the first and last tries will be 94.5 sec. |
| 38 | # TODO(crbug.com/881860): Lower this when crbug.com/877717 is fixed. |
| 39 | TRY_LIMIT = 7 |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 40 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 41 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 42 | # Controls the transport protocol used to communicate with gerrit. |
| 43 | # This is parameterized primarily to enable GerritTestCase. |
| 44 | GERRIT_PROTOCOL = 'https' |
| 45 | |
| 46 | |
| 47 | class GerritError(Exception): |
| 48 | """Exception class for errors commuicating with the gerrit-on-borg service.""" |
| 49 | def __init__(self, http_status, *args, **kwargs): |
| 50 | super(GerritError, self).__init__(*args, **kwargs) |
| 51 | self.http_status = http_status |
| 52 | self.message = '(%d) %s' % (self.http_status, self.message) |
| 53 | |
| 54 | |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 55 | class GerritAuthenticationError(GerritError): |
| 56 | """Exception class for authentication errors during Gerrit communication.""" |
| 57 | |
| 58 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 59 | def _QueryString(params, first_param=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 60 | """Encodes query parameters in the key:val[+key:val...] format specified here: |
| 61 | |
| 62 | https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes |
| 63 | """ |
| 64 | q = [urllib.quote(first_param)] if first_param else [] |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 65 | q.extend(['%s:%s' % (key, val) for key, val in params]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 66 | return '+'.join(q) |
| 67 | |
| 68 | |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 69 | def GetConnectionObject(protocol=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 70 | if protocol is None: |
| 71 | protocol = GERRIT_PROTOCOL |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 72 | if protocol in ('http', 'https'): |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 73 | return httplib2.Http() |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 74 | else: |
| 75 | raise RuntimeError( |
| 76 | "Don't know how to work with protocol '%s'" % protocol) |
| 77 | |
| 78 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 79 | class Authenticator(object): |
| 80 | """Base authenticator class for authenticator implementations to subclass.""" |
| 81 | |
| 82 | def get_auth_header(self, host): |
| 83 | raise NotImplementedError() |
| 84 | |
| 85 | @staticmethod |
| 86 | def get(): |
| 87 | """Returns: (Authenticator) The identified Authenticator to use. |
| 88 | |
| 89 | Probes the local system and its environment and identifies the |
| 90 | Authenticator instance to use. |
| 91 | """ |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 92 | # LUCI Context takes priority since it's normally present only on bots, |
| 93 | # which then must use it. |
| 94 | if LuciContextAuthenticator.is_luci(): |
| 95 | return LuciContextAuthenticator() |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 96 | if GceAuthenticator.is_gce(): |
| 97 | return GceAuthenticator() |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 98 | return CookiesAuthenticator() |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 99 | |
| 100 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 101 | class CookiesAuthenticator(Authenticator): |
| 102 | """Authenticator implementation that uses ".netrc" or ".gitcookies" for token. |
| 103 | |
| 104 | Expected case for developer workstations. |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 105 | """ |
| 106 | |
| 107 | def __init__(self): |
| 108 | self.netrc = self._get_netrc() |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 109 | self.gitcookies = self._get_gitcookies() |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 110 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 111 | @classmethod |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 112 | def get_new_password_url(cls, host): |
| 113 | assert not host.startswith('http') |
| 114 | # Assume *.googlesource.com pattern. |
| 115 | parts = host.split('.') |
| 116 | if not parts[0].endswith('-review'): |
| 117 | parts[0] += '-review' |
| 118 | return 'https://%s/new-password' % ('.'.join(parts)) |
| 119 | |
| 120 | @classmethod |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 121 | def get_new_password_message(cls, host): |
| 122 | assert not host.startswith('http') |
| 123 | # Assume *.googlesource.com pattern. |
| 124 | parts = host.split('.') |
| 125 | if not parts[0].endswith('-review'): |
| 126 | parts[0] += '-review' |
| 127 | url = 'https://%s/new-password' % ('.'.join(parts)) |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 128 | return 'You can (re)generate your credentials by visiting %s' % url |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 129 | |
| 130 | @classmethod |
| 131 | def get_netrc_path(cls): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 132 | path = '_netrc' if sys.platform.startswith('win') else '.netrc' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 133 | return os.path.expanduser(os.path.join('~', path)) |
| 134 | |
| 135 | @classmethod |
| 136 | def _get_netrc(cls): |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 137 | # Buffer the '.netrc' path. Use an empty file if it doesn't exist. |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 138 | path = cls.get_netrc_path() |
Sylvain Defresne | 2b138f7 | 2018-07-12 08:34:48 +0000 | [diff] [blame] | 139 | if not os.path.exists(path): |
| 140 | return netrc.netrc(os.devnull) |
| 141 | |
| 142 | st = os.stat(path) |
| 143 | if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): |
| 144 | print >> sys.stderr, ( |
| 145 | 'WARNING: netrc file %s cannot be used because its file ' |
| 146 | 'permissions are insecure. netrc file permissions should be ' |
| 147 | '600.' % path) |
| 148 | with open(path) as fd: |
| 149 | content = fd.read() |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 150 | |
| 151 | # Load the '.netrc' file. We strip comments from it because processing them |
| 152 | # can trigger a bug in Windows. See crbug.com/664664. |
| 153 | content = '\n'.join(l for l in content.splitlines() |
| 154 | if l.strip() and not l.strip().startswith('#')) |
| 155 | with tempdir() as tdir: |
| 156 | netrc_path = os.path.join(tdir, 'netrc') |
| 157 | with open(netrc_path, 'w') as fd: |
| 158 | fd.write(content) |
| 159 | os.chmod(netrc_path, (stat.S_IRUSR | stat.S_IWUSR)) |
| 160 | return cls._get_netrc_from_path(netrc_path) |
| 161 | |
| 162 | @classmethod |
| 163 | def _get_netrc_from_path(cls, path): |
| 164 | try: |
| 165 | return netrc.netrc(path) |
| 166 | except IOError: |
| 167 | print >> sys.stderr, 'WARNING: Could not read netrc file %s' % path |
| 168 | return netrc.netrc(os.devnull) |
| 169 | except netrc.NetrcParseError as e: |
| 170 | print >> sys.stderr, ('ERROR: Cannot use netrc file %s due to a ' |
| 171 | 'parsing error: %s' % (path, e)) |
| 172 | return netrc.netrc(os.devnull) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 173 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 174 | @classmethod |
| 175 | def get_gitcookies_path(cls): |
Ravi Mistry | 0bfa9ad | 2016-11-21 12:58:31 -0500 | [diff] [blame] | 176 | if os.getenv('GIT_COOKIES_PATH'): |
| 177 | return os.getenv('GIT_COOKIES_PATH') |
Aaron Gable | 8797cab | 2018-03-06 13:55:00 -0800 | [diff] [blame] | 178 | try: |
| 179 | return subprocess2.check_output( |
| 180 | ['git', 'config', '--path', 'http.cookiefile']).strip() |
| 181 | except subprocess2.CalledProcessError: |
| 182 | return os.path.join(os.environ['HOME'], '.gitcookies') |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 183 | |
| 184 | @classmethod |
| 185 | def _get_gitcookies(cls): |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 186 | gitcookies = {} |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 187 | path = cls.get_gitcookies_path() |
| 188 | if not os.path.exists(path): |
| 189 | return gitcookies |
| 190 | |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 191 | try: |
| 192 | f = open(path, 'rb') |
| 193 | except IOError: |
| 194 | return gitcookies |
| 195 | |
| 196 | with f: |
| 197 | for line in f: |
| 198 | try: |
| 199 | fields = line.strip().split('\t') |
| 200 | if line.strip().startswith('#') or len(fields) != 7: |
| 201 | continue |
| 202 | domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6] |
| 203 | if xpath == '/' and key == 'o': |
Eric Boren | 0526335 | 2018-09-18 16:54:45 +0000 | [diff] [blame] | 204 | login, secret_token = value.split('=', 1) |
| 205 | gitcookies[domain] = (login, secret_token) |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 206 | except (IndexError, ValueError, TypeError) as exc: |
Andrii Shyshkalov | 5b04a57 | 2017-01-23 17:44:41 +0100 | [diff] [blame] | 207 | LOGGER.warning(exc) |
Eric Boren | 0526335 | 2018-09-18 16:54:45 +0000 | [diff] [blame] | 208 | |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 209 | return gitcookies |
| 210 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 211 | def _get_auth_for_host(self, host): |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 212 | for domain, creds in self.gitcookies.iteritems(): |
| 213 | if cookielib.domain_match(host, domain): |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 214 | return (creds[0], None, creds[1]) |
| 215 | return self.netrc.authenticators(host) |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 216 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 217 | def get_auth_header(self, host): |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 218 | a = self._get_auth_for_host(host) |
| 219 | if a: |
Eric Boren | 0526335 | 2018-09-18 16:54:45 +0000 | [diff] [blame] | 220 | return 'Basic %s' % (base64.b64encode('%s:%s' % (a[0], a[2]))) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 221 | return None |
| 222 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 223 | def get_auth_email(self, host): |
| 224 | """Best effort parsing of email to be used for auth for the given host.""" |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 225 | a = self._get_auth_for_host(host) |
| 226 | if not a: |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 227 | return None |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 228 | login = a[0] |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 229 | # login typically looks like 'git-xxx.example.com' |
| 230 | if not login.startswith('git-') or '.' not in login: |
| 231 | return None |
| 232 | username, domain = login[len('git-'):].split('.', 1) |
| 233 | return '%s@%s' % (username, domain) |
| 234 | |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 235 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 236 | # Backwards compatibility just in case somebody imports this outside of |
| 237 | # depot_tools. |
| 238 | NetrcAuthenticator = CookiesAuthenticator |
| 239 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 240 | |
| 241 | class GceAuthenticator(Authenticator): |
| 242 | """Authenticator implementation that uses GCE metadata service for token. |
| 243 | """ |
| 244 | |
| 245 | _INFO_URL = 'http://metadata.google.internal' |
smut | 5e9401b | 2017-08-10 15:22:20 -0700 | [diff] [blame] | 246 | _ACQUIRE_URL = ('%s/computeMetadata/v1/instance/' |
| 247 | 'service-accounts/default/token' % _INFO_URL) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 248 | _ACQUIRE_HEADERS = {"Metadata-Flavor": "Google"} |
| 249 | |
| 250 | _cache_is_gce = None |
| 251 | _token_cache = None |
| 252 | _token_expiration = None |
| 253 | |
| 254 | @classmethod |
| 255 | def is_gce(cls): |
Ravi Mistry | fad941b | 2016-11-15 13:00:47 -0500 | [diff] [blame] | 256 | if os.getenv('SKIP_GCE_AUTH_FOR_GIT'): |
| 257 | return False |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 258 | if cls._cache_is_gce is None: |
| 259 | cls._cache_is_gce = cls._test_is_gce() |
| 260 | return cls._cache_is_gce |
| 261 | |
| 262 | @classmethod |
| 263 | def _test_is_gce(cls): |
| 264 | # Based on https://cloud.google.com/compute/docs/metadata#runninggce |
| 265 | try: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 266 | resp, _ = cls._get(cls._INFO_URL) |
Sergio Villar Senin | 0d466d2 | 2018-03-23 14:31:48 +0100 | [diff] [blame] | 267 | except (socket.error, httplib2.ServerNotFoundError, |
| 268 | httplib2.socks.HTTPError): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 269 | # Could not resolve URL. |
| 270 | return False |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 271 | return resp.get('metadata-flavor') == 'Google' |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 272 | |
| 273 | @staticmethod |
| 274 | def _get(url, **kwargs): |
| 275 | next_delay_sec = 1 |
| 276 | for i in xrange(TRY_LIMIT): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 277 | p = urlparse.urlparse(url) |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 278 | c = GetConnectionObject(protocol=p.scheme) |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 279 | resp, contents = c.request(url, 'GET', **kwargs) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 280 | LOGGER.debug('GET [%s] #%d/%d (%d)', url, i+1, TRY_LIMIT, resp.status) |
| 281 | if resp.status < httplib.INTERNAL_SERVER_ERROR: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 282 | return (resp, contents) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 283 | |
Aaron Gable | 92e9f38 | 2017-12-07 11:47:41 -0800 | [diff] [blame] | 284 | # Retry server error status codes. |
| 285 | LOGGER.warn('Encountered server error') |
| 286 | if TRY_LIMIT - i > 1: |
| 287 | LOGGER.info('Will retry in %d seconds (%d more times)...', |
| 288 | next_delay_sec, TRY_LIMIT - i - 1) |
| 289 | time.sleep(next_delay_sec) |
| 290 | next_delay_sec *= 2 |
| 291 | |
| 292 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 293 | @classmethod |
| 294 | def _get_token_dict(cls): |
| 295 | if cls._token_cache: |
| 296 | # If it expires within 25 seconds, refresh. |
| 297 | if cls._token_expiration < time.time() - 25: |
| 298 | return cls._token_cache |
| 299 | |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 300 | resp, contents = cls._get(cls._ACQUIRE_URL, headers=cls._ACQUIRE_HEADERS) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 301 | if resp.status != httplib.OK: |
| 302 | return None |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 303 | cls._token_cache = json.loads(contents) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 304 | cls._token_expiration = cls._token_cache['expires_in'] + time.time() |
| 305 | return cls._token_cache |
| 306 | |
| 307 | def get_auth_header(self, _host): |
| 308 | token_dict = self._get_token_dict() |
| 309 | if not token_dict: |
| 310 | return None |
| 311 | return '%(token_type)s %(access_token)s' % token_dict |
| 312 | |
| 313 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 314 | class LuciContextAuthenticator(Authenticator): |
| 315 | """Authenticator implementation that uses LUCI_CONTEXT ambient local auth. |
| 316 | """ |
| 317 | |
| 318 | @staticmethod |
| 319 | def is_luci(): |
| 320 | return auth.has_luci_context_local_auth() |
| 321 | |
| 322 | def __init__(self): |
| 323 | self._access_token = None |
| 324 | self._ensure_fresh() |
| 325 | |
| 326 | def _ensure_fresh(self): |
| 327 | if not self._access_token or self._access_token.needs_refresh(): |
| 328 | self._access_token = auth.get_luci_context_access_token( |
| 329 | scopes=' '.join([auth.OAUTH_SCOPE_EMAIL, auth.OAUTH_SCOPE_GERRIT])) |
| 330 | |
| 331 | def get_auth_header(self, _host): |
| 332 | self._ensure_fresh() |
| 333 | return 'Bearer %s' % self._access_token.token |
| 334 | |
| 335 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 336 | def CreateHttpConn(host, path, reqtype='GET', headers=None, body=None): |
| 337 | """Opens an https connection to a gerrit service, and sends a request.""" |
| 338 | headers = headers or {} |
| 339 | bare_host = host.partition(':')[0] |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 340 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 341 | a = Authenticator.get().get_auth_header(bare_host) |
| 342 | if a: |
| 343 | headers.setdefault('Authorization', a) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 344 | else: |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 345 | LOGGER.debug('No authorization found for %s.' % bare_host) |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 346 | |
Dan Jacques | 6d5bcc2 | 2016-11-14 15:32:04 -0800 | [diff] [blame] | 347 | url = path |
| 348 | if not url.startswith('/'): |
| 349 | url = '/' + url |
| 350 | if 'Authorization' in headers and not url.startswith('/a/'): |
| 351 | url = '/a%s' % url |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 352 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 353 | if body: |
| 354 | body = json.JSONEncoder().encode(body) |
| 355 | headers.setdefault('Content-Type', 'application/json') |
| 356 | if LOGGER.isEnabledFor(logging.DEBUG): |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 357 | LOGGER.debug('%s %s://%s%s' % (reqtype, GERRIT_PROTOCOL, host, url)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 358 | for key, val in headers.iteritems(): |
| 359 | if key == 'Authorization': |
| 360 | val = 'HIDDEN' |
| 361 | LOGGER.debug('%s: %s' % (key, val)) |
| 362 | if body: |
| 363 | LOGGER.debug(body) |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 364 | conn = GetConnectionObject() |
Andrii Shyshkalov | 86c823e | 2018-09-18 19:51:33 +0000 | [diff] [blame] | 365 | # HACK: httplib.Http has no such attribute; we store req_host here for later |
| 366 | # use in ReadHttpResponse. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 367 | conn.req_host = host |
| 368 | conn.req_params = { |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 369 | 'uri': urlparse.urljoin('%s://%s' % (GERRIT_PROTOCOL, host), url), |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 370 | 'method': reqtype, |
| 371 | 'headers': headers, |
| 372 | 'body': body, |
| 373 | } |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 374 | return conn |
| 375 | |
| 376 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 377 | def ReadHttpResponse(conn, accept_statuses=frozenset([200])): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 378 | """Reads an http response from a connection into a string buffer. |
| 379 | |
| 380 | Args: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 381 | conn: An Http object created by CreateHttpConn above. |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 382 | accept_statuses: Treat any of these statuses as success. Default: [200] |
| 383 | Common additions include 204, 400, and 404. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 384 | Returns: A string buffer containing the connection's reply. |
| 385 | """ |
Steve Kobes | 5611772 | 2018-09-13 18:18:35 +0000 | [diff] [blame] | 386 | sleep_time = 1.5 |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 387 | for idx in range(TRY_LIMIT): |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 388 | response, contents = conn.request(**conn.req_params) |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 389 | |
| 390 | # Check if this is an authentication issue. |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 391 | www_authenticate = response.get('www-authenticate') |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 392 | if (response.status in (httplib.UNAUTHORIZED, httplib.FOUND) and |
| 393 | www_authenticate): |
| 394 | auth_match = re.search('realm="([^"]+)"', www_authenticate, re.I) |
| 395 | host = auth_match.group(1) if auth_match else conn.req_host |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 396 | reason = ('Authentication failed. Please make sure your .gitcookies file ' |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 397 | 'has credentials for %s' % host) |
| 398 | raise GerritAuthenticationError(response.status, reason) |
| 399 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 400 | # If response.status < 500 then the result is final; break retry loop. |
Michael Moss | 120b2e4 | 2018-06-21 23:53:42 +0000 | [diff] [blame] | 401 | # If the response is 404/409, it might be because of replication lag, so |
Aaron Gable | 62ca960 | 2017-05-19 17:24:52 -0700 | [diff] [blame] | 402 | # keep trying anyway. |
Michael Moss | 120b2e4 | 2018-06-21 23:53:42 +0000 | [diff] [blame] | 403 | if ((response.status < 500 and response.status not in [404, 409]) |
Michael Moss | b40a451 | 2017-10-10 11:07:17 -0700 | [diff] [blame] | 404 | or response.status in accept_statuses): |
Andrii Shyshkalov | 5b04a57 | 2017-01-23 17:44:41 +0100 | [diff] [blame] | 405 | LOGGER.debug('got response %d for %s %s', response.status, |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 406 | conn.req_params['method'], conn.req_params['uri']) |
Michael Moss | b40a451 | 2017-10-10 11:07:17 -0700 | [diff] [blame] | 407 | # If 404 was in accept_statuses, then it's expected that the file might |
| 408 | # not exist, so don't return the gitiles error page because that's not the |
| 409 | # "content" that was actually requested. |
| 410 | if response.status == 404: |
| 411 | contents = '' |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 412 | break |
| 413 | # A status >=500 is assumed to be a possible transient error; retry. |
| 414 | http_version = 'HTTP/%s' % ('1.1' if response.version == 11 else '1.0') |
Andy Perelson | a06cd09 | 2018-09-24 21:29:57 +0000 | [diff] [blame] | 415 | LOGGER.warn('A transient error occurred while querying %s:\n' |
| 416 | '%s %s %s\n' |
| 417 | '%s %d %s', |
| 418 | conn.req_host, conn.req_params['method'], |
| 419 | conn.req_params['uri'], |
| 420 | http_version, http_version, response.status, response.reason) |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame^] | 421 | if response.status == 404: |
| 422 | # TODO(crbug/881860): remove this hack. |
| 423 | # HACK: try different Gerrit mirror as a workaround for potentially |
| 424 | # out-of-date mirror hit through default routing. |
| 425 | if conn.req_host == 'chromium-review.googlesource.com': |
| 426 | conn.req_params['uri'] = _UseGerritMirror( |
| 427 | conn.req_params['uri'], 'chromium-review.googlesource.com') |
| 428 | # And don't increase sleep_time in this case, since we suspect we've |
| 429 | # just asked wrong git mirror before. |
| 430 | sleep_time /= 2.0 |
| 431 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 432 | if TRY_LIMIT - idx > 1: |
Aaron Gable | 92e9f38 | 2017-12-07 11:47:41 -0800 | [diff] [blame] | 433 | LOGGER.info('Will retry in %d seconds (%d more times)...', |
| 434 | sleep_time, TRY_LIMIT - idx - 1) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 435 | time.sleep(sleep_time) |
| 436 | sleep_time = sleep_time * 2 |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 437 | if response.status not in accept_statuses: |
Andrii Shyshkalov | 4956f79 | 2017-04-10 14:28:38 +0200 | [diff] [blame] | 438 | if response.status in (401, 403): |
| 439 | print('Your Gerrit credentials might be misconfigured. Try: \n' |
| 440 | ' git cl creds-check') |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 441 | reason = '%s: %s' % (response.reason, contents) |
nodir@chromium.org | a779803 | 2014-04-30 23:40:53 +0000 | [diff] [blame] | 442 | raise GerritError(response.status, reason) |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 443 | return StringIO(contents) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 444 | |
| 445 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 446 | def ReadHttpJsonResponse(conn, accept_statuses=frozenset([200])): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 447 | """Parses an https response as json.""" |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 448 | fh = ReadHttpResponse(conn, accept_statuses) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 449 | # The first line of the response should always be: )]}' |
| 450 | s = fh.readline() |
| 451 | if s and s.rstrip() != ")]}'": |
| 452 | raise GerritError(200, 'Unexpected json output: %s' % s) |
| 453 | s = fh.read() |
| 454 | if not s: |
| 455 | return None |
| 456 | return json.loads(s) |
| 457 | |
| 458 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 459 | def QueryChanges(host, params, first_param=None, limit=None, o_params=None, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 460 | start=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 461 | """ |
| 462 | Queries a gerrit-on-borg server for changes matching query terms. |
| 463 | |
| 464 | Args: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 465 | params: A list of key:value pairs for search parameters, as documented |
| 466 | here (e.g. ('is', 'owner') for a parameter 'is:owner'): |
| 467 | https://gerrit-review.googlesource.com/Documentation/user-search.html#search-operators |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 468 | first_param: A change identifier |
| 469 | limit: Maximum number of results to return. |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 470 | start: how many changes to skip (starting with the most recent) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 471 | o_params: A list of additional output specifiers, as documented here: |
| 472 | https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes |
| 473 | Returns: |
| 474 | A list of json-decoded query results. |
| 475 | """ |
| 476 | # Note that no attempt is made to escape special characters; YMMV. |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 477 | if not params and not first_param: |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 478 | raise RuntimeError('QueryChanges requires search parameters') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 479 | path = 'changes/?q=%s' % _QueryString(params, first_param) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 480 | if start: |
| 481 | path = '%s&start=%s' % (path, start) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 482 | if limit: |
| 483 | path = '%s&n=%d' % (path, limit) |
| 484 | if o_params: |
| 485 | path = '%s&%s' % (path, '&'.join(['o=%s' % p for p in o_params])) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 486 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 487 | |
| 488 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 489 | def GenerateAllChanges(host, params, first_param=None, limit=500, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 490 | o_params=None, start=None): |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 491 | """ |
| 492 | Queries a gerrit-on-borg server for all the changes matching the query terms. |
| 493 | |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 494 | WARNING: this is unreliable if a change matching the query is modified while |
| 495 | this function is being called. |
| 496 | |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 497 | A single query to gerrit-on-borg is limited on the number of results by the |
| 498 | limit parameter on the request (see QueryChanges) and the server maximum |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 499 | limit. |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 500 | |
| 501 | Args: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 502 | params, first_param: Refer to QueryChanges(). |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 503 | limit: Maximum number of requested changes per query. |
| 504 | o_params: Refer to QueryChanges(). |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 505 | start: Refer to QueryChanges(). |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 506 | |
| 507 | Returns: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 508 | A generator object to the list of returned changes. |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 509 | """ |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 510 | already_returned = set() |
| 511 | def at_most_once(cls): |
| 512 | for cl in cls: |
| 513 | if cl['_number'] not in already_returned: |
| 514 | already_returned.add(cl['_number']) |
| 515 | yield cl |
| 516 | |
| 517 | start = start or 0 |
| 518 | cur_start = start |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 519 | more_changes = True |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 520 | |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 521 | while more_changes: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 522 | # This will fetch changes[start..start+limit] sorted by most recently |
| 523 | # updated. Since the rank of any change in this list can be changed any time |
| 524 | # (say user posting comment), subsequent calls may overalp like this: |
| 525 | # > initial order ABCDEFGH |
| 526 | # query[0..3] => ABC |
| 527 | # > E get's updated. New order: EABCDFGH |
| 528 | # query[3..6] => CDF # C is a dup |
| 529 | # query[6..9] => GH # E is missed. |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 530 | page = QueryChanges(host, params, first_param, limit, o_params, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 531 | cur_start) |
| 532 | for cl in at_most_once(page): |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 533 | yield cl |
| 534 | |
| 535 | more_changes = [cl for cl in page if '_more_changes' in cl] |
| 536 | if len(more_changes) > 1: |
| 537 | raise GerritError( |
| 538 | 200, |
| 539 | 'Received %d changes with a _more_changes attribute set but should ' |
| 540 | 'receive at most one.' % len(more_changes)) |
| 541 | if more_changes: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 542 | cur_start += len(page) |
| 543 | |
| 544 | # If we paged through, query again the first page which in most circumstances |
| 545 | # will fetch all changes that were modified while this function was run. |
| 546 | if start != cur_start: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 547 | page = QueryChanges(host, params, first_param, limit, o_params, start) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 548 | for cl in at_most_once(page): |
| 549 | yield cl |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 550 | |
| 551 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 552 | def MultiQueryChanges(host, params, change_list, limit=None, o_params=None, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 553 | start=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 554 | """Initiate a query composed of multiple sets of query parameters.""" |
| 555 | if not change_list: |
| 556 | raise RuntimeError( |
| 557 | "MultiQueryChanges requires a list of change numbers/id's") |
| 558 | q = ['q=%s' % '+OR+'.join([urllib.quote(str(x)) for x in change_list])] |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 559 | if params: |
| 560 | q.append(_QueryString(params)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 561 | if limit: |
| 562 | q.append('n=%d' % limit) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 563 | if start: |
| 564 | q.append('S=%s' % start) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 565 | if o_params: |
| 566 | q.extend(['o=%s' % p for p in o_params]) |
| 567 | path = 'changes/?%s' % '&'.join(q) |
| 568 | try: |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 569 | result = ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 570 | except GerritError as e: |
| 571 | msg = '%s:\n%s' % (e.message, path) |
| 572 | raise GerritError(e.http_status, msg) |
| 573 | return result |
| 574 | |
| 575 | |
| 576 | def GetGerritFetchUrl(host): |
| 577 | """Given a gerrit host name returns URL of a gerrit instance to fetch from.""" |
| 578 | return '%s://%s/' % (GERRIT_PROTOCOL, host) |
| 579 | |
| 580 | |
| 581 | def GetChangePageUrl(host, change_number): |
| 582 | """Given a gerrit host name and change number, return change page url.""" |
| 583 | return '%s://%s/#/c/%d/' % (GERRIT_PROTOCOL, host, change_number) |
| 584 | |
| 585 | |
| 586 | def GetChangeUrl(host, change): |
| 587 | """Given a gerrit host name and change id, return an url for the change.""" |
| 588 | return '%s://%s/a/changes/%s' % (GERRIT_PROTOCOL, host, change) |
| 589 | |
| 590 | |
| 591 | def GetChange(host, change): |
| 592 | """Query a gerrit server for information about a single change.""" |
| 593 | path = 'changes/%s' % change |
| 594 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 595 | |
| 596 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 597 | def GetChangeDetail(host, change, o_params=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 598 | """Query a gerrit server for extended information about a single change.""" |
| 599 | path = 'changes/%s/detail' % change |
| 600 | if o_params: |
| 601 | path += '?%s' % '&'.join(['o=%s' % p for p in o_params]) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 602 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 603 | |
| 604 | |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 605 | def GetChangeCommit(host, change, revision='current'): |
| 606 | """Query a gerrit server for a revision associated with a change.""" |
| 607 | path = 'changes/%s/revisions/%s/commit?links' % (change, revision) |
| 608 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 609 | |
| 610 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 611 | def GetChangeCurrentRevision(host, change): |
| 612 | """Get information about the latest revision for a given change.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 613 | return QueryChanges(host, [], change, o_params=('CURRENT_REVISION',)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 614 | |
| 615 | |
| 616 | def GetChangeRevisions(host, change): |
| 617 | """Get information about all revisions associated with a change.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 618 | return QueryChanges(host, [], change, o_params=('ALL_REVISIONS',)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 619 | |
| 620 | |
| 621 | def GetChangeReview(host, change, revision=None): |
| 622 | """Get the current review information for a change.""" |
| 623 | if not revision: |
| 624 | jmsg = GetChangeRevisions(host, change) |
| 625 | if not jmsg: |
| 626 | return None |
| 627 | elif len(jmsg) > 1: |
| 628 | raise GerritError(200, 'Multiple changes found for ChangeId %s.' % change) |
| 629 | revision = jmsg[0]['current_revision'] |
| 630 | path = 'changes/%s/revisions/%s/review' |
| 631 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 632 | |
| 633 | |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 634 | def GetChangeComments(host, change): |
| 635 | """Get the line- and file-level comments on a change.""" |
| 636 | path = 'changes/%s/comments' % change |
| 637 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 638 | |
| 639 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 640 | def AbandonChange(host, change, msg=''): |
| 641 | """Abandon a gerrit change.""" |
| 642 | path = 'changes/%s/abandon' % change |
tandrii@chromium.org | c7da66a | 2016-03-24 09:52:24 +0000 | [diff] [blame] | 643 | body = {'message': msg} if msg else {} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 644 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 645 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 646 | |
| 647 | |
| 648 | def RestoreChange(host, change, msg=''): |
| 649 | """Restore a previously abandoned change.""" |
| 650 | path = 'changes/%s/restore' % change |
tandrii@chromium.org | c7da66a | 2016-03-24 09:52:24 +0000 | [diff] [blame] | 651 | body = {'message': msg} if msg else {} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 652 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 653 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 654 | |
| 655 | |
| 656 | def SubmitChange(host, change, wait_for_merge=True): |
| 657 | """Submits a gerrit change via Gerrit.""" |
| 658 | path = 'changes/%s/submit' % change |
| 659 | body = {'wait_for_merge': wait_for_merge} |
| 660 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 661 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 662 | |
| 663 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 664 | def HasPendingChangeEdit(host, change): |
| 665 | conn = CreateHttpConn(host, 'changes/%s/edit' % change) |
| 666 | try: |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 667 | ReadHttpResponse(conn) |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 668 | except GerritError as e: |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 669 | # 204 No Content means no pending change. |
| 670 | if e.http_status == 204: |
| 671 | return False |
| 672 | raise |
| 673 | return True |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 674 | |
| 675 | |
| 676 | def DeletePendingChangeEdit(host, change): |
| 677 | conn = CreateHttpConn(host, 'changes/%s/edit' % change, reqtype='DELETE') |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 678 | # On success, gerrit returns status 204; if the edit was already deleted it |
| 679 | # returns 404. Anything else is an error. |
| 680 | ReadHttpResponse(conn, accept_statuses=[204, 404]) |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 681 | |
| 682 | |
Andrii Shyshkalov | ea4fc83 | 2016-12-01 14:53:23 +0100 | [diff] [blame] | 683 | def SetCommitMessage(host, change, description, notify='ALL'): |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 684 | """Updates a commit message.""" |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 685 | assert notify in ('ALL', 'NONE') |
| 686 | path = 'changes/%s/message' % change |
Aaron Gable | 5a4ef45 | 2017-08-24 13:19:56 -0700 | [diff] [blame] | 687 | body = {'message': description, 'notify': notify} |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 688 | conn = CreateHttpConn(host, path, reqtype='PUT', body=body) |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 689 | try: |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 690 | ReadHttpResponse(conn, accept_statuses=[200, 204]) |
| 691 | except GerritError as e: |
| 692 | raise GerritError( |
| 693 | e.http_status, |
| 694 | 'Received unexpected http status while editing message ' |
| 695 | 'in change %s' % change) |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 696 | |
| 697 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 698 | def GetReviewers(host, change): |
| 699 | """Get information about all reviewers attached to a change.""" |
| 700 | path = 'changes/%s/reviewers' % change |
| 701 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 702 | |
| 703 | |
| 704 | def GetReview(host, change, revision): |
| 705 | """Get review information about a specific revision of a change.""" |
| 706 | path = 'changes/%s/revisions/%s/review' % (change, revision) |
| 707 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 708 | |
| 709 | |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 710 | def AddReviewers(host, change, reviewers=None, ccs=None, notify=True, |
| 711 | accept_statuses=frozenset([200, 400, 422])): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 712 | """Add reviewers to a change.""" |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 713 | if not reviewers and not ccs: |
Aaron Gable | df86e30 | 2016-11-08 10:48:03 -0800 | [diff] [blame] | 714 | return None |
Wiktor Garbacz | 6d0d044 | 2017-05-15 12:34:40 +0200 | [diff] [blame] | 715 | if not change: |
| 716 | return None |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 717 | reviewers = frozenset(reviewers or []) |
| 718 | ccs = frozenset(ccs or []) |
| 719 | path = 'changes/%s/revisions/current/review' % change |
| 720 | |
| 721 | body = { |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 722 | 'drafts': 'KEEP', |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 723 | 'reviewers': [], |
| 724 | 'notify': 'ALL' if notify else 'NONE', |
| 725 | } |
| 726 | for r in sorted(reviewers | ccs): |
| 727 | state = 'REVIEWER' if r in reviewers else 'CC' |
| 728 | body['reviewers'].append({ |
| 729 | 'reviewer': r, |
| 730 | 'state': state, |
| 731 | 'notify': 'NONE', # We handled `notify` argument above. |
| 732 | }) |
| 733 | |
| 734 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
| 735 | # Gerrit will return 400 if one or more of the requested reviewers are |
| 736 | # unprocessable. We read the response object to see which were rejected, |
| 737 | # warn about them, and retry with the remainder. |
| 738 | resp = ReadHttpJsonResponse(conn, accept_statuses=accept_statuses) |
| 739 | |
| 740 | errored = set() |
| 741 | for result in resp.get('reviewers', {}).itervalues(): |
| 742 | r = result.get('input') |
| 743 | state = 'REVIEWER' if r in reviewers else 'CC' |
| 744 | if result.get('error'): |
| 745 | errored.add(r) |
| 746 | LOGGER.warn('Note: "%s" not added as a %s' % (r, state.lower())) |
| 747 | if errored: |
| 748 | # Try again, adding only those that didn't fail, and only accepting 200. |
| 749 | AddReviewers(host, change, reviewers=(reviewers-errored), |
| 750 | ccs=(ccs-errored), notify=notify, accept_statuses=[200]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 751 | |
| 752 | |
| 753 | def RemoveReviewers(host, change, remove=None): |
| 754 | """Remove reveiewers from a change.""" |
| 755 | if not remove: |
| 756 | return |
| 757 | if isinstance(remove, basestring): |
| 758 | remove = (remove,) |
| 759 | for r in remove: |
| 760 | path = 'changes/%s/reviewers/%s' % (change, r) |
| 761 | conn = CreateHttpConn(host, path, reqtype='DELETE') |
| 762 | try: |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 763 | ReadHttpResponse(conn, accept_statuses=[204]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 764 | except GerritError as e: |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 765 | raise GerritError( |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 766 | e.http_status, |
| 767 | 'Received unexpected http status while deleting reviewer "%s" ' |
| 768 | 'from change %s' % (r, change)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 769 | |
| 770 | |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 771 | def SetReview(host, change, msg=None, labels=None, notify=None, ready=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 772 | """Set labels and/or add a message to a code review.""" |
| 773 | if not msg and not labels: |
| 774 | return |
| 775 | path = 'changes/%s/revisions/current/review' % change |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 776 | body = {'drafts': 'KEEP'} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 777 | if msg: |
| 778 | body['message'] = msg |
| 779 | if labels: |
| 780 | body['labels'] = labels |
Aaron Gable | fc62f76 | 2017-07-17 11:12:07 -0700 | [diff] [blame] | 781 | if notify is not None: |
Aaron Gable | 75e7872 | 2017-06-09 10:40:16 -0700 | [diff] [blame] | 782 | body['notify'] = 'ALL' if notify else 'NONE' |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 783 | if ready: |
| 784 | body['ready'] = True |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 785 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
| 786 | response = ReadHttpJsonResponse(conn) |
| 787 | if labels: |
| 788 | for key, val in labels.iteritems(): |
| 789 | if ('labels' not in response or key not in response['labels'] or |
| 790 | int(response['labels'][key] != int(val))): |
| 791 | raise GerritError(200, 'Unable to set "%s" label on change %s.' % ( |
| 792 | key, change)) |
| 793 | |
| 794 | |
| 795 | def ResetReviewLabels(host, change, label, value='0', message=None, |
| 796 | notify=None): |
| 797 | """Reset the value of a given label for all reviewers on a change.""" |
| 798 | # This is tricky, because we want to work on the "current revision", but |
| 799 | # there's always the risk that "current revision" will change in between |
| 800 | # API calls. So, we check "current revision" at the beginning and end; if |
| 801 | # it has changed, raise an exception. |
| 802 | jmsg = GetChangeCurrentRevision(host, change) |
| 803 | if not jmsg: |
| 804 | raise GerritError( |
| 805 | 200, 'Could not get review information for change "%s"' % change) |
| 806 | value = str(value) |
| 807 | revision = jmsg[0]['current_revision'] |
| 808 | path = 'changes/%s/revisions/%s/review' % (change, revision) |
| 809 | message = message or ( |
| 810 | '%s label set to %s programmatically.' % (label, value)) |
| 811 | jmsg = GetReview(host, change, revision) |
| 812 | if not jmsg: |
| 813 | raise GerritError(200, 'Could not get review information for revison %s ' |
| 814 | 'of change %s' % (revision, change)) |
| 815 | for review in jmsg.get('labels', {}).get(label, {}).get('all', []): |
| 816 | if str(review.get('value', value)) != value: |
| 817 | body = { |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 818 | 'drafts': 'KEEP', |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 819 | 'message': message, |
| 820 | 'labels': {label: value}, |
| 821 | 'on_behalf_of': review['_account_id'], |
| 822 | } |
| 823 | if notify: |
| 824 | body['notify'] = notify |
| 825 | conn = CreateHttpConn( |
| 826 | host, path, reqtype='POST', body=body) |
| 827 | response = ReadHttpJsonResponse(conn) |
| 828 | if str(response['labels'][label]) != value: |
| 829 | username = review.get('email', jmsg.get('name', '')) |
| 830 | raise GerritError(200, 'Unable to set %s label for user "%s"' |
| 831 | ' on change %s.' % (label, username, change)) |
| 832 | jmsg = GetChangeCurrentRevision(host, change) |
| 833 | if not jmsg: |
| 834 | raise GerritError( |
| 835 | 200, 'Could not get review information for change "%s"' % change) |
| 836 | elif jmsg[0]['current_revision'] != revision: |
| 837 | raise GerritError(200, 'While resetting labels on change "%s", ' |
| 838 | 'a new patchset was uploaded.' % change) |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 839 | |
| 840 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 841 | def CreateGerritBranch(host, project, branch, commit): |
| 842 | """ |
| 843 | Create a new branch from given project and commit |
| 844 | https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-branch |
| 845 | |
| 846 | Returns: |
| 847 | A JSON with 'ref' key |
| 848 | """ |
| 849 | path = 'projects/%s/branches/%s' % (project, branch) |
| 850 | body = {'revision': commit} |
| 851 | conn = CreateHttpConn(host, path, reqtype='PUT', body=body) |
dimu | 7d1af2b | 2017-04-19 16:01:17 -0700 | [diff] [blame] | 852 | response = ReadHttpJsonResponse(conn, accept_statuses=[201]) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 853 | if response: |
| 854 | return response |
| 855 | raise GerritError(200, 'Unable to create gerrit branch') |
| 856 | |
| 857 | |
| 858 | def GetGerritBranch(host, project, branch): |
| 859 | """ |
| 860 | Get a branch from given project and commit |
| 861 | https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-branch |
| 862 | |
| 863 | Returns: |
| 864 | A JSON object with 'revision' key |
| 865 | """ |
| 866 | path = 'projects/%s/branches/%s' % (project, branch) |
| 867 | conn = CreateHttpConn(host, path, reqtype='GET') |
| 868 | response = ReadHttpJsonResponse(conn) |
| 869 | if response: |
| 870 | return response |
| 871 | raise GerritError(200, 'Unable to get gerrit branch') |
| 872 | |
| 873 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 874 | def GetAccountDetails(host, account_id='self'): |
| 875 | """Returns details of the account. |
| 876 | |
| 877 | If account_id is not given, uses magic value 'self' which corresponds to |
| 878 | whichever account user is authenticating as. |
| 879 | |
| 880 | Documentation: |
| 881 | https://gerrit-review.googlesource.com/Documentation/rest-api-accounts.html#get-account |
| 882 | """ |
| 883 | if account_id != 'self': |
| 884 | account_id = int(account_id) |
| 885 | conn = CreateHttpConn(host, '/accounts/%s' % account_id) |
| 886 | return ReadHttpJsonResponse(conn) |
| 887 | |
| 888 | |
Nick Carter | 8692b18 | 2017-11-06 16:30:38 -0800 | [diff] [blame] | 889 | def PercentEncodeForGitRef(original): |
| 890 | """Apply percent-encoding for strings sent to gerrit via git ref metadata. |
| 891 | |
| 892 | The encoding used is based on but stricter than URL encoding (Section 2.1 |
| 893 | of RFC 3986). The only non-escaped characters are alphanumerics, and |
| 894 | 'SPACE' (U+0020) can be represented as 'LOW LINE' (U+005F) or |
| 895 | 'PLUS SIGN' (U+002B). |
| 896 | |
| 897 | For more information, see the Gerrit docs here: |
| 898 | |
| 899 | https://gerrit-review.googlesource.com/Documentation/user-upload.html#message |
| 900 | """ |
| 901 | safe = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ' |
| 902 | encoded = ''.join(c if c in safe else '%%%02X' % ord(c) for c in original) |
| 903 | |
| 904 | # spaces are not allowed in git refs; gerrit will interpret either '_' or |
| 905 | # '+' (or '%20') as space. Use '_' since that has been supported the longest. |
| 906 | return encoded.replace(' ', '_') |
| 907 | |
| 908 | |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 909 | @contextlib.contextmanager |
| 910 | def tempdir(): |
| 911 | tdir = None |
| 912 | try: |
| 913 | tdir = tempfile.mkdtemp(suffix='gerrit_util') |
| 914 | yield tdir |
| 915 | finally: |
| 916 | if tdir: |
| 917 | gclient_utils.rmtree(tdir) |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 918 | |
| 919 | |
| 920 | def ChangeIdentifier(project, change_number): |
| 921 | """Returns change identifier "project~number" suitable for |chagne| arg of |
| 922 | this module API. |
| 923 | |
| 924 | Such format is allows for more efficient Gerrit routing of HTTP requests, |
| 925 | comparing to specifying just change_number. |
| 926 | """ |
| 927 | assert int(change_number) |
| 928 | return '%s~%s' % (urllib.quote(project, safe=''), change_number) |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame^] | 929 | |
| 930 | |
| 931 | # TODO(crbug/881860): remove this hack. |
| 932 | _GERRIT_MIRROR_PREFIXES = ['us1', 'us2', 'us3'] |
| 933 | assert all(3 == len(p) for p in _GERRIT_MIRROR_PREFIXES) |
| 934 | |
| 935 | |
| 936 | def _UseGerritMirror(url, host): |
| 937 | """Returns new url which uses randomly selected mirror for a gerrit host. |
| 938 | |
| 939 | url's host should be for a given host or a result of prior call to this |
| 940 | function. |
| 941 | |
| 942 | Assumes url has a single occurence of the host substring. |
| 943 | """ |
| 944 | assert host in url |
| 945 | suffix = '-mirror-' + host |
| 946 | prefixes = set(_GERRIT_MIRROR_PREFIXES) |
| 947 | prefix_len = len(_GERRIT_MIRROR_PREFIXES[0]) |
| 948 | st = url.find(suffix) |
| 949 | if st == -1: |
| 950 | actual_host = host |
| 951 | else: |
| 952 | # Already uses some mirror. |
| 953 | assert st >= prefix_len, (uri, host, st, prefix_len) |
| 954 | prefixes.remove(url[st-prefix_len:st]) |
| 955 | actual_host = url[st-prefix_len:st+len(suffix)] |
| 956 | return url.replace(actual_host, random.choice(list(prefixes)) + suffix) |