Browse code

pr tasks: - unicodize env variables as well - treat each param from aws credentials as optional - print errors from reading aws credentials file as warnings

Taras Postument authored on 2018/05/08 17:27:34
Showing 1 changed files
... ...
@@ -285,42 +285,46 @@ class Config(object):
285 285
         try:
286 286
             aws_credential_file = os.path.expanduser('~/.aws/credentials') 
287 287
             if 'AWS_CREDENTIAL_FILE' in os.environ and os.path.isfile(os.environ['AWS_CREDENTIAL_FILE']):
288
-                aws_credential_file = os.environ['AWS_CREDENTIAL_FILE']
288
+                aws_credential_file = config_unicodise(os.environ['AWS_CREDENTIAL_FILE'])
289 289
 
290 290
             config = PyConfigParser()
291 291
 
292
-            debug("Reading AWS credentials from", aws_credential_file)
292
+            debug("Reading AWS credentials from %s" % (aws_credential_file))
293 293
             config.read(aws_credential_file)
294 294
 
295
-            profile = "default"
296
-            if 'AWS_PROFILE' in os.environ:
297
-                profile = os.environ['AWS_PROFILE']
295
+            profile = config_unicodise(os.environ.get('AWS_PROFILE', "default"))
298 296
             
299
-            debug("Using AWS profile '{}'".format(profile))
297
+            debug("Using AWS profile '%s'" % (profile))
300 298
 
301 299
             # trying to read aws_access_key_id from credentials file
302
-            profile_access_key = config.get(profile, 'aws_access_key_id')
303
-            debug('Setting "aws_access_key_id" from file {} as "access_key"'.format(aws_credential_file))
304
-            Config().update_option('access_key', config_unicodise(profile_access_key))
300
+            try:
301
+                profile_access_key = config.get(profile, 'aws_access_key_id')
302
+                debug('Setting "aws_access_key_id" from file %s as "access_key"' % (aws_credential_file))
303
+                Config().update_option('access_key', config_unicodise(profile_access_key))
304
+            except NoOptionError as e:
305
+                warning("Couldn't find key '%s' for the AWS Profile '%s' in the credentials file '%s'" % (e.option, e.section, aws_credential_file))
306
+                pass # treat each key as optional
305 307
 
306 308
             # trying to read aws_secret_access_key from credentials file
307
-            profile_secret_key = config.get(profile, 'aws_secret_access_key')
308
-            debug('Setting "aws_secret_access_key" from file {} as "secret_key"'.format(aws_credential_file))
309
-            Config().update_option('secret_key', config_unicodise(profile_secret_key))
310
-            
311 309
             try:
312
-                # trying to read aws_session_token from credentials file
310
+                profile_secret_key = config.get(profile, 'aws_secret_access_key')
311
+                debug('Setting "aws_secret_access_key" from file %s as "secret_key"' % (aws_credential_file))
312
+                Config().update_option('secret_key', config_unicodise(profile_secret_key))
313
+            except NoOptionError as e:
314
+                warning("Couldn't find key '%s' for the AWS Profile '%s' in the credentials file '%s'" % (e.option, e.section, aws_credential_file))
315
+                pass # treat each key as optional
316
+
317
+            # trying to read aws_session_token from credentials file
318
+            try:
313 319
                 profile_access_token = config.get(profile, 'aws_session_token')
314 320
                 debug('Setting "aws_session_token" from file {} as "access_token"'.format(aws_credential_file))
315 321
                 Config().update_option('access_token', config_unicodise(profile_access_token))
316 322
             except NoOptionError:
317 323
                 pass # do nothing, because "access_token" is optional 
318 324
         except IOError as e:
319
-            error("%d accessing credentials file %s" % (e.errno, aws_credential_file))
320
-        except NoOptionError as e:
321
-            error("Couldn't find required key '{}' for the AWS Profile '{}' in the credentials file '{}'".format(e.option, e.section, aws_credential_file))
325
+            warning("%d accessing credentials file %s" % (e.errno, aws_credential_file))
322 326
         except NoSectionError as e:
323
-            error("Couldn't find AWS Profile '{}' in the credentials file '{}'".format(profile, aws_credential_file))
327
+            warning("Couldn't find AWS Profile '%s' in the credentials file '%s'" % (profile, aws_credential_file))
324 328
 
325 329
     def option_list(self):
326 330
         retval = []