Skip to content

Commit 50deeda

Browse files
authored
[AAP-41963] Fix SonarCloud security hotspots (#707)
# [AAP-41963] Fix SonarCloud security hotspots ## Description <!-- Mandatory: Provide a clear, concise description of the changes and their purpose --> - What is being changed? Dockerfile to build image for dab postgres service and ingress service - Why is this change needed? Currently, the `postgres` and `ingress` containers are run as root, about which SonarCloud is complaining. - How does this change address the issue? 1. in `tools/dev_postgres/Dockerfile`: add `USER` directive to run the container as `postgres` non-root user; fix `$${} ` error for environment variables expansion 2. in `compose/ingress/Dockerfile`: modify the `nginx.conf` file to add appropriate configurations for non-root nginx user (reference: https://hub.docker.com/_/nginx), and add `USER` directive ## Type of Change <!-- Mandatory: Check one or more boxes that apply --> - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update - [ ] Test update - [ ] Refactoring (no functional changes) - [x] Development environment change - [x] Configuration change ## Self-Review Checklist <!-- These items help ensure quality - they complement our automated CI checks --> - [x] I have performed a self-review of my code - [x] I have added relevant comments to complex code sections - [x] I have updated documentation where needed - [x] I have considered the security impact of these changes - [x] I have considered performance implications - [x] I have thought about error handling and edge cases - [x] I have tested the changes in my local environment ## Testing Instructions <!-- Optional for test-only changes. Mandatory for all other changes --> <!-- Must be detailed enough for reviewers to reproduce --> ### Prerequisites <!-- List any specific setup required --> ### Steps to Test 1. remove existing `django-ansible-base-postgres`, `nginx` and `django-ansible-base-ingress` container images in your local 2. in `django-ansible-base` dir, run command `make docker compose build`, then run `make docker compose up` 3. check the logs of all containers and confirm that the services are running fine. ### Expected Results <!-- Describe what should happen after following the steps --> - All services in dab should run fine. - `django-ansible-base-ingress-1` container is run as `nginx` user. - `django-ansible-base-nginx-1` container is run as `nginx` user. - `django-ansible-base-postgres` container is run as `postgres` user. ## Additional Context <!-- Optional but helpful information --> ### Required Actions <!-- Check if changes require work in other areas --> <!-- Remove section if no external actions needed --> - [ ] Requires documentation updates <!-- API docs, feature docs, deployment guides --> - [ ] Requires downstream repository changes <!-- Specify repos: django-ansible-base, eda-server, etc. --> - [ ] Requires infrastructure/deployment changes <!-- CI/CD, installer updates, new services --> - [ ] Requires coordination with other teams <!-- UI team, platform services, infrastructure --> - [ ] Blocked by PR/MR: #XXX <!-- Reference blocking PRs/MRs with brief context --> ### Screenshots/Logs <!-- Add if relevant to demonstrate the changes -->
1 parent 1bc3591 commit 50deeda

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

compose/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## `compose` Directory
2+
This directory contains the Dockerfile and configuration files for Nginx and Ingress services used in the `test_app`.
3+
At the moment, this code is only for testing and development purposes, and is not used in production.
4+

compose/ingress/Dockerfile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
FROM mirror.gcr.io/library/nginx:latest
1+
# Dockerfile to build the image for dab_ingress service
22

3+
FROM mirror.gcr.io/library/nginx:1.27
4+
5+
RUN mkdir -p /etc/nginx/ssl && chown -R nginx:nginx /etc/nginx/ssl
36
COPY entrypoint.sh /entrypoint.sh
47
COPY nginx.conf /etc/nginx/conf.d/default.conf
8+
9+
# Modify the default Nginx configuration to allow it to run as a non-root user
10+
# This follows Nginx Docker Hub instructions: https://hub.docker.com/_/nginx
11+
# By default, Nginx writes its PID file to /var/run/nginx.pid, which is a restricted location.
12+
# We change it to /tmp/nginx.pid so that it can be accessed by a non-root user.
13+
RUN sed -i -E '/^user\s+nginx;/d' /etc/nginx/nginx.conf && \
14+
sed -i -E 's|pid\s+/var/run/nginx.pid;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf && \
15+
sed -i '/http {/a \
16+
client_body_temp_path /tmp/client_temp;\n\
17+
proxy_temp_path /tmp/proxy_temp;\n\
18+
fastcgi_temp_path /tmp/fastcgi_temp;\n\
19+
uwsgi_temp_path /tmp/uwsgi_temp;\n\
20+
scgi_temp_path /tmp/scgi_temp;' /etc/nginx/nginx.conf
21+
22+
USER nginx

compose/nginx/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dockerfile to build the image for dab_nginx service
2+
3+
FROM mirror.gcr.io/library/nginx:1.27
4+
5+
COPY nginx.conf /etc/nginx/conf.d/default.conf
6+
7+
# Modify the default Nginx configuration to allow it to run as a non-root user
8+
# This follows Nginx Docker Hub instructions: https://hub.docker.com/_/nginx
9+
# By default, Nginx writes its PID file to /var/run/nginx.pid, which is a restricted location.
10+
# We change it to /tmp/nginx.pid so that it can be accessed by a non-root user.
11+
RUN sed -i -E '/^user\s+nginx;/d' /etc/nginx/nginx.conf && \
12+
sed -i -E 's|pid\s+/var/run/nginx.pid;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf && \
13+
sed -i '/http {/a \
14+
client_body_temp_path /tmp/client_temp;\n\
15+
proxy_temp_path /tmp/proxy_temp;\n\
16+
fastcgi_temp_path /tmp/fastcgi_temp;\n\
17+
uwsgi_temp_path /tmp/uwsgi_temp;\n\
18+
scgi_temp_path /tmp/scgi_temp;' /etc/nginx/nginx.conf
19+
20+
USER nginx

docker-compose.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ services:
3636

3737
# This is the intermediate application reverse proxy without ssl
3838
nginx:
39-
image: "nginx:latest"
40-
volumes:
41-
- './compose/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:z'
39+
build: ./compose/nginx
40+
image: "dab_nginx:1.27"
4241
ports:
4342
- '80:80'
4443
depends_on:
@@ -47,9 +46,8 @@ services:
4746
# This is the ssl terminated "ingress" reverse proxy
4847
ingress:
4948
build: ./compose/ingress
49+
image: "dab_ingress:1.27"
5050
command: './entrypoint.sh'
51-
volumes:
52-
- './compose/ingress/certs:/etc/nginx/ssl:z'
5351
ports:
5452
- "443:443"
5553
depends_on:

tools/dev_postgres/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ ENV POSTGRES_DB=dab_db
44
ENV POSTGRES_USER=dab
55
ENV POSTGRES_PASSWORD=dabing
66

7+
USER postgres
8+
79
EXPOSE 5432
810

9-
HEALTHCHECK --interval=10s --timeout=5s --retries=5 CMD ["pg_isready", "-U", "dab", "-d", "dab_db"]
11+
HEALTHCHECK --interval=10s --timeout=5s --retries=5 CMD ["sh", "-c", "pg_isready -U \"$POSTGRES_USER\" -d \"$POSTGRES_DB\""]

0 commit comments

Comments
 (0)