Write the file to disk.
(logger, storage_manager, cert, save_location, save_type, source)
| 362 | |
| 363 | |
| 364 | def write_file(logger, storage_manager, cert, save_location, save_type, source): |
| 365 | """ |
| 366 | Write the file to disk. |
| 367 | """ |
| 368 | try: |
| 369 | c_file = crypto.load_certificate( |
| 370 | crypto.FILETYPE_PEM, |
| 371 | "-----BEGIN CERTIFICATE-----\n" |
| 372 | + cert["raw"] |
| 373 | + "\n-----END CERTIFICATE-----", |
| 374 | ) |
| 375 | except: |
| 376 | # Once in awhile, it won't decode as PEM. Trying ASN1... |
| 377 | try: |
| 378 | c_file = crypto.load_certificate( |
| 379 | crypto.FILETYPE_ASN1, base64.b64decode(cert["raw"]) |
| 380 | ) |
| 381 | except: |
| 382 | logger.error( |
| 383 | "ERROR: Couldn't write the file but it is saved in the DB. Skipping the write to disk operation." |
| 384 | ) |
| 385 | return |
| 386 | |
| 387 | if save_type == "PEM": |
| 388 | new_file = crypto.dump_certificate(crypto.FILETYPE_PEM, c_file) |
| 389 | if storage_manager.storage_location == storage_manager.LOCAL_FILESYSTEM: |
| 390 | storage_manager.write_file( |
| 391 | save_location + "ct-" + source, |
| 392 | str(cert[source + "_id"]) + ".pem", |
| 393 | new_file, |
| 394 | ) |
| 395 | else: |
| 396 | storage_manager.write_file( |
| 397 | "ct-" + source, str(cert[source + "_id"]) + ".pem", new_file |
| 398 | ) |
| 399 | else: |
| 400 | new_file = crypto.dump_certificate(crypto.FILETYPE_ASN1, c_file) |
| 401 | if storage_manager.storage_location == storage_manager.LOCAL_FILESYSTEM: |
| 402 | storage_manager.write_file( |
| 403 | save_location + "ct-" + source, |
| 404 | str(cert[source + "_id"]) + ".der", |
| 405 | new_file, |
| 406 | ) |
| 407 | else: |
| 408 | storage_manager.write_file( |
| 409 | "ct-" + source, str(cert[source + "_id"]) + ".der", new_file |
| 410 | ) |
| 411 | |
| 412 | |
| 413 | def check_save_location(storage_manager, save_location, source): |