-
Notifications
You must be signed in to change notification settings - Fork 5
/
cbas.py
executable file
·254 lines (210 loc) · 10.9 KB
/
cbas.py
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
#!/usr/bin/env python
''' CBAS document parser and converter class.
Copyright (c) 2020 OWASP Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import os
import regex as re
import json
import csv
import dicttoxml
import copy
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class CBASSMM:
cbas = {}
cbas['Name'] = "Core Business Application - SAP Security Maturity Model"
cbas['ShortName'] = "CBAS-SAPSMM"
cbas['Version'] = ""
cbas['Description'] = "#todo"
cbas['Controls'] = []
boilerplate = "boilerplate"
controls = "Controls"
source_language = 'en'
config = { }
def __init__(self, source_language:str, config:dict):
regex = re.compile('Version (([\d.]+){3})')
self.source_language = source_language
self.boilerplate = self.boilerplate + "_" + self.source_language
self.controls = self.controls + "_" + self.source_language
self.config = config
for line in open(os.path.join(self.boilerplate, "0x01-Frontispiece.md"), encoding="utf8"):
m = re.search(regex, line)
if m:
self.cbas['Version'] = m.group(1)
break
regex = re.compile('## About the Standard\n\n(.*)')
with open(os.path.join(self.boilerplate, "0x01-Frontispiece.md"), encoding="utf8") as content:
m = re.search(regex, content.read())
if m:
self.cbas['Description'] = m.group(1)
self.cbas['Requirements'] = chapters = []
files = [f for f in os.listdir(path = self.controls) if re.match('.*\.md', string=f) ]
for filename in files:
control = {}
control['Shortcode'] = ""
control['Security Function'] = ""
control['CSF Category'] = ""
control['Technology'] = ""
control['Maturity Level'] = ""
control['IPAC'] = []
control['Defender'] = []
control['Prerequisites'] = []
control['Description'] = ""
control['Implementation'] = ""
control['Verification'] = []
control['References'] = []
control['Shortcode'] = filename.replace(".md", "")
with open(os.path.join(self.controls, filename), encoding="utf8" ) as file:
if filename in {'Control_Template.md', 'README.md'}:
continue
text = file.read()
regex = re.compile('(?<=##.*\\n)([^#]*)(?!##)') #select the body
m = re.findall(regex, text)
if m:
control['Description'] = m[0]
control['Implementation'] = m[1]
control['Verification'] = m[2].splitlines()
control['References'] = m[3].splitlines()
regex = re.compile('(?<=---.*\\n)([^#]*)(?=---)') #select the header
m = re.findall(regex, text)
if m:
head = m[0]
for header in head.splitlines():
attribute = header.split(sep=':', maxsplit=1)[0].strip()
value = header.split(sep=':', maxsplit=1)[1]
if self.get_ControlIdentifier_for_lang(identifier='Security Function', lang=self.source_language) == attribute :
control['Security Function'] = value.strip()
continue
elif self.get_ControlIdentifier_for_lang(identifier='Category', lang=self.source_language) == attribute :
control['CSF Category'] = value.strip()
continue
elif self.get_ControlIdentifier_for_lang(identifier='Technology', lang=self.source_language) == attribute :
control['Technology'] = value.strip()
continue
elif self.get_ControlIdentifier_for_lang(identifier='Maturity Level', lang=self.source_language) == attribute :
control['Maturity Level'] = value.strip()
continue
elif self.get_ControlIdentifier_for_lang(identifier='IPAC', lang=self.source_language) == attribute :
regex = re.compile('(?<=\()[IPAC](?=\))') #select the header
m = re.findall(regex, header)
for match in m:
control['IPAC'].append(match)
continue
elif self.get_ControlIdentifier_for_lang(identifier='Defender', lang=self.source_language) == attribute :
for match in value.strip().split(sep=' '):
control['Defender'].append(match)
continue
elif self.get_ControlIdentifier_for_lang(identifier='Prerequisites', lang=self.source_language) == attribute :
for match in value.strip().split(sep=' '):
control['Prerequisites'].append(match)
continue
self.cbas['Controls'].append(control)
def get_Controls(self) ->list:
return self.cbas['Controls']
def to_json(self):
''' Returns a JSON-formatted string '''
return json.dumps(self.cbas, indent = 2, sort_keys = False).strip()
def to_xml(self):
return dicttoxml.dicttoxml(self.cbas, attr_type=False).decode('utf-8')
def to_csv(self):
# make flat structure first
cbas_flat = self.make_flat()
# ''' Returns CSV '''
si = StringIO()
writer = csv.DictWriter(si, [self.get_ControlIdentifier_for_lang(identifier = 'Shortcode'),
self.get_ControlIdentifier_for_lang(identifier = 'Security Function'),
self.get_ControlIdentifier_for_lang(identifier = 'CSF Category'),
self.get_ControlIdentifier_for_lang(identifier = 'IPAC'),
self.get_ControlIdentifier_for_lang(identifier = 'Technology'),
self.get_ControlIdentifier_for_lang(identifier = 'Maturity Level'),
self.get_ControlIdentifier_for_lang(identifier = 'Defender'),
self.get_ControlIdentifier_for_lang(identifier = 'Prerequisites'),
self.get_ControlIdentifier_for_lang(identifier = 'Description'),
self.get_ControlIdentifier_for_lang(identifier = 'Implementation'),
self.get_ControlIdentifier_for_lang(identifier = 'Verification'),
self.get_ControlIdentifier_for_lang(identifier = 'References')])
writer.writeheader()
for row in cbas_flat:
keys = list(row.keys())
for key in keys:
row[self.get_ControlIdentifier_for_lang(identifier = key)] = row.pop(key)
writer.writerows(cbas_flat)
return si.getvalue()
def control_to_md(self, control: dict) -> dict:
filename = "{shortcode}.md".format(shortcode=control['Shortcode'])
md = "---\n\
Security Function: {Security_Function}\n\
Category: {CSF_Category}\n\
Technology: {Technology}\n\
Maturity Level: {Maturity_Level}\n\
IPAC: {IPAC}\n\
Defender: {Defender}\n\
Prerequisite {Prerequisites}:\n\
---\n\
\n\
## Description\n\
\n\
{Description}\n\
\n\
## Implementation\n\
\n\
{Implementation}\n\
\n\
## Verification of Control\n\
\n\
{Verification}\n\
\n\
## References:\n\
\n\
{References}".format(Security_Function = control['Security Function'],
CSF_Category = control['CSF Category'],
Technology = control["Technology"],
Maturity_Level = control['Maturity Level'],
IPAC = ''.join(str(i) for i in control['IPAC']),
Defender = ' '.join(str(d) for d in control['Defender']),
Prerequisites = '- \n'.join(str(p) for p in control['Prerequisites']),
Description = control['Description'],
Implementation = control['Implementation'],
Verification = '\n'.join(str(v) for v in control['Verification']),
References = '\n'.join(str(r) for r in control['References']))
return {'filename': filename, 'content': md}
def make_flat(self) -> list :
cbas_flat = [ ]
control_flat = {}
for control in self.cbas["Controls"]:
control_flat['Shortcode'] = control['Shortcode']
control_flat['Security Function'] = control['Security Function']
control_flat['CSF Category'] = control['CSF Category']
control_flat['Technology'] = control['Technology']
control_flat['Maturity Level'] = control['Maturity Level']
control_flat['IPAC'] = "".join(control['IPAC'])
control_flat['Defender'] = '\n'.join(control['Defender'])
control_flat['Prerequisites'] = '\n'.join(control['Prerequisites'])
control_flat['Description'] = control['Description']
control_flat['Implementation'] = control['Implementation']
control_flat['Verification'] = '\n'.join(control['Verification'])
control_flat['References'] = '\n'.join(control['References'])
cbas_flat.append(copy.copy(control_flat))
control_flat = {}
return cbas_flat
def get_ControlIdentifier_for_lang(self, identifier: str, lang = None) -> str:
if identifier not in self.config.keys():
return identifier
return self.config[identifier][self.source_language if lang is None else lang]