Skip to content

Commit 1174ba0

Browse files
committed
Enable AWS cred file to be read as long as .s3cfg auth fields are
nonexistent or empty. This commit allows the credentials to be read from ~/.aws/credentials or user-specified (via envvar) file if .s3cfg exists but does not have authentication options. The previous behavior is that s3cmd will *not* read credentials from the above path as long as .s3cfg exists (even if it contains no authentication options).
1 parent ae6a635 commit 1174ba0

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

S3/Config.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import http.client as httplib
2525
import locale
2626

27-
try:
27+
try:
2828
from configparser import NoOptionError, NoSectionError, MissingSectionHeaderError, ConfigParser as PyConfigParser
2929
except ImportError:
3030
# Python2 fallback code
@@ -217,8 +217,7 @@ def __init__(self, configfile = None, access_key=None, secret_key=None, access_t
217217
try:
218218
self.read_config_file(configfile)
219219
except IOError:
220-
if 'AWS_CREDENTIAL_FILE' in os.environ or 'AWS_PROFILE' in os.environ:
221-
self.aws_credential_file()
220+
pass
222221

223222
# override these if passed on the command-line
224223
if access_key and secret_key:
@@ -229,7 +228,10 @@ def __init__(self, configfile = None, access_key=None, secret_key=None, access_t
229228
# Do not refresh the IAM role when an access token is provided.
230229
self._access_token_refresh = False
231230

232-
if len(self.access_key)==0:
231+
# The next few clauses are in descending order of priority.
232+
# That is, we will prefer key envvars, then AWS cred file, then IAM auth.
233+
234+
if not self.is_option_nonempty('access_key'):
233235
env_access_key = os.getenv('AWS_ACCESS_KEY') or os.getenv('AWS_ACCESS_KEY_ID')
234236
env_secret_key = os.getenv('AWS_SECRET_KEY') or os.getenv('AWS_SECRET_ACCESS_KEY')
235237
env_access_token = os.getenv('AWS_SESSION_TOKEN') or os.getenv('AWS_SECURITY_TOKEN')
@@ -241,15 +243,25 @@ def __init__(self, configfile = None, access_key=None, secret_key=None, access_t
241243
# Do not refresh the IAM role when an access token is provided.
242244
self._access_token_refresh = False
243245
self.access_token = config_unicodise(env_access_token)
244-
else:
245-
self.role_config()
246+
247+
if not self.is_option_nonempty('access_key'):
248+
self.aws_credential_file()
249+
250+
if not self.is_option_nonempty('access_key'):
251+
self.role_config()
252+
253+
if not self.is_option_nonempty('access_key'):
254+
raise Exception('There is no access key available!')
246255

247256
#TODO check KMS key is valid
248257
if self.kms_key and self.server_side_encryption == True:
249258
warning('Cannot have server_side_encryption (S3 SSE) and KMS_key set (S3 KMS). KMS encryption will be used. Please set server_side_encryption to False')
250259
if self.kms_key and self.signature_v2 == True:
251260
raise Exception('KMS encryption requires signature v4. Please set signature_v2 to False')
252261

262+
def is_option_nonempty(self, option_name):
263+
return hasattr(self, option_name) and bool(str(getattr(self, option_name)))
264+
253265
def role_config(self):
254266
"""
255267
Get credentials from IAM authentication
@@ -273,6 +285,7 @@ def role_config(self):
273285
else:
274286
raise IOError
275287
except:
288+
warning('IAM role config failed')
276289
raise
277290

278291
def role_refresh(self):
@@ -284,9 +297,11 @@ def role_refresh(self):
284297

285298
def aws_credential_file(self):
286299
try:
287-
aws_credential_file = os.path.expanduser('~/.aws/credentials')
300+
aws_credential_file = os.path.expanduser('~/.aws/credentials')
288301
if 'AWS_CREDENTIAL_FILE' in os.environ and os.path.isfile(os.environ['AWS_CREDENTIAL_FILE']):
289302
aws_credential_file = config_unicodise(os.environ['AWS_CREDENTIAL_FILE'])
303+
elif not os.path.isfile(aws_credential_file):
304+
return
290305

291306
config = PyConfigParser()
292307

0 commit comments

Comments
 (0)