Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to define custom properties (name/value pairs) to the TM and elements #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add custom_properties dict for custom data to be inserted into the mo…
…del which does not currently exist in pytm's data model.
nozmore-vera committed Oct 5, 2021
commit fd9c950c19f750986ba3aab03db1ca0a881f184e
5 changes: 5 additions & 0 deletions docs/template.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,10 @@

 

Repo: {tm.props[repo_url]} <br>

&nbsp;

## Dataflow Diagram - Level 0 DFD

![](sample.png)
@@ -54,3 +58,4 @@ Name|Description|Classification
&emsp;
</details>
}|

25 changes: 25 additions & 0 deletions pytm/pytm.py
Original file line number Diff line number Diff line change
@@ -210,6 +210,13 @@ def __set__(self, instance, value):
super().__set__(instance, DataSet(value))


class varDict(var):
def __set__(self, instance, value):
if not isinstance(value, dict):
raise ValueError("expecting a dict, got a {}".format(type(value)))
super().__set__(instance, value)


class DataSet(set):
def __contains__(self, item):
if isinstance(item, str):
@@ -720,6 +727,7 @@ class TM:
doc="""How to handle duplicate Dataflow
with same properties, except name and notes""",
)
props = varDict(dict([]), doc="Custom name/value pairs containing data about the model.")

def __init__(self, name, **kwargs):
for key, value in kwargs.items():
@@ -1142,6 +1150,7 @@ class Element:
required=False,
doc="Location of the source code that describes this element relative to the directory of the model script.",
)
props = varDict(dict([]), doc="Custom name/value pairs containing data about this element.")

def __init__(self, name, **kwargs):
for key, value in kwargs.items():
@@ -1265,6 +1274,22 @@ def inside(self, *boundaries):
return True
return False


def getProperty(self, prop):
"""getter method to extract data from props dict and avoid KeyError exceptions"""

if (self.props):
try:
value = self.props[prop]
print("getProperty:value = " + value)
except KeyError:
value = None
else:
value = None

return value


def _attr_values(self):
klass = self.__class__
result = {}
2 changes: 2 additions & 0 deletions tm.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
tm.description = "This is a sample threat model of a very simple system - a web-based comment system. The user enters comments and these are added to a database and displayed back to the user. The thought is that it is, though simple, a complete enough example to express meaningful threats."
tm.isOrdered = True
tm.mergeResponses = True
tm.props = { "repo_url" : "https://github.com/izar/pytm" }

internet = Boundary("Internet")
server_db = Boundary("Server/DB")
@@ -120,3 +121,4 @@

if __name__ == "__main__":
tm.process()