syncable_cli/analyzer/hadolint/rules/
dl3009.rs

1//! DL3009: Delete the apt-get lists after installing something
2//!
3//! After installing packages with apt-get, the package lists should be
4//! removed to reduce image size.
5
6use crate::analyzer::hadolint::parser::instruction::Instruction;
7use crate::analyzer::hadolint::rules::{SimpleRule, simple_rule};
8use crate::analyzer::hadolint::shell::ParsedShell;
9use crate::analyzer::hadolint::types::Severity;
10
11pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
12    simple_rule(
13        "DL3009",
14        Severity::Info,
15        "Delete the apt-get lists after installing something.",
16        |instr, shell| {
17            match instr {
18                Instruction::Run(_) => {
19                    if let Some(shell) = shell {
20                        // Check if apt-get install is used
21                        let has_apt_install = shell.any_command(|cmd| {
22                            cmd.name == "apt-get" && cmd.has_any_arg(&["install"])
23                        });
24
25                        if !has_apt_install {
26                            return true;
27                        }
28
29                        // Check if lists are cleaned
30                        shell.any_command(|cmd| {
31                            // rm -rf /var/lib/apt/lists/*
32                            (cmd.name == "rm" && cmd.arguments.iter().any(|arg| {
33                                arg.contains("/var/lib/apt/lists")
34                            }))
35                            // Or apt-get clean
36                            || (cmd.name == "apt-get" && cmd.has_any_arg(&["clean", "autoclean"]))
37                        })
38                    } else {
39                        true
40                    }
41                }
42                _ => true,
43            }
44        },
45    )
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use crate::analyzer::hadolint::config::HadolintConfig;
52    use crate::analyzer::hadolint::lint::{LintResult, lint};
53
54    fn lint_dockerfile(content: &str) -> LintResult {
55        lint(content, &HadolintConfig::default())
56    }
57
58    #[test]
59    fn test_apt_get_without_cleanup() {
60        let result =
61            lint_dockerfile("FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx");
62        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
63    }
64
65    #[test]
66    fn test_apt_get_with_rm_cleanup() {
67        let result = lint_dockerfile(
68            "FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/*",
69        );
70        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
71    }
72
73    #[test]
74    fn test_apt_get_with_clean() {
75        let result = lint_dockerfile(
76            "FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx && apt-get clean",
77        );
78        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
79    }
80
81    #[test]
82    fn test_no_apt_get() {
83        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN echo hello");
84        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
85    }
86}