fix(telegraf): Correct supplementary group handling when dropping privileges#903
fix(telegraf): Correct supplementary group handling when dropping privileges#903skartikey wants to merge 2 commits into
Conversation
…vileges
On Alpine, the base image bakes root into several default groups (bin,
daemon, sys, adm, disk, wheel, floppy, dialout, tape, video), so the
container's root process is granted them at runtime and the entrypoint's
group-forwarding loop passed all of them to the telegraf process:
$ docker run --rm telegraf:1.39-alpine id -Gn
telegraf bin daemon sys adm disk wheel floppy dialout tape video
Under su-exec the process ran with 'telegraf' only. Fix it at the source
by dropping root from those groups at build time, which also keeps
'docker run --group-add video' working (an entrypoint-side exclusion
list would silently swallow it).
In addition, seed the forwarded group list from the telegraf user's own
/etc/group memberships instead of the literal 'telegraf', restoring the
initgroups semantics su-exec provided: a derived image that adds the
telegraf user to a group no longer loses that membership, since
'setpriv --groups' replaces the supplementary set rather than extending
it. The Debian entrypoints get the same change for consistency; their
base image bakes no root memberships, so they only gain the initgroups
behaviour.
See #724.
7ec3421 to
a82330c
Compare
srebhan
left a comment
There was a problem hiding this comment.
Thanks @skartikey! One comment...
| # start from the telegraf user's own groups so memberships baked into | ||
| # /etc/group are kept (as su-exec's initgroups did), then honor groups | ||
| # supplied via 'docker run --group-add ...' but drop 'root' and anything | ||
| # already present | ||
| # see https://github.com/influxdata/influxdata-docker/issues/724 | ||
| groups="telegraf" | ||
| groups="$(id -Gn telegraf | tr ' ' ',')" | ||
| extra_groups="$(id -Gn || true)" | ||
| for group in $extra_groups; do | ||
| case "$group" in | ||
| root | telegraf) ;; | ||
| *) groups="$groups,$group" ;; | ||
| case ",${groups},root," in | ||
| *",${group},"*) ;; | ||
| *) groups="${groups},${group}" ;; | ||
| esac | ||
| done |
There was a problem hiding this comment.
As far as I understand the goal here is to remove duplicates and root from the group arguments, isn't it? So wouldn't
groups=$(echo -n "${groups} ${extra_groups}" | tr ' ' '\n' | grep -v 'root' | sort -u | tr '\n' ' ')be easier and more readable?
There was a problem hiding this comment.
Good call, the case loop was doing more work than the problem needs. Applied across all eight entrypoints:
groups="$(printf '%s %s' "$(id -Gn telegraf)" "$(id -Gn || true)" \
| tr ' ' '\n' | grep -vx root | sort -u | paste -sd, -)"
Two tweaks from your version. grep -vx instead of grep -v, since the substring match also drops group names that merely contain "root" (a group named "chroot", for example). And paste -sd, instead of tr '\n' ' ', because setpriv wants a comma list and rejects a space-separated one outright ("Invalid supplementary group id: 'g1 g2'"). Both paste and grep -x are in busybox and coreutils, so it behaves the same on Alpine and Debian.
Replace the per-group case dedupe loop in the telegraf entrypoints with a single pipeline that merges the telegraf user's own groups with those supplied via 'docker run --group-add ...', strips 'root', and de-duplicates. Behavior is unchanged; the list is just built more directly. Using an exact match for 'root' (grep -vx) rather than a substring test also keeps groups whose names merely contain "root", such as "chroot". Verified on rebuilt 1.39 Debian and Alpine images: default run yields telegraf only, named and numeric --group-add are preserved, redundant --group-add telegraf does not duplicate, memberships baked into /etc/group by derived images are kept, and the non-root passthrough path is unchanged.
Follow-up to #861 and the question raised in #724.
Two issues with the su-exec to setpriv migration:
Alpine images leak root's baked-in groups to telegraf. The Alpine base image puts root in
bin daemon sys adm disk wheel floppy dialout tape video, and the entrypoint loop forwards them all:Under su-exec this was
telegrafonly. Fixed at build time by dropping root from those groups, which keeps an intentional--group-add videoworking. Debian images are unaffected.setpriv --groupsreplaces the supplementary set, dropping the telegraf user's own/etc/groupmemberships (su-exec usedgetgrouplist()). Entrypoints now seed the list fromid -Gn telegraf, so derived images that add telegraf to a group keep that membership.Verified on a patched 1.39/alpine build and the Debian entrypoint: default run yields
telegrafonly;--group-add(numeric and named) and baked memberships are preserved; non-root passthrough unchanged.Now that #861 is merged,
telegraf/nightly/alpineis included and carries the identical entrypoint.