|
| 1 | +import json |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | + |
| 5 | +from cnct import ConnectClient |
| 6 | + |
| 7 | +from connect.eaas.core.constants import ( |
| 8 | + EGRESS_PROXY_DEFAULT_MAX_RETRIES, |
| 9 | + EGRESS_PROXY_DEFAULT_PATH, |
| 10 | + EGRESS_PROXY_TLS_CA_CERT_ENV_VAR, |
| 11 | + EGRESS_PROXY_TLS_CLIENT_CERT_ENV_VAR, |
| 12 | + EGRESS_PROXY_TLS_CLIENT_KEY_ENV_VAR, |
| 13 | + EGRESS_PROXY_USER_AGENT_HEADER, |
| 14 | + EGRESS_PROXY_X_CONNECT_TARGET_URL_HEADER, |
| 15 | +) |
| 16 | +from connect.eaas.core.models import EgressProxy, EgressProxyCertificates |
| 17 | + |
| 18 | + |
| 19 | +class EgressProxyClient(ConnectClient): |
| 20 | + """Client for interacting with the Vendor Proxy API.""" |
| 21 | + |
| 22 | + PROXY_PATH = EGRESS_PROXY_DEFAULT_PATH |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + proxy: EgressProxy, |
| 27 | + certificates: EgressProxyCertificates, |
| 28 | + ): |
| 29 | + self.proxy = proxy |
| 30 | + self.cert_file = self._create_temp_cert_file(certificates.client_cert) |
| 31 | + self.key_file = self._create_temp_cert_file(certificates.client_key) |
| 32 | + self.ca_file = self._create_temp_cert_file(certificates.ca_cert) |
| 33 | + |
| 34 | + super().__init__( |
| 35 | + endpoint=self.proxy.url, |
| 36 | + api_key=None, |
| 37 | + max_retries=EGRESS_PROXY_DEFAULT_MAX_RETRIES, |
| 38 | + use_specs=False, |
| 39 | + ) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def _create_temp_cert_file(cert_content): |
| 43 | + """Create a temporary file with certificate content.""" |
| 44 | + temp_file = tempfile.NamedTemporaryFile( |
| 45 | + mode='w', |
| 46 | + delete=False, |
| 47 | + suffix='.pem', |
| 48 | + ) |
| 49 | + temp_file.write(cert_content) |
| 50 | + temp_file.close() |
| 51 | + return temp_file.name |
| 52 | + |
| 53 | + @classmethod |
| 54 | + def require_proxy(cls, account_id: str): |
| 55 | + """ |
| 56 | + Check if a proxy is required for the given account ID. |
| 57 | +
|
| 58 | + Args: |
| 59 | + account_id: The account ID to check (e.g., 'PA-063-101') |
| 60 | + Returns: |
| 61 | + dict | None: Proxy configuration dictionary if it exists |
| 62 | + for the account, None otherwise. |
| 63 | + """ |
| 64 | + egress_config = json.loads(os.getenv('EGRESS_PROXIES_CONFIG') or '{}') |
| 65 | + return egress_config.get(account_id) |
| 66 | + |
| 67 | + @classmethod |
| 68 | + def from_env(cls, account_id: str): |
| 69 | + """ |
| 70 | + Create a VendorProxyClient instance from environment variables. |
| 71 | +
|
| 72 | + Args: |
| 73 | + account_id: The account ID to get proxy config for |
| 74 | + (e.g., 'PA-063-101') |
| 75 | +
|
| 76 | + Environment variables: |
| 77 | + EGRESS_PROXIES_CONFIG: JSON string with proxy configurations |
| 78 | + TLS_CLIENT_KEY: PEM-encoded private key |
| 79 | + TLS_CLIENT_CERT: PEM-encoded client certificate |
| 80 | + TLS_CA_CERT: PEM-encoded CA certificate |
| 81 | + """ |
| 82 | + # Load proxy configuration |
| 83 | + proxy_config = cls.require_proxy(account_id) |
| 84 | + |
| 85 | + if not proxy_config: |
| 86 | + raise ValueError( |
| 87 | + f"No proxy configuration found for account {account_id}", |
| 88 | + ) |
| 89 | + |
| 90 | + proxy = EgressProxy(owner_id=account_id, **proxy_config) |
| 91 | + |
| 92 | + if not all(key in os.environ for key in ( |
| 93 | + EGRESS_PROXY_TLS_CLIENT_CERT_ENV_VAR, |
| 94 | + EGRESS_PROXY_TLS_CLIENT_KEY_ENV_VAR, |
| 95 | + EGRESS_PROXY_TLS_CA_CERT_ENV_VAR, |
| 96 | + )): |
| 97 | + raise ValueError("Missing TLS certificate environment variables") |
| 98 | + |
| 99 | + certificates = EgressProxyCertificates( |
| 100 | + client_cert=os.environ[EGRESS_PROXY_TLS_CLIENT_CERT_ENV_VAR], |
| 101 | + client_key=os.environ[EGRESS_PROXY_TLS_CLIENT_KEY_ENV_VAR], |
| 102 | + ca_cert=os.environ[EGRESS_PROXY_TLS_CA_CERT_ENV_VAR], |
| 103 | + ) |
| 104 | + |
| 105 | + return cls(proxy=proxy, certificates=certificates) |
| 106 | + |
| 107 | + def send_proxied_request(self, *, target_url, target_method, **kwargs): |
| 108 | + """Send a request to the Vendor Proxy API.""" |
| 109 | + kwargs['json'] = kwargs.pop('payload', None) or None |
| 110 | + return self.execute( |
| 111 | + target_method, |
| 112 | + self.PROXY_PATH, |
| 113 | + target_url=target_url, |
| 114 | + **kwargs, |
| 115 | + ) |
| 116 | + |
| 117 | + def _prepare_call_kwargs(self, kwargs): |
| 118 | + target_url = kwargs.pop('target_url') |
| 119 | + kwargs = super()._prepare_call_kwargs(kwargs) |
| 120 | + headers = self._update_headers(target_url, kwargs['headers']) |
| 121 | + self._validate_headers(headers) |
| 122 | + kwargs['headers'] = headers |
| 123 | + kwargs.setdefault('cert', (self.cert_file, self.key_file)) |
| 124 | + kwargs.setdefault('verify', self.ca_file) |
| 125 | + return kwargs |
| 126 | + |
| 127 | + def _update_headers(self, target_url, headers): |
| 128 | + _, rest = headers.get(EGRESS_PROXY_USER_AGENT_HEADER).split('/', 1) |
| 129 | + headers[EGRESS_PROXY_USER_AGENT_HEADER] = ( |
| 130 | + f'connect-egress-proxy-{self.proxy.id}/{rest}' |
| 131 | + ) |
| 132 | + headers[EGRESS_PROXY_X_CONNECT_TARGET_URL_HEADER] = target_url |
| 133 | + headers.pop('Authorization', None) |
| 134 | + return headers |
| 135 | + |
| 136 | + def _validate_headers(self, headers): |
| 137 | + for header in self.proxy.headers: |
| 138 | + if header['name'] not in headers and header.get('required', False): |
| 139 | + raise ValueError( |
| 140 | + f"Missing required header: '{header['name']}'", |
| 141 | + ) |
0 commit comments