From 73f122a690cb3a530fea1b8f64528754f8f9cea0 Mon Sep 17 00:00:00 2001 From: "matthias.gatto" Date: Tue, 18 Jun 2024 14:57:33 +0200 Subject: [PATCH] Enable user to configure proxy and client certificate Signed-off-by: matthias.gatto --- osc_sdk_python/authentication.py | 2 ++ osc_sdk_python/call.py | 11 ++++++++--- osc_sdk_python/credentials.py | 5 ++++- osc_sdk_python/osc-api | 2 +- osc_sdk_python/outscale_gateway.py | 6 ++++-- osc_sdk_python/requester.py | 16 +++++++++++++++- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/osc_sdk_python/authentication.py b/osc_sdk_python/authentication.py index c69b8dd..9a69ad8 100644 --- a/osc_sdk_python/authentication.py +++ b/osc_sdk_python/authentication.py @@ -26,6 +26,8 @@ def __init__(self, credentials, host, self.algorithm = algorithm self.signed_headers = signed_headers self.user_agent = user_agent + self.proxy = credentials.proxy + self.x509_client_cert = credentials.x509_client_cert def forge_headers_signed(self, uri, request_data): date_iso, date = self.build_dates() diff --git a/osc_sdk_python/call.py b/osc_sdk_python/call.py index 41b95e6..f354442 100644 --- a/osc_sdk_python/call.py +++ b/osc_sdk_python/call.py @@ -20,17 +20,22 @@ def __init__(self, logger=None, **kwargs): region=kwargs.pop('region', None), profile=kwargs.pop('profile', None), email=kwargs.pop('email', None), - password=kwargs.pop('password', None)) + password=kwargs.pop('password', None), + proxy=kwargs.pop('proxy', None), + x509_client_cert=kwargs.pop('x509_client_cert', None)) def update_credentials(self, region=None, profile=None, access_key=None, - secret_key=None, email=None, password=None): + secret_key=None, email=None, password=None, proxy=None, + x509_client_cert=None): self.credentials = { 'access_key': access_key, 'secret_key': secret_key, 'region': region, 'profile': profile, 'email': email, - 'password': password + 'password': password, + 'x509_client_cert': x509_client_cert, + 'proxy': proxy } def api(self, action, **data): diff --git a/osc_sdk_python/credentials.py b/osc_sdk_python/credentials.py index 5e996ed..9062ce6 100644 --- a/osc_sdk_python/credentials.py +++ b/osc_sdk_python/credentials.py @@ -7,12 +7,15 @@ DEFAULT_PROFILE="default" class Credentials: - def __init__(self, region, profile, access_key, secret_key, email, password): + def __init__(self, region, profile, access_key, secret_key, email, password, + x509_client_cert=None, proxy=None): self.region = None self.access_key = access_key self.secret_key = secret_key self.email = email self.password = password + self.x509_client_cert=x509_client_cert + self.proxy=proxy if profile is None: profile = os.environ.get('OSC_PROFILE') diff --git a/osc_sdk_python/osc-api b/osc_sdk_python/osc-api index a0a8c98..ae861ab 160000 --- a/osc_sdk_python/osc-api +++ b/osc_sdk_python/osc-api @@ -1 +1 @@ -Subproject commit a0a8c98e746f6ba72b9f6186938eeeecd02bbfe6 +Subproject commit ae861ab23c338a3bf24a597f20fdc8e70cc7eef7 diff --git a/osc_sdk_python/outscale_gateway.py b/osc_sdk_python/outscale_gateway.py index 5d3061c..9ef5b5a 100644 --- a/osc_sdk_python/outscale_gateway.py +++ b/osc_sdk_python/outscale_gateway.py @@ -72,7 +72,8 @@ def __init__(self, retry=True, **kwargs): self.call = Call(logger=self.log, **kwargs) def update_credentials(self, region=None, profile=None, access_key=None, - secret_key=None, email=None, password=None): + secret_key=None, email=None, password=None, + x509_client_cert=None, proxy=None): """ destroy and create a new credential map use for each call. so you can change your ak/sk, region without having to recreate the whole Gateway @@ -80,7 +81,8 @@ def update_credentials(self, region=None, profile=None, access_key=None, example: just updating the password, without renter the login will fail """ self.call.update_credentials(region=region, profile=profile, access_key=access_key, - secret_key=secret_key, email=email, password=password) + secret_key=secret_key, email=email, password=password, + x509_client_cert=x509_client_cert, proxy=x509_client_cert) def access_key(self): return Credentials(**self.call.credentials).access_key diff --git a/osc_sdk_python/requester.py b/osc_sdk_python/requester.py index 2884b9d..259d58b 100644 --- a/osc_sdk_python/requester.py +++ b/osc_sdk_python/requester.py @@ -21,10 +21,24 @@ def send(self, uri, payload): else: headers = self.auth.forge_headers_signed(uri, payload) + if self.auth.x509_client_cert is not None: + cert_file=self.auth.x509_client_cert + else: + cert_file=None + if self.auth.proxy: + if self.auth.proxy.startswith("https"): + proxy= { "https": self.auth.proxy } + else: + proxy= { "http": self.auth.proxy } + else: + proxy=None + + print(proxy, cert_file) with Session() as session: session.mount("https://", self.adapter) session.mount("http://", self.adapter) - response = session.post(self.endpoint, data=payload, headers=headers, verify=True,) + response = session.post(self.endpoint, data=payload, headers=headers, verify=True, + proxies=proxy, cert=cert_file) self.raise_for_status(response) return response.json()