Skip to main content

OCSF Schema Validation

Project description

OCSF Schema Validator

A utility to validate contributions to the OCSF schema, intended to prevent human error when contributing to the schema in order to keep the schema machine-readable.

OCSF provides several include mechanisms to facilitate reuse, but this means individual schema files may be incomplete. This complicates using off-the-shelf schema definition tools for validation.

Query is a federated search solution that normalizes disparate security data to OCSF. This validator is adapted from active code and documentation generation tools written by the Query team.

Getting Started

Prerequisites

Installation

You can install the validator with pip:

$ pip install ocsf-validator

Usage

You can run the validator against your working copy of the schema to identify problems before submitting a PR. Invoke the validator using python and provide it with the path to the root of your working copy.

Examples:

$ python -m ocsf_validator .
$ python -m ocsf_validator ../ocsf-schema

Tests

The validator performs the following tests on a copy of the schema:

  • The schema is readable and all JSON is valid. [FATAL]
  • The directory structure meets expectations. [WARNING]
  • The targets in $include, profiles, and extends directives can be found. [ERROR]
  • All required attributes in schema definition files are present. [WARNING]
  • There are no unrecognized attributes in schema definition files. [WARNING]
  • All attributes in the attribute dictionary are used. [WARNING]
  • There are no name collisions within a record type. [WARNING]
  • All attributes are defined in the attribute dictionary. [WARNING]

If any ERROR or FATAL tests fail, the validator exits with a non-zero exit code.

Technical Overview

The OCSF metaschema is represented as record types by filepath, achieved as follows:

  1. Record types are represented using Python's type system by defining them as Python TypedDicts in types.py. This allows the validator to take advantage of Python's reflection capabilities.
  2. Files and record types are associated by pattern matching the file paths. These patterns are named in matchers.py to allow mistakes to be caught by a type checker.
  3. Types are mapped to filepath patterns in type_mapping.py.

The contents of the OCSF schema to be validated are primarily represented as a Reader defined in reader.py. Readers load the schema definitions to be validated from a source (usually from a filesystem) and contain them without judgement. The process_includes function and other contents of processor.py mutate the contents of a Reader by applying OCSF's various include mechanisms.

Validators are defined in validators.py and test the schema contents for various problematic conditions. Validators should pass Exceptions to a special error Collector defined in errors.py. This module also defines a number of custom exception types that represent problematic schema states. The Collector raises errors by default, but can also hold them until they're aggregated by a larger validation process (e.g., the ValidationRunner).

The ValidationRunner combines all of the building blocks above to read a proposed schema from a filesystem, validate the schema, and provide useful output and a non-zero exit code if any errors were encountered.

Contributing

After checking out, you'll want to install dependencies:

poetry install

Before committing, run the formatters and tests:

poetry run isort .
poetry run black .
poetry run pyright
poetry run pytest

If you're adding a validator, do the following:

  • Write your validate_ function in validate.py to apply a function to the relevant keys in a reader that will run your desired validation. See validators.py for examples.
  • Add any custom errors in errors.py.
  • Create an option to change its severity level in ValidatorOptions and map it in the constructor of ValidationRunner in runner.py.
  • Invoke the new validator in ValidationRunner.validate.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ocsf_validator-0.2.4.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ocsf_validator-0.2.4-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file ocsf_validator-0.2.4.tar.gz.

File metadata

  • Download URL: ocsf_validator-0.2.4.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.10 Darwin/25.3.0

File hashes

Hashes for ocsf_validator-0.2.4.tar.gz
Algorithm Hash digest
SHA256 47029070e15d87b356f11b63018f1940902fee2a70bdf56cc3eca7f722db895e
MD5 61fd805866285b3c0c3f59d691b47dce
BLAKE2b-256 fe69bc6798746778f5d90f5e4410a87c515a60665de8f4f130ba44f84088d95b

See more details on using hashes here.

File details

Details for the file ocsf_validator-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: ocsf_validator-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.10 Darwin/25.3.0

File hashes

Hashes for ocsf_validator-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 810a8d103025c9f4b2b4cf7d09dbe2d85f0ce450c43856290165a7462211c257
MD5 c5e74fde5b0aef57ae9349117a2a8a5c
BLAKE2b-256 616ed2ab85453bb3b91ed6144b57ce9e18ef266f04a6bff7687dcc4afe6ddf5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page