From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api v3 3/6] add helper for parsing SA channel.d files
Date: Tue, 19 Jan 2021 11:38:12 +0100 [thread overview]
Message-ID: <20210119103815.15934-4-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20210119103815.15934-1-s.ivanov@proxmox.com>
RHEL/CentOS based SpamAssassin implementations ship an update script,
which reads shell snippets from
/etc/mail/spamassassin/channel.d/*.conf and uses the information there
to update SA rules from the configured channels [0].
Noticed the existence of this directory/mechanism while reading the
announcement of the updatechannel for the KAM ruleset [1].
Parsing the file as text, instead of sourcing it in a shell, since I
hope that the channel files distributed don't rely on running commands
to get the ruleset url and gpg key.
The parser has some minimal tests added (inspired by the
convert_size_test.pl from pve-common)
[0] https://src.fedoraproject.org/rpms/spamassassin/blob/master/f/sa-update.cronscript
[1] https://mcgrail.com/template/kam.cf_channel
Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
v2->v3:
* refactored read_sa_channel into a sub directly in PMG::Utils to make
it testable
* added tests (hopefully I copied from an appropriate place ;)
src/PMG/Utils.pm | 32 +++++++++++
src/tests/KAM_channel.conf | 34 ++++++++++++
src/tests/Makefile | 1 +
src/tests/missing_gpg_key_channel.conf | 2 +
src/tests/missing_keyid.conf | 7 +++
src/tests/test_sa_channel_parser.pl | 75 ++++++++++++++++++++++++++
6 files changed, 151 insertions(+)
create mode 100644 src/tests/KAM_channel.conf
create mode 100644 src/tests/missing_gpg_key_channel.conf
create mode 100644 src/tests/missing_keyid.conf
create mode 100755 src/tests/test_sa_channel_parser.pl
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index d3fae9e..e3863b0 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1442,5 +1442,37 @@ sub domain_regex {
return $regex;
}
+sub read_sa_channel {
+ my ($filename) = @_;
+
+ my $content = PVE::Tools::file_get_contents($filename);
+ my $channel = {
+ filename => $filename,
+ };
+
+ ($channel->{keyid}) = ($content =~ /^KEYID=([a-fA-F0-9]+)$/m);
+ die "no KEYID in $filename!\n" if !defined($channel->{keyid});
+ ($channel->{channelurl}) = ($content =~ /^CHANNELURL=(.+)$/m);
+ die "no CHANNELURL in $filename!\n" if !defined($channel->{channelurl});
+ ($channel->{gpgkey}) = ($content =~ /(?:^|\n)(-----BEGIN PGP PUBLIC KEY BLOCK-----.+-----END PGP PUBLIC KEY BLOCK-----)(?:\n|$)/s);
+ die "no GPG public key in $filename!\n" if !defined($channel->{gpgkey});
+
+ return $channel;
+};
+
+sub local_spamassassin_channels {
+
+ my $res = [];
+
+ my $local_channel_dir = '/etc/mail/spamassassin/channel.d/';
+
+ PVE::Tools::dir_glob_foreach($local_channel_dir, '.*\.conf', sub {
+ my ($filename) = @_;
+ my $channel = read_sa_channel($local_channel_dir.$filename);
+ push(@$res, $channel);
+ });
+
+ return $res;
+}
1;
diff --git a/src/tests/KAM_channel.conf b/src/tests/KAM_channel.conf
new file mode 100644
index 0000000..50b8bc8
--- /dev/null
+++ b/src/tests/KAM_channel.conf
@@ -0,0 +1,34 @@
+CHANNELURL=kam.sa-channels.mcgrail.com
+KEYID=24C063D8
+# Ignore everything below.
+return 0
+
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+mQINBF96bE0BEADsT1xRD2l19kmUSg9XMfRUtJbMGa9YAQ0a2fayT9IdmR38J4o3
+Ln2fIR0CMa81Q+mi7pSdTpHGqR3t5GjmDGcCN8kwoHbmm0t5F9gK0tFAXThf+e40
+kMdzLNzled4+5D83VyKCNaPm1tmogzYKKIEzTHCqQ7TdahWZDRDFiZJWFkd/9miE
+kURY2uWLCttF+4Aa2AOHUg/7q00NSR8S0jWpLzpVNjbgi/jjkCafhpSZ56aqXHk3
+QrTwJj3sznrLb9TkVZoXFKbBCh15m7mf5VVJVEZpj3BsvbcZJPnBFkCrzPjfShRz
+lttRyiCFflOIcDrClg62tA/a1BmdUuIB5ktdCX8gB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GN0po2VX
+bd9H8DKz1CXBLNZRG0vn5mViSOBzZeGU+K9aAs58GZ46LKA3YfWJ4s5W8BS+J3Ia
+TFpq8U+OO/BSmOkMHZ+OPKWSlxNitFTyQsIdtS1PfqqYc+MK312LdmvrG2KWXE3N
+EnuBffLm6uSOHJA6/0r6THJkffDSuvqM5yU=
+=GVCC
+-----END PGP PUBLIC KEY BLOCK-----
diff --git a/src/tests/Makefile b/src/tests/Makefile
index 79310b3..dc35796 100644
--- a/src/tests/Makefile
+++ b/src/tests/Makefile
@@ -10,6 +10,7 @@ check:
./print_testdb.pl > testdb.txt.new
diff -u testdb.txt testdb.txt.new
./test_greylist.pl
+ ./test_sa_channel_parser.pl
# test_config.pl \
# test_mimetype.pl \
diff --git a/src/tests/missing_gpg_key_channel.conf b/src/tests/missing_gpg_key_channel.conf
new file mode 100644
index 0000000..2bf59f3
--- /dev/null
+++ b/src/tests/missing_gpg_key_channel.conf
@@ -0,0 +1,2 @@
+CHANNELURL=missing.gpg.key.invalid
+KEYID=AAAAAAAA
diff --git a/src/tests/missing_keyid.conf b/src/tests/missing_keyid.conf
new file mode 100644
index 0000000..fe4eacf
--- /dev/null
+++ b/src/tests/missing_keyid.conf
@@ -0,0 +1,7 @@
+CHANNELURL=kam.sa-channels.mcgrail.com
+# Ignore everything below.
+return 0
+
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+-----END PGP PUBLIC KEY BLOCK-----
diff --git a/src/tests/test_sa_channel_parser.pl b/src/tests/test_sa_channel_parser.pl
new file mode 100755
index 0000000..fcbb859
--- /dev/null
+++ b/src/tests/test_sa_channel_parser.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+use PMG::Utils;
+
+my $kam_key = qq{-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+mQINBF96bE0BEADsT1xRD2l19kmUSg9XMfRUtJbMGa9YAQ0a2fayT9IdmR38J4o3
+Ln2fIR0CMa81Q+mi7pSdTp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Cs2IPnyuOSvfrZc5wx1
+ImCS8BpmGjXqaXZAIWLIhpMXvRiboGxX1zzRZLoz7Y5Y5h1MfnY2ASDMddmJpgOv
+Vey/acAB4+6TtCgXmA6Wy8xmsqlId4qBocxX/jCMJ8OsuueYE6eF2jzS/JfbTndA
+7pHOnCoR+ndMra5vaX8MYyGKqxxWyBoKWGgeBs8fSMwHAqRIo9GHWK67nBX0x39U
+x9G0yn/A2dhaGqhui8xrcAHg/OGJErOlDw7YBeVX0RiS6awPyk9fo0IsGN0po2VX
+bd9H8DKz1CXBLNZRG0vn5mViSOBzZeGU+K9aAs58GZ46LKA3YfWJ4s5W8BS+J3Ia
+TFpq8U+OO/BSmOkMHZ+OPKWSlxNitFTyQsIdtS1PfqqYc+MK312LdmvrG2KWXE3N
+EnuBffLm6uSOHJA6/0r6THJkffDSuvqM5yU=
+=GVCC
+-----END PGP PUBLIC KEY BLOCK-----};
+
+my $tests = [
+ [
+ './KAM_channel.conf', # input filename
+ { # result structure
+ filename => './KAM_channel.conf',
+ channelurl => 'kam.sa-channels.mcgrail.com',
+ keyid => '24C063D8',
+ gpgkey => $kam_key,
+ },
+ undef, # error string
+ ],
+ [
+ './missing_gpg_key_channel.conf',
+ undef,
+ 'no GPG public key in ./missing_gpg_key_channel.conf!',
+ ],
+ [
+ './missing_keyid.conf',
+ undef,
+ 'no KEYID in ./missing_keyid.conf!',
+ ],
+];
+
+foreach my $test (@$tests) {
+ my ($filename, $expect, $error) = @$test;
+
+ my $result = eval { PMG::Utils::read_sa_channel($filename); };
+ my $err = $@;
+
+ if ($error) {
+ like($err, qr/^\Q$error\E/, "expected error for $filename: $error");
+ } else {
+ is_deeply($result, $expect, "channel file: $filename parsed correctly");
+ }
+}
+
+done_testing();
--
2.20.1
next prev parent reply other threads:[~2021-01-19 10:39 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-19 10:38 [pmg-devel] [PATCH pmg-api v3 0/6] add support for locally configured SA channels Stoiko Ivanov
2021-01-19 10:38 ` [pmg-devel] [PATCH pmg-api v3 1/6] buildsys: fix check target in main Makefile Stoiko Ivanov
2021-01-19 10:38 ` [pmg-devel] [PATCH pmg-api v3 2/6] buildsys: fix PERLLIB setting in tests/Makefile Stoiko Ivanov
2021-01-19 10:38 ` Stoiko Ivanov [this message]
2021-01-19 10:38 ` [pmg-devel] [PATCH pmg-api v3 4/6] api: spamassassin: read local channels Stoiko Ivanov
2021-01-19 10:38 ` [pmg-devel] [PATCH pmg-api v3 5/6] api: spamassassin: update " Stoiko Ivanov
2021-01-19 10:38 ` [pmg-devel] [PATCH pmg-api v3 6/6] pmg-daily: run sa-update for " Stoiko Ivanov
2021-01-20 10:31 ` [pmg-devel] applied-series: [PATCH pmg-api v3 0/6] add support for locally configured SA channels Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210119103815.15934-4-s.ivanov@proxmox.com \
--to=s.ivanov@proxmox.com \
--cc=pmg-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox