def read_config(paths=None, validate=True): """ Read and validate the admin configuration. :param validate: Validate the configuration. :param validate: bool :param paths: A list of paths to configuration files to read. Reads the standard locations when not specified. :param paths: list :return: A configuration object. :rtype: Config """ if not paths: paths = ['/etc/pulp/admin/admin.conf'] conf_d_dir = '/etc/pulp/admin/conf.d' paths += [ os.path.join(conf_d_dir, i) for i in sorted(os.listdir(conf_d_dir)) ] overrides = os.path.expanduser('~/.pulp/admin.conf') if os.path.exists(overrides): validate_overrides(overrides) paths.append(overrides) config = Config(DEFAULT) config.update(Config(*paths)) if validate: config.validate(SCHEMA) return config
def read_config(path=NODE_CONFIGURATION_PATH, validate=True): """ Get the node configuration object. The node configuration is overridden using values from the pulp consumer.conf and defaulted using server.conf as appropriate. :param path: The optional path to the configuration. :return: The configuration object. :rtype: pulp.common.config.Graph """ config = Config(DEFAULT) config.update(Config(path)) if validate: config.validate(SCHEMA) return config.graph()
def test_unbind(self, repolib_unbind, bind_report): options = {} conduit = MagicMock(autospec=Conduit) repo_id = 'animals' cfg = { 'filesystem': { 'repo_file': 'pulp.repo', 'mirror_list_dir': '/tmp/mirror-list', 'gpg_keys_dir': '/tmp/gpg', 'cert_dir': '/tmp/certs', } } cfg = Config(cfg) conduit.get_consumer_config.return_value = cfg # test handler = bind.RepoHandler({}) report = handler.unbind(conduit, repo_id, options) # validation cfg = cfg.graph() bind_report.assert_called_once_with(repo_id) repolib_unbind.assert_called_once_with( cfg.filesystem.repo_file, os.path.join(cfg.filesystem.mirror_list_dir, repo_id), cfg.filesystem.gpg_keys_dir, cfg.filesystem.cert_dir, repo_id) bind_report.return_value.set_succeeded.assert_called_once_with() self.assertEqual(bind_report.return_value, report)
def setUp(self): super(PulpClientTests, self).setUp() self.config = SafeConfigParser() config_filename = os.path.join(DATA_DIR, 'test-override-client.conf') self.config = Config(config_filename) self.server_mock = mock.Mock() self.pulp_connection = PulpConnection('', server_wrapper=self.server_mock) self.bindings = Bindings(self.pulp_connection) # Disabling color makes it easier to grep results since the character codes aren't there self.recorder = okaara.prompt.Recorder() self.prompt = PulpPrompt(enable_color=False, output=self.recorder, record_tags=True) self.logger = logging.getLogger('pulp') self.exception_handler = ExceptionHandler(self.prompt, self.config) self.context = ClientContext(self.bindings, self.config, self.logger, self.prompt, self.exception_handler) self.cli = PulpCli(self.context) self.context.cli = self.cli
def __init__(self): """ Read from file-system on construction. """ cfg = Config(CONFIG_PATH) files = cfg['filesystem'] path = os.path.join(files['id_cert_dir'], files['id_cert_filename']) Bundle.__init__(self, path)
def get_consumer_config(): class Config(object): def graph(self): config = MagicMock() config.server.verify_ssl = verify_ssl return config return Config()
def get_consumer_config(): class Config(object): def graph(self): config = MagicMock() config.server.ca_path = ca_path return config return Config()
def __init__(self): cfg = Config(CONFIG_PATH) server = cfg['server'] host = server['host'] port = int(server['port']) files = cfg['filesystem'] cert = os.path.join(files['id_cert_dir'], files['id_cert_filename']) connection = PulpConnection(host, port, cert_filename=cert) PulpBindings.__init__(self, connection)
def load_plugin(mock_read): mock_read.return_value = Config() plugin = __import__('pulp.agent.gofer.pulpplugin', {}, {}, ['pulpplugin']) reload(plugin) plugin.descriptor = Mock() plugin.plugin = Mock() plugin.path_monitor = Mock() plugin.registered = True return plugin
def _load_configuration(filenames): """ @param filenames: list of filenames to load @type filenames: list @return: configuration object @rtype: ConfigParser """ config = Config(*filenames) return config
def load_plugin(mock_read): mock_read.return_value = Config() plugin = __import__('pulp.agent.gofer.pulpplugin', {}, {}, ['pulpplugin']) reload(plugin) plugin_cfg = Mock() plugin_cfg.messaging = Mock() plugin.plugin = Mock() plugin.plugin.cfg.return_value = plugin_cfg plugin.path_monitor = Mock() return plugin
def test_override(self): # Setup valid_fp = StringIO(VALID) override_fp = StringIO(OVERRIDE_PROPERTIES) config = Config(valid_fp, override_fp) # Test value = config['server']['url'] # Verify self.assertEqual(value, 'http://bar.com')
def node_configuration(path=NODE_CONFIGURATION_PATH): """ Get the node configuration object. The node configuration is overridden using values from the pulp consumer.conf and defaulted using server.conf as appropriate. :param path: The optional path to the configuration. :return: The configuration object. :rtype: pulp.common.config.Graph """ cfg = Config(path) cfg.validate(NODE_SCHEMA) return cfg.graph()
def __init__(self, name, path): """ @param name: The handler name. @type name: str @param path: The absolute path to the descriptor. @type path: str """ cfg = Config(path) validator = Validator(self.SCHEMA) validator.validate(cfg) self.name = name self.cfg = cfg
def get_consumer_config(self): """ Get the consumer configuration. :return: The consumer configuration object. :rtype: pulp.common.config.Config """ paths = ['/etc/pulp/consumer/consumer.conf'] overrides = os.path.expanduser('~/.pulp/consumer.conf') if os.path.exists(overrides): paths.append(overrides) cfg = Config(*paths) return cfg
def test_bind(self, mock_lock): options = {} conduit = TestConduit(self.CONFIGURATION) bindings = [dict(self.BINDING)] report = self.dispatcher.bind(conduit, bindings, options) self.assertTrue(report.succeeded) self.assertTrue(os.path.isfile(self.REPO_FILE)) repofile = Config(self.REPO_FILE) self.assertEqual(repofile[self.REPO_ID]['name'], self.REPO_NAME) self.assertEqual(repofile[self.REPO_ID]['enabled'], '1') self.assertEqual(repofile[self.REPO_ID]['sslverify'], '1')
def test_bind(self, repolib_bind, urls, bind_report): options = {} conduit = MagicMock(autospec=Conduit) repo_id = 'animals' repo_name = 'Animals' cfg = { 'server': { 'verify_ssl': 'true' }, 'filesystem': { 'repo_file': 'pulp.repo', 'mirror_list_dir': '/tmp/mirror-list', 'gpg_keys_dir': '/tmp/gpg', 'cert_dir': '/tmp/certs', } } cfg = Config(cfg) conduit.get_consumer_config.return_value = cfg details = { 'repo_name': repo_name, 'protocols': ['https'], 'server_name': 'content-world', 'relative_path': 'relative/path' } binding = {'type_id': 'dog', 'repo_id': repo_id, 'details': details} urls.return_value = ['https://content-world'] # test handler = bind.RepoHandler({}) report = handler.bind(conduit, binding, options) # validation cfg = cfg.graph() bind_report.assert_called_once_with(repo_id) repolib_bind.assert_called_once_with( cfg.filesystem.repo_file, os.path.join(cfg.filesystem.mirror_list_dir, repo_id), cfg.filesystem.gpg_keys_dir, cfg.filesystem.cert_dir, repo_id, repo_name, urls.return_value, details.get('gpg_keys', {}), details.get('client_cert'), len(urls.return_value) > 0, verify_ssl=True, ca_path=cfg.server.ca_path) bind_report.return_value.set_succeeded.assert_called_once_with() self.assertEqual(bind_report.return_value, report)
def test_unbind(self, mock_lock): # Setup options = {} conduit = TestConduit(self.CONFIGURATION) bindings = [dict(self.BINDING)] self.dispatcher.bind(conduit, bindings, options) # Test options = {} conduit = TestConduit(self.CONFIGURATION) bindings = [self.UNBINDING] report = self.dispatcher.unbind(conduit, bindings, options) # Verify self.assertTrue(report.succeeded) self.assertTrue(os.path.isfile(self.REPO_FILE)) repofile = Config(self.REPO_FILE) self.assertFalse(self.REPO_ID in repofile)
def is_valid(source_id, descriptor): """ Get whether a content source descriptor is valid. :param source_id: A content source ID. :type source_id: str :param descriptor: A content source descriptor. :type descriptor: dict :return: True if valid. :rtype: bool """ try: schema = list(SCHEMA) schema[0] = source_id validator = Validator((schema, )) cfg = Config({source_id: descriptor}) validator.validate(cfg) return True except ValidationException, e: log.error(str(e))
def read_config(paths=None, validate=True): """ Read and validate the consumer configuration. :param validate: Validate the configuration. :param validate: bool :param paths: A list of paths to configuration files to read. Reads the standard locations when not specified. :param paths: list :return: A configuration object. :rtype: Config """ if not paths: paths = ['/etc/pulp/consumer/consumer.conf'] overrides = os.path.expanduser('~/.pulp/consumer.conf') if os.path.exists(overrides): paths.append(overrides) config = Config(*paths) if validate: config.validate(SCHEMA) return config
def test_clean(self, delete, clean_report): conduit = MagicMock(autospec=Conduit) cfg = { 'filesystem': { 'repo_file': 'pulp.repo', } } cfg = Config(cfg) conduit.get_consumer_config.return_value = cfg # test handler = bind.RepoHandler({}) report = handler.clean(conduit) # validation cfg = cfg.graph() clean_report.assert_called_once_with() delete.assert_called_once_with(cfg.filesystem.repo_file) clean_report.return_value.set_succeeded.assert_called_once_with() self.assertEqual(clean_report.return_value, report)
def validate_overrides(path): """ Raise RuntimeError if the file at 'path' provides a password and is not private to owner. :param path: Full path to the file to check. Assumed the file exists. :type path: basestring :raises: RuntimeError if file is not private and contains a password """ valid_private_perms = [400, 600, 700] file_perm = int(oct(os.stat(path).st_mode & 0777)) cfg = Config(path) if cfg.has_option("auth", "password"): if file_perm not in valid_private_perms: runtime_dict = {'path': path, 'file_perm': file_perm, 'valid_private_perms': valid_private_perms} raise RuntimeError(_( "File %(path)s contains a password and has incorrect permissions: %(file_perm)d, " "It should be one of %(valid_private_perms)s.") % runtime_dict)
def setUp(self): TestCase.setUp(self) self.config = SafeConfigParser() path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', 'client.conf') self.config = Config(path) self.server_mock = mock.Mock() self.pulp_connection = \ PulpConnection('', server_wrapper=self.server_mock) self.bindings = Bindings(self.pulp_connection) self.recorder = okaara.prompt.Recorder() self.prompt = PulpPrompt(enable_color=False, output=self.recorder, record_tags=True) self.logger = logging.getLogger('pulp') self.exception_handler = ExceptionHandler(self.prompt, self.config) self.context = ClientContext(self.bindings, self.config, self.logger, self.prompt, self.exception_handler) self.cli = PulpCli(self.context) self.context.cli = self.cli
def __init__(self, cfg, section): """ Construct the object and validate the configuration. @param cfg: The descriptor configuration. @type cfg: Config @param section: The typedef section name within the descriptor. @type section: str """ schema = (( section, REQUIRED, (('class', REQUIRED, ANY), ), ), ) cfg = Config(cfg, filter=[section]) if cfg: validator = Validator(schema) validator.validate(cfg) self.cfg = cfg[section] else: raise SectionNotFound(section)
def get_consumer_config(self): cfg = Config(self.cfg) return cfg
def test_invalid(self): for s in (MISSING_ENABLED, MISSING_TYPE, MISSING_BASE_URL): fp = StringIO(s) config = Config(fp) for source_id, descriptor in config.items(): self.assertFalse(is_valid(source_id, descriptor))
def test_valid(self): fp = StringIO(VALID) config = Config(fp) for source_id, descriptor in config.items(): self.assertTrue(is_valid(source_id, descriptor))
def fn(s, filter): fp = StringIO(s) d = dict(Config(fp)) return Config(d, filter=filter)
def read(self, s, filter=None): fp = StringIO(s) cfg = Config(fp, filter=filter) return cfg
def node_configuration(self): path = os.path.join(self.parentfs, 'node.crt') with open(path, 'w+') as fp: fp.write(NODE_CERTIFICATE) node_conf = Config({'main': {constants.NODE_CERTIFICATE: path}}) return node_conf.graph()