Ejemplo n.º 1
0
def validate_yaml(yaml_file_path, schema_url, ssl_cert=''):
    try:
        yaml_content = open(yaml_file_path)
    except Exception as err:
        raise Exception('Unable to open file %s:\n%s' %
                        (yaml_file_path, str(err)))

    try:
        yaml_as_object = yaml.load(yaml_content, yaml.FullLoader)
    except ScannerError as err:
        raise Exception('This is not a valid yaml file :\n%s' % str(err))

    if schema_url is None:
        try:
            json_schema = get_yaml_schema()
        except Exception as err:
            raise Exception('Error getting the schema from disk : %s' %
                            str(err))
    else:
        try:
            json_schema = requests.get(
                schema_url, verify=tools.ssl_cert_to_verify(ssl_cert)).text
        except Exception as err:
            raise Exception('Error getting the schema from the url: %s\n%s' %
                            (schema_url, str(err)))
        try:
            update_schema(json_schema)
        except Exception as err:
            raise Exception('Error storing the schema : %s' % str(err))

    try:
        schema_as_object = json.loads(json_schema)
    except JSONDecodeError as err:
        raise Exception('This is not a valid json schema file :\n%s' %
                        str(err))

    v = jsonschema.validators.Draft7Validator(schema_as_object)
    try:
        v.validate(yaml_as_object)
    except jsonschema.SchemaError as err:
        raise Exception('This is not a valid json schema file :\n%s' %
                        str(err))
    except jsonschema.ValidationError as err:
        msgs = ""
        for error in sorted(v.iter_errors(yaml_as_object), key=str):
            path = "\\".join(list(map(lambda x: str(x), error.path)))
            msgs += "\n" + error.message + "\n\tat: " + path + "\n\tgot: \n" + yaml.dump(
                error.instance) + "\n"
        raise ValueError(YAML_NOT_CONFIRM_MESSAGE + '\n' + msgs)
Ejemplo n.º 2
0
def validate_yaml(yaml_file_path, schema_url):
    try:
        yaml_content = open(yaml_file_path)
    except Exception as err:
        raise Exception('Unable to open file %s:\n%s' %
                        (yaml_file_path, str(err)))

    try:
        yaml_as_object = yaml.load(yaml_content, yaml.FullLoader)
    except ScannerError as err:
        raise Exception('This is not a valid yaml file :\n%s' % str(err))

    if schema_url is None:
        try:
            json_schema = get_yaml_schema()
        except Exception as err:
            raise Exception('Error getting the schema from disk : %s' %
                            str(err))
    else:
        try:
            json_schema = requests.get(schema_url).text
        except Exception as err:
            raise Exception('Error getting the schema from the url: %s\n%s' %
                            (schema_url, str(err)))
        try:
            update_schema(json_schema)
        except Exception as err:
            raise Exception('Error storing the schema : %s' % str(err))

    try:
        schema_as_object = json.loads(json_schema)
    except JSONDecodeError as err:
        raise Exception('This is not a valid json schema file :\n%s' %
                        str(err))

    try:
        jsonschema.validate(yaml_as_object, schema_as_object)
    except jsonschema.SchemaError as err:
        raise Exception('This is not a valid json schema file :\n%s' %
                        str(err))
    except jsonschema.ValidationError as err:
        raise Exception(
            'Wrong Yaml structure. Violation of the Neoload schema:\n%s\n\nOn instance:\n%s'
            % (err.message, str(err.instance)))
Ejemplo n.º 3
0
def init_yaml_schema_with_checks(schema_spec, ssl_cert='', check_schema=True):
    json_schema = get_yaml_schema(False)
    if json_schema is not None:
        logging.info('Loaded schema from disk.')
    else:
        logging.warning('No prior cached schema on disk.')

    if not check_schema:
        return json_schema

    # even if there is something local, try checking if it's different from remote
    schema_spec_remote = __default_schema_url
    if schema_spec is None:
        schema_spec = schema_spec_remote
    json_schema_spec = get_json_schema_by_spec(schema_spec, ssl_cert)

    # compare cached to spec/remote
    try:
        logging.info('Comparing cached schema to remote schema')
        hash_disk = "" if json_schema is None else hashlib.sha256(
            json_schema.encode()).hexdigest()
        hash_spec = "" if json_schema_spec is None else hashlib.sha256(
            json_schema_spec.encode()).hexdigest()
        if hash_disk != hash_spec:
            logging.info('Cached schema differs from source!')
            json_schema = json_schema_spec
            update_schema(json_schema_spec)
        else:
            logging.info('No differences between cached and remote schema.')
    except Exception as err:
        logging.warning('Could not update schema cache {}\n{}'.format(
            schema_spec, err))

    if json_schema is None:
        raise bad_as_code_exception.BadAsCodeSchemaException(
            'Could not obtain schema definition therefore could not validate this schema.'
        )

    return json_schema