caddy icon indicating copy to clipboard operation
caddy copied to clipboard

Caddy notifies systemd READY=1 if initial config load fails

Open aabelix opened this issue 4 years ago • 12 comments

On our embedded system, we faced interesting situation where we accidentially deployed /etc/caddy/ as root and after caddy tried to read the certificates it caused the service to crash after it had notified systemd.

Sep 01 13:11:44 hostname caddy[1934]: {"level":"info","ts":1662037904.0107014,"logger":"tls.cache.maintenance","msg":"stopped background certificate maintenance","cache":"0x400042d570"}
Sep 01 13:11:44 hostname caddy[1934]: run: loading initial config: loading new config: loading http app module: provision http: getting tls app: loading tls app module: provision tls: loading certificates: open /etc/caddy/crt.pem: permission denied
Sep 01 13:11:44 hostname systemd[1]: Started Caddy.
Sep 01 13:11:44 hostname systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
Sep 01 13:11:44 hostname systemd[1]: caddy.service: Failed with result 'exit-code'.

I have designed our service initialization process so that if one unit fails to start the device will eventually force reboot but since caddy notifies systemd (and crashes only shortly after) the initialization does not handle this situation.

For me it seems like a race condition but I am not sure if this is intended behavior of the system. I think the notify signal should be send only after all the caddy webserver's internal setup tasks are successfully finished.

aabelix avatar Sep 01 '22 13:09 aabelix

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target swupdate
Requires=network-online.target
BindsTo=swupdate

[Service]
Type=notify
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
Environment="JWT_SHARED_KEY=wetfewrfewwefFWEWEew="

Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target

aabelix avatar Sep 01 '22 13:09 aabelix

I don't see how it's related to the notification. It seems more like a permission issue. What are the perms of /etc/caddy?

Note: you might want to revoke/change that key.

mohammed90 avatar Sep 01 '22 14:09 mohammed90

@carlwgeorge It looks like we're notifying that we're "ready" even if Load() returns an error. Do you think it's proper only to notify readiness if load succeeds?

(Note that Load can be called multiple times throughout the life of the process, as config changes all use it.)

mholt avatar Sep 01 '22 17:09 mholt

I don't see how it's related to the notification. It seems more like a permission issue. What are the perms of /etc/caddy?

Note: you might want to revoke/change that key.

The fact that ’/etc/caddy’ has incorrect permissions is aside of the point. I know how to fix the permissions that is not the issue here.

Note how the systemd log contains entry ’Started Caddy.’ which means that the caddy.service sent ready signal to systemd init process but it failed right after which means that caddy is now dead. I can configure caddy to restart on failures but the real problem is the other backend services that depend on it now start initializing themselves because caddy has already notified the init manager before failing.

The real issue for me here is that caddy service is run as a non-root service and there is no way for me to signal systemd that it now has to kill the services that depend on it which are root processes. I either have to add a monitor service for caddy (which is hacky) or run caddy service as root (yikes!).

I dont think webservers should ever fail after being initialized. My colleague managed to brick one of our devices because we could no longer access the maintainance frontend.

aabelix avatar Sep 01 '22 20:09 aabelix

READY=1

Tells the service manager that service startup is finished, or the service finished loading its configuration. This is only used by systemd if the service definition file has Type=notify set. Since there is little value in signaling non-readiness, the only value services should send is "READY=1" (i.e. "READY=0" is not defined).

https://www.freedesktop.org/software/systemd/man/sd_notify.html

I would think that sending the signal multiple times during life cycle of a process is entirely safe.

aabelix avatar Sep 01 '22 20:09 aabelix

@akeskimo We do notify of reloading just before loading the config. Is it OK to be left in that state if reloading fails?

Note that a service that sends this notification must also send a "READY=1" notification when it completed reloading its configuration.

What do we do if reloading fails?

mholt avatar Sep 01 '22 21:09 mholt

Note how the systemd log contains entry ’Started Caddy.’ which means that the caddy.service sent ready signal to systemd init process but it failed right after which means that caddy is now dead.

Oh I missed that. I didn't mean you don't know how to fix file perms, but it isn't uncommon for users to have faulty file perms and it's first instinct to double-check those. Matt makes a good point about Caddy wrongly sending the ready notification even when new config fails.

mohammed90 avatar Sep 01 '22 21:09 mohammed90

Oh I missed that. I didn't mean you don't know how to fix file perms, but it isn't uncommon for users to have faulty file perms and it's first instinct to double-check those. Matt makes a good point about Caddy wrongly sending the ready notification even when new config fails.

No worries:) I am glad that we are on the same page now.

aabelix avatar Sep 02 '22 04:09 aabelix

@akeskimo We do notify of reloading just before loading the config. Is it OK to be left in that state if reloading fails?

Yes, that would be correct behavior.

Note that a service that sends this notification must also send a "READY=1" notification when it completed reloading its configuration.

What do we do if reloading fails?

One option is to do nothing. The service will stay in "Reloading" state as the documentation that I linked suggests which will also prevent other dependencies from starting that require caddy service to be in active state.

If you want to be more informative, you could send a status message to the service manager that can be a hint about the cause of the error before the application exits.

Example 3. Error Cause Notification

A service could send the following shortly before exiting, on failure:

sd_notifyf(0, "STATUS=Failed to start up: %s\n" "ERRNO=%i", strerror(errno), errno);

There are examples on the bottom of the documentation that I linked earlier.

aabelix avatar Sep 02 '22 05:09 aabelix

Thanks for the info. I have made these improvements in 59286d2. I hope they work well for you! Please try it out :D

mholt avatar Sep 02 '22 15:09 mholt

@mholt, I have updated our caddy to master version that contains your commit and now the service seems to be entering failed state as expected:

Sep 02 19:54:30 localhost caddy[327]: Error: loading initial config: loading new config: loading http app module: provision http: getting tls app: loading tls app module: provision tls: loading certificates: open /etc/caddy/crt.pem: permission denied
Sep 02 19:54:30 localhost systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
Sep 02 19:54:30 localhost systemd[1]: caddy.service: Failed with result 'exit-code'.
Sep 02 19:54:30 localhost systemd[1]: Failed to start Caddy.

I look forward for your next stable release which I guess will be 2.6.0 by then. Thank you for your help, Matt!

aabelix avatar Sep 02 '22 19:09 aabelix

@akeskimo Great! Thanks for verifying!

Yes, 2.6 beta will go out first. I recommend trying it!

mholt avatar Sep 02 '22 20:09 mholt