ssh-cli 0.4.1

Native Rust CLI that gives LLMs (Claude Code, Cursor, Windsurf) the ability to operate remote servers via SSH over stdin/stdout
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! Cifragem at-rest de segredos no `config.toml` (GAP-009 / R-SECRETS-DEFAULT).
//!
//! Ordem de resolução da chave mestra (32 bytes):
//! 1. `SSH_CLI_SECRETS_KEY` — 64 hex chars
//! 2. `SSH_CLI_SECRETS_KEY_FILE` — arquivo com 64 hex chars
//! 3. OS keyring (`service=ssh-cli`, `user=secrets-master-key`) se `SSH_CLI_USE_KEYRING=1`
//! 4. Arquivo XDG `secrets.key` (ao lado do `config.toml`), criado automaticamente na 1ª gravação
//!
//! Opt-out (testes/debug): `SSH_CLI_ALLOW_PLAINTEXT_SECRETS=1` — não auto-cria chave;
//! serialização permanece em texto se nenhuma fonte 1–3 estiver definida.
//!
//! Com chave: serialização grava `sshcli-enc:v1:<base64(nonce||ciphertext)>`.
//!
//! **Nunca** logar ou retornar a chave ou o plaintext em erros públicos.

use crate::erros::{ErroSshCli, ResultadoSshCli};
use chacha20poly1305::aead::{Aead, KeyInit};
use chacha20poly1305::{ChaCha20Poly1305, Nonce};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use zeroize::Zeroize;

/// Prefixo de blobs cifrados no TOML.
pub const PREFIXO_ENC: &str = "sshcli-enc:v1:";

/// Nome do arquivo de chave mestra no diretório de config.
pub const NOME_ARQUIVO_CHAVE: &str = "secrets.key";

/// Override de diretório de config (ex.: `--config-dir`), para alinhar `secrets.key`.
static DIR_CONFIG_OVERRIDE: Mutex<Option<PathBuf>> = Mutex::new(None);

/// Define o diretório de config para resolver `secrets.key` (one-shot; chamado no `executar`).
pub fn definir_diretorio_config(dir: Option<PathBuf>) {
    if let Ok(mut g) = DIR_CONFIG_OVERRIDE.lock() {
        *g = dir;
    }
}

/// Fonte da chave mestra (sem expor material).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FonteChave {
    /// Ausente — at-rest em texto (só com opt-out ou antes da 1ª gravação).
    Nenhuma,
    /// Variável `SSH_CLI_SECRETS_KEY`.
    Env,
    /// Arquivo `SSH_CLI_SECRETS_KEY_FILE`.
    Arquivo,
    /// OS keyring.
    Keyring,
    /// Arquivo XDG / config-dir `secrets.key`.
    XdgArquivo,
}

impl FonteChave {
    /// Nome estável para JSON/doctor.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Nenhuma => "none",
            Self::Env => "env",
            Self::Arquivo => "file",
            Self::Keyring => "keyring",
            Self::XdgArquivo => "xdg_file",
        }
    }
}

/// Relatório de modo de segredos (sem material sensível).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StatusSegredos {
    /// Fonte da chave mestra.
    pub fonte: FonteChave,
    /// Se true, serialização cifra secrets.
    pub cifragem_ativa: bool,
    /// Path do `secrets.key` (pode não existir ainda).
    pub key_file_path: PathBuf,
    /// Se true, opt-out de plaintext está ativo.
    pub plaintext_opt_out: bool,
}

/// True se `SSH_CLI_ALLOW_PLAINTEXT_SECRETS` pede plaintext.
#[must_use]
pub fn plaintext_permitido() -> bool {
    std::env::var("SSH_CLI_ALLOW_PLAINTEXT_SECRETS")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false)
}

/// Diretório de config usado para `secrets.key` (override > SSH_CLI_HOME > XDG).
pub fn diretorio_config_segredos() -> ResultadoSshCli<PathBuf> {
    if let Ok(g) = DIR_CONFIG_OVERRIDE.lock() {
        if let Some(ref d) = *g {
            return Ok(d.clone());
        }
    }
    if let Ok(home) = std::env::var("SSH_CLI_HOME") {
        if home.contains("..") {
            return Err(ErroSshCli::ArgumentoInvalido(
                "SSH_CLI_HOME não pode conter '..'".to_string(),
            ));
        }
        return Ok(PathBuf::from(home));
    }
    let dirs = directories::ProjectDirs::from("", "", "ssh-cli").ok_or_else(|| {
        ErroSshCli::Generico("não foi possível resolver diretório de config".to_string())
    })?;
    Ok(dirs.config_dir().to_path_buf())
}

