forked from OISF/suricata-verify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-eve.py
executable file
·135 lines (117 loc) · 4.3 KB
/
check-eve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#! /usr/bin/env python3
#
# Copyright (C) 2021-2022 Open Information Security Foundation
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import os
import os.path
import argparse
import json
import subprocess
try:
from jsonschema import validate
from jsonschema.exceptions import ValidationError
HAVE_PY = True
except:
HAVE_PY = False
def validate_json(args, json_filename, schema):
status = "OK"
errors = []
if not args.python_validator:
progname = os.path.join(TOPDIR, "eve-validator", "target", "release", "eve-validator")
cp = subprocess.run([progname, "-q", "-s", schema, "--", json_filename])
if cp.returncode != 0:
status = "FAIL"
errors.append(cp.stdout)
else:
with open(json_filename) as f:
for line in f:
obj = json.loads(line)
try:
validate(instance = obj, schema=schema)
except ValidationError as err:
status = "FAIL"
errors.append(err.message)
if not args.quiet:
if status == "FAIL":
print("===> %s: FAIL " % json_filename)
for err in errors:
print(err)
elif args.verbose:
print("===> %s: OK " % json_filename)
return status
def main():
global args
global TOPDIR
parser = argparse.ArgumentParser(description="Validation schema")
parser.add_argument("-v", dest="verbose", action="store_true")
parser.add_argument("-p", dest="python_validator", action="store_true", help="use python validator")
parser.add_argument("file", nargs="?", default=[])
parser.add_argument("-q", dest="quiet", action="store_true")
parser.add_argument("-s", dest="schema", action="store", required=True)
args = parser.parse_args()
TOPDIR = os.path.abspath(os.path.dirname(sys.argv[0]))
tdir = os.path.join(TOPDIR, "tests")
if args.python_validator:
if not HAVE_PY:
print("error: python validation not enabled: install python-jsonschema")
sys.exit(1)
schema = json.load(open(args.schema))
else:
schema = args.schema
checked = 0
passed = 0
failed = 0
isDirectory = True
argfile = args.file
if argfile:
# if the argument is a single file
if os.path.isfile(argfile):
isDirectory = False
status = validate_json(args, argfile, schema)
checked += 1
if status == "OK":
passed += 1
else:
failed += 1
# if the argument is a directory
elif os.path.isdir(argfile):
tdir = argfile
if isDirectory:
# os.walk for eve.json files and validate each one
for dirpath, dirnames, filenames in os.walk(tdir):
if 'eve.json' in filenames:
status = validate_json(args, os.path.join(dirpath, 'eve.json'), schema)
checked += 1
if status == "OK":
passed += 1
else:
failed += 1
if not args.quiet:
print("CHECKED: %d" % (checked))
print("PASSED: %d" % (passed))
print("FAILED: %d" % (failed))
if failed > 0:
return 1
return 0
if __name__ == "__main__":
sys.exit(main())