def read_config(basedir, portnumfile, generated_files=[], _valid_config_sections=None): basedir = abspath_expanduser_unicode(unicode(basedir)) if _valid_config_sections is None: _valid_config_sections = _common_config_sections # complain if there's bad stuff in the config dir _error_about_old_config_files(basedir, generated_files) # canonicalize the portnum file portnumfile = os.path.join(basedir, portnumfile) # (try to) read the main config file config_fname = os.path.join(basedir, "tahoe.cfg") parser = ConfigParser.SafeConfigParser() try: parser = configutil.get_config(config_fname) except EnvironmentError: if os.path.exists(config_fname): raise configutil.validate_config(config_fname, parser, _valid_config_sections()) return _Config(parser, portnumfile, config_fname)
def __init__(self, basedir=u"."): node.Node.__init__(self, basedir) configutil.validate_config(self.config_fname, self.config, _valid_config_sections()) self.init_introducer() webport = self.get_config("node", "web.port", None) if webport: self.init_web(webport) # strports string
def config_from_string(basedir, portnumfile, config_str, _valid_config=None, fpath=None): """ load and validate configuration from in-memory string """ if _valid_config is None: _valid_config = _common_valid_config() if isinstance(config_str, bytes): config_str = config_str.decode("utf-8") # load configuration from in-memory string parser = configutil.get_config_from_string(config_str) configutil.validate_config( "<string>" if fpath is None else fpath.path, parser, _valid_config, ) return _Config( parser, portnumfile, basedir, fpath, _valid_config, )
def set_config(self, section, option, value): """ Set a config option in a section and re-write the tahoe.cfg file :param str section: The name of the section in which to set the option. :param str option: The name of the option to set. :param str value: The value of the option. :raise UnescapedHashError: If the option holds a fURL and there is a ``#`` in the value. """ if option.endswith(".furl") and "#" in value: raise UnescapedHashError(section, option, value) copied_config = configutil.copy_config(self.config) configutil.set_config(copied_config, section, option, value) configutil.validate_config( self._config_fname, copied_config, self.valid_config_sections, ) if self.config_path is not None: configutil.write_config(self.config_path, copied_config) self.config = copied_config
def test_create_client_config(self): d = self.mktemp() os.mkdir(d) fname = os.path.join(d, 'tahoe.cfg') with open(fname, 'w') as f: opts = { "nickname": "nick", "webport": "tcp:3456", "hide-ip": False, "listen": "none", "shares-needed": "1", "shares-happy": "1", "shares-total": "1", } create_node.write_node_config(f, opts) create_node.write_client_config(f, opts) config = configutil.get_config(fname) # should succeed, no exceptions configutil.validate_config( fname, config, client._valid_config(), )
def test_config_validation_success(self): fname = self.create_tahoe_cfg('[node]\nvalid = foo\n') config = configutil.get_config(fname) # should succeed, no exceptions configutil.validate_config( fname, config, self.static_valid_config, )
def test_config_validation_success(self): d = self.mktemp() os.mkdir(d) fname = os.path.join(d, 'tahoe.cfg') with open(fname, 'w') as f: f.write('[node]\nvalid = foo\n') config = configutil.get_config(fname) # should succeed, no exceptions configutil.validate_config(fname, config, dict(node=['valid']))
def test_nothing_valid(self, cfgdict): """ ``validate_config`` raises ``UnknownConfigError`` when the validator is ``ValidConfiguration.nothing()`` for all non-empty configurations. """ cfg = to_configparser(cfgdict) with self.assertRaises(configutil.UnknownConfigError): configutil.validate_config( "<test_everything_valid>", cfg, configutil.ValidConfiguration.nothing(), )
def test_config_validation_success(self): """ ``configutil.validate_config`` returns ``None`` when the configuration it is given has nothing more than the static sections and items defined by the validator. """ # should succeed, no exceptions configutil.validate_config( "<test_config_validation_success>", to_configparser({"node": {"valid": "foo"}}), self.static_valid_config, )
def test_config_dynamic_validation_success(self): """ A configuration with sections and items that are not matched by the static validation but are matched by the dynamic validation is considered valid. """ # should succeed, no exceptions configutil.validate_config( "<test_config_dynamic_validation_success>", to_configparser({"node": {"valid": "foo"}}), self.dynamic_valid_config, )
def config_from_string(basedir, portnumfile, config_str, _valid_config=None): """ load and validate configuration from in-memory string """ if _valid_config is None: _valid_config = _common_valid_config() # load configuration from in-memory string parser = configparser.SafeConfigParser() parser.readfp(StringIO(config_str)) fname = "<in-memory>" configutil.validate_config(fname, parser, _valid_config) return _Config(parser, portnumfile, basedir, fname)
def __init__(self, basedir="."): node.Node.__init__(self, basedir) # All tub.registerReference must happen *after* we upcall, since # that's what does tub.setLocation() configutil.validate_config(self.config_fname, self.config, _valid_config_sections()) self._magic_folder = None self.started_timestamp = time.time() self.logSource = "Client" self.encoding_params = self.DEFAULT_ENCODING_PARAMETERS.copy() self.init_introducer_clients() self.init_stats_provider() self.init_secrets() self.init_node_key() self.init_storage() self.init_control() self._key_generator = KeyGenerator() key_gen_furl = self.get_config("client", "key_generator.furl", None) if key_gen_furl: log.msg("[client]key_generator.furl= is now ignored, see #2783") self.init_client() self.load_static_servers() self.helper = None if self.get_config("helper", "enabled", False, boolean=True): if not self._tub_is_listening: raise ValueError("config error: helper is enabled, but tub " "is not listening ('tub.port=' is empty)") self.init_helper() self.init_ftp_server() self.init_sftp_server() self.init_magic_folder() # If the node sees an exit_trigger file, it will poll every second to see # whether the file still exists, and what its mtime is. If the file does not # exist or has not been modified for a given timeout, the node will exit. exit_trigger_file = os.path.join(self.basedir, self.EXIT_TRIGGER_FILE) if os.path.exists(exit_trigger_file): age = time.time() - os.stat(exit_trigger_file)[stat.ST_MTIME] self.log("%s file noticed (%ds old), starting timer" % (self.EXIT_TRIGGER_FILE, age)) exit_trigger = TimerService(1.0, self._check_exit_trigger, exit_trigger_file) exit_trigger.setServiceParent(self) # this needs to happen last, so it can use getServiceNamed() to # acquire references to StorageServer and other web-statusable things webport = self.get_config("node", "web.port", None) if webport: self.init_web(webport) # strports string
def test_config_dynamic_validation_success(self): """ A configuration with sections and items that are not matched by the static validation but are matched by the dynamic validation is considered valid. """ fname = self.create_tahoe_cfg('[node]\nvalid = foo\n') config = configutil.get_config(fname) # should succeed, no exceptions configutil.validate_config( fname, config, self.dynamic_valid_config, )
def __init__(self, basedir="."): node.Node.__init__(self, basedir) # All tub.registerReference must happen *after* we upcall, since # that's what does tub.setLocation() configutil.validate_config(self.config_fname, self.config, _valid_config_sections()) self._magic_folder = None self.started_timestamp = time.time() self.logSource="Client" self.encoding_params = self.DEFAULT_ENCODING_PARAMETERS.copy() self.init_introducer_clients() self.init_stats_provider() self.init_secrets() self.init_node_key() self.init_storage() self.init_control() self._key_generator = KeyGenerator() key_gen_furl = self.get_config("client", "key_generator.furl", None) if key_gen_furl: log.msg("[client]key_generator.furl= is now ignored, see #2783") self.init_client() self.load_static_servers() self.helper = None if self.get_config("helper", "enabled", False, boolean=True): if not self._tub_is_listening: raise ValueError("config error: helper is enabled, but tub " "is not listening ('tub.port=' is empty)") self.init_helper() self.init_ftp_server() self.init_sftp_server() self.init_magic_folder() # If the node sees an exit_trigger file, it will poll every second to see # whether the file still exists, and what its mtime is. If the file does not # exist or has not been modified for a given timeout, the node will exit. exit_trigger_file = os.path.join(self.basedir, self.EXIT_TRIGGER_FILE) if os.path.exists(exit_trigger_file): age = time.time() - os.stat(exit_trigger_file)[stat.ST_MTIME] self.log("%s file noticed (%ds old), starting timer" % (self.EXIT_TRIGGER_FILE, age)) exit_trigger = TimerService(1.0, self._check_exit_trigger, exit_trigger_file) exit_trigger.setServiceParent(self) # this needs to happen last, so it can use getServiceNamed() to # acquire references to StorageServer and other web-statusable things webport = self.get_config("node", "web.port", None) if webport: self.init_web(webport) # strports string
def config_from_string(basedir, portnumfile, config_str, _valid_config=None): """ load and validate configuration from in-memory string """ if _valid_config is None: _valid_config = _common_valid_config() if isinstance(config_str, bytes): config_str = config_str.decode("utf-8") # load configuration from in-memory string parser = configutil.get_config_from_string(config_str) fname = "<in-memory>" configutil.validate_config(fname, parser, _valid_config) return _Config(parser, portnumfile, basedir, fname)
def test_create_client_config(self): d = self.mktemp() os.mkdir(d) fname = os.path.join(d, 'tahoe.cfg') with open(fname, 'w') as f: opts = {"nickname": "nick", "webport": "tcp:3456", "hide-ip": False, "listen": "none", } create_node.write_node_config(f, opts) create_node.write_client_config(f, opts) config = configutil.get_config(fname) # should succeed, no exceptions configutil.validate_config(fname, config, client._valid_config_sections())
def read_config(basedir, portnumfile, generated_files=[], _valid_config=None): """ Read and validate configuration. :param unicode basedir: directory where configuration data begins :param unicode portnumfile: filename fragment for "port number" files :param list generated_files: a list of automatically-generated configuration files. :param ValidConfiguration _valid_config: (internal use, optional) a structure defining valid configuration sections and keys :returns: :class:`allmydata.node._Config` instance """ basedir = abspath_expanduser_unicode(ensure_text(basedir)) if _valid_config is None: _valid_config = _common_valid_config() # complain if there's bad stuff in the config dir _error_about_old_config_files(basedir, generated_files) # canonicalize the portnum file portnumfile = os.path.join(basedir, portnumfile) # (try to) read the main config file config_fname = os.path.join(basedir, "tahoe.cfg") try: parser = configutil.get_config(config_fname) except EnvironmentError as e: if e.errno != errno.ENOENT: raise # The file is missing, just create empty ConfigParser. parser = configutil.get_config_from_string(u"") configutil.validate_config(config_fname, parser, _valid_config) # make sure we have a private configuration area fileutil.make_dirs(os.path.join(basedir, "private"), 0o700) return _Config(parser, portnumfile, basedir, config_fname)
def read_config(basedir, portnumfile, generated_files=[], _valid_config_sections=None): """ Read and validate configuration. :param unicode basedir: directory where configuration data begins :param unicode portnumfile: filename fragment for "port number" files :param list generated_files: a list of automatically-generated configuration files. :param dict _valid_config_sections: (internal use, optional) a dict-of-dicts structure defining valid configuration sections and keys :returns: :class:`allmydata.node._Config` instance """ basedir = abspath_expanduser_unicode(unicode(basedir)) if _valid_config_sections is None: _valid_config_sections = _common_config_sections # complain if there's bad stuff in the config dir _error_about_old_config_files(basedir, generated_files) # canonicalize the portnum file portnumfile = os.path.join(basedir, portnumfile) # (try to) read the main config file config_fname = os.path.join(basedir, "tahoe.cfg") parser = ConfigParser.SafeConfigParser() try: parser = configutil.get_config(config_fname) except EnvironmentError as e: if e.errno != errno.ENOENT: raise configutil.validate_config(config_fname, parser, _valid_config_sections()) # make sure we have a private configuration area fileutil.make_dirs(os.path.join(basedir, "private"), 0o700) return _Config(parser, portnumfile, basedir, config_fname)
def test_everything_valid(self, cfgdict): """ ``validate_config`` returns ``None`` when the validator is ``ValidConfiguration.everything()``. """ cfg = to_configparser(cfgdict) self.assertIs( configutil.validate_config( "<test_everything_valid>", cfg, configutil.ValidConfiguration.everything(), ), None, )
def test_nothing_empty_valid(self): """ ``validate_config`` returns ``None`` when the validator is ``ValidConfiguration.nothing()`` if the configuration is empty. """ cfg = ConfigParser() self.assertIs( configutil.validate_config( "<test_everything_valid>", cfg, configutil.ValidConfiguration.nothing(), ), None, )
def validate(self, valid_config_sections): configutil.validate_config(self._config_fname, self.config, valid_config_sections)