Skip to content

[WIP] Rootless docker #7129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
FROM alpine:3.9
LABEL maintainer="maintainers@gitea.io"

EXPOSE 22 3000
EXPOSE 2222 3000

RUN apk --no-cache add \
bash \
ca-certificates \
curl \
gettext \
git \
linux-pam \
openssh \
s6 \
openssh-keygen \
sqlite \
su-exec \
tzdata

RUN addgroup \
Expand All @@ -48,14 +45,19 @@ RUN addgroup \
git && \
echo "git:$(dd if=/dev/urandom bs=24 count=1 status=none | base64)" | chpasswd

ENV USER git

ENV HOME "/data/git"
RUN mkdir -p /data /app/gitea && chmod 777 /data
RUN ln -s /app/gitea/gitea /usr/local/bin/gitea

USER git:git
ENV GITEA_CUSTOM /data/gitea

VOLUME ["/data"]

WORKDIR /app/gitea
ENTRYPOINT ["/usr/bin/entrypoint"]
CMD ["/bin/s6-svscan", "/etc/s6"]
CMD ["/app/gitea/gitea", "web"]

COPY docker/root /
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
RUN ln -s /app/gitea/gitea /usr/local/bin/gitea
2 changes: 0 additions & 2 deletions docker/root/etc/s6/.s6-svscan/finish

This file was deleted.

2 changes: 0 additions & 2 deletions docker/root/etc/s6/gitea/finish

This file was deleted.

6 changes: 0 additions & 6 deletions docker/root/etc/s6/gitea/run

This file was deleted.

2 changes: 0 additions & 2 deletions docker/root/etc/s6/openssh/finish

This file was deleted.

6 changes: 0 additions & 6 deletions docker/root/etc/s6/openssh/run

This file was deleted.

29 changes: 0 additions & 29 deletions docker/root/etc/s6/openssh/setup

This file was deleted.

33 changes: 0 additions & 33 deletions docker/root/etc/ssh/sshd_config

This file was deleted.

6 changes: 6 additions & 0 deletions docker/root/etc/templates/app.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
APP_NAME = $APP_NAME
RUN_USER = $RUN_USER
RUN_MODE = $RUN_MODE

[repository]
Expand All @@ -17,7 +18,12 @@ HTTP_PORT = $HTTP_PORT
ROOT_URL = $ROOT_URL
DISABLE_SSH = $DISABLE_SSH
SSH_PORT = $SSH_PORT
START_SSH_SERVER = true
BUILTIN_SSH_SERVER_USER = $RUN_USER
SSH_LISTEN_PORT = $SSH_LISTEN_PORT
LFS_START_SERVER = true
LFS_CONTENT_PATH = /data/git/lfs
;Config via env variable new config

[database]
PATH = /data/gitea/gitea.db
Expand Down
32 changes: 2 additions & 30 deletions docker/root/usr/bin/entrypoint
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
#!/bin/sh

if [ "${USER}" != "git" ]; then
# rename user
sed -i -e "s/^git\:/${USER}\:/g" /etc/passwd
# switch sshd config to different user
sed -i -e "s/AllowUsers git$/AllowUsers ${USER}/g" /etc/ssh/sshd_config
fi

if [ -z "${USER_GID}" ]; then
USER_GID="`id -g ${USER}`"
fi

if [ -z "${USER_UID}" ]; then
USER_UID="`id -u ${USER}`"
fi

## Change GID for USER?
if [ -n "${USER_GID}" ] && [ "${USER_GID}" != "`id -g ${USER}`" ]; then
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*/${USER}:\1:${USER_GID}/" /etc/group
sed -i -e "s/^${USER}:\([^:]*\):\([0-9]*\):[0-9]*/${USER}:\1:\2:${USER_GID}/" /etc/passwd
fi

## Change UID for USER?
if [ -n "${USER_UID}" ] && [ "${USER_UID}" != "`id -u ${USER}`" ]; then
sed -i -e "s/^${USER}:\([^:]*\):[0-9]*:\([0-9]*\)/${USER}:\1:${USER_UID}:\2/" /etc/passwd
fi

for FOLDER in /data/gitea/conf /data/gitea/log /data/git /data/ssh; do
mkdir -p ${FOLDER}
done
[[ -f /usr/bin/setup ]] && source /usr/bin/setup

