Module: Lich::Common::CLI::EncryptionModeChange
- Defined in:
- documented/common/cli/cli_encryption_mode_change.rb
Overview
Handles encryption mode changes via CLI Manages prompting for passwords and calls EntryStore domain logic
Class Method Summary collapse
-
.change_mode(new_mode, provided_password = nil) ⇒ Integer
Change encryption mode for all accounts.
Class Method Details
.change_mode(new_mode, provided_password = nil) ⇒ Integer
Change encryption mode for all accounts
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'documented/common/cli/cli_encryption_mode_change.rb', line 23 def self.change_mode(new_mode, provided_password = nil) data_dir = DATA_DIR yaml_file = Lich::Common::Authentication::EntryStore.yaml_file_path(data_dir) # Validate file exists unless File.exist?(yaml_file) puts "error: Login file not found: #{yaml_file}" Lich.log "error: CLI encryption mode change failed - file not found" return 2 end # Validate mode valid_modes = [:plaintext, :standard, :enhanced] unless valid_modes.include?(new_mode) puts "error: Invalid encryption mode: #{new_mode}" puts "Valid modes: plaintext, standard, enhanced" Lich.log "error: CLI encryption mode change failed - invalid mode: #{new_mode}" return 3 end # Load current mode begin yaml_data = YAML.load_file(yaml_file) current_mode = yaml_data['encryption_mode']&.to_sym || :plaintext account_count = yaml_data['accounts']&.length || 0 rescue StandardError => e puts "error: Failed to read login file: #{e.}" Lich.log "error: CLI encryption mode change failed - read error: #{e.}" return 1 end # Check if already in target mode if current_mode == new_mode puts "info: Already using #{new_mode} encryption mode" Lich.log "info: CLI encryption mode change - already in target mode" return 0 end puts "Changing encryption mode: #{current_mode} -> #{new_mode}" puts "Accounts to re-encrypt: #{account_count}" puts "" # Handle password requirements based on mode transition new_master_password = nil # If leaving Enhanced mode, validate current password if current_mode == :enhanced puts "Current mode is Enhanced encryption." master_password = Lich::Common::Authentication::CLIPassword.prompt_for_master_password if master_password.nil? puts "Cancelled" Lich.log "info: CLI encryption mode change cancelled by user" return 4 end # Validate the password validation_test = yaml_data['master_password_validation_test'] unless Lich::Common::GUI::MasterPasswordManager.validate_master_password( master_password, validation_test ) puts "error: Incorrect master password" Lich.log "error: CLI encryption mode change failed - password validation failed" return 1 end puts "ok: Master password validated" puts "" end # If entering Enhanced mode, get/create master password if new_mode == :enhanced new_master_password = if provided_password provided_password else Lich::Common::Authentication::CLIPassword.get_master_password_from_keychain_or_prompt end if new_master_password.nil? puts "Cancelled" Lich.log "info: CLI encryption mode change cancelled by user" return 4 end puts "ok: Master password accepted" puts "" end # Warn about plaintext mode if new_mode == :plaintext puts "warning: Plaintext mode disables encryption" puts "Passwords will be stored unencrypted and visible in the file." puts "" print "Continue? (yes/no): " input = $stdin.gets if input.nil? || input.strip.downcase != 'yes' puts "Cancelled" Lich.log "info: CLI encryption mode change cancelled by user" return 4 end puts "" end # Call domain method to perform the change success = Lich::Common::Authentication::EntryStore.change_encryption_mode( data_dir, new_mode, new_master_password ) unless success puts "error: Failed to change encryption mode" Lich.log "error: CLI encryption mode change failed at domain level" return 1 end puts "ok: Encryption mode changed: #{current_mode} -> #{new_mode}" puts "ok: #{account_count} accounts re-encrypted" if account_count > 0 Lich.log "info: CLI encryption mode change successful: #{current_mode} -> #{new_mode}" 0 rescue StandardError => e puts "error: Unexpected error during encryption mode change: #{e.}" Lich.log "error: CLI encryption mode change failed: #{e.class}: #{e.}" 1 end |