def get(section, option, *args, **kwargs):
            """Faked get() for ConfigParser class."""
            value = configs.get(section, option, *args, **kwargs)
            if value == NULL_TYPE:
                value = ''
            if 'password' in option and value.startswith(ENCRYPTED_SIGNAL):
                s1 = dynamic_config_get('dbmind_config', 'cipher_s1')
                s2 = dynamic_config_get('dbmind_config', 'cipher_s2')
                iv = dynamic_config_get('iv_table',
                                        '%s-%s' % (section, option))
                try:
                    real_value = value[len(ENCRYPTED_SIGNAL
                                           ):] if value.startswith(
                                               ENCRYPTED_SIGNAL) else value
                    value = security.decrypt(s1, s2, iv, real_value)
                except Exception as e:
                    raise InvalidPasswordException(e)

            else:
                valid, reason = check_config_validity(section,
                                                      option,
                                                      value,
                                                      silent=True)
                if not valid:
                    raise ConfigSettingError(
                        'DBMind failed to start due to %s.' % reason)

            return value
def set_config_parameter(confpath, section: str, option: str, value: str):
    if not os.path.exists(confpath):
        raise ConfigSettingError(
            "Invalid directory '%s', please set up first." % confpath)

    # Section is case sensitive.
    if section.isupper():
        with ConfigUpdater(os.path.join(confpath,
                                        constants.CONFILE_NAME)) as config:
            # If not found, raise NoSectionError or NoOptionError.
            try:
                old_value, comment = config.get(section, option)
            except (NoSectionError, NoOptionError):
                raise ConfigSettingError('Not found the parameter %s-%s.' %
                                         (section, option))
            valid, reason = check_config_validity(section, option, value)
            if not valid:
                raise ConfigSettingError('Incorrect value due to %s.' % reason)
            # If user wants to change password, we should encrypt the plain-text password first.
            if 'password' in option:
                # dynamic_config_xxx searches file from current working directory.
                os.chdir(confpath)
                s1 = dynamic_config_get('dbmind_config', 'cipher_s1')
                s2 = dynamic_config_get('dbmind_config', 'cipher_s2')
                # Every time a new password is generated, update the IV.
                iv = security.generate_an_iv()
                dynamic_config_set('iv_table', '%s-%s' % (section, option), iv)
                cipher = security.encrypt(s1, s2, iv, value)
                value = ENCRYPTED_SIGNAL + cipher
            config.set(section, option, value, comment)
    elif section.islower():
        # dynamic_config_xxx searches file from current working directory.
        os.chdir(confpath)
        try:
            old_value = dynamic_config_get(section, option)
        except ValueError:
            raise ConfigSettingError('Not found the parameter %s-%s.' %
                                     (section, option))
        if not old_value:
            raise ConfigSettingError('Not found the parameter %s-%s.' %
                                     (section, option))
        dynamic_config_set(section, option, value)
    else:
        # If run here, it seems that the format of section string is not correct.
        raise ConfigSettingError(
            '%s is an incorrect section. '
            'Please take note that section string is case sensitive.' %
            section)

    write_to_terminal('Success to modify parameter %s-%s.' % (section, option),
                      color='green')
def test_dynamic_config_db():
    create_dynamic_config_schema()
    assert dynamic_config_get('slow_sql_threshold', 'cpu_usage_limit') == 0.5
    dynamic_config_set('slow_sql_threshold', 'cpu_usage_limit', 1)
    assert dynamic_config_get('slow_sql_threshold', 'cpu_usage_limit') == 1

    dynamic_config_set('slow_sql_threshold', 'no_this_name', 1)
    assert dynamic_config_get('slow_sql_threshold', 'no_this_name') == 1

    try:
        dynamic_config_set('no_this_table', 'no_this_name', 1)
    except AssertionError:
        pass
    else:
        assert False
 def get(*args, **kwargs):
     return dynamic_config_get(*args, **kwargs)
