Skip to content

Commit 576b8d0

Browse files
committed
Adds ruff lint workflow, config and autofixes issues
1 parent 626fe15 commit 576b8d0

36 files changed

+9152
-3959
lines changed

.github/workflows/lint.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request: ~
8+
9+
jobs:
10+
lint:
11+
name: Lint
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v3
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
21+
- name: Install Ruff
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install ruff
25+
26+
- name: Check Code with Ruff
27+
run: |
28+
ruff check --statistics
29+
ruff format --check

docs/conf.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
project = 'MAL Toolbox'
2121
copyright = '2024, Andrei Buhaiu, Giuseppe Nebbione, Nikolaos Kakouros, Jakob Nyberg, Joakim Loxdal'
22-
author = 'Andrei Buhaiu, Giuseppe Nebbione, Nikolaos Kakouros, Jakob Nyberg, Joakim Loxdal'
22+
author = (
23+
'Andrei Buhaiu, Giuseppe Nebbione, Nikolaos Kakouros, Jakob Nyberg, Joakim Loxdal'
24+
)
2325

2426

2527
# -- General configuration ---------------------------------------------------
2628

2729
import os
2830
import sys
31+
2932
sys.path.insert(0, os.path.abspath('../')) # Source code dir relative to this file
3033

3134
# Add any Sphinx extension module names here, as strings. They can be
@@ -53,9 +56,9 @@
5356
# The theme to use for HTML and HTML Help pages. See the documentation for
5457
# a list of builtin themes.
5558
#
56-
html_theme = 'sphinx_rtd_theme' # pip install sphinx_rtd_theme
59+
html_theme = 'sphinx_rtd_theme' # pip install sphinx_rtd_theme
5760

5861
# Add any paths that contain custom static files (such as style sheets) here,
5962
# relative to this directory. They are copied after the builtin static files,
6063
# so a file named "default.css" will overwrite the builtin "default.css".
61-
# html_static_path = ['_static']
64+
# html_static_path = ['_static']

maltoolbox/__init__.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- encoding: utf-8 -*-
21
# MAL Toolbox v0.2.0
32
# Copyright 2024, Andrei Buhaiu.
43
#
@@ -16,41 +15,40 @@
1615
#
1716

1817

19-
"""
20-
MAL-Toolbox Framework
21-
"""
18+
"""MAL-Toolbox Framework."""
2219

2320
__title__ = 'maltoolbox'
2421
__version__ = '0.2.0'
25-
__authors__ = ['Andrei Buhaiu',
22+
__authors__ = [
23+
'Andrei Buhaiu',
2624
'Giuseppe Nebbione',
2725
'Nikolaos Kakouros',
2826
'Jakob Nyberg',
29-
'Joakim Loxdal']
27+
'Joakim Loxdal',
28+
]
3029
__license__ = 'Apache 2.0'
3130
__docformat__ = 'restructuredtext en'
3231

3332
__all__ = ()
3433

35-
import os
3634
import configparser
3735
import logging
36+
import os
3837

3938
ERROR_INCORRECT_CONFIG = 1
4039

41-
CONFIGFILE = os.path.join(
42-
os.path.dirname(os.path.abspath(__file__)),
43-
"default.conf"
44-
)
40+
CONFIGFILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'default.conf')
4541

4642
config = configparser.ConfigParser()
4743
config.read(CONFIGFILE)
4844

4945
if 'logging' not in config:
50-
raise ValueError('Config file is missing essential information, cannot proceed.')
46+
msg = 'Config file is missing essential information, cannot proceed.'
47+
raise ValueError(msg)
5148

5249
if 'log_file' not in config['logging']:
53-
raise ValueError('Config file is missing a log_file location, cannot proceed.')
50+
msg = 'Config file is missing a log_file location, cannot proceed.'
51+
raise ValueError(msg)
5452

5553
log_configs = {
5654
'log_file': config['logging']['log_file'],
@@ -60,9 +58,11 @@
6058
'langspec_file': config['logging']['langspec_file'],
6159
}
6260

63-
os.makedirs(os.path.dirname(log_configs['log_file']), exist_ok = True)
61+
os.makedirs(os.path.dirname(log_configs['log_file']), exist_ok=True)
6462

