Skip to content

Commit bf8f16b

Browse files
committed
Give useful error message on common import issue.
This patch addresses #7758 by checking some heuristics on the string provided for import, and if it looks like a file that wasn't given the URI "file://" prefix, tell the user. This also updates the docs for "configure import" to make this requirement more clear.
1 parent 72fe461 commit bf8f16b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

awscli/customizations/configure/importer.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from awscli.customizations.configure.writer import ConfigFileWriter
2020

2121

22+
class ConfigureImportError(Exception):
23+
pass
24+
25+
2226
class ConfigureImportCommand(BasicCommand):
2327
NAME = 'import'
2428
DESCRIPTION = (
@@ -36,9 +40,11 @@ class ConfigureImportCommand(BasicCommand):
3640
{'name': 'csv',
3741
'required': True,
3842
'help_text': (
39-
'The credentials in CSV format generated by the AWS web console.'
43+
'The credentials in CSV format generated by the AWS web console. '
4044
'The CSV file must contain the "User name", "Access key ID", and '
41-
'"Secret access key" headers.'
45+
'"Secret access key" headers. '
46+
'If passing a CSV file path instead of a CSV-formatted string, '
47+
'the "file://" prefix is required.'
4248
),
4349
'cli_type_name': 'string'},
4450
{'name': 'skip-invalid',
@@ -57,6 +63,9 @@ class ConfigureImportCommand(BasicCommand):
5763
'default': '',
5864
'cli_type_name': 'string'},
5965
]
66+
_POSSIBLE_FILE_ARGUMENT = (
67+
'You may be passing a file to import without the "file://" prefix'
68+
)
6069

6170
def __init__(self, session, csv_parser=None, importer=None,
6271
out_stream=None):
@@ -89,9 +98,21 @@ def _import_csv(self, contents):
8998
import_msg = 'Successfully imported %s profile(s)\n' % len(credentials)
9099
uni_print(import_msg, out_file=self._out_stream)
91100

101+
def _check_possible_filepath(self, csv):
102+
"""
103+
Check if the data 1) wouldn't be imported as anything useful, and 2)
104+
looks like it could be a file.
105+
"""
106+
line_count = csv.count('\n')
107+
if line_count <= 1:
108+
comma_count = csv.count(',')
109+
if comma_count == 0 and os.path.isfile(csv):
110+
raise ConfigureImportError(self._POSSIBLE_FILE_ARGUMENT)
111+
92112
def _run_main(self, parsed_args, parsed_globals):
93113
self._csv_parser.strict = not parsed_args.skip_invalid
94114
self._profile_prefix = parsed_args.profile_prefix
115+
self._check_possible_filepath(parsed_args.csv)
95116
self._import_csv(parsed_args.csv)
96117
return 0
97118

tests/unit/customizations/configure/test_importer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
ConfigureImportCommand,
2323
CSVCredentialParser,
2424
CredentialParserError,
25+
ConfigureImportError,
2526
)
2627
from awscli.customizations.configure.writer import ConfigFileWriter
2728

@@ -88,6 +89,10 @@ def test_import_downloaded_bad_headers(self):
8889
with self.assertRaises(CredentialParserError):
8990
self.import_command(args=['--csv', content], parsed_globals=None)
9091

92+
def test_import_fails_if_suspected_file_without_uri_prefix(self):
93+
with self.assertRaises(ConfigureImportError):
94+
self.import_command(args=['--csv', './README.rst'], parsed_globals=None)
95+
9196

9297
class TestCSVCredentialParser(unittest.TestCase):
9398
def setUp(self):

0 commit comments

Comments
 (0)