Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Validate input documents
results = scripts.run_validation(options)
# Print validation results
scripts.print_results(results, options)
# Determine exit status code and exit.
code = scripts.status_code(results)
sys.exit(code)
except scripts.ArgumentError as ex:
if ex.show_help:
parser.print_help()
scripts.error(ex)
except (errors.ValidationError, IOError) as ex:
scripts.error(
"Validation error occurred: '%s'" % str(ex),
codes.EXIT_VALIDATION_ERROR
)
except Exception:
logging.exception("Fatal error occurred")
sys.exit(codes.EXIT_FAILURE)
if stix_package:
xml = stix_package.to_xml(encoding=None)
validator_options.in_files = io.StringIO(xml)
try:
scripts.set_output_level(validator_options)
validation_results = scripts.validate_file(
validator_options.in_files,
validator_options
)
results = {stix_package.id_: validation_results}
# Print stix-validator results
scripts.print_results(results, validator_options)
except (errors.ValidationError, IOError) as ex:
scripts.error(
"Validation error occurred: '%s'" % str(ex),
codes.EXIT_VALIDATION_ERROR
)
except Exception:
log.exception("Fatal error occurred", extra={'ecode': 0})
sys.exit(codes.EXIT_FAILURE)
return xml
if stix_package:
xml = stix_package.to_xml(encoding=None)
validator_options.in_files = io.StringIO(xml)
try:
scripts.set_output_level(validator_options)
validation_results = scripts.validate_file(
validator_options.in_files,
validator_options
)
results = {stix_package.id_: validation_results}
# Print stix-validator results
scripts.print_results(results, validator_options)
except (errors.ValidationError, IOError) as ex:
scripts.error(
"Validation error occurred: '%s'" % str(ex),
codes.EXIT_VALIDATION_ERROR
)
except Exception:
log.exception("Fatal error occurred", extra={'ecode': 0})
sys.exit(codes.EXIT_FAILURE)
return xml
# Validate input documents
results = scripts.run_validation(options)
# Print validation results
scripts.print_results(results, options)
# Determine exit status code and exit.
code = scripts.status_code(results)
sys.exit(code)
except scripts.ArgumentError as ex:
if ex.show_help:
parser.print_help()
scripts.error(ex)
except (errors.ValidationError, IOError) as ex:
scripts.error(
"Validation error occurred: '%s'" % str(ex),
codes.EXIT_VALIDATION_ERROR
)
except Exception:
logging.exception("Fatal error occurred")
sys.exit(codes.EXIT_FAILURE)
Returns:
An instance of
:class:`.XmlValidationResults`.
Raises:
.ValidationError: If the class was not initialized with a
schema directory and `schemaloc` is ``False`` or if there are
any issues parsing `doc`.
.XMLSchemaIncludeError: If an error occurs while processing the
schemas required for validation.
.XMLSchemaIncludeError: If an error occurs while processing
``xs:include`` directives.
"""
if not (schemaloc or self._schemalocs):
raise errors.ValidationError(
"No schemas to validate against! Try instantiating "
"XmlValidator with use_schemaloc=True or setting the "
"schema_dir param in __init__"
)
root = utils.get_etree_root(doc)
xsd = self._build_uber_schema(root, schemaloc)
is_valid = xsd.validate(root)
return XmlValidationResults(is_valid, xsd.error_log)
def validate(self):
"""Checks that this is a valid InstanceMapping instance.
Raises:
errors.ProfileParseError: If ``namespace`` is ``None`` or
any of the selector values are empty.
"""
if not self.label:
err = "Missing type label in Instance Mapping"
raise errors.ProfileParseError(err)
if not self.namespace:
err = "Missing namespace for '{label}'' in Instance Mapping worksheet"
raise errors.ProfileParseError(err.format(label=self.label))
if not (self.selectors and all(self.selectors)):
err = ("Empty selector for '{label}' in Instance Mapping worksheet. "
"Look for extra commas in field.")
raise errors.ProfileParseError(err.format(label=self.label))
def _raise_invalid_version(self, version):
error = (
"Invalid CybOX version number provided or found in input "
"document: '{0}'"
).format(version)
raise errors.InvalidCyboxVersionError(
message=error,
found=version,
expected=common.CYBOX_VERSIONS
)
@errors.setter
def errors(self, value):
if not value:
self._errors = []
elif utils.is_iterable(value):
self._errors = [XmlSchemaError(x) for x in value]
else:
self._errors = [XmlSchemaError(value)]
initialization schema directory is used.
Returns:
An ``etree.XMLSchema`` instance used to validate `doc`.
Raise:
.XMLSchemaImportError: If an error occurred while building the
dictionary of namespace to schemalocation mappings used to
drive the uber schema creation.
"""
root = utils.get_etree_root(doc)
imports = self._build_required_imports(root, schemaloc)
if not imports:
raise errors.XMLSchemaImportError(
"Cannot validate document. Error occurred while determining "
"schemas required for validation."
)
xsd = etree.fromstring(
"""
"""
)
for ns, loc in iteritems(imports):
loc = loc.replace("\\", "/")