Skip to content

Commit

Permalink
update entrypoint to support a user specified workdir
Browse files Browse the repository at this point in the history
github action workflows needs this
  • Loading branch information
h0tw1r3 committed May 6, 2024
1 parent 07c60e1 commit 215ad01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
echo '::error::expected an error that was not returned'
exit 1
fi
grep -s 'error: /workspace in the container is not mounted' < "$GITHUB_WORKSPACE/.errout"
grep -s 'error: .* is not mounted in the container.' < "$GITHUB_WORKSPACE/.errout"
- name: Test with a workspace volume
run: |
Expand Down Expand Up @@ -73,4 +73,4 @@ jobs:
run: |
cd roottest
docker run --rm -v `pwd`:/root ${{ steps.build.outputs.imageid }} new class toor 2>"$GITHUB_WORKSPACE/.errout"
grep -s 'mounting a volume to /root in the container is deprecated' < "$GITHUB_WORKSPACE/.errout"
grep -s 'the /root workdir is deprecated' < "$GITHUB_WORKSPACE/.errout"
20 changes: 10 additions & 10 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/sh

# re-entrant script to support automatically switching to an unprivileged user
# that matches the ownership of the RUN_VOLUME (see below)
# that matches the ownership of the RUN_WORKDIR (see below)

set -e

RUN_USER=pdk
RUN_VOLUME=/workspace
RUN_WORKDIR="${PWD}"

[ -z "${UID}" ] && UID=$(id -u)
[ -z "${GID}" ] && GID=$(id -g)
Expand All @@ -16,10 +16,10 @@ RUN_VOLUME=/workspace
# check if required path is mounted
# check for deprecated /root volume
if grep -sq " /root " < /proc/mounts ; then
[ -z "$ENTRYPOINT_RELOAD" ] && echo >&2 "mounting a volume to /root in the container is deprecated, use /workspace instead"
RUN_VOLUME=/root
elif ! grep -sq " ${RUN_VOLUME} " < /proc/mounts ; then
echo >&2 "error: ${RUN_VOLUME} in the container is not mounted." ; exit 1
[ -z "$ENTRYPOINT_RELOAD" ] && echo >&2 "warning: the /root workdir is deprecated, use /workspace instead."
RUN_WORKDIR="/root"
elif ! grep -sq " ${RUN_WORKDIR} " < /proc/mounts ; then
echo >&2 "error: ${RUN_WORKDIR} is not mounted in the container." ; exit 1
fi

create_user() {
Expand All @@ -34,18 +34,18 @@ create_user() {
# skip if re-running under newly created user
if [ -z "$ENTRYPOINT_RELOAD" ] ; then
if [ -z "$RUNNING_NON_ROOT" ] ; then
UID=$(stat -c '%u' "$RUN_VOLUME")
GID=$(stat -c '%g' "$RUN_VOLUME")
UID=$(stat -c '%u' "$RUN_WORKDIR")
GID=$(stat -c '%g' "$RUN_WORKDIR")
[ "$UID" -eq 0 ] && RUN_USER="root"
fi
create_user "$UID" "$GID"
# re-run with new user
exec su - $RUN_USER -c "cd $RUN_VOLUME ; ENTRYPOINT_RELOAD=1 $0 $*"
exec su - $RUN_USER -c "cd $RUN_WORKDIR ; ENTRYPOINT_RELOAD=1 $0 $*"
exit
fi

# sanity check supported volumes
for volume in ${RUN_VOLUME} /cache ; do
for volume in ${RUN_WORKDIR} /cache ; do
if [ ! -w "$volume" ] ; then
echo >&2 "error: unable to write to ${volume}. Ensure permissions are correct on the host." ; exit 1
fi
Expand Down

0 comments on commit 215ad01

Please sign in to comment.