/// Path canônico do arquivo de chave mestra local.
pub fn caminho_secrets_key() -> ResultadoSshCli<PathBuf> {
    Ok(diretorio_config_segredos()?.join(NOME_ARQUIVO_CHAVE))
}

/// Resolve chave mestra e origem (não auto-cria).
pub fn carregar_chave_mestra() -> ResultadoSshCli<(Option<[u8; 32]>, FonteChave)> {
    if let Ok(hex) = std::env::var("SSH_CLI_SECRETS_KEY") {
        let chave = parse_hex_chave(hex.trim()).map_err(|e| {
            ErroSshCli::ArgumentoInvalido(format!("SSH_CLI_SECRETS_KEY inválida: {e}"))
        })?;
        return Ok((Some(chave), FonteChave::Env));
    }

    if let Ok(path) = std::env::var("SSH_CLI_SECRETS_KEY_FILE") {
        let texto = std::fs::read_to_string(&path).map_err(|e| {
            ErroSshCli::ArgumentoInvalido(format!("falha lendo SSH_CLI_SECRETS_KEY_FILE: {e}"))
        })?;
        let chave = parse_hex_chave(texto.trim()).map_err(|e| {
            ErroSshCli::ArgumentoInvalido(format!("SSH_CLI_SECRETS_KEY_FILE inválida: {e}"))
        })?;
        return Ok((Some(chave), FonteChave::Arquivo));
    }

    if std::env::var("SSH_CLI_USE_KEYRING")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false)
    {
        match ler_keyring() {
            Ok(Some(chave)) => return Ok((Some(chave), FonteChave::Keyring)),
            Ok(None) => {}
            Err(e) => {
                tracing::warn!(erro = %e, "keyring indisponível; tentando secrets.key");
            }
        }
    }

    let path = caminho_secrets_key()?;
    if path.is_file() {
        let texto = std::fs::read_to_string(&path)
            .map_err(|e| ErroSshCli::Generico(format!("falha lendo {}: {e}", path.display())))?;
        let chave = parse_hex_chave(texto.trim())
            .map_err(|e| ErroSshCli::ArgumentoInvalido(format!("secrets.key inválida: {e}")))?;
        return Ok((Some(chave), FonteChave::XdgArquivo));
    }

    Ok((None, FonteChave::Nenhuma))
}

/// Garante chave para **escrita**: carrega existente ou auto-cria `secrets.key`
/// (salvo opt-out plaintext).
pub fn garantir_chave_para_escrita() -> ResultadoSshCli<(Option<[u8; 32]>, FonteChave)> {
    let (existente, fonte) = carregar_chave_mestra()?;
    if existente.is_some() {
        return Ok((existente, fonte));
    }
    if plaintext_permitido() {
        return Ok((None, FonteChave::Nenhuma));
    }
    let path = caminho_secrets_key()?;
    let hex = gerar_hex_chave()?;
    gravar_chave_arquivo(&path, &hex, false)?;
    let chave = parse_hex_chave(&hex)
        .map_err(|e| ErroSshCli::Generico(format!("chave gerada inválida: {e}")))?;
    Ok((Some(chave), FonteChave::XdgArquivo))
}

/// Status atual (sem carregar material em logs).
pub fn status_segredos() -> ResultadoSshCli<StatusSegredos> {
    let key_file_path = caminho_secrets_key()?;
    let (chave, fonte) = carregar_chave_mestra()?;
    let cifragem_ativa = chave.is_some();
    if let Some(mut k) = chave {
        k.zeroize();
    }
    Ok(StatusSegredos {
        fonte,
        cifragem_ativa,
        key_file_path,
        plaintext_opt_out: plaintext_permitido(),
    })
}

/// True se a string já é blob cifrado.
#[must_use]
pub fn eh_blob_cifrado(valor: &str) -> bool {
    valor.starts_with(PREFIXO_ENC)
}

