OCSF Library
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
-
AttributeError: 'Extension' object has no attribute 'description'
cause You are attempting to access the `description` attribute of an `OCSFExtension` object after upgrading to `ocsf-lib` v0.10.0 or newer.fixThe `description` attribute was renamed to `caption`. Replace `extension.description` with `extension.caption`. -
TypeError: '<' not supported between instances of 'NoneType' and 'str'
cause This or similar errors (e.g., related to `yaml.YAMLError`) can occur if you're trying to load OCSF extensions from YAML files using `OCSFExtension.from_file()` after upgrading to `ocsf-lib` v0.9.0 or newer.fixAs of v0.9.0, OCSF extension files must be in TOML format. Convert your `.yaml` extension files to `.toml` format. -
jsonschema.ValidationError: 'type' is a required property
cause The OCSF event dictionary you are trying to validate against the schema is missing a required field, or a field is malformed. This specific error indicates a missing `type` property (which is often nested).fixCarefully review the OCSF schema documentation for the event type you are trying to create. Ensure all required fields are present, correctly named, and conform to the expected data types. Inspect the `e.path` attribute of the `ValidationError` for the exact location of the missing/incorrect field. -
ModuleNotFoundError: No module named 'ocsf_lib.schema.v1_0_0'
cause You are attempting to directly import a specific version of the OCSF schema, e.g., `from ocsf_lib.schema.v1_0_0 import OCSFSchema`.fixThe OCSF schema versions are loaded internally by the `OCSFSchema` class. Instantiate `OCSFSchema` and pass the desired version as a parameter if needed: `schema = OCSFSchema(version='1.0.0-rc.3')` or simply `schema = OCSFSchema()` for the latest.
Warnings
- breaking The `Extension.description` property was renamed to `Extension.caption` to align with the OCSF Schema specification.
- breaking OCSF extension files must now be in TOML format instead of YAML. The `pyyaml` dependency was removed and replaced with `tomli`/`tomli_w`.
- breaking The `Schema.validate` method now raises `jsonschema.ValidationError` for invalid events instead of the custom `OCSFError`.
- breaking The library switched its internal data modeling to Pydantic v2, which introduced many breaking changes to Pydantic's API.
- gotcha When instantiating `OCSFSchema`, the library will automatically download the schema files if not found locally. This requires an internet connection on the first run or if schema cache is cleared.
Install
-
pip install ocsf-lib
Imports
- OCSFSchema
from ocsf_lib.schema import OCSFSchema
- OCSFEvent
from ocsf_lib.events import OCSFEvent
- OCSFExtension
from ocsf_lib.extensions import OCSFExtension
- OCSFError
from ocsf_lib.exceptions import OCSFError
Quickstart
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}")