13
13
import xmltodict
14
14
import os
15
15
import stat
16
+ import getpass
16
17
17
18
CANONICAL_URI = '/'
18
19
CONFIGURATION_FILE = 'config.json'
@@ -116,9 +117,10 @@ class ApiCall(object):
116
117
SIG_ALGORITHM = 'AWS4-HMAC-SHA256'
117
118
SIG_TYPE = 'AWS4'
118
119
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 ):
120
121
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 ()
122
124
self .check_options ()
123
125
self .init_ephemeral_auth ()
124
126
@@ -146,11 +148,40 @@ def setup_profile_options(self, profile):
146
148
self .date = None
147
149
self .datestamp = None
148
150
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 ):
150
152
self .login = login
151
153
self .password = password
152
154
self .authentication_method = authentication_method
153
155
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
154
185
155
186
def check_options (self ):
156
187
if self .authentication_method not in ['accesskey' , 'password' , 'ephemeral' ]:
@@ -263,12 +294,12 @@ def ephemeral_auth_ak_create(self):
263
294
264
295
expiration_date = datetime .datetime .now () + datetime .timedelta (seconds = self .ephemeral_ak_duration )
265
296
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 )
267
298
#r = call.make_request('CreateAccessKey', ExpirationDate=expiration_date.isoformat())
268
299
269
300
# TODO: create AK
270
301
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 )
272
303
call .make_request ('ReadAccessKeys' )
273
304
ak = call .response ['AccessKeys' ][0 ]['AccessKeyId' ]
274
305
call .make_request ('ReadSecretAccessKey' , AccessKeyId = ak )
@@ -716,7 +747,7 @@ def get_conf(profile):
716
747
raise RuntimeError ('Profile {} not found in configuration file' .format (profile ))
717
748
718
749
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 ):
720
751
calls = {
721
752
'api' : OSCCall ,
722
753
'directlink' : DirectLinkCall ,
@@ -726,7 +757,7 @@ def api_connect(service, call, profile=DEFAULT_PROFILE, login=None, password=Non
726
757
'lbu' : LbuCall ,
727
758
'okms' : OKMSCall ,
728
759
}
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 )
730
761
handler .make_request (call , * args , ** kwargs )
731
762
if handler .response :
732
763
print (json .dumps (handler .response , indent = 4 ))
0 commit comments