Passed
Push — master ( 24cf04...79b4dc )
by -
01:32
created

plain()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462
Metric Value
cc 2
dl 0
loc 12
ccs 3
cts 7
cp 0.4286
crap 2.7462
rs 9.4285
1 1
from time import time, localtime, strftime
0 ignored issues
show
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...
2
3 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...
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...
4
5 1
from spike.model import db
6 1
from spike.model.naxsi_rules import NaxsiRules
7 1
from spike.model.naxsi_rulesets import NaxsiRuleSets
8
9 1
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...
10
11 1
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...
12
13 1
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...
14
15
16 1
@rulesets.route("/")
17
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...
18
    _rulesets = NaxsiRuleSets.query.order_by(NaxsiRuleSets.name).all()
19
    return render_template("/rulesets/index.html", rulesets=_rulesets)
20
21
22 1
@rulesets.route("/plain/")
23 1
@rulesets.route("/plain/<int:rid>")
24 1
def plain(rid=0):
25
    """
26
    Show the rule `rid` in plain text
27
    :param int rid: Rule id
28
    """
29
    if not rid:
30
        out = ''.join(map(__get_rules_for_ruleset, NaxsiRuleSets.query.all()))
31
    else:
32
        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...
33
    return Response(out, mimetype='text/plain')
34
35
36 1
@rulesets.route("/view/<int:rid>")
37 1
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...
38
    if not rid:
39
        return redirect("/rulesets/")
40
    ruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.id == rid).first()
41
    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...
42
43
44 1
@rulesets.route("/new", methods=["POST"])
45
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...
46
    rfile = request.form["rfile"].strip().lower()
47
    rname = request.form["rname"].strip().upper()
48
49
    if NaxsiRuleSets.query.filter(NaxsiRuleSets.file == rfile).first():
50
        flash("ERROR, ruleset exists: %s " % rfile, "error")
51
        return redirect("/rulesets/")
52
53
    db.session.add(NaxsiRuleSets(rfile, rname, "naxsi-ruleset: %s" % rfile, int(time())))
54
    try:
55
        db.session.commit()
56
    except SQLAlchemyError:
57
        db.session.rollback()
58
        flash("ERROR while trying to create ruleset: %s " % rfile, "error")
59
60
    flash("OK created: %s " % rfile, "success")
61
    return redirect("/rulesets/")
62
63
64 1
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...
65
    _rules = NaxsiRules.query.filter(
66
        NaxsiRules.ruleset == ruleset.file,
67
        NaxsiRules.active == 1
68
    ).all()
69
70
    nxruleset = NaxsiRuleSets.query.filter(NaxsiRuleSets.file == ruleset.file).first()
71
    db.session.add(nxruleset)
72
    db.session.commit()
73
    text_rules = ''.join(map(__get_textual_representation_rule, _rules))
74
75
    if with_header is False:
76
        return text_rules
77
78
    header = current_app.config["RULESET_HEADER"]
79
    header = header.replace("RULESET_DESC", ruleset.name)
80
    header = header.replace("RULESET_FILE", ruleset.file)
81
    header = header.replace("RULESET_DATE", strftime("%F - %H:%M", localtime(time())))
82
83
    return header + text_rules
84