Skip to content

Commit 58d461f

Browse files
committed
Allow running the process as a non root user.
I've chowned the /app directory in the container to node user. This allows the process to access the private key. However! I've not set the USER to node in the Dockerfile because then the process would be unable to bind to port 80 internally (or anything below 1024). I didn't want to force it either and make it a breaking change, because many existing setups will be mapping 8080 externally to 80/443 internally, for example. The workaround is therefore on the user (sorry), they will need to set the environment variable to a higher number, or use the --sysctl net.ipv4.ip_unprivileged_port_start=0 flag Reference: moby/moby#8460 (comment) Issue mendhak#14
1 parent f89a7d6 commit 58d461f

3 files changed

Lines changed: 40 additions & 4 deletions

File tree

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ RUN npm install --production
1010

1111
RUN apk --no-cache add openssl && sh generate-cert.sh && rm -rf /var/cache/apk/*
1212

13+
RUN chown -R node:node /app
14+
1315
CMD ["node", "./index.js"]

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,15 @@ Will contain a `json` property in the response/output.
106106
}
107107
}
108108

109+
## Run as a non-root or rootless user
109110

111+
Set the `--user` to `node`, and change the internal ports to a high number.
110112

113+
docker run --user node -e HTTP_PORT=8080 -e HTTPS_PORT=8443 -p 8080:8080 -p 8443:8443 --rm mendhak/http-https-echo
114+
115+
Or use the sysctl flag, like so
116+
117+
docker run --user node --sysctl net.ipv4.ip_unprivileged_port_start=0 -p 8080:80 -p 8443:443 --rm mendhak/http-https-echo
111118

112119
## Output
113120

tests.sh

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
set -euo pipefail
4+
35
function message {
46
echo ""
57
echo "---------------------------------------------------------------"
@@ -34,13 +36,11 @@ pushd testarea
3436
message " Cleaning up from previous test run "
3537
docker ps -q --filter "name=http-echo-tests" | grep -q . && docker stop http-echo-tests
3638

37-
message " Start container "
39+
message " Start container normally "
3840
docker run -d --rm --name http-echo-tests -p 8080:80 -p 8443:443 -t mendhak/http-https-echo
3941
sleep 5
4042

4143

42-
43-
4444
message " Make http(s) request, and test the path, method and header. "
4545
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
4646
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
@@ -172,7 +172,34 @@ fi
172172
message " Stop containers "
173173
docker stop http-echo-tests
174174

175+
message " Check that container can run as a NON ROOT USER "
176+
docker run -d --name http-echo-tests --user node -e HTTP_PORT=8888 -e HTTPS_PORT=9999 -p 8080:8888 -p 8443:9999 --rm mendhak/http-https-echo
177+
178+
WHOAMI=$(docker exec http-echo-tests whoami)
179+
180+
if [ "$WHOAMI" == "node" ]
181+
then
182+
passed "Running as non root user"
183+
else
184+
failed "Running as root user"
185+
exit 1
186+
fi
187+
188+
message " Make http(s) request, and test the path, method and header. "
189+
REQUEST=$(curl -s -k -X PUT -H "Arbitrary:Header" -d aaa=bbb https://localhost:8443/hello-world)
190+
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
191+
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
192+
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
193+
then
194+
passed "HTTPS request passed."
195+
else
196+
failed "HTTPS request failed."
197+
echo $REQUEST | jq
198+
exit 1
199+
fi
175200

201+
message " Stop containers "
202+
docker stop http-echo-tests
176203

177204
popd
178-
rm -rf testarea
205+
rm -rf testarea

0 commit comments

Comments
 (0)