Rework `caddy trust` to use the admin API for fetching the root cert
Iām trying to script setup and configuration of caddy server based on a custom download that includes additional plugins (caddy-auth-portal, caddy-auth-jwt, caddy-trace, and various caddy-dns modules ).
During setup, the caddy unit file is configured to run caddy as a non priveledged user (by design).
To get certificates configured properly we are attempting to use the caddy trust command as root during install/config. I think this is performing the trust install based on the default caddy CA location created upon install, rather than the custom storage file_system root declaration Iām passing in my /etc/caddy/Caddyfile (provided below)
The result is on first startup from systemctl is throwing errors indicating it can not import the root ca because the service is not running as a priveledged user.
What would be nice is to be able to run caddy trust as root, indicating the custom location for all local CA certificate files.
Here is an example of the preamble to the Caddyfile configuration I use to expose the certificate deployment for shared use by our combined components on the processing node. In this scenario letsEncrypt/ZeroTLS is not being used...
{ storage file_system { root /opt/caddy/storage } local_certs http_port 80 https_port 443 }
The rest of our deployment detail is in forum issue https://caddy.community/t/new-feature-caddy-trust-with-support-for-config-switch/11606
systemd file, note we use update-alternatives to map /usr/bin/caddy to the custom arm7 with plugins we pull and lay down in our /opt/caddy/release/ path...
# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# caddy run command or use the caddy-api.service file instead.
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
I'd like to work on this.
I did some thinking about this, and I think that the right solution here is to instead change how caddy trust gets the CA certs, to get them from Caddy's API rather than directly from storage.
Currently, caddy trust uses https://caddyserver.com/docs/conventions#file-locations to find the storage directory, which means if you run sudo caddy trust, it actually uses the root user's HOME environment variable to find the storage location. This is wrong, because if you're running Caddy as a systemd service, the storage would actually be in the HOME of the caddy user.
The workaround for this is to run sudo HOME=~caddy caddy trust, but that's really awkward, and can fail to work correctly in certain situations (like potentially writing the CA certs to storage being owned by the root user, if the certs didn't already exist in the caddy user's storage).
So the solution I suggest is this:
-
Implement a new Admin API endpoint, maybe
GET /pki/certificates/<id>, which would respond the root and intermediate certificates for the given CA ID (see https://caddyserver.com/docs/json/apps/pki/), and maybe some other non-secret metadata. If thelocalCA ID is requested (and onlylocal, not other CA IDs) then it should initialize the local CA (becauselocalis the one that Caddy may implicitly set up if the config has something likelocalhostin it, but other CA IDs should have been explicitly set up in thepkiapp ahead of time). -
Rework
caddy trustto instead make a call to the admin API to fetch the root certificate (this would work very similarly tocaddy reloadorcaddy stopin how it makes a call to the API) then attempts to install trust. Users would typically run it withsudo caddy trustif their user is not a sudoer. The command might look like:caddy trust [--ca <id>] [--config <path>] [--adapter <adapter>]So
<id>would default tolocalif not specified, and the config/adapter is only necessary to use a different admin API endpoint, if the default oflocalhost:2019is not used. -
Consider some changes to
caddy untrustto match. I thinkcaddy untrust --cert <path>is kinda strange of an API, because I'm not sure the user would actually know the path to the certificate they want to untrust, typically. Would probably be more useful to fetch the root cert to untrust from the API in this case as well withcaddy untrust --ca <id>. But I can see it still being useful as an escape hatch to use--pathif Caddy is no longer running, or storage was reset, to get rid of an old root from trust.
If the local CA ID is requested (and only local, not other CA IDs) then it should initialize the local CA (because local is the one that Caddy may implicitly set up if the config has something like localhost in it, but other CA IDs should have been explicitly set up in the pki app ahead of time).
That scares me a little bit given that it's a GET request. I can see why this would be handy but it does make me nervous to be changing state with a GET.
and the config/adapter is only necessary to use a different admin API endpoint,
Is an entire config necessary just for the admin endpoint? The caddy stop command simply has an --address flag that seems to work fine.
Otherwise, the proposal sounds like a good approach.
That scares me a little bit given that it's a GET request. I can see why this would be handy but it does make me nervous to be changing state with a GET.
The current caddy trust command does have that behaviour, so I figured we should keep that. I think it's pretty harmless to provision the CA since it's already implicitly done via other means anyways. Just saves from some speedbumps.
Is an entire config necessary just for the admin endpoint? The caddy stop command simply has an --address flag that seems to work fine.
No, but I would say it's more convenient to point to the config as a source of truth for the admin address. Could have both.
Not sure if this is the proper thread to comment about this, but I've stumbled in a minor nitpick regarding self-signed certificates and trust:
Running Caddy as a systemd service (using the official one from the deb package) in a headless raspberry test server in my LAN, accessing it from my desktop via SSH and browsing via https://raspi hostname. Being a non-public hostname I figured I had to setup a self-signed "snakeoil" certificate in /etc/caddy/Caddyfile: raspi {... tls internal ...}.
So far, so great: it creates the internal CA, publishes the website with a cert, I connect from my desktop Firefox, add the self-signed cert as trusted, done. Website works and I have HTTPS in my LAN
But, back in the server, the nitpicks start:
- Logs show PAM access denied errors as
caddy(unpriviledged) user tries to trust the cert locally using various methods:caddy : user NOT in sudoers ; ... USER=root ; COMMAND=/usr/bin/tee /usr/local/share/ca-certificates ...- This along with a warning about
"certutil" is not available, install "certutil"and a message aboutdefine JAVA_HOME environment variable to use the Java trust. Thanks, but nojavahere.
- This along with a warning about
- As local trust is somewhat useless in a headless server, I disable it in the global block:
{... skip_install_trust ...} - Errors above are fixed, but now I have new warning:
"root certificate trust store installation disabled; unconfigured clients may show warnings". Yes, I know. But this is a server, so no local clients to configure. And remote clients such as my desktop Firefox are already configured. - Seems there's no way to silence that warning but to concede and install a pointless trust. And more errors until you use the awkward
sudo HOME=~caddy caddy trustcommand @francislavoie mentioned.
Given this scenario:
- Shouldn't
skip_install_trustbe the default when running assystemdservice? This implies it is a server, so trusting should be made by (remote) clients, not the local machine. - The "trust check" (and its warning) should skipped/silenced when one explicitly asks to skip the trust install! Or at least have a way to bypass that. Perhaps
skip_trust_check_yes_im_aware_of_unconfigured_clients_thanks?
Whatever the new trust workflow you guys implement, please consider the above and provide a way to smoothen the experience when running as a systemd service, and allow usage without warnings when not installing trust.
PS: Is this the proper issue to discuss this? Feel free to point me to other threads
PS: Is this the proper issue to discuss this? Feel free to point me to other threads
Here is correct! š
Errors above are fixed, but now I have new warning:
In general, warnings are not errors. They're warnings. You can safely ignore them if you find they don't apply to you.
It's not a goal to be able to quiet all warnings, because they actually provide information. The "trust store installation disabled" warning comes with the root cert path attached, which is handy if you forget where Caddy actually stores it. It just gives you a soft reminder that you might have something to do in case you forget.
Shouldn't
skip_install_trustbe the default when running as systemd service?
Caddy itself doesn't really "know" that it's running as a systemd service. It would have to make guesses based on environment, and that adds weird implicit behaviour that I'd rather not have to deal with, based on how Caddy is run. As I said before though, it's essentially harmless to try to install, because it just simply won't have the permissions to and will fail.
In theory, we could have the apt repo installation run caddy trust, but... having an installer install a root cert to your trust store is pretty sketchy without somekind of opt-in. We need the user to configure something that would imply they want it before it being actually okay to try it.
@MestreLion - Sharing a bit on how we set up with an install script that "handles" the trust step for us, @francislavoie shared this approach HOME=~caddy caddy trust in a forum post as well that helped get having trust set up after we install our customized caddy instance that was downloaded from the caddyserver website. These are excerpts from a setup script that is kicked of with sudo...
groupadd caddy
useradd --system --gid caddy --create-home --home-dir /var/lib/caddy -c "Caddy web server" --shell /bin/bash caddy
# we do a bunch of filesystem setup for caddy-auth-portal as well as copy
# our customized caddy binary into a releases path we create
# and the use update-alternatives to map it
update-alternatives --install /usr/bin/caddy caddy /opt/caddy/release/caddy_linux_arm7_custom 20
# at this point we copy in the proper systemd (caddy-api) unit file
#from caddy docs and prep system to use it for systemctl management
# then we bring in our stock "Caddyfile" and load it via api using caddy adapt
# because we have to customize the pki block via API to create a unique root CA
# per deployment, and then finally once everything is set up we start caddy
# then stop caddy (so our custom CA is generated) and then do what
# francis shared to handle trust setup properly
HOME=~caddy caddy trust
systemctl start caddy-api
We have a crapload of custom stuff going on where I comment there (custom templates for caddy-auth-portal, user registration with default account, etc). This gives us a system that has local trust for the caddy CA that we customized by adding a pki configuration via API. This is our workaround for now, but its reliable for us as we build raspberry pi units for our solution.
then we bring in our stock "Caddyfile" and load it via api using caddy adapt because we have to customize the pki block via API to create a unique root CA per deployment
Hmm, makes me think we should have an option to configure the local CA name via Caddyfile I guess. If you want to open a new issue for that, that'd be great š
In general, warnings are not errors. They're warnings. You can safely ignore them if you find they don't apply to you.
It's not a goal to be able to quiet all warnings, because they actually provide information.
They do. Valuable information. Usually alerting about a scenario that can potentially cause issues, or a pitfall to avoid. In an ideal world, the information they provide is considered, appropriate actions are taken, and then I need to be able to say "ok, done, thanks"
The "trust store installation disabled" warning comes with the root cert path attached, which is handy if you forget where Caddy actually stores it. It just gives you a soft reminder that you might have something to do in case you forget.
Fair enough. But without a way to dismiss that reminder, they eventually become just noise, which can make me less sensitive to other, new reminders. It gets harder and harder to keep track and tell apart "this was handled already, just ignore" from "no, this is new, something is not quite right"
Shouldn't
skip_install_trustbe the default when running as systemd service?Caddy itself doesn't really "know" that it's running as a systemd service. It would have to make guesses based on environment, and that adds weird implicit behaviour that I'd rather not have to deal with, based on how Caddy is run.
Fair enough, and I totally agree. But Caddy should also not assume it's running in a developer's workstation either, started via $ caddy run in a terminal for testing and learning purposes. And it kinda behaves that way when issuing some warnings as reminders and trying to automatically install certificates for... java? I'm asking for Caddy not to make guesses, including the current guess that local trust is relevant for all scenarios, or guessing I want to be reminded of that forever.
In theory, we could have the
aptrepo installation runcaddy trust, but... having an installer install a root cert to your trust store is pretty sketchy without somekind of opt-in.
Yes, of course, please don't! I'm actually suggesting the opposite: have an opt-in that tells Caddy to ignore local trust, for real
@MestreLion - Sharing a bit on how we set up with an install script that "handles" the trust step for us, @francislavoie shared this approach
HOME=~caddy caddy trust...
Yes, I also ended up using this in my test server (also a Raspi, aren't they cute? ā„) . It works great, but... it feels weird to perform a trust step at all just to silence a warning. I mean, I believe in this basic scenario installing local trust in the server itself is completely useless, right? There are no browsers installed, no application to consume such self-signed certs.
Maybe I'm missing something (quite likely, as I'm new to devops and sysadmin), but in your scenario how are you using the local trust? What do you need it for?
We have an application stack running on the raspberry pi, with callbacks over HTTPS between components because the jwt authentication token is being shared between those different apps, so trust is necessary between them. We use caddy-auth-portal and jwt token authentication between our custom application and node-red flows that require visibility to who the user is that is performing an action.
caddy trust (when you get it to run properly the way I indicated) is updating the trusted CA's in the /etc/ssl/certs path for the OS that is providing trust for openssl libs based components.
https://www.techrepublic.com/article/how-to-install-ca-certificates-in-ubuntu-server/
When you inspect the /etc/ssl/certs path you see symbolic links that are named as a hash of the certificate subject DN, this is used for trust evaluation for root CA's that are included by default (public root CA's) as well as private root CA's that you add manually or via the caddy trust mechanism.
We have an application stack running on the raspberry pi, with callbacks over HTTPS between components because the jwt authentication token is being shared between those different apps, so trust is necessary between them.
Oh, so if I understood correctly, some services installed on the raspi talk to each other locally via HTTPS? Using Caddy to route/proxy their requests from one another? Ok, that's a very interesting (and different) scenario from my humble Web Server serving Wordpress, Django and stuff
I managed to make some headway on this issue today, I've implemented the first half of what I described in https://github.com/caddyserver/caddy/issues/4058#issuecomment-917429284, in PR #4443. I'll probably try to get this done within the next week or so.
FYI @MestreLion @tgelite, the changes to trust installation is done in #4443! Feel free to check it out, you can build from that PR to try it out for yourself. You can take a look at the caddy help trust output to see how to use it.
Also, FYI @MestreLion, I decided to lower the "root certificate trust store installation disabled; unconfigured clients may show warnings" warning to an INFO level log instead. I think that should resolve your concerns about that.
Lowering to INFO is IMO a very good compromise, nice solution. Might be too subtle for some, still too annoying for others, but overall a win-win for everyone :-)
Is WARNING the default log level in Caddy / Go, just like in Python? If so, perfect!
Is WARNING the default log level in Caddy / Go, just like in Python? If so, perfect!
There's no default, the code is always explicit about the log level to use. We use https://github.com/uber-go/zap for logging.
By default, Caddy will log INFO level and higher, but it can be lowered to DEBUG (with the debug global option, or explicitly configuring the logger) for the most detailed information.
Thanks for this improvement. I'll pull and test.
can there be an option so that if caddy isn't already running that caddy trust starts enough of an admin api to use?
Why would that be needed? I'm not sure I like the idea of the added complexity. The point is that caddy trust will probably not be running as the user under which Caddy will actually run, but you need the CA files to be created by that user. Installing trust only needs to read the CA certs (it shouldn't try to write to Caddy's storage itself) and then write the certs to system trust stores etc.