コード例 #1
0
def validate_private_objects(value: List[str]) -> List[str]:
    """Check that the private objects are reasonable

    :param value: the config value to check
    :returns: the list of private objects
    :raises: validate.ValidateError
    """
    value = validate.is_string_list(value)
    for obj in value:
        if re.search("[^a-z0-9-]", obj, re.IGNORECASE):
            raise validate.ValidateError(
                'Private objects may only contain letters, digits and the'
                ' \"-\" character.')
        if obj.startswith("-") or obj.endswith("-"):
            raise validate.ValidateError(
                "A \"-\" in a private object label must be at least "
                "surrounded by one letter or digit.")
    return value
コード例 #2
0
def get_credentials_config(filename=DEFAULT_FILENAME_SYSTEM_API_CREDENTIALS,
                           log=lambda message: None):
    credentials_config = get_config(filename, CREDENTIAL_SPEC, log)

    if not credentials_config[CREDENTIAL_KEY_KEY] or not credentials_config[
            CREDENTIAL_KEY_SECRET]:
        message = "Invalid key/secret in API credential file (%s)" % filename
        log(message)
        raise validate.ValidateError(message)

    return credentials_config
コード例 #3
0
    def _parse_result(self, result):
        u"""
        This method parses validation results.
        If result is True, then do nothing.
        if include even one false to result,
        this method parse result and raise Exception.
        """
        if result is not True:
            for section, errors in result.iteritems():
                for key, value in errors.iteritems():
                    if value is not True:
                        message = (
                            '"{0}" option in [{1}] is invalid value. {2}'
                            ''.format(key, section, value))
                        print(message)

            err_message = ('Some options are invalid!!! Please see the log!!!')
            raise validate.ValidateError(err_message)

        else:
            return True
コード例 #4
0
def is_logging_level(value):
    """
    Coerces a string to an integer logging level which
    maps to a standard python logging level
    """
    std_levels = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'critical': logging.CRITICAL
    }

    try:
        level = value.lower().strip()
    except:
        raise validate.VdtTypeError(value)

    if not std_levels.get(level):
        raise validate.ValidateError(value)

    return std_levels.get(level)
コード例 #5
0
def validate_command(value: List[str]) -> List[str]:
    """Special validator to check shell commands

    The input must either be a list of strings or a string that shlex.split can
    parse into such.

    :param value: the config value to validate
    :returns: the command after validation
    :raises: validate.ValidateError
    """
    logger.debug("validating %s", value)
    try:
        return validate.is_string_list(value)
    except validate.VdtTypeError:
        logger.debug('continue with %s', value)
        if isinstance(value, str):
            try:
                return shlex.split(value)
            except ValueError as err:
                raise validate.ValidateError(
                    'Error when parsing shell command "{}": {}'.format(
                        value, err))
        raise
コード例 #6
0
                                     preserve_errors=True)

        if validation != True:
            messages = [
                "Problem validating configuration file ('%s')" % filename
            ]

            for section_list, key, result in configobj.flatten_errors(
                    config, validation):
                messages.append(
                    "The '%s' key in section '%s' failed to validate (%s)" %
                    (key, ','.join(section_list) if section_list else
                     'default', result if result else 'Missing'))

            map(log, messages)
            raise validate.ValidateError('\n'.join(messages))

    return config


def get_credentials_config(filename=DEFAULT_FILENAME_SYSTEM_API_CREDENTIALS,
                           log=lambda message: None):
    credentials_config = get_config(filename, CREDENTIAL_SPEC, log)

    if not credentials_config[CREDENTIAL_KEY_KEY] or not credentials_config[
            CREDENTIAL_KEY_SECRET]:
        message = "Invalid key/secret in API credential file (%s)" % filename
        log(message)
        raise validate.ValidateError(message)

    return credentials_config