if [ $# -gt 0 ]; then
exec "$@"
else
exec /bin/s6-svscan /etc/s6
exec /app/gitea/gitea web
fi
14 changes: 6 additions & 8 deletions docker/root/etc/s6/gitea/setup → docker/root/usr/bin/setup
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

#TODO maybe /data/git/.ssh not needed anymore
Copy link
Member

Choose a reason for hiding this comment

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

If docker user internal SSH server, it's not needed

Copy link
Contributor

Choose a reason for hiding this comment

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

While it is not needed for the single process setup, removing it would break existing setups.

Copy link
Member Author

Choose a reason for hiding this comment

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

@das7pad yes but it is for the setup part so if we don't need it anymore we don't need to create it if it doesn't exist.

Copy link
Contributor

Choose a reason for hiding this comment

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

For folks who backup the entire /data/git directory it is not a problem. But for those who just backup non-generated files, read only the repositories directory, the .ssh directory would be missing upon restore.

Copy link
Contributor

Choose a reason for hiding this comment

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

But the internal sshserver doesn't use the .ssh directory...

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking about the third example in #7129 (review).
I know that the files in the .ssh directory are only needed to get the spawned sshd-childs talk with the correct gitea instance.

if [ ! -d /data/git/.ssh ]; then
mkdir -p /data/git/.ssh
chmod 700 /data/git/.ssh
Expand All @@ -26,11 +27,13 @@ if [ ! -f ${GITEA_CUSTOM}/conf/app.ini ]; then
# Substitude the environment variables in the template
APP_NAME=${APP_NAME:-"Gitea: Git with a cup of tea"} \
RUN_MODE=${RUN_MODE:-"dev"} \
RUN_USER=${USER:-"git"} \
SSH_DOMAIN=${SSH_DOMAIN:-"localhost"} \
HTTP_PORT=${HTTP_PORT:-"3000"} \
ROOT_URL=${ROOT_URL:-""} \
DISABLE_SSH=${DISABLE_SSH:-"false"} \
SSH_PORT=${SSH_PORT:-"22"} \
SSH_PORT=${SSH_PORT:-"2222"} \
SSH_LISTEN_PORT=${SSH_LISTEN_PORT:-$SSH_PORT} \
DB_TYPE=${DB_TYPE:-"sqlite3"} \
DB_HOST=${DB_HOST:-"localhost:3306"} \
DB_NAME=${DB_NAME:-"gitea"} \
Expand All @@ -41,12 +44,7 @@ if [ ! -f ${GITEA_CUSTOM}/conf/app.ini ]; then
REQUIRE_SIGNIN_VIEW=${REQUIRE_SIGNIN_VIEW:-"false"} \
SECRET_KEY=${SECRET_KEY:-""} \
envsubst < /etc/templates/app.ini > ${GITEA_CUSTOM}/conf/app.ini

chown ${USER}:git ${GITEA_CUSTOM}/conf/app.ini
fi

# only chown if current owner is not already the gitea ${USER}. No recursive check to save time
if ! [[ $(ls -ld /data/gitea | awk '{print $3}') = ${USER} ]]; then chown -R ${USER}:git /data/gitea; fi
if ! [[ $(ls -ld /app/gitea | awk '{print $3}') = ${USER} ]]; then chown -R ${USER}:git /app/gitea; fi
if ! [[ $(ls -ld /data/git | awk '{print $3}') = ${USER} ]]; then chown -R ${USER}:git /data/git; fi
chmod 0755 /data/gitea /app/gitea /data/git
chmod 0755 /data/gitea /data/git
#chmod 0755 /app/gitea
Copy link
Contributor

Choose a reason for hiding this comment

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

/app/gitea is owned by root.

Suggested change
#chmod 0755 /app/gitea

Copy link
Member Author

@sapk sapk Jun 15, 2019

Choose a reason for hiding this comment

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

Yes and I don't think it is a problem. I will recheck but that a good point that the process inside the containre couldn't change content inside /app/gitea

11 changes: 2 additions & 9 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,11 @@ func checkKeyFingerprint(e Engine, fingerprint string) error {

func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) {
// Calculate fingerprint.
tmpPath, err := writeTmpKeyFile(publicKeyContent)
pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKeyContent))
if err != nil {
return "", err
}
defer os.Remove(tmpPath)
stdout, stderr, err := process.GetManager().Exec("AddPublicKey", "ssh-keygen", "-lf", tmpPath)
if err != nil {
return "", fmt.Errorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
} else if len(stdout) < 2 {
return "", errors.New("not enough output for calculating fingerprint: " + stdout)
}
return strings.Split(stdout, " ")[1], nil
return ssh.FingerprintSHA256(pk), nil
}

func calcFingerprintNative(publicKeyContent string) (string, error) {
Expand Down