Skip to content

Commit f9b7249

Browse files
committed
adjust tests and allow other users
1 parent 0889741 commit f9b7249

11 files changed

Lines changed: 614 additions & 163 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,17 @@ RUN addgroup -g ${READONLY_GID} "${READ_ONLY_GROUP}" && \
221221
RUN chown -R ${READ_ONLY_USER}:${READ_ONLY_GROUP} ${READ_ONLY_FOLDERS} && \
222222
chmod -R 004 ${READ_ONLY_FOLDERS} && \
223223
find ${READ_ONLY_FOLDERS} -type d -exec chmod 005 {} + && \
224-
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 700 ${READ_WRITE_FOLDERS} && \
225-
chown -R ${NETALERTX_USER}:${NETALERTX_GROUP} ${READ_WRITE_FOLDERS} && \
226-
chmod -R 600 ${READ_WRITE_FOLDERS} && \
227-
find ${READ_WRITE_FOLDERS} -type d -exec chmod 700 {} + && \
224+
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 0777 ${READ_WRITE_FOLDERS} && \
228225
chown ${READ_ONLY_USER}:${READ_ONLY_GROUP} /entrypoint.sh /opt /opt/venv && \
229226
chmod 005 /entrypoint.sh ${SYSTEM_SERVICES}/*.sh ${SYSTEM_SERVICES_SCRIPTS}/* ${ENTRYPOINT_CHECKS}/* /app /opt /opt/venv && \
230-
for dir in ${READ_WRITE_FOLDERS}; do \
231-
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 700 "$dir"; \
232-
done && \
227+
# Do not bake first-run artifacts into the image. If present, Docker volume copy-up
228+
# will persist restrictive ownership/modes into fresh named volumes, breaking
229+
# arbitrary non-root UID/GID runs.
230+
rm -f \
231+
"${NETALERTX_CONFIG}/app.conf" \
232+
"${NETALERTX_DB_FILE}" \
233+
"${NETALERTX_DB_FILE}-shm" \
234+
"${NETALERTX_DB_FILE}-wal" || true && \
233235
apk del apk-tools && \
234236
rm -Rf /var /etc/sudoers.d/* /etc/shadow /etc/gshadow /etc/sudoers \
235237
/lib/apk /lib/firmware /lib/modules-load.d /lib/sysctl.d /mnt /home/ /root \

Dockerfile

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,17 @@ RUN addgroup -g ${READONLY_GID} "${READ_ONLY_GROUP}" && \
218218
RUN chown -R ${READ_ONLY_USER}:${READ_ONLY_GROUP} ${READ_ONLY_FOLDERS} && \
219219
chmod -R 004 ${READ_ONLY_FOLDERS} && \
220220
find ${READ_ONLY_FOLDERS} -type d -exec chmod 005 {} + && \
221-
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 700 ${READ_WRITE_FOLDERS} && \
222-
chown -R ${NETALERTX_USER}:${NETALERTX_GROUP} ${READ_WRITE_FOLDERS} && \
223-
chmod -R 600 ${READ_WRITE_FOLDERS} && \
224-
find ${READ_WRITE_FOLDERS} -type d -exec chmod 700 {} + && \
221+
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 0777 ${READ_WRITE_FOLDERS} && \
225222
chown ${READ_ONLY_USER}:${READ_ONLY_GROUP} /entrypoint.sh /opt /opt/venv && \
226223
chmod 005 /entrypoint.sh ${SYSTEM_SERVICES}/*.sh ${SYSTEM_SERVICES_SCRIPTS}/* ${ENTRYPOINT_CHECKS}/* /app /opt /opt/venv && \
227-
for dir in ${READ_WRITE_FOLDERS}; do \
228-
install -d -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} -m 700 "$dir"; \
229-
done && \
224+
# Do not bake first-run artifacts into the image. If present, Docker volume copy-up
225+
# will persist restrictive ownership/modes into fresh named volumes, breaking
226+
# arbitrary non-root UID/GID runs.
227+
rm -f \
228+
"${NETALERTX_CONFIG}/app.conf" \
229+
"${NETALERTX_DB_FILE}" \
230+
"${NETALERTX_DB_FILE}-shm" \
231+
"${NETALERTX_DB_FILE}-wal" || true && \
230232
apk del apk-tools && \
231233
rm -Rf /var /etc/sudoers.d/* /etc/shadow /etc/gshadow /etc/sudoers \
232234
/lib/apk /lib/firmware /lib/modules-load.d /lib/sysctl.d /mnt /home/ /root \

front/plugins/plugin_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def read_config_file():
3737

3838

3939
configFile = read_config_file()
40-
timeZoneSetting = configFile['TIMEZONE']
40+
timeZoneSetting = configFile.get('TIMEZONE', default_tz)
4141
if timeZoneSetting not in all_timezones:
4242
timeZoneSetting = default_tz
4343
timeZone = pytz.timezone(timeZoneSetting)

install/production-filesystem/entrypoint.d/10-mounts.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
#!/usr/bin/env python3
22

3+
"""
4+
Mount Diagnostic Tool
5+
6+
Analyzes container mount points for permission issues, persistence risks, and performance problems.
7+
8+
TODO: Future Enhancements (Roadmap Step 3 & 4)
9+
1. Text-based Output: Replace emoji status indicators (✅, ❌) with plain text (e.g., [OK], [FAIL])
10+
to ensure compatibility with all terminal types and logging systems.
11+
2. OverlayFS/Copy-up Support: Improve detection logic for filesystems like Synology's OverlayFS
12+
where files may appear writable but fail on specific operations (locking, mmap).
13+
3. Root-to-User Context: Ensure this tool remains accurate when the container starts as root
14+
to fix permissions and then drops privileges to the 'netalertx' user. The check should
15+
reflect the *effective* permissions of the application user.
16+
"""
17+
318
import os
419
import sys
520
from dataclasses import dataclass
@@ -80,7 +95,21 @@ def _resolve_writeable_state(target_path: str) -> bool:
8095
seen.add(current)
8196

8297
if os.path.exists(current):
83-
return os.access(current, os.W_OK)
98+
if not os.access(current, os.W_OK):
99+
return False
100+
101+
# OverlayFS/Copy-up check: Try to actually write a file to verify
102+
if os.path.isdir(current):
103+
test_file = os.path.join(current, f".netalertx_write_test_{os.getpid()}")
104+
try:
105+
with open(test_file, "w") as f:
106+
f.write("test")
107+
os.remove(test_file)
108+
return True
109+
except OSError:
110+
return False
111+
112+
return True
84113

85114
parent_dir = os.path.dirname(current)
86115
if not parent_dir or parent_dir == current:

install/production-filesystem/entrypoint.d/15-first-run-config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if [ ! -f "${NETALERTX_CONFIG}/app.conf" ]; then
77
>&2 echo "ERROR: Failed to create config directory ${NETALERTX_CONFIG}"
88
exit 1
99
}
10-
install -m 600 -o ${NETALERTX_USER} -g ${NETALERTX_GROUP} /app/back/app.conf "${NETALERTX_CONFIG}/app.conf" || {
10+
install -m 600 /app/back/app.conf "${NETALERTX_CONFIG}/app.conf" || {
1111
>&2 echo "ERROR: Failed to deploy default config to ${NETALERTX_CONFIG}/app.conf"
1212
exit 2
1313
}

install/production-filesystem/entrypoint.d/30-apply-conf-override.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ mkdir -p "$(dirname "$NETALERTX_CONFIG")" || {
1313
rm -f "$OVERRIDE_FILE"
1414

1515
# Check if APP_CONF_OVERRIDE is set
16-
if [ -z "$APP_CONF_OVERRIDE" ]; then
17-
>&2 echo "APP_CONF_OVERRIDE is not set. Skipping override config file creation."
18-
else
16+
if [ -n "$APP_CONF_OVERRIDE" ]; then
1917
# Save the APP_CONF_OVERRIDE env variable as a JSON file
2018
echo "$APP_CONF_OVERRIDE" > "$OVERRIDE_FILE" || {
2119
>&2 echo "ERROR: Failed to write override config to $OVERRIDE_FILE"

install/production-filesystem/entrypoint.sh

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ fi
5050
RED='\033[1;31m'
5151
GREY='\033[90m'
5252
RESET='\033[0m'
53-
printf "%s" "${RED}"
54-
echo '
53+
NAX='
5554
_ _ _ ___ _ _ __ __
5655
| \ | | | | / _ \| | | | \ \ / /
5756
| \| | ___| |_/ /_\ \ | ___ _ __| |_ \ V /
@@ -60,13 +59,12 @@ echo '
6059
\_| \_/\___|\__\_| |_/_|\___|_| \__\/ \/
6160
'
6261

63-
printf "%s" "${RESET}"
62+
printf "%b%s%b" "${RED}" "${NAX}" "${RESET}"
6463
echo ' Network intruder and presence detector.
6564
https://netalertx.com
6665
6766
'
6867
set -u
69-
7068
FAILED_STATUS=""
7169
echo "Startup pre-checks"
7270
for script in "${ENTRYPOINT_CHECKS}"/*; do
@@ -123,7 +121,6 @@ fi
123121
# Set APP_CONF_OVERRIDE based on GRAPHQL_PORT if not already set
124122
if [ -n "${GRAPHQL_PORT:-}" ] && [ -z "${APP_CONF_OVERRIDE:-}" ]; then
125123
export APP_CONF_OVERRIDE='{"GRAPHQL_PORT":"'"${GRAPHQL_PORT}"'"}'
126-
echo "Setting APP_CONF_OVERRIDE to $APP_CONF_OVERRIDE"
127124
fi
128125

129126

@@ -283,15 +280,6 @@ add_service "${SYSTEM_SERVICES}/start-php-fpm.sh" "php-fpm83"
283280
add_service "${SYSTEM_SERVICES}/start-nginx.sh" "nginx"
284281
add_service "${SYSTEM_SERVICES}/start-backend.sh" "python3"
285282

286-
################################################################################
287-
# Development Mode Debug Switch
288-
################################################################################
289-
# If NETALERTX_DEBUG=1, skip automatic service restart on failure
290-
# Useful for devcontainer debugging where individual services need to be debugged
291-
if [ "${NETALERTX_DEBUG:-0}" -eq 1 ]; then
292-
echo "NETALERTX_DEBUG is set to 1, will not shut down other services if one fails."
293-
fi
294-
295283
################################################################################
296284
# Service Monitoring Loop (Production Mode)
297285
################################################################################
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
3+
# 0-storage-permission.sh: Fix permissions if running as root.
4+
#
5+
# This script checks if running as root and fixes ownership and permissions
6+
# for read-write paths to ensure proper operation.
7+
8+
# --- Color Codes ---
9+
MAGENTA=$(printf '\033[1;35m')
10+
RESET=$(printf '\033[0m')
11+
12+
# --- Main Logic ---
13+
14+
# Define paths that need read-write access
15+
READ_WRITE_PATHS="
16+
${NETALERTX_DATA}
17+
${NETALERTX_DB}
18+
${NETALERTX_API}
19+
${NETALERTX_LOG}
20+
${SYSTEM_SERVICES_RUN}
21+
${NETALERTX_CONFIG}
22+
${NETALERTX_CONFIG_FILE}
23+
${NETALERTX_DB_FILE}
24+
"
25+
26+
TARGET_USER="${NETALERTX_USER:-netalertx}"
27+
28+
# If running as root, fix permissions first
29+
if [ "$(id -u)" -eq 0 ]; then
30+
>&2 printf "%s" "${MAGENTA}"
31+
>&2 cat <<'EOF'
32+
══════════════════════════════════════════════════════════════════════════════
33+
🚨 CRITICAL SECURITY ALERT: NetAlertX is running as ROOT (UID 0)! 🚨
34+
35+
This configuration bypasses all built-in security hardening measures.
36+
You've granted a network monitoring application unrestricted access to
37+
your host system. A successful compromise here could jeopardize your
38+
entire infrastructure.
39+
40+
IMMEDIATE ACTION REQUIRED: Switch to the dedicated 'netalertx' user:
41+
* Remove any 'user:' directive specifying UID 0 from docker-compose.yml or
42+
* switch to the default USER in the image (20211:20211)
43+
44+
IMPORTANT: This corrective mode automatically adjusts ownership of
45+
/data/db and /data/config directories to the netalertx user, ensuring
46+
proper operation in subsequent runs.
47+
48+
Remember: Never operate security-critical tools as root unless you're
49+
actively trying to get pwned.
50+
51+
https://github.com/jokob-sk/NetAlertX/blob/main/docs/docker-troubleshooting/running-as-root.md
52+
══════════════════════════════════════════════════════════════════════════════
53+
EOF
54+
>&2 printf "%s" "${RESET}"
55+
56+
# Set ownership and permissions for each read-write path individually
57+
printf '%s\n' "${READ_WRITE_PATHS}" | while IFS= read -r path; do
58+
[ -n "${path}" ] || continue
59+
echo "DEBUG: Processing $path"
60+
chown -v -R "${TARGET_USER}" "${path}" || echo "DEBUG: chown failed for $path"
61+
find "${path}" -type d -exec chmod -v u+rwx {} \;
62+
find "${path}" -type f -exec chmod -v u+rw {} \;
63+
done
64+
echo Permissions fixed for read-write paths. Please restart the container as user ${TARGET_USER}.
65+
sleep infinity & wait $!
66+
fi

0 commit comments

Comments
 (0)