rash_core 2.18.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/// ANCHOR: module
/// # apt_repository
///
/// Manage APT repositories on Debian/Ubuntu systems.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: full
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Example
///
/// ```yaml
/// - name: Add Docker repository
///   apt_repository:
///     repo: deb https://download.docker.com/linux/ubuntu focal stable
///     state: present
///     filename: docker
///
/// - name: Add repository with custom codename
///   apt_repository:
///     repo: deb http://archive.ubuntu.com/ubuntu jammy main restricted
///     state: present
///     codename: jammy
///
/// - name: Remove old repository
///   apt_repository:
///     repo: deb http://old-repo.example.com focal main
///     state: absent
///
/// - name: Add repository without updating cache
///   apt_repository:
///     repo: deb https://example.com/repo stable main
///     update_cache: false
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::logger::{self, diff};
use crate::modules::{Module, ModuleResult, parse_params};

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

use std::fs::{OpenOptions, read_to_string};
use std::io::prelude::*;
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;

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

const SOURCES_LIST_DIR: &str = "/etc/apt/sources.list.d";
const SOURCES_LIST: &str = "/etc/apt/sources.list";

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

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

fn default_true() -> Option<bool> {
    Some(true)
}

#[derive(Debug, PartialEq, Deserialize)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// Repository string in sources.list format (required).
    pub repo: String,
    /// Whether the repository should exist or not.
    /// **[default: `"present"`]**
    #[serde(default = "default_state")]
    pub state: Option<State>,
    /// File mode for the sources list file (octal, e.g., "0644").
    /// **[default: `"0644"`]**
    pub mode: Option<String>,
    /// Run apt-get update after adding or removing the repository.
    /// **[default: `true`]**
    #[serde(default = "default_true")]
    pub update_cache: Option<bool>,
    /// Whether to validate SSL certificates when fetching the repository.
    /// **[default: `true`]**
    #[serde(default = "default_true")]
    pub validate_certs: Option<bool>,
    /// Custom filename for the sources list (without .list extension).
    /// If not specified, the repository will be added to the main sources.list.
    pub filename: Option<String>,
    /// Distribution codename override.
    pub codename: Option<String>,
}

fn normalize_repo_line(repo: &str, codename_override: Option<&str>) -> Result<String> {
    let mut repo = repo.trim().to_string();

    if let Some(codename) = codename_override {
        let output = Command::new("lsb_release")
            .arg("-sc")
            .output()
            .ok()
            .and_then(|o| {
                if o.status.success() {
                    Some(String::from_utf8_lossy(&o.stdout).trim().to_string())
                } else {
                    None
                }
            });

        if let Some(current_codename) = output {
            repo = repo.replace(&current_codename, codename);
        }
    }

    if !repo.ends_with('\n') {
        repo.push('\n');
    }

    Ok(repo)
}

fn get_sources_file_path(filename: Option<&str>) -> PathBuf {
    match filename {
        Some(name) => {
            let filename = if name.ends_with(".list") {
                name.to_string()
            } else {
                format!("{name}.list")
            };
            PathBuf::from(SOURCES_LIST_DIR).join(filename)
        }
        None => PathBuf::from(SOURCES_LIST),
    }
}

fn repo_exists_in_content(content: &str, repo_line: &str) -> bool {
    let normalized_repo = repo_line.trim();
    content.lines().any(|line| {
        let trimmed = line.trim();
        trimmed == normalized_repo
            || (trimmed.starts_with('#') && trimmed[1..].trim() == normalized_repo)
    })
}

fn repo_exists_in_file(path: &Path, repo_line: &str) -> Result<bool> {
    if !path.exists() {
        return Ok(false);
    }

    let content = read_to_string(path)?;
    Ok(repo_exists_in_content(&content, repo_line))
}

fn find_repo_line_in_content(content: &str, repo_pattern: &str) -> Option<(usize, String)> {
    let normalized_pattern = repo_pattern.trim();
    for (idx, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed == normalized_pattern
            || (trimmed.starts_with('#') && trimmed[1..].trim() == normalized_pattern)
        {
            return Some((idx, line.to_string()));
        }
    }
    None
}

fn add_repo_to_file(path: &Path, repo_line: &str, mode: u32, check_mode: bool) -> Result<bool> {
    if repo_exists_in_file(path, repo_line)? {
        return Ok(false);
    }

    let original_content = if path.exists() {
        read_to_string(path)?
    } else {
        String::new()
    };

    let mut new_content = original_content.clone();
    if !new_content.is_empty() && !new_content.ends_with('\n') {
        new_content.push('\n');
    }
    new_content.push_str(repo_line);

    diff(&original_content, &new_content);

    if !check_mode {
        if let Some(parent) = path.parent()
            && !parent.exists()
        {
            std::fs::create_dir_all(parent)?;
        }

        let mut file = OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .mode(mode)
            .open(path)?;
        file.write_all(new_content.as_bytes())?;
    }

    Ok(true)
}

