ssh-cli 0.5.2

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
// SPDX-License-Identifier: MIT OR Apache-2.0
#![forbid(unsafe_code)]
//! ACME (RFC 8555) via `instant-acme` — DNS-01, XDG storage, agent two-step flow.
//!
//! ## Agent-friendly flow
//!
//! 1. `tls acme issue --domain example.com --print-challenge`  
//!    Creates order, prints TXT challenge JSON, persists `order_url` under XDG.
//! 2. Operator sets `_acme-challenge.example.com` TXT to the printed value.
//! 3. `tls acme complete --domain example.com`  
//!    Resumes the **same** order via URL, `set_ready`, finalize, writes PEMs (0o600).

use std::path::Path;

use instant_acme::{
    Account, AccountCredentials, AuthorizationStatus, ChallengeType, Identifier, LetsEncrypt,
    NewAccount, NewOrder, OrderStatus, RetryPolicy,
};
use serde::{Deserialize, Serialize};

use super::paths::{
    acme_account_path, acme_domain_dir, cert_pem_path, ensure_dir, key_pem_path, order_json_path,
    write_secret_file,
};
use super::provider::ensure_provider;
use super::acme_error_map::map_acme_error;
use crate::domain::{AcmeOrderUrl, HttpsUrl, Rfc3339Utc};
use crate::errors::{SshCliError, SshCliResult};

/// ACME directory selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AcmeDirectory {
    /// Let's Encrypt production.
    #[default]
    Production,
    /// Let's Encrypt staging (no production rate-limits; untrusted chain).
    Staging,
}

impl AcmeDirectory {
    /// Validated HTTPS directory URL (from `instant-acme` constants).
    fn url(self) -> SshCliResult<HttpsUrl> {
        let raw = match self {
            Self::Production => LetsEncrypt::Production.url(),
            Self::Staging => LetsEncrypt::Staging.url(),
        };
        HttpsUrl::try_new(raw).map_err(|e| SshCliError::tls_msg(format!("ACME directory URL: {e}")))
    }

    fn label(self) -> &'static str {
        match self {
            Self::Production => "production",
            Self::Staging => "staging",
        }
    }
}

/// Persisted pending order (challenge phase) — resume via `order_url`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingOrder {
    /// Domain being ordered.
    pub domain: String,
    /// Directory label (`production` / `staging`).
    pub directory: String,
    /// ACME order URL (required to resume the same challenge token).
    pub order_url: AcmeOrderUrl,
    /// DNS TXT name (`_acme-challenge.<domain>`).
    pub dns_name: String,
    /// DNS TXT value for the challenge.
    pub dns_value: String,
    /// RFC 3339 UTC when the challenge was printed.
    pub created_at: Rfc3339Utc,
}

/// Account status for JSON/text output.
#[derive(Debug, Clone, Serialize)]
pub struct AccountStatus {
    /// Whether account credentials exist on disk.
    pub present: bool,
    /// Absolute path to account JSON.
    pub path: String,
    /// Directory used when account was created (if known).
    pub directory: Option<String>,
}

/// Certificate status for one domain.
#[derive(Debug, Clone, Serialize)]
pub struct DomainCertStatus {
    /// Domain leaf.
    pub domain: String,
    /// Cert PEM present.
    pub has_cert: bool,
    /// Key PEM present.
    pub has_key: bool,
    /// Pending order present.
    pub has_pending_order: bool,
    /// Absolute cert path when present.
    pub cert_path: Option<String>,
    /// Absolute key path when present.
    pub key_path: Option<String>,
}

/// Creates (or refuses if exists unless `force`) an ACME account under XDG.
pub async fn create_account(
    config_override: Option<&Path>,
    directory: AcmeDirectory,
    contact: &[String],
    force: bool,
) -> SshCliResult<AccountStatus> {
    ensure_provider()?;
    let path = acme_account_path(config_override)?;
    if path.exists() && !force {
        return Err(SshCliError::InvalidArgument(format!(
            "ACME account already exists at {}; use --force to replace",
            path.display()
        )));
    }
    // G-AUD-06/28: require mailto contact(s) before talking to the CA.
    if contact.is_empty() {
        return Err(SshCliError::InvalidArgument(
            "ACME account create requires at least one --contact mailto:address".into(),
        ));
    }
    for c in contact {
        let c = c.trim();
        if !c.to_ascii_lowercase().starts_with("mailto:")
            || c.len() <= "mailto:".len()
            || !c.contains('@')
        {
            return Err(SshCliError::InvalidArgument(format!(
                "invalid ACME contact '{c}'; expected mailto:user@example.com"
            )));
        }
    }
    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }

    let contacts: Vec<&str> = contact.iter().map(String::as_str).collect();
    let directory_url = directory.url()?;
    let (_account, credentials) = Account::builder()
        .map_err(|e| map_acme_error("ACME account builder", e))?
        .create(
            &NewAccount {
                contact: &contacts,
                terms_of_service_agreed: true,
                only_return_existing: false,
            },
            directory_url.as_str().to_owned(),
            None,
        )
        .await
        .map_err(|e| map_acme_error("ACME create account", e))?;

    let json = serde_json::to_vec_pretty(&credentials)
        .map_err(|e| SshCliError::tls_msg(format!("serialize ACME credentials: {e}")))?;
    write_secret_file(&path, &json)?;

    let meta_path = path.with_extension("meta.json");
    let meta = serde_json::json!({
        "directory": directory.label(),
        "created_at": Rfc3339Utc::now().to_rfc3339(),
    });
    write_secret_file(
        &meta_path,
        serde_json::to_vec_pretty(&meta)
            .map_err(|e| SshCliError::tls_msg(format!("meta: {e}")))?
            .as_slice(),
    )?;

    Ok(AccountStatus {
        present: true,
        path: path.display().to_string(),
        directory: Some(directory.label().to_owned()),
    })
}

