ssh_vault/cli/actions/
view.rs1use crate::cli::actions::Action;
2use crate::vault::{SshVault, dio, find, parse, ssh::decrypt_private_key};
3use anyhow::Result;
4use std::io::{Read, Write};
5use zeroize::Zeroize;
6
7pub fn handle(action: Action) -> Result<()> {
8 match action {
9 Action::View {
10 key,
11 output,
12 vault,
13 passphrase,
14 } => {
15 let mut data = String::new();
16
17 let (mut input, mut output) = dio::setup_io(vault, output)?;
19
20 input.read_to_string(&mut data)?;
21
22 let (key_type, fingerprint, password, data) = parse(&data)?;
24
25 let mut private_key = find::private_key_type(key, key_type)?;
27
28 if private_key.is_encrypted() {
30 private_key = decrypt_private_key(&private_key, passphrase)?;
31 }
32
33 let key_type = find::key_type(&private_key.algorithm())?;
35
36 let vault = SshVault::new(&key_type, None, Some(private_key))?;
37
38 let mut data = vault.view(&password, &data, &fingerprint)?;
39
40 output.write_all(data.as_bytes())?;
41
42 data.zeroize();
44 }
45 _ => unreachable!(),
46 }
47 Ok(())
48}