forked from OWASP/OpenCRE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosib_defs.py
More file actions
459 lines (403 loc) · 15.8 KB
/
osib_defs.py
File metadata and controls
459 lines (403 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import networkx as nx
from pprint import pprint
import logging
import os
import re
import warnings
from dataclasses import asdict, dataclass, field
from datetime import datetime
from enum import Enum
from typing import Any, Collection, Dict, List, Mapping, NewType, Optional, Tuple, Union
from networkx.algorithms.simple_paths import all_simple_paths
from application.defs import cre_defs as defs
import semver
import yaml
from dacite import (
Config,
ForwardReferenceError,
StrictUnionMatchError,
UnexpectedDataError,
from_dict,
)
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# used for serialising and deserialising yaml OSIB documents
# default language is 'en'
default_lang = "en"
# Osib_id is an int or a string
Osib_id = NewType("Osib_id", str)
# Lang is a string, e.g. 'en', 'pt_BR')
Lang = NewType("Lang", str)
@dataclass
class _Osib_base:
def todict(self) -> Dict[str, Any]:
return asdict(
self,
dict_factory=lambda x: {
k: v for (k, v) in x if v not in ["", {}, [], None]
},
)
@dataclass
class _Status(_Osib_base):
"""Status attributes decribing OSIB attributes and list items"""
status: str = field(compare=False, default="")
reviewed: Optional[int] = field(compare=False, default=None)
change: str = field(compare=False, default="")
@dataclass
class _Link(_Status):
"""Basic attributes used by `link`s-list items"""
link: str = field(default="") # osib id (=osib path to an object)
type: Optional[str] = field(default=None)
@dataclass
class _Source(_Status):
"""Basic attributes used by i18n sources-directory items"""
source: Optional[str] = field(default=None) # url
name: str = field(compare=False, default="")
description: Optional[str] = field(compare=False, default=None)
section: Optional[str] = field(default=None)
subsection: Optional[str] = field(default=None)
sectionID: Optional[str] = field(default=None)
@dataclass
class Node_attributes(_Status):
"""Attributes decribing an OSIB object"""
source_id: Optional[str] = field(
default=""
) # Unique id name by source, e.g. document
links: List[_Link] = field(compare=False, default_factory=list)
categories: Optional[List[str]] = field(compare=False, default=None)
maturity: Optional[str] = field(compare=False, default=None)
sources_i18n: Dict[Union[str, Lang], Optional[_Source]] = field(
compare=False, default_factory=dict
)
@dataclass
class Osib_node(_Osib_base):
"""Object-Node for building the OSIB tree"""
aliases: Optional[List[Osib_id]] = field(compare=False, default=None)
attributes: Optional[Node_attributes] = field(compare=False, default=None)
children: Optional[Dict[str, "Osib_node"]] = field(compare=False, default=None)
@dataclass
class Osib_tree(Osib_node):
"""Root-Object for building the OSIB tree"""
doctype: str = field(compare=False, default="OSIB")
schema: Optional[str] = field(
compare=False, default=str(semver.VersionInfo(major=0, minor=0, patch=0))
)
# Date, when the tree has been comiled as int: YYYYMMDD
date: Optional[str] = field(compare=False, default=None)
def read_osib_yaml(yaml_file: str = "") -> List[Dict[str, Any]]:
with open(yaml_file, "r") as fin:
osib_yaml = yaml.safe_load_all(fin)
return [y for y in osib_yaml]
def try_from_file(data: List[Dict[str, Any]] = []) -> List[Osib_tree]:
result = []
for dat in data:
result.append(
from_dict(data_class=Osib_tree, data=dat, config=Config(check_types=False))
) # check_Types=False on purpose as everything is a str
return result
def resolve_path(
osib_link: Optional[_Link] = None,
) -> Tuple[Optional[str], Optional[str]]:
if osib_link and osib_link.link:
reg = r"\w+\.\w+\.(?P<name>\w+)(\.(?P<section>.+$))?"
match = re.search(reg, osib_link.link)
if match:
return (match["name"], match["section"])
return None, None
# TODO: get this to return tools and code depending on type
def find_doc(link: Optional[_Link] = None) -> Optional[defs.Document]:
# TODO (spyros): given a list of existing docs, find if the link exists in the list, otherwise create a new doc and return1.
docname, docsection = resolve_path(link)
if docname and docsection:
return defs.Standard(section=docsection, name=docname)
return None
def _parse_node(
orgname: Optional[str] = None,
root: str = "OSIB",
name: Optional[str] = None,
osib: Optional[Osib_node] = None,
current_path: str = "",
node_type: Optional[defs.Credoctypes] = None,
) -> List[Union[defs.Document, defs.CRE, defs.Standard]]:
result: List[Union[defs.Document, defs.CRE, defs.Standard]] = []
if not osib:
return result
if (
osib is not None
and osib.attributes is not None
and (
"children" not in vars(osib)
or not osib.children
or osib.children in ({}, None)
)
and current_path not in (None, "")
):
res: defs.Document
"register the standard with the current path as subsection"
english_attrs = osib.attributes.sources_i18n.get("en")
if english_attrs:
if node_type == defs.Credoctypes.Standard:
res = defs.Standard(
name=name,
section=current_path.replace(f"{root}.{orgname}.", ""),
hyperlink=english_attrs.source if english_attrs.source else "",
)
elif node_type == defs.Credoctypes.Code:
res = defs.Code(
name=name,
description=english_attrs.description,
hyperlink=english_attrs.source if english_attrs.source else "",
)
elif node_type == defs.Credoctypes.Tool:
ttype = [
t
for t in defs.ToolTypes
if "categories" in vars(osib.attributes)
and osib.attributes.categories
and t in osib.attributes.categories
]
if ttype:
ttype = ttype[0]
osib.attributes.categories.remove(ttype)
osib.attributes.categories.remove(node_type)
res = defs.Tool(
name=name,
description=english_attrs.description,
hyperlink=english_attrs.source if english_attrs.source else "",
tooltype=ttype if ttype else defs.ToolTypes.Unknown,
section=english_attrs.section if english_attrs.section else "",
sectionID=(
english_attrs.sectionID if english_attrs.sectionID else ""
),
)
elif node_type == defs.Credoctypes.CRE:
res = defs.CRE(
name=name,
description=english_attrs.description,
hyperlink=english_attrs.source if english_attrs.source else "",
)
else:
raise ValueError("OSIB node type unknown")
res.metadata = {}
if osib.aliases:
res.metadata["alias"] = [x for x in osib.aliases]
if osib.attributes.source_id:
res.metadata["source_id"] = osib.attributes.source_id
if osib.attributes.maturity:
res.metadata["maturity"] = osib.attributes.maturity
if osib.attributes.categories:
res.metadata["categories"] = [x for x in osib.attributes.categories]
for olink in osib.attributes.links:
linked_doc = find_doc(olink)
if olink.type:
if olink.type.lower() == "parent":
res.add_link(
link=defs.Link(
document=linked_doc, ltype=defs.LinkTypes.PartOf
)
)
elif olink.type.lower() == "child":
res.add_link(
link=defs.Link(
document=linked_doc, ltype=defs.LinkTypes.Contains
)
)
elif olink.type.lower() == "related":
res.add_link(
link=defs.Link(
document=linked_doc, ltype=defs.LinkTypes.Related
)
)
return [res]
else:
logger.warning("OSIB has no english attributes, parsing skipped")
elif osib.children:
for section, child in osib.children.items():
cpath = current_path.replace(f"{root}.{orgname}.", "")
result.extend(
_parse_node(
orgname=orgname,
root=root,
name=name,
osib=child,
current_path=f"{cpath}.{section}" if len(cpath) else f"{section}",
node_type=node_type,
)
)
else:
logger.error(
"OSIB doesn't have children but leaf branch not followed this is a bug"
)
raise Exception(
"OSIB doesn't have children but leaf branch not followed this is a bug"
)
return result
def osib2cre(tree: Osib_tree) -> Tuple[List[defs.CRE], List[defs.Standard]]:
tree_aliases = tree.aliases
attrs = tree.attributes
standards: List[defs.Standard] = []
cres = []
root = tree.doctype
if not tree.children:
return [], []
for orgname, org in tree.children.items():
if org.children:
for pname, project in org.children.items():
if str(pname).lower() != "cre":
node_type = None
if project and project.attributes and project.attributes.categories:
t = [
c
for c in defs.Credoctypes
if c in project.attributes.categories
]
if t:
node_type = t[0]
standards.extend(
_parse_node( # type: ignore # I know what i'm doing just this once
root=root,
orgname=str(orgname),
name=str(pname),
osib=project,
node_type=node_type,
current_path=f"{root}.{orgname}",
)
)
else:
cres.extend(
[
defs.CRE(d)
for d in _parse_node(
root=root,
orgname=str(orgname),
osib=project,
node_type=defs.Credoctypes.CRE,
current_path=f"{root}.{orgname}",
)
]
)
return (cres, standards)
def update_paths(
paths: Optional[nx.DiGraph], pid: str, cid: str, rel_type: defs.LinkTypes
) -> nx.DiGraph:
if not paths:
paths = nx.DiGraph()
paths.add_edge(pid, cid)
return paths
def paths_to_osib(
osib_paths: List[str],
cres: Mapping[str, defs.Document],
related_nodes: List[Tuple[str, str]],
) -> Osib_tree:
# TODO: this doesn't add links (although, the child/parent structure IS the link)
# TODO: this doesn't account for non CRE paths, cres needs to become "documents" and we need to figure this out based on paths of standards or code or whatevs
result: Dict[str, Osib_node] = {}
osibs = {}
owasp = Osib_node(
attributes=Node_attributes(
sources_i18n={
Lang("en"): _Source(
name="Open Web Application Security Project",
source="https://owasp.org",
)
}
)
)
root = Osib_node(
attributes=Node_attributes(
sources_i18n={
Lang("en"): _Source(
name="Common Requirements Enumeration",
source="https://www.opencre.org",
)
}
)
)
# transform everything to Osib Nodes
for id, cre in cres.items():
o = Osib_node(
children={},
attributes=Node_attributes(
source_id=cre.id,
categories=[cre.doctype],
sources_i18n={
Lang("en"): _Source(
description=cre.description,
name=cre.name,
source="", # todo: make source point to opencre.org deeplink
)
},
links=[],
),
)
osibs[id] = o
# Add links to nodes potentially not in the tree first
for r in related_nodes:
l0 = [p for p in osib_paths if f"{r[0]}." in p]
l1 = [p for p in osib_paths if f"{r[1]}." in p]
path = ""
if l0:
path = f"OSIB.OWASP.CRE.{l0[0].split(r[0])[0]}.{r[0]}"
else:
path = f"OSIB.OWASP.CRE.{r[0]}"
result[r[0]] = osibs[r[0]]
osibs[r[0]].attributes.links.append(_Link(link=path, type="Related"))
path = ""
if l1:
path = f"OSIB.OWASP.CRE.{l1[0].split(r[1])[0]}.{r[1]}"
else:
path = f"OSIB.OWASP.CRE.{r[1]}"
result[r[1]] = osibs[r[1]]
osibs[r[1]].attributes.links.append(_Link(link=path, type="Related"))
# build the child-tree structure
for osib_path in sorted(osib_paths, key=len):
pwo = None
for oid in osib_path.split("."):
if pwo is not None:
if oid not in pwo.children:
pwo.children[oid] = osibs[oid]
else:
result[oid] = osibs[oid]
pwo = osibs[oid]
root.children = result
owasp.children = {"CRE": root}
return Osib_tree(children={"OWASP": owasp})
def cre2osib(docs: List[defs.Document]) -> Osib_tree:
# TODO: This only works with a link depth of 1, this is the general assumption for CREs but would be nice to make it work recursively
osib_paths: List[str] = []
related_nodes: List[Tuple[str, str]] = []
cres = {}
root = "OSIB"
org = "OWASP"
project = "CRE"
base_path = f"{root}.{org}.{project}"
osib_graph = nx.DiGraph()
# TODO: get this to update paths attaching the TYPE of document that is in this path somehow, maybe as part of node attrs(?)
for doc in docs:
cres[doc.id] = doc
for link in doc.links:
cres[link.document.id] = link.document
if link.ltype == defs.LinkTypes.PartOf:
osib_paths = update_paths(
paths=osib_graph,
pid=link.document.id,
cid=doc.id,
rel_type=link.ltype,
)
elif link.ltype == defs.LinkTypes.Contains:
osib_paths = update_paths(
paths=osib_graph,
pid=doc.id,
cid=link.document.id,
rel_type=link.ltype,
)
elif link.ltype == defs.LinkTypes.Related:
related_nodes.append((doc.id, link.document.id))
elif link.ltype == defs.LinkTypes.LinkedTo:
related_nodes.append((doc.id, link.document.id))
return paths_to_osib(
osib_paths=[".".join(p) for p in osib_paths],
cres=cres,
related_nodes=related_nodes,
)