Skip to content

Commit

Permalink
Update s3 high level commands to honor --no-verify-ssl
Browse files Browse the repository at this point in the history
Fixes aws#696.
  • Loading branch information
jamesls committed Mar 10, 2014
1 parent 1498cdf commit 2b755f0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 31 deletions.
26 changes: 13 additions & 13 deletions awscli/customizations/s3/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ def __call__(self, args, parsed_globals):
def _do_command(self, parsed_args, parsed_globals):
params = self._build_call_parameters(parsed_args, {})
cmd_params = CommandParameters(self._session, self._name, params)
cmd_params.check_region(parsed_globals)
cmd_params.check_endpoint_url(parsed_globals)
cmd_params.add_region(parsed_globals)
cmd_params.add_endpoint_url(parsed_globals)
cmd_params.add_verify_ssl(parsed_globals)
cmd_params.add_paths(parsed_args.paths)
cmd_params.check_force(parsed_globals)
cmd = CommandArchitecture(self._session, self._name,
Expand Down Expand Up @@ -337,7 +338,8 @@ def _create_operation_parser(self, parameter_table):

def _get_endpoint(self, service, parsed_globals):
return service.get_endpoint(region_name=parsed_globals.region,
endpoint_url=parsed_globals.endpoint_url)
endpoint_url=parsed_globals.endpoint_url,
verify=parsed_globals.verify_ssl)


class ListCommand(S3SubCommand):
Expand Down Expand Up @@ -432,7 +434,7 @@ class WebsiteCommand(S3SubCommand):

def _do_command(self, parsed_args, parsed_globals):
service = self._session.get_service('s3')
endpoint = service.get_endpoint(parsed_globals.region)
endpoint = self._get_endpoint(service, parsed_globals)
operation = service.get_operation('PutBucketWebsite')
bucket = self._get_bucket_name(parsed_args.paths[0])
website_configuration = self._build_website_configuration(parsed_args)
Expand Down Expand Up @@ -498,7 +500,8 @@ def __init__(self, session, cmd, parameters):
self._service = self.session.get_service('s3')
self._endpoint = self._service.get_endpoint(
region_name=self.parameters['region'],
endpoint_url=self.parameters['endpoint_url'])
endpoint_url=self.parameters['endpoint_url'],
verify=self.parameters['verify_ssl'])

def create_instructions(self):
"""
Expand Down Expand Up @@ -746,16 +749,10 @@ def check_force(self, parsed_globals):
except:
pass

def check_region(self, parsed_globals):
"""
This ensures that a region has been specified whether it was using
a configuration file, environment variable, or using the command line.
If the region is specified on the command line it takes priority
over specification via a configuration file or environment variable.
"""
def add_region(self, parsed_globals):
self.parameters['region'] = parsed_globals.region

def check_endpoint_url(self, parsed_globals):
def add_endpoint_url(self, parsed_globals):
"""
Adds endpoint_url to the parameters.
"""
Expand All @@ -764,6 +761,9 @@ def check_endpoint_url(self, parsed_globals):
else:
self.parameters['endpoint_url'] = None

def add_verify_ssl(self, parsed_globals):
self.parameters['verify_ssl'] = parsed_globals.verify_ssl


# This is a dictionary useful for automatically adding the different commands,
# the amount of arguments it takes, and the optional parameters that can appear
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/customizations/s3/fake_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FakeService(object):
def __init__(self, session):
self.session = session

def get_endpoint(self, region_name, endpoint_url=None):
def get_endpoint(self, region_name, endpoint_url=None, verify=None):
endpoint = Mock()
endpoint.region_name = region_name
return endpoint
Expand Down
60 changes: 44 additions & 16 deletions tests/unit/customizations/s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def test_call(self):
['--exclude', 'b']])


class FakeArgs(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

class AWSInitializeTest(unittest.TestCase):
"""
This test ensures that all events are correctly registered such that
Expand Down Expand Up @@ -153,9 +157,13 @@ def test_create_instructions(self):
'mb': ['s3_handler'],
'rb': ['s3_handler']}

