Merge pull request 'Add SpamAssassin filtering for Postfix mail nodes' (#74) from feature/mail-spam-filtering into main
Reviewed-on: #74
This commit was merged in pull request #74.
This commit is contained in:
commit
bb617a99ee
7 files changed
+130
-5
No files matched your search
@@ -0,0 +1,3 @@
|
||||
[Service]
|
||||
PIDFile=/run/spamass/spamass.pid
|
||||
RemainAfterExit=no
|
||||
@@ -0,0 +1,2 @@
|
||||
# Merge into /etc/postfix/main.cf. Keep any other smtpd_milters on this list.
|
||||
smtpd_milters = { unix:spamass/spamass.sock, connect_timeout=3s, content_timeout=30s, default_action=accept }
|
||||
@@ -0,0 +1,6 @@
|
||||
# /etc/default/spamass-milter
|
||||
# Tag mail without rewriting the subject or body. Skip authenticated SMTP.
|
||||
OPTIONS="-m -I -- -d 127.0.0.1 -n 2 -t 15 --connect-retries=1"
|
||||
SOCKET="/var/spool/postfix/spamass/spamass.sock"
|
||||
SOCKETOWNER="spamass-milter:postfix"
|
||||
SOCKETMODE="0660"
|
||||
@@ -0,0 +1,2 @@
|
||||
# /etc/default/spamd
|
||||
OPTIONS="--listen=127.0.0.1 --allowed-ips=127.0.0.1 --username=debian-spamd --nouser-config --max-children=5 --timeout-child=15 --helper-home-dir=/var/lib/spamassassin"
|
||||
+79
-3
@@ -993,6 +993,78 @@ Back up mail on the node that hosts the mailbox. The archive can be used to
|
||||
recover messages and folders into a newly provisioned mailbox with the same
|
||||
address. [Mailbox backup and restore](development.md#back-up-and-restore-a-mailbox)
|
||||
|
||||
## Filter incoming mail with SpamAssassin
|
||||
|
||||
Install the distribution's SpamAssassin and `spamass-milter` packages on
|
||||
the host where Postfix receives mail. Use the same setup for a co-located
|
||||
mail service and a separate mail node. A node shared by multiple Coulobre
|
||||
installations needs one filter configuration.
|
||||
|
||||
```sh
|
||||
sudo apt install spamassassin spamd spamc spamass-milter
|
||||
sudo adduser spamass-milter postfix
|
||||
sudoedit /etc/default/spamd /etc/default/spamass-milter /etc/postfix/main.cf
|
||||
```
|
||||
|
||||
Use the settings from `deploy/spamassassin/spamd.default.example` and
|
||||
`deploy/spamassassin/spamass-milter.default.example` in the corresponding
|
||||
defaults files. The scanner listens on loopback and uses site-wide rules.
|
||||
Postfix reaches the milter through a group-restricted socket inside its
|
||||
queue directory.
|
||||
|
||||
Merge the `smtpd_milters` entry in
|
||||
`deploy/spamassassin/postfix-main.cf.example` into the existing list. If a
|
||||
receiving service overrides `smtpd_milters` in `master.cf`, add the entry
|
||||
there too. The example skips authenticated SMTP and covers messages
|
||||
received by an SMTP service configured with this milter. Deliveries from
|
||||
the local `sendmail` command use Postfix's separate non-SMTP path.
|
||||
[Postfix Milter setup](https://www.postfix.org/MILTER_README.html)
|
||||
|
||||
If Debian 13's `spamass-milter.service` is generated from
|
||||
`/etc/init.d/spamass-milter`, install this drop-in so systemd tracks the
|
||||
milter process.
|
||||
|
||||
```sh
|
||||
sudo install -D -m 0644 deploy/spamassassin/debian-milter.service.conf \
|
||||
/etc/systemd/system/spamass-milter.service.d/coulobre.conf
|
||||
sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
```sh
|
||||
sudo spamassassin --lint
|
||||
sudo systemctl enable spamd.service spamass-milter.service
|
||||
sudo systemctl restart spamd.service spamass-milter.service
|
||||
sudo postfix check
|
||||
sudo postfix reload
|
||||
```
|
||||
|
||||
The example delivers scanned messages with an `X-Spam-Status` header and
|
||||
preserves the subject and body. Set the tagging threshold with
|
||||
`required_score` in `/etc/spamassassin/local.cf`. Restart `spamd.service`
|
||||
after editing its rules. The example lets Postfix accept mail during a
|
||||
milter outage and lets the milter pass mail through during a scanner
|
||||
outage. Messages above the `spamc` size limit also pass through unscanned.
|
||||
The per-milter fallback preserves the failure policy of any other filter.
|
||||
[SpamAssassin settings](https://spamassassin.apache.org/full/4.0.x/doc/Mail_SpamAssassin_Conf.html)
|
||||
|
||||
On a test mail node, check delivery through SMTP to a Coulobre mailbox and
|
||||
inspect the full headers over IMAP.
|
||||
|
||||
1. Send a normal message and look for `X-Spam-Status: No`.
|
||||
2. Send the [GTUBE test message](https://spamassassin.apache.org/gtube/)
|
||||
and look for `X-Spam-Status: Yes` with `GTUBE` in the matched tests.
|
||||
3. Stop `spamd.service` and send another test message. Confirm it arrives
|
||||
without a scanner-added status. Start the scanner and repeat with
|
||||
`spamass-milter.service` stopped. Start both services after testing.
|
||||
|
||||
Use `spamc -d 127.0.0.1 -K` to check the scanner from the mail node. Read
|
||||
scan results with `journalctl -u spamd.service -u spamass-milter.service`
|
||||
and Postfix's filter connection errors with `journalctl -t postfix/smtpd`
|
||||
or in the mail log.
|
||||
Add `spamd` and `spamass-milter` to the mail node's
|
||||
[service health checks](#monitor-local-and-remote-services) to monitor the
|
||||
two units.
|
||||
|
||||
## Provision mail and DNS on separate servers
|
||||
|
||||
Choose one destination per service. For example, run mail locally and publish
|
||||
@@ -1066,6 +1138,8 @@ Set up [BIND9](development.md#apply-a-zone-manifest) or
|
||||
the corresponding [DNS socket](development.md#enable-local-dns-publishing) or
|
||||
[mailbox socket](development.md#enable-local-mailbox-provisioning). The
|
||||
destination runs the local socket service shipped with Coulobre.
|
||||
[Enable SpamAssassin on the mail node](#filter-incoming-mail-with-spamassassin)
|
||||
for incoming spam filtering.
|
||||
|
||||
Save the panel's public key from the previous step as `panel-node.pub` on
|
||||
the destination. Create a receiver with only the socket group for that
|
||||
@@ -1304,9 +1378,11 @@ COULOBRE_SERVICE_HEALTH='{"panel-01":{"local":["panel","worker","scheduler","apa
|
||||
Choose an ID for each node using lower-case letters, numbers, and hyphens,
|
||||
starting with a letter. Keep the IDs stable for the same hosts. List one
|
||||
`local` node per panel installation and use `ssh` for each additional host.
|
||||
Use `bind9`, `postfix`, and `dovecot` under `local` for co-located services.
|
||||
Remote checks accept those same three service names. The SSH client matches
|
||||
each remote node ID to a `Host` entry.
|
||||
Use `bind9`, `postfix`, `dovecot`, `spamd`, and `spamass-milter` under
|
||||
`local` for co-located services or under `ssh` for separate nodes. Only
|
||||
list the spam filter units where filtering is configured. Update the
|
||||
Coulobre package on a remote node before adding its filter units. The
|
||||
SSH client matches each remote node ID to a `Host` entry.
|
||||
|
||||
Install the collector with a dedicated service identity after updating the
|
||||
panel package.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import subprocess
|
||||
from datetime import timedelta
|
||||
from io import StringIO
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
@@ -16,6 +17,7 @@ from coulobre.service_health import (
|
||||
check_remote,
|
||||
check_unit,
|
||||
load_health_targets,
|
||||
main,
|
||||
)
|
||||
|
||||
from .models import Customer, Reseller, ServiceHealth
|
||||
@@ -99,6 +101,37 @@ class ServiceHealthProbeTests(SimpleTestCase):
|
||||
self.assertIn("StrictHostKeyChecking=yes", command)
|
||||
self.assertEqual(command[-2:], ("--", "mail-02"))
|
||||
|
||||
def test_spam_filter_units_can_be_checked_locally_and_remotely(self):
|
||||
filters = ("spamd", "spamass-milter")
|
||||
self.assertEqual(
|
||||
load_health_targets(
|
||||
'{"panel":{"local":["spamd","spamass-milter"]},'
|
||||
'"mail":{"ssh":["spamd","spamass-milter"]}}'
|
||||
),
|
||||
(
|
||||
HealthTarget("panel", "local", filters),
|
||||
HealthTarget("mail", "ssh", filters),
|
||||
),
|
||||
)
|
||||
output = StringIO()
|
||||
with (
|
||||
patch("sys.stdout", output),
|
||||
patch(
|
||||
"coulobre.service_health.subprocess.run",
|
||||
return_value=subprocess.CompletedProcess(
|
||||
(), 0, b"LoadState=loaded\nActiveState=active\n"
|
||||
),
|
||||
) as run,
|
||||
):
|
||||
self.assertEqual(main(), 0)
|
||||
units = {call.args[0][-1] for call in run.call_args_list}
|
||||
self.assertTrue({"spamd.service", "spamass-milter.service"} <= units)
|
||||
report = json.loads(output.getvalue())
|
||||
self.assertEqual(
|
||||
{name: report["services"][name] for name in filters},
|
||||
dict.fromkeys(filters, "active"),
|
||||
)
|
||||
|
||||
|
||||
@override_settings(SERVICE_HEALTH_TARGETS=TARGETS)
|
||||
class ServiceHealthPanelTests(TestCase):
|
||||
|
||||
@@ -15,8 +15,10 @@ SERVICES = {
|
||||
"bind9": ("BIND9", "named.service"),
|
||||
"postfix": ("Postfix", "postfix.service"),
|
||||
"dovecot": ("Dovecot", "dovecot.service"),
|
||||
"spamd": ("SpamAssassin", "spamd.service"),
|
||||
"spamass-milter": ("SpamAssassin milter", "spamass-milter.service"),
|
||||
}
|
||||
REMOTE_SERVICES = ("bind9", "postfix", "dovecot")
|
||||
REMOTE_SERVICES = ("bind9", "postfix", "dovecot", "spamd", "spamass-milter")
|
||||
REMOTE_VERSION = 1
|
||||
SSH_CONFIG = "/etc/coulobre-health/ssh_config"
|
||||
NODE_NAME = re.compile(r"[a-z][a-z0-9-]{0,62}")
|
||||
@@ -172,7 +174,8 @@ def check_remote(target):
|
||||
stdin=subprocess.DEVNULL,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
timeout=15,
|
||||
# Allow for SSH setup and sequential unit checks.
|
||||
timeout=22,
|
||||
check=True,
|
||||
)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
|
||||
Reference in new issue
Block a user