ssh_vault/cli/actions/
view.rs

1use 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            // setup Reader(input) and Writer (output)
18            let (mut input, mut output) = dio::setup_io(vault, output)?;
19
20            input.read_to_string(&mut data)?;
21
22            // parse vault
23            let (key_type, fingerprint, password, data) = parse(&data)?;
24
25            // find the private_key using the vault header AES256 or CHACHA20-POLY1305
26            let mut private_key = find::private_key_type(key, key_type)?;
27
28            // decrypt private_key if encrypted
29            if private_key.is_encrypted() {
30                private_key = decrypt_private_key(&private_key, passphrase)?;
31            }
32
33            // RSA or ED25519
34            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            // zeroize the secret
43            data.zeroize();
44        }
45        _ => unreachable!(),
46    }
47    Ok(())
48}