From edeb770c796d3dc20aca68d2c0c44128c14db3c2 Mon Sep 17 00:00:00 2001 From: Estrada Matthieu Date: Sat, 17 Mar 2018 16:36:26 +0100 Subject: [PATCH 1/3] Add proxy management to client, by login function Ref #71 --- alignak_backend_client/client.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/alignak_backend_client/client.py b/alignak_backend_client/client.py index 7c04112..f11e722 100755 --- a/alignak_backend_client/client.py +++ b/alignak_backend_client/client.py @@ -136,6 +136,7 @@ def __init__(self, endpoint, processes=1): self.authenticated = False self._token = None + self.proxies = None self.timeout = None # TODO: Add this option in config file @@ -174,7 +175,8 @@ def get_response(self, method, endpoint, headers=None, json=None, params=None, d # First stage. Errors are connection errors (timeout, no session, ...) try: response = self.session.request(method=method, url=url, headers=headers, json=json, - params=params, data=data, timeout=self.timeout) + params=params, data=data, proxies=self.proxies, + timeout=self.timeout) logger.debug("response headers: %s", response.headers) logger.debug("response content: %s", response.content) except RequestException as e: @@ -235,7 +237,7 @@ def get_token(self): token = property(get_token, set_token) - def login(self, username, password, generate='enabled'): + def login(self, username, password, generate='enabled', proxies=None): """ Log into the backend and get the token @@ -256,6 +258,8 @@ def login(self, username, password, generate='enabled'): :type password: str :param generate: Can have these values: enabled | force | disabled :type generate: str + :param proxies: dict of proxy (http and / or https) + :type proxies: dict :return: return True if authentication is successfull, otherwise False :rtype: bool """ @@ -263,6 +267,8 @@ def login(self, username, password, generate='enabled'): if not username or not password: raise BackendException(BACKEND_ERROR, "Missing mandatory parameters") + if proxies: + self.proxies = proxies endpoint = 'login' json = {u'username': username, u'password': password} From 1451d2c9dff0336e79655081947d66f876962b96 Mon Sep 17 00:00:00 2001 From: Estrada Matthieu Date: Sat, 17 Mar 2018 18:24:17 +0100 Subject: [PATCH 2/3] Raise exception in case proxy protocols are wrong Ref #71 --- alignak_backend_client/client.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/alignak_backend_client/client.py b/alignak_backend_client/client.py index f11e722..64bae34 100755 --- a/alignak_backend_client/client.py +++ b/alignak_backend_client/client.py @@ -66,6 +66,9 @@ BACKEND_PAGINATION_LIMIT = 50 BACKEND_PAGINATION_DEFAULT = 25 +# Proxy protocols +PROXY_PROTOCOLS = ['http', 'https'] + # Connection error code BACKEND_ERROR = 1000 @@ -268,6 +271,11 @@ def login(self, username, password, generate='enabled', proxies=None): if not username or not password: raise BackendException(BACKEND_ERROR, "Missing mandatory parameters") if proxies: + for key in proxies.keys(): + try: + assert key in PROXY_PROTOCOLS + except AssertionError: + raise BackendException(BACKEND_ERROR, "Wrong proxy protocol ", key) self.proxies = proxies endpoint = 'login' From 776b0fefd41d0505c5c43f16d15acfdf6b22dfeb Mon Sep 17 00:00:00 2001 From: Matthieu Estrada Date: Mon, 19 Mar 2018 10:43:29 +0100 Subject: [PATCH 3/3] Fix proxy reassignment when retry login Ref #71 --- alignak_backend_client/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/alignak_backend_client/client.py b/alignak_backend_client/client.py index 64bae34..d38463b 100755 --- a/alignak_backend_client/client.py +++ b/alignak_backend_client/client.py @@ -270,13 +270,14 @@ def login(self, username, password, generate='enabled', proxies=None): if not username or not password: raise BackendException(BACKEND_ERROR, "Missing mandatory parameters") + if proxies: for key in proxies.keys(): try: assert key in PROXY_PROTOCOLS except AssertionError: raise BackendException(BACKEND_ERROR, "Wrong proxy protocol ", key) - self.proxies = proxies + self.proxies = proxies endpoint = 'login' json = {u'username': username, u'password': password}