Skip to content

Commit 2c951d5

Browse files
committed
Migrate to setpriv from gosu
Fixes: 1304 Signed-off-by: Pat Riehecky <riehecky@fnal.gov>
1 parent 889f944 commit 2c951d5

File tree

4 files changed

+19
-53
lines changed

4 files changed

+19
-53
lines changed

Dockerfile-alpine.template

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,22 @@ RUN set -eux; \
1313
# see https://github.com/docker-library/postgres/issues/274
1414
install --verbose --directory --owner postgres --group postgres --mode 1777 /var/lib/postgresql
1515

16-
# grab gosu for easy step-down from root
17-
# https://github.com/tianon/gosu/releases
18-
ENV GOSU_VERSION 1.17
19-
RUN set -eux; \
20-
\
21-
apk add --no-cache --virtual .gosu-deps \
22-
ca-certificates \
23-
dpkg \
24-
gnupg \
25-
; \
26-
\
27-
dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
28-
wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
29-
wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
30-
\
31-
# verify the signature
32-
export GNUPGHOME="$(mktemp -d)"; \
33-
gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
34-
gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
35-
gpgconf --kill all; \
36-
rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
37-
\
38-
# clean up fetch dependencies
39-
apk del --no-network .gosu-deps; \
40-
\
41-
chmod +x /usr/local/bin/gosu; \
42-
# verify that the binary works
43-
gosu --version; \
44-
gosu nobody true
4516
{{ if env.version | IN("13", "14", "15", "16") then ( -}}
46-
RUN set -eux; ln -svf gosu /usr/local/bin/su-exec; su-exec nobody true # backwards compatibility (removed in PostgreSQL 17+)
17+
# backwards compatibility (removed in PostgreSQL 17+)
18+
RUN set -eux; \
19+
\
20+
apk add --no-cache su-exec; \
21+
echo 'testing su-exec:' ; \
22+
su-exec nobody id
4723
{{ ) else "" end -}}
4824

25+
# Install real setpriv from util-linux rather than busybox
26+
RUN set -eux; \
27+
\
28+
apk add --no-cache setpriv; \
29+
echo 'testing setpriv:' ; \
30+
setpriv --reuid=nobody --regid=nobody --clear-groups id
31+
4932
# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
5033
# alpine doesn't require explicit locale-file generation
5134
ENV LANG en_US.utf8

Dockerfile-debian.template

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,11 @@ RUN set -ex; \
2020
; \
2121
rm -rf /var/lib/apt/lists/*
2222

23-
# grab gosu for easy step-down from root
24-
# https://github.com/tianon/gosu/releases
25-
ENV GOSU_VERSION 1.17
23+
# ensure setpriv works as expected
2624
RUN set -eux; \
27-
savedAptMark="$(apt-mark showmanual)"; \
28-
apt-get update; \
29-
apt-get install -y --no-install-recommends ca-certificates wget; \
30-
rm -rf /var/lib/apt/lists/*; \
31-
dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
32-
wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
33-
wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
34-
export GNUPGHOME="$(mktemp -d)"; \
35-
gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
36-
gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
37-
gpgconf --kill all; \
38-
rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
39-
apt-mark auto '.*' > /dev/null; \
40-
[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
41-
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
42-
chmod +x /usr/local/bin/gosu; \
43-
gosu --version; \
44-
gosu nobody true
25+
\
26+
echo 'testing setpriv:' ; \
27+
setpriv --reuid=nobody --regid=nobody --clear-groups id
4528

4629
# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
4730
RUN set -eux; \

docker-ensure-initdb.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ docker_setup_env
2727
docker_create_db_directories
2828
if [ "$(id -u)" = '0' ]; then
2929
# then restart script as postgres user
30-
exec gosu postgres "$BASH_SOURCE" "$@"
30+
exec setpriv --reuid=postgres --regid=postgres --clear-groups "$BASH_SOURCE" "$@"
3131
fi
3232

3333
# only run initialization on an empty data directory

docker-entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ _main() {
344344
docker_create_db_directories
345345
if [ "$(id -u)" = '0' ]; then
346346
# then restart script as postgres user
347-
exec gosu postgres "$BASH_SOURCE" "$@"
347+
exec setpriv --reuid=postgres --regid=postgres --clear-groups "$BASH_SOURCE" "$@"
348348
fi
349349

350350
# only run initialization on an empty data directory

0 commit comments

Comments
 (0)