CHAT_OUTPUT_BEGIN
COMMAND_ID=READER-NGINX-HELPER-INTERFACE-AUDIT-327
STATUS=OK
RC=0
HOST=pve01
MODE=read-only
COMPONENT=cluster-audit
REFERENCE_REGISTER_CHECK=OK
REFERENCE_SHA256=5763f2f8edc75fcf6f3951f8c95896d1cc112ceccb6cf5b64a383f540a4deb8e
ERROR_REGISTER_CHECK=OK
ERROR_REGISTER_SHA256=08b3a769b8e38eaa1acec915352fb328180d70caa7ff6cd9d1ec0da426262752
COMMAND_SHA256=1ecb4d4b55f034d8f3108c3ed697227ad08cdeb41f63c571cd76f146fa87ceb6
DUPLICATE_FAILED_COMMAND_BLOCKED=false
EXECUTION_STARTED=true
CHANGES_MADE=false
SANITIZED=yes
SECRETS_INCLUDED=no
PRIVATE_ADDRESSES_INCLUDED=no
RAW_EVIDENCE_SHA256=fe37d1fe51b073ce5c3e29eba30db46bb555a6d5f5b7c56be036d164d6745ba0
SANITIZED_OUTPUT_SHA256=fe37d1fe51b073ce5c3e29eba30db46bb555a6d5f5b7c56be036d164d6745ba0
OUTPUT_BEGIN
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/test
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sha256sum
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/awk
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/sed
DETAIL=LOCAL_DEPENDENCY=PASS:/usr/bin/bash
DETAIL=NGINX_HELPER_SHA256=f900b15bfdd19a1b543037cf8b9c1583e71dc8115bbb5812cf74d474e3e7859b
DETAIL=NGINX_HELPER_INTEGRITY=PASS
DETAIL=NGINX_HELPER_SYNTAX=PASS
DETAIL=NGINX_HELPER_LINE_COUNT=70
DETAIL=NGINX_HELPER_SOURCE_BEGIN
#!/usr/bin/bash
set -Eeuo pipefail

readonly VM_HELPER=/usr/local/sbin/homelab-vm130-exec
readonly VM_HELPER_SHA256=1959773393723b013af7c93cd29a7b833fd607c903d044e2fcef6accaa29c275
readonly SHA256SUM=/usr/bin/sha256sum
readonly AWK=/usr/bin/awk
readonly GREP=/usr/bin/grep
readonly MKTEMP=/usr/bin/mktemp
readonly RM=/usr/bin/rm
readonly CAT=/usr/bin/cat

if (($# == 0)); then
    echo "EDGE_NGINX_EXEC_ERROR=NO_COMMAND" >&2
    exit 64
fi

if [[ ! -x "$VM_HELPER" ]]; then
    echo "EDGE_NGINX_EXEC_ERROR=VM_HELPER_MISSING" >&2
    exit 69
fi

actual_vm_sha="$("$SHA256SUM" "$VM_HELPER" | "$AWK" '{print $1}')"
if [[ "$actual_vm_sha" != "$VM_HELPER_SHA256" ]]; then
    echo "EDGE_NGINX_EXEC_ERROR=VM_HELPER_INTEGRITY" >&2
    exit 70
fi

tmpdir="$("$MKTEMP" -d /tmp/homelab-edge-nginx-exec.XXXXXX)"
cleanup() {
    "$RM" -rf -- "$tmpdir"
}
trap cleanup EXIT

if ! "$VM_HELPER" /usr/bin/ps -eo pid=,comm=,args= >"$tmpdir/ps.out" 2>"$tmpdir/ps.err"; then
    echo "EDGE_NGINX_EXEC_ERROR=PROCESS_QUERY" >&2
    "$CAT" "$tmpdir/ps.err" >&2
    exit 71
fi

"$AWK" '$2=="nginx" && /nginx: master process/ {print $1}' "$tmpdir/ps.out" >"$tmpdir/masters"
master_count="$("$AWK" 'END{print NR+0}' "$tmpdir/masters")"
if [[ "$master_count" != 1 ]]; then
    echo "EDGE_NGINX_EXEC_ERROR=MASTER_COUNT:$master_count" >&2
    exit 72
fi

master_pid="$("$AWK" 'NR==1{print $1}' "$tmpdir/masters")"
if [[ ! "$master_pid" =~ ^[0-9]+$ ]]; then
    echo "EDGE_NGINX_EXEC_ERROR=INVALID_MASTER_PID" >&2
    exit 73
fi

if ! "$VM_HELPER" /usr/bin/cat "/proc/$master_pid/cgroup" >"$tmpdir/cgroup.out" 2>"$tmpdir/cgroup.err"; then
    echo "EDGE_NGINX_EXEC_ERROR=CGROUP_QUERY" >&2
    "$CAT" "$tmpdir/cgroup.err" >&2
    exit 74
fi

if ! "$GREP" -Eq 'docker-[0-9a-f]+[.]scope|/docker/' "$tmpdir/cgroup.out"; then
    echo "EDGE_NGINX_EXEC_ERROR=NOT_DOCKER_NGINX" >&2
    exit 75
fi

if ! "$VM_HELPER" /usr/bin/test -f "/proc/$master_pid/root/data/custom_nginx/server_proxy.conf"; then
    echo "EDGE_NGINX_EXEC_ERROR=PERSISTENT_PATH_MISSING" >&2
    exit 76
fi

exec "$VM_HELPER" /usr/bin/nsenter -t "$master_pid" -m -p -n -u -i --root --wd -- "$@"
DETAIL=NGINX_HELPER_SOURCE_END
DETAIL=NGINX_HELPER_SOURCE_TRUNCATED=NO
CHANGES_MADE=NO

OUTPUT_END
CHAT_OUTPUT_END
