Pydantic Models for OCSF

0.0.6 · active · verified Fri Apr 17

ocsf-pydantic provides Pydantic v2 models for the Open Cybersecurity Schema Framework (OCSF). It enables type-safe Python representations of OCSF schemas, facilitating event parsing, validation, and generation in cybersecurity applications. The current version is 0.0.6, and its release cadence is irregular, typically aligned with updates to the OCSF specification or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `FileActivity` OCSF event using the `ocsf-pydantic` models. It populates essential fields like time, correlation ID, file details, and user information, then prints the resulting event in JSON format. It also shows how to access nested fields.

from datetime import datetime, timezone
from ocsf_pydantic.events.file_activity import FileActivity
from ocsf_pydantic.objects.file import File
from ocsf_pydantic.objects.user import User

# Create an OCSF FileActivity event
file_activity_event = FileActivity(
    time=datetime.now(timezone.utc),
    correlation_uid="example-correlation-id-456",
    activity_id=1,  # Represents FileActivityId.CREATE
    file=File(
        name="report.pdf",
        path="/home/user/documents/report.pdf",
        size=10240,
        hash_md5="d41d8cd98f00b204e9800998ecf8427e"
    ),
    user=User(name="analyst_user", uid="U007"),
    message="New report generated by analyst_user"
)

# Print the event as JSON
print(file_activity_event.model_dump_json(indent=2))

# Access a specific field
print(f"\nEvent Type Name: {file_activity_event.activity_name}")
print(f"File Name: {file_activity_event.file.name}")

view raw JSON →