Skip to content

Commit 3352df0

Browse files
committed
feat(nginx): Make nginx Read-only-friendly
1 parent eb3c83a commit 3352df0

File tree

8 files changed

+60
-8
lines changed

8 files changed

+60
-8
lines changed

Dockerfile.nginx-alpine

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,24 @@ COPY dojo/ ./dojo/
6464
RUN env DD_SECRET_KEY='.' DD_DJANGO_DEBUG_TOOLBAR_ENABLED=True python3 manage.py collectstatic --noinput --verbosity=2 && true
6565

6666
FROM nginx:1.29.1-alpine3.22@sha256:42a516af16b852e33b7682d5ef8acbd5d13fe08fecadc7ed98605ba5e3b26ab8
67-
ARG uid=1001
68-
ARG appuser=defectdojo
67+
ARG uid=101
68+
ARG gid=101
6969
COPY --from=collectstatic /app/static/ /usr/share/nginx/html/static/
7070
COPY wsgi_params nginx/nginx.conf nginx/nginx_TLS.conf /etc/nginx/
7171
COPY docker/entrypoint-nginx.sh /
7272
RUN \
73-
apk add --no-cache openssl && \
73+
apk add --no-cache openssl
74+
RUN \
7475
chmod -R g=u /var/cache/nginx && \
76+
chown -R ${uid}:${gid} /var/cache/nginx && \
7577
mkdir /var/run/defectdojo && \
7678
chmod -R g=u /var/run/defectdojo && \
79+
chown -R ${uid}:${gid} /var/run/defectdojo && \
80+
chmod -R g=u /run/defectdojo && \
81+
chown -R ${uid}:${gid} /run/defectdojo && \
7782
mkdir -p /etc/nginx/ssl && \
7883
chmod -R g=u /etc/nginx && \
84+
chown -R ${uid}:${gid} /etc/nginx && \
7985
true
8086
ENV \
8187
DD_UWSGI_PASS="uwsgi_server" \
@@ -86,6 +92,6 @@ ENV \
8692
NGINX_METRICS_ENABLED="false" \
8793
METRICS_HTTP_AUTH_USER="" \
8894
METRICS_HTTP_AUTH_PASSWORD=""
89-
USER ${uid}
95+
USER ${uid}:${gid}
9096
EXPOSE 8080
9197
ENTRYPOINT ["/entrypoint-nginx.sh"]

docker-compose.override.https.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ services:
44
environment:
55
USE_TLS: 'true'
66
GENERATE_TLS_CERTIFICATE: 'true'
7+
tmpfs:
8+
- /etc/nginx/ssl:uid=101,gid=101
79
ports:
810
- target: 8443
911
published: ${DD_TLS_PORT:-8443}

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ services:
1919
NGINX_METRICS_ENABLED: "${NGINX_METRICS_ENABLED:-false}"
2020
DD_UWSGI_HOST: "${DD_UWSGI_HOST:-uwsgi}"
2121
DD_UWSGI_PORT: "${DD_UWSGI_PORT:-3031}"
22+
read_only: true
2223
volumes:
2324
- defectdojo_media:/usr/share/nginx/html/media
25+
tmpfs:
26+
- /run/defectdojo:uid=101,gid=101
27+
- /var/cache/nginx:uid=101,gid=101
2428
ports:
2529
- target: 8080
2630
published: ${DD_PORT:-8080}

helm/defectdojo/Chart.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ dependencies:
1919
condition: redis.enabled
2020
annotations:
2121
artifacthub.io/prerelease: "true"
22-
artifacthub.io/changes: ""
22+
artifacthub.io/changes: |
23+
- kind: changed
24+
description: Hardening of Nginx container

helm/defectdojo/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,12 @@ A Helm chart for Kubernetes to install DefectDojo
611611
| django.mediaPersistentVolume.persistentVolumeClaim.size | string | `"5Gi"` | |
612612
| django.mediaPersistentVolume.persistentVolumeClaim.storageClassName | string | `""` | |
613613
| django.mediaPersistentVolume.type | string | `"emptyDir"` | |
614-
| django.nginx.containerSecurityContext.runAsUser | int | `1001` | |
614+
| django.nginx.containerSecurityContext.allowPrivilegeEscalation | bool | `false` | |
615+
| django.nginx.containerSecurityContext.capabilities.drop[0] | string | `"ALL"` | |
616+
| django.nginx.containerSecurityContext.readOnlyRootFilesystem | bool | `true` | |
617+
| django.nginx.containerSecurityContext.runAsGroup | int | `101` | |
618+
| django.nginx.containerSecurityContext.runAsNonRoot | bool | `true` | |
619+
| django.nginx.containerSecurityContext.runAsUser | int | `101` | |
615620
| django.nginx.extraEnv | list | `[]` | |
616621
| django.nginx.extraVolumeMounts | list | `[]` | |
617622
| django.nginx.resources.limits.cpu | string | `"2000m"` | |

helm/defectdojo/templates/django-deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ spec:
7676
volumes:
7777
- name: run
7878
emptyDir: {}
79+
- name: nginx_cache
80+
emptyDir: {}
7981
{{- if .Values.localsettingspy }}
8082
- name: localsettingspy
8183
configMap:
@@ -296,6 +298,8 @@ spec:
296298
volumeMounts:
297299
- name: run
298300
mountPath: /run/defectdojo
301+
- name: nginx_cache
302+
mountPath: /var/cache/nginx
299303
{{- with .Values.django.extraVolumeMounts }}
300304
{{- . | toYaml | nindent 8 }}
301305
{{- end }}

helm/defectdojo/values.schema.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,29 @@
399399
"containerSecurityContext": {
400400
"type": "object",
401401
"properties": {
402+
"allowPrivilegeEscalation": {
403+
"type": "boolean"
404+
},
405+
"capabilities": {
406+
"type": "object",
407+
"properties": {
408+
"drop": {
409+
"type": "array",
410+
"items": {
411+
"type": "string"
412+
}
413+
}
414+
}
415+
},
416+
"readOnlyRootFilesystem": {
417+
"type": "boolean"
418+
},
419+
"runAsGroup": {
420+
"type": "integer"
421+
},
422+
"runAsNonRoot": {
423+
"type": "boolean"
424+
},
402425
"runAsUser": {
403426
"type": "integer"
404427
}

helm/defectdojo/values.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,14 @@ django:
286286
nginx:
287287
# Container security context for the nginx containers.
288288
containerSecurityContext:
289-
# nginx dockerfile sets USER=1001
290-
runAsUser: 1001
289+
allowPrivilegeEscalation: false
290+
readOnlyRootFilesystem: true
291+
capabilities:
292+
drop:
293+
- ALL
294+
runAsNonRoot: true
295+
runAsUser: 101
296+
runAsGroup: 101
291297
# To extra environment variables to the nginx container, you can use extraEnv. For example:
292298
# extraEnv:
293299
# - name: FOO

0 commit comments

Comments
 (0)