def test_ssh_proxy_session( mocker, instance_id, ssh_key, get_instance_details_response, config ): mocker.patch("aws_gate.ssh_proxy.get_aws_client") mocker.patch("aws_gate.ssh_proxy.get_aws_resource") mocker.patch("aws_gate.ssh_proxy.query_instance", return_value=instance_id) mocker.patch("aws_gate.ssh_proxy.SshKey", return_value=ssh_key) mocker.patch("aws_gate.ssh_proxy.SshKeyUploader", return_value=mocker.MagicMock()) mocker.patch( "aws_gate.ssh_proxy.get_instance_details", return_value=get_instance_details_response, ) session_mock = mocker.patch( "aws_gate.ssh_proxy.SshProxySession", return_value=mocker.MagicMock() ) mocker.patch("aws_gate.decorators.is_existing_profile", return_value=True) mocker.patch("aws_gate.decorators._plugin_exists", return_value=True) mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0") ssh_proxy( config=config, instance_name=instance_id, profile_name="default", region_name="eu-west-1", ) assert session_mock.called
def test_ssh_proxy_without_config(self): with patch('aws_gate.ssh_proxy.get_aws_client', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.get_aws_resource', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.query_instance', return_value=None), \ patch('aws_gate.ssh_proxy.SshKey', return_value=self.ssh_key), \ patch('aws_gate.decorators._plugin_exists', return_value=True), \ patch('aws_gate.decorators.execute_plugin', return_value='1.1.23.0'): with self.assertRaises(ValueError): ssh_proxy(config=self.empty_config, instance_name=self.instance_id, profile_name='default', region_name='eu-west-1')
def test_ssh_proxy_exception_invalid_profile(self): with patch('aws_gate.ssh_proxy.get_aws_client', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.get_aws_resource', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.query_instance', return_value=None), \ patch('aws_gate.ssh_proxy.SshKey', return_value=self.ssh_key), \ patch('aws_gate.decorators._plugin_exists', return_value=True), \ patch('aws_gate.decorators.execute_plugin', return_value='1.1.23.0'), \ patch('aws_gate.decorators.is_existing_region', return_value=True): with self.assertRaises(ValueError): ssh_proxy(config=self.config, profile_name='invalid-profile', instance_name=self.instance_id, region_name='eu-west-1')
def test_ssh_proxy_without_config(mocker, ssh_key, instance_id, empty_config): mocker.patch("aws_gate.ssh_proxy.get_aws_client") mocker.patch("aws_gate.ssh_proxy.get_aws_resource") mocker.patch("aws_gate.ssh_proxy.query_instance", return_value=None) mocker.patch("aws_gate.ssh_proxy.SshKey", return_value=ssh_key) mocker.patch("aws_gate.decorators._plugin_exists", return_value=True) mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0") with pytest.raises(ValueError): ssh_proxy( config=empty_config, instance_name=instance_id, profile_name="default", region_name="eu-west-1", )
def test_ssh_proxy_exception_invalid_region(mocker, instance_id, ssh_key, config): mocker.patch("aws_gate.ssh_proxy.get_aws_client") mocker.patch("aws_gate.ssh_proxy.get_aws_resource") mocker.patch("aws_gate.ssh_proxy.query_instance", return_value=instance_id) mocker.patch("aws_gate.ssh_proxy.SshKey", return_value=ssh_key) mocker.patch("aws_gate.decorators.is_existing_profile", return_value=True) mocker.patch("aws_gate.decorators._plugin_exists", return_value=True) mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0") with pytest.raises(ValueError): ssh_proxy( config=config, region_name="invalid-region", instance_name=instance_id, profile_name="default", )
def test_ssh_proxy_session(self): with patch('aws_gate.ssh_proxy.get_aws_client', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.get_aws_resource', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.query_instance', return_value=self.instance_id), \ patch('aws_gate.ssh_proxy.SshKey', return_value=self.ssh_key), \ patch('aws_gate.ssh_proxy.SshKeyUploader', return_value=MagicMock()), \ patch('aws_gate.ssh_proxy.get_instance_details', return_value=self.get_instance_details_response), \ patch('aws_gate.ssh_proxy.SshProxySession', return_value=MagicMock()) as session_mock, \ patch('aws_gate.decorators.is_existing_profile', return_value=True), \ patch('aws_gate.decorators._plugin_exists', return_value=True), \ patch('aws_gate.decorators.execute_plugin', return_value='1.1.23.0'): ssh_proxy(config=self.config, instance_name=self.instance_id, profile_name='default', region_name='eu-west-1') self.assertTrue(session_mock.called)
def main(): args = parse_arguments() if not DEBUG: sys.excepthook = lambda exc_type, exc_value, traceback: logger.error( exc_value) log_level = logging.ERROR log_format = "%(message)s" # We want to silence dependencies logging.getLogger("botocore").setLevel(logging.CRITICAL) logging.getLogger("boto3").setLevel(logging.CRITICAL) logging.getLogger("urllib3").setLevel(logging.CRITICAL) if args.verbose: log_level = logging.INFO if DEBUG: log_level = logging.DEBUG log_format = "%(asctime)s - %(name)-28s - %(levelname)-5s - %(message)s" logging.basicConfig(level=log_level, stream=sys.stderr, format=log_format) try: config = load_config_from_files() except (ValidationError, ScannerError) as e: raise ValueError("Invalid configuration provided: {}".format(e)) # We want to provide default values in cases they are not configured # in ~/.aws/config or availabe a environment variables default_region = get_default_region() if default_region is None: default_region = AWS_DEFAULT_REGION # We try to obtain default profile from the environment or use 'default' to # save a call to boto3. In the environment, we check if we are being called # from aws-vault first or not. Then we return 'default' as boto3 will # https://github.com/boto/boto3/blob/develop/boto3/session.py#L93 if "AWS_VAULT" in os.environ: logger.debug( "aws-vault usage detected, defaulting to the AWS profile from $AWS_VAULT" ) default_profile = (os.environ.get("AWS_VAULT") or os.environ.get("AWS_PROFILE") or AWS_DEFAULT_PROFILE) profile = _get_profile(args=args, config=config, default=default_profile) region = _get_region(args=args, config=config, default=default_region) logger.debug('Using AWS profile "%s" in region "%s"', profile, region) if args.subcommand == "bootstrap": bootstrap(force=args.force) if args.subcommand == "session": session( config=config, instance_name=args.instance_name, region_name=region, profile_name=profile, ) if args.subcommand == "ssh": ssh( config=config, instance_name=args.instance_name, region_name=region, profile_name=profile, user=args.os_user, port=args.port, key_type=args.key_type, key_size=args.key_size, command=args.command, ) if args.subcommand == "ssh-config": ssh_config(region_name=region, profile_name=profile, user=args.os_user, port=args.port) if args.subcommand == "ssh-proxy": ssh_proxy( config=config, instance_name=args.instance_name, region_name=region, profile_name=profile, user=args.os_user, port=args.port, key_type=args.key_type, key_size=args.key_size, ) if args.subcommand in ["ls", "list"]: fields = args.output.split(",") list_instances( region_name=region, profile_name=profile, output_format=args.format, fields=fields, )
def main(): # We want to provide default values in cases they are not configured in ~/.aws/config or availabe as # environment variables default_region = get_default_region() if default_region is None: default_region = AWS_DEFAULT_REGION # We try to obtain default profile from the environment or use 'default' to save call to boto3. # boto3 will also return 'default': https://github.com/boto/boto3/blob/develop/boto3/session.py#L93 default_profile = os.environ.get('AWS_PROFILE') or AWS_DEFAULT_PROFILE args = parse_arguments() if not DEBUG: sys.excepthook = lambda exc_type, exc_value, traceback: logger.error( exc_value) log_level = logging.ERROR log_format = '%(message)s' # We want to silence dependencies logging.getLogger('botocore').setLevel(logging.CRITICAL) logging.getLogger('boto3').setLevel(logging.CRITICAL) logging.getLogger('urllib3').setLevel(logging.CRITICAL) if args.verbose: log_level = logging.INFO if DEBUG: log_level = logging.DEBUG log_format = '%(asctime)s - %(name)-28s - %(levelname)-5s - %(message)s' logging.basicConfig(level=log_level, stream=sys.stderr, format=log_format) try: config = load_config_from_files() except (ValidationError, ScannerError) as e: raise ValueError('Invalid configuration provided: {}'.format(e)) profile = _get_profile(args=args, config=config, default=default_profile) region = _get_region(args=args, config=config, default=default_region) logger.debug('Using AWS profile "%s" in region "%s"', profile, region) if args.subcommand == 'bootstrap': bootstrap(force=args.force) if args.subcommand == 'session': session(config=config, instance_name=args.instance_name, region_name=region, profile_name=profile) if args.subcommand == 'ssh-config': ssh_config(region_name=region, profile_name=profile, user=args.os_user, port=args.port) if args.subcommand == 'ssh-proxy': ssh_proxy(config=config, instance_name=args.instance_name, region_name=region, profile_name=profile, user=args.os_user, port=args.port, key_type=args.key_type, key_size=args.key_size) if args.subcommand in ['ls', 'list']: list_instances(region_name=region, profile_name=profile)