ssh_vault/cli/actions/
edit.rs1use crate::cli::actions::{Action, process_input};
2use crate::vault::{SshVault, crypto, dio, find, parse, ssh::decrypt_private_key};
3use anyhow::Result;
4use secrecy::{SecretSlice, SecretString};
5use std::io::{Read, Write};
6
7pub fn handle(action: Action) -> Result<()> {
11 match action {
12 Action::Edit {
13 key,
14 vault,
15 passphrase,
16 } => {
17 let mut vault_data = String::new();
18
19 let (mut input, mut output) = dio::setup_io(Some(vault.clone()), Some(vault))?;
21
22 input.read_to_string(&mut vault_data)?;
24
25 let (key_type, fingerprint, password, data) = parse(&vault_data)?;
27
28 let mut private_key = find::private_key_type(key, key_type)?;
30
31 if private_key.is_encrypted() {
33 private_key = decrypt_private_key(&private_key, passphrase)?;
34 }
35
36 let key_type = find::key_type(&private_key.algorithm())?;
38
39 let vault = SshVault::new(&key_type, None, Some(private_key))?;
41
42 let secret = vault.view(&password, &data, &fingerprint)?;
44
45 let mut new_secret = Vec::new();
47
48 process_input(&mut new_secret, Some(SecretString::from(secret)))?;
50
51 let password: SecretSlice<u8> = crypto::gen_password()?;
53
54 let out = vault.create(password, &mut new_secret)?;
56
57 output.truncate()?;
59 output.write_all(out.as_bytes())?;
60 }
61 _ => unreachable!(),
62 }
63 Ok(())
64}