fn remove_repo_from_file(path: &Path, repo_line: &str, check_mode: bool) -> Result<bool> {
    if !path.exists() {
        return Ok(false);
    }

    let original_content = read_to_string(path)?;

    let Some((line_idx, _)) = find_repo_line_in_content(&original_content, repo_line) else {
        return Ok(false);
    };

    let mut lines: Vec<String> = original_content.lines().map(|s| s.to_string()).collect();
    lines.remove(line_idx);

    let new_content = if lines.is_empty() {
        String::new()
    } else {
        format!("{}\n", lines.join("\n"))
    };

    diff(&original_content, &new_content);

    if !check_mode {
        if lines.is_empty() {
            std::fs::remove_file(path)?;
        } else {
            let mut file = OpenOptions::new().write(true).truncate(true).open(path)?;
            file.write_all(new_content.as_bytes())?;
        }
    }

    Ok(true)
}

fn run_apt_get_update(validate_certs: bool) -> Result<()> {
    let mut cmd = Command::new("apt-get");
    cmd.arg("update");

    if !validate_certs {
        cmd.env("APT_CONFIG", "/dev/null");
        cmd.arg("-o");
        cmd.arg("Acquire::https::Verify-Peer=false");
        cmd.arg("-o");
        cmd.arg("Acquire::https::Verify-Host=false");
    }

    let output = cmd.output().map_err(|e| {
        Error::new(
            ErrorKind::SubprocessFail,
            format!("Failed to run apt-get update: {e}"),
        )
    })?;

    if !output.status.success() {
        return Err(Error::new(
            ErrorKind::SubprocessFail,
            String::from_utf8_lossy(&output.stderr).to_string(),
        ));
    }

    Ok(())
}

pub fn apt_repository(params: Params, check_mode: bool) -> Result<ModuleResult> {
    trace!("params: {params:?}");

    let state = params.state.unwrap_or_default();
    let mode = match params.mode {
        Some(ref mode_str) => u32::from_str_radix(mode_str, 8).map_err(|e| {
            Error::new(
                ErrorKind::InvalidData,
                format!("Invalid mode '{mode_str}': {e}"),
            )
        })?,
        None => 0o644,
    };
    let update_cache = params.update_cache.unwrap_or(true);
    let validate_certs = params.validate_certs.unwrap_or(true);

    let repo_line = normalize_repo_line(&params.repo, params.codename.as_deref())?;

    let file_path = get_sources_file_path(params.filename.as_deref());

    let changed = match state {
        State::Present => {
            logger::add(std::slice::from_ref(&params.repo));
            add_repo_to_file(&file_path, &repo_line, mode, check_mode)?
        }
        State::Absent => {
            logger::remove(std::slice::from_ref(&params.repo));
            remove_repo_from_file(&file_path, &repo_line, check_mode)?
        }
    };

    if changed && update_cache && !check_mode {
        run_apt_get_update(validate_certs)?;
    }

    Ok(ModuleResult {
        changed,
        output: Some(file_path.to_string_lossy().to_string()),
        extra: Some(serde_norway::Value::String(repo_line.trim().to_string())),
    })
}

#[derive(Debug)]
pub struct AptRepository;

impl Module for AptRepository {
    fn get_name(&self) -> &str {
        "apt_repository"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        Ok((
            apt_repository(parse_params(optional_params)?, check_mode)?,
            None,
        ))
    }

