Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch timeouts from socket, surface AWS keys not available error to user #998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions S3/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import io
import sys
import socket
import json
from . import Progress
from .SortedDict import SortedDict
Expand All @@ -24,7 +25,7 @@
import http.client as httplib
import locale

try:
try:
from configparser import NoOptionError, NoSectionError, MissingSectionHeaderError, ConfigParser as PyConfigParser
except ImportError:
# Python2 fallback code
Expand Down Expand Up @@ -272,6 +273,8 @@ def role_config(self):
raise IOError
else:
raise IOError
except socket.timeout:
raise socket.timeout
except:
raise

Expand All @@ -284,7 +287,7 @@ def role_refresh(self):

def aws_credential_file(self):
try:
aws_credential_file = os.path.expanduser('~/.aws/credentials')
aws_credential_file = os.path.expanduser('~/.aws/credentials')
if 'AWS_CREDENTIAL_FILE' in os.environ and os.path.isfile(os.environ['AWS_CREDENTIAL_FILE']):
aws_credential_file = config_unicodise(os.environ['AWS_CREDENTIAL_FILE'])

Expand All @@ -297,7 +300,7 @@ def aws_credential_file(self):
# 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()
# to be able to read the file with PyConfigParser()
config_string = None
with open(aws_credential_file, 'r') as f:
config_string = '[default]\n' + f.read()
Expand All @@ -308,7 +311,7 @@ def aws_credential_file(self):
debug("Using AWS profile '%s'" % (profile))

# get_key - helper function to read the aws profile credentials
# including the legacy ones as described here: https://blog.csanchez.org/2011/05/
# including the legacy ones as described here: https://blog.csanchez.org/2011/05/
def get_key(profile, key, legacy_key, print_warning=True):
result = None

Expand All @@ -323,26 +326,26 @@ def get_key(profile, key, legacy_key, print_warning=True):
profile = "default"
result = config.get(profile, key)
warning(
"Legacy configuratin key '%s' used, " % (key) +
"Legacy configuratin key '%s' used, " % (key) +
"please use the standardized config format as described here: " +
"https://aws.amazon.com/blogs/security/a-new-and-standardized-way-to-manage-credentials-in-the-aws-sdks/"
)
except NoOptionError as e:
pass

if result:
debug("Found the configuration option '%s' for the AWS Profile '%s' in the credentials file %s" % (key, profile, aws_credential_file))
debug("Found the configuration option '%s' for the AWS Profile '%s' in the credentials file %s" % (key, profile, aws_credential_file))
return result

profile_access_key = get_key(profile, "aws_access_key_id", "AWSAccessKeyId")
profile_access_key = get_key(profile, "aws_access_key_id", "AWSAccessKeyId")
if profile_access_key:
Config().update_option('access_key', config_unicodise(profile_access_key))

profile_secret_key = get_key(profile, "aws_secret_access_key", "AWSSecretKey")
profile_secret_key = get_key(profile, "aws_secret_access_key", "AWSSecretKey")
if profile_secret_key:
Config().update_option('secret_key', config_unicodise(profile_secret_key))

profile_access_token = get_key(profile, "aws_session_token", None, False)
profile_access_token = get_key(profile, "aws_session_token", None, False)
if profile_access_token:
Config().update_option('access_token', config_unicodise(profile_access_token))

Expand Down
3 changes: 3 additions & 0 deletions s3cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2804,6 +2804,9 @@ def main():
cfg = Config(options.config, options.access_key, options.secret_key, options.access_token)
except ValueError as exc:
raise ParameterError(unicode(exc))
except socket.timeout:
error("AWS access keys not available.")
sys.exit(EX_CONFIG)
except IOError as e:
if options.run_configure:
cfg = Config()
Expand Down