Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mTLS-capable HTTP proxy with self-signed certs #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading