Commit 6eb44978 authored by Alexis Deruelle's avatar Alexis Deruelle
Browse files

feat: implement proxy handling with urllib3

Since refactoring in !64, tokens are generated using 'urllib3' instead
of the 'requests' library.

However urllib3 does not honor proxy related environement variables which
breaks pipelines using Google Cloud authentication in runners behind a
proxy.

This commit implements proxy handling with urllib3:

- Check for https_proxy, HTTPS_PROXY, http_proxy or HTTP_PROXY
  environment variables (by order of precedence),
- parse proxy url from first detected variable,
- set authentication info if present,
- use urllib3.ProxyManager() if proxy is set,
- fallback to urllib3.PoolManager() otherwise.

fixes: 5d44387979f888ed2091b68429b2357c2f4025e0
parent b2b110b0
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
import os

from urllib.parse import urlparse

import certifi
import urllib3
from starlette.exceptions import HTTPException

http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED", ca_certs=certifi.where())
cert_kw = {
    "cert_reqs": "CERT_REQUIRED",
    "ca_certs": certifi.where(),
}

var_names = ["https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"]
proxy_var = next(filter(bool, map(os.environ.get, var_names)), None)

if proxy_var:
    if not proxy_var.startswith("http"):
        proxy_var = f"http://{proxy_var}"

    url = urlparse(proxy_var)
    proxy_auth = (
        f"{url.username}:{url.password}" if url.username and url.password else None
    )
    headers = urllib3.make_headers(proxy_basic_auth=proxy_auth)
    proxy_url = f'{url.scheme}://{url.hostname}:{url.port or "80"}/'
    http = urllib3.ProxyManager(proxy_url, proxy_headers=headers, **cert_kw)
else:
    http = urllib3.PoolManager(**cert_kw)

JWT_TOKEN = os.environ.get("GCP_JWT") or os.environ.get("CI_JOB_JWT_V2")