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

Enable user to configure proxy and client certificate #134

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
2 changes: 2 additions & 0 deletions osc_sdk_python/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions osc_sdk_python/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion osc_sdk_python/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 4 additions & 2 deletions osc_sdk_python/outscale_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ 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
as the object is recreate, you can't expect to keep parameter from the old configuration
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
Expand Down
16 changes: 15 additions & 1 deletion osc_sdk_python/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading