Browse code

Completed some PR tasks:- better imports handling- more debug logging- better error logging- use `Config().update_option()` to set keys

Taras Postument authored on 2018/05/07 23:56:05
Showing 1 changed files
... ...
@@ -24,10 +24,11 @@ except ImportError:
24 24
     import http.client as httplib
25 25
 import locale
26 26
 
27
-try:
28
-    import configparser as defaultConfigParser
27
+try: 
28
+ from configparser import NoOptionError, NoSectionError, ConfigParser as PyConfigParser
29 29
 except ImportError:
30
-    import ConfigParser as defaultConfigParser
30
+  # Python2 fallback code
31
+  from ConfigParser import NoOptionError, NoSectionError, ConfigParser as PyConfigParser
31 32
 
32 33
 try:
33 34
     unicode
... ...
@@ -282,33 +283,44 @@ class Config(object):
282 282
 
283 283
     def aws_credential_file(self):
284 284
         try:
285
-            config = defaultConfigParser.ConfigParser()
286
-
287 285
             aws_credential_file = os.path.expanduser('~/.aws/credentials') 
288 286
             if 'AWS_CREDENTIAL_FILE' in os.environ and os.path.isfile(os.environ['AWS_CREDENTIAL_FILE']):
289 287
                 aws_credential_file = os.environ['AWS_CREDENTIAL_FILE']
290 288
 
289
+            config = PyConfigParser()
290
+
291 291
             debug("Reading AWS credentials from", aws_credential_file)
292 292
             config.read(aws_credential_file)
293
+
293 294
             profile = "default"
294 295
             if 'AWS_PROFILE' in os.environ:
295
-                profile = os.environ['AWS_PROFILE'] 
296
+                profile = os.environ['AWS_PROFILE']
297
+            
298
+            debug("Using AWS profile '{}'".format(profile))
296 299
 
300
+            # trying to read aws_access_key_id from credentials file
297 301
             profile_access_key = config.get(profile, 'aws_access_key_id')
298
-            profile_secret_key = config.get(profile, 'aws_secret_access_key')
299
-            self.access_key = config_unicodise(profile_access_key)
300
-            self.secret_key = config_unicodise(profile_secret_key)
302
+            debug('Setting "aws_access_key_id" from file {} as "access_key"'.format(aws_credential_file))
303
+            Config().update_option('access_key', config_unicodise(profile_access_key))
301 304
 
305
+            # trying to read aws_secret_access_key from credentials file
306
+            profile_secret_key = config.get(profile, 'aws_secret_access_key')
307
+            debug('Setting "aws_secret_access_key" from file {} as "secret_key"'.format(aws_credential_file))
308
+            Config().update_option('secret_key', config_unicodise(profile_secret_key))
309
+            
302 310
             try:
311
+                # trying to read aws_session_token from credentials file
303 312
                 profile_access_token = config.get(profile, 'aws_session_token')
304
-                self.access_token = config_unicodise(profile_access_token) 
305
-            except defaultConfigParser.NoOptionError:
306
-                pass
307
-
313
+                debug('Setting "aws_session_token" from file {} as "access_token"'.format(aws_credential_file))
314
+                Config().update_option('access_token', config_unicodise(profile_access_token))
315
+            except NoOptionError:
316
+                pass # do nothing, because "access_token" is optional 
308 317
         except IOError as e:
309
-            error("%d accessing credentials file %s" % (e.errno,os.environ['AWS_CREDENTIAL_FILE']))
310
-        except (defaultConfigParser.NoOptionError, defaultConfigParser.NoSectionError) as e:
311
-            error(e)
318
+            error("%d accessing credentials file %s" % (e.errno, aws_credential_file))
319
+        except NoOptionError as e:
320
+            error("Couldn't find required key '{}' for the AWS Profile '{}' in the credentials file '{}'".format(e.option, e.section, aws_credential_file))
321
+        except NoSectionError as e:
322
+            error("Couldn't find AWS Profile '{}' in the credentials file '{}'".format(profile, aws_credential_file))
312 323
 
313 324
     def option_list(self):
314 325
         retval = []