/// Serializa um segredo para TOML: cifra se houver (ou auto-criar) chave; senão plaintext.
///
/// Segredo **vazio** nunca vira blob `sshcli-enc` (GAP-SSH-EXP-001): export redacted zera
/// senhas e deve gravar `""` legível, não ciphertext de string vazia (que engana import
/// em outra máquina sem a master-key e finge "secret present").
pub fn serializar_segredo(plaintext: &str) -> ResultadoSshCli<String> {
    if plaintext.is_empty() {
        return Ok(String::new());
    }
    let (chave, _) = garantir_chave_para_escrita()?;
    match chave {
        None => Ok(plaintext.to_string()),
        Some(mut key) => {
            let out = cifrar(&key, plaintext)?;
            key.zeroize();
            Ok(out)
        }
    }
}

/// Desserializa de TOML: decifra blobs `sshcli-enc:v1:`; senão devolve como está.
pub fn deserializar_segredo(armazenado: &str) -> ResultadoSshCli<String> {
    if !eh_blob_cifrado(armazenado) {
        return Ok(armazenado.to_string());
    }
    let (chave, _) = carregar_chave_mestra()?;
    let mut key = chave.ok_or_else(|| {
        ErroSshCli::ArgumentoInvalido(
            "config contém secrets cifrados; defina SSH_CLI_SECRETS_KEY, SSH_CLI_SECRETS_KEY_FILE, SSH_CLI_USE_KEYRING=1 ou secrets.key (ssh-cli secrets init)"
                .to_string(),
        )
    })?;
    let plain = decifrar(&key, armazenado)?;
    key.zeroize();
    Ok(plain)
}

/// Gera 32 bytes aleatórios como hex 64.
pub fn gerar_hex_chave() -> ResultadoSshCli<String> {
    let mut bytes = [0u8; 32];
    getrandom::getrandom(&mut bytes)
        .map_err(|e| ErroSshCli::Generico(format!("RNG falhou: {e}")))?;
    let hex: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
    bytes.zeroize();
    Ok(hex)
}

/// Grava chave hex em arquivo com 0o600 (quando suportado).
pub fn gravar_chave_arquivo(path: &Path, hex64: &str, force: bool) -> ResultadoSshCli<()> {
    let _ = parse_hex_chave(hex64)
        .map_err(|e| ErroSshCli::ArgumentoInvalido(format!("chave inválida: {e}")))?;
    if path.exists() && !force {
        return Err(ErroSshCli::ArgumentoInvalido(format!(
            "{} já existe; use --force para sobrescrever",
            path.display()
        )));
    }
    if let Some(pai) = path.parent() {
        std::fs::create_dir_all(pai)?;
    }
    let pai = path.parent().unwrap_or_else(|| Path::new("."));
    let mut tmp = tempfile::NamedTempFile::new_in(pai)
        .map_err(|e| ErroSshCli::Generico(format!("tempfile secrets.key: {e}")))?;
    use std::io::Write;
    tmp.write_all(hex64.trim().as_bytes())
        .map_err(|e| ErroSshCli::Generico(format!("write secrets.key: {e}")))?;
    tmp.write_all(b"\n")
        .map_err(|e| ErroSshCli::Generico(format!("write secrets.key: {e}")))?;
    tmp.as_file()
        .sync_all()
        .map_err(|e| ErroSshCli::Generico(format!("fsync secrets.key: {e}")))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(0o600);
        tmp.as_file()
            .set_permissions(perms)
            .map_err(|e| ErroSshCli::Generico(format!("chmod secrets.key: {e}")))?;
    }
    tmp.persist(path)
        .map_err(|e| ErroSshCli::Generico(format!("persist secrets.key: {e}")))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600));
    }
    Ok(())
}

/// Inicializa master-key em arquivo XDG ou keyring. **Nunca** imprime a chave.
pub fn init_master_key(use_keyring: bool, force: bool) -> ResultadoSshCli<StatusSegredos> {
    let hex = gerar_hex_chave()?;
    if use_keyring {
        if !force {
            match ler_keyring() {
                Ok(Some(_)) => {
                    return Err(ErroSshCli::ArgumentoInvalido(
                        "keyring já contém master-key; use --force".to_string(),
                    ));
                }
                Ok(None) => {}
                Err(e) => return Err(e),
            }
        }
        gravar_chave_no_keyring(&hex)?;
        drop(hex);
        return status_segredos();
    }
    let path = caminho_secrets_key()?;
    gravar_chave_arquivo(&path, &hex, force)?;
    drop(hex);
    status_segredos()
}

