44import os
55import yaml
66import 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
99
1010from sigma .conversion .base import Backend
1111from sigma .plugins import InstalledSigmaPlugins
2020backends = plugins .backends
2121pipeline_resolver = plugins .get_pipeline_resolver ()
2222pipelines = list (pipeline_resolver .list_pipelines ())
23- pipelines_names = [p [0 ] for p in pipelines ]
2423
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 }
3330 )
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" ])
4755def 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" ])
5270def convert ():
53- # get params from request
5471 rule = str (base64 .b64decode (request .json ["rule" ]), "utf-8" )
5572 # check if input is valid yaml
5673 try :
5774 yaml .safe_load (rule )
5875 except :
59- print ("error" )
6076 return Response (
6177 f"YamlError: Malformed yaml file" , status = 400 , mimetype = "text/html"
6278 )
@@ -84,7 +100,7 @@ def convert():
84100 try :
85101 backend_class = backends [target ]
86102 except :
87- return Response (f"Unknown Backend " , status = 400 , mimetype = "text/html" )
103+ return Response (f"Unknown Target " , status = 400 , mimetype = "text/html" )
88104
89105 try :
90106 processing_pipeline = pipeline_resolver .resolve (pipeline )
@@ -109,6 +125,7 @@ def convert():
109125
110126 return result
111127
112-
113128if __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