Skip to content

Added how to use Caddy Certificate for Postal #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions content/3.features/smtp-tls.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,107 @@ You can use the command below to generate a self-signed certificate.
openssl req -x509 -newkey rsa:4096 -keyout /opt/postal/config/smtp.key -out /opt/postal/config/smtp.cert -sha256 -days 365 -nodes
```

### Using Caddy certificate for TLS

#### Setup automatic copying from Caddy to Postal

To remove the need of the manual maintenance task to copy the certificate from Caddy to Postal, we can automate this. The original discussion and author can be found [here](https://github.com/orgs/postalserver/discussions/2673).

##### Install inotify-tools

Install the toolset which provides `inotifywait`, used to monitor certificate changes.

```bash
sudo apt-get update
sudo apt-get install inotify-tools
```

##### Create Monitoring Script

Create a script named `monitor_certs.sh`:

```bash
nano /opt/postal/monitor_certs.sh
```

Add following code to the script file:

```bash
#!/bin/bash

CERT_DIR="/opt/postal/caddy-data/caddy/certificates/acme-v02.api.letsencrypt.org-directory/YOURDOMAIN/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this only work if you have caddy installed directly on the server or are you mapping a docker volume to get this out of the container?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes this is for a direct install on the server, if it is in a docker volume, you'd probably have to adjust the paths

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having just re-familiarised myself with the docs https://docs.postalserver.io/getting-started/installation#caddy

looks like you have used the docker paths right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As yes, of course, wherever possible i used the docker installation path and information

CERT_FILE="${CERT_DIR}YOURDOMAIN.crt"
KEY_FILE="${CERT_DIR}YOURDOMAIN.key"

while true; do
inotifywait -e modify "$CERT_FILE" "$KEY_FILE"

# Copy the certificates to Postal's configuration directory
cp "$CERT_FILE" /opt/postal/config/smtp.cert
cp "$KEY_FILE" /opt/postal/config/smtp.key

# Adjust permissions to ensure Postal can read the certificates
chmod o+r /opt/postal/config/smtp.*

# Restart Postal to use the new certificates
postal restart
done
```

Make the script executable:
```bash
chmod +x /opt/postal/monitor_certs.sh
```

##### Create a systemd Service

Make a systemd service file:

```bash
sudo nano /etc/systemd/system/monitor_certs.service
```

Insert the following content:

```yaml
[Unit]
Description=Monitor Caddy Certificates for Postal

[Service]
ExecStart=/opt/postal/monitor_certs.sh
Restart=always
User=your_username
Group=your_groupname

[Install]
WantedBy=multi-user.target
```

##### Activate the Service

Reload the systemd daemons:

```bash
sudo systemctl daemon-reload
```

Enable and start the service:

```bash
sudo systemctl enable monitor_certs.service
sudo systemctl start monitor_certs.service
```

##### Initial Manual Certificate Copy

Before the monitoring script takes over, you should manually copy the certificates for the first time:

```bash
cp /opt/postal/caddy-data/caddy/certificates/acme-v02.api.letsencrypt.org-directory/YOURDOMAIN/YOURDOMAIN.crt /opt/postal/config/smtp.cert
cp /opt/postal/caddy-data/caddy/certificates/acme-v02.api.letsencrypt.org-directory/YOURDOMAIN/YOURDOMAIN.key /opt/postal/config/smtp.key
chmod o+r /opt/postal/config/smtp.*
```

## Configuration

Once you have a key and certificate you will need to enable TLS in the configuration file (`/opt/postal/config/postal.yml`). Additional options are available too.
Expand Down