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                        let has_cleanup = 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
39                        has_cleanup
40                    } else {
41                        true
42                    }
43                }
44                _ => true,
45            }
46        },
47    )
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use crate::analyzer::hadolint::config::HadolintConfig;
54    use crate::analyzer::hadolint::lint::{LintResult, lint};
55
56    fn lint_dockerfile(content: &str) -> LintResult {
57        lint(content, &HadolintConfig::default())
58    }
59
60    #[test]
61    fn test_apt_get_without_cleanup() {
62        let result =
63            lint_dockerfile("FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx");
64        assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
65    }
66
67    #[test]
68    fn test_apt_get_with_rm_cleanup() {
69        let result = lint_dockerfile(
70            "FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx && rm -rf /var/lib/apt/lists/*",
71        );
72        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
73    }
74
75    #[test]
76    fn test_apt_get_with_clean() {
77        let result = lint_dockerfile(
78            "FROM ubuntu:20.04\nRUN apt-get update && apt-get install -y nginx && apt-get clean",
79        );
80        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
81    }
82
83    #[test]
84    fn test_no_apt_get() {
85        let result = lint_dockerfile("FROM ubuntu:20.04\nRUN echo hello");
86        assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3009"));
87    }
88}