rash_core 2.21.0

Declarative shell scripting using Rust native bindings
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
515
516
517
/// ANCHOR: module
/// # certbot
///
/// Manage SSL/TLS certificates using Let's Encrypt via certbot.
///
/// This module automates obtaining and renewing TLS certificates from Let's Encrypt
/// using the certbot tool. It supports both HTTP-01 and DNS-01 challenges and is
/// idempotent — it will only request a new certificate when one does not already exist
/// or is within `expire_days` of expiration.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: full
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Examples
///
/// ```yaml
/// - name: Obtain certificate for example.com
///   certbot:
///     domains:
///       - example.com
///       - www.example.com
///     email: admin@example.com
///
/// - name: Obtain certificate with DNS challenge
///   certbot:
///     domains:
///       - example.com
///     email: admin@example.com
///     challenge: dns
///     expire_days: 14
///
/// - name: Remove certificate for example.com
///   certbot:
///     domains:
///       - example.com
///     email: admin@example.com
///     state: absent
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::modules::{Module, ModuleResult, parse_params};

#[cfg(feature = "docs")]
use rash_derive::DocJsonSchema;

use std::fs;
use std::path::Path;
use std::process::Command as StdCommand;
use std::time::{SystemTime, UNIX_EPOCH};

use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_norway::Value as YamlValue;
use serde_norway::value;

const CERTBOT_BIN: &str = "certbot";
const DEFAULT_CERT_DIR: &str = "/etc/letsencrypt/live";
const DEFAULT_EXPIRE_DAYS: u64 = 30;

#[derive(Default, Debug, Clone, Copy, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema))]
#[serde(rename_all = "lowercase")]
pub(crate) enum State {
    Absent,
    #[default]
    Present,
}

fn default_state() -> Option<State> {
    Some(State::default())
}

#[derive(Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// List of domain names for the certificate.
    pub domains: Vec<String>,
    /// Email address for Let's Encrypt registration and notifications.
    pub email: String,
    /// Challenge type to use for domain validation.
    /// **[default: `"http"`]**
    #[serde(default = "default_challenge")]
    pub challenge: String,
    /// Renew the certificate if it expires within this many days.
    /// **[default: `30`]**
    #[serde(default = "default_expire_days")]
    pub expire_days: u64,
    /// Whether the certificate should exist or not.
    /// **[default: `"present"`]**
    #[serde(default = "default_state")]
    pub state: Option<State>,
}

fn default_challenge() -> String {
    "http".to_string()
}

fn default_expire_days() -> u64 {
    DEFAULT_EXPIRE_DAYS
}

fn primary_domain(domains: &[String]) -> &str {
    domains.first().map_or("", |v| v.as_str())
}

fn fullchain_path(domain: &str) -> String {
    format!("{}/{}/fullchain.pem", DEFAULT_CERT_DIR, domain)
}

fn check_certificate_expiry(domain: &str, expire_days: u64) -> Result<bool> {
    let chain_path = fullchain_path(domain);
    if !Path::new(&chain_path).exists() {
        return Ok(true);
    }

    let output = StdCommand::new("openssl")
        .args(["x509", "-enddate", "-noout", "-in", &chain_path])
        .output()
        .map_err(|e| Error::new(ErrorKind::SubprocessFail, e))?;

    if !output.status.success() {
        return Ok(true);
    }

    let expiry_str = String::from_utf8_lossy(&output.stdout);
    let needs_renewal = parse_expiry_days(&expiry_str, expire_days)?;
    Ok(needs_renewal)
}

fn parse_expiry_days(expiry_str: &str, expire_days: u64) -> Result<bool> {
    let date_part = expiry_str
        .trim()
        .strip_prefix("notAfter=")
        .unwrap_or(expiry_str.trim());

    let output = StdCommand::new("date")
        .args(["-d", date_part, "+%s"])
        .output()
        .map_err(|e| Error::new(ErrorKind::SubprocessFail, e))?;

    if !output.status.success() {
        return Ok(true);
    }

    let expiry_ts: i64 = String::from_utf8_lossy(&output.stdout)
        .trim()
        .parse()
        .unwrap_or(0);

    let now_ts: i64 = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);

    let remaining_days = (expiry_ts - now_ts) / 86400;
    Ok(remaining_days <= expire_days as i64)
}

fn obtain_certificate(params: &Params, check_mode: bool) -> Result<ModuleResult> {
    let domain = primary_domain(&params.domains);
    let chain_path = fullchain_path(domain);

    let needs_renewal = check_certificate_expiry(domain, params.expire_days)?;

    if !needs_renewal {
        return Ok(ModuleResult {
            changed: false,
            output: Some(format!("Certificate for {} is valid", domain)),
            extra: None,
        });
    }

    if check_mode {
        return Ok(ModuleResult {
            changed: true,
            output: Some(format!(
                "Would obtain certificate for: {}",
                params.domains.join(", ")
            )),
            extra: None,
        });
    }

    let mut cmd = StdCommand::new(CERTBOT_BIN);
    cmd.args(["certonly", "--non-interactive", "--agree-tos"]);

    match params.challenge.as_str() {
        "dns" => {
            cmd.arg("--manual");
            cmd.arg("--preferred-challenges").arg("dns");
        }
        _ => {
            cmd.arg("--standalone");
            cmd.arg("--preferred-challenges").arg("http");
        }
    }

    cmd.arg("--email").arg(&params.email);

    for d in &params.domains {
        cmd.arg("-d").arg(d);
    }

    let output = cmd.output().map_err(|e| {
        Error::new(
            ErrorKind::SubprocessFail,
            format!("Failed to execute certbot: {}", e),
        )
    })?;

    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();

    if !output.status.success() {
        return Err(Error::new(
            ErrorKind::SubprocessFail,
            format!("certbot failed: {}", stderr.trim()),
        ));
    }

    let extra = Some(value::to_value(json!({
        "rc": output.status.code(),
        "stdout": stdout,
        "stderr": stderr,
        "certificate_path": chain_path,
    }))?);

    Ok(ModuleResult {
        changed: true,
        output: Some(format!("Certificate obtained for {}", domain)),
        extra,
    })
}

