ocsf-schema-compiler 0.9.2__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_schema_compiler-0.9.2/LICENSE +202 -0
- ocsf_schema_compiler-0.9.2/PKG-INFO +130 -0
- ocsf_schema_compiler-0.9.2/README.md +102 -0
- ocsf_schema_compiler-0.9.2/pyproject.toml +64 -0
- ocsf_schema_compiler-0.9.2/setup.cfg +4 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/__init__.py +1 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/__main__.py +113 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/compiler.py +2558 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/exceptions.py +2 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/jsonish.py +191 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/legacy_mode.py +41 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler/utils.py +98 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/PKG-INFO +130 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/SOURCES.txt +18 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/dependency_links.txt +1 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/entry_points.txt +2 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/requires.txt +4 -0
- ocsf_schema_compiler-0.9.2/src/ocsf_schema_compiler.egg-info/top_level.txt +1 -0
- ocsf_schema_compiler-0.9.2/tests/test_expected_changes.py +112 -0
- ocsf_schema_compiler-0.9.2/tests/test_regressions.py +202 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ocsf-schema-compiler
|
|
3
|
+
Version: 0.9.2
|
|
4
|
+
Summary: Compile an Open Cybersecurity Schema Framework (OCSF) schema
|
|
5
|
+
Author-email: Rick Mouritzen <rick@rickmode.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/ocsf/ocsf-schema-compiler
|
|
8
|
+
Project-URL: Repository, https://github.com/ocsf/ocsf-schema-compiler.git
|
|
9
|
+
Project-URL: Issues, https://github.com/ocsf/ocsf-schema-compiler/issues
|
|
10
|
+
Keywords: compiler,ocsf,ocsf-schema,schema
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
18
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.14
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: bumpver; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff; extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# OCSF Schema Compiler
|
|
30
|
+
This is a Python library and command-line tool for compiling the Open Cybersecurity Schema Framework (OCSF) schema, specifically the schema at https://github.com/ocsf/ocsf-schema.
|
|
31
|
+
|
|
32
|
+
## Getting started
|
|
33
|
+
There are three ways to use the OCSF Schema Compiler:
|
|
34
|
+
1. As a command-line tool, installed from PyPI.
|
|
35
|
+
2. As a library, installed from PyPI.
|
|
36
|
+
3. As a developer working on this project.
|
|
37
|
+
|
|
38
|
+
Python version 3.14 or later is required.
|
|
39
|
+
|
|
40
|
+
## Using `ocsf-schema-compiler` as a command-line tool
|
|
41
|
+
Create a virtual environment then install with `pip`. For example:
|
|
42
|
+
```shell
|
|
43
|
+
python3 -m venv .venv
|
|
44
|
+
source ./.venv/bin/activate
|
|
45
|
+
python -m pip install ocsf-schema-compiler
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Running from this environment is now a matter of calling `ocsf-schema-compiler`:
|
|
49
|
+
```shell
|
|
50
|
+
ocsf-schema-compiler -h
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The basic usage is passing the base directory of a schema to the compiler and capturing the output to a file.
|
|
54
|
+
```shell
|
|
55
|
+
ocsf-schema-compiler path/to/ocsf-schema > schema.json
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Using `ocsf-schema-compiler` as a library
|
|
59
|
+
Create a virtual environment then install with `pip`. For example:
|
|
60
|
+
```shell
|
|
61
|
+
python3 -m venv .venv
|
|
62
|
+
source ./.venv/bin/activate
|
|
63
|
+
pip install ocsf-schema-compiler
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The compiler is implemented in the `SchemaCompiler` class. The class constructor the same options as the command-line tool. The class's `compile` method does the heavy lifting, returning a `dict` containing the compiled schema. More specifically, `compiler` returns an `ocsf_schema_compiler.jsonish.JObject`, which is a type alias for JSON-compatible `dict`.
|
|
67
|
+
```python
|
|
68
|
+
from pathlib import Path
|
|
69
|
+
|
|
70
|
+
from ocsf_schema_compiler.compiler import SchemaCompiler
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
compiler = SchemaCompiler(Path("path/to/ocsf-schema"))
|
|
74
|
+
output = compiler.compile()
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
See `ocsf_schema_compiler.__main__` for a working example.
|
|
78
|
+
|
|
79
|
+
## Developing `ocsf-schema-compiler`
|
|
80
|
+
The recommended way to work on OCSF projects is via a fork into your own GitHub profile or organization. Create your fork of [this repo](https://github.com/ocsf/ocsf-schema-compiler) with the [GitHub CLI](https://cli.github.com/) tool (or, more painfully, manually).
|
|
81
|
+
|
|
82
|
+
This project requires Python 3.14 or later, and otherwise has no runtime dependencies. This mean you can run it directly from a cloned repo's `src` directory without creating a virtual environment.
|
|
83
|
+
|
|
84
|
+
I usually run with a subshell so my current directory remains in the base of the cloned repo. I also often use the [jq](https://jqlang.org/) tool to format the JSON output. For example:
|
|
85
|
+
```shell
|
|
86
|
+
cd path/to/ocsf-schema-compiler
|
|
87
|
+
$(cd src && python3 -m ocsf_schema_compiler ~/path/to/ocsf-schema > jq -S > ~/path/to/output/schema.json)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
This project has regression tests in the `tests` directory built using the `unittest` library. These also can be run without a virtual environment. The tests can be run with the `Makefile` target `tests`.
|
|
91
|
+
```shell
|
|
92
|
+
make tests
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
This project uses [Ruff](https://docs.astral.sh/ruff/) for linting and code formatting. Ruff's formatting is very similar to Black with some minor differences (improvements, in my opinion). This requires a virtual environment with both installed. With the virtual environment activated the linting and formatting can be run with the `Makefile` target `lint`.
|
|
96
|
+
|
|
97
|
+
This project's `.gitignore` assumes the virtual environment is at `.venv`.
|
|
98
|
+
|
|
99
|
+
```shell
|
|
100
|
+
# A standard Python virtual environment works fine
|
|
101
|
+
python3 -m venv .venv
|
|
102
|
+
source ./.venv/bin/activate
|
|
103
|
+
|
|
104
|
+
# Install the tools
|
|
105
|
+
pip install ruff
|
|
106
|
+
|
|
107
|
+
# Now the lint target will work
|
|
108
|
+
make lint
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Also with a virtual environment, a local install can be used to run the compiler.
|
|
112
|
+
```shell
|
|
113
|
+
# A standard Python virtual environment works fine
|
|
114
|
+
python3 -m venv .venv
|
|
115
|
+
source ./.venv/bin/activate
|
|
116
|
+
|
|
117
|
+
pip install -e .
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
Integrating Ruff with your editor is recommended. See [Editor integration | Ruff](https://docs.astral.sh/ruff/editors/).
|
|
122
|
+
|
|
123
|
+
## Publishing
|
|
124
|
+
This project follows the publishing approach described by this tutorial: [How to Publish an Open-Source Python Package to PyPI — Real Python](https://realpython.com/pypi-publish-python-package/), including use of the [Build](https://pypa-build.readthedocs.io/) and [Twine](https://twine.readthedocs.io/) tools. The [BumpVer](https://pypi.org/project/bumpver/) tool is also used to increment versions and keep the various mentions of the version in sync.
|
|
125
|
+
|
|
126
|
+
## Copyright
|
|
127
|
+
Copyright © OCSF a Series of LF Projects, LLC. See [NOTICE](https://github.com/ocsf/ocsf-schema-compiler/blob/main/NOTICE) for details.
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
This project is distributed under the Apache License Version 2.0. See [LICENSE](https://github.com/ocsf/ocsf-schema-compiler/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# OCSF Schema Compiler
|
|
2
|
+
This is a Python library and command-line tool for compiling the Open Cybersecurity Schema Framework (OCSF) schema, specifically the schema at https://github.com/ocsf/ocsf-schema.
|
|
3
|
+
|
|
4
|
+
## Getting started
|
|
5
|
+
There are three ways to use the OCSF Schema Compiler:
|
|
6
|
+
1. As a command-line tool, installed from PyPI.
|
|
7
|
+
2. As a library, installed from PyPI.
|
|
8
|
+
3. As a developer working on this project.
|
|
9
|
+
|
|
10
|
+
Python version 3.14 or later is required.
|
|
11
|
+
|
|
12
|
+
## Using `ocsf-schema-compiler` as a command-line tool
|
|
13
|
+
Create a virtual environment then install with `pip`. For example:
|
|
14
|
+
```shell
|
|
15
|
+
python3 -m venv .venv
|
|
16
|
+
source ./.venv/bin/activate
|
|
17
|
+
python -m pip install ocsf-schema-compiler
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Running from this environment is now a matter of calling `ocsf-schema-compiler`:
|
|
21
|
+
```shell
|
|
22
|
+
ocsf-schema-compiler -h
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The basic usage is passing the base directory of a schema to the compiler and capturing the output to a file.
|
|
26
|
+
```shell
|
|
27
|
+
ocsf-schema-compiler path/to/ocsf-schema > schema.json
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Using `ocsf-schema-compiler` as a library
|
|
31
|
+
Create a virtual environment then install with `pip`. For example:
|
|
32
|
+
```shell
|
|
33
|
+
python3 -m venv .venv
|
|
34
|
+
source ./.venv/bin/activate
|
|
35
|
+
pip install ocsf-schema-compiler
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The compiler is implemented in the `SchemaCompiler` class. The class constructor the same options as the command-line tool. The class's `compile` method does the heavy lifting, returning a `dict` containing the compiled schema. More specifically, `compiler` returns an `ocsf_schema_compiler.jsonish.JObject`, which is a type alias for JSON-compatible `dict`.
|
|
39
|
+
```python
|
|
40
|
+
from pathlib import Path
|
|
41
|
+
|
|
42
|
+
from ocsf_schema_compiler.compiler import SchemaCompiler
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
compiler = SchemaCompiler(Path("path/to/ocsf-schema"))
|
|
46
|
+
output = compiler.compile()
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
See `ocsf_schema_compiler.__main__` for a working example.
|
|
50
|
+
|
|
51
|
+
## Developing `ocsf-schema-compiler`
|
|
52
|
+
The recommended way to work on OCSF projects is via a fork into your own GitHub profile or organization. Create your fork of [this repo](https://github.com/ocsf/ocsf-schema-compiler) with the [GitHub CLI](https://cli.github.com/) tool (or, more painfully, manually).
|
|
53
|
+
|
|
54
|
+
This project requires Python 3.14 or later, and otherwise has no runtime dependencies. This mean you can run it directly from a cloned repo's `src` directory without creating a virtual environment.
|
|
55
|
+
|
|
56
|
+
I usually run with a subshell so my current directory remains in the base of the cloned repo. I also often use the [jq](https://jqlang.org/) tool to format the JSON output. For example:
|
|
57
|
+
```shell
|
|
58
|
+
cd path/to/ocsf-schema-compiler
|
|
59
|
+
$(cd src && python3 -m ocsf_schema_compiler ~/path/to/ocsf-schema > jq -S > ~/path/to/output/schema.json)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This project has regression tests in the `tests` directory built using the `unittest` library. These also can be run without a virtual environment. The tests can be run with the `Makefile` target `tests`.
|
|
63
|
+
```shell
|
|
64
|
+
make tests
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
This project uses [Ruff](https://docs.astral.sh/ruff/) for linting and code formatting. Ruff's formatting is very similar to Black with some minor differences (improvements, in my opinion). This requires a virtual environment with both installed. With the virtual environment activated the linting and formatting can be run with the `Makefile` target `lint`.
|
|
68
|
+
|
|
69
|
+
This project's `.gitignore` assumes the virtual environment is at `.venv`.
|
|
70
|
+
|
|
71
|
+
```shell
|
|
72
|
+
# A standard Python virtual environment works fine
|
|
73
|
+
python3 -m venv .venv
|
|
74
|
+
source ./.venv/bin/activate
|
|
75
|
+
|
|
76
|
+
# Install the tools
|
|
77
|
+
pip install ruff
|
|
78
|
+
|
|
79
|
+
# Now the lint target will work
|
|
80
|
+
make lint
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Also with a virtual environment, a local install can be used to run the compiler.
|
|
84
|
+
```shell
|
|
85
|
+
# A standard Python virtual environment works fine
|
|
86
|
+
python3 -m venv .venv
|
|
87
|
+
source ./.venv/bin/activate
|
|
88
|
+
|
|
89
|
+
pip install -e .
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
Integrating Ruff with your editor is recommended. See [Editor integration | Ruff](https://docs.astral.sh/ruff/editors/).
|
|
94
|
+
|
|
95
|
+
## Publishing
|
|
96
|
+
This project follows the publishing approach described by this tutorial: [How to Publish an Open-Source Python Package to PyPI — Real Python](https://realpython.com/pypi-publish-python-package/), including use of the [Build](https://pypa-build.readthedocs.io/) and [Twine](https://twine.readthedocs.io/) tools. The [BumpVer](https://pypi.org/project/bumpver/) tool is also used to increment versions and keep the various mentions of the version in sync.
|
|
97
|
+
|
|
98
|
+
## Copyright
|
|
99
|
+
Copyright © OCSF a Series of LF Projects, LLC. See [NOTICE](https://github.com/ocsf/ocsf-schema-compiler/blob/main/NOTICE) for details.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
This project is distributed under the Apache License Version 2.0. See [LICENSE](https://github.com/ocsf/ocsf-schema-compiler/blob/main/LICENSE) for details.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ocsf-schema-compiler"
|
|
7
|
+
version = "0.9.2"
|
|
8
|
+
description = "Compile an Open Cybersecurity Schema Framework (OCSF) schema"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Rick Mouritzen", email = "rick@rickmode.com" }]
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["compiler", "ocsf", "ocsf-schema", "schema"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Topic :: Software Development :: Build Tools",
|
|
22
|
+
"Topic :: Software Development :: Compilers",
|
|
23
|
+
"Topic :: Software Development :: Libraries",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
]
|
|
26
|
+
requires-python = ">= 3.14"
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/ocsf/ocsf-schema-compiler"
|
|
30
|
+
Repository = "https://github.com/ocsf/ocsf-schema-compiler.git"
|
|
31
|
+
Issues = "https://github.com/ocsf/ocsf-schema-compiler/issues"
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
ocsf-schema-compiler = "ocsf_schema_compiler.__main__:main"
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
dev = ["bumpver", "ruff"]
|
|
38
|
+
|
|
39
|
+
[tool.bumpver]
|
|
40
|
+
current_version = "0.9.2"
|
|
41
|
+
version_pattern = "MAJOR.MINOR.PATCH"
|
|
42
|
+
commit_message = "Bump version {old_version} -> {new_version}"
|
|
43
|
+
tag_message = "{new_version}"
|
|
44
|
+
tag_scope = "default"
|
|
45
|
+
pre_commit_hook = ""
|
|
46
|
+
post_commit_hook = ""
|
|
47
|
+
commit = true
|
|
48
|
+
tag = true
|
|
49
|
+
push = false
|
|
50
|
+
|
|
51
|
+
[tool.bumpver.file_patterns]
|
|
52
|
+
"pyproject.toml" = ['current_version = "{version}"', 'version = "{version}"']
|
|
53
|
+
"src/ocsf_schema_compiler/__init__.py" = ["{version}"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint]
|
|
56
|
+
# E501 includes long strings in line-length checks.
|
|
57
|
+
# https://docs.astral.sh/ruff/rules/line-too-long/
|
|
58
|
+
# W505 includes long comments in line-length checks when enabled by setting below
|
|
59
|
+
# https://docs.astral.sh/ruff/rules/doc-line-too-long/
|
|
60
|
+
extend-select = ["E501", "W505"]
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint.pycodestyle]
|
|
63
|
+
# max-doc-length = 88
|
|
64
|
+
ignore-overlong-task-comments = false
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.9.2"
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from argparse import ArgumentParser
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from sys import stderr
|
|
6
|
+
from time import perf_counter
|
|
7
|
+
|
|
8
|
+
from ocsf_schema_compiler import __version__
|
|
9
|
+
from ocsf_schema_compiler.compiler import SchemaCompiler
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main():
|
|
15
|
+
parser = ArgumentParser(
|
|
16
|
+
description=f"Open Cybersecurity Schema Framework Schema Compiler, version "
|
|
17
|
+
f"{__version__}. Compile an OCSF schema directory structure down to a single"
|
|
18
|
+
" JSON object written to standard output. Logs are written to standard error."
|
|
19
|
+
" Source code at https://github.com/ocsf/ocsf-schema-compiler.",
|
|
20
|
+
)
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"path",
|
|
23
|
+
type=Path,
|
|
24
|
+
help="path to an OCSF schema directory (e.g., a git clone of"
|
|
25
|
+
" https://github.com/ocsf/ocsf-schema)",
|
|
26
|
+
)
|
|
27
|
+
parser.add_argument(
|
|
28
|
+
"-i",
|
|
29
|
+
"--ignore-platform-extensions",
|
|
30
|
+
action="store_true",
|
|
31
|
+
default=False,
|
|
32
|
+
help="ignore platform extensions (if any); these are in an extensions directory"
|
|
33
|
+
" under the schema directory; default: %(default)s",
|
|
34
|
+
)
|
|
35
|
+
parser.add_argument(
|
|
36
|
+
"-e",
|
|
37
|
+
"--extensions-path",
|
|
38
|
+
action="append",
|
|
39
|
+
type=Path,
|
|
40
|
+
metavar="PATH",
|
|
41
|
+
dest="extensions_paths",
|
|
42
|
+
help="optional path to a directory containing one or more OCSF schema"
|
|
43
|
+
" extensions; can be repeated",
|
|
44
|
+
)
|
|
45
|
+
group = parser.add_mutually_exclusive_group()
|
|
46
|
+
group.add_argument(
|
|
47
|
+
"-b",
|
|
48
|
+
"--browser-mode",
|
|
49
|
+
action="store_true",
|
|
50
|
+
default=False,
|
|
51
|
+
help="include extra information needed by the schema browser (the OCSF Server);"
|
|
52
|
+
" cannot be used with the -l, --legacy-mode option; default: %(default)s",
|
|
53
|
+
)
|
|
54
|
+
group.add_argument(
|
|
55
|
+
"-l",
|
|
56
|
+
"--legacy-mode",
|
|
57
|
+
action="store_true",
|
|
58
|
+
default=False,
|
|
59
|
+
help="output schema in legacy export schema layout; cannot be used with the"
|
|
60
|
+
" -b, --browser-mode option; default: %(default)s",
|
|
61
|
+
)
|
|
62
|
+
parser.add_argument(
|
|
63
|
+
"-s",
|
|
64
|
+
"--scope-extension-keys",
|
|
65
|
+
action="store_true",
|
|
66
|
+
default=False,
|
|
67
|
+
help="scope extension keys; can only be used with the -l, --legacy-mode option;"
|
|
68
|
+
" default: %(default)s",
|
|
69
|
+
)
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
"--log-level",
|
|
72
|
+
choices=("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"),
|
|
73
|
+
default="INFO",
|
|
74
|
+
help="set log level; logs are written to standard error; default: %(default)s",
|
|
75
|
+
)
|
|
76
|
+
parser.add_argument(
|
|
77
|
+
"-v",
|
|
78
|
+
"--version",
|
|
79
|
+
action="version",
|
|
80
|
+
version="%(prog)s " + __version__,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
args = parser.parse_args()
|
|
84
|
+
if args.scope_extension_keys and not args.legacy_mode:
|
|
85
|
+
parser.error("-s, --scope-extension-keys requires -l, --legacy-mode")
|
|
86
|
+
|
|
87
|
+
logging.basicConfig(
|
|
88
|
+
format="%(levelname)s: %(message)s",
|
|
89
|
+
style="%",
|
|
90
|
+
stream=stderr,
|
|
91
|
+
level=args.log_level,
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
start_seconds = perf_counter()
|
|
95
|
+
|
|
96
|
+
compiler = SchemaCompiler(
|
|
97
|
+
args.path,
|
|
98
|
+
args.ignore_platform_extensions,
|
|
99
|
+
args.extensions_paths,
|
|
100
|
+
args.browser_mode,
|
|
101
|
+
args.legacy_mode,
|
|
102
|
+
args.scope_extension_keys,
|
|
103
|
+
)
|
|
104
|
+
output = compiler.compile()
|
|
105
|
+
|
|
106
|
+
duration = perf_counter() - start_seconds
|
|
107
|
+
logger.info("Schema compilation took %.3f seconds", duration)
|
|
108
|
+
|
|
109
|
+
print(json.dumps(output))
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
if __name__ == "__main__":
|
|
113
|
+
main()
|