Skip to content

Conversation

philjb
Copy link
Contributor

@philjb philjb commented Aug 18, 2025

The init.sh script assumes that it being run as root so thus we can use
sudo to switch user to the influxdb user to run buildtsi; otherwise the
files are owned by root and influxdb can't start.

philjb added 2 commits August 18, 2025 12:53
The init.sh script assumes that it being run as root so thus we can use
sudo to switch user to the influxdb user to run buildtsi; otherwise the
files are owned by root and influxdb can't start.

* fixes #26698
su - influxdb -c "/usr/bin/influx_inspect buildtsi -compact-series-file \
-datadir "${DATA_DIR}" \
-waldir "${WAL_DIR}"
-waldir "${WAL_DIR}""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quoting seems off here. I think you want:

su - influxdb -c "/usr/bin/influx_inspect buildtsi -compact-series-file \
            -datadir '${DATA_DIR}'                                  \
            -waldir  '${WAL_DIR}'"

(this puts ${DATA_DIR} and ${WAL_DIR} in single quotes, which should be fine since you are double quoting the entire call to /usr/bin/influx_inspect buildtsi under su. I did not test this, but it is clear that you have nested double quotes (you could backslash the inner double quotes instead of using single quotes). Untested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the single quote around the vars will prevent their substitution so that's probably not it.

Lemme try it with runuser instead. That seems to be available by default in my ubuntu container, but sudo isn't.

Copy link
Contributor

@jdstrand jdstrand Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the single quote around the vars will prevent their substitution so that's probably not it

I was thinking it wouldn't cause this problem because at the time of the evaluation by the shell, ${DATA_DIR} and ${WAL_DIR} should be filled in for the su invocation. Eg:

$ cat ./foo.sh
#!/bin/sh
set -x

SOMEVAR="here-i-am"
su - jamie -c "echo SOMEVAR='$SOMEVAR'"

$ sudo ./foo.sh
+ SOMEVAR=here-i-am
+ su - jamie -c echo SOMEVAR='here-i-am'    # at invocation, $SOMEVAR is already evaluated and quoted
SOMEVAR=here-i-am

You could also backslash the inner double quotes to achieve a similar result:

su - influxdb -c "/usr/bin/influx_inspect buildtsi -compact-series-file \
            -datadir \"${DATA_DIR}\"                                  \
            -waldir  \"${WAL_DIR}\""

Lemme try it with runuser instead

su is annoying and there are better tools, yes. I suggest setpriv which is also from util-linux if you don't want to use su. runuser uses the PAM stack where setpriv does not.

@philjb philjb requested a review from jdstrand August 18, 2025 21:19
-datadir "${DATA_DIR}" \
-waldir "${WAL_DIR}""
echo "Building tsi with influxd_inspect buildtsi."
runuser -u influxdb -- /usr/bin/influx_inspect buildtsi -compact-series-file \
Copy link
Contributor

@jdstrand jdstrand Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest setpriv instead of runuser as it doesn't go through the PAM stack (which you don't need). See man runuser. Ie:

setpriv --reuid influxdb --regid influxdb --clear-groups /usr/bin/influx_inspect buildtsi -compact-series-file ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jamie - please take another look.

see also: https://man7.org/linux/man-pages/man1/setpriv.1.html

@philjb philjb added the 1.x label Aug 19, 2025
We want to run a command as another user but su has a frustrating syntax
for calling a command and escaping, runuser is simpiler but delegates to
su so both use PAM which is not needed in this case. It was recommended
to use setpriv which a full toolkit for setting privilege bits and can
mimic su/runuser by setting privilege to a specific user for running a
command. It seems to work and be easy to use in a script.
@philjb philjb requested a review from jdstrand August 20, 2025 19:23
jdstrand
jdstrand previously approved these changes Aug 20, 2025
-datadir "${DATA_DIR}" \
# buildtsi prompts with a warning it is is run as root as the files it makes will be owned by root.
# In that case, it awaits an interactive Yes but that can't be supplied. All around, best to run it
# as the influxdb user. sudo is also an option but not as available as su
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: s/as su/as setpriv/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch - this comment had several issues; i improved it.

@philjb philjb merged commit 7ca78d1 into master-1.x Aug 20, 2025
9 checks passed
@philjb philjb deleted the pjb/26698/fix-init.sh branch August 20, 2025 22:34
devanbenz pushed a commit that referenced this pull request Sep 29, 2025
* fix(packaging): init.sh should run buildtsi as influxdb user

The init.sh script assumes that it being run as root so thus we can use
sudo to switch user to the influxdb user to run buildtsi; otherwise the
files are owned by root and influxdb can't start.

* fixes #26698

* chore: switch from sudo to su; update comment

* chore: switch to runuser as the su syntax and debugging is difficult

* chore: switch to setpriv to avoid PAM

We want to run a command as another user but su has a frustrating syntax
for calling a command and escaping, runuser is simpiler but delegates to
su so both use PAM which is not needed in this case. It was recommended
to use setpriv which a full toolkit for setting privilege bits and can
mimic su/runuser by setting privilege to a specific user for running a
command. It seems to work and be easy to use in a script.

* chore: update comment to reflect setpriv usage

(cherry picked from commit 7ca78d1)
devanbenz added a commit that referenced this pull request Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants