Skip to content

Commit 3093d83

Browse files
authored
Merge pull request #7 from komacke/harden-deployment
Harden deployment
2 parents 1b42fc1 + f78b417 commit 3093d83

4 files changed

Lines changed: 103 additions & 23 deletions

File tree

docker/Dockerfile

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22
## REFER TO THE DOCUMENTATION FOR CONFIGURATION OF YOUR HAMCLOCK!
33
FROM alpine:3.23
44
ARG HC_SIZE
5+
ARG HC_UID
6+
ARG HC_GID
57

68
USER root
7-
WORKDIR /root/hamclock
9+
WORKDIR /opt/hamclock
810
# Install prerequisites
911
RUN apk update && \
1012
apk add curl make g++ libx11-dev openssl unzip perl && \
1113
rm -rf /var/cache/apk/*
1214

1315
# Install Hamclock
1416
COPY ESPHamClock/ ESPHamClock/
15-
RUN cd ESPHamClock && \
17+
COPY hamclock-contrib/ hamclock-contrib/
18+
COPY docker/run.sh ./
19+
COPY docker/backend_host ./
20+
RUN addgroup -g $HC_GID hamclock && \
21+
adduser -u $HC_UID -G hamclock -h /opt -HD -s /sbin/nologin hamclock && \
22+
\
23+
cp hamclock-contrib/hceeprom.pl ESPHamClock/ && \
24+
chmod +x ESPHamClock/hceeprom.pl && \
25+
chmod +x run.sh && \
26+
\
27+
mkdir /opt/.hamclock && \
28+
chown -R hamclock:hamclock /opt/hamclock /opt/.hamclock && \
29+
cd ESPHamClock && \
30+
\
1631
if [ -n "$HC_SIZE" ]; then \
1732
make -j 4 hamclock-web-${HC_SIZE} && \
1833
make install && \
@@ -23,19 +38,10 @@ RUN cd ESPHamClock && \
2338
cp hamclock-web-$size /usr/local/bin/ && \
2439
make clean; \
2540
done; \
26-
chown root /usr/local/bin/hamclock-web-* && \
27-
chmod u+s /usr/local/bin/hamclock-web-*; \
41+
chown hamclock /usr/local/bin/hamclock-web-*; \
2842
fi
2943

30-
COPY hamclock-contrib/ hamclock-contrib/
31-
# Install Hamclock Contrib and move hceeprom to hamclock directory
32-
RUN cp hamclock-contrib/hceeprom.pl /root/hamclock/ESPHamClock && \
33-
chmod +x /root/hamclock/ESPHamClock/hceeprom.pl
34-
35-
# Copy runfile
36-
COPY docker/run.sh ./
37-
COPY docker/backend_host ./
38-
RUN chmod +x run.sh
44+
USER hamclock
3945

40-
WORKDIR /root/hamclock/ESPHamClock
41-
CMD ["/root/hamclock/run.sh"]
46+
WORKDIR /opt/hamclock/ESPHamClock
47+
CMD ["/opt/hamclock/run.sh"]

docker/build-image.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Variables to set
44
IMAGE_BASE=komacke/hamclock
5+
HC_UID=1199
6+
HC_GID=1199
57

68
# Don't set anything past here
79
TAG=$(git describe --exact-match --tags 2>/dev/null)
@@ -135,9 +137,9 @@ build_image() {
135137
pushd "$HERE/.." >/dev/null
136138
echo $GIT_VERSION > git.version
137139
if [ $MULTI_PLATFORM == true ]; then
138-
docker buildx build $NOCACHE_ARG --pull $SET_HC_SIZE -t $IMAGE -f docker/Dockerfile --platform linux/amd64,linux/arm64 --push .
140+
docker buildx build $NOCACHE_ARG --pull --build-arg HC_UID=$HC_UID --build-arg HC_GID=$HC_GID $SET_HC_SIZE -t $IMAGE -f docker/Dockerfile --platform linux/amd64,linux/arm64 --push .
139141
else
140-
docker build $NOCACHE_ARG --pull $SET_HC_SIZE -t $IMAGE -f docker/Dockerfile .
142+
docker build $NOCACHE_ARG --pull --build-arg HC_UID=$HC_UID --build-arg HC_GID=$HC_GID $SET_HC_SIZE -t $IMAGE -f docker/Dockerfile .
141143
fi
142144
rm -f git.version
143145
RETVAL=$?

docker/manage-hc-docker.sh

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ IMAGE_BASE=komacke/hamclock
88
# Get our directory locations in order
99
HERE="$(realpath -s "$(dirname "$0")" 2>/dev/null)"
1010
[ -z "$HERE" ] && HERE="$(realpath "$(dirname "$0")")"
11+
REL_HERE="$(dirname "$0")"
1112
THIS="$(basename "$0")"
1213
STARTED_FROM="$PWD"
1314
cd $HERE
@@ -24,6 +25,8 @@ DEFAULT_BACKEND_HOST=-
2425
DEFAULT_HC_SIZE=-
2526
# the following env is the lighttpd env file
2627
DEFAULT_HC_EEPROM=hc.settings
28+
HC_UID=1199
29+
HC_GID=1199
2730

2831
# the following env is for sticky settings
2932
STICKY_ENV_FILE=$DOCKER_PROJECT.env
@@ -413,7 +416,7 @@ docker_compose_up() {
413416
docker_compose_yml && docker compose -f <(echo "$DOCKER_COMPOSE_YML") create
414417
RETVAL=$?
415418
[ $RETVAL -ne 0 ] && return $RETVAL
416-
docker compose $INITIAL_CONFIG_FILE -f <(echo "$DOCKER_COMPOSE_YML") up -d
419+
docker compose -f <(echo "$DOCKER_COMPOSE_YML") up -d
417420
RETVAL=$?
418421
fi
419422

@@ -687,7 +690,75 @@ determine_eeprom_file() {
687690
if [ ! -e "$HC_EEPROM" ]; then
688691
touch "$HC_EEPROM"
689692
if [ -r "$HERE/config.env" ]; then
690-
INITIAL_CONFIG_FILE="--env-file $HERE/config.env"
693+
INITIAL_CONFIG_FILE="env_file:
694+
- $HERE/config.env"
695+
fi
696+
fi
697+
698+
hc_settings_perms
699+
}
700+
701+
# checking if the HC_EEPROM file is writable by the user in the container. If not the
702+
# container will crash and we need to fix it.
703+
hc_settings_perms() {
704+
# hc.settings needs to be writable by user 1199:1199
705+
HC_PERMS=$(stat -c '%a' "$HC_EEPROM")
706+
707+
# who owns it
708+
HC_OWN=$(stat -c '%u' "$HC_EEPROM")
709+
HC_GRP=$(stat -c '%g' "$HC_EEPROM")
710+
711+
CAN_ACCESS=false
712+
713+
# test for u+rw
714+
if [[ "$HC_OWN" == "$HC_UID" && "$HC_PERMS" == [67]?? ]]; then
715+
CAN_ACCESS=true
716+
717+
# test for g+rw
718+
elif [[ "$HC_GRP" == "$HC_GID" && "$HC_PERMS" == ?[67]? ]]; then
719+
CAN_ACCESS=true
720+
721+
# test for o+rw
722+
elif [[ "$HC_PERMS" == ??[67] ]]; then
723+
CAN_ACCESS=true
724+
725+
# otherwise try to fix it
726+
else
727+
728+
# set o+rw
729+
chmod o+rw "$HC_EEPROM" >/dev/null 2>&1
730+
PERM_RETVAL=$?
731+
732+
# if we couldn't set it, we can copy it, delete the original and
733+
# set the perms
734+
if [ $PERM_RETVAL -ne 0 ]; then
735+
736+
# we can do it if the container isn't holding the fh
737+
get_current_image_tag
738+
if [ "$CURRENT_DOCKER_IMAGE" == null ]; then
739+
cp "$HC_EEPROM" "$HC_EEPROM.tmp"
740+
rm -f "$HC_EEPROM"
741+
mv "$HC_EEPROM.tmp" "$HC_EEPROM"
742+
chmod o+rw $HC_EEPROM >/dev/null 2>&1
743+
PERM_RETVAL=$?
744+
if [ $PERM_RETVAL -eq 0 ]; then
745+
CAN_ACCESS=true
746+
else
747+
CAN_ACCESS=false
748+
fi
749+
750+
# otherwise we need to take harsher measures.
751+
else
752+
CAN_ACCESS=false
753+
fi
754+
fi
755+
fi
756+
757+
# take harsher measures - down the container and don't cause an infinite loop
758+
if [ $CAN_ACCESS == false ]; then
759+
if [ ${FUNCNAME[3]} != docker_compose_down ]; then
760+
docker_compose_down
761+
[ ${FUNCNAME[1]} != hc_settings_perms ] && hc_settings_perms
691762
fi
692763
fi
693764
}
@@ -808,6 +879,7 @@ services:
808879
- UTC_OFFSET=0
809880
$DC_BACKEND_HOST
810881
$DC_HC_SIZE
882+
$INITIAL_CONFIG_FILE
811883
container_name: $CONTAINER
812884
image: $IMAGE
813885
restart: unless-stopped
@@ -817,7 +889,7 @@ services:
817889
volumes:
818890
- type: bind
819891
source: $HC_EEPROM
820-
target: /root/.hamclock/eeprom
892+
target: /opt/.hamclock/eeprom
821893
bind:
822894
selinux: Z
823895
healthcheck:

docker/run.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ if [ -z "$HC_SIZE" ]; then
55
HC_SIZE=2400x1440
66
fi
77

8-
if [ -z "$BACKEND_HOST" -a -e /root/hamclock/backend_host ]; then
9-
BACKEND_HOST="$(grep -v '^#' /root/hamclock/backend_host)"
8+
if [ -z "$BACKEND_HOST" -a -e /opt/hamclock/backend_host ]; then
9+
BACKEND_HOST="$(grep -v '^#' /opt/hamclock/backend_host)"
1010
fi
1111
if [ -n "$BACKEND_HOST" ]; then
1212
BACKEND_ARG="-b $BACKEND_HOST"
1313
fi
1414

15-
# these values only matter if there is not a /root/.hamclock/eeprom file.
15+
# these values only matter if there is not an /opt/.hamclock/eeprom file.
1616
perl hceeprom.pl NV_CALLSIGN $CALLSIGN && \
1717
perl hceeprom.pl NV_DE_GRID $LOCATOR && \
1818
perl hceeprom.pl NV_DE_LAT $LAT && \

0 commit comments

Comments
 (0)