OCSF Library

0.10.4 · active · verified Fri Apr 17

A Python library for working with the Open Cybersecurity Schema Framework (OCSF) JSON schema. It provides tools for validating OCSF events, loading schemas, and managing OCSF extensions. The current version is 0.10.4, and it has an active, though irregular, release cadence with significant updates between minor versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the OCSF schema and validate an example OCSF event against it. It highlights the primary use case of the `ocsf-lib` for ensuring OCSF event compliance.

from ocsf_lib.schema import OCSFSchema
from jsonschema import ValidationError
import json

# An example minimal OCSF event (Process Activity Create)
# This example is simplified; real OCSF events are more complex and follow specific OCSF types.
example_event = {
    "activity_id": 1,
    "activity_name": "Create",
    "category_uid": 1,
    "category_name": "Audit Activity",
    "class_uid": 1001,
    "class_name": "Process Activity",
    "metadata": {
        "product": {
            "name": "MyApplication",
            "vendor_name": "MyVendor",
            "version": "1.0.0"
        },
        "version": "1.0.0-rc.3" # OCSF Schema version this event conforms to
    },
    "severity_id": 1,
    "severity": "Informational",
    "start_time": "2023-10-27T10:00:00Z",
    "time": "2023-10-27T10:00:00Z",
    "type_uid": 100101,
    "type_name": "Process Activity: Create",
    "process": {
        "pid": 1234,
        "name": "example_process",
        "command_line": "/usr/bin/example --flag"
    }
}

try:
    # 1. Load the OCSF schema
    # By default, it loads the latest recommended version. 
    # You can specify a version, e.g., OCSFSchema(version="1.0.0-rc.3")
    schema = OCSFSchema()
    print(f"Successfully loaded OCSF Schema version: {schema.version}")

    # 2. Validate an OCSF event against the loaded schema
    print(f"\nAttempting to validate event:\n{json.dumps(example_event, indent=2)}")
    schema.validate(example_event)
    print("\nSUCCESS: The example event is valid according to the OCSF schema.")

except ValidationError as e:
    print(f"\nVALIDATION ERROR: The event is NOT valid.")
    print(f"  Message: {e.message}")
    print(f"  Path: {list(e.path)}")
    print(f"  Validator: {e.validator} (value: {e.validator_value})")
except Exception as e:
    print(f"\nAn unexpected error occurred: {e}")

view raw JSON →