Revize b3c80ccb
Přidáno uživatelem David Friesecký před téměř 4 roky(ů)
src/dao/certificate_repository.py | ||
---|---|---|
1 |
from sqlite3 import Connection, Error |
|
1 |
from sqlite3 import Connection, Error, DatabaseError, IntegrityError, ProgrammingError, OperationalError, NotSupportedError
|
|
2 | 2 |
from typing import Dict, List |
3 | 3 | |
4 | 4 |
from src.exceptions.database_exception import DatabaseException |
... | ... | |
7 | 7 |
from src.model.certificate import Certificate |
8 | 8 |
from src.utils.logger import Logger |
9 | 9 | |
10 |
INTEGRITY_ERROR_MSG = "Database relational integrity corrupted." |
|
11 |
PROGRAMMING_ERROR_MSG = "Exception raised for programming errors (etc. SQL statement)." |
|
12 |
OPERATIONAL_ERROR_MSG = "Exception raised for errors that are related to the database’s operation." |
|
13 |
NOT_SUPPORTED_ERROR_MSG = "Method or database API was used which is not supported by the database" |
|
14 |
DATABASE_ERROR_MSG = "Unknown exception that are related to the database." |
|
15 |
ERROR_MSG = "Unknown exception." |
|
16 | ||
10 | 17 | |
11 | 18 |
class CertificateRepository: |
12 | 19 | |
... | ... | |
30 | 37 |
:return: the result of whether the creation was successful |
31 | 38 |
""" |
32 | 39 | |
40 |
Logger.debug("Function launched.") |
|
41 | ||
33 | 42 |
try: |
34 | 43 |
sql = (f"INSERT INTO {TAB_CERTIFICATES} " |
35 | 44 |
f"({COL_COMMON_NAME}," |
... | ... | |
66 | 75 |
values = [last_id, usage_id] |
67 | 76 |
self.cursor.execute(sql, values) |
68 | 77 |
self.connection.commit() |
69 |
except Error as e: |
|
70 |
raise DatabaseException(e) |
|
78 |
except IntegrityError: |
|
79 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
80 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
81 |
except ProgrammingError: |
|
82 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
83 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
84 |
except OperationalError: |
|
85 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
86 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
87 |
except NotSupportedError: |
|
88 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
89 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
90 |
except DatabaseError: |
|
91 |
Logger.error(DATABASE_ERROR_MSG) |
|
92 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
93 |
except Error: |
|
94 |
Logger.error(ERROR_MSG) |
|
95 |
raise DatabaseException(ERROR_MSG) |
|
71 | 96 | |
72 | 97 |
return last_id |
73 | 98 | |
... | ... | |
80 | 105 |
:return: instance of the Certificate object |
81 | 106 |
""" |
82 | 107 | |
108 |
Logger.debug("Function launched.") |
|
109 | ||
83 | 110 |
try: |
84 | 111 |
sql = (f"SELECT * FROM {TAB_CERTIFICATES} " |
85 | 112 |
f"WHERE {COL_ID} = ?") |
... | ... | |
110 | 137 |
usage_dict, |
111 | 138 |
certificate_row[5], |
112 | 139 |
certificate_row[6]) |
113 |
except Error as e: |
|
114 |
raise DatabaseException(e) |
|
140 |
except IntegrityError: |
|
141 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
142 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
143 |
except ProgrammingError: |
|
144 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
145 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
146 |
except OperationalError: |
|
147 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
148 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
149 |
except NotSupportedError: |
|
150 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
151 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
152 |
except DatabaseError: |
|
153 |
Logger.error(DATABASE_ERROR_MSG) |
|
154 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
155 |
except Error: |
|
156 |
Logger.error(ERROR_MSG) |
|
157 |
raise DatabaseException(ERROR_MSG) |
|
115 | 158 | |
116 | 159 |
return certificate |
117 | 160 | |
... | ... | |
124 | 167 |
:return: list of certificates |
125 | 168 |
""" |
126 | 169 | |
170 |
Logger.debug("Function launched.") |
|
171 | ||
127 | 172 |
try: |
128 | 173 |
sql_extension = "" |
129 | 174 |
values = [] |
... | ... | |
159 | 204 |
usage_dict, |
160 | 205 |
certificate_row[5], |
161 | 206 |
certificate_row[6])) |
162 |
except Error as e: |
|
163 |
raise DatabaseException(e) |
|
207 |
except IntegrityError: |
|
208 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
209 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
210 |
except ProgrammingError: |
|
211 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
212 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
213 |
except OperationalError: |
|
214 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
215 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
216 |
except NotSupportedError: |
|
217 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
218 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
219 |
except DatabaseError: |
|
220 |
Logger.error(DATABASE_ERROR_MSG) |
|
221 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
222 |
except Error: |
|
223 |
Logger.error(ERROR_MSG) |
|
224 |
raise DatabaseException(ERROR_MSG) |
|
164 | 225 | |
165 | 226 |
return certificates |
166 | 227 | |
... | ... | |
176 | 237 |
:return: the result of whether the updation was successful |
177 | 238 |
""" |
178 | 239 | |
240 |
Logger.debug("Function launched.") |
|
241 | ||
179 | 242 |
try: |
180 | 243 |
sql = (f"UPDATE {TAB_CERTIFICATES} " |
181 | 244 |
f"SET {COL_COMMON_NAME} = ?, " |
... | ... | |
213 | 276 |
values = [certificate_id, usage_id] |
214 | 277 |
self.cursor.execute(sql, values) |
215 | 278 |
self.connection.commit() |
216 |
except Error as e: |
|
217 |
raise DatabaseException(e) |
|
279 |
except IntegrityError: |
|
280 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
281 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
282 |
except ProgrammingError: |
|
283 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
284 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
285 |
except OperationalError: |
|
286 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
287 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
288 |
except NotSupportedError: |
|
289 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
290 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
291 |
except DatabaseError: |
|
292 |
Logger.error(DATABASE_ERROR_MSG) |
|
293 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
294 |
except Error: |
|
295 |
Logger.error(ERROR_MSG) |
|
296 |
raise DatabaseException(ERROR_MSG) |
|
218 | 297 | |
219 | 298 |
return self.cursor.rowcount > 0 |
220 | 299 | |
... | ... | |
227 | 306 |
:return: the result of whether the deletion was successful |
228 | 307 |
""" |
229 | 308 | |
309 |
Logger.debug("Function launched.") |
|
310 | ||
230 | 311 |
try: |
231 | 312 |
sql = (f"DELETE FROM {TAB_CERTIFICATES} " |
232 | 313 |
f"WHERE {COL_ID} = ?") |
233 | 314 |
values = [certificate_id] |
234 | 315 |
self.cursor.execute(sql, values) |
235 | 316 |
self.connection.commit() |
236 |
except Error as e: |
|
237 |
raise DatabaseException(e) |
|
317 |
except IntegrityError: |
|
318 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
319 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
320 |
except ProgrammingError: |
|
321 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
322 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
323 |
except OperationalError: |
|
324 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
325 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
326 |
except NotSupportedError: |
|
327 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
328 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
329 |
except DatabaseError: |
|
330 |
Logger.error(DATABASE_ERROR_MSG) |
|
331 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
332 |
except Error: |
|
333 |
Logger.error(ERROR_MSG) |
|
334 |
raise DatabaseException(ERROR_MSG) |
|
238 | 335 | |
239 | 336 |
return self.cursor.rowcount > 0 |
240 | 337 | |
... | ... | |
252 | 349 |
sqlite3.Error if an exception is thrown |
253 | 350 |
""" |
254 | 351 | |
352 |
Logger.debug("Function launched.") |
|
353 | ||
255 | 354 |
try: |
256 | 355 |
if revocation_date != "" and revocation_reason == "": |
257 | 356 |
revocation_reason = REV_REASON_UNSPECIFIED |
... | ... | |
267 | 366 |
certificate_id] |
268 | 367 |
self.cursor.execute(sql, values) |
269 | 368 |
self.connection.commit() |
270 |
except Error as e: |
|
271 |
raise DatabaseException(e) |
|
369 |
except IntegrityError: |
|
370 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
371 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
372 |
except ProgrammingError: |
|
373 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
374 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
375 |
except OperationalError: |
|
376 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
377 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
378 |
except NotSupportedError: |
|
379 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
380 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
381 |
except DatabaseError: |
|
382 |
Logger.error(DATABASE_ERROR_MSG) |
|
383 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
384 |
except Error: |
|
385 |
Logger.error(ERROR_MSG) |
|
386 |
raise DatabaseException(ERROR_MSG) |
|
272 | 387 | |
273 | 388 |
return self.cursor.rowcount > 0 |
274 | 389 | |
... | ... | |
283 | 398 |
sqlite3.Error if an exception is thrown |
284 | 399 |
""" |
285 | 400 | |
401 |
Logger.debug("Function launched.") |
|
402 | ||
286 | 403 |
try: |
287 | 404 |
sql = (f"UPDATE {TAB_CERTIFICATES} " |
288 | 405 |
f"SET {COL_REVOCATION_DATE} = '', " |
... | ... | |
291 | 408 |
values = [certificate_id] |
292 | 409 |
self.cursor.execute(sql, values) |
293 | 410 |
self.connection.commit() |
294 |
except Error as e: |
|
295 |
raise DatabaseException(e) |
|
411 |
except IntegrityError: |
|
412 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
413 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
414 |
except ProgrammingError: |
|
415 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
416 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
417 |
except OperationalError: |
|
418 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
419 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
420 |
except NotSupportedError: |
|
421 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
422 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
423 |
except DatabaseError: |
|
424 |
Logger.error(DATABASE_ERROR_MSG) |
|
425 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
426 |
except Error: |
|
427 |
Logger.error(ERROR_MSG) |
|
428 |
raise DatabaseException(ERROR_MSG) |
|
296 | 429 | |
297 | 430 |
return self.cursor.rowcount > 0 |
298 | 431 | |
... | ... | |
308 | 441 |
sqlite3.Error if an exception is thrown |
309 | 442 |
""" |
310 | 443 | |
444 |
Logger.debug("Function launched.") |
|
445 | ||
311 | 446 |
try: |
312 | 447 |
sql = (f"SELECT * FROM {TAB_CERTIFICATES} " |
313 | 448 |
f"WHERE {COL_PARENT_ID} = ? AND {COL_REVOCATION_DATE} IS NOT NULL AND {COL_REVOCATION_DATE} != ''") |
... | ... | |
338 | 473 |
usage_dict, |
339 | 474 |
certificate_row[5], |
340 | 475 |
certificate_row[6])) |
341 |
except Error as e: |
|
342 |
raise DatabaseException(e) |
|
476 |
except IntegrityError: |
|
477 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
478 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
479 |
except ProgrammingError: |
|
480 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
481 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
482 |
except OperationalError: |
|
483 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
484 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
485 |
except NotSupportedError: |
|
486 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
487 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
488 |
except DatabaseError: |
|
489 |
Logger.error(DATABASE_ERROR_MSG) |
|
490 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
491 |
except Error: |
|
492 |
Logger.error(ERROR_MSG) |
|
493 |
raise DatabaseException(ERROR_MSG) |
|
343 | 494 | |
344 | 495 |
return certificates |
345 | 496 | |
... | ... | |
355 | 506 |
sqlite3.Error if an exception is thrown |
356 | 507 |
""" |
357 | 508 | |
509 |
Logger.debug("Function launched.") |
|
510 | ||
358 | 511 |
try: |
359 | 512 |
sql = (f"SELECT * FROM {TAB_CERTIFICATES} " |
360 | 513 |
f"WHERE {COL_PARENT_ID} = ? AND {COL_ID} != ?") |
... | ... | |
385 | 538 |
usage_dict, |
386 | 539 |
certificate_row[5], |
387 | 540 |
certificate_row[6])) |
388 |
except Error as e: |
|
389 |
raise DatabaseException(e) |
|
541 |
except IntegrityError: |
|
542 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
543 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
544 |
except ProgrammingError: |
|
545 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
546 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
547 |
except OperationalError: |
|
548 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
549 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
550 |
except NotSupportedError: |
|
551 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
552 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
553 |
except DatabaseError: |
|
554 |
Logger.error(DATABASE_ERROR_MSG) |
|
555 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
556 |
except Error: |
|
557 |
Logger.error(ERROR_MSG) |
|
558 |
raise DatabaseException(ERROR_MSG) |
|
390 | 559 | |
391 | 560 |
return certificates |
392 | 561 | |
... | ... | |
397 | 566 |
:param certificate_id: target certificate ID |
398 | 567 |
:return: list of all descendants |
399 | 568 |
""" |
400 |
def dfs(children_of, this, collection: list): |
|
401 |
for child in children_of(this.certificate_id): |
|
402 |
dfs(children_of, child, collection) |
|
403 |
collection.append(this) |
|
404 | 569 | |
405 |
subtree_root = self.read(certificate_id) |
|
406 |
if subtree_root is None: |
|
407 |
return None |
|
570 |
Logger.debug("Function launched.") |
|
571 | ||
572 |
try: |
|
573 |
def dfs(children_of, this, collection: list): |
|
574 |
for child in children_of(this.certificate_id): |
|
575 |
dfs(children_of, child, collection) |
|
576 |
collection.append(this) |
|
577 | ||
578 |
subtree_root = self.read(certificate_id) |
|
579 |
if subtree_root is None: |
|
580 |
return None |
|
581 | ||
582 |
all_certs = [] |
|
583 |
dfs(self.get_all_issued_by, subtree_root, all_certs) |
|
584 |
except IntegrityError: |
|
585 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
586 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
587 |
except ProgrammingError: |
|
588 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
589 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
590 |
except OperationalError: |
|
591 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
592 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
593 |
except NotSupportedError: |
|
594 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
595 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
596 |
except DatabaseError: |
|
597 |
Logger.error(DATABASE_ERROR_MSG) |
|
598 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
599 |
except Error: |
|
600 |
Logger.error(ERROR_MSG) |
|
601 |
raise DatabaseException(ERROR_MSG) |
|
408 | 602 | |
409 |
all_certs = [] |
|
410 |
dfs(self.get_all_issued_by, subtree_root, all_certs) |
|
411 | 603 |
return all_certs |
412 | 604 | |
413 | 605 |
def get_next_id(self) -> int: |
... | ... | |
415 | 607 |
Get identifier of the next certificate that will be inserted into the database |
416 | 608 |
:return: identifier of the next certificate that will be added into the database |
417 | 609 |
""" |
418 |
# get next IDs of all tables |
|
419 |
self.cursor.execute("SELECT * FROM SQLITE_SEQUENCE") |
|
420 |
results = self.cursor.fetchall() |
|
421 | ||
422 |
# search for next ID in Certificates table and return it |
|
423 |
for result in results: |
|
424 |
if result[0] == TAB_CERTIFICATES: |
|
425 |
return result[1] + 1 # current last id + 1 |
|
426 |
# if certificates table is not present in the query results, return 1 |
|
610 | ||
611 |
Logger.debug("Function launched.") |
|
612 | ||
613 |
try: |
|
614 |
# get next IDs of all tables |
|
615 |
self.cursor.execute("SELECT * FROM SQLITE_SEQUENCE") |
|
616 |
results = self.cursor.fetchall() |
|
617 | ||
618 |
# search for next ID in Certificates table and return it |
|
619 |
for result in results: |
|
620 |
if result[0] == TAB_CERTIFICATES: |
|
621 |
return result[1] + 1 # current last id + 1 |
|
622 |
# if certificates table is not present in the query results, return 1 |
|
623 |
except IntegrityError: |
|
624 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
625 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
626 |
except ProgrammingError: |
|
627 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
628 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
629 |
except OperationalError: |
|
630 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
631 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
632 |
except NotSupportedError: |
|
633 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
634 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
635 |
except DatabaseError: |
|
636 |
Logger.error(DATABASE_ERROR_MSG) |
|
637 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
638 |
except Error: |
|
639 |
Logger.error(ERROR_MSG) |
|
640 |
raise DatabaseException(ERROR_MSG) |
|
641 | ||
427 | 642 |
return 1 |
src/dao/private_key_repository.py | ||
---|---|---|
1 |
from sqlite3 import Connection, Cursor, Error
|
|
1 |
from sqlite3 import Connection, Error, DatabaseError, IntegrityError, ProgrammingError, OperationalError, NotSupportedError
|
|
2 | 2 |
from typing import List |
3 | 3 | |
4 | 4 |
from injector import inject |
... | ... | |
6 | 6 |
from src.exceptions.database_exception import DatabaseException |
7 | 7 |
from src.model.private_key import PrivateKey |
8 | 8 |
from src.constants import * |
9 |
from src.utils.logger import Logger |
|
10 | ||
11 |
INTEGRITY_ERROR_MSG = "Database relational integrity corrupted." |
|
12 |
PROGRAMMING_ERROR_MSG = "Exception raised for programming errors (etc. SQL statement)." |
|
13 |
OPERATIONAL_ERROR_MSG = "Exception raised for errors that are related to the database’s operation." |
|
14 |
NOT_SUPPORTED_ERROR_MSG = "Method or database API was used which is not supported by the database" |
|
15 |
DATABASE_ERROR_MSG = "Unknown exception that are related to the database." |
|
16 |
ERROR_MSG = "Unknown exception." |
|
9 | 17 | |
10 | 18 | |
11 | 19 |
class PrivateKeyRepository: |
... | ... | |
41 | 49 |
self.cursor.execute(sql, values) |
42 | 50 |
last_id = self.cursor.lastrowid |
43 | 51 |
self.connection.commit() |
44 |
except Error as e: |
|
45 |
raise DatabaseException(e) |
|
52 |
except IntegrityError: |
|
53 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
54 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
55 |
except ProgrammingError: |
|
56 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
57 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
58 |
except OperationalError: |
|
59 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
60 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
61 |
except NotSupportedError: |
|
62 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
63 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
64 |
except DatabaseError: |
|
65 |
Logger.error(DATABASE_ERROR_MSG) |
|
66 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
67 |
except Error: |
|
68 |
Logger.error(ERROR_MSG) |
|
69 |
raise DatabaseException(ERROR_MSG) |
|
46 | 70 | |
47 | 71 |
return last_id |
48 | 72 | |
... | ... | |
68 | 92 |
private_key: PrivateKey = PrivateKey(private_key_row[0], |
69 | 93 |
private_key_row[1], |
70 | 94 |
private_key_row[2]) |
71 |
except Error as e: |
|
72 |
raise DatabaseException(e) |
|
95 |
except IntegrityError: |
|
96 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
97 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
98 |
except ProgrammingError: |
|
99 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
100 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
101 |
except OperationalError: |
|
102 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
103 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
104 |
except NotSupportedError: |
|
105 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
106 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
107 |
except DatabaseError: |
|
108 |
Logger.error(DATABASE_ERROR_MSG) |
|
109 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
110 |
except Error: |
|
111 |
Logger.error(ERROR_MSG) |
|
112 |
raise DatabaseException(ERROR_MSG) |
|
73 | 113 | |
74 | 114 |
return private_key |
75 | 115 | |
... | ... | |
90 | 130 |
private_keys.append(PrivateKey(private_key_row[0], |
91 | 131 |
private_key_row[1], |
92 | 132 |
private_key_row[2])) |
93 |
except Error as e: |
|
94 |
raise DatabaseException(e) |
|
133 |
except IntegrityError: |
|
134 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
135 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
136 |
except ProgrammingError: |
|
137 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
138 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
139 |
except OperationalError: |
|
140 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
141 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
142 |
except NotSupportedError: |
|
143 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
144 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
145 |
except DatabaseError: |
|
146 |
Logger.error(DATABASE_ERROR_MSG) |
|
147 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
148 |
except Error: |
|
149 |
Logger.error(ERROR_MSG) |
|
150 |
raise DatabaseException(ERROR_MSG) |
|
95 | 151 | |
96 | 152 |
return private_keys |
97 | 153 | |
... | ... | |
115 | 171 |
private_key_id] |
116 | 172 |
self.cursor.execute(sql, values) |
117 | 173 |
self.connection.commit() |
118 |
except Error as e: |
|
119 |
raise DatabaseException(e) |
|
174 |
except IntegrityError: |
|
175 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
176 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
177 |
except ProgrammingError: |
|
178 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
179 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
180 |
except OperationalError: |
|
181 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
182 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
183 |
except NotSupportedError: |
|
184 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
185 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
186 |
except DatabaseError: |
|
187 |
Logger.error(DATABASE_ERROR_MSG) |
|
188 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
189 |
except Error: |
|
190 |
Logger.error(ERROR_MSG) |
|
191 |
raise DatabaseException(ERROR_MSG) |
|
120 | 192 | |
121 | 193 |
return self.cursor.rowcount > 0 |
122 | 194 | |
... | ... | |
135 | 207 |
values = [private_key_id] |
136 | 208 |
self.cursor.execute(sql, values) |
137 | 209 |
self.connection.commit() |
138 |
except Error as e: |
|
139 |
raise DatabaseException |
|
210 |
except IntegrityError: |
|
211 |
Logger.error(INTEGRITY_ERROR_MSG) |
|
212 |
raise DatabaseException(INTEGRITY_ERROR_MSG) |
|
213 |
except ProgrammingError: |
|
214 |
Logger.error(PROGRAMMING_ERROR_MSG) |
|
215 |
raise DatabaseException(PROGRAMMING_ERROR_MSG) |
|
216 |
except OperationalError: |
|
217 |
Logger.error(OPERATIONAL_ERROR_MSG) |
|
218 |
raise DatabaseException(OPERATIONAL_ERROR_MSG) |
|
219 |
except NotSupportedError: |
|
220 |
Logger.error(NOT_SUPPORTED_ERROR_MSG) |
|
221 |
raise DatabaseException(NOT_SUPPORTED_ERROR_MSG) |
|
222 |
except DatabaseError: |
|
223 |
Logger.error(DATABASE_ERROR_MSG) |
|
224 |
raise DatabaseException(DATABASE_ERROR_MSG) |
|
225 |
except Error: |
|
226 |
Logger.error(ERROR_MSG) |
|
227 |
raise DatabaseException(ERROR_MSG) |
|
140 | 228 | |
141 | 229 |
return self.cursor.rowcount > 0 |
src/services/certificate_service.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import time |
18 | 18 | |
19 |
from src.utils.logger import Logger |
|
20 | ||
19 | 21 |
VALID_FROM_TO_DATE_FORMAT = "%d.%m.%Y %H:%M:%S" |
20 | 22 |
CA_EXTENSIONS = "basicConstraints=critical,CA:TRUE" |
21 | 23 |
CRL_EXTENSION = "crlDistributionPoints=URI:" |
... | ... | |
47 | 49 |
:param days: Number of days for which the generated cert. will be considered valid |
48 | 50 |
:return: An instance of Certificate class representing the generated root CA cert |
49 | 51 |
""" |
52 | ||
53 |
Logger.debug("Function launched.") |
|
54 | ||
50 | 55 |
if usages is None: |
51 | 56 |
usages = {} |
52 | 57 | |
... | ... | |
81 | 86 |
:param cert_type: Type of this certificate (see constants.py) |
82 | 87 |
:return: An instance of the Certificate class wrapping the values passed via method parameters |
83 | 88 |
""" |
89 | ||
90 |
Logger.debug("Function launched.") |
|
91 | ||
84 | 92 |
# parse the generated pem for subject and notBefore/notAfter fields |
85 | 93 |
# TODO this could be improved in the future in such way that calling openssl is not required to parse the dates |
86 | 94 |
subj, not_before, not_after = self.cryptography_service.parse_cert_pem(cert_pem) |
... | ... | |
108 | 116 |
:param days: Number of days for which the generated cert. will be considered valid |
109 | 117 |
:return: An instance of Certificate class representing the generated intermediate CA cert |
110 | 118 |
""" |
119 | ||
120 |
Logger.debug("Function launched.") |
|
121 | ||
111 | 122 |
if usages is None: |
112 | 123 |
usages = {} |
113 | 124 | |
... | ... | |
168 | 179 |
:param days: Number of days for which the generated cert. will be considered valid |
169 | 180 |
:return: An instance of Certificate class representing the generated cert |
170 | 181 |
""" |
182 | ||
183 |
Logger.debug("Function launched.") |
|
184 | ||
171 | 185 |
if usages is None: |
172 | 186 |
usages = {} |
173 | 187 | |
... | ... | |
204 | 218 |
:return: Instance of the Certificate class containing a certificate with the given id or `None` if such |
205 | 219 |
certificate is not found |
206 | 220 |
""" |
221 | ||
222 |
Logger.debug("Function launched.") |
|
223 | ||
207 | 224 |
return self.certificate_repository.read(unique_id) |
208 | 225 | |
209 | 226 |
def get_certificates(self, cert_type=None) -> List[Certificate]: |
... | ... | |
214 | 231 |
:return: List of instances of the Certificate class representing all certificates present in the certificate |
215 | 232 |
repository. An empty list is returned when no certificates are found. |
216 | 233 |
""" |
234 | ||
235 |
Logger.debug("Function launched.") |
|
236 | ||
217 | 237 |
return self.certificate_repository.read_all(cert_type) |
218 | 238 | |
219 | 239 |
def get_chain_of_trust(self, from_id: int, to_id: int = -1, exclude_root=True) -> List[Certificate]: |
... | ... | |
226 | 246 |
:return: a list of certificates representing the chain of trust starting with the certificate given by `from_id` |
227 | 247 |
ID |
228 | 248 |
""" |
249 | ||
250 |
Logger.debug("Function launched.") |
|
251 | ||
229 | 252 |
# read the first certificate of the chain |
230 | 253 |
start_cert = self.certificate_repository.read(from_id) |
231 | 254 | |
... | ... | |
273 | 296 |
:param unique_id: ID of specific certificate |
274 | 297 |
""" |
275 | 298 | |
299 |
Logger.debug("Function launched.") |
|
300 | ||
276 | 301 |
to_delete = self.certificate_repository.get_all_descendants_of(unique_id) |
277 | 302 |
if to_delete is None: |
303 |
Logger.error(f"No such certificate found 'ID = {unique_id}'.") |
|
278 | 304 |
raise CertificateNotFoundException(unique_id) |
279 | 305 | |
280 | 306 |
for cert in to_delete: |
281 | 307 |
try: |
282 | 308 |
self.set_certificate_revocation_status(cert.certificate_id, STATUS_REVOKED) |
283 | 309 |
except CertificateAlreadyRevokedException: |
310 |
Logger.info(f"Certificate already revoked 'ID = {unique_id}'.") |
|
284 | 311 |
# TODO log as an info/debug, not warning or above <-- perfectly legal |
285 | 312 |
pass |
286 | 313 | |
287 |
self.certificate_repository.delete(cert.certificate_id) |
|
288 |
# TODO log if not successfully deleted |
|
314 |
if not self.certificate_repository.delete(cert.certificate_id): |
|
315 |
Logger.error(f"The certificate has not been deleted 'ID = {cert.certificate_id}'.") |
|
316 | ||
289 | 317 | |
290 | 318 |
def get_certificates_issued_by(self, unique_id): |
291 | 319 |
""" |
... | ... | |
294 | 322 |
:param unique_id: target certificate ID |
295 | 323 |
:return: children of unique_id |
296 | 324 |
""" |
325 | ||
326 |
Logger.debug("Function launched.") |
|
327 | ||
297 | 328 |
try: |
298 | 329 |
if self.certificate_repository.read(unique_id) is None: |
330 |
Logger.error(f"No such certificate found 'ID = {unique_id}'.") |
|
299 | 331 |
raise CertificateNotFoundException(unique_id) |
300 | 332 |
except DatabaseException: |
333 |
Logger.error(f"No such certificate found 'ID = {unique_id}'.") |
|
301 | 334 |
raise CertificateNotFoundException(unique_id) |
302 | 335 | |
303 | 336 |
return self.certificate_repository.get_all_issued_by(unique_id) |
... | ... | |
310 | 343 |
:param id: identifier of the certificate whose status is to be changed |
311 | 344 |
:param status: new status of the certificate |
312 | 345 |
""" |
346 | ||
347 |
Logger.debug("Function launched.") |
|
348 | ||
313 | 349 |
if status not in CERTIFICATE_STATES: |
350 |
Logger.error(f"Wrong parameter, invalid status '{status}'.") |
|
314 | 351 |
raise CertificateStatusInvalidException(status) |
315 | 352 |
if reason not in CERTIFICATE_REVOCATION_REASONS: |
353 |
Logger.error(f"Wrong parameter, invalid reason '{reason}'.") |
|
316 | 354 |
raise RevocationReasonInvalidException(reason) |
317 | 355 | |
318 | 356 |
# check whether the certificate exists |
319 | 357 |
certificate = self.certificate_repository.read(id) |
320 | 358 |
if certificate is None: |
359 |
Logger.error(f"No such certificate found 'ID = {id}'.") |
|
321 | 360 |
raise CertificateNotFoundException(id) |
322 | 361 | |
323 | 362 |
updated = False |
... | ... | |
327 | 366 |
# check if the certificate is not revoked already |
328 | 367 |
revoked = self.certificate_repository.get_all_revoked_by(certificate.parent_id) |
329 | 368 |
if certificate.certificate_id in [x.certificate_id for x in revoked]: |
369 |
Logger.error(f"Certificate already revoked 'ID = {id}'.") |
|
330 | 370 |
raise CertificateAlreadyRevokedException(id) |
331 | 371 | |
332 | 372 |
revocation_timestamp = int(time.time()) |
333 | 373 |
updated = self.certificate_repository.set_certificate_revoked(id, str(revocation_timestamp), reason) |
334 | 374 | |
335 | 375 |
if not updated: |
336 |
# TODO log this |
|
376 |
Logger.error(f"Repository returned 'false' from clear_certificate_revocation() " |
|
377 |
f"or set_certificate_revoked().") |
|
337 | 378 |
raise UnknownException("Repository returned 'false' from clear_certificate_revocation() " |
338 | 379 |
"or set_certificate_revoked().") |
339 | 380 | |
... | ... | |
343 | 384 |
:param certificate: certificate instance whose Subject shall be parsed |
344 | 385 |
:return: instance of Subject class containing resulting distinguished name |
345 | 386 |
""" |
387 | ||
388 |
Logger.debug("Function launched.") |
|
389 | ||
346 | 390 |
(subject, _, _) = self.cryptography_service.parse_cert_pem(certificate.pem_data) |
347 | 391 |
return subject |
348 | 392 | |
... | ... | |
353 | 397 |
should be extracted. |
354 | 398 |
:return: a string containing the extracted public key in PEM format |
355 | 399 |
""" |
400 | ||
401 |
Logger.debug("Function launched.") |
|
402 | ||
356 | 403 |
return self.cryptography_service.extract_public_key_from_certificate(certificate.pem_data) |
357 | 404 | |
358 | 405 |
def __get_crl_endpoint(self, ca_identifier: int) -> str: |
... | ... | |
363 | 410 |
:param ca_identifier: ID of issuing authority |
364 | 411 |
:return: CRL endpoint for the given CA |
365 | 412 |
""" |
413 | ||
414 |
Logger.debug("Function launched.") |
|
415 | ||
366 | 416 |
return self.configuration.base_server_url + "/api/crl/" + str(ca_identifier) |
367 | 417 | |
368 | 418 |
def __get_ocsp_endpoint(self, ca_identifier: int) -> str: |
... | ... | |
373 | 423 |
:param ca_identifier: ID of issuing authority |
374 | 424 |
:return: OCSP endpoint for the given CA |
375 | 425 |
""" |
426 | ||
427 |
Logger.debug("Function launched.") |
|
428 | ||
376 | 429 |
return self.configuration.base_server_url + "/api/ocsp/" + str(ca_identifier) |
377 | 430 | |
378 | 431 |
src/services/cryptography.py | ||
---|---|---|
7 | 7 |
from src.model.certificate import Certificate |
8 | 8 |
from src.model.private_key import PrivateKey |
9 | 9 |
from src.model.subject import Subject |
10 |
from src.utils.logger import Logger |
|
10 | 11 |
from src.utils.temporary_file import TemporaryFile |
11 | 12 | |
12 | 13 |
# encryption method to be used when generating private keys |
... | ... | |
41 | 42 |
:param subject: subject to be converted |
42 | 43 |
:return: a dictionary containing openssl field names mapped to subject's fields |
43 | 44 |
""" |
45 | ||
46 |
Logger.debug("Function launched.") |
|
47 | ||
44 | 48 |
subj_dict = {} |
45 | 49 |
if subject.common_name is not None: |
46 | 50 |
subj_dict["CN"] = subject.common_name |
... | ... | |
71 | 75 |
:param executable: Executable to be run (defaults to openssl) |
72 | 76 |
:return: If the process ends with a zero return code then the STDOUT of the process is returned as a byte array. |
73 | 77 |
""" |
78 | ||
79 |
Logger.debug("Function launched.") |
|
80 | ||
74 | 81 |
if args is None: |
75 | 82 |
args = [] |
76 | 83 |
try: |
... | ... | |
87 | 94 |
if proc.returncode != 0: |
88 | 95 |
# if the process did not result in zero result code, then raise an exception |
89 | 96 |
if err is not None and len(err) > 0: |
97 |
Logger.error("CryptographyException") |
|
90 | 98 |
raise CryptographyException(executable, args, err.decode()) |
91 | 99 |
else: |
100 |
Logger.error("CryptographyException") |
|
92 | 101 |
raise CryptographyException(executable, args, |
93 | 102 |
f""""Execution resulted in non-zero argument""") |
94 | 103 | |
95 | 104 |
return out |
96 | 105 |
except FileNotFoundError: |
106 |
Logger.error("CryptographyException") |
|
97 | 107 |
raise CryptographyException(executable, args, f""""{executable}" not found in the current PATH.""") |
98 | 108 | |
99 | 109 |
def create_private_key(self, passphrase=None): |
... | ... | |
103 | 113 |
encrypted at all). Empty passphrase ("") also results in a key that is not encrypted. |
104 | 114 |
:return: string containing the generated private key in PEM format |
105 | 115 |
""" |
116 | ||
117 |
Logger.debug("Function launched.") |
|
118 | ||
106 | 119 |
if passphrase is None or len(passphrase) == 0: |
107 | 120 |
return self.__run_for_output(["genrsa", "2048"]).decode() |
108 | 121 |
else: |
... | ... | |
123 | 136 | |
124 | 137 |
:return: string containing the generated certificate in PEM format |
125 | 138 |
""" |
139 | ||
140 |
Logger.debug("Function launched.") |
|
141 | ||
126 | 142 |
assert key is not None |
127 | 143 |
assert subject is not None |
128 | 144 | |
... | ... | |
170 | 186 |
:return: string containing the generated certificate signing request in PEM format |
171 | 187 |
""" |
172 | 188 | |
189 |
Logger.debug("Function launched.") |
|
190 | ||
173 | 191 |
subj_param = self.__subject_to_param_format(subject) |
174 | 192 | |
175 | 193 |
args = ["req", "-new", "-subj", subj_param, "-key", "-"] |
... | ... | |
195 | 213 |
:return: string containing the generated and signed certificate in PEM format |
196 | 214 |
""" |
197 | 215 | |
216 |
Logger.debug("Function launched.") |
|
217 | ||
198 | 218 |
# concatenate CSR, issuer certificate and issuer's key (will be used in the openssl call) |
199 | 219 |
proc_input = csr + issuer_pem + issuer_key |
200 | 220 | |
... | ... | |
238 | 258 |
:param sn: serial number to be set, when "None" is set a random serial number is generated |
239 | 259 |
:return: string containing the generated certificate in PEM format |
240 | 260 |
""" |
261 | ||
262 |
Logger.debug("Function launched.") |
|
263 | ||
241 | 264 |
csr = self.__create_csr(subject, subject_key, key_pass=subject_key_pass) |
242 | 265 |
return self.__sign_csr(csr, issuer_pem, issuer_key, issuer_key_pass=issuer_key_pass, extensions=extensions, |
243 | 266 |
days=days, sn=sn) |
... | ... | |
250 | 273 |
:param certificate: certificate to be verified in PEM format |
251 | 274 |
:return: Returns `true` if the certificate is not expired, `false` when expired. |
252 | 275 |
""" |
276 | ||
277 |
Logger.debug("Function launched.") |
|
278 | ||
253 | 279 |
# call openssl to check whether the certificate is valid to this date |
254 | 280 |
args = [OPENSSL_EXECUTABLE, "x509", "-checkend", "0", "-noout", "-text", "-in", "-"] |
255 | 281 | |
... | ... | |
268 | 294 |
return False |
269 | 295 |
else: |
270 | 296 |
# the process failed because of some other reason (incorrect cert format) |
297 |
Logger.error("CryptographyException") |
|
271 | 298 |
raise CryptographyException(OPENSSL_EXECUTABLE, args, err.decode()) |
272 | 299 | |
273 | 300 |
def extract_public_key_from_private_key(self, private_key_pem: str, passphrase=None) -> str: |
... | ... | |
277 | 304 |
:param passphrase: passphrase to be provided when the supplied private key is encrypted |
278 | 305 |
:return: a string containing the extracted public key in PEM format |
279 | 306 |
""" |
307 | ||
308 |
Logger.debug("Function launched.") |
|
309 | ||
280 | 310 |
args = ["rsa", "-in", "-", "-pubout"] |
281 | 311 |
if passphrase is not None: |
282 | 312 |
args.extend(["-passin", f"pass:{passphrase}"]) |
... | ... | |
288 | 318 |
:param cert_pem: PEM data representing a certificate from which a public key should be extracted |
289 | 319 |
:return: a string containing the extracted public key in PEM format |
290 | 320 |
""" |
321 | ||
322 |
Logger.debug("Function launched.") |
|
323 | ||
291 | 324 |
# extracting public key from a certificate does not seem to require a passphrase even when |
292 | 325 |
# signed using an encrypted PK |
293 | 326 |
args = ["x509", "-in", "-", "-noout", "-pubkey"] |
... | ... | |
300 | 333 |
:param cert_pem: a certificated in a PEM format to be parsed |
301 | 334 |
:return: a tuple containing a subject, NOT_BEFORE and NOT_AFTER dates |
302 | 335 |
""" |
336 | ||
337 |
Logger.debug("Function launched.") |
|
338 | ||
303 | 339 |
# run openssl x509 to view certificate content |
304 | 340 |
args = ["x509", "-noout", "-subject", "-startdate", "-enddate", "-in", "-"] |
305 | 341 | |
... | ... | |
356 | 392 |
Get version of the OpenSSL installed on the system |
357 | 393 |
:return: version of the OpenSSL as returned from the process |
358 | 394 |
""" |
395 | ||
396 |
Logger.debug("Function launched.") |
|
397 | ||
359 | 398 |
return self.__run_for_output(["version"]).decode("utf-8") |
360 | 399 | |
361 | 400 |
def generate_crl(self, cert: Certificate, key: PrivateKey, index_file_path: str) -> str: |
... | ... | |
368 | 407 |
:param index_file_path: path to a file that contains the openssl index with all revoked certificates |
369 | 408 |
:return: CRL encoded in PEM format string |
370 | 409 |
""" |
410 | ||
411 |
Logger.debug("Function launched.") |
|
412 | ||
371 | 413 |
# openssl ca requires the .srl file to exists, therefore a dummy, unused file is created |
372 | 414 |
with TemporaryFile("serial.srl", "0") as serial_file, \ |
373 | 415 |
TemporaryFile("crl.conf", CRL_CONFIG % (index_file_path, serial_file)) as config_file, \ |
... | ... | |
392 | 434 |
:param der_ocsp_request: DER encoded OCSP Request |
393 | 435 |
:return: DER encoded OCSP Response |
394 | 436 |
""" |
437 | ||
438 |
Logger.debug("Function launched.") |
|
439 | ||
395 | 440 |
with TemporaryFile("certificate.pem", cert.pem_data) as ca_certificate, \ |
396 | 441 |
TemporaryFile("private_key.pem", key.private_key) as key_file, \ |
397 | 442 |
TemporaryFile("request.der", der_ocsp_request) as request_file: |
... | ... | |
413 | 458 |
self.message = message |
414 | 459 | |
415 | 460 |
def __str__(self): |
416 |
return f""" |
|
461 |
# TODO check log is valid here |
|
462 |
msg = f""" |
|
417 | 463 |
EXECUTABLE: {self.executable} |
418 | 464 |
ARGS: {self.args} |
419 | 465 |
MESSAGE: {self.message} |
420 | 466 |
""" |
467 | ||
468 |
Logger.error(msg) |
|
469 |
return msg |
src/services/key_service.py | ||
---|---|---|
3 | 3 |
from src.dao.private_key_repository import PrivateKeyRepository |
4 | 4 |
from src.model.private_key import PrivateKey |
5 | 5 |
from src.services.cryptography import CryptographyService |
6 |
from src.utils.logger import Logger |
|
6 | 7 | |
7 | 8 | |
8 | 9 |
class KeyService: |
... | ... | |
18 | 19 |
:param passphrase: Passphrase to be used when encrypting the PK |
19 | 20 |
:return: An instance of the <PrivateKey> class representing the generated PK |
20 | 21 |
""" |
22 | ||
23 |
Logger.debug("Function launched.") |
|
24 | ||
21 | 25 |
# generate a new private key |
22 | 26 |
private_key_pem = self.cryptography_service.create_private_key(passphrase) |
23 | 27 | |
... | ... | |
38 | 42 |
:param unique_id: ID of the PK to be found |
39 | 43 |
:return:An instance of the required PK or `None` |
40 | 44 |
""" |
45 | ||
46 |
Logger.debug("Function launched.") |
|
47 | ||
41 | 48 |
return self.private_key_repository.read(unique_id) |
42 | 49 | |
43 | 50 |
def get_keys(self, unique_ids=None): |
... | ... | |
47 | 54 |
:param unique_ids: An array containing IDs of PKs to be fetched from the repository. |
48 | 55 |
:return: A list of instances of the PrivateKey class representing the PKs found |
49 | 56 |
""" |
57 | ||
58 |
Logger.debug("Function launched.") |
|
59 | ||
50 | 60 |
if unique_ids is None: |
51 | 61 |
return self.private_key_repository.read_all() |
52 | 62 |
else: |
... | ... | |
60 | 70 |
:param unique_id: ID of specific certificate to be deleted |
61 | 71 |
:return: `True` when the deletion was successful. `False` in other case |
62 | 72 |
""" |
73 | ||
74 |
Logger.debug("Function launched.") |
|
75 | ||
63 | 76 |
return self.private_key_repository.delete(unique_id) |
64 | 77 | |
65 | 78 |
def get_public_key(self, private_key: PrivateKey): |
... | ... | |
68 | 81 |
:param private_key: private key from which a public key should be extracted |
69 | 82 |
:return: a string containing the extracted public key in PEM format |
70 | 83 |
""" |
84 | ||
85 |
Logger.debug("Function launched.") |
|
86 | ||
71 | 87 |
return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key, |
72 | 88 |
private_key.password) |
Také k dispozici: Unified diff
Re #8570 - Added logs