|
19 | 19 | KEY_RING_DESKTOP_USER, SSL_MODES, RESTRICTION_TYPE_DATABASES, |
20 | 20 | RESTRICTION_TYPE_SQL) |
21 | 21 | from pgadmin.utils.crypto import encrypt, decrypt |
22 | | -from pgadmin.model import db, Server |
| 22 | +from pgadmin.model import db, Server, SharedServer |
23 | 23 | from flask import current_app |
24 | 24 | from pgadmin.utils.master_password import set_masterpass_check_text |
25 | 25 | from pgadmin.utils.driver import get_driver |
@@ -440,37 +440,45 @@ def migrate_saved_passwords(master_key, master_password): |
440 | 440 | return passwords_migrated, error |
441 | 441 |
|
442 | 442 |
|
443 | | -def reencrpyt_server_passwords(user_id, old_key, new_key): |
444 | | - """ |
445 | | - This function will decrypt the saved passwords in SQLite with old key |
446 | | - and then encrypt with new key |
447 | | - """ |
| 443 | +def __reencrpyt_server_password(server, old_key, new_key): |
448 | 444 | from pgadmin.utils.driver import get_driver |
449 | 445 | driver = get_driver(config.PG_DEFAULT_DRIVER) |
450 | 446 |
|
451 | | - for server in Server.query.filter_by(user_id=user_id).all(): |
452 | | - manager = driver.connection_manager(server.id) |
453 | | - _password_check(server, manager, old_key, new_key) |
| 447 | + manager = driver.connection_manager(server.id) |
| 448 | + _password_check(server, manager, old_key, new_key) |
454 | 449 |
|
455 | | - if server.tunnel_password is not None: |
456 | | - tunnel_password = decrypt(server.tunnel_password, old_key) |
457 | | - if isinstance(tunnel_password, bytes): |
458 | | - tunnel_password = tunnel_password.decode() |
| 450 | + if server.tunnel_password is not None: |
| 451 | + tunnel_password = decrypt(server.tunnel_password, old_key) |
| 452 | + if isinstance(tunnel_password, bytes): |
| 453 | + tunnel_password = tunnel_password.decode() |
459 | 454 |
|
460 | | - tunnel_password = encrypt(tunnel_password, new_key) |
461 | | - setattr(server, 'tunnel_password', tunnel_password) |
462 | | - manager.tunnel_password = tunnel_password |
463 | | - elif manager.tunnel_password is not None: |
464 | | - tunnel_password = decrypt(manager.tunnel_password, old_key) |
| 455 | + tunnel_password = encrypt(tunnel_password, new_key) |
| 456 | + setattr(server, 'tunnel_password', tunnel_password) |
| 457 | + manager.tunnel_password = tunnel_password |
| 458 | + elif manager.tunnel_password is not None: |
| 459 | + tunnel_password = decrypt(manager.tunnel_password, old_key) |
465 | 460 |
|
466 | | - if isinstance(tunnel_password, bytes): |
467 | | - tunnel_password = tunnel_password.decode() |
| 461 | + if isinstance(tunnel_password, bytes): |
| 462 | + tunnel_password = tunnel_password.decode() |
468 | 463 |
|
469 | | - tunnel_password = encrypt(tunnel_password, new_key) |
470 | | - manager.tunnel_password = tunnel_password |
| 464 | + tunnel_password = encrypt(tunnel_password, new_key) |
| 465 | + manager.tunnel_password = tunnel_password |
471 | 466 |
|
472 | | - db.session.commit() |
473 | | - manager.update_session() |
| 467 | + db.session.commit() |
| 468 | + manager.update_session() |
| 469 | + |
| 470 | + |
| 471 | +def reencrpyt_server_passwords(user_id, old_key, new_key): |
| 472 | + """ |
| 473 | + This function will decrypt the saved passwords in SQLite with old key |
| 474 | + and then encrypt with new key |
| 475 | + """ |
| 476 | + for server in Server.query.filter_by(user_id=user_id).all(): |
| 477 | + __reencrpyt_server_password(server, old_key, new_key) |
| 478 | + |
| 479 | + # Ensure saved shared server passwords are re-encrypted. |
| 480 | + for server in SharedServer.query.filter_by(user_id=user_id).all(): |
| 481 | + __reencrpyt_server_password(server, old_key, new_key) |
474 | 482 |
|
475 | 483 |
|
476 | 484 | def remove_saved_passwords(user_id): |
|
0 commit comments