-
Notifications
You must be signed in to change notification settings - Fork 0
/
gerrit_util.py
1809 lines (1482 loc) · 64.9 KB
/
gerrit_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Utilities for requesting information for a Gerrit server via HTTPS.
https://gerrit-review.googlesource.com/Documentation/rest-api.html
"""
from __future__ import annotations
import base64
import contextlib
import http.cookiejar
import json
import logging
import os
import random
import re
import shutil
import socket
import subprocess
import sys
import tempfile
import threading
import time
import urllib.parse
from dataclasses import dataclass
from io import StringIO
from multiprocessing.pool import ThreadPool
from typing import Any, Container, Dict, List, Optional
from typing import Tuple, TypedDict, cast
import httplib2
import httplib2.socks
import auth
import gclient_utils
import metrics
import metrics_utils
import newauth
import scm
import subprocess2
# HACK: httplib2 has significant bugs with its proxy support in
# python3. All httplib2 code should be rewritten to just use python
# stdlib which does not have these bugs.
#
# Prior to that, however, we will directly patch the buggy
# implementation of httplib2.socks.socksocket.__rewriteproxy which does
# not properly expect bytes as its argument instead of str.
#
# Note that __rewriteproxy is inherently buggy, as it relies on the
# python stdlib client to send the entire request header in a single
# call to socket.sendall, which is not explicitly guaranteed.
#
# Changes:
# * all string literals changed to bytes literals.
# * all __symbols changed to _socksocket__symbols.
# * Type annotations added to function signature.
def __fixed_rewrite_proxy(self: httplib2.socks.socksocket, header: bytes):
""" rewrite HTTP request headers to support non-tunneling proxies
(i.e. those which do not support the CONNECT method).
This only works for HTTP (not HTTPS) since HTTPS requires tunneling.
"""
host, endpt = None, None
hdrs = header.split(b"\r\n")
for hdr in hdrs:
if hdr.lower().startswith(b"host:"):
host = hdr
elif hdr.lower().startswith(b"get") or hdr.lower().startswith(b"post"):
endpt = hdr
if host and endpt:
hdrs.remove(host)
hdrs.remove(endpt)
host = host.split(b" ")[1]
endpt = endpt.split(b" ")
if self._socksocket__proxy[4] != None \
and self._socksocket__proxy[5] != None:
hdrs.insert(0, self._socksocket__getauthheader())
hdrs.insert(0, b"Host: %s" % host)
hdrs.insert(0,
b"%s http://%s%s %s" % (endpt[0], host, endpt[1], endpt[2]))
return b"\r\n".join(hdrs)
httplib2.socks.socksocket._socksocket__rewriteproxy = __fixed_rewrite_proxy
# TODO: Should fix these warnings.
# pylint: disable=line-too-long
LOGGER = logging.getLogger()
# With a starting sleep time of 12.0 seconds, x <= [1.8-2.2]x backoff, and six
# total tries, the sleep time between the first and last tries will be ~6 min
# (excluding time for each try).
TRY_LIMIT = 6
SLEEP_TIME = 12.0
MAX_BACKOFF = 2.2
MIN_BACKOFF = 1.8
# Controls the transport protocol used to communicate with Gerrit.
# This is parameterized primarily to enable GerritTestCase.
GERRIT_PROTOCOL = 'https'
# Controls how many concurrent Gerrit connections there can be.
MAX_CONCURRENT_CONNECTION = 20
def time_sleep(seconds):
# Use this so that it can be mocked in tests without interfering with python
# system machinery.
return time.sleep(seconds)
def time_time():
# Use this so that it can be mocked in tests without interfering with python
# system machinery.
return time.time()
def log_retry_and_sleep(seconds, attempt):
LOGGER.info('Will retry in %d seconds (%d more times)...', seconds,
TRY_LIMIT - attempt - 1)
time_sleep(seconds)
return seconds * random.uniform(MIN_BACKOFF, MAX_BACKOFF)
class GerritError(Exception):
"""Exception class for errors commuicating with the gerrit-on-borg service."""
def __init__(self, http_status, message, *args, **kwargs):
super(GerritError, self).__init__(*args, **kwargs)
self.http_status = http_status
self.message = '(%d) %s' % (self.http_status, message)
def __str__(self):
return self.message
def _QueryString(params, first_param=None):
"""Encodes query parameters in the key:val[+key:val...] format specified here:
https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes
"""
q = [urllib.parse.quote(first_param)] if first_param else []
q.extend(['%s:%s' % (key, val.replace(" ", "+")) for key, val in params])
return '+'.join(q)
class SSOHelper(object):
"""SSOHelper finds a Google-internal SSO helper."""
_sso_cmd: Optional[str] = None
def find_cmd(self) -> str:
"""Returns the cached command-line to invoke git-remote-sso.
If git-remote-sso is not in $PATH, returns None.
"""
if self._sso_cmd is not None:
return self._sso_cmd
cmd = shutil.which('git-remote-sso')
if cmd is None:
cmd = ''
self._sso_cmd = cmd
return cmd
# Global instance
ssoHelper = SSOHelper()
def ShouldUseSSO(host: str) -> bool:
"""Return True if we should use SSO for the current user."""
LOGGER.debug("Determining whether we should use SSO...")
if not newauth.Enabled():
LOGGER.debug("SSO=False: not opted in")
return False
if newauth.SkipSSO():
LOGGER.debug("SSO=False: set skip SSO config")
return False
if not ssoHelper.find_cmd():
LOGGER.debug("SSO=False: no SSO command")
return False
cwd = os.getcwd()
email = scm.GIT.GetConfig(cwd, 'user.email', default='')
if email.endswith('@google.com'):
LOGGER.debug("SSO=True: email is google.com")
return True
if not email.endswith('@chromium.org'):
LOGGER.debug("SSO=False: not chromium.org")
return False
authenticator = SSOAuthenticator()
records = GetAccountEmails(host, 'self', authenticator=authenticator)
if any(email == r['email'] for r in records):
LOGGER.debug("SSO=True: email is linked to google.com")
return True
LOGGER.debug("SSO=False: unlinked chromium.org")
return False
class Authenticator(object):
"""Base authenticator class for authenticator implementations to subclass."""
# Cached Authenticator subclass instance, resolved via get().
_resolved: Optional[Authenticator] = None
_resolved_lock = threading.Lock()
def authenticate(self, conn: HttpConn):
"""Adds authentication information to the HttpConn."""
raise NotImplementedError()
def debug_summary_state(self) -> str:
"""If this Authenticator has any debugging information about its state,
_WriteGitPushTraces will call this to include in the git push traces.
Return value is any relevant debugging information with all PII/secrets
redacted.
"""
raise NotImplementedError()
@classmethod
def is_applicable(cls, *, conn: Optional[HttpConn] = None) -> bool:
"""Must return True if this Authenticator is available in the current
environment."""
raise NotImplementedError()
def ensure_authenticated(self, gerrit_host: str, git_host: str) -> Tuple[bool, str]:
"""Returns (bypassable, error message).
If the error message is empty, there is no error to report.
If bypassable is true, the caller will allow the user to continue past the
error.
"""
return (True, '')
@classmethod
def get(cls):
"""Returns: (Authenticator) The identified Authenticator to use.
Probes the local system and its environment and identifies the
Authenticator instance to use.
The resolved Authenticator instance is cached as a class variable.
"""
with cls._resolved_lock:
if ret := cls._resolved:
return ret
use_new_auth = newauth.Enabled()
# Allow skipping SSOAuthenticator for local testing purposes.
skip_sso = newauth.SkipSSO()
if use_new_auth:
LOGGER.debug('Authenticator.get: using new auth stack')
if LuciContextAuthenticator.is_applicable():
LOGGER.debug(
'Authenticator.get: using LUCI context authenticator')
ret = LuciContextAuthenticator()
else:
LOGGER.debug(
'Authenticator.get: using chained authenticator')
a = [
SSOAuthenticator(),
# GCE detection can't distinguish cloud workstations.
GceAuthenticator(),
LuciAuthAuthenticator(),
]
if skip_sso:
LOGGER.debug(
'Authenticator.get: skipping SSOAuthenticator.')
authenticators = authenticators[1:]
ret = ChainedAuthenticator(a)
cls._resolved = ret
return ret
authenticators = [
LuciContextAuthenticator,
GceAuthenticator,
CookiesAuthenticator,
]
for candidate in authenticators:
if candidate.is_applicable():
LOGGER.debug('Authenticator.get: Selected %s.',
candidate.__name__)
ret = candidate()
cls._resolved = ret
return ret
auth_names = ', '.join(a.__name__ for a in authenticators)
raise ValueError(
f"Could not find suitable authenticator, tried: [{auth_names}]."
)
class SSOAuthenticator(Authenticator):
"""SSOAuthenticator implements a Google-internal authentication scheme.
TEMPORARY configuration for Googlers (one `url` block for each Gerrit host):
[url "sso://chromium/"]
insteadOf = https://chromium.googlesource.com/
insteadOf = http://chromium.googlesource.com/
[depot-tools]
useNewAuthStack = 1
"""
# This is set to true in tests, allows _parse_config to consume expired
# cookies.
_testing_load_expired_cookies = False
# How long we should wait for the sso helper to write and close stdout.
# Overridden in tests.
_timeout_secs = 5
@dataclass
class SSOInfo:
proxy: httplib2.ProxyInfo
cookies: http.cookiejar.CookieJar
headers: Dict[str, str]
# SSOInfo is a cached blob of information used by the `authenticate` method.
_sso_info: Optional[SSOInfo] = None
_sso_info_lock = threading.Lock()
@classmethod
def _resolve_sso_cmd(cls) -> Tuple[str, ...]:
"""Returns the cached command-line to invoke git-remote-sso.
If git-remote-sso is not in $PATH, returns ().
"""
cmd = ssoHelper.find_cmd()
if not cmd:
return ()
return (
cmd,
'-print_config',
'sso://*.git.corp.google.com',
)
@classmethod
def is_applicable(cls, *, conn: Optional[HttpConn] = None) -> bool:
if not cls._resolve_sso_cmd():
return False
email = scm.GIT.GetConfig(os.getcwd(), 'user.email', default='')
return email.endswith('@google.com')
@classmethod
def _parse_config(cls, config: str) -> SSOInfo:
parsed: Dict[str, str] = dict(line.strip().split('=', 1)
for line in config.splitlines())
fullAuthHeader = cast(
str,
scm.GIT.Capture([
'config',
'-f',
parsed['include.path'],
'http.extraHeader',
]))
headerKey, headerValue = fullAuthHeader.split(':', 1)
headers = {headerKey.strip(): headerValue.strip()}
proxy_host, proxy_port = parsed['http.proxy'].split(':', 1)
cfpath = parsed['http.cookiefile']
cj = http.cookiejar.MozillaCookieJar(cfpath)
# NOTE: python3.8 doesn't support httponly cookie lines, so we parse
# this manually. Once we move to python3.10+, this hack can be removed.
with open(cfpath) as cf:
cookiedata = cf.read().replace('#HttpOnly_', '')
# _really_load is the way that MozillaCookieJar subclasses
# FileCookieJar. Calling this directly is better than reimplementing the
# entire _really_load function manually.
cj._really_load(
StringIO(cookiedata),
cfpath,
ignore_discard=False,
ignore_expires=cls._testing_load_expired_cookies,
)
return cls.SSOInfo(proxy=httplib2.ProxyInfo(
httplib2.socks.PROXY_TYPE_HTTP_NO_TUNNEL, proxy_host.encode(),
int(proxy_port)),
cookies=cj,
headers=headers)
@classmethod
def _launch_sso_helper(cls) -> SSOInfo:
"""Launches the git-remote-sso process and extracts the parsed SSOInfo.
Raises an exception if something goes wrong.
"""
cmd = cls._resolve_sso_cmd()
with tempdir() as tdir:
tf = os.path.join(tdir, 'git-remote-sso.stderr')
with open(tf, mode='w') as stderr_file:
# NOTE: The git-remote-sso helper does the following:
#
# 1. writes files to disk.
# 2. writes config to stdout, referencing those files.
# 3. closes stdout (thus sending EOF to us, allowing
# sys.stdout.read() to complete).
# 4. waits for stdin to close.
# 5. deletes files on disk (which is why we make sys.stdin a PIPE
# instead of closing it outright).
#
# NOTE: the http.proxy value in the emitted config points to
# a socket which is owned by a system service, not `proc` itself.
with subprocess2.Popen(cmd,
stdout=subprocess2.PIPE,
stderr=stderr_file,
stdin=subprocess2.PIPE,
encoding='utf-8') as proc:
stderr_file.close() # we can close after process starts.
timedout = False
def _fire_timeout():
nonlocal timedout
timedout = True
proc.kill()
timer = threading.Timer(cls._timeout_secs, _fire_timeout)
timer.start()
try:
stdout_data = proc.stdout.read()
finally:
timer.cancel()
if timedout:
LOGGER.error(
'SSOAuthenticator: Timeout: %r: reading config.',
cmd)
raise subprocess.TimeoutExpired(
cmd=cmd, timeout=cls._timeout_secs)
# if the process already ended, then something is wrong.
retcode = proc.poll()
# if stdout was closed without any data, we need to wait for
# end-of-process here and hope for an error message - the
# poll above is racy in this case (we could see stdout EOF
# but the process may not have quit yet).
if not retcode and not stdout_data:
retcode = proc.wait(timeout=cls._timeout_secs)
# We timed out while doing `wait` - we can't safely open
# stderr on windows, so just emit a generic timeout
# exception.
if retcode is None:
LOGGER.error(
'SSOAuthenticator: Timeout: %r: waiting error output.',
cmd)
raise subprocess.TimeoutExpired(
cmd=cmd, timeout=cls._timeout_secs)
# Finally, if the poll or wait ended up getting the retcode,
# it means the process failed, so we can read the stderr
# file and reflect it back to the user.
if retcode is not None:
# process failed - we should be able to read the tempfile.
with open(tf, encoding='utf-8') as stderr:
sys.exit(
f'SSOAuthenticator: exit {retcode}: {stderr.read().strip()}'
)
return cls._parse_config(stdout_data)
@classmethod
def _get_sso_info(cls) -> SSOInfo:
with cls._sso_info_lock:
info = cls._sso_info
if not info:
info = cls._launch_sso_helper()
cls._sso_info = info
return info
def authenticate(self, conn: HttpConn):
sso_info = self._get_sso_info()
conn.proxy_info = sso_info.proxy
conn.req_headers.update(sso_info.headers)
# Now we must rewrite:
# https://xxx.googlesource.com ->
# http://xxx.git.corp.google.com
parsed = urllib.parse.urlparse(conn.req_uri)
parsed = parsed._replace(scheme='http')
if (hostname :=
parsed.hostname) and hostname.endswith('.googlesource.com'):
assert not parsed.port, "SSOAuthenticator: netloc: port not supported"
assert not parsed.username, "SSOAuthenticator: netloc: username not supported"
assert not parsed.password, "SSOAuthenticator: netloc: password not supported"
hostname_parts = hostname.rsplit('.', 2) # X, googlesource, com
conn.req_host = hostname_parts[0] + '.git.corp.google.com'
parsed = parsed._replace(netloc=conn.req_host)
conn.req_uri = parsed.geturl()
# Finally, add cookies
sso_info.cookies.add_cookie_header(conn)
assert 'Cookie' in conn.req_headers, (
'sso_info.cookies.add_cookie_header failed to add Cookie')
def debug_summary_state(self) -> str:
return ''
class CookiesAuthenticator(Authenticator):
"""Authenticator implementation that uses ".gitcookies" for token.
Expected case for developer workstations.
"""
_EMPTY = object()
def __init__(self):
# Credentials will be loaded lazily on first use. This ensures
# Authenticator get() can always construct an authenticator, even if
# something is broken. This allows 'creds-check' to proceed to actually
# checking creds later, rigorously (instead of blowing up with a cryptic
# error if they are wrong).
self._gitcookies = self._EMPTY
@classmethod
def is_applicable(cls, *, conn: Optional[HttpConn] = None) -> bool:
# We consider CookiesAuthenticator always applicable for now.
return True
@property
def gitcookies(self):
if self._gitcookies is self._EMPTY:
self._gitcookies = self._get_gitcookies()
return self._gitcookies
@classmethod
def get_new_password_url(cls, host):
assert not host.startswith('http')
# Assume *.googlesource.com pattern.
parts = host.split('.')
# remove -review suffix if present.
if parts[0].endswith('-review'):
parts[0] = parts[0][:-len('-review')]
return 'https://%s/new-password' % ('.'.join(parts))
@classmethod
def _get_new_password_message(cls, host):
if host is None:
return ('Git host for Gerrit upload is unknown. Check your remote '
'and the branch your branch is tracking. This tool assumes '
'that you are using a git server at *.googlesource.com.')
url = cls.get_new_password_url(host)
return 'You can (re)generate your credentials by visiting %s' % url
@classmethod
def get_gitcookies_path(cls) -> str:
if envVal := os.getenv('GIT_COOKIES_PATH'):
return envVal
return os.path.expanduser(
scm.GIT.GetConfig(os.getcwd(), 'http.cookiefile',
os.path.join('~', '.gitcookies')))
@classmethod
def _get_gitcookies(cls):
gitcookies = {}
path = cls.get_gitcookies_path()
if not os.path.exists(path):
return gitcookies
try:
f = gclient_utils.FileRead(path, 'rb').splitlines()
except IOError:
return gitcookies
for line in f:
try:
fields = line.strip().split('\t')
if line.strip().startswith('#') or len(fields) != 7:
continue
domain, xpath, key, value = fields[0], fields[2], fields[
5], fields[6]
if xpath == '/' and key == 'o':
if value.startswith('git-'):
login, secret_token = value.split('=', 1)
gitcookies[domain] = (login, secret_token)
else:
gitcookies[domain] = ('', value)
except (IndexError, ValueError, TypeError) as exc:
LOGGER.warning(exc)
return gitcookies
def _get_auth_for_host(self, host):
for domain, creds in self.gitcookies.items():
if http.cookiejar.domain_match(host, domain):
return (creds[0], creds[1])
return None
def authenticate(self, conn: HttpConn):
a = self._get_auth_for_host(conn.req_host)
if a:
login, cred = a
if login:
secret = base64.b64encode(f'{login}:{cred}'.encode('utf-8'))
conn.req_headers[
'Authorization'] = f'Basic {secret.decode("utf-8")}'
else:
conn.req_headers['Authorization'] = f'Bearer {cred}'
def ensure_authenticated(self, gerrit_host: str, git_host: str) -> Tuple[bool, str]:
"""Returns (bypassable, error message).
If the error message is empty, there is no error to report.
If bypassable is true, the caller will allow the user to continue past the
error.
"""
# Lazy-loader to identify Gerrit and Git hosts.
gerrit_auth = self._get_auth_for_host(gerrit_host)
git_auth = self._get_auth_for_host(git_host)
if gerrit_auth and git_auth:
if gerrit_auth == git_auth:
return True, ''
all_gsrc = self._get_auth_for_host('d0esN0tEx1st.googlesource.com')
print(
'WARNING: You have different credentials for Gerrit and git hosts:\n'
' %s\n'
' %s\n'
' Consider running the following command:\n'
' git cl creds-check\n'
' %s\n'
' %s' %
(git_host, gerrit_host,
('Hint: delete creds for .googlesource.com' if all_gsrc else
''), self._get_new_password_message(git_host)))
return True, 'If you know what you are doing'
missing = (([] if gerrit_auth else [gerrit_host]) +
([] if git_auth else [git_host]))
return False, ('Credentials for the following hosts are required:\n'
' %s\n'
'These are read from %s\n'
'%s' % ('\n '.join(missing), self.get_gitcookies_path(),
self._get_new_password_message(git_host)))
# Used to redact the cookies from the gitcookies file.
GITCOOKIES_REDACT_RE = re.compile(r'1/.*')
def debug_summary_state(self) -> str:
gitcookies_path = self.get_gitcookies_path()
if os.path.isfile(gitcookies_path):
gitcookies = gclient_utils.FileRead(gitcookies_path)
return self.GITCOOKIES_REDACT_RE.sub('REDACTED', gitcookies)
return ''
def get_auth_email(self, host):
"""Best effort parsing of email to be used for auth for the given host."""
a = self._get_auth_for_host(host)
if not a:
return None
login = a[0]
# login typically looks like 'git-xxx.example.com'
if not login.startswith('git-') or '.' not in login:
return None
username, domain = login[len('git-'):].split('.', 1)
return '%s@%s' % (username, domain)
class GceAuthenticator(Authenticator):
"""Authenticator implementation that uses GCE metadata service for token.
"""
_INFO_URL = 'http://metadata.google.internal'
_ACQUIRE_URL = ('%s/computeMetadata/v1/instance/'
'service-accounts/default/token' % _INFO_URL)
_ACQUIRE_HEADERS = {"Metadata-Flavor": "Google"}
_cache_is_gce = None
_token_cache = None
_token_expiration = None
@classmethod
def is_applicable(cls, *, conn: Optional[HttpConn] = None):
if os.getenv('SKIP_GCE_AUTH_FOR_GIT'):
return False
if cls._cache_is_gce is None:
cls._cache_is_gce = cls._test_is_gce()
return cls._cache_is_gce
@classmethod
def _test_is_gce(cls):
# Based on https://cloud.google.com/compute/docs/metadata#runninggce
resp, _ = cls._get(cls._INFO_URL)
if resp is None:
return False
return resp.get('metadata-flavor') == 'Google'
@staticmethod
def _get(url, **kwargs):
next_delay_sec = 1.0
for i in range(TRY_LIMIT):
p = urllib.parse.urlparse(url)
if p.scheme not in ('http', 'https'):
raise RuntimeError("Don't know how to work with protocol '%s'" %
p.scheme)
try:
resp, contents = httplib2.Http().request(url, 'GET', **kwargs)
except (socket.error, httplib2.HttpLib2Error,
httplib2.socks.ProxyError) as e:
LOGGER.debug('GET [%s] raised %s', url, e)
return None, None
LOGGER.debug('GET [%s] #%d/%d (%d)', url, i + 1, TRY_LIMIT,
resp.status)
if resp.status < 500:
return (resp, contents)
# Retry server error status codes.
LOGGER.warning('Encountered server error')
if TRY_LIMIT - i > 1:
next_delay_sec = log_retry_and_sleep(next_delay_sec, i)
return None, None
@classmethod
def _get_token_dict(cls):
# If cached token is valid for at least 25 seconds, return it.
if cls._token_cache and time_time() + 25 < cls._token_expiration:
return cls._token_cache
resp, contents = cls._get(cls._ACQUIRE_URL,
headers=cls._ACQUIRE_HEADERS)
if resp is None or resp.status != 200:
return None
cls._token_cache = json.loads(contents)
cls._token_expiration = cls._token_cache['expires_in'] + time_time()
return cls._token_cache
def authenticate(self, conn: HttpConn):
token_dict = self._get_token_dict()
if not token_dict:
return
conn.req_headers[
'Authorization'] = '%(token_type)s %(access_token)s' % token_dict
def debug_summary_state(self) -> str:
# TODO(b/343230702) - report ambient account name.
return ''
class LuciContextAuthenticator(Authenticator):
"""Authenticator implementation that uses LUCI_CONTEXT ambient local auth.
"""
@staticmethod
def is_applicable(*, conn: Optional[HttpConn] = None):
return auth.has_luci_context_local_auth()
def __init__(self):
self._authenticator = auth.Authenticator(' '.join(
[auth.OAUTH_SCOPE_EMAIL, auth.OAUTH_SCOPE_GERRIT]))
def authenticate(self, conn: HttpConn):
conn.req_headers[
'Authorization'] = f'Bearer {self._authenticator.get_access_token().token}'
def debug_summary_state(self) -> str:
# TODO(b/343230702) - report ambient account name.
return ''
class LuciAuthAuthenticator(LuciContextAuthenticator):
"""Authenticator implementation that uses `luci-auth` credentials.
This is the same as LuciContextAuthenticator, except that it is for local
non-google.com developer credentials.
"""
@staticmethod
def is_applicable(*, conn: Optional[HttpConn] = None):
return True
class ChainedAuthenticator(Authenticator):
"""Authenticator that delegates to others in sequence.
Authenticators should implement the method `is_applicable_for`.
"""
def __init__(self, authenticators: List[Authenticator]):
self.authenticators = list(authenticators)
def is_applicable(self, *, conn: Optional[HttpConn] = None) -> bool:
return bool(any(
a.is_applicable(conn=conn) for a in self.authenticators))
def authenticate(self, conn: HttpConn):
for a in self.authenticators:
if a.is_applicable(conn=conn):
a.authenticate(conn)
break
else:
raise ValueError(
f'{self!r} has no applicable authenticator for {conn!r}')
def debug_summary_state(self) -> str:
return ''
class ReqParams(TypedDict):
uri: str
method: str
headers: Dict[str, str]
body: Optional[str]
class HttpConn(httplib2.Http):
"""HttpConn is an httplib2.Http with additional request-specific fields."""
def __init__(self, *args, req_host: str, req_uri: str, req_method: str,
req_headers: Dict[str, str], req_body: Optional[str],
**kwargs) -> None:
self.req_host = req_host
self.req_uri = req_uri
self.req_method = req_method
self.req_headers = req_headers
self.req_body = req_body
super().__init__(*args, **kwargs)
@property
def req_params(self) -> ReqParams:
return {
'uri': self.req_uri,
'method': self.req_method,
'headers': self.req_headers,
'body': self.req_body,
}
# NOTE: We want to use HttpConn with CookieJar.add_cookie_header, so have
# compatible interface for that here.
#
# NOTE: Someone should really normalize this 'HttpConn' and httplib2
# implementation to just be plain python3 stdlib instead. All of this was
# written during the bad old days of python2.6/2.7, pre-vpython.
def has_header(self, header: str) -> bool:
return header in self.req_headers
def get_full_url(self) -> str:
return self.req_uri
def get_header(self,
header: str,
default: Optional[str] = None) -> Optional[str]:
return self.req_headers.get(header, default)
def add_unredirected_header(self, header: str, value: str):
# NOTE: httplib2 does not support unredirected headers.
self.req_headers[header] = value
@property
def unverifiable(self) -> bool:
return False
@property
def origin_req_host(self) -> str:
return self.req_host
@property
def type(self) -> str:
return urllib.parse.urlparse(self.req_uri).scheme
@property
def host(self) -> str:
return self.req_host
def CreateHttpConn(host,
path,
reqtype='GET',
headers: Optional[Dict[str, str]] = None,
body: Optional[Dict] = None,
timeout=300,
*,
authenticator: Optional[Authenticator] = None) -> HttpConn:
"""Opens an HTTPS connection to a Gerrit service, and sends a request."""
headers = headers or {}
bare_host = host.partition(':')[0]
url = path
if not url.startswith('/'):
url = '/' + url
if not url.startswith('/a/'):
url = '/a%s' % url
rendered_body: Optional[str] = None
if body:
rendered_body = json.dumps(body, sort_keys=True)
headers.setdefault('Content-Type', 'application/json')
uri = urllib.parse.urljoin(f'{GERRIT_PROTOCOL}://{host}', url)
conn = HttpConn(timeout=timeout,
req_host=host,
req_uri=uri,
req_method=reqtype,
req_headers=headers,
req_body=rendered_body)
if authenticator is None:
authenticator = Authenticator.get()
# TODO(crbug.com/1059384): Automatically detect when running on cloudtop.
if isinstance(authenticator, GceAuthenticator):
print('If you\'re on a cloudtop instance, export '
'SKIP_GCE_AUTH_FOR_GIT=1 in your env.')
authenticator.authenticate(conn)
if 'Authorization' not in conn.req_headers:
LOGGER.debug('No authorization found for %s.' % bare_host)
if LOGGER.isEnabledFor(logging.DEBUG):
LOGGER.debug('%s %s', conn.req_method, conn.req_uri)
LOGGER.debug('conn.proxy_info=%r', conn.proxy_info)
for key, val in conn.req_headers.items():
if key in ('Authorization', 'Cookie'):
val = 'HIDDEN'
LOGGER.debug('%s: %s', key, val)
if conn.req_body:
LOGGER.debug(conn.req_body)
return conn
def ReadHttpResponse(conn: HttpConn,
accept_statuses: Container[int] = frozenset([200])):
"""Reads an HTTP response from a connection into a string buffer.
Args:
conn: An Http object created by CreateHttpConn above.
accept_statuses: Treat any of these statuses as success. Default: [200]
Common additions include 204, 400, and 404.
Returns:
A string buffer containing the connection's reply.
"""
response = contents = None
sleep_time = SLEEP_TIME
for idx in range(TRY_LIMIT):
before_response = time.time()
try:
response, contents = conn.request(**conn.req_params)
except socket.timeout:
if idx < TRY_LIMIT - 1:
sleep_time = log_retry_and_sleep(sleep_time, idx)
continue
raise
contents = contents.decode('utf-8', 'replace')
response_time = time.time() - before_response
metrics.collector.add_repeated(
'http_requests',
metrics_utils.extract_http_metrics(conn.req_params['uri'],
conn.req_params['method'],
response.status, response_time))
# If response.status is an accepted status,
# or response.status < 500 then the result is final; break retry loop.
# If the response is 404/409 it might be because of replication lag,
# so keep trying anyway. If it is 429, it is generally ok to retry after
# a backoff.
if (response.status in accept_statuses or response.status < 500
and response.status not in [404, 409, 429]):
LOGGER.debug('got response %d for %s %s', response.status,
conn.req_params['method'], conn.req_params['uri'])
# If 404 was in accept_statuses, then it's expected that the file
# might not exist, so don't return the gitiles error page because
# that's not the "content" that was actually requested.
if response.status == 404:
contents = ''
break
# A status >=500 is assumed to be a possible transient error; retry.
http_version = 'HTTP/%s' % ('1.1' if response.version == 11 else '1.0')
LOGGER.warning(
'A transient error occurred while querying %s:\n'
'%s %s %s\n'
'%s %d %s\n'
'%s', conn.req_host, conn.req_params['method'],
conn.req_params['uri'], http_version, http_version, response.status,
response.reason, contents)
if idx < TRY_LIMIT - 1:
sleep_time = log_retry_and_sleep(sleep_time, idx)
# end of retries loop
# Help the type checker a bit here - it can't figure out the `except` logic
# in the loop above.
assert response, (
"Impossible: End of retry loop without response or exception.")
if response.status in accept_statuses:
return StringIO(contents)