mirror of
https://github.com/chatmail/relay.git
synced 2026-05-13 17:34:38 +00:00
Compare commits
21 Commits
j4n/docker
...
ipv4-only
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1651917a57 | ||
|
|
2ba19fd888 | ||
|
|
26c5e1e847 | ||
|
|
8ae2c475fb | ||
|
|
85223245e4 | ||
|
|
f152b0fca9 | ||
|
|
808eb3e53e | ||
|
|
bbacd74c9f | ||
|
|
ff541b81ea | ||
|
|
ed9b4092a8 | ||
|
|
1b8ad3ca12 | ||
|
|
f85d304e65 | ||
|
|
4d1856d8f1 | ||
|
|
ae2ab52aa9 | ||
|
|
d0c396538b | ||
|
|
78a4e28408 | ||
|
|
2432d4f498 | ||
|
|
31301abb42 | ||
|
|
6b4edd8502 | ||
|
|
9c467ab3e8 | ||
|
|
774350778b |
@@ -1,18 +0,0 @@
|
|||||||
data/
|
|
||||||
venv/
|
|
||||||
__pycache__
|
|
||||||
*.pyc
|
|
||||||
*.orig
|
|
||||||
*.ini
|
|
||||||
.pytest_cache
|
|
||||||
.env
|
|
||||||
|
|
||||||
# Slim build context — .git/ alone can be 100s of MB
|
|
||||||
.git
|
|
||||||
.github/
|
|
||||||
docs/
|
|
||||||
tests/
|
|
||||||
|
|
||||||
# Exclude markdown files but keep www/src/*.md (used by WebsiteDeployer)
|
|
||||||
*.md
|
|
||||||
!www/**/*.md
|
|
||||||
375
.github/workflows/deploy.yaml
vendored
375
.github/workflows/deploy.yaml
vendored
@@ -1,375 +0,0 @@
|
|||||||
name: Deploy
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
- j4n/docker-pr
|
|
||||||
pull_request:
|
|
||||||
paths-ignore:
|
|
||||||
- 'scripts/**'
|
|
||||||
- '**/README.md'
|
|
||||||
- 'CHANGELOG.md'
|
|
||||||
- 'LICENSE'
|
|
||||||
|
|
||||||
env:
|
|
||||||
REGISTRY: ghcr.io
|
|
||||||
IMAGE_NAME: ${{ github.repository }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-docker:
|
|
||||||
name: Build Docker image
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
packages: write
|
|
||||||
outputs:
|
|
||||||
image: ${{ steps.image-ref.outputs.image }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Login to GHCR
|
|
||||||
if: github.event_name == 'push'
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ${{ env.REGISTRY }}
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata (tags, labels)
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
# Tagged releases: v1.2.3 -> :1.2.3, :1.2, :latest
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
# Branch pushes: foo/docker-pr -> :foo-docker-pr
|
|
||||||
type=ref,event=branch
|
|
||||||
# Always: :sha-<hash>
|
|
||||||
type=sha
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: docker/chatmail_relay.dockerfile
|
|
||||||
push: ${{ github.event_name == 'push' }}
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
build-args: |
|
|
||||||
GIT_HASH=${{ github.sha }}
|
|
||||||
|
|
||||||
- name: Output image reference
|
|
||||||
id: image-ref
|
|
||||||
run: |
|
|
||||||
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
|
||||||
IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
|
||||||
echo "image=${IMAGE}" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
name: Deploy to ${{ matrix.host }}
|
|
||||||
needs: build-docker
|
|
||||||
# dont do the regular tests on this branch
|
|
||||||
if: >-
|
|
||||||
!cancelled() && (
|
|
||||||
github.event_name == 'push' ||
|
|
||||||
(github.event_name == 'pull_request' && !startsWith(github.head_ref, 'j4n/'))
|
|
||||||
)
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 60
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- host: staging2.testrun.org
|
|
||||||
acme_dir: acme
|
|
||||||
dkim_dir: dkimkeys
|
|
||||||
zone_file: staging.testrun.org-default.zone
|
|
||||||
disable_ipv6: false
|
|
||||||
add_ssh_keys: true
|
|
||||||
- host: staging-ipv4.testrun.org
|
|
||||||
acme_dir: acme-ipv4
|
|
||||||
dkim_dir: dkimkeys-ipv4
|
|
||||||
zone_file: staging-ipv4.testrun.org-default.zone
|
|
||||||
disable_ipv6: true
|
|
||||||
add_ssh_keys: false
|
|
||||||
environment:
|
|
||||||
name: ${{ matrix.host }}
|
|
||||||
url: https://${{ matrix.host }}/
|
|
||||||
concurrency: ${{ matrix.host }}
|
|
||||||
steps:
|
|
||||||
# --- Common setup ---
|
|
||||||
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: prepare SSH and save ACME/DKIM
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ACME_DIR: ${{ matrix.acme_dir }}
|
|
||||||
DKIM_DIR: ${{ matrix.dkim_dir }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
mkdir ~/.ssh
|
|
||||||
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
|
||||||
chmod 600 ~/.ssh/id_ed25519
|
|
||||||
ssh-keyscan ${HOST} > ~/.ssh/known_hosts
|
|
||||||
# save previous acme & dkim state (trailing slash = copy contents)
|
|
||||||
rsync -avz root@${HOST}:/var/lib/acme/ ${ACME_DIR}/ || true
|
|
||||||
rsync -avz root@${HOST}:/etc/dkimkeys/ ${DKIM_DIR}/ || true
|
|
||||||
# backup to ns.testrun.org if contents are useful
|
|
||||||
if [ -f ${DKIM_DIR}/opendkim.private ]; then
|
|
||||||
rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" ${DKIM_DIR}/ root@ns.testrun.org:/tmp/${DKIM_DIR}/ || true
|
|
||||||
fi
|
|
||||||
if [ "$(ls -A ${ACME_DIR}/certs 2>/dev/null)" ]; then
|
|
||||||
rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" ${ACME_DIR}/ root@ns.testrun.org:/tmp/${ACME_DIR}/ || true
|
|
||||||
fi
|
|
||||||
# make sure CAA record isn't set
|
|
||||||
scp -o StrictHostKeyChecking=accept-new .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: rebuild VPS
|
|
||||||
env:
|
|
||||||
SERVER_ID: ${{ matrix.host == 'staging2.testrun.org' && secrets.STAGING_SERVER_ID || secrets.STAGING_IPV4_SERVER_ID }}
|
|
||||||
run: |
|
|
||||||
curl -X POST \
|
|
||||||
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-d '{"image":"debian-12"}' \
|
|
||||||
"https://api.hetzner.cloud/v1/servers/${SERVER_ID}/actions/rebuild"
|
|
||||||
|
|
||||||
- run: scripts/initenv.sh
|
|
||||||
- name: append venv/bin to PATH
|
|
||||||
run: echo venv/bin >>$GITHUB_PATH
|
|
||||||
|
|
||||||
- name: wait for VPS rebuild
|
|
||||||
id: wait-for-vps
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
rm ~/.ssh/known_hosts
|
|
||||||
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new root@${HOST} id -u ; do sleep 1 ; done
|
|
||||||
|
|
||||||
- name: restore ACME/DKIM
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ACME_DIR: ${{ matrix.acme_dir }}
|
|
||||||
DKIM_DIR: ${{ matrix.dkim_dir }}
|
|
||||||
run: |
|
|
||||||
# download from ns.testrun.org
|
|
||||||
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/${ACME_DIR}/ acme-restore/ || true
|
|
||||||
rsync -avz root@ns.testrun.org:/tmp/${DKIM_DIR}/ dkimkeys-restore/ || true
|
|
||||||
# restore to VPS
|
|
||||||
rsync -avz acme-restore/ root@${HOST}:/var/lib/acme/ || true
|
|
||||||
rsync -avz dkimkeys-restore/ root@${HOST}:/etc/dkimkeys/ || true
|
|
||||||
ssh root@${HOST} chown root:root -R /var/lib/acme || true
|
|
||||||
|
|
||||||
- name: bare offline tests
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
run: pytest --pyargs cmdeploy
|
|
||||||
|
|
||||||
- name: bare deploy
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
DISABLE_IPV6: ${{ matrix.disable_ipv6 }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'apt update && apt install -y git python3.11-venv python3-dev gcc'
|
|
||||||
ssh root@${HOST} 'git clone https://github.com/chatmail/relay'
|
|
||||||
ssh root@${HOST} "cd relay && git checkout ${{ github.head_ref || github.ref_name }}"
|
|
||||||
ssh root@${HOST} 'cd relay && scripts/initenv.sh'
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy init ${HOST}"
|
|
||||||
if [ "${DISABLE_IPV6}" = "true" ]; then
|
|
||||||
ssh root@${HOST} "sed -i 's#disable_ipv6 = False#disable_ipv6 = True#' relay/chatmail.ini"
|
|
||||||
fi
|
|
||||||
ssh root@${HOST} "sed -i 's/#\s*mtail_address/mtail_address/' relay/chatmail.ini"
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy run --verbose --skip-dns-check --ssh-host localhost"
|
|
||||||
|
|
||||||
- name: bare DNS
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} chown opendkim:opendkim -R /etc/dkimkeys
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy dns --zonefile staging-generated.zone --ssh-host localhost"
|
|
||||||
ssh root@${HOST} cat relay/staging-generated.zone >> .github/workflows/${ZONE}
|
|
||||||
cat .github/workflows/${ZONE}
|
|
||||||
scp .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: bare integration tests
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} "cd relay && CHATMAIL_DOMAIN2=ci-chatmail.testrun.org scripts/cmdeploy test --slow --ssh-host localhost"
|
|
||||||
|
|
||||||
- name: bare final DNS check
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} "cd relay && scripts/cmdeploy dns -v --ssh-host localhost"
|
|
||||||
|
|
||||||
# --- Docker deploy (push only, runs even if bare failed) ---
|
|
||||||
|
|
||||||
- name: stop bare services
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'systemctl stop postfix dovecot nginx opendkim unbound filtermail doveauth chatmail-metadata iroh-relay mtail fcgiwrap acmetool 2>/dev/null || true'
|
|
||||||
|
|
||||||
- name: install Docker on VPS
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'apt-get update && apt-get install -y ca-certificates curl'
|
|
||||||
ssh root@${HOST} 'install -m 0755 -d /etc/apt/keyrings'
|
|
||||||
ssh root@${HOST} 'curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc'
|
|
||||||
ssh root@${HOST} 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" > /etc/apt/sources.list.d/docker.list'
|
|
||||||
ssh root@${HOST} 'apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin'
|
|
||||||
|
|
||||||
- name: prepare Docker bind mounts
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'mkdir -p /srv/chatmail/certs /srv/chatmail/dkim'
|
|
||||||
ssh root@${HOST} 'cp -a /var/lib/acme/. /srv/chatmail/certs/ && cp -a /etc/dkimkeys/. /srv/chatmail/dkim/' || true
|
|
||||||
|
|
||||||
- name: generate and upload chatmail.ini
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
cmdeploy init ${HOST}
|
|
||||||
sed -i 's/#\s*mtail_address/mtail_address/' chatmail.ini
|
|
||||||
scp chatmail.ini root@${HOST}:/srv/chatmail/chatmail.ini
|
|
||||||
|
|
||||||
- name: deploy with Docker
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
GHCR_IMAGE="${{ needs.build-docker.outputs.image }}"
|
|
||||||
rsync -avz --exclude='.git' --exclude='venv' --exclude='__pycache__' ./ root@${HOST}:/srv/chatmail/relay/
|
|
||||||
# Login to GHCR on VPS and pull pre-built image
|
|
||||||
echo "${{ secrets.GITHUB_TOKEN }}" | ssh root@${HOST} 'docker login ghcr.io -u ${{ github.actor }} --password-stdin'
|
|
||||||
ssh root@${HOST} "docker pull ${GHCR_IMAGE}"
|
|
||||||
ssh root@${HOST} "cd /srv/chatmail/relay && CHATMAIL_IMAGE=${GHCR_IMAGE} MAIL_DOMAIN=${HOST} docker compose -f docker-compose.yaml -f docker/docker-compose.ci.yaml up -d"
|
|
||||||
|
|
||||||
- name: wait for container healthy
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
# Stream journald inside the container
|
|
||||||
ssh root@${HOST} 'docker exec chatmail journalctl -f --no-pager' &
|
|
||||||
LOG_PID=$!
|
|
||||||
trap "kill $LOG_PID 2>/dev/null || true" EXIT
|
|
||||||
for i in $(seq 1 60); do
|
|
||||||
status=$(ssh root@${HOST} 'docker inspect --format={{.State.Health.Status}} chatmail 2>/dev/null' || echo "missing")
|
|
||||||
echo " [$i/60] status=$status"
|
|
||||||
if [ "$status" = "healthy" ]; then
|
|
||||||
echo "Container is healthy."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
if [ "$status" = "unhealthy" ]; then
|
|
||||||
echo "Container is unhealthy!"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
echo "Container did not become healthy."
|
|
||||||
kill $LOG_PID 2>/dev/null || true
|
|
||||||
echo "--- failed units ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail systemctl --failed --no-pager' || true
|
|
||||||
echo "--- service logs ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail journalctl -u dovecot -u postfix -u nginx -u unbound --no-pager -n 50' || true
|
|
||||||
echo "--- listening ports ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail ss -tlnp' || true
|
|
||||||
echo "--- chatmail.ini ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cat /etc/chatmail/chatmail.ini' || true
|
|
||||||
exit 1
|
|
||||||
|
|
||||||
- name: show container state
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
echo "--- listening ports ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail ss -tlnp'
|
|
||||||
echo "--- chatmail.ini ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cat /etc/chatmail/chatmail.ini'
|
|
||||||
|
|
||||||
- name: Docker offline tests
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: CHATMAIL_DOCKER=chatmail pytest --pyargs cmdeploy
|
|
||||||
|
|
||||||
- name: Docker DNS
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
# Reset zone file in case bare DNS already appended to it
|
|
||||||
git checkout .github/workflows/${ZONE}
|
|
||||||
ssh root@${HOST} 'docker exec chatmail chown opendkim:opendkim -R /etc/dkimkeys'
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cmdeploy dns --ssh-host @local --zonefile /opt/chatmail/staging.zone --verbose'
|
|
||||||
ssh root@${HOST} 'docker cp chatmail:/opt/chatmail/staging.zone /tmp/staging.zone'
|
|
||||||
scp root@${HOST}:/tmp/staging.zone staging-generated.zone
|
|
||||||
cat staging-generated.zone >> .github/workflows/${ZONE}
|
|
||||||
cat .github/workflows/${ZONE}
|
|
||||||
scp .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: Docker integration tests
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: CHATMAIL_DOCKER=chatmail CHATMAIL_DOMAIN2=ci-chatmail.testrun.org cmdeploy test --slow
|
|
||||||
|
|
||||||
- name: Docker final DNS check
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} 'docker exec chatmail cmdeploy dns -v --ssh-host @local'
|
|
||||||
|
|
||||||
# --- Cleanup ---
|
|
||||||
|
|
||||||
- name: add SSH keys
|
|
||||||
if: >-
|
|
||||||
!cancelled() && matrix.add_ssh_keys
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: ssh root@${{ matrix.host }} 'curl -s https://github.com/hpk42.keys https://github.com/j4n.keys >> .ssh/authorized_keys'
|
|
||||||
104
.github/workflows/test-and-deploy-ipv4only.yaml
vendored
Normal file
104
.github/workflows/test-and-deploy-ipv4only.yaml
vendored
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
name: deploy on staging-ipv4.testrun.org, and run tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- 'scripts/**'
|
||||||
|
- '**/README.md'
|
||||||
|
- 'CHANGELOG.md'
|
||||||
|
- 'LICENSE'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
name: deploy on staging-ipv4.testrun.org, and run tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
environment:
|
||||||
|
name: staging-ipv4.testrun.org
|
||||||
|
url: https://staging-ipv4.testrun.org/
|
||||||
|
concurrency: staging-ipv4.testrun.org
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: prepare SSH
|
||||||
|
run: |
|
||||||
|
mkdir ~/.ssh
|
||||||
|
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
ssh-keyscan staging-ipv4.testrun.org > ~/.ssh/known_hosts
|
||||||
|
# save previous acme & dkim state
|
||||||
|
rsync -avz root@staging-ipv4.testrun.org:/var/lib/acme acme-ipv4 || true
|
||||||
|
rsync -avz root@staging-ipv4.testrun.org:/etc/dkimkeys dkimkeys-ipv4 || true
|
||||||
|
# store previous acme & dkim state on ns.testrun.org, if it contains useful certs
|
||||||
|
if [ -f dkimkeys-ipv4/dkimkeys/opendkim.private ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" dkimkeys-ipv4 root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
if [ "$(ls -A acme-ipv4/acme/certs)" ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" acme-ipv4 root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
# make sure CAA record isn't set
|
||||||
|
scp -o StrictHostKeyChecking=accept-new .github/workflows/staging-ipv4.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging-ipv4.testrun.org /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: rebuild staging-ipv4.testrun.org to have a clean VPS
|
||||||
|
run: |
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"image":"debian-12"}' \
|
||||||
|
"https://api.hetzner.cloud/v1/servers/${{ secrets.STAGING_IPV4_SERVER_ID }}/actions/rebuild"
|
||||||
|
|
||||||
|
- run: scripts/initenv.sh
|
||||||
|
|
||||||
|
- name: append venv/bin to PATH
|
||||||
|
run: echo venv/bin >>$GITHUB_PATH
|
||||||
|
|
||||||
|
- name: upload TLS cert after rebuilding
|
||||||
|
run: |
|
||||||
|
echo " --- wait until staging-ipv4.testrun.org VPS is rebuilt --- "
|
||||||
|
rm ~/.ssh/known_hosts
|
||||||
|
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org id -u ; do sleep 1 ; done
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org id -u
|
||||||
|
# download acme & dkim state from ns.testrun.org
|
||||||
|
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/acme-ipv4/acme acme-restore || true
|
||||||
|
rsync -avz root@ns.testrun.org:/tmp/dkimkeys-ipv4/dkimkeys dkimkeys-restore || true
|
||||||
|
# restore acme & dkim state to staging2.testrun.org
|
||||||
|
rsync -avz acme-restore/acme root@staging-ipv4.testrun.org:/var/lib/ || true
|
||||||
|
rsync -avz dkimkeys-restore/dkimkeys root@staging-ipv4.testrun.org:/etc/ || true
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org chown root:root -R /var/lib/acme || true
|
||||||
|
|
||||||
|
- name: run deploy-chatmail offline tests
|
||||||
|
run: pytest --pyargs cmdeploy
|
||||||
|
|
||||||
|
- name: setup dependencies
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org apt update
|
||||||
|
ssh root@staging-ipv4.testrun.org apt install -y git python3.11-venv python3-dev gcc
|
||||||
|
ssh root@staging-ipv4.testrun.org git clone https://github.com/chatmail/relay
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && git checkout " ${{ github.head_ref }}
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/initenv.sh"
|
||||||
|
|
||||||
|
- name: initialize config
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy init staging-ipv4.testrun.org"
|
||||||
|
ssh root@staging-ipv4.testrun.org "sed -i 's#disable_ipv6 = False#disable_ipv6 = True#' relay/chatmail.ini"
|
||||||
|
ssh root@staging-ipv4.testrun.org "sed -i 's/#\s*mtail_address/mtail_address/' relay/chatmail.ini"
|
||||||
|
|
||||||
|
- run: ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy run --verbose --skip-dns-check --ssh-host localhost"
|
||||||
|
|
||||||
|
- name: set DNS entries
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy dns --zonefile staging-generated.zone --ssh-host localhost"
|
||||||
|
ssh root@staging-ipv4.testrun.org cat relay/staging-generated.zone >> .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging-ipv4.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging-ipv4.testrun.org /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: cmdeploy test
|
||||||
|
run: ssh root@staging-ipv4.testrun.org "cd relay && CHATMAIL_DOMAIN2=ci-chatmail.testrun.org scripts/cmdeploy test --slow --ssh-host localhost"
|
||||||
|
|
||||||
|
- name: cmdeploy dns
|
||||||
|
run: ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy dns -v --ssh-host localhost"
|
||||||
|
|
||||||
97
.github/workflows/test-and-deploy.yaml
vendored
Normal file
97
.github/workflows/test-and-deploy.yaml
vendored
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
name: deploy on staging2.testrun.org, and run tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- 'scripts/**'
|
||||||
|
- '**/README.md'
|
||||||
|
- 'CHANGELOG.md'
|
||||||
|
- 'LICENSE'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
name: deploy on staging2.testrun.org, and run tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
environment:
|
||||||
|
name: staging2.testrun.org
|
||||||
|
url: https://staging2.testrun.org/
|
||||||
|
concurrency: staging2.testrun.org
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: prepare SSH
|
||||||
|
run: |
|
||||||
|
mkdir ~/.ssh
|
||||||
|
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
ssh-keyscan staging2.testrun.org > ~/.ssh/known_hosts
|
||||||
|
# save previous acme & dkim state
|
||||||
|
rsync -avz root@staging2.testrun.org:/var/lib/acme . || true
|
||||||
|
rsync -avz root@staging2.testrun.org:/etc/dkimkeys . || true
|
||||||
|
# store previous acme & dkim state on ns.testrun.org, if it contains useful certs
|
||||||
|
if [ -f dkimkeys/opendkim.private ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" dkimkeys root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
if [ "$(ls -A acme/certs)" ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" acme root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
# make sure CAA record isn't set
|
||||||
|
scp -o StrictHostKeyChecking=accept-new .github/workflows/staging.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging2.testrun.org /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: rebuild staging2.testrun.org to have a clean VPS
|
||||||
|
run: |
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"image":"debian-12"}' \
|
||||||
|
"https://api.hetzner.cloud/v1/servers/${{ secrets.STAGING_SERVER_ID }}/actions/rebuild"
|
||||||
|
|
||||||
|
- run: scripts/initenv.sh
|
||||||
|
|
||||||
|
- name: append venv/bin to PATH
|
||||||
|
run: echo venv/bin >>$GITHUB_PATH
|
||||||
|
|
||||||
|
- name: upload TLS cert after rebuilding
|
||||||
|
run: |
|
||||||
|
echo " --- wait until staging2.testrun.org VPS is rebuilt --- "
|
||||||
|
rm ~/.ssh/known_hosts
|
||||||
|
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org id -u ; do sleep 1 ; done
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org id -u
|
||||||
|
# download acme & dkim state from ns.testrun.org
|
||||||
|
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/acme acme-restore || true
|
||||||
|
rsync -avz root@ns.testrun.org:/tmp/dkimkeys dkimkeys-restore || true
|
||||||
|
# restore acme & dkim state to staging2.testrun.org
|
||||||
|
rsync -avz acme-restore/acme root@staging2.testrun.org:/var/lib/ || true
|
||||||
|
rsync -avz dkimkeys-restore/dkimkeys root@staging2.testrun.org:/etc/ || true
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org chown root:root -R /var/lib/acme || true
|
||||||
|
|
||||||
|
- name: add hpk42 key to staging server
|
||||||
|
run: ssh root@staging2.testrun.org 'curl -s https://github.com/hpk42.keys >> .ssh/authorized_keys'
|
||||||
|
|
||||||
|
- name: run deploy-chatmail offline tests
|
||||||
|
run: pytest --pyargs cmdeploy
|
||||||
|
|
||||||
|
- run: |
|
||||||
|
cmdeploy init staging2.testrun.org
|
||||||
|
sed -i 's/#\s*mtail_address/mtail_address/' chatmail.ini
|
||||||
|
|
||||||
|
- run: cmdeploy run --verbose --skip-dns-check
|
||||||
|
|
||||||
|
- name: set DNS entries
|
||||||
|
run: |
|
||||||
|
cmdeploy dns --zonefile staging-generated.zone --verbose
|
||||||
|
cat staging-generated.zone >> .github/workflows/staging.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging2.testrun.org /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: cmdeploy test
|
||||||
|
run: CHATMAIL_DOMAIN2=ci-chatmail.testrun.org cmdeploy test --slow
|
||||||
|
|
||||||
|
- name: cmdeploy dns
|
||||||
|
run: cmdeploy dns -v
|
||||||
|
|
||||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -164,9 +164,3 @@ cython_debug/
|
|||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
chatmail.zone
|
chatmail.zone
|
||||||
|
|
||||||
# docker
|
|
||||||
/data/
|
|
||||||
/custom/
|
|
||||||
docker-compose.override.yaml
|
|
||||||
.env
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ where = ['src']
|
|||||||
[project.scripts]
|
[project.scripts]
|
||||||
doveauth = "chatmaild.doveauth:main"
|
doveauth = "chatmaild.doveauth:main"
|
||||||
chatmail-metadata = "chatmaild.metadata:main"
|
chatmail-metadata = "chatmaild.metadata:main"
|
||||||
chatmail-metrics = "chatmaild.metrics:main"
|
|
||||||
chatmail-expire = "chatmaild.expire:main"
|
chatmail-expire = "chatmaild.expire:main"
|
||||||
chatmail-fsreport = "chatmaild.fsreport:main"
|
chatmail-fsreport = "chatmaild.fsreport:main"
|
||||||
lastlogin = "chatmaild.lastlogin:main"
|
lastlogin = "chatmaild.lastlogin:main"
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import ipaddress
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -20,7 +21,10 @@ def read_config(inipath):
|
|||||||
class Config:
|
class Config:
|
||||||
def __init__(self, inipath, params):
|
def __init__(self, inipath, params):
|
||||||
self._inipath = inipath
|
self._inipath = inipath
|
||||||
self.mail_domain = params["mail_domain"]
|
if is_valid_ipv4(params["mail_domain"]):
|
||||||
|
self.mail_domain = f"[{params.get('mail_domain')}]"
|
||||||
|
else:
|
||||||
|
self.mail_domain = params["mail_domain"]
|
||||||
self.max_user_send_per_minute = int(params.get("max_user_send_per_minute", 60))
|
self.max_user_send_per_minute = int(params.get("max_user_send_per_minute", 60))
|
||||||
self.max_user_send_burst_size = int(params.get("max_user_send_burst_size", 10))
|
self.max_user_send_burst_size = int(params.get("max_user_send_burst_size", 10))
|
||||||
self.max_mailbox_size = params["max_mailbox_size"]
|
self.max_mailbox_size = params["max_mailbox_size"]
|
||||||
@@ -76,7 +80,7 @@ class Config:
|
|||||||
)
|
)
|
||||||
self.tls_cert_mode = "external"
|
self.tls_cert_mode = "external"
|
||||||
self.tls_cert_path, self.tls_key_path = parts
|
self.tls_cert_path, self.tls_key_path = parts
|
||||||
elif self.mail_domain.startswith("_"):
|
elif self.mail_domain.startswith("_") or is_valid_ipv4(params["mail_domain"]):
|
||||||
self.tls_cert_mode = "self"
|
self.tls_cert_mode = "self"
|
||||||
self.tls_cert_path = "/etc/ssl/certs/mailserver.pem"
|
self.tls_cert_path = "/etc/ssl/certs/mailserver.pem"
|
||||||
self.tls_key_path = "/etc/ssl/private/mailserver.key"
|
self.tls_key_path = "/etc/ssl/private/mailserver.key"
|
||||||
@@ -157,3 +161,12 @@ def get_default_config_content(mail_domain, **overrides):
|
|||||||
lines.append(line)
|
lines.append(line)
|
||||||
content = "\n".join(lines)
|
content = "\n".join(lines)
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_ipv4(address: str) -> bool:
|
||||||
|
"""Check if a mail_domain is an IPv4 address."""
|
||||||
|
try:
|
||||||
|
ipaddress.IPv4Address(address)
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import filelock
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import crypt_r
|
import crypt_r
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -13,6 +16,7 @@ from .dictproxy import DictProxy
|
|||||||
from .migrate_db import migrate_from_db_to_maildir
|
from .migrate_db import migrate_from_db_to_maildir
|
||||||
|
|
||||||
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
||||||
|
VALID_LOCALPART_RE = re.compile(r"^[a-z0-9._-]+$")
|
||||||
|
|
||||||
|
|
||||||
def encrypt_password(password: str):
|
def encrypt_password(password: str):
|
||||||
@@ -52,6 +56,10 @@ def is_allowed_to_create(config: Config, user, cleartext_password) -> bool:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if not VALID_LOCALPART_RE.match(localpart):
|
||||||
|
logging.warning("localpart %r contains invalid characters", localpart)
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -140,8 +148,13 @@ class AuthDictProxy(DictProxy):
|
|||||||
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
||||||
return
|
return
|
||||||
|
|
||||||
user.set_password(encrypt_password(cleartext_password))
|
lock = filelock.FileLock(str(user.password_path) + ".lock", timeout=5)
|
||||||
print(f"Created address: {addr}", file=sys.stderr)
|
with lock:
|
||||||
|
userdata = user.get_userdb_dict()
|
||||||
|
if userdata:
|
||||||
|
return userdata
|
||||||
|
user.set_password(encrypt_password(cleartext_password))
|
||||||
|
print(f"Created address: {addr}", file=sys.stderr)
|
||||||
return user.get_userdb_dict()
|
return user.get_userdb_dict()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,11 @@ class MetadataDictProxy(DictProxy):
|
|||||||
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
|
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
|
||||||
return f"O{self.iroh_relay}\n"
|
return f"O{self.iroh_relay}\n"
|
||||||
elif keyname == "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn":
|
elif keyname == "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn":
|
||||||
res = turn_credentials()
|
try:
|
||||||
|
res = turn_credentials()
|
||||||
|
except Exception:
|
||||||
|
logging.exception("failed to get TURN credentials")
|
||||||
|
return "N\n"
|
||||||
port = 3478
|
port = 3478
|
||||||
return f"O{self.turn_hostname}:{port}:{res}\n"
|
return f"O{self.turn_hostname}:{port}:{res}\n"
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def main(vmail_dir=None):
|
|
||||||
if vmail_dir is None:
|
|
||||||
vmail_dir = sys.argv[1]
|
|
||||||
|
|
||||||
accounts = 0
|
|
||||||
ci_accounts = 0
|
|
||||||
|
|
||||||
for path in Path(vmail_dir).iterdir():
|
|
||||||
if not path.joinpath("cur").is_dir():
|
|
||||||
continue
|
|
||||||
accounts += 1
|
|
||||||
if path.name[:3] in ("ci-", "ac_"):
|
|
||||||
ci_accounts += 1
|
|
||||||
|
|
||||||
print("# HELP total number of accounts")
|
|
||||||
print("# TYPE accounts gauge")
|
|
||||||
print(f"accounts {accounts}")
|
|
||||||
print("# HELP number of CI accounts")
|
|
||||||
print("# TYPE ci_accounts gauge")
|
|
||||||
print(f"ci_accounts {ci_accounts}")
|
|
||||||
print("# HELP number of non-CI accounts")
|
|
||||||
print("# TYPE nonci_accounts gauge")
|
|
||||||
print(f"nonci_accounts {accounts - ci_accounts}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -3,12 +3,11 @@
|
|||||||
"""CGI script for creating new accounts."""
|
"""CGI script for creating new accounts."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import random
|
|
||||||
import secrets
|
import secrets
|
||||||
import string
|
import string
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from chatmaild.config import Config, read_config
|
from chatmaild.config import Config, is_valid_ipv4, read_config
|
||||||
|
|
||||||
CONFIG_PATH = "/usr/local/lib/chatmaild/chatmail.ini"
|
CONFIG_PATH = "/usr/local/lib/chatmaild/chatmail.ini"
|
||||||
ALPHANUMERIC = string.ascii_lowercase + string.digits
|
ALPHANUMERIC = string.ascii_lowercase + string.digits
|
||||||
@@ -16,7 +15,9 @@ ALPHANUMERIC_PUNCT = string.ascii_letters + string.digits + string.punctuation
|
|||||||
|
|
||||||
|
|
||||||
def create_newemail_dict(config: Config):
|
def create_newemail_dict(config: Config):
|
||||||
user = "".join(random.choices(ALPHANUMERIC, k=config.username_max_length))
|
user = "".join(
|
||||||
|
secrets.choice(ALPHANUMERIC) for _ in range(config.username_max_length)
|
||||||
|
)
|
||||||
password = "".join(
|
password = "".join(
|
||||||
secrets.choice(ALPHANUMERIC_PUNCT)
|
secrets.choice(ALPHANUMERIC_PUNCT)
|
||||||
for _ in range(config.password_min_length + 3)
|
for _ in range(config.password_min_length + 3)
|
||||||
@@ -30,7 +31,15 @@ def create_dclogin_url(email, password):
|
|||||||
Uses ic=3 (AcceptInvalidCertificates) so chatmail clients
|
Uses ic=3 (AcceptInvalidCertificates) so chatmail clients
|
||||||
can connect to servers with self-signed TLS certificates.
|
can connect to servers with self-signed TLS certificates.
|
||||||
"""
|
"""
|
||||||
return f"dclogin:{quote(email, safe='@')}?p={quote(password, safe='')}&v=1&ic=3"
|
domain = email.split("@")[-1]
|
||||||
|
domain_without_brackets = domain.strip("[").strip("]")
|
||||||
|
if is_valid_ipv4(domain_without_brackets):
|
||||||
|
imap_host = "&ih=" + domain_without_brackets
|
||||||
|
smtp_host = "&sh=" + domain_without_brackets
|
||||||
|
else:
|
||||||
|
imap_host = ""
|
||||||
|
smtp_host = ""
|
||||||
|
return f"dclogin:{quote(email, safe='@[]')}?p={quote(password, safe='')}&v=1{imap_host}{smtp_host}&ic=3"
|
||||||
|
|
||||||
|
|
||||||
def print_new_account():
|
def print_new_account():
|
||||||
|
|||||||
@@ -120,6 +120,60 @@ def test_handle_dovecot_protocol_iterate(gencreds, example_config):
|
|||||||
assert not lines[2]
|
assert not lines[2]
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_localpart_characters(make_config):
|
||||||
|
"""Test that is_allowed_to_create rejects localparts with invalid characters."""
|
||||||
|
config = make_config("chat.example.org", {"username_min_length": "3"})
|
||||||
|
password = "zequ0Aimuchoodaechik"
|
||||||
|
domain = config.mail_domain
|
||||||
|
|
||||||
|
# valid localparts
|
||||||
|
assert is_allowed_to_create(config, f"abc123@{domain}", password)
|
||||||
|
assert is_allowed_to_create(config, f"a.b-c_d@{domain}", password)
|
||||||
|
|
||||||
|
# uppercase rejected
|
||||||
|
assert not is_allowed_to_create(config, f"Abc123@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"ABCDEFG@{domain}", password)
|
||||||
|
|
||||||
|
# spaces and special chars rejected
|
||||||
|
assert not is_allowed_to_create(config, f"a b cde@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc+def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc!def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"ab@cdef@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc/def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc\\def@{domain}", password)
|
||||||
|
|
||||||
|
|
||||||
|
def test_concurrent_creation_same_account(dictproxy):
|
||||||
|
"""Test that concurrent creation of the same account doesn't corrupt password."""
|
||||||
|
addr = "racetest1@chat.example.org"
|
||||||
|
password = "zequ0Aimuchoodaechik"
|
||||||
|
num_threads = 10
|
||||||
|
results = queue.Queue()
|
||||||
|
|
||||||
|
def create():
|
||||||
|
try:
|
||||||
|
res = dictproxy.lookup_passdb(addr, password)
|
||||||
|
results.put(("ok", res))
|
||||||
|
except Exception:
|
||||||
|
results.put(("err", traceback.format_exc()))
|
||||||
|
|
||||||
|
threads = [threading.Thread(target=create, daemon=True) for _ in range(num_threads)]
|
||||||
|
for t in threads:
|
||||||
|
t.start()
|
||||||
|
for t in threads:
|
||||||
|
t.join(timeout=10)
|
||||||
|
|
||||||
|
passwords_seen = set()
|
||||||
|
for _ in range(num_threads):
|
||||||
|
status, res = results.get()
|
||||||
|
if status == "err":
|
||||||
|
pytest.fail(f"concurrent creation failed\n{res}")
|
||||||
|
passwords_seen.add(res["password"])
|
||||||
|
|
||||||
|
# all threads must see the same password hash
|
||||||
|
assert len(passwords_seen) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_50_concurrent_lookups_different_accounts(gencreds, dictproxy):
|
def test_50_concurrent_lookups_different_accounts(gencreds, dictproxy):
|
||||||
num_threads = 50
|
num_threads = 50
|
||||||
req_per_thread = 5
|
req_per_thread = 5
|
||||||
|
|||||||
@@ -112,6 +112,43 @@ def test_report(mbox1, example_config):
|
|||||||
report_main(args)
|
report_main(args)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_mdir_filters_by_path(mbox1, example_config):
|
||||||
|
"""Test that Report with mdir='cur' only counts messages in cur/ subdirectory."""
|
||||||
|
from chatmaild.fsreport import Report
|
||||||
|
|
||||||
|
now = datetime.utcnow().timestamp()
|
||||||
|
|
||||||
|
# Set password mtime to old enough so min_login_age check passes
|
||||||
|
password = Path(mbox1.basedir).joinpath("password")
|
||||||
|
old_time = now - 86400 * 10 # 10 days ago
|
||||||
|
os.utime(password, (old_time, old_time))
|
||||||
|
|
||||||
|
# Reload mailbox with updated mtime
|
||||||
|
from chatmaild.expire import MailboxStat
|
||||||
|
|
||||||
|
mbox = MailboxStat(mbox1.basedir)
|
||||||
|
|
||||||
|
# Report without mdir — should count all messages
|
||||||
|
rep_all = Report(now=now, min_login_age=1, mdir=None)
|
||||||
|
rep_all.process_mailbox_stat(mbox)
|
||||||
|
total_all = rep_all.message_buckets[0]
|
||||||
|
|
||||||
|
# Report with mdir='cur' — should only count cur/ messages
|
||||||
|
rep_cur = Report(now=now, min_login_age=1, mdir="cur")
|
||||||
|
rep_cur.process_mailbox_stat(mbox)
|
||||||
|
total_cur = rep_cur.message_buckets[0]
|
||||||
|
|
||||||
|
# Report with mdir='new' — should only count new/ messages
|
||||||
|
rep_new = Report(now=now, min_login_age=1, mdir="new")
|
||||||
|
rep_new.process_mailbox_stat(mbox)
|
||||||
|
total_new = rep_new.message_buckets[0]
|
||||||
|
|
||||||
|
# cur has 500-byte msg, new has 600-byte msg (from fill_mbox)
|
||||||
|
assert total_cur == 500
|
||||||
|
assert total_new == 600
|
||||||
|
assert total_all == 500 + 600
|
||||||
|
|
||||||
|
|
||||||
def test_expiry_cli_basic(example_config, mbox1):
|
def test_expiry_cli_basic(example_config, mbox1):
|
||||||
args = (str(example_config._inipath),)
|
args = (str(example_config._inipath),)
|
||||||
expiry_main(args)
|
expiry_main(args)
|
||||||
|
|||||||
@@ -314,6 +314,51 @@ def test_persistent_queue_items(tmp_path, testaddr, token):
|
|||||||
assert not queue_item < item2 and not item2 < queue_item
|
assert not queue_item < item2 and not item2 < queue_item
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_exception_returns_N(notifier, metadata, monkeypatch):
|
||||||
|
"""Test that turn_credentials() failure returns N\\n instead of crashing."""
|
||||||
|
import chatmaild.metadata
|
||||||
|
|
||||||
|
dictproxy = MetadataDictProxy(
|
||||||
|
notifier=notifier,
|
||||||
|
metadata=metadata,
|
||||||
|
turn_hostname="turn.example.org",
|
||||||
|
)
|
||||||
|
|
||||||
|
def mock_turn_credentials():
|
||||||
|
raise ConnectionRefusedError("socket not available")
|
||||||
|
|
||||||
|
monkeypatch.setattr(chatmaild.metadata, "turn_credentials", mock_turn_credentials)
|
||||||
|
|
||||||
|
transactions = {}
|
||||||
|
res = dictproxy.handle_dovecot_request(
|
||||||
|
"Lshared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||||
|
"\tuser@example.org",
|
||||||
|
transactions,
|
||||||
|
)
|
||||||
|
assert res == "N\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_success(notifier, metadata, monkeypatch):
|
||||||
|
"""Test that valid turn_credentials() returns TURN URI."""
|
||||||
|
import chatmaild.metadata
|
||||||
|
|
||||||
|
dictproxy = MetadataDictProxy(
|
||||||
|
notifier=notifier,
|
||||||
|
metadata=metadata,
|
||||||
|
turn_hostname="turn.example.org",
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setattr(chatmaild.metadata, "turn_credentials", lambda: "user:pass")
|
||||||
|
|
||||||
|
transactions = {}
|
||||||
|
res = dictproxy.handle_dovecot_request(
|
||||||
|
"Lshared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||||
|
"\tuser@example.org",
|
||||||
|
transactions,
|
||||||
|
)
|
||||||
|
assert res == "Oturn.example.org:3478:user:pass\n"
|
||||||
|
|
||||||
|
|
||||||
def test_iroh_relay(dictproxy):
|
def test_iroh_relay(dictproxy):
|
||||||
rfile = io.BytesIO(
|
rfile = io.BytesIO(
|
||||||
b"\n".join(
|
b"\n".join(
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
from chatmaild.metrics import main
|
|
||||||
|
|
||||||
|
|
||||||
def test_main(tmp_path, capsys):
|
|
||||||
paths = []
|
|
||||||
for x in ("ci-asllkj", "ac_12l3kj", "qweqwe", "ci-l1k2j31l2k3"):
|
|
||||||
p = tmp_path.joinpath(x)
|
|
||||||
p.mkdir()
|
|
||||||
p.joinpath("cur").mkdir()
|
|
||||||
paths.append(p)
|
|
||||||
|
|
||||||
tmp_path.joinpath("nomailbox").mkdir()
|
|
||||||
|
|
||||||
main(tmp_path)
|
|
||||||
out, _ = capsys.readouterr()
|
|
||||||
d = {}
|
|
||||||
for line in out.split("\n"):
|
|
||||||
if line.strip() and not line.startswith("#"):
|
|
||||||
name, num = line.split()
|
|
||||||
d[name] = int(num)
|
|
||||||
|
|
||||||
assert d["accounts"] == 4
|
|
||||||
assert d["ci_accounts"] == 3
|
|
||||||
assert d["nonci_accounts"] == 1
|
|
||||||
73
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
73
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from chatmaild.turnserver import turn_credentials
|
||||||
|
|
||||||
|
SOCKET_PATH = "/run/chatmail-turn/turn.socket"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def turn_socket(tmp_path):
|
||||||
|
"""Create a real Unix socket server at a temp path."""
|
||||||
|
sock_path = str(tmp_path / "turn.socket")
|
||||||
|
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
server.bind(sock_path)
|
||||||
|
server.listen(1)
|
||||||
|
yield sock_path, server
|
||||||
|
server.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _call_turn_credentials(sock_path):
|
||||||
|
"""Call turn_credentials but connect to sock_path instead of hardcoded path."""
|
||||||
|
original_connect = socket.socket.connect
|
||||||
|
|
||||||
|
def patched_connect(self, address):
|
||||||
|
if address == SOCKET_PATH:
|
||||||
|
address = sock_path
|
||||||
|
return original_connect(self, address)
|
||||||
|
|
||||||
|
with patch.object(socket.socket, "connect", patched_connect):
|
||||||
|
return turn_credentials()
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_timeout(turn_socket):
|
||||||
|
"""Server accepts but never responds — must raise socket.timeout."""
|
||||||
|
sock_path, server = turn_socket
|
||||||
|
|
||||||
|
def accept_and_hang():
|
||||||
|
conn, _ = server.accept()
|
||||||
|
time.sleep(30)
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
t = threading.Thread(target=accept_and_hang, daemon=True)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
with pytest.raises(socket.timeout):
|
||||||
|
_call_turn_credentials(sock_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_connection_refused(tmp_path):
|
||||||
|
"""Socket file doesn't exist — must raise ConnectionRefusedError or FileNotFoundError."""
|
||||||
|
missing = str(tmp_path / "nonexistent.socket")
|
||||||
|
with pytest.raises((ConnectionRefusedError, FileNotFoundError)):
|
||||||
|
_call_turn_credentials(missing)
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_success(turn_socket):
|
||||||
|
"""Server responds with credentials — must return stripped string."""
|
||||||
|
sock_path, server = turn_socket
|
||||||
|
|
||||||
|
def respond():
|
||||||
|
conn, _ = server.accept()
|
||||||
|
conn.sendall(b"testuser:testpass\n")
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
t = threading.Thread(target=respond, daemon=True)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
result = _call_turn_credentials(sock_path)
|
||||||
|
assert result == "testuser:testpass"
|
||||||
@@ -4,6 +4,7 @@ import socket
|
|||||||
|
|
||||||
def turn_credentials() -> str:
|
def turn_credentials() -> str:
|
||||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
||||||
|
client_socket.settimeout(5)
|
||||||
client_socket.connect("/run/chatmail-turn/turn.socket")
|
client_socket.connect("/run/chatmail-turn/turn.socket")
|
||||||
with client_socket.makefile("rb") as file:
|
with client_socket.makefile("rb") as file:
|
||||||
return file.readline().decode("utf-8").strip()
|
return file.readline().decode("utf-8").strip()
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class AcmetoolDeployer(Deployer):
|
|||||||
)
|
)
|
||||||
files.template(
|
files.template(
|
||||||
src=importlib.resources.files(__package__).joinpath("desired.yaml.j2"),
|
src=importlib.resources.files(__package__).joinpath("desired.yaml.j2"),
|
||||||
dest=f"/var/lib/acme/desired/{self.domains[0]}", # 0 is mailhost TLD
|
dest=f"/var/lib/acme/desired/{self.domains[0]}", # 0 is mailhost TLD
|
||||||
user="root",
|
user="root",
|
||||||
group="root",
|
group="root",
|
||||||
mode="644",
|
mode="644",
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import sys
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pyinfra
|
import pyinfra
|
||||||
from chatmaild.config import read_config, write_initial_config
|
from chatmaild.config import read_config, write_initial_config, is_valid_ipv4
|
||||||
from packaging import version
|
from packaging import version
|
||||||
from termcolor import colored
|
from termcolor import colored
|
||||||
|
|
||||||
@@ -87,11 +87,11 @@ def run_cmd_options(parser):
|
|||||||
def run_cmd(args, out):
|
def run_cmd(args, out):
|
||||||
"""Deploy chatmail services on the remote server."""
|
"""Deploy chatmail services on the remote server."""
|
||||||
|
|
||||||
ssh_host = args.ssh_host if args.ssh_host else args.config.mail_domain
|
ssh_host = args.ssh_host if args.ssh_host else args.config.mail_domain.strip("[").strip("]")
|
||||||
sshexec = get_sshexec(ssh_host)
|
sshexec = get_sshexec(ssh_host)
|
||||||
require_iroh = args.config.enable_iroh_relay
|
require_iroh = args.config.enable_iroh_relay
|
||||||
strict_tls = args.config.tls_cert_mode == "acme"
|
strict_tls = args.config.tls_cert_mode == "acme"
|
||||||
if not args.dns_check_disabled:
|
if not args.dns_check_disabled and not is_valid_ipv4(args.config.mail_domain.strip("[").strip("]")):
|
||||||
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
remote_data = dns.get_initial_remote_data(sshexec, args.config.mail_domain)
|
||||||
if not dns.check_initial_remote_data(remote_data, strict_tls=strict_tls, print=out.red):
|
if not dns.check_initial_remote_data(remote_data, strict_tls=strict_tls, print=out.red):
|
||||||
return 1
|
return 1
|
||||||
@@ -101,14 +101,17 @@ def run_cmd(args, out):
|
|||||||
env["CHATMAIL_WEBSITE_ONLY"] = "True" if args.website_only else ""
|
env["CHATMAIL_WEBSITE_ONLY"] = "True" if args.website_only else ""
|
||||||
env["CHATMAIL_DISABLE_MAIL"] = "True" if args.disable_mail else ""
|
env["CHATMAIL_DISABLE_MAIL"] = "True" if args.disable_mail else ""
|
||||||
env["CHATMAIL_REQUIRE_IROH"] = "True" if require_iroh else ""
|
env["CHATMAIL_REQUIRE_IROH"] = "True" if require_iroh else ""
|
||||||
if not args.dns_check_disabled:
|
if not args.dns_check_disabled and not is_valid_ipv4(args.config.mail_domain.strip("[").strip("]")):
|
||||||
env["CHATMAIL_ADDR_V4"] = remote_data.get("A") or ""
|
env["CHATMAIL_ADDR_V4"] = remote_data.get("A") or ""
|
||||||
env["CHATMAIL_ADDR_V6"] = remote_data.get("AAAA") or ""
|
env["CHATMAIL_ADDR_V6"] = remote_data.get("AAAA") or ""
|
||||||
deploy_path = importlib.resources.files(__package__).joinpath("run.py").resolve()
|
deploy_path = importlib.resources.files(__package__).joinpath("run.py").resolve()
|
||||||
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
|
pyinf = "pyinfra --dry" if args.dry_run else "pyinfra"
|
||||||
|
|
||||||
cmd = f"{pyinf} --ssh-user root {ssh_host} {deploy_path} -y"
|
cmd = f"{pyinf} --ssh-user root {ssh_host} {deploy_path} -y"
|
||||||
if ssh_host == "localhost":
|
if ssh_host in ["localhost", "@docker"]:
|
||||||
|
if ssh_host == "@docker":
|
||||||
|
env["CHATMAIL_NOPORTCHECK"] = "True"
|
||||||
|
env["CHATMAIL_NOSYSCTL"] = "True"
|
||||||
cmd = f"{pyinf} @local {deploy_path} -y"
|
cmd = f"{pyinf} @local {deploy_path} -y"
|
||||||
|
|
||||||
if version.parse(pyinfra.__version__) < version.parse("3"):
|
if version.parse(pyinfra.__version__) < version.parse("3"):
|
||||||
@@ -116,24 +119,18 @@ def run_cmd(args, out):
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retcode = out.check_call(cmd, env=env)
|
out.check_call(cmd, env=env)
|
||||||
if args.website_only:
|
if args.website_only:
|
||||||
if retcode == 0:
|
out.green("Website deployment completed.")
|
||||||
out.green("Website deployment completed.")
|
|
||||||
else:
|
|
||||||
out.red("Website deployment failed.")
|
|
||||||
elif retcode == 0:
|
|
||||||
out.green("Deploy completed, call `cmdeploy dns` next.")
|
|
||||||
elif not args.dns_check_disabled and strict_tls and not remote_data["acme_account_url"]:
|
elif not args.dns_check_disabled and strict_tls and not remote_data["acme_account_url"]:
|
||||||
out.red("Deploy completed but letsencrypt not configured")
|
out.red("Deploy completed but letsencrypt not configured")
|
||||||
out.red("Run 'cmdeploy run' again")
|
out.red("Run 'cmdeploy run' again")
|
||||||
retcode = 0
|
|
||||||
else:
|
else:
|
||||||
out.red("Deploy failed")
|
out.green("Deploy completed, call `cmdeploy dns` next.")
|
||||||
|
return 0
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
out.red("Deploy failed")
|
out.red("Deploy failed")
|
||||||
retcode = 1
|
return 1
|
||||||
return retcode
|
|
||||||
|
|
||||||
|
|
||||||
def dns_cmd_options(parser):
|
def dns_cmd_options(parser):
|
||||||
@@ -319,7 +316,7 @@ def add_ssh_host_option(parser):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--ssh-host",
|
"--ssh-host",
|
||||||
dest="ssh_host",
|
dest="ssh_host",
|
||||||
help="Run commands on 'localhost' or on a specific SSH host "
|
help="Run commands on 'localhost', via '@docker', or on a specific SSH host "
|
||||||
"instead of chatmail.ini's mail_domain.",
|
"instead of chatmail.ini's mail_domain.",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -381,7 +378,9 @@ def get_parser():
|
|||||||
|
|
||||||
def get_sshexec(ssh_host: str, verbose=True):
|
def get_sshexec(ssh_host: str, verbose=True):
|
||||||
if ssh_host in ["localhost", "@local"]:
|
if ssh_host in ["localhost", "@local"]:
|
||||||
return LocalExec(verbose)
|
return LocalExec(verbose, docker=False)
|
||||||
|
elif ssh_host == "@docker":
|
||||||
|
return LocalExec(verbose, docker=True)
|
||||||
if verbose:
|
if verbose:
|
||||||
print(f"[ssh] login to {ssh_host}")
|
print(f"[ssh] login to {ssh_host}")
|
||||||
return SSHExec(ssh_host, verbose=verbose)
|
return SSHExec(ssh_host, verbose=verbose)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from io import StringIO
|
from io import BytesIO, StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from chatmaild.config import read_config
|
from chatmaild.config import read_config
|
||||||
@@ -123,7 +123,6 @@ def _install_remote_venv_with_chatmaild() -> None:
|
|||||||
|
|
||||||
def _configure_remote_venv_with_chatmaild(config) -> None:
|
def _configure_remote_venv_with_chatmaild(config) -> None:
|
||||||
remote_base_dir = "/usr/local/lib/chatmaild"
|
remote_base_dir = "/usr/local/lib/chatmaild"
|
||||||
remote_venv_dir = f"{remote_base_dir}/venv"
|
|
||||||
remote_chatmail_inipath = f"{remote_base_dir}/chatmail.ini"
|
remote_chatmail_inipath = f"{remote_base_dir}/chatmail.ini"
|
||||||
root_owned = dict(user="root", group="root", mode="644")
|
root_owned = dict(user="root", group="root", mode="644")
|
||||||
|
|
||||||
@@ -134,16 +133,13 @@ def _configure_remote_venv_with_chatmaild(config) -> None:
|
|||||||
**root_owned,
|
**root_owned,
|
||||||
)
|
)
|
||||||
|
|
||||||
files.template(
|
files.file(
|
||||||
src=get_resource("metrics.cron.j2"),
|
path="/etc/cron.d/chatmail-metrics",
|
||||||
dest="/etc/cron.d/chatmail-metrics",
|
present=False,
|
||||||
user="root",
|
)
|
||||||
group="root",
|
files.file(
|
||||||
mode="644",
|
path="/var/www/html/metrics",
|
||||||
config={
|
present=False,
|
||||||
"mailboxes_dir": config.mailboxes_dir,
|
|
||||||
"execpath": f"{remote_venv_dir}/bin/chatmail-metrics",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -271,6 +267,9 @@ class WebsiteDeployer(Deployer):
|
|||||||
# if www_folder is a hugo page, build it
|
# if www_folder is a hugo page, build it
|
||||||
if build_dir:
|
if build_dir:
|
||||||
www_path = build_webpages(src_dir, build_dir, self.config)
|
www_path = build_webpages(src_dir, build_dir, self.config)
|
||||||
|
if www_path is None:
|
||||||
|
logger.warning("Web page build failed, skipping website deployment")
|
||||||
|
return
|
||||||
# if it is not a hugo page, upload it as is
|
# if it is not a hugo page, upload it as is
|
||||||
files.rsync(
|
files.rsync(
|
||||||
f"{www_path}/", "/var/www/html", flags=["-avz", "--chown=www-data"]
|
f"{www_path}/", "/var/www/html", flags=["-avz", "--chown=www-data"]
|
||||||
@@ -479,6 +478,14 @@ class ChatmailDeployer(Deployer):
|
|||||||
self.mail_domain = mail_domain
|
self.mail_domain = mail_domain
|
||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
|
files.put(
|
||||||
|
name="Disable installing recommended packages globally",
|
||||||
|
src=BytesIO(b'APT::Install-Recommends "false";\n'),
|
||||||
|
dest="/etc/apt/apt.conf.d/00InstallRecommends",
|
||||||
|
user="root",
|
||||||
|
group="root",
|
||||||
|
mode="644",
|
||||||
|
)
|
||||||
apt.update(name="apt update", cache_time=24 * 3600)
|
apt.update(name="apt update", cache_time=24 * 3600)
|
||||||
apt.upgrade(name="upgrade apt packages", auto_remove=True)
|
apt.upgrade(name="upgrade apt packages", auto_remove=True)
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,9 @@ class DovecotDeployer(Deployer):
|
|||||||
restart = False if self.disable_mail else self.need_restart
|
restart = False if self.disable_mail else self.need_restart
|
||||||
|
|
||||||
systemd.service(
|
systemd.service(
|
||||||
name="Disable dovecot for now" if self.disable_mail else "Start and enable Dovecot",
|
name="Disable dovecot for now"
|
||||||
|
if self.disable_mail
|
||||||
|
else "Start and enable Dovecot",
|
||||||
service="dovecot.service",
|
service="dovecot.service",
|
||||||
running=False if self.disable_mail else True,
|
running=False if self.disable_mail else True,
|
||||||
enabled=False if self.disable_mail else True,
|
enabled=False if self.disable_mail else True,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ listen = 0.0.0.0
|
|||||||
protocols = imap lmtp
|
protocols = imap lmtp
|
||||||
|
|
||||||
auth_mechanisms = plain
|
auth_mechanisms = plain
|
||||||
|
auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@[]
|
||||||
|
|
||||||
{% if debug == true %}
|
{% if debug == true %}
|
||||||
auth_verbose = yes
|
auth_verbose = yes
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
*/5 * * * * root {{ config.execpath }} {{ config.mailboxes_dir }} >/var/www/html/metrics
|
|
||||||
@@ -54,7 +54,7 @@ http {
|
|||||||
include /etc/nginx/mime.types;
|
include /etc/nginx/mime.types;
|
||||||
default_type application/octet-stream;
|
default_type application/octet-stream;
|
||||||
|
|
||||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
|
ssl_protocols TLSv1.2 TLSv1.3;
|
||||||
ssl_prefer_server_ciphers on;
|
ssl_prefer_server_ciphers on;
|
||||||
ssl_certificate {{ config.tls_cert_path }};
|
ssl_certificate {{ config.tls_cert_path }};
|
||||||
ssl_certificate_key {{ config.tls_key_path }};
|
ssl_certificate_key {{ config.tls_key_path }};
|
||||||
@@ -79,10 +79,6 @@ http {
|
|||||||
try_files $uri $uri/ =404;
|
try_files $uri $uri/ =404;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /metrics {
|
|
||||||
default_type text/plain;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /new {
|
location /new {
|
||||||
{% if config.tls_cert_mode != "self" %}
|
{% if config.tls_cert_mode != "self" %}
|
||||||
if ($request_method = GET) {
|
if ($request_method = GET) {
|
||||||
|
|||||||
@@ -97,7 +97,9 @@ class PostfixDeployer(Deployer):
|
|||||||
server.shell(
|
server.shell(
|
||||||
name="Validate postfix configuration",
|
name="Validate postfix configuration",
|
||||||
# Extract stderr and quit with error if non-zero
|
# Extract stderr and quit with error if non-zero
|
||||||
commands=["""bash -c 'w=$(postconf 2>&1 >/dev/null); [[ -z "$w" ]] || { echo "$w"; false; }'"""],
|
commands=[
|
||||||
|
"""bash -c 'w=$(postconf 2>&1 >/dev/null); [[ -z "$w" ]] || { echo "$w"; false; }'"""
|
||||||
|
],
|
||||||
)
|
)
|
||||||
self.need_restart = need_restart
|
self.need_restart = need_restart
|
||||||
|
|
||||||
|
|||||||
@@ -54,14 +54,15 @@ smtpd_tls_exclude_ciphers = aNULL, RC4, MD5, DES
|
|||||||
tls_preempt_cipherlist = yes
|
tls_preempt_cipherlist = yes
|
||||||
|
|
||||||
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
|
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
|
||||||
myhostname = {{ config.mail_domain }}
|
|
||||||
alias_maps = hash:/etc/aliases
|
alias_maps = hash:/etc/aliases
|
||||||
alias_database = hash:/etc/aliases
|
alias_database = hash:/etc/aliases
|
||||||
|
|
||||||
# Postfix does not deliver mail for any domain by itself.
|
# Postfix does not deliver mail for any domain by itself.
|
||||||
# Primary domain is listed in `virtual_mailbox_domains` instead
|
# Primary domain is listed in `virtual_mailbox_domains` instead
|
||||||
# and handed over to Dovecot.
|
# and handed over to Dovecot.
|
||||||
mydestination =
|
mydestination = {{ config.mail_domain }}
|
||||||
|
local_transport = lmtp:unix:private/dovecot-lmtp
|
||||||
|
local_recipient_maps =
|
||||||
|
|
||||||
relayhost =
|
relayhost =
|
||||||
{% if disable_ipv6 %}
|
{% if disable_ipv6 %}
|
||||||
@@ -88,8 +89,6 @@ inet_protocols = ipv4
|
|||||||
inet_protocols = all
|
inet_protocols = all
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
virtual_transport = lmtp:unix:private/dovecot-lmtp
|
|
||||||
virtual_mailbox_domains = {{ config.mail_domain }}
|
|
||||||
lmtp_header_checks = regexp:/etc/postfix/lmtp_header_cleanup
|
lmtp_header_checks = regexp:/etc/postfix/lmtp_header_cleanup
|
||||||
|
|
||||||
mua_client_restrictions = permit_sasl_authenticated, reject
|
mua_client_restrictions = permit_sasl_authenticated, reject
|
||||||
|
|||||||
@@ -80,7 +80,9 @@ filter unix - n n - - lmtp
|
|||||||
127.0.0.1:{{ config.postfix_reinject_port }} inet n - n - 100 smtpd
|
127.0.0.1:{{ config.postfix_reinject_port }} inet n - n - 100 smtpd
|
||||||
-o syslog_name=postfix/reinject
|
-o syslog_name=postfix/reinject
|
||||||
-o milter_macro_daemon_name=ORIGINATING
|
-o milter_macro_daemon_name=ORIGINATING
|
||||||
|
{% if "[" not in config.mail_domain %}
|
||||||
-o smtpd_milters=unix:opendkim/opendkim.sock
|
-o smtpd_milters=unix:opendkim/opendkim.sock
|
||||||
|
{% endif %}
|
||||||
-o cleanup_service_name=authclean
|
-o cleanup_service_name=authclean
|
||||||
|
|
||||||
# Local SMTP server for reinjecting incoming filtered mail
|
# Local SMTP server for reinjecting incoming filtered mail
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ def get_dkim_entry(mail_domain, pre_command, dkim_selector):
|
|||||||
print=log_progress,
|
print=log_progress,
|
||||||
)
|
)
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
return
|
return None, None
|
||||||
dkim_value_raw = f"v=DKIM1;k=rsa;p={dkim_pubkey};s=email;t=s"
|
dkim_value_raw = f"v=DKIM1;k=rsa;p={dkim_pubkey};s=email;t=s"
|
||||||
dkim_value = '" "'.join(re.findall(".{1,255}", dkim_value_raw))
|
dkim_value = '" "'.join(re.findall(".{1,255}", dkim_value_raw))
|
||||||
web_dkim_value = "".join(re.findall(".{1,255}", dkim_value_raw))
|
web_dkim_value = "".join(re.findall(".{1,255}", dkim_value_raw))
|
||||||
|
|||||||
@@ -40,5 +40,5 @@ def dovecot_recalc_quota(user):
|
|||||||
#
|
#
|
||||||
for line in output.split("\n"):
|
for line in output.split("\n"):
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if parts[2] == "STORAGE":
|
if len(parts) >= 6 and parts[2] == "STORAGE":
|
||||||
return dict(value=int(parts[3]), limit=int(parts[4]), percent=int(parts[5]))
|
return dict(value=int(parts[3]), limit=int(parts[4]), percent=int(parts[5]))
|
||||||
|
|||||||
@@ -50,9 +50,6 @@ class SSHExec:
|
|||||||
FuncError = FuncError
|
FuncError = FuncError
|
||||||
|
|
||||||
def __init__(self, host, verbose=False, python="python3", timeout=60):
|
def __init__(self, host, verbose=False, python="python3", timeout=60):
|
||||||
docker_container = os.environ.get("CHATMAIL_DOCKER")
|
|
||||||
if docker_container:
|
|
||||||
python = f"docker exec -i {docker_container} python3"
|
|
||||||
self.gateway = execnet.makegateway(f"ssh=root@{host}//python={python}")
|
self.gateway = execnet.makegateway(f"ssh=root@{host}//python={python}")
|
||||||
self._remote_cmdloop_channel = bootstrap_remote(self.gateway, remote)
|
self._remote_cmdloop_channel = bootstrap_remote(self.gateway, remote)
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
@@ -90,8 +87,9 @@ class SSHExec:
|
|||||||
class LocalExec:
|
class LocalExec:
|
||||||
FuncError = FuncError
|
FuncError = FuncError
|
||||||
|
|
||||||
def __init__(self, verbose=False):
|
def __init__(self, verbose=False, docker=False):
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
self.docker = docker
|
||||||
|
|
||||||
def __call__(self, call, kwargs=None, log_callback=None):
|
def __call__(self, call, kwargs=None, log_callback=None):
|
||||||
if kwargs is None:
|
if kwargs is None:
|
||||||
@@ -103,6 +101,10 @@ class LocalExec:
|
|||||||
if not title:
|
if not title:
|
||||||
title = call.__name__
|
title = call.__name__
|
||||||
where = "locally"
|
where = "locally"
|
||||||
|
if self.docker:
|
||||||
|
if call == remote.rdns.perform_initial_checks:
|
||||||
|
kwargs["pre_command"] = "docker exec chatmail "
|
||||||
|
where = "in docker"
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print_stderr(f"Running {where}: {title}(**{kwargs})")
|
print_stderr(f"Running {where}: {title}(**{kwargs})")
|
||||||
return self(call, kwargs, log_callback=print_stderr)
|
return self(call, kwargs, log_callback=print_stderr)
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ from chatmaild.config import read_config
|
|||||||
from cmdeploy.cmdeploy import main
|
from cmdeploy.cmdeploy import main
|
||||||
|
|
||||||
|
|
||||||
def test_init(tmp_path, maildomain):
|
def test_init(tmp_path, maildomain_sanitized):
|
||||||
inipath = tmp_path.joinpath("chatmail.ini")
|
inipath = tmp_path.joinpath("chatmail.ini")
|
||||||
main(["init", "--config", str(inipath), maildomain])
|
main(["init", "--config", str(inipath), maildomain_sanitized])
|
||||||
config = read_config(inipath)
|
config = read_config(inipath)
|
||||||
assert config.mail_domain == maildomain
|
assert config.mail_domain.strip("[").strip("]") == maildomain_sanitized
|
||||||
|
|
||||||
|
|
||||||
def test_capabilities(imap):
|
def test_capabilities(imap):
|
||||||
@@ -92,7 +92,7 @@ def test_concurrent_logins_same_account(
|
|||||||
def test_no_vrfy(chatmail_config):
|
def test_no_vrfy(chatmail_config):
|
||||||
domain = chatmail_config.mail_domain
|
domain = chatmail_config.mail_domain
|
||||||
|
|
||||||
s = smtplib.SMTP(domain)
|
s = smtplib.SMTP(domain.strip("[").strip("]"))
|
||||||
s.starttls()
|
s.starttls()
|
||||||
|
|
||||||
s.putcmd("vrfy", f"wrongaddress@{chatmail_config.mail_domain}")
|
s.putcmd("vrfy", f"wrongaddress@{chatmail_config.mail_domain}")
|
||||||
|
|||||||
@@ -10,31 +10,31 @@ def test_gen_qr_png_data(maildomain):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.filterwarnings("ignore::urllib3.exceptions.InsecureRequestWarning")
|
@pytest.mark.filterwarnings("ignore::urllib3.exceptions.InsecureRequestWarning")
|
||||||
def test_fastcgi_working(maildomain, chatmail_config):
|
def test_fastcgi_working(maildomain_sanitized, chatmail_config):
|
||||||
url = f"https://{maildomain}/new"
|
url = f"https://{maildomain_sanitized}/new"
|
||||||
print(url)
|
print(url)
|
||||||
verify = chatmail_config.tls_cert_mode == "acme"
|
verify = chatmail_config.tls_cert_mode == "acme"
|
||||||
res = requests.post(url, verify=verify)
|
res = requests.post(url, verify=verify)
|
||||||
assert maildomain in res.json().get("email")
|
assert maildomain_sanitized in res.json().get("email")
|
||||||
assert len(res.json().get("password")) > chatmail_config.password_min_length
|
assert len(res.json().get("password")) > chatmail_config.password_min_length
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.filterwarnings("ignore::urllib3.exceptions.InsecureRequestWarning")
|
@pytest.mark.filterwarnings("ignore::urllib3.exceptions.InsecureRequestWarning")
|
||||||
def test_newemail_configure(maildomain, rpc, chatmail_config):
|
def test_newemail_configure(maildomain_sanitized, rpc, chatmail_config):
|
||||||
"""Test configuring accounts by scanning a QR code works."""
|
"""Test configuring accounts by scanning a QR code works."""
|
||||||
url = f"DCACCOUNT:https://{maildomain}/new"
|
url = f"DCACCOUNT:https://{maildomain_sanitized}/new"
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
account_id = rpc.add_account()
|
account_id = rpc.add_account()
|
||||||
if chatmail_config.tls_cert_mode == "self":
|
if chatmail_config.tls_cert_mode == "self":
|
||||||
# deltachat core's rustls rejects self-signed HTTPS certs during
|
# deltachat core's rustls rejects self-signed HTTPS certs during
|
||||||
# set_config_from_qr, so fetch credentials via requests instead
|
# set_config_from_qr, so fetch credentials via requests instead
|
||||||
res = requests.post(f"https://{maildomain}/new", verify=False)
|
res = requests.post(f"https://{maildomain_sanitized}/new", verify=False)
|
||||||
data = res.json()
|
data = res.json()
|
||||||
rpc.add_or_update_transport(account_id, {
|
rpc.add_or_update_transport(account_id, {
|
||||||
"addr": data["email"],
|
"addr": data["email"],
|
||||||
"password": data["password"],
|
"password": data["password"],
|
||||||
"imapServer": maildomain,
|
"imapServer": maildomain_sanitized,
|
||||||
"smtpServer": maildomain,
|
"smtpServer": maildomain_sanitized,
|
||||||
"certificateChecks": "acceptInvalidCertificates",
|
"certificateChecks": "acceptInvalidCertificates",
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ class TestSSHExecutor:
|
|||||||
assert out == out2
|
assert out == out2
|
||||||
|
|
||||||
def test_perform_initial(self, sshexec, maildomain):
|
def test_perform_initial(self, sshexec, maildomain):
|
||||||
|
if "[" in maildomain:
|
||||||
|
pytest.skip("Relay doesn't have a domain")
|
||||||
res = sshexec(
|
res = sshexec(
|
||||||
remote.rdns.perform_initial_checks, kwargs=dict(mail_domain=maildomain)
|
remote.rdns.perform_initial_checks, kwargs=dict(mail_domain=maildomain)
|
||||||
)
|
)
|
||||||
@@ -131,7 +133,7 @@ def test_authenticated_from(cmsetup, maildata):
|
|||||||
|
|
||||||
@pytest.mark.parametrize("from_addr", ["fake@example.org", "fake@testrun.org"])
|
@pytest.mark.parametrize("from_addr", ["fake@example.org", "fake@testrun.org"])
|
||||||
def test_reject_missing_dkim(cmsetup, maildata, from_addr):
|
def test_reject_missing_dkim(cmsetup, maildata, from_addr):
|
||||||
domain = cmsetup.maildomain
|
domain = cmsetup.maildomain.strip("[").strip("]")
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
sock.settimeout(10)
|
sock.settimeout(10)
|
||||||
try:
|
try:
|
||||||
@@ -143,7 +145,7 @@ def test_reject_missing_dkim(cmsetup, maildata, from_addr):
|
|||||||
msg = maildata(
|
msg = maildata(
|
||||||
"encrypted.eml", from_addr=from_addr, to_addr=recipient.addr
|
"encrypted.eml", from_addr=from_addr, to_addr=recipient.addr
|
||||||
).as_string()
|
).as_string()
|
||||||
conn = smtplib.SMTP(cmsetup.maildomain, 25, timeout=10)
|
conn = smtplib.SMTP(cmsetup.maildomain.strip("[").strip("]"), 25, timeout=10)
|
||||||
conn.starttls()
|
conn.starttls()
|
||||||
|
|
||||||
with conn as s:
|
with conn as s:
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def imap_mailbox(cmfactory, ssl_context):
|
|||||||
(ac1,) = cmfactory.get_online_accounts(1)
|
(ac1,) = cmfactory.get_online_accounts(1)
|
||||||
user = ac1.get_config("addr")
|
user = ac1.get_config("addr")
|
||||||
password = ac1.get_config("mail_pw")
|
password = ac1.get_config("mail_pw")
|
||||||
host = user.split("@")[1]
|
host = user.split("@")[1].strip("[").strip("]")
|
||||||
mailbox = imap_tools.MailBox(host, ssl_context=ssl_context)
|
mailbox = imap_tools.MailBox(host, ssl_context=ssl_context)
|
||||||
mailbox.login(user, password)
|
mailbox.login(user, password)
|
||||||
mailbox.dc_ac = ac1
|
mailbox.dc_ac = ac1
|
||||||
@@ -178,7 +178,7 @@ def test_hide_senders_ip_address(cmfactory, ssl_context):
|
|||||||
chat.send_text("testing submission header cleanup")
|
chat.send_text("testing submission header cleanup")
|
||||||
user2.wait_for_incoming_msg()
|
user2.wait_for_incoming_msg()
|
||||||
addr = user2.get_config("addr")
|
addr = user2.get_config("addr")
|
||||||
host = addr.split("@")[1]
|
host = addr.split("@")[1].strip("[").strip("]")
|
||||||
pw = user2.get_config("mail_pw")
|
pw = user2.get_config("mail_pw")
|
||||||
mailbox = imap_tools.MailBox(host, ssl_context=ssl_context)
|
mailbox = imap_tools.MailBox(host, ssl_context=ssl_context)
|
||||||
mailbox.login(addr, pw)
|
mailbox.login(addr, pw)
|
||||||
|
|||||||
@@ -61,8 +61,13 @@ def maildomain(chatmail_config):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def sshdomain(maildomain):
|
def maildomain_sanitized(maildomain):
|
||||||
return os.environ.get("CHATMAIL_SSH", maildomain)
|
return maildomain.strip("[").strip("]")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def sshdomain(maildomain_sanitized):
|
||||||
|
return os.environ.get("CHATMAIL_SSH", maildomain_sanitized)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -75,7 +80,7 @@ def maildomain2():
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sshdomain2(maildomain2):
|
def sshdomain2(maildomain2):
|
||||||
return os.environ.get("CHATMAIL_SSH2", maildomain2)
|
return os.environ.get("CHATMAIL_SSH2", maildomain2.strip("[").strip("]"))
|
||||||
|
|
||||||
|
|
||||||
def pytest_report_header():
|
def pytest_report_header():
|
||||||
@@ -176,14 +181,14 @@ def ssl_context(chatmail_config):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def imap(maildomain, ssl_context):
|
def imap(maildomain_sanitized, ssl_context):
|
||||||
return ImapConn(maildomain, ssl_context=ssl_context)
|
return ImapConn(maildomain_sanitized, ssl_context=ssl_context)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def make_imap_connection(maildomain, ssl_context):
|
def make_imap_connection(maildomain_sanitized, ssl_context):
|
||||||
def make_imap_connection():
|
def make_imap_connection():
|
||||||
conn = ImapConn(maildomain, ssl_context=ssl_context)
|
conn = ImapConn(maildomain_sanitized, ssl_context=ssl_context)
|
||||||
conn.connect()
|
conn.connect()
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
@@ -227,14 +232,14 @@ class ImapConn:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def smtp(maildomain, ssl_context):
|
def smtp(maildomain_sanitized, ssl_context):
|
||||||
return SmtpConn(maildomain, ssl_context=ssl_context)
|
return SmtpConn(maildomain_sanitized, ssl_context=ssl_context)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def make_smtp_connection(maildomain, ssl_context):
|
def make_smtp_connection(maildomain_sanitized, ssl_context):
|
||||||
def make_smtp_connection():
|
def make_smtp_connection():
|
||||||
conn = SmtpConn(maildomain, ssl_context=ssl_context)
|
conn = SmtpConn(maildomain_sanitized, ssl_context=ssl_context)
|
||||||
conn.connect()
|
conn.connect()
|
||||||
return conn
|
return conn
|
||||||
|
|
||||||
@@ -321,8 +326,8 @@ class ChatmailACFactory:
|
|||||||
"password": password,
|
"password": password,
|
||||||
# Setting server explicitly skips requesting autoconfig XML,
|
# Setting server explicitly skips requesting autoconfig XML,
|
||||||
# see https://datatracker.ietf.org/doc/draft-ietf-mailmaint-autoconfig/
|
# see https://datatracker.ietf.org/doc/draft-ietf-mailmaint-autoconfig/
|
||||||
"imapServer": domain,
|
"imapServer": domain.strip("[").strip("]"),
|
||||||
"smtpServer": domain,
|
"smtpServer": domain.strip("[").strip("]"),
|
||||||
}
|
}
|
||||||
if self.chatmail_config.tls_cert_mode == "self":
|
if self.chatmail_config.tls_cert_mode == "self":
|
||||||
transport["certificateChecks"] = "acceptInvalidCertificates"
|
transport["certificateChecks"] = "acceptInvalidCertificates"
|
||||||
@@ -402,9 +407,6 @@ class Remote:
|
|||||||
case "@local": command = []
|
case "@local": command = []
|
||||||
case "localhost": command = []
|
case "localhost": command = []
|
||||||
case _: command = ["ssh", f"root@{self.sshdomain}"]
|
case _: command = ["ssh", f"root@{self.sshdomain}"]
|
||||||
docker_container = os.environ.get("CHATMAIL_DOCKER")
|
|
||||||
if docker_container:
|
|
||||||
command += ["docker", "exec", docker_container]
|
|
||||||
[command.append(arg) for arg in getjournal.split()]
|
[command.append(arg) for arg in getjournal.split()]
|
||||||
self.popen = subprocess.Popen(
|
self.popen = subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
@@ -457,7 +459,7 @@ class CMSetup:
|
|||||||
|
|
||||||
class CMUser:
|
class CMUser:
|
||||||
def __init__(self, maildomain, addr, password, ssl_context=None):
|
def __init__(self, maildomain, addr, password, ssl_context=None):
|
||||||
self.maildomain = maildomain
|
self.maildomain = maildomain.strip("[").strip("]")
|
||||||
self.addr = addr
|
self.addr = addr
|
||||||
self.password = password
|
self.password = password
|
||||||
self.ssl_context = ssl_context
|
self.ssl_context = ssl_context
|
||||||
|
|||||||
@@ -60,6 +60,29 @@ def mockdns(request, mockdns_base, mockdns_expected):
|
|||||||
return mockdns_base
|
return mockdns_base
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetDkimEntry:
|
||||||
|
def test_dkim_entry_returns_tuple_on_success(self, mockdns):
|
||||||
|
entry, web_entry = remote.rdns.get_dkim_entry(
|
||||||
|
"some.domain", "", dkim_selector="opendkim"
|
||||||
|
)
|
||||||
|
# May return None,None if openssl not available, but should never crash
|
||||||
|
if entry is not None:
|
||||||
|
assert "opendkim._domainkey.some.domain" in entry
|
||||||
|
assert "opendkim._domainkey.some.domain" in web_entry
|
||||||
|
|
||||||
|
def test_dkim_entry_returns_none_tuple_on_error(self, monkeypatch):
|
||||||
|
"""CalledProcessError must return (None, None), not bare None."""
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
|
def failing_shell(command, fail_ok=False, print=print):
|
||||||
|
raise CalledProcessError(1, command)
|
||||||
|
|
||||||
|
monkeypatch.setattr(remote.rdns, "shell", failing_shell)
|
||||||
|
result = remote.rdns.get_dkim_entry("some.domain", "", dkim_selector="opendkim")
|
||||||
|
assert result == (None, None)
|
||||||
|
assert result[0] is None and result[1] is None
|
||||||
|
|
||||||
|
|
||||||
class TestPerformInitialChecks:
|
class TestPerformInitialChecks:
|
||||||
def test_perform_initial_checks_ok1(self, mockdns, mockdns_expected):
|
def test_perform_initial_checks_ok1(self, mockdns, mockdns_expected):
|
||||||
remote_data = remote.rdns.perform_initial_checks("some.domain")
|
remote_data = remote.rdns.perform_initial_checks("some.domain")
|
||||||
|
|||||||
68
cmdeploy/src/cmdeploy/tests/test_rshell.py
Normal file
68
cmdeploy/src/cmdeploy/tests/test_rshell.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from cmdeploy.remote.rshell import dovecot_recalc_quota
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_normal_output():
|
||||||
|
"""Normal doveadm output returns parsed dict."""
|
||||||
|
normal_output = (
|
||||||
|
"Quota name Type Value Limit %\n"
|
||||||
|
"User quota STORAGE 5 102400 0\n"
|
||||||
|
"User quota MESSAGE 2 - 0\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", return_value=normal_output):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
# shell is called twice (recalc + get), patch returns same for both
|
||||||
|
assert result == {"value": 5, "limit": 102400, "percent": 0}
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_empty_output():
|
||||||
|
"""Empty doveadm output (trailing newline) must not IndexError."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
# quota get returns only empty lines
|
||||||
|
return "\n\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_malformed_output():
|
||||||
|
"""Malformed output with too few columns must not crash."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
# partial line, fewer than 6 parts
|
||||||
|
return "Quota name\nUser quota STORAGE\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_header_only():
|
||||||
|
"""Only header line, no data rows."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
return "Quota name Type Value Limit %\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
@@ -1,266 +0,0 @@
|
|||||||
Docker installation
|
|
||||||
===================
|
|
||||||
|
|
||||||
This section provides instructions for installing a chatmail relay
|
|
||||||
using Docker Compose.
|
|
||||||
|
|
||||||
.. note::
|
|
||||||
|
|
||||||
- Docker support is experimental, CI builds and tests the image automatically, but please report bugs.
|
|
||||||
- The image wraps the cmdeploy process detailed in the :doc:`getting_started` instructions in a Debian-systemd image with r/w access to `/sys/fs`
|
|
||||||
- Currently amd64-only (arm64 should work but is untested).
|
|
||||||
|
|
||||||
|
|
||||||
Setup Preparation
|
|
||||||
-----------------
|
|
||||||
|
|
||||||
We use ``chat.example.org`` as the chatmail domain in the following
|
|
||||||
steps. Please substitute it with your own domain.
|
|
||||||
|
|
||||||
1. Install docker and docker compose v2 (check with `docker compose version`), install, e.g., through
|
|
||||||
- Debian 12 through the `official install instructions <https://docs.docker.com/engine/install/debian/#install-using-the-repository>`_
|
|
||||||
- Debian 13+ with `apt install docker docker-compose`
|
|
||||||
|
|
||||||
If you must use v1 (EOL since 2023), use `docker-compose` in the following and modify the `docker-compose.yaml` to use `privileged: true` instead of `cgroup: host`, though that gives the container full privileges.
|
|
||||||
|
|
||||||
2. Setup the initial DNS records.
|
|
||||||
The following is an example in the familiar BIND zone file format with
|
|
||||||
a TTL of 1 hour (3600 seconds).
|
|
||||||
Please substitute your domain and IP addresses.
|
|
||||||
|
|
||||||
::
|
|
||||||
|
|
||||||
chat.example.org. 3600 IN A 198.51.100.5
|
|
||||||
chat.example.org. 3600 IN AAAA 2001:db8::5
|
|
||||||
www.chat.example.org. 3600 IN CNAME chat.example.org.
|
|
||||||
mta-sts.chat.example.org. 3600 IN CNAME chat.example.org.
|
|
||||||
|
|
||||||
3. Configure kernel parameters on the host, as these can not be set from the container::
|
|
||||||
|
|
||||||
echo "fs.inotify.max_user_instances=65536" | sudo tee -a /etc/sysctl.d/99-inotify.conf
|
|
||||||
echo "fs.inotify.max_user_watches=65536" | sudo tee -a /etc/sysctl.d/99-inotify.conf
|
|
||||||
sudo sysctl --system
|
|
||||||
|
|
||||||
|
|
||||||
Docker Compose Setup
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
Pre-built images are available from GitHub Container Registry. The
|
|
||||||
``main`` branch and tagged releases are pushed automatically by CI::
|
|
||||||
|
|
||||||
docker pull ghcr.io/chatmail/relay:main # latest main branch
|
|
||||||
docker pull ghcr.io/chatmail/relay:1.2.3 # tagged release
|
|
||||||
|
|
||||||
|
|
||||||
Create service directory
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
Either:
|
|
||||||
|
|
||||||
- Create a service directory, e.g., `/srv/chatmail-relay`::
|
|
||||||
|
|
||||||
mkdir -p /srv/chatmail-relay && cd /srv/chatmail-relay
|
|
||||||
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker-compose.yaml
|
|
||||||
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker-compose.override.yaml.example -O docker-compose.override.yaml
|
|
||||||
|
|
||||||
- or clone the chatmail repo ::
|
|
||||||
|
|
||||||
git clone https://github.com/chatmail/relay
|
|
||||||
cd relay
|
|
||||||
|
|
||||||
|
|
||||||
Customize and start
|
|
||||||
^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
1. Set the fully qualified domain name of the relay::
|
|
||||||
|
|
||||||
echo 'MAIL_DOMAIN=chat.example.org' > .env
|
|
||||||
|
|
||||||
The container generates a ``chatmail.ini`` with defaults from
|
|
||||||
``MAIL_DOMAIN`` on first start. To customize chatmail settings, mount
|
|
||||||
your own ``chatmail.ini`` instead (see `Custom chatmail.ini`_ below).
|
|
||||||
|
|
||||||
2. All local customizations (data paths, extra volumes, config mounts) go in
|
|
||||||
``docker-compose.override.yaml``, which Compose merges automatically with
|
|
||||||
the base file. By default, all data is stored in docker volumes, you will
|
|
||||||
likely want to at least create and configure the mail storage location, but
|
|
||||||
you might also want to configure external TLS certificates there.
|
|
||||||
|
|
||||||
3. Start the container::
|
|
||||||
|
|
||||||
docker compose up -d
|
|
||||||
docker compose logs -f chatmail # view logs, Ctrl+C to exit
|
|
||||||
|
|
||||||
4. After installation is complete, open ``https://chat.example.org`` in
|
|
||||||
your browser.
|
|
||||||
|
|
||||||
Finish install and test
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
You can test the installation with::
|
|
||||||
|
|
||||||
pip install cmping chat.example.org # or
|
|
||||||
uvx cmping chat.example.org # if you use https://docs.astral.sh/uv/
|
|
||||||
|
|
||||||
You should check and extend your DNS records for better interoperability::
|
|
||||||
|
|
||||||
# Show required DNS records
|
|
||||||
docker exec chatmail cmdeploy dns --ssh-host @local
|
|
||||||
|
|
||||||
You can check server status with::
|
|
||||||
|
|
||||||
docker exec chatmail cmdeploy status --ssh-host @local
|
|
||||||
|
|
||||||
You can run some benchmarks (can also run from any machine with cmdeploy installed)::
|
|
||||||
|
|
||||||
docker exec chatmail cmdeploy bench
|
|
||||||
|
|
||||||
You can run the test suite with::
|
|
||||||
|
|
||||||
docker exec chatmail cmdeploy test --ssh-host localhost
|
|
||||||
|
|
||||||
You can look at logs::
|
|
||||||
|
|
||||||
docker exec chatmail journalctl -fu postfix@-
|
|
||||||
|
|
||||||
|
|
||||||
Customization
|
|
||||||
-------------
|
|
||||||
|
|
||||||
Website
|
|
||||||
^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
You can customize the chatmail landing page by mounting a directory with
|
|
||||||
your own website source files.
|
|
||||||
|
|
||||||
1. Create a directory with your custom website source::
|
|
||||||
|
|
||||||
mkdir -p ./custom/www/src
|
|
||||||
nano ./custom/www/src/index.md
|
|
||||||
|
|
||||||
2. Add the volume mount in ``docker-compose.override.yaml``::
|
|
||||||
|
|
||||||
services:
|
|
||||||
chatmail:
|
|
||||||
volumes:
|
|
||||||
- ./custom/www:/opt/chatmail-www
|
|
||||||
|
|
||||||
3. Restart the service::
|
|
||||||
|
|
||||||
docker compose down
|
|
||||||
docker compose up -d
|
|
||||||
|
|
||||||
|
|
||||||
Custom chatmail.ini
|
|
||||||
^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
If you want to go beyond simply setting the ``MAIL_DOMAIN`` in ``.env``, you
|
|
||||||
can use a regular `chatmail.ini` to give you full control.
|
|
||||||
|
|
||||||
1. Extract the generated config from a running container::
|
|
||||||
|
|
||||||
docker cp chatmail:/etc/chatmail/chatmail.ini ./chatmail.ini
|
|
||||||
|
|
||||||
2. Edit ``chatmail.ini`` as needed.
|
|
||||||
|
|
||||||
3. Add the volume mount in ``docker-compose.override.yaml`` ::
|
|
||||||
|
|
||||||
services:
|
|
||||||
chatmail:
|
|
||||||
volumes:
|
|
||||||
- ./chatmail.ini:/etc/chatmail/chatmail.ini
|
|
||||||
|
|
||||||
4. Restart the container, the container skips generating a new one: ::
|
|
||||||
|
|
||||||
docker compose down && docker compose up -d
|
|
||||||
|
|
||||||
|
|
||||||
External TLS certificates
|
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
If TLS certificates are managed outside the container (e.g. by certbot,
|
|
||||||
acmetool, or Traefik on the host), mount them into the container and set
|
|
||||||
``TLS_EXTERNAL_CERT_AND_KEY`` in ``docker-compose.override.yaml``.
|
|
||||||
Changed certificates are picked up automatically via inotify.
|
|
||||||
See the examples in the example override and :ref:`external-tls` in the getting started guide for details.
|
|
||||||
|
|
||||||
|
|
||||||
Migrating from a bare-metal install
|
|
||||||
------------------------------------
|
|
||||||
|
|
||||||
If you have an existing bare-metal chatmail installation and want to
|
|
||||||
switch to Docker:
|
|
||||||
|
|
||||||
1. Stop all existing services::
|
|
||||||
|
|
||||||
systemctl stop postfix dovecot doveauth nginx opendkim unbound \
|
|
||||||
acmetool-redirector filtermail filtermail-incoming chatmail-turn \
|
|
||||||
iroh-relay chatmail-metadata lastlogin mtail
|
|
||||||
systemctl disable postfix dovecot doveauth nginx opendkim unbound \
|
|
||||||
acmetool-redirector filtermail filtermail-incoming chatmail-turn \
|
|
||||||
iroh-relay chatmail-metadata lastlogin mtail
|
|
||||||
|
|
||||||
2. Copy your existing ``chatmail.ini`` and mount it into the container
|
|
||||||
(see `Custom chatmail.ini`_ above)::
|
|
||||||
|
|
||||||
cp /usr/local/lib/chatmaild/chatmail.ini ./chatmail.ini
|
|
||||||
|
|
||||||
3. Copy persistent data into the ``./data/`` subdirectories (for example, as configured in `Customize and start`_) ::
|
|
||||||
|
|
||||||
mkdir -p data/dkim data/certs data/mail
|
|
||||||
|
|
||||||
# DKIM keys
|
|
||||||
cp -a /etc/dkimkeys/* data/dkim/
|
|
||||||
|
|
||||||
# TLS certificates
|
|
||||||
rsync -a /var/lib/acme/ data/certs/
|
|
||||||
|
|
||||||
Note that ownership of dkim and acme is adjusted on container start.
|
|
||||||
|
|
||||||
For the mail directory::
|
|
||||||
|
|
||||||
rsync -a /home/vmail/ data/mail/
|
|
||||||
|
|
||||||
Alternatively, mount ``/home/vmail`` directly by changing the volume
|
|
||||||
in ``docker-compose-override.yaml``::
|
|
||||||
|
|
||||||
- /home/vmail:/home/vmail
|
|
||||||
|
|
||||||
The three ``./data/`` subdirectories cover all persistent state.
|
|
||||||
Everything else is regenerated by the ``configure`` and ``activate``
|
|
||||||
stages on container start.
|
|
||||||
|
|
||||||
Building the image
|
|
||||||
------------------
|
|
||||||
|
|
||||||
Clone the repository and build the Docker image::
|
|
||||||
|
|
||||||
git clone https://github.com/chatmail/relay
|
|
||||||
cd relay
|
|
||||||
docker/build.sh
|
|
||||||
|
|
||||||
The build bakes all binaries, Python packages, and the install stage
|
|
||||||
into the image. After building, only ``docker-compose.yaml`` and a ``.env``
|
|
||||||
with ``MAIL_DOMAIN`` are needed to run the container. The `build.sh` passes the
|
|
||||||
git hash onto the docker build so it can be determined if there has been a
|
|
||||||
change that warrants a redeploy.
|
|
||||||
|
|
||||||
You can transfer a locally built image to your server directly (pigz is parallel `gzip` which can be used instead as well) ::
|
|
||||||
|
|
||||||
docker save chatmail-relay:latest | pigz | ssh chat.example.org 'pigz -d | docker load'
|
|
||||||
|
|
||||||
|
|
||||||
Forcing a full reinstall
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
On container start, only the ``configure`` and ``activate`` stages run by default.
|
|
||||||
|
|
||||||
To force a full reinstall (e.g. after updating the source), either
|
|
||||||
rebuild the image::
|
|
||||||
|
|
||||||
docker compose build chatmail
|
|
||||||
docker compose up -d
|
|
||||||
|
|
||||||
Or override the stages at runtime without rebuilding::
|
|
||||||
|
|
||||||
CMDEPLOY_STAGES="install,configure,activate" docker compose up -d
|
|
||||||
@@ -98,12 +98,6 @@ steps. Please substitute it with your own domain.
|
|||||||
configure at your DNS provider (it can take some time until they are
|
configure at your DNS provider (it can take some time until they are
|
||||||
public).
|
public).
|
||||||
|
|
||||||
Docker installation
|
|
||||||
-------------------
|
|
||||||
|
|
||||||
There is experimental support for running chatmail via Docker Compose.
|
|
||||||
See :doc:`docker` for full setup instructions.
|
|
||||||
|
|
||||||
Other helpful commands
|
Other helpful commands
|
||||||
----------------------
|
----------------------
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ Contributions and feedback welcome through the https://github.com/chatmail/relay
|
|||||||
:maxdepth: 5
|
:maxdepth: 5
|
||||||
|
|
||||||
getting_started
|
getting_started
|
||||||
docker
|
|
||||||
proxy
|
proxy
|
||||||
migrate
|
migrate
|
||||||
overview
|
overview
|
||||||
|
|||||||
@@ -109,10 +109,6 @@ short overview of ``chatmaild`` services:
|
|||||||
is contacted by Dovecot when a user logs in and stores the date of
|
is contacted by Dovecot when a user logs in and stores the date of
|
||||||
the login.
|
the login.
|
||||||
|
|
||||||
- `metrics <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/metrics.py>`_
|
|
||||||
collects some metrics and displays them at
|
|
||||||
``https://example.org/metrics``.
|
|
||||||
|
|
||||||
``www/``
|
``www/``
|
||||||
~~~~~~~~~
|
~~~~~~~~~
|
||||||
|
|
||||||
@@ -142,11 +138,9 @@ Chatmail relay dependency diagram
|
|||||||
nginx-internal --- autoconfig.xml;
|
nginx-internal --- autoconfig.xml;
|
||||||
certs-nginx[("`TLS certs
|
certs-nginx[("`TLS certs
|
||||||
/var/lib/acme`")] --> nginx-internal;
|
/var/lib/acme`")] --> nginx-internal;
|
||||||
systemd-timer --- chatmail-metrics;
|
|
||||||
systemd-timer --- acmetool;
|
systemd-timer --- acmetool;
|
||||||
systemd-timer --- chatmail-expire-daily;
|
systemd-timer --- chatmail-expire-daily;
|
||||||
systemd-timer --- chatmail-fsreport-daily;
|
systemd-timer --- chatmail-fsreport-daily;
|
||||||
chatmail-metrics --- website;
|
|
||||||
acmetool --> certs[("`TLS certs
|
acmetool --> certs[("`TLS certs
|
||||||
/var/lib/acme`")];
|
/var/lib/acme`")];
|
||||||
nginx-external --- |993|dovecot;
|
nginx-external --- |993|dovecot;
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
# Local overrides: copy to docker-compose.override.yaml in the repo root.
|
|
||||||
# Compose automatically merges this with docker-compose.yaml.
|
|
||||||
#
|
|
||||||
# cp docker-compose.override.yaml.example docker-compose.override.yaml
|
|
||||||
#
|
|
||||||
# Volumes are APPENDED to the base file's volumes list, environment and other scalar keys are MERGED by key.
|
|
||||||
services:
|
|
||||||
chatmail:
|
|
||||||
volumes:
|
|
||||||
## Data paths — bind-mount to host directories for easy access/backup.
|
|
||||||
|
|
||||||
# - ./data/dkim:/etc/dkimkeys
|
|
||||||
# - ./data/certs:/var/lib/acme
|
|
||||||
|
|
||||||
# - ./data/mail:/home/vmail
|
|
||||||
## Or mount from an existing bare-metal install.
|
|
||||||
# - /home/vmail:/home/vmail
|
|
||||||
|
|
||||||
## Mount your own chatmail.ini (skips auto-generation):
|
|
||||||
# - ./chatmail.ini:/etc/chatmail/chatmail.ini
|
|
||||||
|
|
||||||
## Custom website:
|
|
||||||
# - ./custom/www:/opt/chatmail-www
|
|
||||||
|
|
||||||
## Debug — mount scripts from the repo for live editing:
|
|
||||||
# - ./docker/chatmail-init.sh:/chatmail-init.sh
|
|
||||||
# - ./docker/entrypoint.sh:/entrypoint.sh
|
|
||||||
|
|
||||||
# environment:
|
|
||||||
## Mount certs (above) and set TLS_EXTERNAL_CERT_AND_KEY to in-container paths.
|
|
||||||
## A tls-cert-reload.path watcher inside the container reloads services
|
|
||||||
## when the cert file changes. However, inotify does not cross bind-mount
|
|
||||||
## boundaries, so host-side renewals (certbot, acmetool, etc.) must
|
|
||||||
## notify the container explicitly. Add this to your renewal hook:
|
|
||||||
##
|
|
||||||
## docker exec chatmail systemctl start tls-cert-reload.service
|
|
||||||
##
|
|
||||||
## Host acmetool (bare-metal migration): create mount above, and
|
|
||||||
## rsync -a /var/lib/acme/live data/certs
|
|
||||||
# TLS_EXTERNAL_CERT_AND_KEY: "/var/lib/acme/live/${MAIL_DOMAIN}/fullchain /var/lib/acme/live/${MAIL_DOMAIN}/privkey"
|
|
||||||
##
|
|
||||||
## (Untested) Traefik certs-dumper (see docker/docker-compose-traefik.yaml) - also add volume:
|
|
||||||
## - traefik-certs:/certs:ro
|
|
||||||
# TLS_EXTERNAL_CERT_AND_KEY: "/certs/${MAIL_DOMAIN}/certificate.crt /certs/${MAIL_DOMAIN}/privatekey.key"
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
# Base compose file — do not edit. Put customizations (data paths, extra
|
|
||||||
# volumes, env overrides) in docker-compose.override.yaml instead.
|
|
||||||
# See docker/docker-compose.override.yaml.example for a starting point.
|
|
||||||
#
|
|
||||||
# Security notes: this container uses
|
|
||||||
# - network_mode:host chatmail needs many ports (25, 53, 80, 143, 443, 465,
|
|
||||||
# 587, 993, 3340, 8443) and needs to operate from the real IP, which bridging
|
|
||||||
# would make tricky
|
|
||||||
# - cgroup:host (required for systemd).
|
|
||||||
# Together these give the container near-host-level access. This is acceptable
|
|
||||||
# for a dedicated mail server, but be aware that the container can bind any
|
|
||||||
# port and see all host network traffic.
|
|
||||||
|
|
||||||
services:
|
|
||||||
chatmail:
|
|
||||||
build:
|
|
||||||
context: ./
|
|
||||||
dockerfile: docker/chatmail_relay.dockerfile
|
|
||||||
args:
|
|
||||||
GIT_HASH: ${GIT_HASH:-unknown}
|
|
||||||
image: chatmail-relay:latest
|
|
||||||
restart: unless-stopped
|
|
||||||
container_name: chatmail
|
|
||||||
# Required for systemd — use only one of the following:
|
|
||||||
cgroup: host # compose v2
|
|
||||||
# privileged: true # compose v1 (less restricted)
|
|
||||||
tty: true # required for logs
|
|
||||||
tmpfs: # required for systemd
|
|
||||||
- /tmp
|
|
||||||
- /run
|
|
||||||
- /run/lock
|
|
||||||
logging:
|
|
||||||
driver: none
|
|
||||||
environment:
|
|
||||||
MAIL_DOMAIN: $MAIL_DOMAIN
|
|
||||||
network_mode: "host"
|
|
||||||
volumes:
|
|
||||||
## system (required)
|
|
||||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
|
||||||
## data (defaults — override in docker-compose.override.yaml)
|
|
||||||
- mail:/home/vmail
|
|
||||||
- dkim:/etc/dkimkeys
|
|
||||||
- certs:/var/lib/acme
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
mail:
|
|
||||||
dkim:
|
|
||||||
certs:
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# Build the chatmail Docker image with the current git hash baked in.
|
|
||||||
# Usage: ./docker/build.sh [extra docker-compose build args...]
|
|
||||||
#
|
|
||||||
# .git/ is excluded from the build context (.dockerignore) so the hash
|
|
||||||
# must be passed as a build arg from the host.
|
|
||||||
|
|
||||||
export GIT_HASH=$(git rev-parse HEAD)
|
|
||||||
exec docker compose build "$@"
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Run container setup commands
|
|
||||||
After=multi-user.target
|
|
||||||
ConditionPathExists=/chatmail-init.sh
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=oneshot
|
|
||||||
ExecStart=/bin/bash /chatmail-init.sh
|
|
||||||
RemainAfterExit=true
|
|
||||||
WorkingDirectory=/opt/chatmail
|
|
||||||
PassEnvironment=<envs_list>
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
set -euo pipefail
|
|
||||||
export CHATMAIL_INI="${CHATMAIL_INI:-/etc/chatmail/chatmail.ini}"
|
|
||||||
export CHATMAIL_NOSYSCTL=True
|
|
||||||
export CHATMAIL_NOPORTCHECK=True
|
|
||||||
|
|
||||||
CMDEPLOY=/opt/cmdeploy/bin/cmdeploy
|
|
||||||
|
|
||||||
if [ -z "$MAIL_DOMAIN" ]; then
|
|
||||||
echo "ERROR: Environment variable 'MAIL_DOMAIN' must be set!" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Generate DKIM keys if not mounted
|
|
||||||
if [ ! -f /etc/dkimkeys/opendkim.private ]; then
|
|
||||||
/usr/sbin/opendkim-genkey -D /etc/dkimkeys -d "$MAIL_DOMAIN" -s opendkim
|
|
||||||
fi
|
|
||||||
# Fix ownership for bind-mounted keys (host opendkim UID may differ from container)
|
|
||||||
chown -R opendkim:opendkim /etc/dkimkeys
|
|
||||||
|
|
||||||
# Create chatmail.ini, skip if mounted
|
|
||||||
mkdir -p "$(dirname "$CHATMAIL_INI")"
|
|
||||||
if [ ! -f "$CHATMAIL_INI" ]; then
|
|
||||||
$CMDEPLOY init --config "$CHATMAIL_INI" "$MAIL_DOMAIN"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Auto-detect IPv6: if the host has no IPv6 connectivity, set disable_ipv6
|
|
||||||
# in the ini so dovecot/postfix/nginx bind to IPv4 only.
|
|
||||||
# Uses network_mode:host so /proc/net/if_inet6 reflects the host's stack.
|
|
||||||
if [ ! -e /proc/net/if_inet6 ]; then
|
|
||||||
if grep -q '^disable_ipv6 = False' "$CHATMAIL_INI"; then
|
|
||||||
sed -i 's/^disable_ipv6 = False/disable_ipv6 = True/' "$CHATMAIL_INI"
|
|
||||||
echo "[INFO] IPv6 not available, set disable_ipv6 = True"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Inject external TLS paths from env var unless defined in chatmail.ini
|
|
||||||
if [ -n "${TLS_EXTERNAL_CERT_AND_KEY:-}" ]; then
|
|
||||||
if ! grep -q '^tls_external_cert_and_key' "$CHATMAIL_INI"; then
|
|
||||||
echo "tls_external_cert_and_key = $TLS_EXTERNAL_CERT_AND_KEY" >> "$CHATMAIL_INI"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Ensure mailboxes directory exists (chatmail-metadata needs it at startup,
|
|
||||||
# but Dovecot only creates it on first mail delivery)
|
|
||||||
mkdir -p "/home/vmail/mail/${MAIL_DOMAIN}"
|
|
||||||
chown vmail:vmail "/home/vmail/mail/${MAIL_DOMAIN}"
|
|
||||||
|
|
||||||
# --- Deploy fingerprint: skip cmdeploy run if nothing changed ---
|
|
||||||
# On restart with identical image+config, systemd already brings up all
|
|
||||||
# enabled services only configure+activate are needed here.
|
|
||||||
IMAGE_VERSION_FILE="/etc/chatmail-image-version"
|
|
||||||
FINGERPRINT_FILE="/etc/chatmail/.deploy-fingerprint"
|
|
||||||
image_ver="none"
|
|
||||||
[ -f "$IMAGE_VERSION_FILE" ] && image_ver=$(cat "$IMAGE_VERSION_FILE")
|
|
||||||
config_hash=$(sha256sum "$CHATMAIL_INI" | cut -c1-16)
|
|
||||||
current_fp="${image_ver}:${config_hash}"
|
|
||||||
|
|
||||||
# CMDEPLOY_STAGES non-empty in env = operator override -> always run.
|
|
||||||
# Otherwise, if fingerprint matches the last successful deploy, skip.
|
|
||||||
if [ -z "${CMDEPLOY_STAGES:-}" ] \
|
|
||||||
&& [ -f "$FINGERPRINT_FILE" ] \
|
|
||||||
&& [ "$(cat "$FINGERPRINT_FILE")" = "$current_fp" ]; then
|
|
||||||
echo "[INFO] No changes detected ($current_fp), skipping deploy."
|
|
||||||
else
|
|
||||||
export CMDEPLOY_STAGES="${CMDEPLOY_STAGES:-configure,activate}"
|
|
||||||
|
|
||||||
# Skip DNS check when MAIL_DOMAIN is a bare IP address
|
|
||||||
SKIP_DNS=""
|
|
||||||
if [[ "$MAIL_DOMAIN" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]] || [[ "$MAIL_DOMAIN" =~ : ]]; then
|
|
||||||
SKIP_DNS="--skip-dns-check"
|
|
||||||
fi
|
|
||||||
$CMDEPLOY run --config "$CHATMAIL_INI" --ssh-host @local $SKIP_DNS
|
|
||||||
|
|
||||||
# Restore the build-time hash
|
|
||||||
cp /etc/chatmail-image-version /etc/chatmail-version
|
|
||||||
echo "$current_fp" > "$FINGERPRINT_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Signal success to Docker healthcheck
|
|
||||||
touch /run/chatmail-init.done
|
|
||||||
|
|
||||||
# Forward journald to console so `docker compose logs` works
|
|
||||||
grep -q '^ForwardToConsole=yes' /etc/systemd/journald.conf \
|
|
||||||
|| echo "ForwardToConsole=yes" >> /etc/systemd/journald.conf
|
|
||||||
systemctl restart systemd-journald
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
# syntax=docker/dockerfile:1
|
|
||||||
FROM jrei/systemd-debian:12 AS base
|
|
||||||
|
|
||||||
ENV LANG=en_US.UTF-8
|
|
||||||
|
|
||||||
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
|
|
||||||
--mount=type=cache,target=/var/lib/apt/lists,sharing=locked \
|
|
||||||
echo 'APT::Install-Recommends "0";' > /etc/apt/apt.conf.d/01norecommend && \
|
|
||||||
echo 'APT::Install-Suggests "0";' >> /etc/apt/apt.conf.d/01norecommend && \
|
|
||||||
apt-get update && \
|
|
||||||
DEBIAN_FRONTEND=noninteractive TZ=UTC \
|
|
||||||
apt-get install -y \
|
|
||||||
ca-certificates \
|
|
||||||
gcc \
|
|
||||||
git \
|
|
||||||
python3 \
|
|
||||||
python3-dev \
|
|
||||||
python3-venv \
|
|
||||||
tzdata \
|
|
||||||
locales && \
|
|
||||||
sed -i -e "s/# $LANG.*/$LANG UTF-8/" /etc/locale.gen && \
|
|
||||||
dpkg-reconfigure --frontend=noninteractive locales && \
|
|
||||||
update-locale LANG=$LANG
|
|
||||||
|
|
||||||
# --- Build-time: install cmdeploy venv and run install stage ---
|
|
||||||
# Editable install so importlib.resources reads directly from the source tree.
|
|
||||||
# On container start only "configure,activate" stages run.
|
|
||||||
|
|
||||||
# Copy dependency metadata first so pip install layer is cached
|
|
||||||
COPY cmdeploy/pyproject.toml /opt/chatmail/cmdeploy/pyproject.toml
|
|
||||||
COPY chatmaild/pyproject.toml /opt/chatmail/chatmaild/pyproject.toml
|
|
||||||
|
|
||||||
# Dummy scaffolding so editable install can discover packages
|
|
||||||
RUN mkdir -p /opt/chatmail/cmdeploy/src/cmdeploy \
|
|
||||||
/opt/chatmail/chatmaild/src/chatmaild && \
|
|
||||||
touch /opt/chatmail/cmdeploy/src/cmdeploy/__init__.py \
|
|
||||||
/opt/chatmail/chatmaild/src/chatmaild/__init__.py
|
|
||||||
|
|
||||||
# Dummy git repo: .git/ is excluded from the build context (.dockerignore)
|
|
||||||
# but setuptools calls `git ls-files` when building the sdist.
|
|
||||||
WORKDIR /opt/chatmail
|
|
||||||
RUN --mount=type=cache,target=/root/.cache/pip \
|
|
||||||
git init -q && \
|
|
||||||
python3 -m venv /opt/cmdeploy && \
|
|
||||||
/opt/cmdeploy/bin/pip install -e chatmaild/ -e cmdeploy/
|
|
||||||
|
|
||||||
# Full source copy (editable install's .egg-link still points here)
|
|
||||||
COPY . /opt/chatmail/
|
|
||||||
|
|
||||||
# Minimal chatmail.ini
|
|
||||||
RUN printf '[params]\nmail_domain = build.local\n' > /tmp/chatmail.ini
|
|
||||||
|
|
||||||
RUN CMDEPLOY_STAGES=install \
|
|
||||||
CHATMAIL_INI=/tmp/chatmail.ini \
|
|
||||||
CHATMAIL_NOSYSCTL=True \
|
|
||||||
CHATMAIL_NOPORTCHECK=True \
|
|
||||||
/opt/cmdeploy/bin/pyinfra @local \
|
|
||||||
/opt/chatmail/cmdeploy/src/cmdeploy/run.py -y
|
|
||||||
|
|
||||||
RUN cp -a www/ /opt/chatmail-www/
|
|
||||||
|
|
||||||
# Remove build-only packages and their deps — not needed at runtime
|
|
||||||
RUN apt-get purge -y gcc git python3-dev && \
|
|
||||||
apt-get autoremove -y && \
|
|
||||||
rm -f /tmp/chatmail.ini
|
|
||||||
|
|
||||||
# Record image version (used in deploy fingerprint at runtime).
|
|
||||||
# GIT_HASH is passed as a build arg (from docker-compose or CI) so that
|
|
||||||
# .git/ can be excluded from the build context via .dockerignore.
|
|
||||||
# Two files: chatmail-image-version is the immutable build hash (survives
|
|
||||||
# deploys); chatmail-version is overwritten by cmdeploy run and restored
|
|
||||||
# from the image version after each deploy in chatmail-init.sh.
|
|
||||||
ARG GIT_HASH=unknown
|
|
||||||
RUN echo "$GIT_HASH" > /etc/chatmail-image-version && \
|
|
||||||
echo "$GIT_HASH" > /etc/chatmail-version
|
|
||||||
# --- End build-time install ---
|
|
||||||
|
|
||||||
ENV TZ=:/etc/localtime
|
|
||||||
ENV PATH="/opt/cmdeploy/bin:${PATH}"
|
|
||||||
RUN ln -s /etc/chatmail/chatmail.ini /opt/chatmail/chatmail.ini
|
|
||||||
|
|
||||||
ARG CHATMAIL_INIT_SERVICE_PATH=/lib/systemd/system/chatmail-init.service
|
|
||||||
COPY ./docker/chatmail-init.service "$CHATMAIL_INIT_SERVICE_PATH"
|
|
||||||
RUN ln -sf "$CHATMAIL_INIT_SERVICE_PATH" "/etc/systemd/system/multi-user.target.wants/chatmail-init.service"
|
|
||||||
|
|
||||||
# Remove default nginx site config at build time (not in entrypoint)
|
|
||||||
RUN rm -f /etc/nginx/sites-enabled/default
|
|
||||||
|
|
||||||
COPY --chmod=555 ./docker/chatmail-init.sh /chatmail-init.sh
|
|
||||||
COPY --chmod=555 ./docker/entrypoint.sh /entrypoint.sh
|
|
||||||
COPY --chmod=555 ./docker/healthcheck.sh /healthcheck.sh
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=10s --start-period=180s --timeout=10s --retries=3 \
|
|
||||||
CMD /healthcheck.sh
|
|
||||||
|
|
||||||
STOPSIGNAL SIGRTMIN+3
|
|
||||||
|
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
|
||||||
|
|
||||||
CMD [ "--default-standard-output=journal+console", \
|
|
||||||
"--default-standard-error=journal+console" ]
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
# Used by .github/workflows/docker-ci.yaml
|
|
||||||
# The GHCR image is set via CHATMAIL_IMAGE env var at deploy time.
|
|
||||||
services:
|
|
||||||
chatmail:
|
|
||||||
image: ${CHATMAIL_IMAGE:-chatmail-relay:latest}
|
|
||||||
volumes:
|
|
||||||
- /srv/chatmail/chatmail.ini:/etc/chatmail/chatmail.ini
|
|
||||||
- /srv/chatmail/dkim:/etc/dkimkeys
|
|
||||||
- /srv/chatmail/certs:/var/lib/acme
|
|
||||||
environment:
|
|
||||||
TLS_EXTERNAL_CERT_AND_KEY: /var/lib/acme/live/${MAIL_DOMAIN}/fullchain /var/lib/acme/live/${MAIL_DOMAIN}/privkey
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -eo pipefail
|
|
||||||
|
|
||||||
CHATMAIL_INIT_SERVICE_PATH="${CHATMAIL_INIT_SERVICE_PATH:-/lib/systemd/system/chatmail-init.service}"
|
|
||||||
|
|
||||||
env_vars="MAIL_DOMAIN CMDEPLOY_STAGES CHATMAIL_INI TLS_EXTERNAL_CERT_AND_KEY PATH"
|
|
||||||
sed -i "s|<envs_list>|$env_vars|g" "$CHATMAIL_INIT_SERVICE_PATH"
|
|
||||||
|
|
||||||
exec /lib/systemd/systemd "$@"
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# returns 0 when chatmail-init succeeded and all expected services are running.
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
test -f /run/chatmail-init.done
|
|
||||||
|
|
||||||
# Core services
|
|
||||||
services="chatmail-metadata doveauth dovecot filtermail filtermail-incoming nginx postfix unbound"
|
|
||||||
|
|
||||||
# Optional services
|
|
||||||
for svc in iroh-relay turnserver; do
|
|
||||||
systemctl is-enabled "$svc" 2>/dev/null && services="$services $svc"
|
|
||||||
done
|
|
||||||
|
|
||||||
exec systemctl is-active $services
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
MAIL_DOMAIN=chat.example.com
|
|
||||||
Reference in New Issue
Block a user