4
4
import os
5
5
import yaml
6
6
import base64
7
- import json
8
- from flask import Flask , jsonify , render_template , request , Response
7
+ import importlib . metadata as metadata
8
+ from flask import Flask , jsonify , request , Response
9
9
10
10
from sigma .conversion .base import Backend
11
11
from sigma .plugins import InstalledSigmaPlugins
20
20
backends = plugins .backends
21
21
pipeline_resolver = plugins .get_pipeline_resolver ()
22
22
pipelines = list (pipeline_resolver .list_pipelines ())
23
- pipelines_names = [p [0 ] for p in pipelines ]
24
23
25
-
26
- @app .route ("/" )
27
- def home ():
28
- formats = []
29
- for backend in backends .keys ():
30
- for name , description in plugins .backends [backend ].formats .items ():
31
- formats .append (
32
- {"name" : name , "description" : description , "backend" : backend }
24
+ @app .route ("/api/v1/targets" , methods = ["GET" ])
25
+ def get_targets ():
26
+ response = []
27
+ for name , backend in backends .items ():
28
+ response .append (
29
+ {"name" : name , "description" : backend .name }
33
30
)
34
-
35
- for name , pipeline in pipelines :
36
- if len (pipeline .allowed_backends ) > 0 :
37
- pipeline .backends = ", " .join (pipeline .allowed_backends )
38
- else :
39
- pipeline .backends = "all"
40
-
41
- return render_template (
42
- "index.html" , backends = backends , pipelines = pipelines , formats = formats
43
- )
44
-
45
-
46
- @app .route ("/getpipelines" , methods = ["GET" ])
31
+ return jsonify (response )
32
+
33
+ @app .route ("/api/v1/formats" , methods = ["GET" ])
34
+ def get_formats ():
35
+ args = request .args
36
+ response = []
37
+ if len (args ) == 0 :
38
+ for backend in backends .keys ():
39
+ for name , description in plugins .backends [backend ].formats .items ():
40
+ response .append (
41
+ {"name" : name , "description" : description , "target" : backend }
42
+ )
43
+ elif "target" in args :
44
+ target = args .get ("target" )
45
+ for backend in backends .keys ():
46
+ if backend == target :
47
+ for name , description in plugins .backends [backend ].formats .items ():
48
+ response .append (
49
+ {"name" : name , "description" : description }
50
+ )
51
+
52
+ return jsonify (response )
53
+
54
+ @app .route ("/api/v1/pipelines" , methods = ["GET" ])
47
55
def get_pipelines ():
48
- return jsonify (pipelines_names )
49
-
50
-
51
- @app .route ("/sigma" , methods = ["POST" ])
56
+ args = request .args
57
+ response = []
58
+ if len (args ) == 0 :
59
+ for name , pipeline in pipelines :
60
+ response .append ({"name" : name , "targets" : list (pipeline .allowed_backends )})
61
+ elif "target" in args :
62
+ target = args .get ("target" )
63
+ for name , pipeline in pipelines :
64
+ if (len (pipeline .allowed_backends ) == 0 ) or (target in pipeline .allowed_backends ):
65
+ response .append ({"name" : name , "targets" : list (pipeline .allowed_backends )})
66
+ return jsonify (response )
67
+
68
+
69
+ @app .route ("/api/v1/convert" , methods = ["POST" ])
52
70
def convert ():
53
- # get params from request
54
71
rule = str (base64 .b64decode (request .json ["rule" ]), "utf-8" )
55
72
# check if input is valid yaml
56
73
try :
57
74
yaml .safe_load (rule )
58
75
except :
59
- print ("error" )
60
76
return Response (
61
77
f"YamlError: Malformed yaml file" , status = 400 , mimetype = "text/html"
62
78
)
@@ -84,7 +100,7 @@ def convert():
84
100
try :
85
101
backend_class = backends [target ]
86
102
except :
87
- return Response (f"Unknown Backend " , status = 400 , mimetype = "text/html" )
103
+ return Response (f"Unknown Target " , status = 400 , mimetype = "text/html" )
88
104
89
105
try :
90
106
processing_pipeline = pipeline_resolver .resolve (pipeline )
@@ -109,6 +125,7 @@ def convert():
109
125
110
126
return result
111
127
112
-
113
128
if __name__ == "__main__" :
114
- app .run (host = "0.0.0.0" , port = int (os .environ .get ("PORT" , 8000 )))
129
+ current_version = metadata .version ("sigma-cli" )
130
+ port = int (f'8{ current_version .replace ("." ,"" )} ' )
131
+ app .run (host = "0.0.0.0" , port = int (os .environ .get ("PORT" , port )))
0 commit comments