Skip to content

Commit 544fa6f

Browse files
author
izar tarandach
committed
Adding support for multi threat libraries following issue #261
1 parent 2ec73a6 commit 544fa6f

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,4 @@ plantuml.jar
126126
tm/
127127
/sqldump
128128
/tests/.config.pytm
129+
.aider*

pytm/pytm.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, e, context):
5151

5252

5353
logger = logging.getLogger(__name__)
54+
_defaultThreatsFile = os.path.dirname(__file__) + "/threatlib/threats.json"
5455

5556

5657
class var(object):
@@ -787,8 +788,7 @@ class TM:
787788
)
788789
name = varString("", required=True, doc="Model name")
789790
description = varString("", required=True, doc="Model description")
790-
threatsFile = varStrings("")
791-
threatsFileInit = False
791+
threatsFile = varStrings([_defaultThreatsFile])
792792
isOrdered = varBool(False, doc="Automatically order all Dataflows")
793793
mergeResponses = varBool(False, doc="Merge response edges in DFDs")
794794
ignoreUnused = varBool(False, doc="Ignore elements not used in any Dataflow")
@@ -836,9 +836,9 @@ def _init_threats(self):
836836

837837
def _add_threats(self):
838838

839-
for threat_file in self.threatsFile:
839+
for tf in self.threatsFile:
840840
try:
841-
with open(threat_file, "r", encoding="utf8") as threat_file:
841+
with open(tf, "r", encoding="utf8") as threat_file:
842842
threats_json = json.load(threat_file)
843843
except (FileNotFoundError, PermissionError, IsADirectoryError) as e:
844844
raise UIError(
@@ -1131,14 +1131,19 @@ def _process(self):
11311131

11321132
# delaying loading of threats to accomodate multiple threat files in the
11331133
# command line
1134-
if self.threatsFileInit == False:
1135-
tfs = [os.path.dirname(__file__) + "/threatlib/threats.json"]
1136-
if result.threat_files:
1137-
tfs.extend(result.threat_files)
1138-
self.threatsFile = tfs
1139-
self._init_threats()
1140-
self.threatsFileInit = True
1141-
1134+
if result.threat_files:
1135+
# start by removing the default
1136+
del self.threatsFile[0]
1137+
if "default" in result.threat_files:
1138+
index = result.threat_files.index("default")
1139+
result.threat_files[index] = _defaultThreatsFile
1140+
for x in result.threat_files:
1141+
self.threatsFile.append(x)
1142+
else:
1143+
# it is just the default file, so no need to do anything
1144+
pass
1145+
self._init_threats()
1146+
11421147
if result.debug:
11431148
logger.setLevel(logging.DEBUG)
11441149

sample.png

-58.9 KB
Binary file not shown.

tests/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,5 +1405,5 @@
14051405
"name": "my test tm",
14061406
"onDuplicates": "Action.NO_ACTION",
14071407
"threatsExcluded": [],
1408-
"threatsFile": "pytm/threatlib/threats.json"
1408+
"threatsFile": "{'pytm/threatlib/threats.json'}"
14091409
}

tests/test_private_func.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ def test_kwargs(self):
4949

5050
def test_load_threats(self):
5151
tm = TM("TM")
52-
self.assertNotEqual(len(TM._threats), 1)
52+
self.assertNotEqual(len(TM._threats), 0)
53+
54+
''' commenting this bit off since it is now valid to change threatsFile,
55+
but looking for confirmation from Jan that it is ok to do so
5356
with self.assertRaises(UIError):
5457
tm.threatsFile = "threats.json"
5558
5659
with self.assertRaises(UIError):
5760
TM("TM", threatsFile="threats.json")
61+
'''
5862

5963
def test_responses(self):
6064
tm = TM("my test tm", description="aa", isOrdered=True)

0 commit comments

Comments
 (0)