Skip to content

Commit

Permalink
Add support for mTLS-capable HTTP proxy with self-signed certs
Browse files Browse the repository at this point in the history
  • Loading branch information
viranch committed Oct 3, 2024
1 parent ac3974a commit 9d2bf2a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ns1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,21 @@ def __init__(self, apiKey=None, config=None, configFile=None, keyID=None):
"""
self.config = config

if self.config is None:
self._loadConfig(apiKey, configFile)
if not isinstance(self.config, Config):
self._loadConfig(apiKey, config, configFile)

if keyID:
self.config.useKeyID(keyID)

def _loadConfig(self, apiKey, configFile):
def _loadConfig(self, apiKey, config, configFile):
self.config = Config()

if apiKey:
self.config.createFromAPIKey(apiKey)
if config is None:
config = {}
config["apiKey"] = apiKey

self.config.loadFromDict(config)
else:
configFile = (
Config.DEFAULT_CONFIG_FILE if not configFile else configFile
Expand Down
15 changes: 14 additions & 1 deletion ns1/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def _doDefaults(self):
if "follow_pagination" not in self._data:
self._data["follow_pagination"] = False

if "http_proxy" not in self._data:
self._data["proxy"] = None

if "client_cert" not in self._data:
self._data["client_cert"] = None

if "cert_verify" not in self._data:
self._data["cert_verify"] = True

def createFromAPIKey(self, apikey, maybeWriteDefault=False):
"""
Create a basic config from a single API key
Expand All @@ -109,7 +118,11 @@ def loadFromDict(self, d):
:param dict d: Python dictionary containing configuration items
"""
self._data = d
apikey = d.pop("apiKey", None)
if apikey:
self.createFromAPIKey(apikey)

self._data.update(d)
self._doDefaults()

def loadFromString(self, body):
Expand Down
6 changes: 6 additions & 0 deletions ns1/rest/transport/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def __init__(self, config):
if not have_requests:
raise ImportError("requests module required for RequestsTransport")
TransportBase.__init__(self, config, self.__module__)

self.session = requests.Session()
if self._config.get("http_proxy", None):
self.session.proxies = {"https": self._config["http_proxy"]}
self.session.cert = self._config.get("client_cert")
self.session.verify = self._config.get("cert_verify", True)

self.REQ_MAP = {
"GET": self.session.get,
"POST": self.session.post,
Expand Down

0 comments on commit 9d2bf2a

Please sign in to comment.