1 | import os |
||
2 | from skf import settings |
||
3 | from shutil import copyfile |
||
4 | from flask import Flask |
||
0 ignored issues
–
show
|
|||
5 | from sqlite3 import dbapi2 as sqlite3 |
||
6 | |||
7 | |||
8 | app = Flask(__name__) |
||
9 | |||
10 | def connect_db(): |
||
11 | """Connects to the specific database.""" |
||
12 | rv = sqlite3.connect(os.path.join(app.root_path, settings.DATABASE)) |
||
13 | rv.row_factory = sqlite3.Row |
||
14 | return rv |
||
15 | |||
16 | |||
17 | def init_db(): |
||
18 | """Initializes the database.""" |
||
19 | try: |
||
20 | os.remove(os.path.join(app.root_path, settings.DATABASE)) |
||
21 | open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') |
||
22 | os.remove(os.path.join(app.root_path, 'db.sqlite_schema')) |
||
23 | copyfile(os.path.join(app.root_path, "schema.sql"), os.path.join(app.root_path, 'db.sqlite_schema')) |
||
24 | init_md_checklists() |
||
25 | init_md_code_examples() |
||
26 | init_md_knowledge_base() |
||
27 | db = connect_db() |
||
28 | with app.open_resource(os.path.join(app.root_path, 'db.sqlite_schema'), mode='r') as f: |
||
29 | db.cursor().executescript(f.read()) |
||
30 | db.commit() |
||
31 | return True |
||
32 | except: |
||
33 | return False |
||
34 | |||
35 | |||
36 | def update_db(): |
||
37 | """Update the database.""" |
||
38 | try: |
||
39 | os.remove(os.path.join(app.root_path, 'db.sqlite_schema')) |
||
40 | db = connect_db() |
||
41 | db.session.delete("TRUNCATE TABLE kb_items") |
||
42 | db.session.delete("TRUNCATE TABLE code_items") |
||
43 | db.session.delete("TRUNCATE TABLE checklists") |
||
44 | db.session.commit() |
||
45 | |||
46 | init_md_checklists() |
||
47 | init_md_code_examples() |
||
48 | init_md_knowledge_base() |
||
49 | |||
50 | with app.open_resource(os.path.join(app.root_path, 'db.sqlite_schema'), mode='r') as f: |
||
51 | db.cursor().executescript(f.read()) |
||
52 | db.commit() |
||
53 | return True |
||
54 | except: |
||
55 | return False |
||
56 | |||
57 | |||
58 | def get_db(): |
||
59 | """Opens a new database connection if there is none yet for the current application context.""" |
||
60 | if not hasattr(g, settings.DATABASE): |
||
61 | g.sqlite_db = connect_db() |
||
62 | return g.sqlite_db |
||
63 | |||
64 | |||
65 | def init_md_knowledge_base(): |
||
66 | """Converts markdown knowledge-base items to DB.""" |
||
67 | kb_dir = os.path.join(app.root_path, 'markdown/knowledge_base') |
||
68 | try: |
||
69 | for filename in os.listdir(kb_dir): |
||
70 | if filename.endswith(".md"): |
||
71 | name_raw = filename.split("-") |
||
72 | kbID = name_raw[0].replace("_", " ") |
||
73 | title = name_raw[3].replace("_", " ") |
||
74 | file = os.path.join(kb_dir, filename) |
||
75 | data = open(file, 'r') |
||
76 | file_content = data.read() |
||
77 | data.close() |
||
78 | content_escaped = file_content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
79 | query = "INSERT OR REPLACE INTO kb_items (kbID, content, title) VALUES ('"+kbID+"','"+content_escaped+"', '"+title+"'); \n" |
||
80 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
81 | myfile.write(query) |
||
82 | print('Initialized the markdown knowledge-base.') |
||
83 | return True |
||
84 | except: |
||
85 | return False |
||
86 | |||
87 | |||
88 | def init_md_code_examples(): |
||
89 | """Converts markdown code-example items to DB.""" |
||
90 | kb_dir = os.path.join(app.root_path, 'markdown/code_examples/') |
||
91 | code_langs = ['asp', 'java', 'php', 'flask', 'django', 'go', 'ruby'] |
||
92 | try: |
||
93 | for lang in code_langs: |
||
94 | for filename in os.listdir(kb_dir+lang): |
||
95 | if filename.endswith(".md"): |
||
96 | name_raw = filename.split("-") |
||
97 | title = name_raw[3].replace("_", " ") |
||
98 | file = os.path.join(kb_dir+lang, filename) |
||
99 | data = open(file, 'r') |
||
100 | file_content = data.read() |
||
101 | data.close() |
||
102 | content_escaped = file_content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
103 | query = "INSERT OR REPLACE INTO code_items (content, title, code_lang) VALUES ('"+content_escaped+"', '"+title+"', '"+lang+"'); \n" |
||
104 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
105 | myfile.write(query) |
||
106 | print('Initialized the markdown code-example.') |
||
107 | return True |
||
108 | except: |
||
109 | return False |
||
110 | |||
111 | |||
112 | def init_md_checklists(): |
||
113 | """Converts markdown checklists items to DB.""" |
||
114 | kb_dir = os.path.join(app.root_path, 'markdown/checklists/') |
||
115 | try: |
||
116 | checklists = ['asvs', 'custom', 'masvs', 'pcidss32'] |
||
117 | for checklist in checklists: |
||
118 | if checklist == "asvs": |
||
119 | for filename in os.listdir(kb_dir+checklist): |
||
120 | if filename.endswith(".md"): |
||
121 | name_raw = filename.split("-") |
||
122 | level = name_raw[4].replace("_", " ") |
||
123 | kbid_raw = name_raw[6].split(".") |
||
124 | kb_id = kbid_raw[0] |
||
125 | if level == "0": |
||
126 | # For the ASVS categories |
||
127 | file = os.path.join(kb_dir+checklist, filename) |
||
128 | data = open(file, 'r') |
||
129 | file_content = data.read() |
||
130 | data.close() |
||
131 | checklistID_raw = file_content.split(":") |
||
132 | checklistID = checklistID_raw[0] |
||
133 | checklistID = checklistID.lstrip('V') |
||
134 | checklistID = checklistID+".0" |
||
135 | else : |
||
136 | # For the ASVS items |
||
137 | file = os.path.join(kb_dir+checklist, filename) |
||
138 | data = open(file, 'r') |
||
139 | file_content = data.read() |
||
140 | data.close() |
||
141 | checklistID_raw = file_content.split(" ") |
||
142 | checklistID = checklistID_raw[0] |
||
143 | file = os.path.join(kb_dir+checklist, filename) |
||
144 | data = open(file, 'r') |
||
145 | file_content = data.read() |
||
146 | data.close() |
||
147 | content = file_content.split(' ', 1)[1] |
||
148 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
149 | query = "INSERT OR REPLACE INTO checklists (checklist_type, checklistID, content, level, kbID) VALUES (0, '"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
150 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
151 | myfile.write(query) |
||
152 | if checklist == 'masvs': |
||
153 | for filename in os.listdir(kb_dir+checklist): |
||
154 | if filename.endswith(".md"): |
||
155 | name_raw = filename.split("-") |
||
156 | level = name_raw[4].replace("_", " ") |
||
157 | kbid_raw = name_raw[6].split(".") |
||
158 | kb_id = kbid_raw[0] |
||
159 | if level == "0": |
||
160 | # For the MASVS categories |
||
161 | file = os.path.join(kb_dir+checklist, filename) |
||
162 | data = open(file, 'r') |
||
163 | file_content = data.read() |
||
164 | data.close() |
||
165 | checklistID_raw = file_content.split(":") |
||
166 | checklistID = checklistID_raw[0] |
||
167 | checklistID = checklistID.lstrip('V') |
||
168 | checklistID = checklistID+".0" |
||
169 | else : |
||
170 | # For the MASVS items |
||
171 | file = os.path.join(kb_dir+checklist, filename) |
||
172 | data = open(file, 'r') |
||
173 | file_content = data.read() |
||
174 | data.close() |
||
175 | checklistID_raw = file_content.split(" ") |
||
176 | checklistID = checklistID_raw[0] |
||
177 | file = os.path.join(kb_dir+checklist, filename) |
||
178 | data = open(file, 'r') |
||
179 | file_content = data.read() |
||
180 | data.close() |
||
181 | content = file_content.split(' ', 1)[1] |
||
182 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
183 | query = "INSERT OR REPLACE INTO checklists (checklist_type, checklistID, content, level, kbID) VALUES (1, '"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
184 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
185 | myfile.write(query) |
||
186 | if checklist == "pcidss32": |
||
187 | for filename in os.listdir(kb_dir+checklist): |
||
188 | if filename.endswith(".md"): |
||
189 | name_raw = filename.split("-") |
||
190 | level = name_raw[4].replace("_", " ") |
||
191 | kbid_raw = name_raw[6].split(".") |
||
192 | kb_id = kbid_raw[0] |
||
193 | if level == "0": |
||
194 | # For the pcidss32 categories |
||
195 | file = os.path.join(kb_dir+checklist, filename) |
||
196 | data = open(file, 'r') |
||
197 | file_content = data.read() |
||
198 | data.close() |
||
199 | checklistID_raw = file_content.split(":") |
||
200 | checklistID = checklistID_raw[0] |
||
201 | checklistID = checklistID.lstrip('V') |
||
202 | checklistID = checklistID+".0" |
||
203 | else : |
||
204 | # For the pcidss32 items |
||
205 | file = os.path.join(kb_dir+checklist, filename) |
||
206 | data = open(file, 'r') |
||
207 | file_content = data.read() |
||
208 | data.close() |
||
209 | checklistID_raw = file_content.split(" ") |
||
210 | checklistID = checklistID_raw[0] |
||
211 | file = os.path.join(kb_dir+checklist, filename) |
||
212 | data = open(file, 'r') |
||
213 | file_content = data.read() |
||
214 | data.close() |
||
215 | content = file_content.split(' ', 1)[1] |
||
216 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
217 | query = "INSERT OR REPLACE INTO checklists (checklist_type, checklistID, content, level, kbID) VALUES (2, '"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
218 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
219 | myfile.write(query) |
||
220 | if checklist == "custom": |
||
221 | for filename in os.listdir(kb_dir+checklist): |
||
222 | if filename.endswith(".md"): |
||
223 | name_raw = filename.split("-") |
||
224 | level = name_raw[4].replace("_", " ") |
||
225 | kbid_raw = name_raw[6].split(".") |
||
226 | kb_id = kbid_raw[0] |
||
227 | if level == "0": |
||
228 | # For the custom categories |
||
229 | file = os.path.join(kb_dir+checklist, filename) |
||
230 | data = open(file, 'r') |
||
231 | file_content = data.read() |
||
232 | data.close() |
||
233 | checklistID_raw = file_content.split(":") |
||
234 | checklistID = checklistID_raw[0] |
||
235 | checklistID = checklistID.lstrip('V') |
||
236 | checklistID = checklistID+".0" |
||
237 | else : |
||
238 | # For the custom items |
||
239 | file = os.path.join(kb_dir+checklist, filename) |
||
240 | data = open(file, 'r') |
||
241 | file_content = data.read() |
||
242 | data.close() |
||
243 | checklistID_raw = file_content.split(" ") |
||
244 | checklistID = checklistID_raw[0] |
||
245 | file = os.path.join(kb_dir+checklist, filename) |
||
246 | data = open(file, 'r') |
||
247 | file_content = data.read() |
||
248 | data.close() |
||
249 | content = file_content.split(' ', 1)[1] |
||
250 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
251 | query = "INSERT OR REPLACE INTO checklists (checklist_type, checklistID, content, level, kbID) VALUES (3, '"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
252 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
253 | myfile.write(query) |
||
254 | print('Initialized the markdown checklists.') |
||
255 | return True |
||
256 | except Exception as e: |
||
257 | print('Exception in file db_tools, method init_md_checklists: ' + e) |
||
258 | return False |
||
259 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.