Skip to content

Commit 15880db

Browse files
Add new interactive option
With `--interactive` option, user will be promp credentials when not provided through configuration file, environement variable or command line parameters. Credentials include: - login - passphrase - Access Key - Secret Key ref #76 Signed-off-by: Jérôme Jutteau <jerome.jutteau@outscale.com>
1 parent f7c5f1a commit 15880db

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

osc_sdk/sdk.py

+38-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import xmltodict
1414
import os
1515
import stat
16+
import getpass
1617

1718
CANONICAL_URI = '/'
1819
CONFIGURATION_FILE = 'config.json'
@@ -116,9 +117,10 @@ class ApiCall(object):
116117
SIG_ALGORITHM = 'AWS4-HMAC-SHA256'
117118
SIG_TYPE = 'AWS4'
118119

119-
def __init__(self, profile=DEFAULT_PROFILE, login=None, password=None, authentication_method=DEFAULT_AUTHENTICATION_METHOD, ephemeral_ak_duration=DEFAULT_EPHEMERAL_AK_DURATION_S):
120+
def __init__(self, profile=DEFAULT_PROFILE, login=None, password=None, authentication_method=DEFAULT_AUTHENTICATION_METHOD, ephemeral_ak_duration=DEFAULT_EPHEMERAL_AK_DURATION_S ,interactive=False):
120121
self.setup_profile_options(profile)
121-
self.setup_cmd_options(login, password, authentication_method, ephemeral_ak_duration)
122+
self.setup_cmd_options(login, password, authentication_method, ephemeral_ak_duration, interactive)
123+
self.setup_interactive_options()
122124
self.check_options()
123125
self.init_ephemeral_auth()
124126

@@ -146,11 +148,40 @@ def setup_profile_options(self, profile):
146148
self.date = None
147149
self.datestamp = None
148150

149-
def setup_cmd_options(self, login, password, authentication_method, ephemeral_ak_duration):
151+
def setup_cmd_options(self, login, password, authentication_method, ephemeral_ak_duration, interactive):
150152
self.login = login
151153
self.password = password
152154
self.authentication_method = authentication_method
153155
self.ephemeral_ak_duration = ephemeral_ak_duration
156+
self.interactive = interactive
157+
158+
def setup_interactive_options(self):
159+
if not self.interactive:
160+
return
161+
if self.authentication_method == 'password':
162+
if self.login == None:
163+
self.login = self.user_input_interactive('Login: ')
164+
if self.password == None:
165+
self.password = self.user_input_interactive_secure('Password: ')
166+
if self.authentication_method == 'accesskey':
167+
if self.access_key == None:
168+
self.access_key = self.user_input_interactive('Access Key: ')
169+
if self.secret_key == None:
170+
self.secret_key = self.user_input_interactive_secure('Secret Key: ')
171+
172+
def user_input_interactive(self, prompt=''):
173+
# avoid input (W1632)
174+
print(prompt, end='', flush=True)
175+
r = sys.stdin.readline().splitlines()[0]
176+
if len(r) == 0:
177+
return None
178+
return r
179+
180+
def user_input_interactive_secure(self, prompt=''):
181+
secret = getpass.getpass(prompt=prompt)
182+
if len(secret) == 0:
183+
return None
184+
return secret
154185

155186
def check_options(self):
156187
if self.authentication_method not in ['accesskey', 'password', 'ephemeral']:
@@ -263,12 +294,12 @@ def ephemeral_auth_ak_create(self):
263294

264295
expiration_date = datetime.datetime.now() + datetime.timedelta(seconds=self.ephemeral_ak_duration)
265296
try:
266-
#call = OSCCall(profile=self.profile_name, login=self.login, password=self.password, authentication_method='password', ephemeral_ak_duration=0)
297+
#call = OSCCall(profile=self.profile_name, login=self.login, password=self.password, authentication_method='password', interactive=self.interfactive)
267298
#r = call.make_request('CreateAccessKey', ExpirationDate=expiration_date.isoformat())
268299

269300
# TODO: create AK
270301
ed = "2705-10-27T16:40:27.864019"
271-
call = OSCCall(profile=self.profile_name, authentication_method='accesskey')
302+
call = OSCCall(profile=self.profile_name, authentication_method='accesskey', interactive=self.interactive)
272303
call.make_request('ReadAccessKeys')
273304
ak = call.response['AccessKeys'][0]['AccessKeyId']
274305
call.make_request('ReadSecretAccessKey', AccessKeyId=ak)
@@ -716,7 +747,7 @@ def get_conf(profile):
716747
raise RuntimeError('Profile {} not found in configuration file'.format(profile))
717748

718749

719-
def api_connect(service, call, profile=DEFAULT_PROFILE, login=None, password=None, authentication_method=DEFAULT_AUTHENTICATION_METHOD, ephemeral_ak_duration=DEFAULT_EPHEMERAL_AK_DURATION_S, *args, **kwargs):
750+
def api_connect(service, call, profile=DEFAULT_PROFILE, login=None, password=None, authentication_method=DEFAULT_AUTHENTICATION_METHOD, ephemeral_ak_duration=DEFAULT_EPHEMERAL_AK_DURATION_S, interactive=False, *args, **kwargs):
720751
calls = {
721752
'api': OSCCall,
722753
'directlink': DirectLinkCall,
@@ -726,7 +757,7 @@ def api_connect(service, call, profile=DEFAULT_PROFILE, login=None, password=Non
726757
'lbu': LbuCall,
727758
'okms': OKMSCall,
728759
}
729-
handler = calls[service](profile, login, password, authentication_method, ephemeral_ak_duration)
760+
handler = calls[service](profile, login, password, authentication_method, ephemeral_ak_duration, interactive)
730761
handler.make_request(call, *args, **kwargs)
731762
if handler.response:
732763
print(json.dumps(handler.response, indent=4))

0 commit comments

Comments
 (0)