|
| 1 | +"""Credentials storage command handler.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from .router import register_command_handler |
| 8 | + |
| 9 | + |
| 10 | +def _migrate_credentials( |
| 11 | + app: Any, |
| 12 | + old_service: Any, |
| 13 | + new_service: Any, |
| 14 | +) -> tuple[int, int]: |
| 15 | + """Migrate credentials from old service to new service. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + Tuple of (migrated_count, error_count). |
| 19 | + """ |
| 20 | + migrated = 0 |
| 21 | + errors = 0 |
| 22 | + |
| 23 | + for conn in getattr(app, "connections", []): |
| 24 | + name = getattr(conn, "name", None) |
| 25 | + if not name: |
| 26 | + continue |
| 27 | + |
| 28 | + # Migrate database password |
| 29 | + try: |
| 30 | + db_pw = old_service.get_password(name) |
| 31 | + if db_pw: |
| 32 | + new_service.set_password(name, db_pw) |
| 33 | + migrated += 1 |
| 34 | + except Exception: |
| 35 | + errors += 1 |
| 36 | + |
| 37 | + # Migrate SSH password |
| 38 | + try: |
| 39 | + ssh_pw = old_service.get_ssh_password(name) |
| 40 | + if ssh_pw: |
| 41 | + new_service.set_ssh_password(name, ssh_pw) |
| 42 | + migrated += 1 |
| 43 | + except Exception: |
| 44 | + errors += 1 |
| 45 | + |
| 46 | + return migrated, errors |
| 47 | + |
| 48 | + |
| 49 | +def _handle_credentials_command(app: Any, cmd: str, args: list[str]) -> bool: |
| 50 | + if cmd != "credentials": |
| 51 | + return False |
| 52 | + |
| 53 | + from sqlit.domains.connections.app.credentials import ( |
| 54 | + ALLOW_PLAINTEXT_CREDENTIALS_SETTING, |
| 55 | + KeyringCredentialsService, |
| 56 | + PlaintextFileCredentialsService, |
| 57 | + build_credentials_service, |
| 58 | + is_keyring_usable, |
| 59 | + reset_credentials_service, |
| 60 | + ) |
| 61 | + |
| 62 | + value = args[0].lower() if args else "" |
| 63 | + |
| 64 | + if value == "plaintext": |
| 65 | + settings = app.services.settings_store.load_all() |
| 66 | + was_plaintext = settings.get(ALLOW_PLAINTEXT_CREDENTIALS_SETTING) is True |
| 67 | + |
| 68 | + # Try to migrate from keyring if switching |
| 69 | + migrated = 0 |
| 70 | + if not was_plaintext and is_keyring_usable(): |
| 71 | + try: |
| 72 | + old_service = KeyringCredentialsService() |
| 73 | + new_service = PlaintextFileCredentialsService() |
| 74 | + migrated, _ = _migrate_credentials(app, old_service, new_service) |
| 75 | + except Exception: |
| 76 | + pass # Migration is best-effort |
| 77 | + |
| 78 | + # Enable plaintext storage |
| 79 | + settings[ALLOW_PLAINTEXT_CREDENTIALS_SETTING] = True |
| 80 | + app.services.settings_store.save_all(settings) |
| 81 | + |
| 82 | + # Rebuild credentials service |
| 83 | + reset_credentials_service() |
| 84 | + app.services.credentials_service = build_credentials_service(app.services.settings_store) |
| 85 | + if hasattr(app.services.connection_store, "set_credentials_service"): |
| 86 | + app.services.connection_store.set_credentials_service(app.services.credentials_service) |
| 87 | + |
| 88 | + msg = "Credentials will be stored as plaintext in ~/.sqlit/ (protected folder)" |
| 89 | + if migrated > 0: |
| 90 | + msg += f" ({migrated} password(s) migrated from keyring)" |
| 91 | + app.notify(msg) |
| 92 | + return True |
| 93 | + |
| 94 | + if value == "keyring": |
| 95 | + from sqlit.shared.core.store import CONFIG_DIR |
| 96 | + |
| 97 | + settings = app.services.settings_store.load_all() |
| 98 | + was_plaintext = settings.get(ALLOW_PLAINTEXT_CREDENTIALS_SETTING) is True |
| 99 | + |
| 100 | + if not is_keyring_usable(): |
| 101 | + app.notify("Keyring unavailable. Cannot switch to keyring storage.", severity="warning") |
| 102 | + return True |
| 103 | + |
| 104 | + # Try to migrate from plaintext if switching |
| 105 | + migrated = 0 |
| 106 | + if was_plaintext: |
| 107 | + try: |
| 108 | + old_service = PlaintextFileCredentialsService() |
| 109 | + new_service = KeyringCredentialsService() |
| 110 | + migrated, _ = _migrate_credentials(app, old_service, new_service) |
| 111 | + except Exception: |
| 112 | + pass # Migration is best-effort |
| 113 | + |
| 114 | + # Clear plaintext credentials file after migration |
| 115 | + try: |
| 116 | + creds_file = CONFIG_DIR / "credentials.json" |
| 117 | + if creds_file.exists(): |
| 118 | + creds_file.unlink() |
| 119 | + except Exception: |
| 120 | + pass # Best-effort cleanup |
| 121 | + |
| 122 | + # Switch to keyring |
| 123 | + settings[ALLOW_PLAINTEXT_CREDENTIALS_SETTING] = False |
| 124 | + app.services.settings_store.save_all(settings) |
| 125 | + |
| 126 | + # Rebuild credentials service |
| 127 | + reset_credentials_service() |
| 128 | + app.services.credentials_service = build_credentials_service(app.services.settings_store) |
| 129 | + if hasattr(app.services.connection_store, "set_credentials_service"): |
| 130 | + app.services.connection_store.set_credentials_service(app.services.credentials_service) |
| 131 | + |
| 132 | + msg = "Credentials will be stored in system keyring" |
| 133 | + if migrated > 0: |
| 134 | + msg += f" ({migrated} password(s) migrated from plaintext)" |
| 135 | + app.notify(msg) |
| 136 | + return True |
| 137 | + |
| 138 | + if not value: |
| 139 | + # Show current status |
| 140 | + settings = app.services.settings_store.load_all() |
| 141 | + allow_plaintext = settings.get(ALLOW_PLAINTEXT_CREDENTIALS_SETTING) |
| 142 | + keyring_ok = is_keyring_usable() |
| 143 | + |
| 144 | + if allow_plaintext: |
| 145 | + app.notify("Credentials: plaintext (~/.sqlit/credentials.json)") |
| 146 | + elif keyring_ok: |
| 147 | + app.notify("Credentials: system keyring") |
| 148 | + else: |
| 149 | + app.notify("Credentials: keyring unavailable, passwords not persisted", severity="warning") |
| 150 | + return True |
| 151 | + |
| 152 | + app.notify("Usage: :credentials [plaintext|keyring]", severity="warning") |
| 153 | + return True |
| 154 | + |
| 155 | + |
| 156 | +register_command_handler(_handle_credentials_command) |
0 commit comments