-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ocsf/mark-observables
Mark observables
- Loading branch information
Showing
10 changed files
with
393 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "ocsf-lib" | ||
version = "0.4.0" | ||
version = "0.5.0" | ||
description = "Tools for working with the OCSF schema" | ||
authors = ["Jeremy Fisher <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -23,6 +23,9 @@ pytest-env = "^1.1.3" | |
|
||
[tool.poetry.scripts] | ||
compare = "ocsf.compare.__main__:main" | ||
validate-compatibility = "ocsf.validate.compatibility.__main__:main" | ||
compile = "ocsf.compile.__main__:main" | ||
schema = "ocsf.schema.__main__:main" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|
@@ -31,7 +34,7 @@ build-backend = "poetry.core.masonry.api" | |
[tool.pyright] | ||
typeCheckingMode = "strict" | ||
strict = ["src/ocsf"] | ||
reportPrivateUsage = false | ||
reportPrivateUsage = false # Unit testing private/protected things is A-OK in my book | ||
|
||
[tool.pytest.ini_options] | ||
markers = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,123 @@ | ||
# TODO this file is a stub for testing and should be converted into an example | ||
"""Compile a repository into a schema and dump it as JSON to STDOUT. | ||
from pprint import pprint | ||
from ocsf.repository import read_repo | ||
Valid command line arguments are: | ||
``` | ||
positional arguments: | ||
path Path to the OCSF repository | ||
from .compiler import Compilation | ||
options: | ||
-h, --help show this help message and exit | ||
--profile [PROFILE ...] | ||
The name of a profile to be enabled (defaults to all) | ||
--ignore-profile [IGNORE_PROFILE ...] | ||
The name of a profile to be disabled | ||
--extension [EXTENSION ...] | ||
The short path name (e.g. 'windows') of an extension to be enabled (defaults to all) | ||
--ignore-extension [IGNORE_EXTENSION ...] | ||
The short path name of an extension to be disabled | ||
--prefix-extensions Prefix object and event names and any attributes that reference them as their type with the extension name | ||
--no-prefix-extensions | ||
Do not prefix object and event names and any attributes that reference them as their type with the extension name | ||
--set-object-types Set type to 'object' and object_type to the object name for type references to objects | ||
--no-set-object-types | ||
Do not set type to 'object' and object_type to the object name for type references to objects | ||
--set-observable Set the observable field on attributes to the corresponding Observable Type ID where applicable | ||
--no-set-observable Do not set the observable field on attributes to the corresponding Observable Type ID where applicable | ||
``` | ||
Examples: | ||
Build the schema: | ||
$ python -m ocsf.compile /path/to/repo | ||
Build the schema with the data_security profile disabled: | ||
$ python -m ocsf.compile /path/to/repo --ignore-profile=data_security | ||
PATH = "/Users/jfisher/Source/ocsf/ocsf-schema" | ||
Build the schema with only the windows extension enabled: | ||
repo = read_repo(PATH, preserve_raw_data=True) | ||
compiler = Compilation(repo) | ||
$ python -m ocsf.compile /path/to/repo --extension=windows | ||
# TARGET = "events/iam/authentication.json" | ||
# TARGET = "events/base_event.json" | ||
TARGET = "objects/process.json" | ||
# TARGET = "extensions/windows/events/prefetch_query.json" | ||
# TARGET = "includes/classification.json" | ||
""" | ||
|
||
from argparse import ArgumentParser | ||
|
||
from ocsf.repository import read_repo | ||
from ocsf.schema import to_json | ||
|
||
from .compiler import Compilation | ||
from .options import CompilationOptions | ||
|
||
analysis = compiler.analyze() | ||
order = compiler.order() | ||
compile = compiler.compile() | ||
schema = compiler.build() | ||
def main(): | ||
parser = ArgumentParser(description="Compile an OCSF repository into a schema and dump it as JSON to STDOUT") | ||
parser.add_argument("path", help="Path to the OCSF repository") | ||
parser.add_argument("--profile", nargs="*", help="The name of a profile to be enabled (defaults to all)") | ||
parser.add_argument("--ignore-profile", nargs="*", help="The name of a profile to be disabled") | ||
parser.add_argument( | ||
"--extension", | ||
nargs="*", | ||
help="The short path name (e.g. 'windows') of an extension to be enabled (defaults to all)", | ||
) | ||
parser.add_argument("--ignore-extension", nargs="*", help="The short path name of an extension to be disabled") | ||
parser.add_argument( | ||
"--prefix-extensions", | ||
default=True, | ||
action="store_true", | ||
help="Prefix object and event names and any attributes that reference them as their type with the extension name", | ||
) | ||
parser.add_argument( | ||
"--no-prefix-extensions", | ||
dest="prefix_extensions", | ||
action="store_false", | ||
help="Do not prefix object and event names and any attributes that reference them as their type with the extension name", | ||
) | ||
parser.add_argument( | ||
"--set-object-types", | ||
default=True, | ||
action="store_true", | ||
help="Set type to 'object' and object_type to the object name for type references to objects", | ||
) | ||
parser.add_argument( | ||
"--no-set-object-types", | ||
dest="set_object_types", | ||
action="store_false", | ||
help="Do not set type to 'object' and object_type to the object name for type references to objects", | ||
) | ||
parser.add_argument( | ||
"--set-observable", | ||
default=True, | ||
action="store_true", | ||
help="Set the observable field on attributes to the corresponding Observable Type ID where applicable", | ||
) | ||
parser.add_argument( | ||
"--no-set-observable", | ||
dest="set_observable", | ||
action="store_false", | ||
help="Do not set the observable field on attributes to the corresponding Observable Type ID where applicable", | ||
) | ||
|
||
print(f"TARGET: {TARGET}") | ||
print("ORDER") | ||
prereqs: set[str] = set() | ||
args = parser.parse_args() | ||
|
||
options = CompilationOptions() | ||
|
||
def find_op(target: str): | ||
for o in order: | ||
if o.target == target and o.target not in prereqs: | ||
if o.prerequisite is not None and o.prerequisite not in prereqs: | ||
prereqs.add(o.prerequisite) | ||
find_op(o.prerequisite) | ||
# pprint(o) | ||
return None | ||
if args.profile: | ||
options.profiles = args.profile | ||
if args.ignore_profile: | ||
options.ignore_profiles = args.ignore_profile | ||
if args.extension: | ||
options.extensions = args.extension | ||
if args.ignore_extension: | ||
options.ignore_extensions = args.ignore_extension | ||
|
||
options.prefix_extensions = args.prefix_extensions | ||
options.set_object_types = args.set_object_types | ||
options.set_observable = args.set_observable | ||
|
||
find_op(TARGET) | ||
repo = read_repo(args.path, preserve_raw_data=False) | ||
compiler = Compilation(repo, options) | ||
|
||
for o in order: | ||
if o.target in prereqs or o.target == TARGET: | ||
pprint(o) | ||
if o.target in compile: | ||
for op, change in compile[o.target]: | ||
if op == o: | ||
pprint(change) | ||
print(to_json(compiler.build())) | ||
|
||
# print() | ||
# print("COMPILE") | ||
# | ||
# for prereq in prereqs: | ||
# if prereq in compile: | ||
# pprint(compile[prereq]) | ||
# pprint(compile[TARGET]) | ||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.