From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
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))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 9395E9FB23
 for <pve-devel@lists.proxmox.com>; Tue,  7 Nov 2023 11:20:02 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 13D74304AD
 for <pve-devel@lists.proxmox.com>; Tue,  7 Nov 2023 11:19:10 +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 <pve-devel@lists.proxmox.com>; Tue,  7 Nov 2023 11:19:06 +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 071F646924
 for <pve-devel@lists.proxmox.com>; Tue,  7 Nov 2023 11:19:06 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue,  7 Nov 2023 11:18:14 +0100
Message-Id: <20231107101827.340100-15-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 pve-manager 14/27] vzdump: adapt to new matcher
 based notification system
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 07 Nov 2023 10:20:02 -0000

To ease the migration from old-style mailto/mailnotification paramters
for backup jobs, the code will add a ephemeral sendmail endpoint and
a matcher.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 PVE/API2/VZDump.pm |  8 +-------
 PVE/VZDump.pm      | 40 +++++++++++++++++++---------------------
 2 files changed, 20 insertions(+), 28 deletions(-)

diff --git a/PVE/API2/VZDump.pm b/PVE/API2/VZDump.pm
index 3886772e..f66fc740 100644
--- a/PVE/API2/VZDump.pm
+++ b/PVE/API2/VZDump.pm
@@ -44,9 +44,7 @@ __PACKAGE__->register_method ({
 	    ."'Datastore.AllocateSpace' on the backup storage. The 'tmpdir', 'dumpdir' and "
 	    ."'script' parameters are restricted to the 'root\@pam' user. The 'maxfiles' and "
 	    ."'prune-backups' settings require 'Datastore.Allocate' on the backup storage. The "
-	    ."'bwlimit', 'performance' and 'ionice' parameters require 'Sys.Modify' on '/'. "
-	    ."If 'notification-target' is set, then the 'Mapping.Use' permission is needed on "
-	    ."'/mapping/notification/<target>'.",
+	    ."'bwlimit', 'performance' and 'ionice' parameters require 'Sys.Modify' on '/'. ",
 	user => 'all',
     },
     protected => 1,
@@ -115,10 +113,6 @@ __PACKAGE__->register_method ({
 	    $rpcenv->check($user, "/storage/$storeid", [ 'Datastore.AllocateSpace' ]);
 	}
 
-	if (my $target = $param->{'notification-target'}) {
-	    PVE::Notify::check_may_use_target($target, $rpcenv);
-	}
-
 	my $worker = sub {
 	    my $upid = shift;
 
diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 454ab494..b0574d41 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -452,20 +452,18 @@ sub send_notification {
     my $opts = $self->{opts};
     my $mailto = $opts->{mailto};
     my $cmdline = $self->{cmdline};
-    my $target = $opts->{"notification-target"};
-    # Fall back to 'mailnotification' if 'notification-policy' is not set.
-    # If both are set, 'notification-policy' takes precedence
-    my $policy = $opts->{"notification-policy"} // $opts->{mailnotification} // 'always';
+    # Old-style notification policy. This parameter will influce
+    # if an ad-hoc notification target/matcher will be created.
+    my $policy = $opts->{"notification-policy"} //
+	$opts->{mailnotification} //
+	'always';
 
-    return if ($policy eq 'never');
 
     sanitize_task_list($tasklist);
     my $error_count = count_failed_tasks($tasklist);
 
     my $failed = ($error_count || $err);
 
-    return if (!$failed && ($policy eq 'failure'));
-
     my $status_text = $failed ? 'backup failed' : 'backup successful';
 
     if ($err) {
@@ -489,8 +487,10 @@ sub send_notification {
 	    "See Task History for details!\n";
     };
 
+    my $hostname = get_hostname();
+
     my $notification_props = {
-	"hostname"      => get_hostname(),
+	"hostname"      => $hostname,
 	"error-message" => $err,
 	"guest-table"   => build_guest_table($tasklist),
 	"logs"          => $text_log_part,
@@ -498,9 +498,16 @@ sub send_notification {
 	"total-time"    => $total_time,
     };
 
+    my $fields = {
+	type => "vzdump",
+	hostname => $hostname,
+    };
+
     my $notification_config = PVE::Notify::read_config();
 
-    if ($mailto && scalar(@$mailto)) {
+    my $legacy_sendmail = $policy eq "always" || ($policy eq "failure" && $failed);
+
+    if ($mailto && scalar(@$mailto) && $legacy_sendmail) {
 	# <, >, @ are not allowed in endpoint names, but that is only
 	# verified once the config is serialized. That means that
 	# we can rely on that fact that no other endpoint with this name exists.
@@ -514,29 +521,20 @@ sub send_notification {
 
 	my $endpoints = [$endpoint_name];
 
-	# Create an anonymous group containing the sendmail endpoint and the
-	# $target endpoint, if specified
-	if ($target) {
-	    push @$endpoints, $target;
-	}
-
-	$target = "<group-$endpoint_name>";
-	$notification_config->add_group(
-	    $target,
+	$notification_config->add_matcher(
+	    "<matcher-$endpoint_name>",
 	    $endpoints,
 	);
     }
 
-    return if (!$target);
-
     my $severity = $failed ? "error" : "info";
 
     PVE::Notify::notify(
-	$target,
 	$severity,
 	$subject_template,
 	$body_template,
 	$notification_props,
+	$fields,
 	$notification_config
     );
 };
-- 
2.39.2