syncable_cli/analyzer/hadolint/rules/
dl3037.rs1use crate::analyzer::hadolint::parser::instruction::Instruction;
6use crate::analyzer::hadolint::rules::{simple_rule, SimpleRule};
7use crate::analyzer::hadolint::shell::ParsedShell;
8use crate::analyzer::hadolint::types::Severity;
9
10pub fn rule() -> SimpleRule<impl Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync> {
11 simple_rule(
12 "DL3037",
13 Severity::Warning,
14 "Specify version with `zypper install <package>=<version>`.",
15 |instr, shell| {
16 match instr {
17 Instruction::Run(_) => {
18 if let Some(shell) = shell {
19 !shell.any_command(|cmd| {
20 if cmd.name == "zypper" && cmd.has_any_arg(&["install", "in"]) {
21 let packages = get_zypper_packages(cmd);
22 packages.iter().any(|pkg| !is_pinned_zypper_package(pkg))
23 } else {
24 false
25 }
26 })
27 } else {
28 true
29 }
30 }
31 _ => true,
32 }
33 },
34 )
35}
36
37fn get_zypper_packages(cmd: &crate::analyzer::hadolint::shell::Command) -> Vec<&str> {
38 let mut packages = Vec::new();
39 let mut found_install = false;
40
41 for arg in &cmd.arguments {
42 if arg == "install" || arg == "in" {
43 found_install = true;
44 continue;
45 }
46 if found_install && !arg.starts_with('-') {
47 packages.push(arg.as_str());
48 }
49 }
50
51 packages
52}
53
54fn is_pinned_zypper_package(pkg: &str) -> bool {
55 if pkg.starts_with('-') {
56 return true;
57 }
58 if pkg.ends_with(".rpm") {
59 return true;
60 }
61 pkg.contains('=') || pkg.contains(">=") || pkg.contains("<=")
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use crate::analyzer::hadolint::lint::{lint, LintResult};
69 use crate::analyzer::hadolint::config::HadolintConfig;
70
71 fn lint_dockerfile(content: &str) -> LintResult {
72 lint(content, &HadolintConfig::default())
73 }
74
75 #[test]
76 fn test_zypper_unpinned() {
77 let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx");
78 assert!(result.failures.iter().any(|f| f.code.as_str() == "DL3037"));
79 }
80
81 #[test]
82 fn test_zypper_pinned() {
83 let result = lint_dockerfile("FROM opensuse:latest\nRUN zypper -n install nginx=1.20.0");
84 assert!(!result.failures.iter().any(|f| f.code.as_str() == "DL3037"));
85 }
86}