def args_to_importer_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update importer calls @raise InvalidConfig: if one or more arguments is not valid for the importer """ importer_config = _prep_config(kwargs, IMPORTER_CONFIG_KEYS) # Parsing of true/false boolean_arguments = ('ssl_verify', 'verify_size', 'verify_checksum', 'newest', 'remove_old') convert_boolean_arguments(boolean_arguments, importer_config) # Read in the contents of any files that were specified file_arguments = ('ssl_ca_cert', 'ssl_client_cert', 'ssl_client_key') convert_file_contents(file_arguments, importer_config) # Handle skip types if importer_config.get('skip', None) is not None: skip_as_list = _convert_skip_types(importer_config['skip']) importer_config['skip'] = skip_as_list if 'num_old_packages' in importer_config: if importer_config['num_old_packages'] is None: importer_config['num_old_packages'] = 0 else: importer_config['num_old_packages'] = int(importer_config['num_old_packages']) LOG.debug('Importer configuration options') LOG.debug(importer_config) return importer_config
def args_to_yum_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, YUM_DISTRIBUTOR_CONFIG_KEYS) # Read in the contents of any files that were specified file_arguments = ("auth_cert", "auth_ca", "https_ca", "gpgkey") arg_utils.convert_file_contents(file_arguments, distributor_config) # There is an explicit flag for enabling/disabling repository protection. # This may be useful to expose to the user to quickly turn on/off repo # auth for debugging purposes. For now, if the user has specified an auth # CA, assume they also want to flip that protection flag to true. if "auth_ca" in distributor_config: if distributor_config["auth_ca"] is None: # This would occur if the user requested to remove the CA (as # compared to not mentioning it at all, which is the outer if statement. distributor_config["protected"] = False else: # If there is something in the CA, assume it's turning on auth and # flip the flag to true. distributor_config["protected"] = True return distributor_config
def parse_ssl_group(self, user_input): """ Reads any SSL-related config options from the user input and packages them into the Pulp standard importer config format. :param user_input: keyword arguments from the CLI framework containing user input :type user_input: dict :return: suitable representation of the config that can be stored on the repo :rtype: dict """ key_tuples = ( (constants.KEY_SSL_CA_CERT, self.options_bundle.opt_feed_ca_cert.keyword), (constants.KEY_SSL_VALIDATION, self.options_bundle.opt_verify_feed_ssl.keyword), (constants.KEY_SSL_CLIENT_CERT, self.options_bundle.opt_feed_cert.keyword), (constants.KEY_SSL_CLIENT_KEY, self.options_bundle.opt_feed_key.keyword), ) config = {} for config_key, input_key in key_tuples: safe_parse(user_input, config, input_key, config_key) arg_utils.convert_file_contents(('ssl_ca_cert', 'ssl_client_cert', 'ssl_client_key'), config) return config
def args_to_yum_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, YUM_DISTRIBUTOR_CONFIG_KEYS) # Read in the contents of any files that were specified file_arguments = ('auth_cert', 'auth_ca', 'https_ca', 'gpgkey') arg_utils.convert_file_contents(file_arguments, distributor_config) # There is an explicit flag for enabling/disabling repository protection. # This may be useful to expose to the user to quickly turn on/off repo # auth for debugging purposes. For now, if the user has specified an auth # CA, assume they also want to flip that protection flag to true. if 'auth_ca' in distributor_config: if distributor_config['auth_ca'] is None: # This would occur if the user requested to remove the CA (as # compared to not mentioning it at all, which is the outer if statement. distributor_config['protected'] = False else: # If there is something in the CA, assume it's turning on auth and # flip the flag to true. distributor_config['protected'] = True return distributor_config
def test_args_key_is_none(self, mock_open): """ Tests that when the args[key] is none, the function does not attempt to open the file """ # Setup file_keys = ('key1', 'key2') args = {'key1': None, 'key2': None} # Call convert_file_contents and assert open was never called arg_utils.convert_file_contents(file_keys, args) self.assertEqual(0, mock_open.call_count)
def test_keys_not_in_args(self, mock_open): """ Tests that when a key is not in the args dict, nothing is done """ # Setup file_keys = ('key1', 'key2') args = {'other_key1': 'file2', 'other_key2': 'file2'} # Call convert_file_contents and assert open was never called arg_utils.convert_file_contents(file_keys, args) self.assertEqual(0, mock_open.call_count)
def test_args_key_is_none(self, mock_open): """ Tests that when the args[key] is none, the function does not attempt to open the file """ # Setup file_keys = ('key1', 'key2') args = {'key1': None, 'key2': None} # Call convert_file_contents and assert open was never called arg_utils.convert_file_contents(file_keys, args) self.assertEqual(0, mock_open.call_count)
def test_keys_not_in_args(self, mock_open): """ Tests that when a key is not in the args dict, nothing is done """ # Setup file_keys = ('key1', 'key2') args = {'other_key1': 'file2', 'other_key2': 'file2'} # Call convert_file_contents and assert open was never called arg_utils.convert_file_contents(file_keys, args) self.assertEqual(0, mock_open.call_count)
def args_to_export_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, EXPORT_DISTRIBUTOR_CONFIG_KEYS) # Read in the contents of any files that were specified file_arguments = ("https_ca",) arg_utils.convert_file_contents(file_arguments, distributor_config) return distributor_config
def args_to_export_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, EXPORT_DISTRIBUTOR_CONFIG_KEYS) # Read in the contents of any files that were specified file_arguments = ('https_ca', ) arg_utils.convert_file_contents(file_arguments, distributor_config) return distributor_config
def _parse_distributor_config(self, user_input): """ Generate an ISODistributor configuration based on the given parameters (user input). :param user_input: The keys and values passed to the CLI by the user :type user_input: dict """ key_tuples = ( (constants.CONFIG_SERVE_HTTP, self.opt_http.keyword), (constants.CONFIG_SERVE_HTTPS, self.opt_https.keyword), (constants.CONFIG_SSL_AUTH_CA_CERT, self.opt_auth_ca.keyword), ) config = {} for config_key, input_key in key_tuples: safe_parse(user_input, config, input_key, config_key) arg_utils.convert_file_contents((constants.CONFIG_SSL_AUTH_CA_CERT,), config) return config
def args_to_distributor_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update distributor calls @raise InvalidConfig: if one or more arguments is not valid for the distributor """ distributor_config = _prep_config(kwargs, DISTRIBUTOR_CONFIG_KEYS) # Parsing of true/false boolean_arguments = ('http', 'https', 'generate_metadata') convert_boolean_arguments(boolean_arguments, distributor_config) # Read in the contents of any files that were specified file_arguments = ('auth_cert', 'auth_ca', 'https_ca', 'gpgkey') convert_file_contents(file_arguments, distributor_config) # Handle skip types if distributor_config.get('skip', None) is not None: skip_as_list = _convert_skip_types(distributor_config['skip']) distributor_config['skip'] = skip_as_list # There is an explicit flag for enabling/disabling repository protection. # This may be useful to expose to the user to quickly turn on/off repo # auth for debugging purposes. For now, if the user has specified an auth # CA, assume they also want to flip that protection flag to true. if 'auth_ca' in distributor_config: if distributor_config['auth_ca'] is None: # This would occur if the user requested to remove the CA (as # compared to not mentioning it at all, which is the outer if statement. distributor_config['protected'] = False else: # If there is something in the CA, assume it's turning on auth and # flip the flag to true. distributor_config['protected'] = True LOG.debug('Distributor configuration options') LOG.debug(distributor_config) return distributor_config
def _parse_distributor_config(self, user_input): """ Generate an ISODistributor configuration based on the given parameters (user input). :param user_input: The keys and values passed to the CLI by the user :type user_input: dict """ key_tuples = ( (constants.CONFIG_SERVE_HTTP, self.opt_http.keyword), (constants.CONFIG_SERVE_HTTPS, self.opt_https.keyword), (constants.CONFIG_SSL_AUTH_CA_CERT, self.opt_auth_ca.keyword), ) config = {} for config_key, input_key in key_tuples: safe_parse(user_input, config, input_key, config_key) arg_utils.convert_file_contents((constants.CONFIG_SSL_AUTH_CA_CERT, ), config) return config
def test_keys_in_args(self, mock_open): """ Tests that when a key is in the the arg dict (and the arg[key] is not none), the function attempts to open the file """ # Setup file_keys = ('key1', 'key2') args = {'key1': 'filename1', 'key2': 'filename2', 'key3': 'filename3'} # Mock a file for mock_open to return, and a return value for the read call mock_file = mock.MagicMock(spec=file) mock_file.read.return_value = 'Fake return to a read call' mock_open.return_value = mock_file # Call convert_file_contents and assert open was called 3 times with the correct filename arg_utils.convert_file_contents(file_keys, args) self.assertEqual(2, mock_open.call_count) self.assertEqual(mock_open.call_args_list[0][0], ('filename1', )) self.assertEqual(mock_open.call_args_list[1][0], ('filename2', )) # Assert that read's return value was placed in the args dict self.assertEqual(args['key1'], mock_file.read.return_value) self.assertEqual(args['key2'], mock_file.read.return_value) self.assertEqual(args['key3'], 'filename3')
def test_keys_in_args(self, mock_open): """ Tests that when a key is in the the arg dict (and the arg[key] is not none), the function attempts to open the file """ # Setup file_keys = ('key1', 'key2') args = {'key1': 'filename1', 'key2': 'filename2', 'key3': 'filename3'} # Mock a file for mock_open to return, and a return value for the read call mock_file = mock.MagicMock(spec=file) mock_file.read.return_value = 'Fake return to a read call' mock_open.return_value = mock_file # Call convert_file_contents and assert open was called 3 times with the correct filename arg_utils.convert_file_contents(file_keys, args) self.assertEqual(2, mock_open.call_count) self.assertEqual(mock_open.call_args_list[0][0], ('filename1',)) self.assertEqual(mock_open.call_args_list[1][0], ('filename2',)) # Assert that read's return value was placed in the args dict self.assertEqual(args['key1'], mock_file.read.return_value) self.assertEqual(args['key2'], mock_file.read.return_value) self.assertEqual(args['key3'], 'filename3')
def args_to_importer_config(kwargs): """ Takes the arguments read from the CLI and converts the client-side input to the server-side expectations. The supplied dict will not be modified. @return: config to pass into the add/update importer calls @raise InvalidConfig: if one or more arguments is not valid for the importer """ importer_config = _prep_config(kwargs, IMPORTER_CONFIG_KEYS) # Read in the contents of any files that were specified file_arguments = ('ssl_ca_cert', 'ssl_client_cert', 'ssl_client_key') convert_file_contents(file_arguments, importer_config) if 'num_old_packages' in importer_config: if importer_config['num_old_packages'] is None: importer_config['num_old_packages'] = 0 else: importer_config['num_old_packages'] = int(importer_config['num_old_packages']) LOG.debug('Importer configuration options') LOG.debug(importer_config) return importer_config