/// Grava chave mestra (hex) no OS keyring. Não imprime a chave.
pub fn gravar_chave_no_keyring(hex64: &str) -> ResultadoSshCli<()> {
    let _ = parse_hex_chave(hex64)
        .map_err(|e| ErroSshCli::ArgumentoInvalido(format!("chave inválida: {e}")))?;
    let entry = keyring::Entry::new("ssh-cli", "secrets-master-key")
        .map_err(|e| ErroSshCli::Generico(format!("keyring Entry::new falhou: {e}")))?;
    entry
        .set_password(hex64.trim())
        .map_err(|e| ErroSshCli::Generico(format!("keyring set falhou: {e}")))?;
    Ok(())
}

fn parse_hex_chave(hex: &str) -> Result<[u8; 32], String> {
    let h = hex.trim();
    if h.len() != 64 {
        return Err("espere 64 caracteres hex (32 bytes)".to_string());
    }
    let mut out = [0u8; 32];
    for i in 0..32 {
        let byte =
            u8::from_str_radix(&h[i * 2..i * 2 + 2], 16).map_err(|_| "hex inválido".to_string())?;
        out[i] = byte;
    }
    Ok(out)
}

fn cifrar(key: &[u8; 32], plaintext: &str) -> ResultadoSshCli<String> {
    let cipher = ChaCha20Poly1305::new_from_slice(key)
        .map_err(|_| ErroSshCli::Generico("chave AEAD inválida".to_string()))?;
    let mut nonce_bytes = [0u8; 12];
    getrandom::getrandom(&mut nonce_bytes)
        .map_err(|e| ErroSshCli::Generico(format!("RNG falhou: {e}")))?;
    let nonce = Nonce::from_slice(&nonce_bytes);
    let ciphertext = cipher
        .encrypt(nonce, plaintext.as_bytes())
        .map_err(|_| ErroSshCli::Generico("falha ao cifrar segredo".to_string()))?;
    let mut packed = Vec::with_capacity(12 + ciphertext.len());
    packed.extend_from_slice(&nonce_bytes);
    packed.extend_from_slice(&ciphertext);
    Ok(format!(
        "{PREFIXO_ENC}{}",
        base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &packed)
    ))
}

fn decifrar(key: &[u8; 32], blob: &str) -> ResultadoSshCli<String> {
    let b64 = blob
        .strip_prefix(PREFIXO_ENC)
        .ok_or_else(|| ErroSshCli::Generico("blob cifrado malformado".to_string()))?;
    let packed = base64::Engine::decode(&base64::engine::general_purpose::STANDARD, b64)
        .map_err(|_| ErroSshCli::Generico("blob cifrado base64 inválido".to_string()))?;
    if packed.len() < 12 + 16 {
        return Err(ErroSshCli::Generico(
            "blob cifrado demasiado curto".to_string(),
        ));
    }
    let (nonce_bytes, ct) = packed.split_at(12);
    let cipher = ChaCha20Poly1305::new_from_slice(key)
        .map_err(|_| ErroSshCli::Generico("chave AEAD inválida".to_string()))?;
    let nonce = Nonce::from_slice(nonce_bytes);
    let plain = cipher.decrypt(nonce, ct).map_err(|_| {
        ErroSshCli::Generico("falha ao decifrar segredo (chave errada?)".to_string())
    })?;
    String::from_utf8(plain)
        .map_err(|_| ErroSshCli::Generico("segredo decifrado não é UTF-8".to_string()))
}

fn ler_keyring() -> ResultadoSshCli<Option<[u8; 32]>> {
    let entry = keyring::Entry::new("ssh-cli", "secrets-master-key")
        .map_err(|e| ErroSshCli::Generico(format!("keyring Entry::new falhou: {e}")))?;
    match entry.get_password() {
        Ok(hex) => {
            let chave = parse_hex_chave(&hex).map_err(|e| {
                ErroSshCli::ArgumentoInvalido(format!("keyring master-key inválida: {e}"))
            })?;
            Ok(Some(chave))
        }
        Err(keyring::Error::NoEntry) => Ok(None),
        Err(e) => Err(ErroSshCli::Generico(format!("keyring get falhou: {e}"))),
    }
}

