Skip to content
Closed
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
25 changes: 23 additions & 2 deletions awscli/customizations/configure/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
from awscli.customizations.configure.writer import ConfigFileWriter


class ConfigureImportError(Exception):
pass


class ConfigureImportCommand(BasicCommand):
NAME = 'import'
DESCRIPTION = (
Expand All @@ -36,9 +40,11 @@ class ConfigureImportCommand(BasicCommand):
{'name': 'csv',
'required': True,
'help_text': (
'The credentials in CSV format generated by the AWS web console.'
'The credentials in CSV format generated by the AWS web console. '
'The CSV file must contain the "User name", "Access key ID", and '
'"Secret access key" headers.'
'"Secret access key" headers. '
'If passing a CSV file path instead of a CSV-formatted string, '
'the "file://" prefix is required.'
),
'cli_type_name': 'string'},
{'name': 'skip-invalid',
Expand All @@ -57,6 +63,9 @@ class ConfigureImportCommand(BasicCommand):
'default': '',
'cli_type_name': 'string'},
]
_POSSIBLE_FILE_ARGUMENT = (
'You may be passing a file to import without the "file://" prefix'
)

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

def _check_possible_filepath(self, csv):
"""
Check if the data 1) wouldn't be imported as anything useful, and 2)
looks like it could be a file.
"""
line_count = csv.count('\n')
if line_count <= 1:
comma_count = csv.count(',')
if comma_count == 0 and os.path.isfile(csv):
raise ConfigureImportError(self._POSSIBLE_FILE_ARGUMENT)

def _run_main(self, parsed_args, parsed_globals):
self._csv_parser.strict = not parsed_args.skip_invalid
self._profile_prefix = parsed_args.profile_prefix
self._check_possible_filepath(parsed_args.csv)
self._import_csv(parsed_args.csv)
return 0

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/customizations/configure/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
ConfigureImportCommand,
CSVCredentialParser,
CredentialParserError,
ConfigureImportError,
)
from awscli.customizations.configure.writer import ConfigFileWriter

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

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


class TestCSVCredentialParser(unittest.TestCase):
def setUp(self):
Expand Down