external-systems 0.1.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of external-systems might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: external-systems
3
- Version: 0.1.0
3
+ Version: 0.3.0
4
4
  Summary: A Python library for interacting with Foundry Sources
5
5
  License: Apache-2.0
6
6
  Keywords: Palantir,Foundry,Sources,Compute Modules,Python Functions,Transforms
@@ -22,7 +22,7 @@ Description-Content-Type: text/markdown
22
22
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/external-systems)
23
23
  [![PyPI](https://img.shields.io/pypi/v/external-systems)](https://pypi.org/project/external-systems/)
24
24
  [![License](https://img.shields.io/badge/License-Apache%202.0-lightgrey.svg)](https://opensource.org/licenses/Apache-2.0)
25
-
25
+ <a href="https://autorelease.general.dmz.palantir.tech/palantir/external-systems"><img src="https://img.shields.io/badge/Perform%20an-Autorelease-success.svg" alt="Autorelease"></a>
26
26
 
27
27
  > [!WARNING]
28
28
  > This SDK is incubating and subject to change.
@@ -2,7 +2,7 @@
2
2
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/external-systems)
3
3
  [![PyPI](https://img.shields.io/pypi/v/external-systems)](https://pypi.org/project/external-systems/)
4
4
  [![License](https://img.shields.io/badge/License-Apache%202.0-lightgrey.svg)](https://opensource.org/licenses/Apache-2.0)
5
-
5
+ <a href="https://autorelease.general.dmz.palantir.tech/palantir/external-systems"><img src="https://img.shields.io/badge/Perform%20an-Autorelease-success.svg" alt="Autorelease"></a>
6
6
 
7
7
  > [!WARNING]
8
8
  > This SDK is incubating and subject to change.
@@ -16,4 +16,4 @@
16
16
  # The version is set during the publishing step (since we can't know the version in advance)
17
17
  # using the autorelease bot
18
18
 
19
- __version__ = "0.1.0"
19
+ __version__ = "0.3.0"
@@ -19,6 +19,7 @@ import re
19
19
  import socket
20
20
  import ssl
21
21
  import time
22
+ from typing import Optional
22
23
 
23
24
  import urllib3
24
25
 
@@ -30,15 +31,20 @@ NUM_RETRIES = 10
30
31
  RETRYABLE_RESPONSE_CODES = {503, 429}
31
32
 
32
33
 
33
- def create_socket(https_proxy_uri: str, target_host: str, target_port: int) -> socket.socket:
34
+ def create_socket(
35
+ https_proxy_uri: str, target_host: str, target_port: int, custom_ca_bundle_path: Optional[str] = None
36
+ ) -> socket.socket:
34
37
  """
35
38
  Establishes a socket connection through an HTTPS proxy to a target host and port.
36
- Parameters:
39
+
40
+ Args:
37
41
  https_proxy_uri (str): The URI of the HTTPS proxy, must include auth if required.
38
42
  target_host (str): The hostname of the target server to connect to.
39
43
  target_port (int): The port number of the target server to connect to.
44
+
40
45
  Returns:
41
46
  socket.socket: A connected SSL socket to the target host and port through the proxy.
47
+
42
48
  Raises:
43
49
  ValueError: If the proxy URI does not specify a hostname or port, or if the connection fails after retrying, with an invalid response code.
44
50
  RuntimeError: If there is an exception during the socket creation process.
@@ -55,7 +61,7 @@ def create_socket(https_proxy_uri: str, target_host: str, target_port: int) -> s
55
61
  last_response_code = -1
56
62
  for _ in range(NUM_RETRIES):
57
63
  try:
58
- proxy_socket = _create_ssl_socket(parsed_proxy_uri.hostname, parsed_proxy_uri.port)
64
+ proxy_socket = _create_ssl_socket(parsed_proxy_uri.hostname, parsed_proxy_uri.port, custom_ca_bundle_path)
59
65
  proxy_socket.sendall(f"CONNECT {target_host}:{target_port} HTTP/1.1\r\n".encode())
60
66
  proxy_socket.sendall(f"Host: {target_host}:{target_port}\r\n".encode())
61
67
 
@@ -88,14 +94,16 @@ def create_socket(https_proxy_uri: str, target_host: str, target_port: int) -> s
88
94
  raise ValueError(f"Failed to establish tunnel, invalid response code: {last_response_code}")
89
95
 
90
96
 
91
- def _create_ssl_socket(proxy_host: str, proxy_port: int) -> socket.socket:
92
- ca_bundle_path = os.environ.get("REQUESTS_CA_BUNDLE")
97
+ def _create_ssl_socket(proxy_host: str, proxy_port: int, custom_ca_bundle_path: Optional[str] = None) -> socket.socket:
98
+ ca_bundle_path = (
99
+ custom_ca_bundle_path if custom_ca_bundle_path is not None else os.environ.get("REQUESTS_CA_BUNDLE")
100
+ )
93
101
  if not ca_bundle_path or not os.path.isfile(ca_bundle_path):
94
- log.warning("The REQUESTS_CA_BUNDLE environment variable does not exist or is not a file.")
95
- raise ValueError("REQUESTS_CA_BUNDLE does not exist")
102
+ log.warning("The CA_BUNDLE environment variable does not exist or is not a file.")
103
+ raise ValueError("CA_BUNDLE does not exist")
96
104
  if not os.access(ca_bundle_path, os.R_OK):
97
- log.warning("The REQUESTS_CA_BUNDLE file is not readable.")
98
- raise ValueError("REQUESTS_CA_BUNDLE is not readable")
105
+ log.warning("The CA_BUNDLE file is not readable.")
106
+ raise ValueError("CA_BUNDLE is not readable")
99
107
  context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
100
108
  context.load_verify_locations(ca_bundle_path)
101
109
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -29,6 +29,7 @@ from ._connections import HttpsConnection
29
29
  from ._proxies import create_proxy_session
30
30
  from ._refreshable import DefaultSessionCredentialsManager, Refreshable, RefreshHandler
31
31
  from ._sockets import create_socket
32
+ from ._utils import read_file
32
33
 
33
34
  log = logging.getLogger(__name__)
34
35
 
@@ -46,6 +47,7 @@ class Source:
46
47
  egress_proxy_token: Optional[str],
47
48
  source_configuration: Optional[Any],
48
49
  credentials_refresh_handler: Optional[RefreshHandler[SourceCredentials]] = None,
50
+ ca_bundle_path: Optional[str] = None,
49
51
  ):
50
52
  self._source_parameters = source_parameters
51
53
  self._on_prem_proxy_service_uris = on_prem_proxy_service_uris
@@ -53,6 +55,7 @@ class Source:
53
55
  self._source_configuration = source_configuration
54
56
  self._egress_proxy_token = egress_proxy_token
55
57
  self._credentials_refresh_handler = credentials_refresh_handler
58
+ self._custom_ca_bundle_path = ca_bundle_path
56
59
 
57
60
  @cached_property
58
61
  def secrets(self) -> Mapping[str, str]:
@@ -84,11 +87,17 @@ class Source:
84
87
 
85
88
  new_ca_contents = []
86
89
 
87
- # If requests env var is set, add all existing CAs
88
- requests_ca_bundle_path = os.environ.get("REQUESTS_CA_BUNDLE")
89
- if requests_ca_bundle_path is not None:
90
- with open(requests_ca_bundle_path) as requests_ca_bundle_file:
91
- new_ca_contents.append(requests_ca_bundle_file.read())
90
+ # If a custom CA bundle path is provided, use it.
91
+ # Otherwise, use the requests CA bundle path if it is set.
92
+ ca_bundle_path = (
93
+ self._custom_ca_bundle_path
94
+ if self._custom_ca_bundle_path is not None
95
+ else os.environ.get("REQUESTS_CA_BUNDLE")
96
+ )
97
+
98
+ # Copy the CA bundle contents to the new CA bundle file.
99
+ if ca_bundle_path:
100
+ new_ca_contents.append(read_file(ca_bundle_path))
92
101
 
93
102
  # Add all CAs for the source
94
103
  for required_ca in self._source_parameters.server_certificates.values():
@@ -232,4 +241,4 @@ class Source:
232
241
  if not self._https_proxy_url:
233
242
  raise ValueError("Only usable with Agent Proxy Sources")
234
243
 
235
- return create_socket(self._https_proxy_url, target_host, target_port)
244
+ return create_socket(self._https_proxy_url, target_host, target_port, self._custom_ca_bundle_path)
@@ -23,3 +23,8 @@ def has_expiration_property(source_credentials: SourceCredentials) -> bool:
23
23
  """
24
24
 
25
25
  return hasattr(source_credentials, "expiration")
26
+
27
+
28
+ def read_file(path: str) -> str:
29
+ with open(path) as file:
30
+ return file.read()
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "external-systems"
3
- version = "0.1.0"
3
+ version = "0.3.0"
4
4
  description = "A Python library for interacting with Foundry Sources"
5
5
  authors = ["Palantir Technologies, Inc."]
6
6
  license = "Apache-2.0"