#[cfg(test)]
mod testes {
    use super::*;
    use serial_test::serial;
    use tempfile::TempDir;

    fn limpar_env_chave() {
        std::env::remove_var("SSH_CLI_SECRETS_KEY");
        std::env::remove_var("SSH_CLI_SECRETS_KEY_FILE");
        std::env::remove_var("SSH_CLI_USE_KEYRING");
        std::env::remove_var("SSH_CLI_ALLOW_PLAINTEXT_SECRETS");
        std::env::remove_var("SSH_CLI_HOME");
        definir_diretorio_config(None);
    }

    /// Isola testes do XDG real (nunca poluir config do usuário).
    fn sandbox() -> TempDir {
        limpar_env_chave();
        let tmp = TempDir::new().unwrap();
        definir_diretorio_config(Some(tmp.path().to_path_buf()));
        tmp
    }

    #[test]
    #[serial]
    fn roundtrip_com_chave_env() {
        let _tmp = sandbox();
        let hex = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
        std::env::set_var("SSH_CLI_SECRETS_KEY", hex);
        let plain = "fake-test-password-not-real";
        let enc = serializar_segredo(plain).unwrap();
        assert!(eh_blob_cifrado(&enc));
        assert!(!enc.contains(plain));
        let back = deserializar_segredo(&enc).unwrap();
        assert_eq!(back, plain);
        limpar_env_chave();
    }

    #[test]
    #[serial]
    fn opt_out_mantem_plaintext() {
        let _tmp = sandbox();
        std::env::set_var("SSH_CLI_ALLOW_PLAINTEXT_SECRETS", "1");
        let plain = "fake-plaintext-only-for-unit-test";
        let out = serializar_segredo(plain).unwrap();
        assert_eq!(out, plain);
        assert!(!eh_blob_cifrado(&out));
        limpar_env_chave();
    }

    #[test]
    #[serial]
    fn default_auto_cria_secrets_key() {
        let tmp = sandbox();
        let plain = "fake-auto-enc-password";
        let enc = serializar_segredo(plain).unwrap();
        assert!(eh_blob_cifrado(&enc));
        assert!(!enc.contains(plain));
        assert!(tmp.path().join(NOME_ARQUIVO_CHAVE).is_file());
        let back = deserializar_segredo(&enc).unwrap();
        assert_eq!(back, plain);
        limpar_env_chave();
    }

    #[test]
    #[serial]
    fn blob_sem_chave_falha() {
        let tmp = sandbox();
        let hex = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
        std::env::set_var("SSH_CLI_SECRETS_KEY", hex);
        let enc = serializar_segredo("fake-secret").unwrap();
        // Remove env e qualquer secrets.key do sandbox
        limpar_env_chave();
        definir_diretorio_config(Some(tmp.path().to_path_buf()));
        let _ = std::fs::remove_file(tmp.path().join(NOME_ARQUIVO_CHAVE));
        std::env::set_var("SSH_CLI_ALLOW_PLAINTEXT_SECRETS", "1");
        let err = deserializar_segredo(&enc).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("cifrados") || msg.contains("SSH_CLI") || msg.contains("secrets"),
            "msg={msg}"
        );
        limpar_env_chave();
    }

    #[test]
    #[serial]
    fn empty_secret_never_encrypted_blob() {
        // GAP-SSH-EXP-001
        let _tmp = sandbox();
        let hex = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
        std::env::set_var("SSH_CLI_SECRETS_KEY", hex);
        let out = serializar_segredo("").unwrap();
        assert_eq!(out, "");
        assert!(!eh_blob_cifrado(&out));
        limpar_env_chave();
    }

    #[test]
    fn parse_hex_tamanho() {
        assert!(parse_hex_chave("aa").is_err());
        assert!(parse_hex_chave(
            "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"
        )
        .is_ok());
    }

    #[test]
    #[serial]
    fn init_cria_arquivo() {
        limpar_env_chave();
        let tmp = TempDir::new().unwrap();
        definir_diretorio_config(Some(tmp.path().to_path_buf()));
        let st = init_master_key(false, false).unwrap();
        assert!(st.cifragem_ativa);
        assert_eq!(st.fonte, FonteChave::XdgArquivo);
        assert!(st.key_file_path.is_file());
        limpar_env_chave();
    }
}