syncable_cli/analyzer/hadolint/rules/
dl3004.rs1use 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 "DL3004",
14 Severity::Error,
15 "Do not use sudo as it leads to unpredictable behavior. Use a tool like gosu to enforce root",
16 |instr, shell| match instr {
17 Instruction::Run(_) => {
18 if let Some(shell) = shell {
19 !shell.any_command(|cmd| cmd.name == "sudo")
20 } else {
21 true
22 }
23 }
24 _ => true,
25 },
26 )
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use crate::analyzer::hadolint::parser::instruction::RunArgs;
33 use crate::analyzer::hadolint::rules::{Rule, RuleState};
34
35 #[test]
36 fn test_no_sudo() {
37 let rule = rule();
38 let mut state = RuleState::new();
39
40 let instr = Instruction::Run(RunArgs::shell("apt-get update"));
41 let shell = ParsedShell::parse("apt-get update");
42 rule.check(&mut state, 1, &instr, Some(&shell));
43 assert!(state.failures.is_empty());
44 }
45
46 #[test]
47 fn test_with_sudo() {
48 let rule = rule();
49 let mut state = RuleState::new();
50
51 let instr = Instruction::Run(RunArgs::shell("sudo apt-get update"));
52 let shell = ParsedShell::parse("sudo apt-get update");
53 rule.check(&mut state, 1, &instr, Some(&shell));
54 assert_eq!(state.failures.len(), 1);
55 assert_eq!(state.failures[0].code.as_str(), "DL3004");
56 }
57}