Test Setup Failed
Push — master ( 63da58...9fdcd8 )
by -
01:30
created

plain()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 12
rs 9.4285
1
from flask import current_app, Blueprint, render_template, request, redirect, flash, Response
1 ignored issue
show
Coding Style introduced by
This line is too long as per the coding-style (93/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Configuration introduced by
The import flask could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
2
from spike.model import NaxsiRules, NaxsiRuleSets
3
from time import time, localtime, strftime
4
from spike.model import check_constraint, db
5
from sqlalchemy.exc import SQLAlchemyError
0 ignored issues
show
Configuration introduced by
The import sqlalchemy.exc could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
7
from rules import __get_textual_representation_rule
0 ignored issues
show
Configuration introduced by
The import rules could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
9
rulesets = Blueprint('rulesets', __name__)
0 ignored issues
show
Coding Style Naming introduced by
The name rulesets does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
10
11
12
@rulesets.route("/")
13
def index():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
14
    _rulesets = NaxsiRuleSets.query.order_by(NaxsiRuleSets.name).all()
15
    return render_template("/rulesets/index.html", rulesets=_rulesets)
16
17
@rulesets.route("/plain/")
18
@rulesets.route("/plain/<int:rid>")
19
def plain(rid=0):
20
    """
21
    Show the rule `rid` in plain text
22
    :param int rid: Rule id
23
    """
24
    if not rid:
25
        out = ''.join(map(__get_rules_for_ruleset, NaxsiRuleSets.query.all()))
26
    else:
27
        out = __get_rules_for_ruleset(NaxsiRuleSets.query.filter(NaxsiRuleSets.id == rid).first())
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (98/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
28
    return Response(out, mimetype='text/plain')
29
30
31
@rulesets.route("/view/<int:rid>")
32
def view(rid=0):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
33
    if not rid:
34
        return redirect("/rulesets/")
35
    ruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.id == rid).first()
36
    return render_template("/rulesets/view.html", r=ruleset, rout=__get_rules_for_ruleset(ruleset))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (99/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
37
38
39
@rulesets.route("/new", methods=["POST"])
40
def new():  # TODO filter parameter
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
41
    rfile = request.form["rfile"].strip().lower()
42
    rname = request.form["rname"].strip().upper()
43
44
    cie = check_constraint("ruleset", rfile)
45
    if cie:
46
        flash("ERROR, ruleset exists: %s " % rfile, "error")
47
        return redirect("/rulesets/")
48
49
    db.session.add(NaxsiRuleSets(rfile, rname, "naxsi-ruleset: %s" % rfile, int(time())))
50
    try:
51
        db.session.commit()
52
    except SQLAlchemyError:
53
        db.session.rollback()
54
        flash("ERROR while trying to create ruleset: %s " % rfile, "error")
55
56
    flash("OK created: %s " % rfile, "success")
57
    return redirect("/rulesets/")
58
59
60
def __get_rules_for_ruleset(ruleset, with_header = True):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
def __get_rules_for_ruleset(ruleset, with_header = True):
^
Loading history...
61
    _rules = NaxsiRules.query.filter(
62
        NaxsiRules.ruleset == ruleset.file,
63
        NaxsiRules.active == 1
64
    ).all()
65
66
    nxruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.file == ruleset.file).first()
67
    db.session.add(nxruleset)
68
    db.session.commit()
69
    text_rules = ''.join(map(__get_textual_representation_rule, _rules))
70
71
    if with_header is False:
72
        return text_rules
73
74
    header = current_app.config["RULESET_HEADER"]
75
    header = header.replace("RULESET_DESC", ruleset.name)
76
    header = header.replace("RULESET_FILE", ruleset.file)
77
    header = header.replace( "RULESET_DATE", strftime("%F - %H:%M", localtime(time())))
0 ignored issues
show
Coding Style introduced by
No space allowed after bracket
header = header.replace( "RULESET_DATE", strftime("F - H:%M", localtime(time())))
^
Loading history...
78
79
    return header + text_rules
80