/// Loads account presence / path without contacting ACME.
pub fn load_account_status(config_override: Option<&Path>) -> SshCliResult<AccountStatus> {
    let path = acme_account_path(config_override)?;
    let directory = std::fs::read_to_string(path.with_extension("meta.json"))
        .ok()
        .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
        .and_then(|v| v.get("directory")?.as_str().map(str::to_owned));
    Ok(AccountStatus {
        present: path.is_file(),
        path: path.display().to_string(),
        directory,
    })
}

async fn load_account(config_override: Option<&Path>) -> SshCliResult<Account> {
    ensure_provider()?;
    let path = acme_account_path(config_override)?;
    if !path.is_file() {
        return Err(SshCliError::FileNotFound(format!(
            "ACME account missing at {}; run `ssh-cli tls acme account create`",
            path.display()
        )));
    }
    let data = std::fs::read_to_string(&path)
        .map_err(|e| SshCliError::tls_msg(format!("read account: {e}")))?;
    let credentials: AccountCredentials = serde_json::from_str(&data)
        .map_err(|e| SshCliError::tls_msg(format!("parse account credentials: {e}")))?;
    Account::builder()
        .map_err(|e| map_acme_error("ACME account builder", e))?
        .from_credentials(credentials)
        .await
        .map_err(|e| map_acme_error("restore ACME account", e))
}

/// Starts DNS-01 order and returns challenge details (does **not** wait for DNS).
pub async fn acme_issue_print_challenge(
    config_override: Option<&Path>,
    domain: &str,
    directory: AcmeDirectory,
) -> SshCliResult<PendingOrder> {
    let account = load_account(config_override).await?;
    let identifiers = vec![Identifier::Dns(domain.to_owned())];
    let mut order = account
        .new_order(&NewOrder::new(identifiers.as_slice()))
        .await
        .map_err(|e| map_acme_error("ACME new_order", e))?;

    let order_url = AcmeOrderUrl::try_new(order.url())
        .map_err(|e| SshCliError::tls_msg(format!("ACME order URL: {e}")))?;
    let mut dns_name = String::new();
    let mut dns_value = String::new();

    let mut authorizations = order.authorizations();
    while let Some(result) = authorizations.next().await {
        let mut authz = result.map_err(|e| map_acme_error("ACME authz", e))?;
        match authz.status {
            AuthorizationStatus::Pending => {}
            AuthorizationStatus::Valid => continue,
            other => {
                return Err(SshCliError::InvalidArgument(format!(
                    "unexpected ACME authorization status: {other:?}"
                )));
            }
        }
        let challenge = authz
            .challenge(ChallengeType::Dns01)
            .ok_or_else(|| SshCliError::InvalidArgument("no DNS-01 challenge on order".into()))?;
        dns_name = format!("_acme-challenge.{}", challenge.identifier());
        dns_value = challenge.key_authorization().dns_value();
    }

    if dns_value.is_empty() {
        return Err(SshCliError::InvalidArgument(
            "ACME order had no pending DNS-01 challenge".into(),
        ));
    }

    let pending = PendingOrder {
        domain: domain.to_owned(),
        directory: directory.label().to_owned(),
        order_url,
        dns_name,
        dns_value,
        created_at: Rfc3339Utc::now(),
    };

    let dir = acme_domain_dir(config_override, domain)?;
    ensure_dir(&dir)?;
    let order_path = order_json_path(&dir);
    let bytes = serde_json::to_vec_pretty(&pending)
        .map_err(|e| SshCliError::tls_msg(format!("serialize pending order: {e}")))?;
    write_secret_file(&order_path, &bytes)?;

    Ok(pending)
}