65-
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M')
63+
formatter = logging.Formatter(
64+
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M'
65+
)
6666
file_handler = logging.FileHandler(log_configs['log_file'], mode='w')
6767
file_handler.setFormatter(formatter)
6868

@@ -78,7 +78,6 @@
7878
if 'neo4j' in config:
7979
for term in ['uri', 'username', 'password', 'dbname']:
8080
if term not in config['neo4j']:
81-
8281
msg = (
8382
'Config file is missing essential Neo4J '
8483
f'information: {term}, cannot proceed.'

maltoolbox/__main__.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
"""
2-
Command-line interface for MAL toolbox operations
1+
"""Command-line interface for MAL toolbox operations.
32
43
Usage:
54
maltoolbox attack-graph generate [options] <model> <lang_file>
@@ -20,58 +19,61 @@
2019
- If --neo4j is used, the Neo4j instance should be running. The connection
2120
parameters required for this app to reach the Neo4j instance should be
2221
defined in the default.conf file.
22+
2323
"""
2424

25-
import logging
2625
import json
26+
import logging
27+
2728
import docopt
2829

2930
from maltoolbox.wrappers import create_attack_graph
31+
3032
from . import log_configs, neo4j_configs
31-
from .language.compiler import MalCompiler
3233
from .ingestors import neo4j
34+
from .language.compiler import MalCompiler
3335

3436
logger = logging.getLogger(__name__)
3537

36-
def generate_attack_graph(
37-
model_file: str,
38-
lang_file: str,
39-
send_to_neo4j: bool
40-
) -> None:
41-
"""Create an attack graph and optionally send to neo4j
42-
38+
39+
def generate_attack_graph(model_file: str, lang_file: str, send_to_neo4j: bool) -> None:
40+
"""Create an attack graph and optionally send to neo4j.
41+
4342
Args:
4443
model_file - path to the model file
4544
lang_file - path to the language file
4645
send_to_neo4j - whether to ingest into neo4j or not
46+
4747
"""
4848
attack_graph = create_attack_graph(lang_file, model_file)
4949
if log_configs['attackgraph_file']:
50-
attack_graph.save_to_file(
51-
log_configs['attackgraph_file']
52-
)
50+
attack_graph.save_to_file(log_configs['attackgraph_file'])
5351

5452
if send_to_neo4j:
5553
logger.debug('Ingest model graph into Neo4J database.')
56-
neo4j.ingest_model(attack_graph.model,
54+
neo4j.ingest_model(
55+
attack_graph.model,
5756
neo4j_configs['uri'],
5857
neo4j_configs['username'],
5958
neo4j_configs['password'],
6059
neo4j_configs['dbname'],
61-
delete=True)
60+
delete=True,
61+
)
6262
logger.debug('Ingest attack graph into Neo4J database.')
63-
neo4j.ingest_attack_graph(attack_graph,
63+
neo4j.ingest_attack_graph(
64+
attack_graph,
6465
neo4j_configs['uri'],
6566
neo4j_configs['username'],
6667
neo4j_configs['password'],
6768
neo4j_configs['dbname'],
68-
delete=False)
69+
delete=False,
70+
)
6971

7072

7173
def compile(lang_file: str, output_file: str) -> None:
72-
"""Compile language and dump into output file"""
74+
"""Compile language and dump into output file."""
7375
compiler = MalCompiler()
74-
with open(output_file, "w") as f:
76+
with open(output_file, 'w', encoding='utf-8') as f:
7577
json.dump(compiler.compile(lang_file), f, indent=2)
7678

7779

maltoolbox/attackgraph/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
"""
2-
Contains tools used to generate attack graphs from MAL instance
1+
"""Contains tools used to generate attack graphs from MAL instance
32
models and analyze attack graphs.
43
"""
54

6-
from .attacker import Attacker
7-
from .attackgraph import AttackGraph
8-
from .node import AttackGraphNode
5+
from .attacker import Attacker as Attacker
6+
from .attackgraph import AttackGraph as AttackGraph
7+
from .node import AttackGraphNode as AttackGraphNode

0 commit comments

Comments
 (0)