fn remove_certificate(params: &Params, check_mode: bool) -> Result<ModuleResult> {
    let domain = primary_domain(&params.domains);
    let chain_path = fullchain_path(domain);
    let cert_exists = Path::new(&chain_path).exists();

    if !cert_exists {
        return Ok(ModuleResult {
            changed: false,
            output: Some(format!("Certificate for {} does not exist", domain)),
            extra: None,
        });
    }

    if check_mode {
        return Ok(ModuleResult {
            changed: true,
            output: Some(format!("Would remove certificate for {}", domain)),
            extra: None,
        });
    }

    let output = StdCommand::new(CERTBOT_BIN)
        .args(["delete", "--non-interactive", "--cert-name", domain])
        .output()
        .map_err(|e| {
            Error::new(
                ErrorKind::SubprocessFail,
                format!("Failed to execute certbot: {}", e),
            )
        })?;

    let stderr = String::from_utf8_lossy(&output.stderr).to_string();

    if !output.status.success() {
        let live_dir = format!("{}/{}", DEFAULT_CERT_DIR, domain);
        let archive_dir = format!("/etc/letsencrypt/archive/{}", domain);
        let _ = fs::remove_dir_all(&live_dir);
        let _ = fs::remove_dir_all(&archive_dir);
        let _ = fs::remove_file(format!("/etc/letsencrypt/renewal/{}.conf", domain));

        if !Path::new(&fullchain_path(domain)).exists() {
            return Ok(ModuleResult {
                changed: true,
                output: Some(format!(
                    "Certificate files removed for {} (fallback)",
                    domain
                )),
                extra: None,
            });
        }

        return Err(Error::new(
            ErrorKind::SubprocessFail,
            format!("certbot delete failed: {}", stderr.trim()),
        ));
    }

    Ok(ModuleResult {
        changed: true,
        output: Some(format!("Certificate removed for {}", domain)),
        extra: None,
    })
}

#[derive(Debug)]
pub struct Certbot;

impl Module for Certbot {
    fn get_name(&self) -> &str {
        "certbot"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        let params: Params = parse_params(optional_params)?;

        if params.domains.is_empty() {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "domains must not be empty",
            ));
        }

        match params.state.unwrap_or_default() {
            State::Present => Ok((obtain_certificate(&params, check_mode)?, None)),
            State::Absent => Ok((remove_certificate(&params, check_mode)?, None)),
        }
    }

    #[cfg(feature = "docs")]
    fn get_json_schema(&self) -> Option<Schema> {
        Some(Params::get_json_schema())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_params() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
              - www.example.com
            email: admin@example.com
            challenge: http
            expire_days: 30
            state: present
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.domains, vec!["example.com", "www.example.com"]);
        assert_eq!(params.email, "admin@example.com");
        assert_eq!(params.challenge, "http");
        assert_eq!(params.expire_days, 30);
        assert_eq!(params.state, Some(State::Present));
    }

    #[test]
    fn test_parse_params_minimal() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            email: admin@example.com
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.domains, vec!["example.com"]);
        assert_eq!(params.email, "admin@example.com");
        assert_eq!(params.challenge, "http");
        assert_eq!(params.expire_days, 30);
        assert_eq!(params.state, Some(State::Present));
    }

    #[test]
    fn test_parse_params_dns_challenge() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            email: admin@example.com
            challenge: dns
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.challenge, "dns");
    }

    #[test]
    fn test_parse_params_absent() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            email: admin@example.com
            state: absent
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, Some(State::Absent));
    }

    #[test]
    fn test_parse_params_missing_domains() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            email: admin@example.com
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_parse_params_missing_email() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_parse_params_random_field() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            email: admin@example.com
            unknown_field: value
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_primary_domain() {
        let domains = vec!["example.com".to_string(), "www.example.com".to_string()];
        assert_eq!(primary_domain(&domains), "example.com");
    }

    #[test]
    fn test_fullchain_path() {
        assert_eq!(
            fullchain_path("example.com"),
            "/etc/letsencrypt/live/example.com/fullchain.pem"
        );
    }

    #[test]
    fn test_invalid_state() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains:
              - example.com
            email: admin@example.com
            state: invalid
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_empty_domains() {
        let certbot = Certbot;
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            domains: []
            email: admin@example.com
            "#,
        )
        .unwrap();
        let error = certbot
            .exec(&GlobalParams::default(), yaml, &Value::UNDEFINED, false)
            .unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }

    #[test]
    fn test_default_challenge() {
        assert_eq!(default_challenge(), "http");
    }

    #[test]
    fn test_default_expire_days() {
        assert_eq!(default_expire_days(), 30);
    }

    #[test]
    fn test_default_state() {
        assert_eq!(default_state(), Some(State::Present));
    }
}