ocsf-json-schema 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ocsf_json_schema-1.0.0/.gitignore +10 -0
- ocsf_json_schema-1.0.0/LICENSE +21 -0
- ocsf_json_schema-1.0.0/PKG-INFO +234 -0
- ocsf_json_schema-1.0.0/README.md +224 -0
- ocsf_json_schema-1.0.0/pyproject.toml +64 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/__about__.py +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/__init__.py +26 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/embedded.py +113 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/loader.py +36 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.0.0-rc.2.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.0.0-rc.3.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.0.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.1.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.2.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.3.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.4.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/1.5.0.json +1 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/LICENSE.txt +177 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/ocsf/README.md +41 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/pickle_it.py +39 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/schema.py +317 -0
- ocsf_json_schema-1.0.0/src/ocsf_json_schema/utility.py +16 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Neil Smith
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ocsf-json-schema
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A Python library for generating OCSF JSON schemas.
|
|
5
|
+
Author-email: Neil Smith <neil@nsmith.net>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# OCSF JSON Schema
|
|
12
|
+
|
|
13
|
+
> [!NOTE]
|
|
14
|
+
> This is a work in progress—primarily for my own learning about OCSF. Please treat this as beta-grade code.
|
|
15
|
+
|
|
16
|
+
## Overview
|
|
17
|
+
|
|
18
|
+
This project provides a tool for generating [JSON Schema](https://json-schema.org/draft/2020-12) files that can
|
|
19
|
+
be used for validating instances of events that follow the Open Cybersecurity Schema Framework.
|
|
20
|
+
|
|
21
|
+
## Project Goal
|
|
22
|
+
|
|
23
|
+
The goal of this project is to generate OCSF JSON Schema files locally using Python, eliminating the need to download them from [schema.ocsf.io](https://schema.ocsf.io). Given the large number of possible variations across OCSF versions, classes, objects, and profiles, this approach provides a more efficient way to validate events—especially when dealing with diverse inputs. It also reduces reliance on external servers, making validation both faster and more sustainable.
|
|
24
|
+
|
|
25
|
+
OCSF JSON Schemas are complex, consisting of numerous interrelated classes and objects spanning multiple schema versions. Additionally, the structure must adapt based on the selected OCSF profiles. This tool generates schemas that closely align with those from [schema.ocsf.io](https://schema.ocsf.io) but introduces key differences:
|
|
26
|
+
|
|
27
|
+
- **Explicit JSON Schema Draft Version**: We explicitly define [JSON Schema Draft 2020-12](https://json-schema.org/draft/2020-12) and validate our outputs against it. The schemas from `schema.ocsf.io` do not specify a draft version.
|
|
28
|
+
- **Extended Features**: We add support for the `deprecated` flag, as well as the `at_least_one` and `just_one` constraints.
|
|
29
|
+
- **Absolute `$id` References**: Classes and objects are assigned absolute `$id` values corresponding to their canonical URIs on `schema.ocsf.io`, ensuring consistency.
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## OCSF Version
|
|
33
|
+
The following OCSF versions are packaged for convenience in [ocsf_json_schema/ocsf/](ocsf_json_schema/ocsf/). Please also see
|
|
34
|
+
the [README](ocsf_json_schema/ocsf/README.md) for details on generating the Picket version of the schema files, which
|
|
35
|
+
can give a slight performance boost.
|
|
36
|
+
|
|
37
|
+
- 1.0.0
|
|
38
|
+
- 1.0.0-rc.2
|
|
39
|
+
- 1.0.0-rc.3
|
|
40
|
+
- 1.1.0
|
|
41
|
+
- 1.2.0
|
|
42
|
+
- 1.3.0
|
|
43
|
+
- 1.4.0
|
|
44
|
+
- 1.5.0
|
|
45
|
+
|
|
46
|
+
You can also [bring your own schema](#bring-your-own-schema) if required or desired.
|
|
47
|
+
|
|
48
|
+
## Setup
|
|
49
|
+
|
|
50
|
+
Requires Python 3.10 or above. There are no other dependencies needed for normal use.
|
|
51
|
+
|
|
52
|
+
To run the tests, install the dev dependencies (`pytest`, `pytest-cov` & `jsonschema`).
|
|
53
|
+
```shell
|
|
54
|
+
pip install -e '.[dev]'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Tests can be run with
|
|
58
|
+
```shell
|
|
59
|
+
pytest
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
### Lookup class schemas
|
|
65
|
+
|
|
66
|
+
This will generate the JSON schema for:
|
|
67
|
+
- OCSF Schema 1.4.0
|
|
68
|
+
- The 'authentication' class (3002)
|
|
69
|
+
- With the 'cloud' and 'datetime' profiles
|
|
70
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import json
|
|
74
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
75
|
+
|
|
76
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
77
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
78
|
+
|
|
79
|
+
# If you only have the class_uid, you can look up the class_name.
|
|
80
|
+
class_name = ocsf_schema.lookup_class_name_from_uid(class_uid=3002)
|
|
81
|
+
|
|
82
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
83
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
84
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
85
|
+
class_name=class_name, profiles=['cloud', 'datetime']
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
# See what was generated.
|
|
89
|
+
print(json.dumps(json_schema, indent=2))
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
If you don't want objects embedded, i.e. you want the schema only for the class itself, you can
|
|
93
|
+
use the `OcsfJsonSchema` rather than `OcsfJsonSchemaEmbedded`.
|
|
94
|
+
|
|
95
|
+
### Lookup object schemas
|
|
96
|
+
|
|
97
|
+
This will generate the JSON schema for:
|
|
98
|
+
- OCSF Schema 1.4.0
|
|
99
|
+
- The 'metadata' object
|
|
100
|
+
- With the 'cloud' and 'datetime' profiles
|
|
101
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import json
|
|
105
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
106
|
+
|
|
107
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
108
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
109
|
+
|
|
110
|
+
# Returns the JSON schema for the 'metadata' object,
|
|
111
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
112
|
+
json_schema = ocsf_schema.get_object_schema(
|
|
113
|
+
object_name='metadata', profiles=['cloud', 'datetime']
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
# See what was generated.
|
|
117
|
+
print(json.dumps(json_schema, indent=2))
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
If you don't want other objects embedded, i.e. you want the schema only for the object itself, you can
|
|
121
|
+
use the `OcsfJsonSchema` rather than `OcsfJsonSchemaEmbedded`.
|
|
122
|
+
|
|
123
|
+
### Lookup class or object schemas by their URI
|
|
124
|
+
|
|
125
|
+
Absolute OCSF schema URIs look like:
|
|
126
|
+
- https://schema.ocsf.io/schema/1.4.0/classes/authentication?profiles=cloud,datetime
|
|
127
|
+
- https://schema.ocsf.io/schema/1.4.0/objects/metadata?profiles=cloud,datetime
|
|
128
|
+
|
|
129
|
+
This will generate the JSON schema for:
|
|
130
|
+
- OCSF Schema 1.4.0
|
|
131
|
+
- The 'authentication' class (3002)
|
|
132
|
+
- With the 'cloud' and 'datetime' profiles
|
|
133
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
import json
|
|
137
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
138
|
+
|
|
139
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
140
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
141
|
+
|
|
142
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
143
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
144
|
+
json_schema = ocsf_schema.get_schema_from_uri(
|
|
145
|
+
uri="https://schema.ocsf.io/schema/1.4.0/classes/authentication?profiles=cloud,datetime"
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# See what was generated.
|
|
149
|
+
print(json.dumps(json_schema, indent=2))
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Bring your own schema
|
|
153
|
+
|
|
154
|
+
If you want to use a version of the OCSF schema that's not packaged (`-dev` instances, for example), then you can
|
|
155
|
+
being your own schema.
|
|
156
|
+
|
|
157
|
+
For example, download the dev schema:
|
|
158
|
+
```shell
|
|
159
|
+
curl -o 1.6.0-dev.json https://schema.ocsf.io/1.6.0-dev/export/schema
|
|
160
|
+
```
|
|
161
|
+
Then
|
|
162
|
+
```python
|
|
163
|
+
import json
|
|
164
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
165
|
+
|
|
166
|
+
with open("1.6.0-dev.json", 'r') as file:
|
|
167
|
+
schema_from_file = json.load(file)
|
|
168
|
+
|
|
169
|
+
# Loads the version of the OCSF Schema from the above file.
|
|
170
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(schema_from_file)
|
|
171
|
+
|
|
172
|
+
# If you only have the class_uid, you can lookup the class_name.
|
|
173
|
+
class_name = ocsf_schema.lookup_class_name_from_uid(class_uid=3002)
|
|
174
|
+
|
|
175
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
176
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
177
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
178
|
+
class_name=class_name, profiles=['cloud', 'datetime']
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
# See what was generated.
|
|
182
|
+
print(json.dumps(json_schema, indent=2))
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Validate an OCSF log against the schema
|
|
186
|
+
|
|
187
|
+
> [!NOTE]
|
|
188
|
+
> The validation process itself is outside the scope of this project, but here's an example of how you _could_ do it.
|
|
189
|
+
> `jsonschema` isn't a dependency of `ocsf-json-schema`, so you'll need to install it yourself if you wish to use it.
|
|
190
|
+
|
|
191
|
+
The generated JSON Schema files can be used with any JSON validator that supports 2020-12. Python's `jsonschema`, for example.
|
|
192
|
+
|
|
193
|
+
Assuming you have an instance of a OCSF event in the file `authentication.log.json`:
|
|
194
|
+
```python
|
|
195
|
+
import json
|
|
196
|
+
from jsonschema import validate, exceptions
|
|
197
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
198
|
+
|
|
199
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
200
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
201
|
+
|
|
202
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
203
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
204
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
205
|
+
class_name='authentication', profiles=['cloud', 'datetime']
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
with open("authentication.log.json", 'r') as file:
|
|
209
|
+
log_file = json.load(file)
|
|
210
|
+
|
|
211
|
+
try:
|
|
212
|
+
# An exception is raised if the log file's schema is not as expected.
|
|
213
|
+
validate(instance=log_file, schema=json_schema)
|
|
214
|
+
print("Log's schema is valid.")
|
|
215
|
+
except exceptions.SchemaError as e:
|
|
216
|
+
print(f"Log's schema is invalid: {e}")
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Notes on validation
|
|
220
|
+
|
|
221
|
+
- `null` values are not supported and, if present, will likely result in the validation failing. If a value is `null`, the key/value pair should be removed before validation. This aligns with the JSON Schema files from [schema.ocsf.io](https://schema.ocsf.io).
|
|
222
|
+
- If you are validating a file that was previously parquet, be careful of fields that should be a dictionary, but may have been converted to a list of tuples. The validator will expect these fields to be a dictionary. This will most likely occur for fields of type `object`. For example, `unmapped`.
|
|
223
|
+
|
|
224
|
+
## Build
|
|
225
|
+
|
|
226
|
+
This repo can be built as a package with the following.
|
|
227
|
+
```shell
|
|
228
|
+
pip install build
|
|
229
|
+
rm ocsf_json_schema/ocsf/*.pkl
|
|
230
|
+
python -m build
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
# Licence
|
|
234
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# OCSF JSON Schema
|
|
2
|
+
|
|
3
|
+
> [!NOTE]
|
|
4
|
+
> This is a work in progress—primarily for my own learning about OCSF. Please treat this as beta-grade code.
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
This project provides a tool for generating [JSON Schema](https://json-schema.org/draft/2020-12) files that can
|
|
9
|
+
be used for validating instances of events that follow the Open Cybersecurity Schema Framework.
|
|
10
|
+
|
|
11
|
+
## Project Goal
|
|
12
|
+
|
|
13
|
+
The goal of this project is to generate OCSF JSON Schema files locally using Python, eliminating the need to download them from [schema.ocsf.io](https://schema.ocsf.io). Given the large number of possible variations across OCSF versions, classes, objects, and profiles, this approach provides a more efficient way to validate events—especially when dealing with diverse inputs. It also reduces reliance on external servers, making validation both faster and more sustainable.
|
|
14
|
+
|
|
15
|
+
OCSF JSON Schemas are complex, consisting of numerous interrelated classes and objects spanning multiple schema versions. Additionally, the structure must adapt based on the selected OCSF profiles. This tool generates schemas that closely align with those from [schema.ocsf.io](https://schema.ocsf.io) but introduces key differences:
|
|
16
|
+
|
|
17
|
+
- **Explicit JSON Schema Draft Version**: We explicitly define [JSON Schema Draft 2020-12](https://json-schema.org/draft/2020-12) and validate our outputs against it. The schemas from `schema.ocsf.io` do not specify a draft version.
|
|
18
|
+
- **Extended Features**: We add support for the `deprecated` flag, as well as the `at_least_one` and `just_one` constraints.
|
|
19
|
+
- **Absolute `$id` References**: Classes and objects are assigned absolute `$id` values corresponding to their canonical URIs on `schema.ocsf.io`, ensuring consistency.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## OCSF Version
|
|
23
|
+
The following OCSF versions are packaged for convenience in [ocsf_json_schema/ocsf/](ocsf_json_schema/ocsf/). Please also see
|
|
24
|
+
the [README](ocsf_json_schema/ocsf/README.md) for details on generating the Picket version of the schema files, which
|
|
25
|
+
can give a slight performance boost.
|
|
26
|
+
|
|
27
|
+
- 1.0.0
|
|
28
|
+
- 1.0.0-rc.2
|
|
29
|
+
- 1.0.0-rc.3
|
|
30
|
+
- 1.1.0
|
|
31
|
+
- 1.2.0
|
|
32
|
+
- 1.3.0
|
|
33
|
+
- 1.4.0
|
|
34
|
+
- 1.5.0
|
|
35
|
+
|
|
36
|
+
You can also [bring your own schema](#bring-your-own-schema) if required or desired.
|
|
37
|
+
|
|
38
|
+
## Setup
|
|
39
|
+
|
|
40
|
+
Requires Python 3.10 or above. There are no other dependencies needed for normal use.
|
|
41
|
+
|
|
42
|
+
To run the tests, install the dev dependencies (`pytest`, `pytest-cov` & `jsonschema`).
|
|
43
|
+
```shell
|
|
44
|
+
pip install -e '.[dev]'
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Tests can be run with
|
|
48
|
+
```shell
|
|
49
|
+
pytest
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Lookup class schemas
|
|
55
|
+
|
|
56
|
+
This will generate the JSON schema for:
|
|
57
|
+
- OCSF Schema 1.4.0
|
|
58
|
+
- The 'authentication' class (3002)
|
|
59
|
+
- With the 'cloud' and 'datetime' profiles
|
|
60
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
import json
|
|
64
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
65
|
+
|
|
66
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
67
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
68
|
+
|
|
69
|
+
# If you only have the class_uid, you can look up the class_name.
|
|
70
|
+
class_name = ocsf_schema.lookup_class_name_from_uid(class_uid=3002)
|
|
71
|
+
|
|
72
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
73
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
74
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
75
|
+
class_name=class_name, profiles=['cloud', 'datetime']
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# See what was generated.
|
|
79
|
+
print(json.dumps(json_schema, indent=2))
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
If you don't want objects embedded, i.e. you want the schema only for the class itself, you can
|
|
83
|
+
use the `OcsfJsonSchema` rather than `OcsfJsonSchemaEmbedded`.
|
|
84
|
+
|
|
85
|
+
### Lookup object schemas
|
|
86
|
+
|
|
87
|
+
This will generate the JSON schema for:
|
|
88
|
+
- OCSF Schema 1.4.0
|
|
89
|
+
- The 'metadata' object
|
|
90
|
+
- With the 'cloud' and 'datetime' profiles
|
|
91
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import json
|
|
95
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
96
|
+
|
|
97
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
98
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
99
|
+
|
|
100
|
+
# Returns the JSON schema for the 'metadata' object,
|
|
101
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
102
|
+
json_schema = ocsf_schema.get_object_schema(
|
|
103
|
+
object_name='metadata', profiles=['cloud', 'datetime']
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# See what was generated.
|
|
107
|
+
print(json.dumps(json_schema, indent=2))
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
If you don't want other objects embedded, i.e. you want the schema only for the object itself, you can
|
|
111
|
+
use the `OcsfJsonSchema` rather than `OcsfJsonSchemaEmbedded`.
|
|
112
|
+
|
|
113
|
+
### Lookup class or object schemas by their URI
|
|
114
|
+
|
|
115
|
+
Absolute OCSF schema URIs look like:
|
|
116
|
+
- https://schema.ocsf.io/schema/1.4.0/classes/authentication?profiles=cloud,datetime
|
|
117
|
+
- https://schema.ocsf.io/schema/1.4.0/objects/metadata?profiles=cloud,datetime
|
|
118
|
+
|
|
119
|
+
This will generate the JSON schema for:
|
|
120
|
+
- OCSF Schema 1.4.0
|
|
121
|
+
- The 'authentication' class (3002)
|
|
122
|
+
- With the 'cloud' and 'datetime' profiles
|
|
123
|
+
- Including the schema of all referenced OCSF objects embedded.
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
import json
|
|
127
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
128
|
+
|
|
129
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
130
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
131
|
+
|
|
132
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
133
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
134
|
+
json_schema = ocsf_schema.get_schema_from_uri(
|
|
135
|
+
uri="https://schema.ocsf.io/schema/1.4.0/classes/authentication?profiles=cloud,datetime"
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
# See what was generated.
|
|
139
|
+
print(json.dumps(json_schema, indent=2))
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Bring your own schema
|
|
143
|
+
|
|
144
|
+
If you want to use a version of the OCSF schema that's not packaged (`-dev` instances, for example), then you can
|
|
145
|
+
being your own schema.
|
|
146
|
+
|
|
147
|
+
For example, download the dev schema:
|
|
148
|
+
```shell
|
|
149
|
+
curl -o 1.6.0-dev.json https://schema.ocsf.io/1.6.0-dev/export/schema
|
|
150
|
+
```
|
|
151
|
+
Then
|
|
152
|
+
```python
|
|
153
|
+
import json
|
|
154
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
155
|
+
|
|
156
|
+
with open("1.6.0-dev.json", 'r') as file:
|
|
157
|
+
schema_from_file = json.load(file)
|
|
158
|
+
|
|
159
|
+
# Loads the version of the OCSF Schema from the above file.
|
|
160
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(schema_from_file)
|
|
161
|
+
|
|
162
|
+
# If you only have the class_uid, you can lookup the class_name.
|
|
163
|
+
class_name = ocsf_schema.lookup_class_name_from_uid(class_uid=3002)
|
|
164
|
+
|
|
165
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
166
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
167
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
168
|
+
class_name=class_name, profiles=['cloud', 'datetime']
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# See what was generated.
|
|
172
|
+
print(json.dumps(json_schema, indent=2))
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Validate an OCSF log against the schema
|
|
176
|
+
|
|
177
|
+
> [!NOTE]
|
|
178
|
+
> The validation process itself is outside the scope of this project, but here's an example of how you _could_ do it.
|
|
179
|
+
> `jsonschema` isn't a dependency of `ocsf-json-schema`, so you'll need to install it yourself if you wish to use it.
|
|
180
|
+
|
|
181
|
+
The generated JSON Schema files can be used with any JSON validator that supports 2020-12. Python's `jsonschema`, for example.
|
|
182
|
+
|
|
183
|
+
Assuming you have an instance of a OCSF event in the file `authentication.log.json`:
|
|
184
|
+
```python
|
|
185
|
+
import json
|
|
186
|
+
from jsonschema import validate, exceptions
|
|
187
|
+
from ocsf_json_schema import get_ocsf_schema, OcsfJsonSchemaEmbedded
|
|
188
|
+
|
|
189
|
+
# Loads the packaged 1.4.0 version of the OCSF Schema
|
|
190
|
+
ocsf_schema = OcsfJsonSchemaEmbedded(get_ocsf_schema(version='1.4.0'))
|
|
191
|
+
|
|
192
|
+
# Returns the JSON schema for the 'authentication' class,
|
|
193
|
+
# with the 'cloud' and 'datetime' profiles applied.
|
|
194
|
+
json_schema = ocsf_schema.get_class_schema(
|
|
195
|
+
class_name='authentication', profiles=['cloud', 'datetime']
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
with open("authentication.log.json", 'r') as file:
|
|
199
|
+
log_file = json.load(file)
|
|
200
|
+
|
|
201
|
+
try:
|
|
202
|
+
# An exception is raised if the log file's schema is not as expected.
|
|
203
|
+
validate(instance=log_file, schema=json_schema)
|
|
204
|
+
print("Log's schema is valid.")
|
|
205
|
+
except exceptions.SchemaError as e:
|
|
206
|
+
print(f"Log's schema is invalid: {e}")
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Notes on validation
|
|
210
|
+
|
|
211
|
+
- `null` values are not supported and, if present, will likely result in the validation failing. If a value is `null`, the key/value pair should be removed before validation. This aligns with the JSON Schema files from [schema.ocsf.io](https://schema.ocsf.io).
|
|
212
|
+
- If you are validating a file that was previously parquet, be careful of fields that should be a dictionary, but may have been converted to a list of tuples. The validator will expect these fields to be a dictionary. This will most likely occur for fields of type `object`. For example, `unmapped`.
|
|
213
|
+
|
|
214
|
+
## Build
|
|
215
|
+
|
|
216
|
+
This repo can be built as a package with the following.
|
|
217
|
+
```shell
|
|
218
|
+
pip install build
|
|
219
|
+
rm ocsf_json_schema/ocsf/*.pkl
|
|
220
|
+
python -m build
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
# Licence
|
|
224
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ocsf-json-schema"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "A Python library for generating OCSF JSON schemas."
|
|
9
|
+
authors = [{ name = "Neil Smith", email = "neil@nsmith.net" }]
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICEN[CS]E*"]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.10"
|
|
14
|
+
dependencies = [] # No dependencies for normal use
|
|
15
|
+
|
|
16
|
+
[tool.hatch.version]
|
|
17
|
+
path = "src/ocsf_json_schema/__about__.py"
|
|
18
|
+
|
|
19
|
+
[tool.hatch.envs.dev]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"ruff",
|
|
22
|
+
"pytest",
|
|
23
|
+
"pytest-mock",
|
|
24
|
+
"pytest-cov",
|
|
25
|
+
"jsonschema",
|
|
26
|
+
]
|
|
27
|
+
[tool.hatch.envs.dev.scripts]
|
|
28
|
+
fmt = [
|
|
29
|
+
"ruff check --select I --fix .",
|
|
30
|
+
"ruff format ."
|
|
31
|
+
]
|
|
32
|
+
listbuild = [
|
|
33
|
+
"tar -tzf dist/*.tar.gz",
|
|
34
|
+
"unzip -l dist/*.whl"
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.sdist]
|
|
38
|
+
exclude = [
|
|
39
|
+
"/dist",
|
|
40
|
+
"/tests",
|
|
41
|
+
"/.github",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
[tool.hatch.build.targets.wheel]
|
|
45
|
+
packages = ["src/ocsf_json_schema"]
|
|
46
|
+
|
|
47
|
+
[tool.coverage.run]
|
|
48
|
+
source_pkgs = ["ocsf_json_schema", "tests"]
|
|
49
|
+
branch = true
|
|
50
|
+
parallel = true
|
|
51
|
+
omit = [
|
|
52
|
+
"src/ocsf_json_schema/__about__.py",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.coverage.paths]
|
|
56
|
+
ocsf_json_schema = ["src/ocsf_json_schema", "*/ocsf-json-schema/src/ocsf_json_schema"]
|
|
57
|
+
tests = ["tests", "*/ocsf-json-schema/tests"]
|
|
58
|
+
|
|
59
|
+
[tool.coverage.report]
|
|
60
|
+
exclude_lines = [
|
|
61
|
+
"no cov",
|
|
62
|
+
"if __name__ == .__main__.:",
|
|
63
|
+
"if TYPE_CHECKING:",
|
|
64
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ocsf_json_schema
|
|
3
|
+
----------------
|
|
4
|
+
A Python library for handling OCSF JSON schemas.
|
|
5
|
+
|
|
6
|
+
Exports:
|
|
7
|
+
- OcsfJsonSchema: The main class for schema processing.
|
|
8
|
+
- load_ocsf_schema_json: Function to load schema from a JSON file.
|
|
9
|
+
- load_ocsf_schema_pickle: Function to load schema from a Pickle file.
|
|
10
|
+
- get_ocsf_schema: Function to retrieve schema based on version.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .schema import OcsfJsonSchema
|
|
14
|
+
from .embedded import OcsfJsonSchemaEmbedded
|
|
15
|
+
from .loader import load_ocsf_schema_json, load_ocsf_schema_pickle, get_ocsf_schema, get_packaged_versions
|
|
16
|
+
from .pickle_it import pickle_it
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"OcsfJsonSchema",
|
|
20
|
+
"OcsfJsonSchemaEmbedded",
|
|
21
|
+
"load_ocsf_schema_json",
|
|
22
|
+
"load_ocsf_schema_pickle",
|
|
23
|
+
"get_ocsf_schema",
|
|
24
|
+
"pickle_it",
|
|
25
|
+
"get_packaged_versions"
|
|
26
|
+
]
|