Watch
1
0
Fork
You've already forked Seyyed_arc
0
forked from hesabix/arc
Seyyed_arc/scripts/lib/hesabix_deploy_env.sh
Babak Alizadeh 8bb55d80f4 fix(deploy): let the API user read .deploy_env for branding
Keep .deploy_env owner root but group-readable (640) so www-data can load BRANDING_* instead of silently falling back to default Hesabix names.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 15:40:01 +03:30

109 lines
2.9 KiB
Shell

#!/usr/bin/env bash
# Read/write Hesabix deployment env files (.deploy_env, .deploy_saved_vars).
APP_ROOT="${APP_ROOT:-/opt/hesabix}"
HESABIX_DEPLOY_ENV="${APP_ROOT}/.deploy_env"
HESABIX_DEPLOY_SAVED_VARS="${APP_ROOT}/.deploy_saved_vars"
hesabix_require_deploy_env() {
if [[ ! -f "${HESABIX_DEPLOY_ENV}" ]]; then
echo "Hesabix not deployed yet (${HESABIX_DEPLOY_ENV} missing). Run deploy.sh first." >&2
return 1
fi
}
# API process (typically www-data) must read branding keys from .deploy_env.
# Keep owner root; allow the API group to read (640), not world.
hesabix_secure_deploy_env_file() {
local file="${1:-${HESABIX_DEPLOY_ENV}}"
[[ -f "$file" ]] || return 0
local grp=""
if [[ -f "${APP_ROOT}/app/hesabixAPI/.env" ]]; then
grp="$(stat -c %G "${APP_ROOT}/app/hesabixAPI/.env" 2>/dev/null || true)"
fi
if [[ -z "$grp" ]] && getent group www-data >/dev/null 2>&1; then
grp="www-data"
fi
if [[ -n "$grp" ]]; then
chgrp "$grp" "$file" 2>/dev/null || true
chmod 640 "$file" 2>/dev/null || true
else
chmod 640 "$file" 2>/dev/null || true
fi
}
hesabix_load_deploy_env() {
hesabix_require_deploy_env || return 1
set -a
# shellcheck source=/dev/null
source "${HESABIX_DEPLOY_ENV}"
set +a
if [[ -f "${HESABIX_DEPLOY_SAVED_VARS}" ]]; then
local line key val
while IFS= read -r line; do
[[ "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]] || continue
key="${BASH_REMATCH[1]}"
val="${BASH_REMATCH[2]}"
if [[ -z "${!key:-}" ]]; then
export "${key}=${val}"
fi
done < "${HESABIX_DEPLOY_SAVED_VARS}"
fi
}
# Usage: hesabix_patch_env_file /path/to/file KEY=val KEY2=val2 ...
hesabix_patch_env_file() {
local file="$1"
shift
[[ $# -gt 0 ]] || return 0
python3 - "$file" "$@" <<'PY'
import os
import re
import sys
path = sys.argv[1]
updates = {}
for item in sys.argv[2:]:
key, _, val = item.partition("=")
if key:
updates[key] = val
lines = []
if os.path.isfile(path):
with open(path, encoding="utf-8", errors="replace") as f:
lines = f.readlines()
key_re = re.compile(r"^([A-Za-z_][A-Za-z0-9_]*)=")
out = []
done = set()
for line in lines:
m = key_re.match(line)
if m and m.group(1) in updates:
k = m.group(1)
out.append(f"{k}={updates[k]}\n")
done.add(k)
else:
out.append(line)
for k, v in updates.items():
if k not in done:
out.append(f"{k}={v}\n")
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.writelines(out)
PY
if [[ "$(basename "$file")" == ".deploy_env" ]]; then
hesabix_secure_deploy_env_file "$file"
else
chmod 600 "${file}" 2>/dev/null || true
fi
}
hesabix_validate_domain() {
local domain="$1"
if [[ ! "${domain}" =~ ^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$ ]]; then
echo "Invalid domain format: ${domain}" >&2
return 1
fi
return 0
}