Пример #5
0
def initialize_and_check_config(confpath, interactive=False):
    if not os.path.exists(confpath):
        raise SetupError('Not found the directory %s.' % confpath)
    confpath = os.path.realpath(confpath)  # in case of dir changed.
    os.chdir(confpath)
    dbmind_conf_path = os.path.join(confpath, constants.CONFILE_NAME)
    dynamic_config_path = os.path.join(confpath, constants.DYNAMIC_CONFIG)

    def _create_dynamic_config_schema_and_generate_keys():
        utils.write_to_terminal(
            'Starting to generate a dynamic config file...', color='green')
        create_dynamic_config_schema()
        s1_ = security.safe_random_string(16)
        s2_ = security.safe_random_string(16)
        dynamic_config_set('dbmind_config', 'cipher_s1', s1_)
        dynamic_config_set('dbmind_config', 'cipher_s2', s2_)
        return s1_, s2_

    if not os.path.exists(dynamic_config_path):
        # If dynamic config file does not exist, create a new one.
        s1, s2 = _create_dynamic_config_schema_and_generate_keys()
    else:
        # If exists, need not create a new dynamic config file
        # and directly load hash key s1 and s2 from it.
        s1 = dynamic_config_get('dbmind_config', 'cipher_s1')
        s2 = dynamic_config_get('dbmind_config', 'cipher_s2')
        if not (s1 and s2):
            # If s1 or s2 is invalid, it indicates that an broken event may occurred while generating
            # the dynamic config file. Hence, the whole process of generation is unreliable and we have to
            # generate a new dynamic config file.
            os.unlink(dynamic_config_path)
            s1, s2 = _create_dynamic_config_schema_and_generate_keys()

    # Check some configurations and encrypt passwords.
    with ConfigUpdater(dbmind_conf_path) as config:
        if not interactive:
            for section, section_comment in config.sections(SKIP_LIST):
                for option, value, inline_comment in config.items(section):
                    valid, invalid_reason = check_config_validity(
                        section, option, value)
                    if not valid:
                        raise SetupError(
                            "Wrong %s-%s in the file dbmind.conf due to '%s'. Please revise it."
                            % (section, option, invalid_reason))

        utils.write_to_terminal(
            'Starting to encrypt the plain-text passwords in the config file...',
            color='green')
        for section, section_comment in config.sections(SKIP_LIST):
            for option, value, inline_comment in config.items(section):
                if 'password' in option and value != NULL_TYPE:
                    # Skip when the password has encrypted.
                    if value.startswith(ENCRYPTED_SIGNAL):
                        continue
                    # Every time a new password is generated, update the IV.
                    iv = security.generate_an_iv()
                    dynamic_config_set('iv_table', '%s-%s' % (section, option),
                                       iv)
                    cipher_text = security.encrypt(s1, s2, iv, value)
                    # Use a signal ENCRYPTED_SIGNAL to mark the password that has been encrypted.
                    decorated_cipher_text = ENCRYPTED_SIGNAL + cipher_text
                    config.set(section, option, decorated_cipher_text,
                               inline_comment)

    # config and initialize meta-data database.
    utils.write_to_terminal(
        'Starting to initialize and check the essential variables...',
        color='green')
    global_vars.dynamic_configs = DynamicConfig
    global_vars.configs = load_sys_configs(constants.CONFILE_NAME)
    utils.write_to_terminal(
        'Starting to connect to meta-database and create tables...',
        color='green')
    try:
        create_metadatabase_schema(check_first=False)
        utils.write_to_terminal('The setup process finished successfully.',
                                color='green')
    except DuplicateTableError:
        utils.write_to_terminal(
            'The given database has duplicate tables. '
            'If you want to reinitialize the database, press [R]. '
            'If you want to keep the existent tables, press [K].',
            color='red')
        input_char = ''
        while input_char not in ('R', 'K'):
            input_char = input(
                'Press [R] to reinitialize; Press [K] to keep and ignore:'
            ).upper()
        if input_char == 'R':
            utils.write_to_terminal(
                'Starting to drop existent tables in meta-database...',
                color='green')
            destroy_metadatabase()
            utils.write_to_terminal(
                'Starting to create tables for meta-database...',
                color='green')
            create_metadatabase_schema(check_first=True)
        if input_char == 'K':
            utils.write_to_terminal('Ignoring...', color='green')
        utils.write_to_terminal('The setup process finished successfully.',
                                color='green')
    except SQLExecutionError:
        utils.write_to_terminal(
            'Failed to link metadatabase due to unknown error, '
            'please check the database and its configuration.',
            color='red')