Skip to content

Commit e423a8f

Browse files
committed
Run gitblit as non-root user
Add user and group `gitblit` with id `8117` to the image. Add an entry-point script, which will drop root privileges when starting gitblit. It also handles command overrides or running gitblit with additional command line parameters. Closes #10
1 parent 01f890f commit e423a8f

File tree

6 files changed

+146
-31
lines changed

6 files changed

+146
-31
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.git

Dockerfile

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@ FROM openjdk:8-jre-slim
33
ENV GITBLIT_VERSION 1.9.0
44
ENV GITBLIT_DOWNLOAD_SHA 349302ded75edfed98f498576861210c0fe205a8721a254be65cdc3d8cdd76f1
55

6+
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added
7+
RUN groupadd -r -g 8117 gitblit && useradd -r -M -g gitblit -u 8117 -d /opt/gitblit gitblit
8+
9+
610
LABEL maintainer="James Moger <james.moger@gitblit.com>, Florian Zschocke <f.zschocke+gitblit@gmail.com>" \
711
org.label-schema.schema-version="1.0" \
812
org.label-schema.version="${GITBLIT_VERSION}"
913

1014

1115
ENV GITBLIT_DOWNLOAD_URL https://github.com/gitblit/gitblit/releases/download/v${GITBLIT_VERSION}/gitblit-${GITBLIT_VERSION}.tar.gz
1216

