Commit b18bb4c
authored
Relands #18153 with all review feedback addressed, full test coverage,
and live manual verification against Google Cloud mTLS endpoints.
Fixes #18147
### Changes
* Replaced naive `any(prefix in url for prefix in MTLS_URL_PREFIXES)`
substring matching with `_mtls_helper.is_mtls_endpoint(url)`.
* Parsed hostname using `urllib.parse.urlsplit` to avoid false positives
on URL path/query parameters.
* Added support for Private Service Connect (PSC) custom domains
(`*.p.googleapis.com`, `p.googleapis.com`).
* Normalized hostnames by stripping trailing root dots (`.rstrip(".")`)
for FQDNs.
* Annotated `is_mtls_endpoint` with `object` instead of `Any` for strict
static type checking.
* Added comprehensive parametrized unit tests (57 cases in
`test__mtls_helper.py`).
---
### Manual Verification & Findings
1. **Non-mTLS URLs (`storage.googleapis.com`)**: On receiving `401
Unauthorized`, `AuthorizedSession` executes the standard OAuth token
refresh & retry (2 HTTP requests: 401 ➔ 200 OK), and completely **skips
certificate rotation (`spy_check Call Count: 0`)**.
2. **PSC / mTLS URLs (`storage.p.googleapis.com`)**: On receiving `401
Unauthorized`, `AuthorizedSession` **triggers certificate rotation
inspection (`spy_check Call Count: 1`)** before refreshing credentials
and retrying with `200 OK`.
<details>
<summary><b>Click to expand Live Manual Test Output (100%
Green)</b></summary>
```text
========================================================================
Real Production mTLS Certificate & Endpoint Live Manual Verification
========================================================================
===========================================================================
>>> 1. INSPECTING ACTUAL CLIENT CERTIFICATE ON MACHINE
===========================================================================
Client Certificate Status: LOADED FROM DISK / ECP
Certificate Size: 1428 bytes
Private Key Size: 1675 bytes
Certificate Subject: <Name(CN=www.example.com,OU=Google Testing unit,O=Google Testing)>
Certificate Issuer: <Name(CN=Google Testing Intermediate CA,OU=Google Testing unit,O=Google Testing)>
SHA-256 Fingerprint: <SHA256_CERT_FINGERPRINT_REDACTED>
===========================================================================
>>> 2. ESTABLISHING REAL LIVE mTLS HANDSHAKE (pubsub.mtls.googleapis.com)
===========================================================================
Session mTLS Enabled: True
Cached Cert Size in Session: 1428 bytes
Connecting to: https://pubsub.mtls.googleapis.com/v1/projects/<PROJECT_ID>/topics
Live mTLS Handshake Status: SUCCESS (HTTP 403)
===========================================================================
>>> 3. LIVE 401 TEST: NON-mTLS URL WITH PATH TRAP (Bug 1 Fix)
===========================================================================
Target URL: https://storage.googleapis.com/storage/v1/b/mtls.googleapis.com
Notice: Path has 'mtls.googleapis.com', but host is standard 'storage.googleapis.com'
HTTP Requests Sent: 2 (1st: 401 Unauthorized -> 2nd [Retry]: 200 OK)
Endpoint Detected as mTLS: False (Expected: False)
spy_check Call Count: 0 (Expected: 0)
[PASS] Successfully avoided cert rotation check (0 calls) despite 401 and retry!
===========================================================================
>>> 4. LIVE 401 TEST: PRIVATE SERVICE CONNECT (PSC) URL (Bug 2 Fix)
===========================================================================
Target URL: https://storage.p.googleapis.com/b/production-bucket
Notice: Private Service Connect domain (*.p.googleapis.com)
HTTP Requests Sent: 2 (1st: 401 Unauthorized -> 2nd [Retry]: 200 OK)
Endpoint Detected as mTLS: True (Expected: True)
spy_check Call Count: 1 (Expected: 1 on 401)
Cert Check Triggered: True (Inspected disk certificate and computed fingerprint)
[PASS] Successfully triggered cert inspection on 401 (1 call) and retried with 200 OK!
========================================================================
ALL REAL CERTIFICATE LIVE MANUAL TESTS PASSED SUCCESSFULLY! (100% GREEN)
========================================================================
1 parent ad2ef26 commit b18bb4c
6 files changed
Lines changed: 238 additions & 21 deletions
File tree
- packages/google-auth
- google/auth/transport
- tests/transport
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
840 | 841 | | |
841 | 842 | | |
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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | | - | |
| 650 | + | |
657 | 651 | | |
658 | 652 | | |
659 | 653 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | 412 | | |
418 | 413 | | |
419 | 414 | | |
| |||
436 | 431 | | |
437 | 432 | | |
438 | 433 | | |
| 434 | + | |
439 | 435 | | |
440 | 436 | | |
441 | 437 | | |
| |||
Lines changed: 78 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
1888 | 1889 | | |
1889 | 1890 | | |
1890 | 1891 | | |
| 1892 | + | |
| 1893 | + | |
| 1894 | + | |
| 1895 | + | |
| 1896 | + | |
| 1897 | + | |
| 1898 | + | |
| 1899 | + | |
| 1900 | + | |
| 1901 | + | |
| 1902 | + | |
| 1903 | + | |
| 1904 | + | |
| 1905 | + | |
| 1906 | + | |
| 1907 | + | |
| 1908 | + | |
| 1909 | + | |
| 1910 | + | |
| 1911 | + | |
| 1912 | + | |
| 1913 | + | |
| 1914 | + | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
| 1918 | + | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
| 1929 | + | |
| 1930 | + | |
| 1931 | + | |
| 1932 | + | |
| 1933 | + | |
| 1934 | + | |
| 1935 | + | |
| 1936 | + | |
| 1937 | + | |
| 1938 | + | |
| 1939 | + | |
| 1940 | + | |
| 1941 | + | |
| 1942 | + | |
| 1943 | + | |
| 1944 | + | |
| 1945 | + | |
| 1946 | + | |
| 1947 | + | |
| 1948 | + | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
664 | 664 | | |
665 | 665 | | |
666 | 666 | | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
667 | 670 | | |
668 | 671 | | |
669 | 672 | | |
| |||
677 | 680 | | |
678 | 681 | | |
679 | 682 | | |
680 | | - | |
| 683 | + | |
681 | 684 | | |
682 | 685 | | |
683 | 686 | | |
| |||
939 | 942 | | |
940 | 943 | | |
941 | 944 | | |
942 | | - | |
| 945 | + | |
943 | 946 | | |
944 | 947 | | |
945 | 948 | | |
| |||
950 | 953 | | |
951 | 954 | | |
952 | 955 | | |
| 956 | + | |
953 | 957 | | |
954 | 958 | | |
955 | 959 | | |
956 | | - | |
| 960 | + | |
957 | 961 | | |
| 962 | + | |
958 | 963 | | |
959 | | - | |
960 | | - | |
961 | | - | |
962 | | - | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
963 | 969 | | |
964 | 970 | | |
965 | 971 | | |
966 | 972 | | |
967 | 973 | | |
968 | | - | |
| 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 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
969 | 1006 | | |
970 | 1007 | | |
971 | 1008 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
385 | 385 | | |
386 | 386 | | |
387 | 387 | | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
388 | 391 | | |
389 | 392 | | |
390 | 393 | | |
| |||
398 | 401 | | |
399 | 402 | | |
400 | 403 | | |
401 | | - | |
| 404 | + | |
402 | 405 | | |
403 | 406 | | |
404 | 407 | | |
| |||
655 | 658 | | |
656 | 659 | | |
657 | 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 | + | |
658 | 722 | | |
659 | 723 | | |
660 | 724 | | |
| |||
0 commit comments