Skip to content

Commit

Permalink
Fixes #1361 and #1027 - fix python 3 compat for configparser and add …
Browse files Browse the repository at this point in the history
…AWS shared credential env var (#1365)
  • Loading branch information
ms-jpq authored Jan 20, 2025
1 parent f1f6854 commit 1a793a9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions S3/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
# In python 3, unicode -> str, and str -> bytes
unicode = str

PY3 = (sys.version_info >= (3, 0))


def is_bool_true(value):
"""Check to see if a string is true, yes, on, or 1
Expand Down Expand Up @@ -268,7 +270,7 @@ def __init__(self, configfile = None, access_key=None, secret_key=None, access_t
try:
self.read_config_file(configfile)
except IOError:
if 'AWS_CREDENTIAL_FILE' in os.environ or 'AWS_PROFILE' in os.environ:
if 'AWS_SHARED_CREDENTIALS_FILE' in os.environ or 'AWS_CREDENTIAL_FILE' in os.environ or 'AWS_PROFILE' in os.environ:
self.aws_credential_file()

# override these if passed on the command-line
Expand Down Expand Up @@ -440,7 +442,8 @@ def role_refresh(self):
def aws_credential_file(self):
try:
aws_credential_file = os.path.expanduser('~/.aws/credentials')
credential_file_from_env = os.environ.get('AWS_CREDENTIAL_FILE')
credential_file_from_env = os.environ.get('AWS_SHARED_CREDENTIALS_FILE') \
or os.environ.get('AWS_CREDENTIAL_FILE')
if credential_file_from_env and \
os.path.isfile(credential_file_from_env):
aws_credential_file = base_unicodise(credential_file_from_env)
Expand All @@ -455,17 +458,23 @@ def aws_credential_file(self):
config_string = fp.read()
try:
try:
# readfp is replaced by read_file in python3,
# but so far readfp it is still available.
config.readfp(io.StringIO(config_string))
buf = io.StringIO(config_string)
if PY3:
config.read_file(buf)
else:
config.readfp(buf)
except MissingSectionHeaderError:
# if header is missing, this could be deprecated
# credentials file format as described here:
# https://blog.csanchez.org/2011/05/
# then do the hacky-hack and add default header
# to be able to read the file with PyConfigParser()
config_string = u'[default]\n' + config_string
config.readfp(io.StringIO(config_string))
buf = io.StringIO(config_string)
if PY3:
config.read_file(buf)
else:
config.readfp(buf)
except ParsingError as exc:
raise ValueError(
"Error reading aws_credential_file "
Expand Down

0 comments on commit 1a793a9

Please sign in to comment.