/// Completes ACME after DNS TXT is published: resume order → set_ready → finalize.
pub async fn acme_complete(
    config_override: Option<&Path>,
    domain: &str,
) -> SshCliResult<DomainCertStatus> {
    let dir = acme_domain_dir(config_override, domain)?;
    let pending_path = order_json_path(&dir);
    if !pending_path.is_file() {
        return Err(SshCliError::FileNotFound(format!(
            "no pending ACME order for {domain}; run `tls acme issue --domain {domain} --print-challenge` first"
        )));
    }

    let data = std::fs::read_to_string(&pending_path)
        .map_err(|e| SshCliError::tls_msg(format!("read pending order: {e}")))?;
    let pending: PendingOrder = serde_json::from_str(&data)
        .map_err(|e| SshCliError::tls_msg(format!("parse pending order: {e}")))?;

    let account = load_account(config_override).await?;
    let mut order = account
        .order(pending.order_url.to_string_owned())
        .await
        .map_err(|e| map_acme_error("ACME resume order", e))?;

    let mut authorizations = order.authorizations();
    while let Some(result) = authorizations.next().await {
        let mut authz = result.map_err(|e| map_acme_error("ACME authz", e))?;
        match authz.status {
            AuthorizationStatus::Pending => {}
            AuthorizationStatus::Valid => continue,
            other => {
                return Err(SshCliError::InvalidArgument(format!(
                    "unexpected ACME authorization status: {other:?}"
                )));
            }
        }
        let mut challenge = authz
            .challenge(ChallengeType::Dns01)
            .ok_or_else(|| SshCliError::InvalidArgument("no DNS-01 challenge".into()))?;
        challenge
            .set_ready()
            .await
            .map_err(|e| map_acme_error("ACME set_ready", e))?;
    }

    let status = order
        .poll_ready(&RetryPolicy::default())
        .await
        .map_err(|e| map_acme_error("ACME poll_ready", e))?;
    if status != OrderStatus::Ready {
        return Err(SshCliError::InvalidArgument(format!(
            "ACME order not ready (status={status:?}); verify DNS TXT {} = {}",
            pending.dns_name, pending.dns_value
        )));
    }

    let private_key_pem = order
        .finalize()
        .await
        .map_err(|e| map_acme_error("ACME finalize", e))?;
    let cert_chain_pem = order
        .poll_certificate(&RetryPolicy::default())
        .await
        .map_err(|e| map_acme_error("ACME poll_certificate", e))?;

    ensure_dir(&dir)?;
    let cert_path = cert_pem_path(&dir);
    let key_path = key_pem_path(&dir);
    write_secret_file(&cert_path, cert_chain_pem.as_bytes())?;
    write_secret_file(&key_path, private_key_pem.as_bytes())?;
    let _ = std::fs::remove_file(pending_path);

    Ok(DomainCertStatus {
        domain: domain.to_owned(),
        has_cert: true,
        has_key: true,
        has_pending_order: false,
        cert_path: Some(cert_path.display().to_string()),
        key_path: Some(key_path.display().to_string()),
    })
}

/// Status for one domain or all under ACME dir.
pub fn acme_status(
    config_override: Option<&Path>,
    domain: Option<&str>,
) -> SshCliResult<Vec<DomainCertStatus>> {
    if let Some(d) = domain {
        return Ok(vec![domain_status(config_override, d)?]);
    }
    acme_list(config_override)
}

/// Lists all domain directories under ACME storage.
pub fn acme_list(config_override: Option<&Path>) -> SshCliResult<Vec<DomainCertStatus>> {
    let root = super::paths::resolve_tls_root(config_override)?
        .join(crate::constants::TLS_ACME_DIR_NAME);
    if !root.exists() {
        return Ok(Vec::new());
    }
    let mut out = Vec::new();
    for entry in std::fs::read_dir(&root)
        .map_err(|e| SshCliError::tls_msg(format!("list acme: {e}")))?
    {
        let entry = entry.map_err(|e| SshCliError::tls_msg(format!("list acme entry: {e}")))?;
        if !entry.file_type().map(|t| t.is_dir()).unwrap_or(false) {
            continue;
        }
        if let Some(name) = entry.file_name().to_str() {
            out.push(domain_status(config_override, name)?);
        }
    }
    out.sort_by(|a, b| a.domain.cmp(&b.domain));
    Ok(out)
}

fn domain_status(config_override: Option<&Path>, domain: &str) -> SshCliResult<DomainCertStatus> {
    let dir = acme_domain_dir(config_override, domain)?;
    let cert = cert_pem_path(&dir);
    let key = key_pem_path(&dir);
    let pending = order_json_path(&dir);
    Ok(DomainCertStatus {
        domain: domain.to_owned(),
        has_cert: cert.is_file(),
        has_key: key.is_file(),
        has_pending_order: pending.is_file(),
        cert_path: cert.is_file().then(|| cert.display().to_string()),
        key_path: key.is_file().then(|| key.display().to_string()),
    })
}