From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id A8A4D9F9A5 for ; Tue, 7 Nov 2023 11:19:03 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 914AD301EC for ; Tue, 7 Nov 2023 11:19:03 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Tue, 7 Nov 2023 11:19:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9351746904 for ; Tue, 7 Nov 2023 11:19:02 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Date: Tue, 7 Nov 2023 11:18:06 +0100 Message-Id: <20231107101827.340100-7-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231107101827.340100-1-l.wagner@proxmox.com> References: <20231107101827.340100-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.017 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH proxmox 06/27] notify: let a matcher always match if it has no matching directives X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Nov 2023 10:19:03 -0000 This should be a bit more intuitive to users than the current behavior, which is 'always match' for mode==all and 'never match' for mode==any. The current behavior originates in the neutral element of the underlying logical operation (and, or). Signed-off-by: Lukas Wagner --- proxmox-notify/src/matcher.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/proxmox-notify/src/matcher.rs b/proxmox-notify/src/matcher.rs index e299fd0..553ca87 100644 --- a/proxmox-notify/src/matcher.rs +++ b/proxmox-notify/src/matcher.rs @@ -265,17 +265,22 @@ impl MatcherConfig { let mode = self.mode.unwrap_or_default(); let mut is_match = mode.neutral_element(); + // If there are no matching directives, the matcher will always match + let mut no_matchers = true; if let Some(severity_matchers) = self.match_severity.as_deref() { + no_matchers = false; is_match = mode.apply( is_match, self.check_matches(notification, severity_matchers)?, ); } if let Some(field_matchers) = self.match_field.as_deref() { + no_matchers = false; is_match = mode.apply(is_match, self.check_matches(notification, field_matchers)?); } if let Some(calendar_matchers) = self.match_calendar.as_deref() { + no_matchers = false; is_match = mode.apply( is_match, self.check_matches(notification, calendar_matchers)?, @@ -284,7 +289,7 @@ impl MatcherConfig { let invert_match = self.invert_match.unwrap_or_default(); - Ok(if is_match != invert_match { + Ok(if is_match != invert_match || no_matchers { Some(self.target.as_deref().unwrap_or_default()) } else { None @@ -455,4 +460,25 @@ mod tests { let matcher: SeverityMatcher = "info,notice,warning,error".parse().unwrap(); assert!(matcher.matches(¬ification).unwrap()); } + + #[test] + fn test_empty_matcher_matches_always() { + let notification = Notification::new_templated( + Severity::Notice, + "test", + "test", + Value::Null, + Default::default(), + ); + + for mode in [MatchModeOperator::All, MatchModeOperator::Any] { + let config = MatcherConfig { + name: "matcher".to_string(), + mode: Some(mode), + ..Default::default() + }; + + assert!(config.matches(¬ification).unwrap().is_some()) + } + } } -- 2.39.2