From 7ce0438a709c5131d95cc5c0e87f0de085652442 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 4 Aug 2021 06:55:46 -0500 Subject: [PATCH 1/2] refactor(config): Allow defaulting type patterns --- src/config.rs | 54 +++++++++++++++++++++++++++++++------------- src/policy.rs | 62 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 76 insertions(+), 40 deletions(-) diff --git a/src/config.rs b/src/config.rs index 2290093..74c6309 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ pub struct Config { pub files: Walk, pub default: EngineConfig, #[serde(rename = "type")] - pub type_: std::collections::HashMap, + pub type_: TypeEngineConfig, #[serde(skip)] pub overrides: EngineConfig, } @@ -39,7 +39,7 @@ impl Config { Self { files: Walk::from_defaults(), default: EngineConfig::from_defaults(), - type_: Default::default(), + type_: TypeEngineConfig::from_defaults(), overrides: EngineConfig::default(), } } @@ -47,12 +47,7 @@ impl Config { pub fn update(&mut self, source: &Config) { self.files.update(&source.files); self.default.update(&source.default); - for (type_name, engine) in source.type_.iter() { - self.type_ - .entry(type_name.to_owned()) - .or_insert_with(TypeEngineConfig::default) - .update(engine); - } + self.type_.update(&source.type_); self.overrides.update(&source.overrides); } } @@ -148,15 +143,44 @@ impl Walk { #[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields, default)] -#[serde(rename_all = "kebab-case")] +#[serde(transparent)] pub struct TypeEngineConfig { + pub patterns: std::collections::HashMap, +} + +impl TypeEngineConfig { + pub fn from_defaults() -> Self { + let empty = Self::default(); + Self { + patterns: empty.patterns().collect(), + } + } + + pub fn update(&mut self, source: &Self) { + for (type_name, engine) in source.patterns.iter() { + self.patterns + .entry(type_name.to_owned()) + .or_insert_with(GlobEngineConfig::default) + .update(engine); + } + } + + pub fn patterns(&self) -> impl Iterator { + self.patterns.clone().into_iter() + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(deny_unknown_fields, default)] +#[serde(rename_all = "kebab-case")] +pub struct GlobEngineConfig { pub extend_glob: Vec, #[serde(flatten)] pub engine: EngineConfig, } -impl TypeEngineConfig { - pub fn update(&mut self, source: &TypeEngineConfig) { +impl GlobEngineConfig { + pub fn update(&mut self, source: &GlobEngineConfig) { self.extend_glob.extend(source.extend_glob.iter().cloned()); self.engine.update(&source.engine); } @@ -449,8 +473,8 @@ mod test { #[test] fn test_extend_glob_updates() { - let null = TypeEngineConfig::default(); - let extended = TypeEngineConfig { + let null = GlobEngineConfig::default(); + let extended = GlobEngineConfig { extend_glob: vec!["*.foo".into()], ..Default::default() }; @@ -463,11 +487,11 @@ mod test { #[test] fn test_extend_glob_extends() { - let base = TypeEngineConfig { + let base = GlobEngineConfig { extend_glob: vec!["*.foo".into()], ..Default::default() }; - let extended = TypeEngineConfig { + let extended = GlobEngineConfig { extend_glob: vec!["*.bar".into()], ..Default::default() }; diff --git a/src/policy.rs b/src/policy.rs index ede2fa6..5c85e83 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -139,21 +139,21 @@ impl<'s> ConfigEngine<'s> { } let mut types = Default::default(); - std::mem::swap(&mut types, &mut config.type_); + std::mem::swap(&mut types, &mut config.type_.patterns); let mut types = types .into_iter() .map(|(type_, type_engine)| { let mut new_engine = config.default.clone(); new_engine.update(&type_engine.engine); new_engine.update(&config.overrides); - let new_type_engine = crate::config::TypeEngineConfig { + let new_type_engine = crate::config::GlobEngineConfig { extend_glob: type_engine.extend_glob, engine: new_engine, }; (type_, new_type_engine) }) .collect(); - std::mem::swap(&mut types, &mut config.type_); + std::mem::swap(&mut types, &mut config.type_.patterns); config.default.update(&config.overrides); @@ -179,7 +179,7 @@ impl<'s> ConfigEngine<'s> { let mut type_matcher = ignore::types::TypesBuilder::new(); type_matcher.add_defaults(); let mut types: std::collections::HashMap<_, _> = Default::default(); - for (type_name, type_engine) in type_.into_iter() { + for (type_name, type_engine) in type_.patterns() { if type_engine.extend_glob.is_empty() { if type_matcher .definitions() @@ -371,14 +371,16 @@ mod test { check_filename: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { - engine: crate::config::EngineConfig { - check_filename: Some(false), - check_file: Some(true), + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { + engine: crate::config::EngineConfig { + check_filename: Some(false), + check_file: Some(true), + ..Default::default() + }, ..Default::default() }, - ..Default::default() }, }, overrides: crate::config::EngineConfig { @@ -395,13 +397,18 @@ mod test { assert_eq!(loaded.default.binary, Some(false)); assert_eq!(loaded.default.check_filename, Some(true)); assert_eq!(loaded.default.check_file, Some(false)); - assert_eq!(loaded.type_[type_name.as_str()].engine.binary, Some(false)); assert_eq!( - loaded.type_[type_name.as_str()].engine.check_filename, + loaded.type_.patterns[type_name.as_str()].engine.binary, Some(false) ); assert_eq!( - loaded.type_[type_name.as_str()].engine.check_file, + loaded.type_.patterns[type_name.as_str()] + .engine + .check_filename, + Some(false) + ); + assert_eq!( + loaded.type_.patterns[type_name.as_str()].engine.check_file, Some(false) ); } @@ -415,9 +422,11 @@ mod test { let type_name = kstring::KString::from_static(NEVER_EXIST_TYPE); let config = crate::config::Config { - type_: maplit::hashmap! { - type_name => crate::config::TypeEngineConfig { - ..Default::default() + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name => crate::config::GlobEngineConfig { + ..Default::default() + }, }, }, ..Default::default() @@ -457,12 +466,14 @@ mod test { binary: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { - extend_glob: vec![type_name], - engine: crate::config::EngineConfig { - binary: Some(false), - ..Default::default() + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { + extend_glob: vec![type_name], + engine: crate::config::EngineConfig { + binary: Some(false), + ..Default::default() + }, }, }, }, @@ -489,14 +500,15 @@ mod test { binary: Some(true), ..Default::default() }, - type_: maplit::hashmap! { - type_name.clone() => crate::config::TypeEngineConfig { + type_: crate::config::TypeEngineConfig { + patterns: maplit::hashmap! { + type_name.clone() => crate::config::GlobEngineConfig { extend_glob: vec![type_name], engine: crate::config::EngineConfig { binary: Some(false), ..Default::default() }, - }, + }}, }, ..Default::default() }; From 27bd817498542878bc2cee74f3973943c26fe4c3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 4 Aug 2021 07:00:01 -0500 Subject: [PATCH 2/2] fix(config): Skip checking cert contents Fixes #327 --- src/config.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 74c6309..2a21607 100644 --- a/src/config.rs +++ b/src/config.rs @@ -166,7 +166,34 @@ impl TypeEngineConfig { } pub fn patterns(&self) -> impl Iterator { - self.patterns.clone().into_iter() + let mut patterns = self.patterns.clone(); + patterns.entry("cert".into()).or_insert_with(|| { + GlobEngineConfig { + extend_glob: vec![ + // Certificate files: + "*.crt".into(), + "*.cer".into(), + "*.ca-bundle".into(), + "*.p7b".into(), + "*.p7c".into(), + "*.p7s".into(), + "*.pem".into(), + // Keystore Files: + "*.key".into(), + "*.keystore".into(), + "*.jks".into(), + // Combined certificate and key files: + "*.p12".into(), + "*.pfx".into(), + "*.pem".into(), + ], + engine: EngineConfig { + check_file: Some(false), + ..Default::default() + }, + } + }); + patterns.into_iter() } }