Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit beb7b26

Browse files
committed
Improve resiliency for arbitrary docker run -u 1006:1006 environments #175
1 parent 9a22755 commit beb7b26

File tree

5 files changed

+44
-36
lines changed

5 files changed

+44
-36
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ jobs:
4040
- travis_retry ./test/script_scenario_basic
4141
- travis_retry ./test/script_scenario_restart
4242

43-
# - env: test=scenario_arbitrary_uid
44-
# script:
45-
# - travis_retry ./test/before_install_pull
46-
# - travis_retry ./test/script_scenario_arbitrary_uid
43+
- env: test=scenario_arbitrary_uid
44+
script:
45+
- travis_retry ./test/before_install_pull
46+
- travis_retry ./test/script_scenario_arbitrary_uid
4747

4848
- env: test=scenario_node_dies
4949
script:

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,14 @@ RUN useradd seluser \
156156
--create-home \
157157
&& usermod -a -G sudo seluser \
158158
&& gpasswd -a seluser video \
159-
&& echo 'ALL ALL = (ALL) NOPASSWD: ALL' >> /etc/sudoers \
160-
&& echo 'seluser:secret' | chpasswd
159+
&& echo 'seluser:secret' | chpasswd \
160+
&& useradd extrauser \
161+
--shell /bin/bash \
162+
&& usermod -a -G sudo extrauser \
163+
&& gpasswd -a extrauser video \
164+
&& gpasswd -a extrauser seluser \
165+
&& echo 'extrauser:secret' | chpasswd \
166+
&& echo 'ALL ALL = (ALL) NOPASSWD: ALL' >> /etc/sudoers
161167

162168
#==============================
163169
# Java8 - OpenJDK JRE headless

bin/entry.sh

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@ if [ -f /var/run/secrets/kubernetes.io/serviceaccount/token ]; then
1818
export USE_KUBERNETES=true
1919
fi
2020

21-
# Let's stop using sudo in K8s environments
21+
CURRENT_UID="$(id -u)"
22+
CURRENT_GID="$(id -g)"
23+
24+
# Ensure that assigned uid has entry in /etc/passwd.
25+
if ! whoami &> /dev/null; then
26+
echo "extrauser:x:${CURRENT_UID}:0::/home/extrauser:/bin/bash" >> /etc/passwd
27+
fi
28+
29+
# Tests if the container works without sudo access
2230
if [ "${REMOVE_SELUSER_FROM_SUDOERS_FOR_TESTING}" == "true" ]; then
23-
# This doesn't seem to work unless you logout:
24-
# sudo gpasswd -d seluser sudo
2531
sudo rm $(which sudo)
2632
if sudo pwd >/dev/null 2>&1; then
2733
die "Somehow we still have sudo access despite having removed it. Quitting. $(sudo pwd)"
2834
fi
2935
fi
3036

31-
CURRENT_UID="$(id -u)"
32-
CURRENT_GID="$(id -g)"
33-
34-
# Ensure that assigned uid has entry in /etc/passwd.
35-
if [ ${CURRENT_UID} -ne 1000 ]; then
36-
echo "${USER}:x:${CURRENT_UID}:${CURRENT_GID}:,,,:/home/seluser:/bin/bash" >> /tmp/passwd
37-
# cat /etc/passwd | sed -e "s/^${USER}:/builder:/" > /tmp/passwd
38-
cat /etc/passwd | sed -e "s/^${USER}:/seluser:/" > /tmp/passwd
39-
cat /tmp/passwd > /etc/passwd
40-
rm /tmp/passwd
41-
fi
42-
4337
# Flag to know if we have sudo acess
4438
if sudo pwd >/dev/null 2>&1; then
4539
export WE_HAVE_SUDO_ACCESS="true"
@@ -48,16 +42,13 @@ else
4842
warn "We don't have sudo"
4943
fi
5044

51-
# if [ ${CURRENT_UID} -ne 1000 ]; then
52-
# if [ "${WE_HAVE_SUDO_ACCESS}" == "true" ]; then
53-
# warn 1
54-
# sudo groupadd --gid ${CURRENT_GID} selgroup
55-
# warn 2
56-
# sudo gpasswd -a ${USER} seluser
57-
# warn 3
58-
# sudo gpasswd -a ${USER} selgroup
59-
# fi
60-
# fi
45+
if [ ${CURRENT_GID} -ne 1000 ]; then
46+
if [ "${WE_HAVE_SUDO_ACCESS}" == "true" ]; then
47+
sudo groupadd --gid ${CURRENT_GID} selgroup
48+
# sudo gpasswd -a ${USER} seluser
49+
sudo gpasswd -a ${USER} selgroup
50+
fi
51+
fi
6152

6253
#==============================================
6354
# Java blocks until kernel have enough entropy
@@ -70,7 +61,7 @@ if [ "${WE_HAVE_SUDO_ACCESS}" == "true" ]; then
7061
# with --privileged and sudo here works more reliable
7162
sudo -E haveged
7263
else
73-
haveged || true
64+
haveged
7465
fi
7566

7667
# Workaround that might help to get dbus working in docker

test/script_run_all_tests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ rm -rf ./videos
1010
# In Travis these ones require travis_retry
1111
if [ "${CI}" != "true" ]; then
1212
./test/script_scenario_basic
13-
# ./test/script_scenario_arbitrary_uid
13+
./test/script_scenario_arbitrary_uid
1414
./test/script_scenario_restart
1515
./test/script_scenario_node_dies
1616
./test/script_scenario_make

video-rec/bin/start-video-rec.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ set -e
1111

1212
# Remove the video file if exists
1313
# Added a non-sudo conditional so this works on non-sudo environments like K8s
14-
(sudo rm -f "${VIDEO_BASE_PATH}"*) || (rm -f "${VIDEO_BASE_PATH}"*)
14+
if [ "${WE_HAVE_SUDO_ACCESS}" == "true" ]; then
15+
sudo rm -f "${VIDEO_BASE_PATH}"*
16+
else
17+
rm -f "${VIDEO_BASE_PATH}"*
18+
fi
1519

1620
# record testing video using password file
1721
# using sudo due to http://stackoverflow.com/questions/23544282/
@@ -32,8 +36,15 @@ export final_video_path="${VIDEOS_DIR}/${VIDEO_FILE_NAME}.${VIDEO_FILE_EXTENSION
3236

3337
# Fix perms to be able to start ffmpeg without sudo
3438
# Added a non-sudo conditional so this works on non-sudo environments like K8s
35-
(sudo touch "${tmp_video_path}") || (touch "${tmp_video_path}")
36-
(sudo chown seluser:seluser "${tmp_video_path}") || (chown seluser:seluser "${tmp_video_path}") || true
39+
if [ "${WE_HAVE_SUDO_ACCESS}" == "true" ]; then
40+
TMP_CUR_USER=$(whoami)
41+
TMP_CUR_GROUP=$(id -g)
42+
sudo touch "${tmp_video_path}"
43+
sudo chown ${TMP_CUR_USER}:${TMP_CUR_GROUP} "${tmp_video_path}"
44+
sudo chmod 666 "${tmp_video_path}"
45+
else
46+
touch "${tmp_video_path}"
47+
fi
3748

3849
# avconv or ffmpeg
3950
ffmpeg -f x11grab \

0 commit comments

Comments
 (0)