Пример #1
0
    def test_return_none_if_dcos_mode(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{'dcos_mode': True})

        with patch('conductr_cli.custom_settings.load_from_file',
                   load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_not_called()

        self.assertIsNone(result)
Пример #2
0
    def test_return_none_if_dcos_mode(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{
            'dcos_mode': True
        })

        with patch('conductr_cli.custom_settings.load_from_file', load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_not_called()

        self.assertIsNone(result)
Пример #3
0
    def test_return_none_if_password_is_none(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{
            'host': '10.0.0.1',
            'port': 7776,
            'dcos_mode': False
        })

        with patch('conductr_cli.custom_settings.load_from_file', load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_called_with(input_args)

        self.assertIsNone(result)
Пример #4
0
    def test_return_fallback_credentials_from_host(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{
            'host': '10.0.0.1',
            'port': 9005,
            'dcos_mode': False
        })

        with patch('conductr_cli.custom_settings.load_from_file', load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_called_with(input_args)

        self.assertEqual(('one', 'one-password'), result)
Пример #5
0
    def test_return_none_if_password_is_none(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{
            'host': '10.0.0.1',
            'port': 7776,
            'dcos_mode': False
        })

        with patch('conductr_cli.custom_settings.load_from_file',
                   load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_called_with(input_args)

        self.assertIsNone(result)
Пример #6
0
    def test_return_fallback_credentials_from_host(self):
        load_from_file_mock = MagicMock(return_value=self.custom_settings)
        input_args = MagicMock(**{
            'host': '10.0.0.1',
            'port': 9005,
            'dcos_mode': False
        })

        with patch('conductr_cli.custom_settings.load_from_file',
                   load_from_file_mock):
            result = custom_settings.load_conductr_credentials(input_args)

        load_from_file_mock.assert_called_with(input_args)

        self.assertEqual(('one', 'one-password'), result)
Пример #7
0
def run(_args=[], configure_logging=True):
    # If we're being invoked via DC/OS then route our http
    # calls via its extension to the requests library. In
    # addition remove the 'conduct-dcos' and 'conduct' arg so that the conduct
    # sub-commands are positioned correctly, along with their
    # arguments.
    if sys.argv and Path(
            sys.argv[0]).name == constants.DCOS_COMMAND_PREFIX + 'conduct':
        dcos_mode = True
        _args = sys.argv[2:]
    else:
        dcos_mode = False
        if not _args:
            # Remove the 'conduct' arg so that we start with the sub command directly
            _args = sys.argv[1:]
    # Parse arguments
    parser = build_parser(dcos_mode)
    argcomplete.autocomplete(parser)
    args = parser.parse_args(_args)
    args.dcos_mode = dcos_mode
    if not vars(args).get('func'):
        if vars(args).get('dcos_info'):
            print(
                'Lightbend ConductR sub commands. Type \'dcos conduct\' to see more.'
            )
            exit(0)
        else:
            parser.print_help()
    else:
        # Offline functions are the functions which do not require network to run, e.g. `conduct version` or
        # `conduct setup-dcos`.
        offline_functions = ['version', 'setup']

        # Only setup network related args (i.e. host, bundle resolvers, basic auth, etc) for functions which requires
        # connectivity to ConductR.
        current_function = vars(args).get('func').__name__
        if current_function not in offline_functions:
            # Add custom plugin dir to import path
            custom_plugins_dir = vars(args).get('custom_plugins_dir')
            if custom_plugins_dir:
                sys.path.append(custom_plugins_dir)

            # DC/OS provides the location of ConductR...
            if dcos_mode:
                args.command = 'dcos conduct'
                dcos_url = urlparse(config.get_config_val('core.dcos_url'))
                args.scheme = dcos_url.scheme
                args.ip = dcos_url.hostname
                default_http_port = 80 if dcos_url.scheme == 'http' else 443
                args.port = dcos_url.port if dcos_url.port else default_http_port
                dcos_url_path = dcos_url.path if dcos_url.path else '/'
                args.base_path = dcos_url_path + 'service/{}/'.format(
                    DEFAULT_DCOS_SERVICE)
            else:
                args.command = 'conduct'

            # Set ConductR host is --host or --ip argument not set
            # Also set the local_connection argument accordingly
            host_from_args = conduct_url.conductr_host(args)
            if not host_from_args:
                host_from_env = host.resolve_host_from_env()
                if host_from_env:
                    args.host = host_from_env
                    args.local_connection = False
                else:
                    args.host = host.resolve_default_host()
            else:
                args.local_connection = False

            args.cli_parameters = get_cli_parameters(args)
            args.custom_settings = custom_settings.load_from_file(args)

            args.conductr_auth = custom_settings.load_conductr_credentials(
                args)

            # Ensure HTTPS is used if authentication is configured
            if args.conductr_auth and args.scheme != 'https':
                args.scheme = 'https'

            args.server_verification_file = custom_settings.load_server_ssl_verification_file(
                args)
            # Ensure verification file exists if specified
            if args.server_verification_file \
                    and not os.path.exists(args.server_verification_file):
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error(
                    'Ensure server SSL verification file exists: {}'.format(
                        args.server_verification_file))
                exit(1)

            if not args.dcos_mode and args.scheme == 'https':
                disable_urllib3_warnings()

        if configure_logging:
            logging_setup.configure_logging(args)

        is_completed_without_error = args.func(args)
        if not is_completed_without_error:
            exit(1)
Пример #8
0
def run(_args=[], configure_logging=True):
    # If we're being invoked via DC/OS then route our http
    # calls via its extension to the requests library. In
    # addition remove the 'conduct-dcos' and 'conduct' arg so that the conduct
    # sub-commands are positioned correctly, along with their
    # arguments.
    if sys.argv and Path(sys.argv[0]).name == constants.DCOS_COMMAND_PREFIX + 'conduct':
        dcos_mode = True
        _args = sys.argv[2:]
    else:
        dcos_mode = False
        if not _args:
            # Remove the 'conduct' arg so that we start with the sub command directly
            _args = sys.argv[1:]
    # Parse arguments
    parser = build_parser(dcos_mode)
    argcomplete.autocomplete(parser)
    args = parser.parse_args(_args)
    args.dcos_mode = dcos_mode
    if not vars(args).get('func'):
        if vars(args).get('dcos_info'):
            print('Lightbend ConductR sub commands. Type \'dcos conduct\' to see more.')
            exit(0)
        else:
            parser.print_help()
    else:
        # Offline functions are the functions which do not require network to run, e.g. `conduct version` or
        # `conduct setup-dcos`.
        offline_functions = ['version', 'setup']

        # Only setup network related args (i.e. host, bundle resolvers, basic auth, etc) for functions which requires
        # connectivity to ConductR.
        current_function = vars(args).get('func').__name__
        if current_function not in offline_functions:
            # Add custom plugin dir to import path
            custom_plugins_dir = vars(args).get('custom_plugins_dir')
            if custom_plugins_dir:
                sys.path.append(custom_plugins_dir)

            # DC/OS provides the location of ConductR...
            if dcos_mode:
                args.command = 'dcos conduct'
                dcos_url = urlparse(config.get_config_val('core.dcos_url'))
                args.scheme = dcos_url.scheme
                args.ip = dcos_url.hostname
                default_http_port = 80 if dcos_url.scheme == 'http' else 443
                args.port = dcos_url.port if dcos_url.port else default_http_port
                dcos_url_path = dcos_url.path if dcos_url.path else '/'
                args.base_path = dcos_url_path + 'service/{}/'.format(DEFAULT_DCOS_SERVICE)
            else:
                args.command = 'conduct'

            # Ensure ConductR host is not empty
            host_from_args = conduct_url.conductr_host(args)
            if not host_from_args:
                host_from_env = host.resolve_default_host()
                if host_from_env:
                    args.host = host_from_env
                else:
                    # Configure logging so error message can be logged properly before exiting with failure
                    logging_setup.configure_logging(args)
                    log = logging.getLogger(__name__)
                    log.error('ConductR host address is not specified')
                    log.error('Please ensure either `{}` environment is specified,'
                              ' or specify the ConductR host using `--host` argument'.format(CONDUCTR_HOST))
                    exit(1)
            else:
                args.local_connection = False

            args.cli_parameters = get_cli_parameters(args)
            args.custom_settings = custom_settings.load_from_file(args)

            args.conductr_auth = custom_settings.load_conductr_credentials(args)

            # Ensure HTTPS is used if authentication is configured
            if args.conductr_auth and not args.scheme == 'https':
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error('Unable to use Basic Auth over {}'.format(args.scheme))
                log.error('Please ensure either `{}` environment is set to `https`,'
                          ' or specify https using `--scheme https` argument'.format(CONDUCTR_SCHEME))
                exit(1)

            args.server_verification_file = custom_settings.load_server_ssl_verification_file(args)
            # Ensure verification file exists if specified
            if args.server_verification_file \
                    and not os.path.exists(args.server_verification_file):
                # Configure logging so error message can be logged properly before exiting with failure
                logging_setup.configure_logging(args)
                log = logging.getLogger(__name__)
                log.error('Ensure server SSL verification file exists: {}'.format(args.server_verification_file))
                exit(1)

            if not args.dcos_mode and args.scheme == 'https':
                disable_urllib3_warnings()

        if configure_logging:
            logging_setup.configure_logging(args)

        is_completed_without_error = args.func(args)
        if not is_completed_without_error:
            exit(1)