Refactor Dockerfile and Helm chart to enhance security and user permissions
All checks were successful
Talks slides — image & chart / vars (push) Successful in 1s
Talks slides — image & chart / Helm chart (push) Successful in 11s
Talks slides — image & chart / Build container image (push) Successful in 33s

- Updated Dockerfile to run as non-root user 'nginx' and adjusted Nginx configuration for improved security.
- Added pod security context in values.yaml to align with the non-root user setup.
- Refined deployment.yaml to utilize the new pod security context for better compliance with Kubernetes security standards.
This commit is contained in:
Le Prévost-Corvellec Arnault
2026-04-08 21:36:09 +02:00
parent e1576d2360
commit 9a4942daad
3 changed files with 28 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
# syntax=docker/dockerfile:1
# BuildKit / buildx : cache apk + permissions posées au build (moins de travail / capabilities au runtime).
# BuildKit / buildx : cache apk + permissions au build.
# Limage nginx:alpine fournit déjà lutilisateur / groupe nginx (typiquement 101:101) — pas besoin de le créer.
FROM nginx:alpine
RUN --mount=type=cache,target=/var/cache/apk \
@@ -7,9 +8,7 @@ RUN --mount=type=cache,target=/var/cache/apk \
WORKDIR /usr/share/nginx/html
# Image figée au clone ; le conteneur met à jour via refresh.sh (git pull origin main).
# Seul le dossier content/ est extrait (sparse checkout) : léger et aligné sur la racine Nginx.
# Limage nginx:alpine place déjà des fichiers ici — il faut vider le répertoire avant git clone vers « . ».
# Image figée au clone ; refresh.sh fait git pull en boucle (même utilisateur que nginx grâce à USER nginx).
ARG TALKS_REPO_URL=https://git.specificat.io/arnault/Talks.git
ARG TALKS_BRANCH=main
ARG TALKS_SPARSE_DIR=content
@@ -17,14 +16,13 @@ ARG TALKS_SPARSE_DIR=content
RUN find . -mindepth 1 -delete \
&& git clone --filter=blob:none --sparse --branch "${TALKS_BRANCH}" --single-branch "${TALKS_REPO_URL}" . \
&& git sparse-checkout init --cone \
&& git sparse-checkout set "${TALKS_SPARSE_DIR}" \
&& git config --global --add safe.directory /usr/share/nginx/html
&& git sparse-checkout set "${TALKS_SPARSE_DIR}"
COPY nginx/default.conf /etc/nginx/conf.d/default.conf
COPY refresh.sh /refresh.sh
# Caches et logs : créés ici avec le même schéma que lentrypoint nginx (évite le chown au démarrage).
# Propriétaire nginx (cf. /etc/nginx/nginx.conf user) → lentrypoint ne refait pas chown si tout est déjà cohérent.
# Master non-root : pid hors /run (root-only), directive user commentée (évite setgid vers 101).
# Caches, logs, dépôt : nginx — pas de setuid/setgid ni CAP dans Kubernetes.
RUN chmod +x /refresh.sh \
&& mkdir -p \
/var/cache/nginx/client_temp \
@@ -36,9 +34,19 @@ RUN chmod +x /refresh.sh \
&& chown -R nginx:nginx \
/var/cache/nginx \
/var/log/nginx \
/usr/share/nginx/html
/usr/share/nginx/html \
&& sed -i 's|pid /run/nginx.pid;|pid /tmp/nginx.pid;|g' /etc/nginx/nginx.conf \
&& sed -i 's|pid /run/nginx/nginx.pid;|pid /tmp/nginx.pid;|g' /etc/nginx/nginx.conf \
&& sed -i 's/^[[:space:]]*user nginx;/# user nginx (master non-root)/' /etc/nginx/nginx.conf \
&& mkdir -p /home/nginx \
&& chown nginx:nginx /home/nginx /refresh.sh \
&& su -s /bin/sh nginx -c 'HOME=/home/nginx git config --global --add safe.directory /usr/share/nginx/html'
# Port non privilégié (pas de CAP_NET_BIND_SERVICE) ; le Service K8s mappe souvent 80 → 8080.
ENV HOME=/home/nginx
USER nginx
# Port non privilégié ; le Service K8s mappe souvent 80 → 8080.
EXPOSE 8080
CMD sh -c "/refresh.sh & exec nginx -g 'daemon off;'"