    fn force_string_on_params(&self) -> bool {
        false
    }

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_parse_params() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            repo: deb https://example.com/repo stable main
            state: present
            filename: example
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(
            params,
            Params {
                repo: "deb https://example.com/repo stable main".to_owned(),
                state: Some(State::Present),
                mode: None,
                update_cache: Some(true),
                validate_certs: Some(true),
                filename: Some("example".to_owned()),
                codename: None,
            }
        );
    }

    #[test]
    fn test_parse_params_minimal() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            repo: deb https://example.com/repo stable main
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.repo, "deb https://example.com/repo stable main");
        assert_eq!(params.state, Some(State::Present));
        assert_eq!(params.filename, None);
    }

    #[test]
    fn test_normalize_repo_line() {
        let repo = "deb https://example.com/repo stable main";
        let normalized = normalize_repo_line(repo, None).unwrap();
        assert!(normalized.ends_with('\n'));
    }

    #[test]
    fn test_get_sources_file_path_with_filename() {
        let path = get_sources_file_path(Some("docker"));
        assert_eq!(
            path.to_str().unwrap(),
            "/etc/apt/sources.list.d/docker.list"
        );
    }

    #[test]
    fn test_get_sources_file_path_with_extension() {
        let path = get_sources_file_path(Some("custom.list"));
        assert_eq!(
            path.to_str().unwrap(),
            "/etc/apt/sources.list.d/custom.list"
        );
    }

    #[test]
    fn test_get_sources_file_path_without_filename() {
        let path = get_sources_file_path(None);
        assert_eq!(path.to_str().unwrap(), "/etc/apt/sources.list");
    }

    #[test]
    fn test_repo_exists_in_content() {
        let content = "deb https://example.com/repo stable main\ndeb http://other.com focal main\n";
        assert!(repo_exists_in_content(
            content,
            "deb https://example.com/repo stable main"
        ));
        assert!(!repo_exists_in_content(
            content,
            "deb https://notpresent.com repo main"
        ));
    }

    #[test]
    fn test_repo_exists_in_content_commented() {
        let content = "# deb https://example.com/repo stable main\n";
        assert!(repo_exists_in_content(
            content,
            "deb https://example.com/repo stable main"
        ));
    }

    #[test]
    fn test_find_repo_line_in_content() {
        let content = "deb https://example.com/repo stable main\ndeb http://other.com focal main\n";
        let result = find_repo_line_in_content(content, "deb https://example.com/repo stable main");
        assert!(result.is_some());
        assert_eq!(result.unwrap().0, 0);
    }

    #[test]
    fn test_add_repo_to_file_new_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        let changed = add_repo_to_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            0o644,
            false,
        )
        .unwrap();

        assert!(changed);
        let content = fs::read_to_string(&file_path).unwrap();
        assert!(content.contains("deb https://example.com/repo stable main"));
    }

    #[test]
    fn test_add_repo_to_file_existing_repo() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(&file_path, "deb https://example.com/repo stable main\n").unwrap();

        let changed = add_repo_to_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            0o644,
            false,
        )
        .unwrap();

        assert!(!changed);
    }

    #[test]
    fn test_add_repo_to_file_append() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(&file_path, "deb http://first.com focal main\n").unwrap();

        let changed = add_repo_to_file(
            &file_path,
            "deb http://second.com jammy main\n",
            0o644,
            false,
        )
        .unwrap();

        assert!(changed);
        let content = fs::read_to_string(&file_path).unwrap();
        assert!(content.contains("first.com"));
        assert!(content.contains("second.com"));
    }

    #[test]
    fn test_remove_repo_from_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(
            &file_path,
            "deb https://example.com/repo stable main\ndeb http://other.com focal main\n",
        )
        .unwrap();

        let changed = remove_repo_from_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            false,
        )
        .unwrap();

        assert!(changed);
        let content = fs::read_to_string(&file_path).unwrap();
        assert!(!content.contains("example.com"));
        assert!(content.contains("other.com"));
    }

    #[test]
    fn test_remove_repo_from_file_not_found() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(&file_path, "deb http://other.com focal main\n").unwrap();

        let changed = remove_repo_from_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            false,
        )
        .unwrap();

        assert!(!changed);
    }

    #[test]
    fn test_remove_repo_from_file_removes_empty_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(&file_path, "deb https://example.com/repo stable main\n").unwrap();

        let changed = remove_repo_from_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            false,
        )
        .unwrap();

        assert!(changed);
        assert!(!file_path.exists());
    }

    #[test]
    fn test_add_repo_check_mode() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        let changed = add_repo_to_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            0o644,
            true,
        )
        .unwrap();

        assert!(changed);
        assert!(!file_path.exists());
    }

    #[test]
    fn test_remove_repo_check_mode() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.list");

        fs::write(
            &file_path,
            "deb https://example.com/repo stable main\ndeb http://other.com focal main\n",
        )
        .unwrap();
        let original_content = fs::read_to_string(&file_path).unwrap();

        let changed = remove_repo_from_file(
            &file_path,
            "deb https://example.com/repo stable main\n",
            true,
        )
        .unwrap();

        assert!(changed);
        let content_after = fs::read_to_string(&file_path).unwrap();
        assert_eq!(original_content, content_after);
    }

    #[test]
    fn test_parse_params_all_options() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            repo: deb https://example.com/repo stable main
            state: absent
            mode: "0600"
            update_cache: false
            validate_certs: false
            filename: custom
            codename: jammy
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params.state, Some(State::Absent));
        assert_eq!(params.mode, Some("0600".to_owned()));
        assert_eq!(params.update_cache, Some(false));
        assert_eq!(params.validate_certs, Some(false));
        assert_eq!(params.filename, Some("custom".to_owned()));
        assert_eq!(params.codename, Some("jammy".to_owned()));
    }

    #[test]
    fn test_parse_params_random_field() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            repo: deb https://example.com/repo stable main
            foo: bar
            "#,
        )
        .unwrap();
        let error = parse_params::<Params>(yaml).unwrap_err();
        assert_eq!(error.kind(), ErrorKind::InvalidData);
    }
}