params = {'filters': True, 'region': 'us-east-1', 'endpoint_url': None}
params = {'filters': True, 'region': 'us-east-1', 'endpoint_url': None,
'verify_ssl': None}
for cmd in cmds:
cmd_arc = CommandArchitecture(self.session, cmd, {'region': 'us-east-1', 'endpoint_url': None})
cmd_arc = CommandArchitecture(self.session, cmd,
{'region': 'us-east-1',
'endpoint_url': None,
'verify_ssl': None})
cmd_arc.create_instructions()
self.assertEqual(cmd_arc.instructions, instructions[cmd])

Expand All @@ -176,7 +184,7 @@ def test_run_cp_put(self):
params = {'dir_op': False, 'dryrun': True, 'quiet': False,
'src': local_file, 'dest': s3_file, 'filters': filters,
'paths_type': 'locals3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'cp', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -191,7 +199,7 @@ def test_error_on_same_line_as_status(self):
params = {'dir_op': False, 'dryrun': False, 'quiet': False,
'src': local_file, 'dest': s3_file, 'filters': filters,
'paths_type': 'locals3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'cp', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -213,7 +221,7 @@ def test_run_cp_get(self):
params = {'dir_op': False, 'dryrun': True, 'quiet': False,
'src': s3_file, 'dest': local_file, 'filters': filters,
'paths_type': 's3local', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'cp', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -229,7 +237,7 @@ def test_run_cp_copy(self):
params = {'dir_op': False, 'dryrun': True, 'quiet': False,
'src': s3_file, 'dest': s3_file, 'filters': filters,
'paths_type': 's3s3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'cp', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -245,7 +253,7 @@ def test_run_mv(self):
params = {'dir_op': False, 'dryrun': True, 'quiet': False,
'src': s3_file, 'dest': s3_file, 'filters': filters,
'paths_type': 's3s3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'mv', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -261,7 +269,7 @@ def test_run_remove(self):
params = {'dir_op': False, 'dryrun': True, 'quiet': False,
'src': s3_file, 'dest': s3_file, 'filters': filters,
'paths_type': 's3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'rm', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -281,7 +289,7 @@ def test_run_sync(self):
params = {'dir_op': True, 'dryrun': True, 'quiet': False,
'src': local_dir, 'dest': s3_prefix, 'filters': filters,
'paths_type': 'locals3', 'region': 'us-east-1',
'endpoint_url': None}
'endpoint_url': None, 'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'sync', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -295,7 +303,8 @@ def test_run_mb(self):
s3_prefix = 's3://' + self.bucket + '/'
params = {'dir_op': True, 'dryrun': True, 'quiet': False,
'src': s3_prefix, 'dest': s3_prefix, 'paths_type': 's3',
'region': 'us-east-1', 'endpoint_url': None}
'region': 'us-east-1', 'endpoint_url': None,
'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'mb', params)
cmd_arc.create_instructions()
cmd_arc.run()
Expand All @@ -309,7 +318,8 @@ def test_run_rb(self):
s3_prefix = 's3://' + self.bucket + '/'
params = {'dir_op': True, 'dryrun': True, 'quiet': False,
'src': s3_prefix, 'dest': s3_prefix, 'paths_type': 's3',
'region': 'us-east-1', 'endpoint_url': None}
'region': 'us-east-1', 'endpoint_url': None,
'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'rb', params)
cmd_arc.create_instructions()
rc = cmd_arc.run()
Expand All @@ -324,7 +334,8 @@ def test_run_rb_nonzero_rc(self):
s3_prefix = 's3://' + self.bucket + '/'
params = {'dir_op': True, 'dryrun': False, 'quiet': False,
'src': s3_prefix, 'dest': s3_prefix, 'paths_type': 's3',
'region': 'us-east-1', 'endpoint_url': None}
'region': 'us-east-1', 'endpoint_url': None,
'verify_ssl': None}
cmd_arc = CommandArchitecture(self.session, 'rb', params)
cmd_arc.create_instructions()
rc = cmd_arc.run()
Expand Down Expand Up @@ -369,7 +380,7 @@ def test_check_path_type_pass(self):

for cmd in cmds.keys():
cmd_param = CommandParameters(self.session, cmd, {})
cmd_param.check_region(mock.Mock())
cmd_param.add_region(mock.Mock())
correct_paths = cmds[cmd]
for path_args in correct_paths:
cmd_param.check_path_type(combos[path_args])
Expand Down Expand Up @@ -397,7 +408,7 @@ def test_check_path_type_fail(self):

for cmd in cmds.keys():
cmd_param = CommandParameters(self.session, cmd, {})
cmd_param.check_region(mock.Mock())
cmd_param.add_region(mock.Mock())
wrong_paths = cmds[cmd]
for path_args in wrong_paths:
with self.assertRaises(TypeError):
Expand All @@ -423,7 +434,7 @@ def test_check_src_path_pass(self):
for filename in files:
parameters['dir_op'] = filename[1]
cmd_parameter = CommandParameters(self.session, 'put', parameters)
cmd_parameter.check_region(mock.Mock())
cmd_parameter.add_region(mock.Mock())
cmd_parameter.check_src_path(filename[0])

def test_check_force(self):
Expand Down Expand Up @@ -494,14 +505,31 @@ def setUp(self):
def test_ls_command_with_no_args(self):
options = {'default': 's3://', 'nargs': '?'}
ls_command = ListCommand('ls', self.session, options)
ls_command([], mock.Mock())
parsed_args = FakeArgs(region=None, endpoint_url=None, verify_ssl=None)
ls_command([], parsed_args)
# We should only be a single call.
self.session.get_service.return_value.get_operation.assert_called_with(
'ListBuckets')
call = self.session.get_service.return_value.get_operation\
.return_value.call
self.assertEqual(call.call_count, 1)
self.assertEqual(call.call_args[1], {})
# Verify get_endpoint
get_endpoint = self.session.get_service.return_value.get_endpoint
args = get_endpoint.call_args
self.assertEqual(args, mock.call(region_name=None, endpoint_url=None,
verify=None))

def test_ls_with_verify_argument(self):
options = {'default': 's3://', 'nargs': '?'}
ls_command = ListCommand('ls', self.session, options)
parsed_args = FakeArgs(region='us-west-2', endpoint_url=None, verify_ssl=False)
ls_command([], parsed_args)
# Verify get_endpoint
get_endpoint = self.session.get_service.return_value.get_endpoint
args = get_endpoint.call_args
self.assertEqual(args, mock.call(region_name='us-west-2', endpoint_url=None,
verify=False))

def test_ls_command_for_bucket(self):
options = {'default': 's3://', 'nargs': '?'}
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/test_clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,21 @@ def test_s3_with_region_and_endpoint_url(self):
's3 ls s3://test --region us-east-1 --endpoint-url https://foobar.com/',
expected_rc=0)
endpoint.assert_called_with(region_name='us-east-1',
endpoint_url='https://foobar.com/')
endpoint_url='https://foobar.com/',
verify=None)

def test_s3_with_no_verify_ssl(self):
with mock.patch('botocore.service.Service.get_endpoint') as endpoint:
http_response = models.Response()
http_response.status_code = 200
endpoint.return_value.make_request.return_value = (
http_response, {'CommonPrefixes': [], 'Contents': []})
self.assert_params_for_cmd(
's3 ls s3://test --no-verify-ssl',
expected_rc=0)
endpoint.assert_called_with(region_name=None,
endpoint_url=None,
verify=False)

def inject_new_param(self, argument_table, **kwargs):
argument = CustomArgument('unknown-arg', {})
Expand Down

0 comments on commit 2b755f0

Please sign in to comment.