Skip to content

Commit

Permalink
Merge pull request #19 from nasbench/enhance-errors
Browse files Browse the repository at this point in the history
feat: enhance error message/code and apply black formatter
  • Loading branch information
M3NIX authored Nov 8, 2023
2 parents c1dec39 + 05a2736 commit 9a74093
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
45 changes: 27 additions & 18 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import yaml
import base64
from flask import Flask, render_template, request
from flask import Flask, render_template, request, Response

from sigma.conversion.base import Backend
from sigma.plugins import InstalledSigmaPlugins
Expand All @@ -15,55 +15,64 @@
plugins = InstalledSigmaPlugins.autodiscover()
backends = plugins.backends
pipeline_resolver = plugins.get_pipeline_resolver()
pipelines = list(pipeline_resolver.list_pipelines())
pipelines = list(pipeline_resolver.list_pipelines())

@app.route('/')

@app.route("/")
def home():
formats = []
for backend in backends.keys():
for name, description in plugins.backends[backend].formats.items():
formats.append({"name": name, "description": description, "backend": backend})

formats.append(
{"name": name, "description": description, "backend": backend}
)

for name, pipeline in pipelines:
if len(pipeline.allowed_backends) > 0:
pipeline.backends = ", ".join(pipeline.allowed_backends)
else:
pipeline.backends = "all"

return render_template('index.html', backends=backends, pipelines=pipelines, formats=formats)
return render_template(
"index.html", backends=backends, pipelines=pipelines, formats=formats
)

@app.route('/sigma', methods=['POST'])
def convert():

@app.route("/sigma", methods=["POST"])
def convert():
# get params from request
rule = str(base64.b64decode(request.json['rule']), "utf-8")
rule = str(base64.b64decode(request.json["rule"]), "utf-8")
# check if input is valid yaml
try:
yaml.safe_load(rule)
except:
print("error")
return ("Error: No valid yaml input")
return Response(
f"YamlError: Malformed yaml file", status=400, mimetype="text/html"
)

pipeline = []
if request.json['pipeline']:
for p in request.json['pipeline']:
if request.json["pipeline"]:
for p in request.json["pipeline"]:
pipeline.append(p)
target = request.json['target']
format = request.json['format']

target = request.json["target"]
format = request.json["format"]

backend_class = backends[target]
processing_pipeline = pipeline_resolver.resolve(pipeline)
backend : Backend = backend_class(processing_pipeline=processing_pipeline)
backend: Backend = backend_class(processing_pipeline=processing_pipeline)

try:
sigma_rule = SigmaCollection.from_yaml(rule)
result = backend.convert(sigma_rule, format)
if isinstance(result, list):
result = result[0]
except SigmaError as e:
return "Error: " + str(e)
return Response(f"SigmaError: {str(e)}", status=400, mimetype="text/html")

return result

if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 8000)))

if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
2 changes: 2 additions & 0 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ function convert(sigmaRule) {
queryCode.innerHTML = xhr.response;
queryCode.value = xhr.response;
Prism.highlightElement(queryCode); // rerun code highlighting
} else if (xhr.status === 400) {
queryCode.innerHTML = xhr.response;
} else if (xhr.status === 500) {
queryCode.innerHTML = "Error: Something went wrong";
}
Expand Down

0 comments on commit 9a74093

Please sign in to comment.