canonical / pebble

Take control of your internal daemons!
https://canonical-pebble.readthedocs-hosted.com/
GNU General Public License v3.0
143 stars 54 forks source link

fix(cmdstate): better error message if $HOME dir doesn't exist #309

Closed benhoyt closed 11 months ago

benhoyt commented 11 months ago

Currently if you create a user without a home directory (or a home directory that doesn't exist), pebble exec fails with a hard-to-understand error message which implies the executable you're trying to run doesn't exist. (As #189 points out, you can easily create a user without creating the home directory.)

For the first version of this PR, we made that not fail (and use "/" as the working dir). However, as @hpidcock pointed out, that's problematic, and it probably should fail. Otherwise the caller might call rm -rf bin with HOME=/home/ubuntu, which if it doesn't exist will fall back to deleting /bin -- likely not what you want.

So it's better to return an error in this case. The charm or whoever's calling Pebble will need to fix their charm, by:

1) Explicitly specifying a working dir which does exist, or 2) Creating the HOME directory.

To test this, build and run Pebble with sudo in one terminal:

$ go build ./cmd/pebble
$ sudo ./pebble run
2023-09-19T05:08:20.457Z [pebble] Started daemon.
...

Then create a user with a non-existent home directory (I've created tstuser with non-existent HOME dir /home/tstuser for this example). Then use sudo ./pebble exec:

# BEFORE THIS PR
$ sudo ./pebble exec --user tstuser --group tstuser -- pwd
error: cannot perform the following tasks:
- exec command "pwd" (fork/exec /usr/bin/pwd: no such file or directory)

# AFTER THIS PR
$ sudo ./pebble exec --user tstuser --group tstuser -- pwd
error: cannot call exec: home directory "/home/tstuser" does not exist

Fixes #189

benhoyt commented 11 months ago

This ended up being just error message improvements, not behavioural changes -- merged without further review.