docker-mailserver icon indicating copy to clipboard operation
docker-mailserver copied to clipboard

How to correctly mount traefiks letsencrypt certificates from named volume?

Open dhax opened this issue 6 years ago • 7 comments

The recommended way for using Traefik with letsencrypt is to use a named volume to save the certificates. This makes it currently impossible to mount these certificates in the way described in the documentation for docker-mailserver, i.e. one cannot use a subdirectory or file only to mount like this:

volumes:
    - /etc/ssl/acme-v2.json:/etc/letsencrypt/acme.json:ro

If I try to mount them as read-only mailserver complains about not being able to access them. The only way I got it to work was to mount the whole letsencrypt directory and not restricting it to read-only like this:

volumes:
    - traefik-certificates:/etc/letsencrypt

This seems to work fine so far. My question basically is if this could have any negative effects on security or risk losing the certificates because of docker-mailserver messing with them.

Thanks!

dhax avatar Nov 12 '20 18:11 dhax

Could you point me to this wiki entry?


So you have tried

volumes:
    - traefik-certificates:/etc/letsencrypt:ro

? And you are sure you haven't misspelled anything?


I personally would advise against mounting this certificate file with write permissions. In theory, docker-mailserver is just reading this file, but you never know. I use Traefik v2 too, but without named volumes, so I can mount the acme.json directly and as described. If this is an option for you, do it this way. That's why I'm curious what the wiki entry says, since you're right: File-based mounting with named volumes is not possible (as far as I know).

Furthermore, using a named volume for the certificate file is a bad idea in so far as you might accidentally delete this file when Traefik is not running.

georglauterbach avatar Nov 12 '20 19:11 georglauterbach

I am running traefik and mailserver using docker-compose with docker-machine deploying on a remote host. Not sure about the risk to accidentally delete the named volume, also in this case the volume would just be recreated and new certs requested again when traefik restarts. I could also mount a local folder on the host, but as I went the other route and struggled to get it working and finally was wondering about negative side effects. Hope this helps others trying to do the same.

Anyhow, what I noticed now is the following behavior when creating traefik, the named volume and mailserver for the very first time on a newly provisioned docker host:

  1. Start Traefik service, the named volume get created.
  2. Start tls/letsencrypt configured whoami service as workaround to obtain the cert for the mailserver domain, cert gets successfully retrieved. TLS is enabled in Traefik dashboard for mail.myrealdomain.org.
  3. Stop and remove whoami server.
  4. Start docker-mailserver first time with read-only access as in volumes: - traefik-certificates:/etc/letsencrypt:ro This gives me the following errors:
Configuring mail server
mail       | mkdir: cannot create directory '/etc/letsencrypt/live': Read-only file system
mail       | /usr/local/bin/helper-functions.sh: line 84: /etc/letsencrypt/live/mail.myrealdomain.org/key.pem: No such file or directory
mail       |  Error Cannot access '/etc/letsencrypt/live/mail.myrealdomain.org/fullchain.pem' or '/etc/letsencrypt/live/myrealdomain.org/fullchain.pem'
mail       | Starting Misc
mail       | ...
  1. Remove read-only from traefik-certificates mount, recreate mailserver works without any error:
mail       | Configuring mail server
mail       | Cert found in /etc/letsencrypt/acme.json for mail.myrealdomain.org
mail       | Starting Misc
  1. Add read-only again to the mount and recreate mailserver again shows this message:
mail       | Configuring mail server
mail       | /usr/local/bin/helper-functions.sh: line 84: /etc/letsencrypt/live/mail.myrealdomain.org/key.pem: Read-only file system
mail       | Starting Misc

Beside that last message by helper-function.sh everything seems to work and checktls.com says all is OK.

So the point here seems if you really need or want to use named volumes you have to mount the volume with write access as it tries to create the 'live/mail.myrealdomain.org' folder within the mounted letsencrypt folder.

Not sure why it does that if only needs to read the acme.json file already present in the mounted letsencrypt folder. Or is it required by mailserver to create the 'live/mail.myrealdomain.org' containing the fullchain.pem and key.pem files? Creating these folders and files is no problem if mounted like in the wiki example, i.e. only mounting the acme.json file so that mailserver has full write access to the container's local /etc/letsencrypt folder. Thanks for clarifying.

dhax avatar Nov 12 '20 21:11 dhax

I've had a look at the start-mailserver.sh script and the corresponding helper-functions.sh, and it makes perfect sense for the scripts to write to the volume. As the acme.json cannot be natively used by the mailserver, the certificates are extracted and written to the location , e.g. /etc/letsencrypt/live/mail.myrealdomain.org/key.pem. Therefore, the :ro mount does not work.

This becomes critical when Traefik issues a new certificate, which cannot be written when the acme.json is updated when using the named volume with read-only permissions. In theory, there shouldn't be any writes to the acme.json, so mounting it with a named volume without :ro seems to be fine, but I'm not a fan of it. If you can, use a local volume for the certificate files and mount the file directly.

I will adjust the wiki page accordingly. Thank you for noticing this!

georglauterbach avatar Nov 13 '20 10:11 georglauterbach

I just had a look at the wiki, but the mount there is not a named volume:

volumes:
  - /etc/ssl/acme-v2.json:/acme.json

Where are you getting the traefik-certificates in

volumes:
    - traefik-certificates:/etc/letsencrypt

from?

Can't you just mount

volumes:
   /etc/ssl/acme-v2.json:/etc/letsencrypt/acme.json:ro

?

georglauterbach avatar Nov 13 '20 10:11 georglauterbach

Hi, thanks for confirming that mailserver needs to extract and write the certificates to its local live folder. The idea to mount a named volume is not in the mailserver wiki, it's just another docker option when setting up traefik and want to use a named volume for whatever reason. When I set up Traefik first I wasn't thinking about or even reading the mailserver wiki, so I got into this situation later...

So the problem with this named volume setup is that when traefik renews the certificates, mailserver can't renew the extracted version in the live folder without removing the read-only on the mount and recreating the container again. Also this live folder is shared with traefik, so both could try to manipulate files here simultaneously, which is definitely not a good idea.

So for this reason I guess for now the only valid and future proof setup regarding certificates is to mount it like described now in the mailserver wiki for the traefik setup, i.e. a volume on the host and only mounting the acme.json in read-only mode.

Thanks again for the fast response and this great product in general!

dhax avatar Nov 13 '20 10:11 dhax

I just encountered the exact same issue when trying the exact same setup as @dhax

I prefer named volumes too. Too bad I have to hardcode the paths to my letsencrypt directory instead of using the named volume I already have.

But is it really an issue of named volumes or is the issue that mounting the file acme.json:ro works and mounting the directory letsencrypt:ro doesn't?

Anyway, maybe a link to this issue could be added to the following section of the documentation? https://docker-mailserver.github.io/docker-mailserver/edge/config/security/ssl/#traefik-v2

3id0 avatar Dec 15 '21 23:12 3id0

There were recent changes to the handling of LE certificates, but I think we still need to folder to be read-write - and for good reason. It basically comes down to missing write permissions if you mount the named volume (a directory in this case) :ro whereas mounting it :wr should in theory work - but that would be bad practice as it allows the mail server to touch the certificate.

Anyway, maybe a link to this issue could be added to the following section of the documentation?

Feel free to open a PR.

georglauterbach avatar Dec 16 '21 11:12 georglauterbach