diff --git a/apps/pocket-id/2.19.3-distroless/.env.sample b/apps/pocket-id/2.19.3-distroless/.env.sample new file mode 100644 index 000000000..a764ae41d --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/.env.sample @@ -0,0 +1,5 @@ +CONTAINER_NAME=pocket-id-compose-check +PANEL_APP_PORT_HTTP=1411 +POCKET_ID_APP_URL=http://localhost:1411 +POCKET_ID_ENCRYPTION_KEY=generate +POCKET_ID_TRUST_PROXY=false diff --git a/apps/pocket-id/2.19.3-distroless/data.yml b/apps/pocket-id/2.19.3-distroless/data.yml new file mode 100644 index 000000000..a787be24c --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/data.yml @@ -0,0 +1,68 @@ +additionalProperties: + formFields: + - default: 1411 + edit: true + envKey: PANEL_APP_PORT_HTTP + labelEn: HTTP Port + labelZh: HTTP 端口 + label: + en: HTTP Port + zh: HTTP 端口 + zh-Hant: HTTP 埠 + ja: HTTP ポート + ko: HTTP 포트 + ru: HTTP-порт + ms: Port HTTP + pt-br: Porta HTTP + required: true + rule: paramPort + type: number + - default: http://localhost:1411 + edit: true + envKey: POCKET_ID_APP_URL + labelEn: External App URL + labelZh: 外部访问地址 + label: + en: External App URL + zh: 外部访问地址 + zh-Hant: 外部存取網址 + ja: 外部アプリ URL + ko: 외부 앱 URL + ru: Внешний URL приложения + ms: URL aplikasi luaran + pt-br: URL externo do aplicativo + required: true + rule: paramExtUrl + type: text + - default: generate + edit: true + envKey: POCKET_ID_ENCRYPTION_KEY + labelEn: Encryption Key + labelZh: 加密密钥 + label: + en: Encryption Key + zh: 加密密钥 + zh-Hant: 加密金鑰 + ja: 暗号化キー + ko: 암호화 키 + ru: Ключ шифрования + ms: Kunci penyulitan + pt-br: Chave de criptografia + required: true + type: password + - default: 'false' + edit: true + envKey: POCKET_ID_TRUST_PROXY + labelEn: Trusted Proxy Addresses + labelZh: 可信代理地址 + label: + en: Trusted Proxy Addresses + zh: 可信代理地址 + zh-Hant: 可信代理位址 + ja: 信頼するプロキシアドレス + ko: 신뢰할 프록시 주소 + ru: Адреса доверенных прокси + ms: Alamat proksi dipercayai + pt-br: Enderecos de proxy confiaveis + required: true + type: text diff --git a/apps/pocket-id/2.19.3-distroless/data/.gitkeep b/apps/pocket-id/2.19.3-distroless/data/.gitkeep new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/data/.gitkeep @@ -0,0 +1 @@ + diff --git a/apps/pocket-id/2.19.3-distroless/docker-compose.yml b/apps/pocket-id/2.19.3-distroless/docker-compose.yml new file mode 100644 index 000000000..a5ed51720 --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/docker-compose.yml @@ -0,0 +1,27 @@ +services: + pocket-id: + image: "pocketid/pocket-id:v2.19.3-distroless" + container_name: ${CONTAINER_NAME} + restart: always + networks: + - 1panel-network + ports: + - "${PANEL_APP_PORT_HTTP}:1411" + environment: + - APP_URL=${POCKET_ID_APP_URL} + - ENCRYPTION_KEY=${POCKET_ID_ENCRYPTION_KEY} + - TRUST_PROXY=${POCKET_ID_TRUST_PROXY} + volumes: + - "./data:/app/data" + healthcheck: + test: ["CMD", "/app/pocket-id", "healthcheck"] + interval: 90s + timeout: 5s + retries: 3 + start_period: 10s + labels: + createdBy: "Apps" + +networks: + 1panel-network: + external: true diff --git a/apps/pocket-id/2.19.3-distroless/scripts/init.sh b/apps/pocket-id/2.19.3-distroless/scripts/init.sh new file mode 100755 index 000000000..4c9d49aa2 --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/scripts/init.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +ENV_FILE="${ENV_FILE:-${ROOT_DIR}/.env}" + +fail() { + printf '%s\n' "$1" >&2 + exit 1 +} + +read_env_value() { + local key="$1" + local value="" + value="$(grep -E "^${key}=" "$ENV_FILE" | tail -n 1 | cut -d '=' -f 2- || true)" + if [[ ${#value} -ge 2 && "${value:0:1}" == '"' && "${value: -1}" == '"' ]]; then + value="${value:1:${#value}-2}" + elif [[ ${#value} -ge 2 && "${value:0:1}" == "'" && "${value: -1}" == "'" ]]; then + value="${value:1:${#value}-2}" + fi + printf '%s\n' "$value" +} + +set_env_value() { + local key="$1" + local value="$2" + local temp_file + temp_file="$(mktemp "${ROOT_DIR}/.pocket-id-env.tmp.XXXXXX")" + awk -v key="$key" -v value="$value" ' + BEGIN { written = 0 } + $0 ~ "^" key "=" { + if (!written) { + print key "=" value + written = 1 + } + next + } + { print } + END { + if (!written) print key "=" value + } + ' "$ENV_FILE" > "$temp_file" + chmod 600 "$temp_file" + mv -f -- "$temp_file" "$ENV_FILE" +} + +generate_key() { + local value="" + if command -v openssl >/dev/null 2>&1; then + value="$(openssl rand -base64 32 | tr -d '\n')" + elif [[ -r /dev/urandom ]] && command -v base64 >/dev/null 2>&1; then + value="$(head -c 32 /dev/urandom | base64 | tr -d '\n')" + fi + [[ "$value" =~ ^[A-Za-z0-9+/]{43}=$ ]] || fail "Unable to generate a secure Pocket ID encryption key" + printf '%s\n' "$value" +} + +[[ -f "$ENV_FILE" ]] || fail "$ENV_FILE not found" +[[ ! -L "$ENV_FILE" ]] || fail "$ENV_FILE must not be a symbolic link" + +app_url="$(read_env_value POCKET_ID_APP_URL)" +[[ -n "$app_url" ]] || fail "POCKET_ID_APP_URL is required" +case "$app_url" in + *$'\n'* | *$'\r'* | *\\* | *'$'* | *'#'* | *'"'* | *"'") fail "POCKET_ID_APP_URL contains unsupported dotenv characters" ;; +esac + +trust_proxy="$(read_env_value POCKET_ID_TRUST_PROXY)" +[[ -n "$trust_proxy" ]] || trust_proxy=false +[[ "$trust_proxy" =~ ^(false|[0-9A-Fa-f:.,/[:space:]-]+)$ ]] || fail "POCKET_ID_TRUST_PROXY must be false or a comma-separated IP/CIDR list" + +requested_key="$(read_env_value POCKET_ID_ENCRYPTION_KEY)" +if [[ -z "$requested_key" || "$requested_key" == "generate" ]]; then + encryption_key="$(generate_key)" +else + [[ "$requested_key" =~ ^[A-Za-z0-9+/]{43}=$ ]] || fail "POCKET_ID_ENCRYPTION_KEY must be generate or a base64-encoded 32-byte key" + encryption_key="$requested_key" +fi + +data_dir="${ROOT_DIR}/data" +[[ ! -L "$data_dir" ]] || fail "Pocket ID data directory must not be a symbolic link" +mkdir -p -- "$data_dir" +if [[ "$(id -u)" -eq 0 ]]; then + chown 65532:65532 "$data_dir" + chmod 700 "$data_dir" +else + [[ "$(stat -c '%u:%g' "$data_dir")" == "65532:65532" ]] || fail "Pocket ID init must run as root to prepare the non-root data directory" + [[ "$(stat -c '%a' "$data_dir")" == "700" ]] || fail "Pocket ID data directory must have mode 700" +fi + +set_env_value POCKET_ID_APP_URL "$app_url" +set_env_value POCKET_ID_ENCRYPTION_KEY "$encryption_key" +set_env_value POCKET_ID_TRUST_PROXY "$trust_proxy" diff --git a/apps/pocket-id/2.19.3-distroless/scripts/uninstall.sh b/apps/pocket-id/2.19.3-distroless/scripts/uninstall.sh new file mode 100755 index 000000000..ab8a8d674 --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/scripts/uninstall.sh @@ -0,0 +1,2 @@ +#!/bin/bash +docker-compose down --volumes diff --git a/apps/pocket-id/2.19.3-distroless/scripts/upgrade.sh b/apps/pocket-id/2.19.3-distroless/scripts/upgrade.sh new file mode 100755 index 000000000..f990332d9 --- /dev/null +++ b/apps/pocket-id/2.19.3-distroless/scripts/upgrade.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd -P)" +ENV_FILE="${ENV_FILE:-${ROOT_DIR}/.env}" + +fail() { + printf '%s\n' "$1" >&2 + exit 1 +} + +read_env_value() { + local content="$1" + local key="$2" + local value="" + value="$(printf '%s\n' "$content" | awk -F '=' -v key="$key" '$1 == key { sub(/^[^=]*=/, ""); value = $0 } END { print value }')" + if [[ ${#value} -ge 2 && "${value:0:1}" == '"' && "${value: -1}" == '"' ]]; then + value="${value:1:${#value}-2}" + elif [[ ${#value} -ge 2 && "${value:0:1}" == "'" && "${value: -1}" == "'" ]]; then + value="${value:1:${#value}-2}" + fi + printf '%s\n' "$value" +} + +set_env_value() { + local key="$1" + local value="$2" + local temp_file + temp_file="$(mktemp "${ROOT_DIR}/.pocket-id-env.tmp.XXXXXX")" + if ! awk -v key="$key" -v value="$value" ' + BEGIN { written = 0 } + $0 ~ "^" key "=" { + if (!written) { + print key "=" value + written = 1 + } + next + } + { print } + END { + if (!written) print key "=" value + } + ' "$ENV_FILE" > "$temp_file"; then + rm -f -- "$temp_file" + fail "Unable to prepare the Pocket ID environment file" + fi + if ! chmod 600 "$temp_file" || ! mv -f -- "$temp_file" "$ENV_FILE"; then + rm -f -- "$temp_file" + fail "Unable to update the Pocket ID encryption key" + fi +} + +[[ -f "$ENV_FILE" ]] || fail "$ENV_FILE not found" +[[ ! -L "$ENV_FILE" ]] || fail "$ENV_FILE must not be a symbolic link" + +app_key="$(basename "$(dirname "$ROOT_DIR")")" +install_name="$(basename "$ROOT_DIR")" +[[ "$app_key" == "pocket-id" ]] || fail "Unexpected Pocket ID install path" +[[ -n "$install_name" && "$install_name" != "." && "$install_name" != ".." && "$install_name" != *$'\n'* && "$install_name" != *$'\r'* ]] || fail "Invalid Pocket ID install name" + +onepanel_root="$(cd "${ROOT_DIR}/../../../.." && pwd -P)" || fail "Cannot resolve the 1Panel root from the Pocket ID install path" +backup_dir="${onepanel_root}/backup/app/local${app_key}/${install_name}" +[[ -d "$backup_dir" && ! -L "$backup_dir" ]] || fail "Cannot find the pre-upgrade 1Panel backup for this Pocket ID installation; refusing to replace its encryption key" + +latest_backup="$(find "$backup_dir" -mindepth 1 -maxdepth 1 -type f -name "upgrade_backup_${install_name}_*.tar.gz" -printf '%T@ %f\n' | sort -nr | awk 'NR == 1 { print }')" +[[ -n "$latest_backup" ]] || fail "Cannot find the pre-upgrade 1Panel backup archive; refusing to replace the Pocket ID encryption key" +backup_file="${latest_backup#* }" +backup_archive="${backup_dir}/${backup_file}" +[[ -f "$backup_archive" && ! -L "$backup_archive" ]] || fail "The pre-upgrade 1Panel backup archive is unavailable; refusing to replace the Pocket ID encryption key" + +backup_name="${backup_file%.tar.gz}" +app_archive="${backup_name}/app.tar.gz" +if ! tar -tzf "$backup_archive" | grep -Fx "$app_archive" > /dev/null; then + fail "The pre-upgrade 1Panel backup does not contain the Pocket ID app archive; refusing to replace its encryption key" +fi + +env_members="$(tar -xOzf "$backup_archive" "$app_archive" | tar -tzf - | awk -v install="$install_name" '$0 == install "/.env" || $0 == "./" install "/.env" { print }')" +env_member_count="$(printf '%s\n' "$env_members" | awk 'NF { count++ } END { print count + 0 }')" +[[ "$env_member_count" == "1" ]] || fail "The pre-upgrade 1Panel backup has no unique Pocket ID .env file; refusing to replace its encryption key" +env_member="$(printf '%s\n' "$env_members" | head -n 1)" +previous_env="$(tar -xOzf "$backup_archive" "$app_archive" | tar -xOzf - -- "$env_member")" || fail "Cannot read the pre-upgrade Pocket ID .env file; refusing to replace its encryption key" +previous_key="$(read_env_value "$previous_env" POCKET_ID_ENCRYPTION_KEY)" +[[ "$previous_key" =~ ^[A-Za-z0-9+/]{43}=$ ]] || fail "The pre-upgrade Pocket ID encryption key is missing or invalid; refusing to replace it" + +requested_key="$(read_env_value "$(cat "$ENV_FILE")" POCKET_ID_ENCRYPTION_KEY)" +if [[ -z "$requested_key" || "$requested_key" == "generate" ]]; then + set_env_value POCKET_ID_ENCRYPTION_KEY "$previous_key" +elif [[ "$requested_key" != "$previous_key" ]]; then + fail "POCKET_ID_ENCRYPTION_KEY changed during upgrade; restore the previous value or use Pocket ID's supported key-rotation procedure" +fi + +exec "${SCRIPT_DIR}/init.sh"