13-
# Download and Install Gitblit & Move the data files to a separate directory
17+
# Install fetch dependencies, and gsou to step down from root
1418
RUN set -eux ; \
1519
apt-get update && apt-get install -y --no-install-recommends \
1620
wget \
21+
gosu \
1722
; \
1823
rm -rf /var/lib/apt/lists/* ; \
24+
# Download and install Gitblit
1925
wget --progress=bar:force:noscroll -O gitblit.tar.gz ${GITBLIT_DOWNLOAD_URL} ; \
2026
echo "${GITBLIT_DOWNLOAD_SHA} *gitblit.tar.gz" | sha256sum -c - ; \
2127
mkdir -p /opt/gitblit ; \
2228
tar xzf gitblit.tar.gz -C /opt/gitblit --strip-components 1 ; \
23-
rm -f gitblit.tar.gz ;
29+
rm -f gitblit.tar.gz ; \
30+
# Remove unneeded scripts.
31+
rm -f /opt/gitblit/install-service-*.sh ; \
32+
rm -r /opt/gitblit/service-*.sh ;
33+
34+
35+
36+
37+
38+
2439

2540

2641
ENV GITBLIT_VAR /var/opt/gitblit
@@ -29,7 +44,7 @@ ENV GITBLIT_VAR /var/opt/gitblit
2944
RUN set -eux ; \
3045
gbetc=$GITBLIT_VAR/etc ; \
3146
gbsrv=$GITBLIT_VAR/srv ; \
32-
mkdir -p -m 0770 $gbsrv ; \
47+
mkdir -p -m 0750 $gbsrv ; \
3348
mv /opt/gitblit/data/git $gbsrv ; \
3449
ln -s $gbsrv/git /opt/gitblit/data/git ; \
3550
mv /opt/gitblit/data $gbetc ; \
@@ -126,19 +141,35 @@ include = gitblit-docker.properties\n\
126141
''#\n\
127142
\n' > $gbetc/gitblit.properties ; \
128143
\
129-
# Remove unneeded scripts.
130-
rm -f /opt/gitblit/install-service-*.sh ; \
131-
rm -r /opt/gitblit/service-*.sh ;
144+
\
145+
# Change ownership to gitblit user for all files that the process needs to write
146+
chown -R gitblit:gitblit $GITBLIT_VAR ; \
147+
# Set file permissions so that gitblit can read all and others cannot mess up
148+
# or read private data
149+
chmod -R o-rwx $gbsrv ; \
150+
chmod -R u+rwxs $gbsrv $gbsrv/git ; \
151+
chmod -R u+rwxs $gbetc ; \
152+
chmod -R o-rwx $gbetc ; \
153+
chmod ug=r $gbetc/defaults.properties ; \
154+
chmod g-w $gbetc/gitblit-docker.properties ; \
155+
chmod 0664 $gbetc/gitblit.properties ;
156+
132157

133158

134159
# Setup the Docker container environment
135160
ENV PATH /opt/gitblit:$PATH
136161

137162
WORKDIR /opt/gitblit
138163

139-
EXPOSE 8080 8443 9418 29418
140-
141164
VOLUME $GITBLIT_VAR
142165

143-
# run application
144-
CMD ["java", "-server", "-Xmx1024M", "-Djava.awt.headless=true", "-cp", "gitblit.jar:ext/*", "com.gitblit.GitBlitServer", "--baseFolder", "/var/opt/gitblit/etc"]
166+
167+
COPY docker-entrypoint.sh /usr/local/bin/
168+
ENTRYPOINT ["docker-entrypoint.sh"]
169+
170+
# 8080: HTTP front-end and transport
171+
# 8443: HTTPS front-end and transport
172+
# 9418: Git protocol transport
173+
# 29418: SSH transport
174+
EXPOSE 8080 8443 9418 29418
175+
CMD ["gitblit"]

Dockerfile.alpine

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@ FROM openjdk:8-jre-alpine
33
ENV GITBLIT_VERSION 1.9.0
44
ENV GITBLIT_DOWNLOAD_SHA 349302ded75edfed98f498576861210c0fe205a8721a254be65cdc3d8cdd76f1
55

6+
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever packages get added
7+
RUN addgroup -S -g 8117 gitblit && adduser -S -H -G gitblit -u 8117 -h /opt/gitblit gitblit
8+
9+
610
LABEL maintainer="James Moger <james.moger@gitblit.com>, Florian Zschocke <f.zschocke+gitblit@gmail.com>" \
711
org.label-schema.schema-version="1.0" \
812
org.label-schema.version="${GITBLIT_VERSION}"
913

1014

1115
ENV GITBLIT_DOWNLOAD_URL https://github.com/gitblit/gitblit/releases/download/v${GITBLIT_VERSION}/gitblit-${GITBLIT_VERSION}.tar.gz
1216

13-
14-
15-
16-
17-
# Download and install Gitblit
17+
# Install su-exec to step down from root
1818
RUN set -eux; \
19+
apk add --no-cache \
20+
'su-exec>=0.2' \
21+
; \
22+
\
23+
\
24+
# Download and install Gitblit
1925
wget -nv -O gitblit.tar.gz ${GITBLIT_DOWNLOAD_URL} ; \
2026
echo "${GITBLIT_DOWNLOAD_SHA} *gitblit.tar.gz" | sha256sum -c - ; \
2127
mkdir -p /opt/gitblit ; \
2228
tar xzf gitblit.tar.gz -C /opt/gitblit --strip-components 1 ; \
23-
rm -f gitblit.tar.gz ;
29+
rm -f gitblit.tar.gz ; \
30+
# Remove unneeded scripts.
31+
rm -f /opt/gitblit/install-service-*.sh ; \
32+
rm -r /opt/gitblit/service-*.sh ; \
33+
\
34+
# Change shell to 'sh' for Alpine
35+
for file in /opt/gitblit/*.sh ; do \
36+
sed -i -e 's;bin/bash;bin/sh;' $file ; \
37+
done
38+
2439

2540

2641
ENV GITBLIT_VAR /var/opt/gitblit
@@ -29,7 +44,7 @@ ENV GITBLIT_VAR /var/opt/gitblit
2944
RUN set -eux ; \
3045
gbetc=$GITBLIT_VAR/etc ; \
3146
gbsrv=$GITBLIT_VAR/srv ; \
32-
mkdir -p -m 0770 $gbsrv ; \
47+
mkdir -p -m 0750 $gbsrv ; \
3348
mv /opt/gitblit/data/git $gbsrv ; \
3449
ln -s $gbsrv/git /opt/gitblit/data/git ; \
3550
mv /opt/gitblit/data $gbetc ; \
@@ -126,26 +141,35 @@ include = gitblit-docker.properties\n\
126141
''#\n\
127142
\n' > $gbetc/gitblit.properties ; \
128143
\
129-
# Remove unneeded scripts.
130-
rm -f /opt/gitblit/install-service-*.sh ; \
131-
rm -r /opt/gitblit/service-*.sh ;
132-
144+
\
145+
# Change ownership to gitblit user for all files that the process needs to write
146+
chown -R gitblit:gitblit $GITBLIT_VAR ; \
147+
# Set file permissions so that gitblit can read all and others cannot mess up
148+
# or read private data
149+
chmod -R o-rwx $gbsrv ; \
150+
chmod -R u+rwxs $gbsrv $gbsrv/git ; \
151+
chmod -R u+rwxs $gbetc ; \
152+
chmod -R o-rwx $gbetc ; \
153+
chmod ug=r $gbetc/defaults.properties ; \
154+
chmod g-w $gbetc/gitblit-docker.properties ; \
155+
chmod 0644 $gbetc/gitblit.properties ;
133156

134-
# Change shell to 'sh' for Alpine
135-
RUN set -eux ; \
136-
for file in /opt/gitblit/*.sh ; do \
137-
sed -i -e 's;bin/bash;bin/sh;' $file ; \
138-
done
139157

140158

141159
# Setup the Docker container environment
142160
ENV PATH /opt/gitblit:$PATH
143161

144162
WORKDIR /opt/gitblit
145163

146-
EXPOSE 8080 8443 9418 29418
147-
148164
VOLUME $GITBLIT_VAR
149165

150-
# run application
151-
CMD ["java", "-server", "-Xmx1024M", "-Djava.awt.headless=true", "-cp", "gitblit.jar:ext/*", "com.gitblit.GitBlitServer", "--baseFolder", "/var/opt/gitblit/etc"]
166+
167+
COPY docker-entrypoint.sh /usr/local/bin/
168+
ENTRYPOINT ["docker-entrypoint.sh"]
169+
170+
# 8080: HTTP front-end and transport
171+
# 8443: HTTPS front-end and transport
172+
# 9418: Git protocol transport
173+
# 29418: SSH transport
174+
EXPOSE 8080 8443 9418 29418
175+
CMD ["gitblit"]

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ The Gitblit image stores data under `/var/opt/gitblit`. A Docker volume is defin
3939
so that data is stored persistently and efficiently. The data is split into a subfolder for
4040
configuration data (`etc/`) and one for repository data (`srv/`).
4141

42+
### User id
43+
44+
The gitblit server is run under the user and group id `8117`, assigned to the user `gitblit`.
45+
4246

4347
## Build Instructions
4448

@@ -52,7 +56,7 @@ git clone https://github.com/gitblit/gitblit-docker.git
5256
### Build your Docker container
5357
```
5458
cd gitblit-docker
55-
sudo docker build -t my-gitblit - < Dockerfile
59+
sudo docker build -t my-gitblit .
5660
```
5761

5862
### Run your Gitblit container and setup localhost port-forwarding (*-p localhost:container*)

docker-entrypoint.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
set -e
3+
4+
gitblit_path=/opt/gitblit
5+
gitblit="java -server -Xmx1024M -Djava.awt.headless=true -cp ${gitblit_path}/gitblit.jar:${gitblit_path}/ext/* com.gitblit.GitBlitServer"
6+
7+
8+
# use gosu or su-exec to step down from root
9+
runas ()
10+
{
11+
command -v su-exec > /dev/null && exec su-exec "$@"
12+
13+
command -v gosu > /dev/null && exec gosu "$@"
14+
15+
echo "Could not find any program to drop root priviledges. Exiting."
16+
exit 1
17+
}
18+
19+
20+
21+
# check if arguments are cmdline parameters. then we start gitblit with these parameters.
22+
# first arg is --option or -something
23+
if [ "${1#-}" != "$1" ] ; then
24+
set -- gitblit "$@"
25+
fi
26+
27+
28+
# if we should run gitblit, replace with the java command
29+
if [ "$1" = 'gitblit' ]; then
30+
shift
31+
# if no base folder is given, set the one in our docker default
32+
baseFolder=
33+
echo "$*" | grep -q -- "--baseFolder" || baseFolder="--baseFolder $GITBLIT_VAR/etc"
34+
set -- $gitblit $baseFolder "$@"
35+
36+
37+
# allow the container to be started with `--user`
38+
if [ "$(id -u)" = '0' ]; then
39+
runas gitblit "$@"
40+
fi
41+
fi
42+
43+
44+
# either run gitblit, if started with --user, or whatever else was given as a command
45+
exec "$@"

hub-readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ $ sudo docker run -d --name gitblit --tmpfs /var/opt/gitblit/temp -p 8443:8443 g
105105
```
106106

107107

108+
## User and group id
109+
110+
Since image version 1.9.0-3 the gitblit process will be started as a non privileged user. The user id and group id used by the images are both `8117`.
111+
112+
```console
113+
$ docker run -it --rm gitblit id gitblit
114+
uid=8117(gitblit) gid=8117(gitblit) groups=8117(gitblit)
115+
```
116+
117+
108118

109119
# Image Variants
110120
The `gitblit/gitblit` images come in multiple flavors, each designed for a specific use case.

0 commit comments

Comments
 (0)