Private
Public Access
1
0

feat: add bump-version.sh script for version management

Automates version bumps across all version source files:
- Cargo.toml (PRIMARY - workspace.package.version)
- debian/changelog (prepend new entry)
- debian/control (update Version field)
- scripts/build-package.sh (update VERSION variable)
- frontend/package.json (update version field)
- Stale references check after bump

Usage: ./scripts/bump-version.sh <new_version> <old_version>
This commit is contained in:
2026-05-28 10:52:16 -05:00
commit 124b5b0e3b
153 changed files with 41878 additions and 0 deletions

187
scripts/backup.sh Executable file
View File

@ -0,0 +1,187 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Nightly Backup Script
# =============================================================================
# Run via cron or systemd timer.
# Performs:
# 1. pg_dump of the patch_manager database
# 2. CA material backup (/etc/patch-manager/ca/)
# 3. Config backup (/etc/patch-manager/config.toml, jwt keys, tls certs)
# - Secrets are excluded unless GPG_RECIPIENT is set for encryption
# =============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $(date +%Y-%m-%dT%H:%M:%S) $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $(date +%Y-%m-%dT%H:%M:%S) $*"; }
error() { echo -e "${RED}[ERROR]${NC} $(date +%Y-%m-%dT%H:%M:%S) $*" >&2; }
# ---------------------------------------------------------------------------
# Configuration (override via environment or config file)
# ---------------------------------------------------------------------------
BACKUP_DIR="${BACKUP_DIR:-/var/backups/patch-manager}"
DB_NAME="${DB_NAME:-patch_manager}"
DB_USER="${DB_USER:-patch_manager}"
CONFIG_DIR="/etc/patch-manager"
RETENTION_DAYS="${RETENTION_DAYS:-30}"
GPG_RECIPIENT="${GPG_RECIPIENT:-}" # Set to GPG key ID to encrypt secret backups
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# ---------------------------------------------------------------------------
# Pre-flight checks
# ---------------------------------------------------------------------------
[[ $EUID -ne 0 ]] && error "This script must be run as root."
command -v pg_dump >/dev/null 2>&1 || error "pg_dump not found. Install postgresql-client."
command -v gpg >/dev/null 2>&1 || { [[ -n "${GPG_RECIPIENT}" ]] && error "GPG_RECIPIENT set but gpg not found." || true; }
mkdir -p "${BACKUP_DIR}"
chmod 700 "${BACKUP_DIR}"
BACKUP_SUCCESS=true
# ---------------------------------------------------------------------------
# 1. Database backup (pg_dump, custom format for parallel restore)
# ---------------------------------------------------------------------------
info "Starting database backup..."
DB_FILE="${BACKUP_DIR}/patch_manager_db_${TIMESTAMP}.dump"
if sudo -u postgres pg_dump -Fc -f "${DB_FILE}" "${DB_NAME}" 2>/dev/null; then
chmod 600 "${DB_FILE}"
chown root:root "${DB_FILE}"
SIZE=$(du -h "${DB_FILE}" | cut -f1)
info "Database backup complete: ${DB_FILE} (${SIZE})"
else
error "Database backup FAILED."
BACKUP_SUCCESS=false
fi
# ---------------------------------------------------------------------------
# 2. CA material backup
# ---------------------------------------------------------------------------
info "Starting CA material backup..."
CA_DIR="${CONFIG_DIR}/ca"
if [[ -d "${CA_DIR}" ]]; then
CA_FILE="${BACKUP_DIR}/patch_manager_ca_${TIMESTAMP}.tar.gz.gpg"
CA_TAR=$(mktemp /tmp/pm_ca_backup_XXXXXX.tar.gz)
tar -czf "${CA_TAR}" -C "$(dirname "${CA_DIR}")" ca/ 2>/dev/null
if [[ -n "${GPG_RECIPIENT}" ]]; then
# Encrypt with GPG — CA key is the most critical secret
if gpg --batch --encrypt --recipient "${GPG_RECIPIENT}" --output "${CA_FILE}" "${CA_TAR}" 2>/dev/null; then
chmod 600 "${CA_FILE}"
CA_SIZE=$(du -h "${CA_FILE}" | cut -f1)
info "CA backup (encrypted): ${CA_FILE} (${CA_SIZE})"
else
error "CA backup GPG encryption FAILED."
BACKUP_SUCCESS=false
fi
rm -f "${CA_TAR}"
else
# No GPG recipient — store unencrypted with strict permissions
warn "GPG_RECIPIENT not set. CA backup stored UNENCRYPTED with strict permissions."
CA_FILE_UNENCRYPTED="${BACKUP_DIR}/patch_manager_ca_${TIMESTAMP}.tar.gz"
mv "${CA_TAR}" "${CA_FILE_UNENCRYPTED}"
chmod 600 "${CA_FILE_UNENCRYPTED}"
chown root:root "${CA_FILE_UNENCRYPTED}"
CA_SIZE=$(du -h "${CA_FILE_UNENCRYPTED}" | cut -f1)
info "CA backup (unencrypted): ${CA_FILE_UNENCRYPTED} (${CA_SIZE})"
fi
else
warn "CA directory not found at ${CA_DIR}, skipping CA backup."
fi
# ---------------------------------------------------------------------------
# 3. Config backup (excluding secrets unless encrypted destination)
# ---------------------------------------------------------------------------
info "Starting config backup..."
CONFIG_FILE="${BACKUP_DIR}/patch_manager_config_${TIMESTAMP}.tar.gz"
CONFIG_FILE_GPG="${BACKUP_DIR}/patch_manager_config_${TIMESTAMP}.tar.gz.gpg"
CONFIG_TAR=$(mktemp /tmp/pm_config_backup_XXXXXX.tar.gz)
# Build file list — always include non-secret config files
CONFIG_FILES=(
"${CONFIG_DIR}/config.toml"
"${CONFIG_DIR}/jwt/verify.pem"
)
# Include TLS cert (public) if present
if [[ -f "${CONFIG_DIR}/tls/tls.crt" ]]; then
CONFIG_FILES+=("${CONFIG_DIR}/tls/tls.crt")
fi
# Build tar from existing files only
EXISTING_FILES=()
for f in "${CONFIG_FILES[@]}"; do
[[ -f "${f}" ]] && EXISTING_FILES+=("${f}")
done
if [[ ${#EXISTING_FILES[@]} -gt 0 ]]; then
tar -czf "${CONFIG_TAR}" "${EXISTING_FILES[@]}" 2>/dev/null
# If GPG_RECIPIENT is set, include secrets in the backup (encrypted)
if [[ -n "${GPG_RECIPIENT}" ]]; then
# Add secret files to a separate encrypted archive
SECRET_FILES=()
[[ -f "${CONFIG_DIR}/jwt/signing.pem" ]] && SECRET_FILES+=("${CONFIG_DIR}/jwt/signing.pem")
[[ -f "${CONFIG_DIR}/tls/tls.key" ]] && SECRET_FILES+=("${CONFIG_DIR}/tls/tls.key")
[[ -f "${CONFIG_DIR}/config.toml" ]] && SECRET_FILES+=("${CONFIG_DIR}/config.toml") # May contain DB URL
if [[ ${#SECRET_FILES[@]} -gt 0 ]]; then
# Re-create tar with secrets included
ALL_FILES=("${EXISTING_FILES[@]}" "${SECRET_FILES[@]}")
# Deduplicate
ALL_FILES_UNIQUE=( $(echo "${ALL_FILES[@]}" | tr ' ' '\n' | sort -u) )
rm -f "${CONFIG_TAR}"
tar -czf "${CONFIG_TAR}" "${ALL_FILES_UNIQUE[@]}" 2>/dev/null
fi
gpg --batch --encrypt --recipient "${GPG_RECIPIENT}" --output "${CONFIG_FILE_GPG}" "${CONFIG_TAR}" 2>/dev/null
chmod 600 "${CONFIG_FILE_GPG}"
rm -f "${CONFIG_TAR}"
CFG_SIZE=$(du -h "${CONFIG_FILE_GPG}" | cut -f1)
info "Config backup (encrypted, secrets included): ${CONFIG_FILE_GPG} (${CFG_SIZE})"
else
# No encryption — secrets excluded, only public config
mv "${CONFIG_TAR}" "${CONFIG_FILE}"
chmod 600 "${CONFIG_FILE}"
chown root:root "${CONFIG_FILE}"
CFG_SIZE=$(du -h "${CONFIG_FILE}" | cut -f1)
info "Config backup (secrets excluded): ${CONFIG_FILE} (${CFG_SIZE})"
fi
else
warn "No config files found, skipping config backup."
fi
# ---------------------------------------------------------------------------
# 4. Retention cleanup
# ---------------------------------------------------------------------------
info "Cleaning up backups older than ${RETENTION_DAYS} days..."
DELETED_COUNT=0
for pattern in "patch_manager_db_" "patch_manager_ca_" "patch_manager_config_"; do
while IFS= read -r -d '' old_file; do
rm -f "${old_file}"
((DELETED_COUNT++)) || true
done < <(find "${BACKUP_DIR}" -name "${pattern}*" -mtime +"${RETENTION_DAYS}" -print0)
done
info "Removed ${DELETED_COUNT} expired backup(s)."
# ---------------------------------------------------------------------------
# 5. Summary
# ---------------------------------------------------------------------------
if [[ "${BACKUP_SUCCESS}" == true ]]; then
info "=== Backup completed successfully ==="
info "Backup directory: ${BACKUP_DIR}"
info "Total size: $(du -sh "${BACKUP_DIR}" | cut -f1)"
info "RPO target: 24 hours (nightly schedule)"
exit 0
else
error "=== Backup completed WITH ERRORS ==="
exit 1
fi

42
scripts/build-frontend.sh Executable file
View File

@ -0,0 +1,42 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Frontend Build Script
# =============================================================================
# Builds the React + TypeScript SPA and copies output to the system frontend dir.
# Run from the repository root.
# =============================================================================
set -euo pipefail
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
FRONTEND_DIR="${REPO_ROOT}/frontend"
DEST_DIR="${1:-/usr/share/patch-manager/frontend}"
info "Building React SPA..."
cd "${FRONTEND_DIR}"
# Install dependencies if node_modules not present
if [[ ! -d node_modules ]]; then
info "Installing npm dependencies..."
npm ci
fi
# Build
info "Running vite build..."
npm run build
# Deploy to destination
info "Copying build output to ${DEST_DIR}..."
mkdir -p "${DEST_DIR}"
rm -rf "${DEST_DIR:?}/"
cp -r dist/* "${DEST_DIR}/"
info "Frontend build complete → ${DEST_DIR}"

151
scripts/build-package.sh Executable file
View File

@ -0,0 +1,151 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Build .deb Package for Ubuntu 24.04
# =============================================================================
# Produces: linux-patch-manager_1.0.0-1_amd64.deb
# Prerequisites:
# - Rust toolchain (cargo, rustc >= 1.75)
# - Node.js >= 18 + npm
# - dpkg-deb
# =============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
VERSION="0.1.9"
RELEASE="1"
PKG_NAME="linux-patch-manager"
DEB_NAME="${PKG_NAME}_${VERSION}-${RELEASE}_amd64.deb"
BUILD_DIR="${PROJECT_ROOT}/package-build"
info "=== Linux Patch Manager — Package Build ==="
info "Version: ${VERSION}-${RELEASE}"
info "Target: Ubuntu 24.04 (noble) amd64"
echo
# ---------------------------------------------------------------------------
# 1. Build Rust binaries (release mode)
# ---------------------------------------------------------------------------
info "Step 1/5: Building Rust binaries (release mode)..."
cd "${PROJECT_ROOT}"
cargo build --release 2>&1 | tail -5
# Verify binaries exist
for bin in pm-web pm-worker; do
[[ -f "${PROJECT_ROOT}/target/release/${bin}" ]] || error "${bin} not found in target/release/"
done
info "Rust binaries built successfully."
# Strip debug symbols for smaller package
for bin in pm-web pm-worker; do
strip "${PROJECT_ROOT}/target/release/${bin}" 2>/dev/null || warn "strip failed for ${bin} (may already be stripped)"
done
info "Binaries stripped."
# ---------------------------------------------------------------------------
# 2. Build frontend
# ---------------------------------------------------------------------------
info "Step 2/5: Building frontend..."
cd "${PROJECT_ROOT}/frontend"
if [[ ! -d "node_modules" ]]; then
info "Installing frontend dependencies..."
npm ci --production 2>&1 | tail -3
fi
npm run build 2>&1 | tail -5
[[ -d "${PROJECT_ROOT}/frontend/dist" ]] || error "Frontend build failed: dist/ not found"
info "Frontend built successfully."
# ---------------------------------------------------------------------------
# 3. Assemble package directory structure
# ---------------------------------------------------------------------------
info "Step 3/5: Assembling package structure..."
rm -rf "${BUILD_DIR}"
mkdir -p "${BUILD_DIR}/DEBIAN"
mkdir -p "${BUILD_DIR}/usr/local/bin"
mkdir -p "${BUILD_DIR}/usr/share/patch-manager/frontend"
mkdir -p "${BUILD_DIR}/usr/share/patch-manager/migrations"
mkdir -p "${BUILD_DIR}/lib/systemd/system"
# Binaries
cp "${PROJECT_ROOT}/target/release/pm-web" "${BUILD_DIR}/usr/local/bin/pm-web"
cp "${PROJECT_ROOT}/target/release/pm-worker" "${BUILD_DIR}/usr/local/bin/pm-worker"
cp "${PROJECT_ROOT}/scripts/backup.sh" "${BUILD_DIR}/usr/local/bin/backup.sh"
chmod 755 "${BUILD_DIR}/usr/local/bin/pm-web"
chmod 755 "${BUILD_DIR}/usr/local/bin/pm-worker"
chmod 700 "${BUILD_DIR}/usr/local/bin/backup.sh"
# Frontend
cp -r "${PROJECT_ROOT}/frontend/dist/"* "${BUILD_DIR}/usr/share/patch-manager/frontend/"
# Config example
cp "${PROJECT_ROOT}/config/config.example.toml" "${BUILD_DIR}/usr/share/patch-manager/config.example.toml"
# Migrations
cp "${PROJECT_ROOT}/migrations/"*.sql "${BUILD_DIR}/usr/share/patch-manager/migrations/"
# Systemd units
cp "${PROJECT_ROOT}/systemd/patch-manager-web.service" "${BUILD_DIR}/lib/systemd/system/"
cp "${PROJECT_ROOT}/systemd/patch-manager-worker.service" "${BUILD_DIR}/lib/systemd/system/"
cp "${PROJECT_ROOT}/systemd/patch-manager.target" "${BUILD_DIR}/lib/systemd/system/"
# DEBIAN control files
cp "${PROJECT_ROOT}/debian/control" "${BUILD_DIR}/DEBIAN/control"
cp "${PROJECT_ROOT}/debian/postinst" "${BUILD_DIR}/DEBIAN/postinst"
cp "${PROJECT_ROOT}/debian/prerm" "${BUILD_DIR}/DEBIAN/prerm"
cp "${PROJECT_ROOT}/debian/postrm" "${BUILD_DIR}/DEBIAN/postrm"
chmod 755 "${BUILD_DIR}/DEBIAN/postinst" "${BUILD_DIR}/DEBIAN/prerm" "${BUILD_DIR}/DEBIAN/postrm"
# Update Version in control file to match Cargo.toml version
sed -i "s/^Version: .*/Version: ${VERSION}-${RELEASE}/" "${BUILD_DIR}/DEBIAN/control"
# Calculate installed size (in KB)
INSTALLED_SIZE=$(du -sk "${BUILD_DIR}" | cut -f1)
sed -i "s/^Installed-Size: .*/Installed-Size: ${INSTALLED_SIZE}/" "${BUILD_DIR}/DEBIAN/control"
info "Package structure assembled (${INSTALLED_SIZE} KB)."
# ---------------------------------------------------------------------------
# 4. Build .deb package
# ---------------------------------------------------------------------------
info "Step 4/5: Building .deb package..."
dpkg-deb --build "${BUILD_DIR}" "${PROJECT_ROOT}/${DEB_NAME}"
info ".deb package created: ${DEB_NAME}"
# ---------------------------------------------------------------------------
# 5. Verify and summarize
# ---------------------------------------------------------------------------
info "Step 5/5: Verifying package..."
dpkg-deb --info "${PROJECT_ROOT}/${DEB_NAME}"
echo
dpkg-deb --contents "${PROJECT_ROOT}/${DEB_NAME}" | head -20 || true
echo
PKG_SIZE=$(du -h "${PROJECT_ROOT}/${DEB_NAME}" | cut -f1)
info "=== Package Build Complete ==="
info "Package: ${DEB_NAME}"
info "Size: ${PKG_SIZE}"
echo
echo -e "${CYAN}Installation instructions:${NC}"
echo " 1. Copy ${DEB_NAME} to the target Ubuntu 24.04 host"
echo " 2. Install: sudo dpkg -i ${DEB_NAME}"
echo " 3. Or with auto-deps: sudo apt install ./${DEB_NAME}"
echo " 4. Configure database URL in /etc/patch-manager/config.toml"
echo " 5. Start: systemctl enable --now patch-manager.target"
echo
# Cleanup build directory
rm -rf "${BUILD_DIR}"
info "Build directory cleaned up."

82
scripts/bump-version.sh Executable file
View File

@ -0,0 +1,82 @@
#!/bin/bash
# Bump version across all version source files for linux_patch_manager
# Usage: ./scripts/bump-version.sh <new_version> <old_version>
# Example: ./scripts/bump-version.sh 0.1.10 0.1.9
set -euo pipefail
NEW_VERSION="${1:?Usage: bump-version.sh <new_version> <old_version>}"
OLD_VERSION="${2:?Usage: bump-version.sh <new_version> <old_version>}"
REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$REPO_DIR"
echo "=== Bumping version from $OLD_VERSION to $NEW_VERSION ==="
echo ""
# 1. Cargo.toml (PRIMARY - workspace.package.version)
sed -i "s/version = \"$OLD_VERSION\"/version = \"$NEW_VERSION\"/" Cargo.toml
echo "[1/5] Cargo.toml: $OLD_VERSION -> $NEW_VERSION"
# 2. debian/changelog - Prepend new entry using temp file
TEMP_CHANGELOG=$(mktemp)
echo "linux-patch-manager ($NEW_VERSION-1) unstable; urgency=low" > "$TEMP_CHANGELOG"
echo "" >> "$TEMP_CHANGELOG"
echo " * Release v$NEW_VERSION" >> "$TEMP_CHANGELOG"
echo "" >> "$TEMP_CHANGELOG"
echo " -- git-echo <git-echo@moon-dragon.us> $(date -R)" >> "$TEMP_CHANGELOG"
echo "" >> "$TEMP_CHANGELOG"
cat debian/changelog >> "$TEMP_CHANGELOG"
mv "$TEMP_CHANGELOG" debian/changelog
echo "[2/5] debian/changelog: Added entry for $NEW_VERSION"
# 3. debian/control - Update Version field
if grep -q "^Version:" debian/control 2>/dev/null; then
sed -i "s/^Version: .*/Version: $NEW_VERSION-1/" debian/control
echo "[3/5] debian/control: -> $NEW_VERSION-1"
else
echo "[3/5] debian/control: Version field not found, skipping"
fi
# 4. scripts/build-package.sh - Update VERSION variable
if [ -f scripts/build-package.sh ]; then
sed -i "s/^VERSION=\".*\"/VERSION=\"$NEW_VERSION\"/" scripts/build-package.sh
echo "[4/5] scripts/build-package.sh: -> $NEW_VERSION"
else
echo "[4/5] scripts/build-package.sh: Not found, skipping"
fi
# 5. frontend/package.json - Update version field
if [ -f frontend/package.json ]; then
sed -i "s/\"version\": \"[^\"]*\"/\"version\": \"$NEW_VERSION\"/" frontend/package.json
echo "[5/5] frontend/package.json: -> $NEW_VERSION"
else
echo "[5/5] frontend/package.json: Not found, skipping"
fi
echo ""
echo "=== Version bump complete ==="
echo ""
echo "Verification:"
echo " Cargo.toml: $(grep '^version' Cargo.toml | head -1)"
echo " debian/changelog: $(head -1 debian/changelog)"
if [ -f debian/control ]; then
echo " debian/control: $(grep '^Version' debian/control)"
fi
if [ -f scripts/build-package.sh ]; then
echo " build-package.sh: $(grep '^VERSION=' scripts/build-package.sh)"
fi
if [ -f frontend/package.json ]; then
echo " frontend/package.json: $(grep '"version"' frontend/package.json | head -1)"
fi
echo ""
echo "Stale references check:"
grep -r "$OLD_VERSION" --include='*.toml' --include='*.sh' --include='*.json' --include='control' . 2>/dev/null | grep -v 'target/' | grep -v '.git/' | grep -v 'node_modules/' | grep -v 'bump-version.sh' || echo " No stale references found"
echo ""
echo "Next steps:"
echo " 1. Review changes: git diff"
echo " 2. Commit: git commit -am 'chore: bump version to $NEW_VERSION'"
echo " 3. Push: git push origin master"
echo " 4. Tag: git tag v$NEW_VERSION && git push origin v$NEW_VERSION"
echo " 5. Create release via Gitea API"

117
scripts/create-release.py Executable file
View File

@ -0,0 +1,117 @@
#!/usr/bin/env python3
"""Create a Gitea release and upload a .deb package as an asset.
Usage:
python3 create-release.py --tag v1.0.0 --deb linux-patch-manager_1.0.0-1_amd64.deb
Environment variables:
GITEA_TOKEN - API token (required, falls back to GITHUB_TOKEN)
GITEA_URL - Gitea base URL (default: http://192.168.2.189:3000)
GITEA_REPO - Repository path (default: git-echo/linux_patch_manager)
"""
import argparse
import json
import os
import sys
import urllib.request
import urllib.error
def create_release(base_url, repo, token, tag, title, body):
url = f"{base_url}/api/v1/repos/{repo}/releases"
data = json.dumps({
"tag_name": tag,
"title": title,
"body": body,
}).encode()
req = urllib.request.Request(
url,
data=data,
headers={
"Authorization": f"token {token}",
"Content-Type": "application/json",
},
method="POST",
)
try:
resp = urllib.request.urlopen(req)
result = json.loads(resp.read())
return result
except urllib.error.HTTPError as e:
print(f"ERROR: Failed to create release (HTTP {e.code}): {e.read().decode()[:500]}")
sys.exit(1)
def upload_asset(base_url, repo, token, release_id, deb_path):
url = f"{base_url}/api/v1/repos/{repo}/releases/{release_id}/assets"
filename = os.path.basename(deb_path)
boundary = "----FormBoundary7MA4YWxkTrZu0gW"
with open(deb_path, "rb") as f:
deb_data = f.read()
body = (
f"--{boundary}\r\n"
f'Content-Disposition: form-data; name="attachment"; filename="{filename}"\r\n'
f"Content-Type: application/octet-stream\r\n\r\n"
).encode() + deb_data + (
f"\r\n--{boundary}\r\n"
f'Content-Disposition: form-data; name="name"\r\n\r\n'
f"{filename}\r\n"
f"--{boundary}--\r\n"
).encode()
req = urllib.request.Request(
url,
data=body,
headers={
"Authorization": f"token {token}",
"Content-Type": f"multipart/form-data; boundary={boundary}",
},
method="POST",
)
try:
resp = urllib.request.urlopen(req)
result = json.loads(resp.read())
return result
except urllib.error.HTTPError as e:
print(f"ERROR: Failed to upload asset (HTTP {e.code}): {e.read().decode()[:500]}")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description="Create Gitea release with .deb asset")
parser.add_argument("--tag", required=True, help="Tag name (e.g. v1.0.0)")
parser.add_argument("--deb", required=True, help="Path to .deb file")
parser.add_argument("--version", required=True, help="Version string")
args = parser.parse_args()
token = os.environ.get("GITEA_TOKEN", os.environ.get("GITHUB_TOKEN", ""))
if not token:
print("ERROR: GITEA_TOKEN or GITHUB_TOKEN not set")
sys.exit(1)
base_url = os.environ.get("GITEA_URL", "http://192.168.2.189:3000")
repo = os.environ.get("GITEA_REPO", "git-echo/linux_patch_manager")
title = f"Release {args.version}"
body = (
f"Automated build from tag {args.tag}.\n\n"
f"## Installation\n\n"
f"```bash\n"
f"sudo apt install ./{os.path.basename(args.deb)}\n"
f"```\n"
)
print(f"Creating release for tag: {args.tag} repo: {repo}")
print(f"DEB: {args.deb}")
release = create_release(base_url, repo, token, args.tag, title, body)
release_id = release["id"]
print(f"Release created! ID: {release_id}")
asset = upload_asset(base_url, repo, token, release_id, args.deb)
print(f"Upload SUCCESS! Asset ID: {asset.get('id')}")
print(f"Download URL: {asset.get('browser_download_url', 'N/A')}")
print("Release upload complete")
if __name__ == "__main__":
main()

27
scripts/git-hooks/install.sh Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Git Hooks Installer
# =============================================================================
# Installs pre-commit and pre-push hooks into .git/hooks/
# Run from repo root: ./scripts/git-hooks/install.sh
# =============================================================================
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel)"
HOOKS_DIR="${REPO_ROOT}/.git/hooks"
SOURCE_DIR="${REPO_ROOT}/scripts/git-hooks"
echo "Installing git hooks from ${SOURCE_DIR} ..."
for hook in pre-commit pre-push; do
if [[ -f "${SOURCE_DIR}/${hook}" ]]; then
cp "${SOURCE_DIR}/${hook}" "${HOOKS_DIR}/${hook}"
chmod +x "${HOOKS_DIR}/${hook}"
echo " ✓ Installed ${hook}"
else
echo " ⚠ Skipped ${hook} (not found)"
fi
done
echo "Done. Hooks will run automatically on commit and push."

36
scripts/git-hooks/pre-commit Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Pre-Commit Hook
# =============================================================================
# Auto-formats Rust code and runs frontend checks before each commit.
# Prevents CI format-check and lint failures by catching issues locally.
# Install: ./scripts/git-hooks/install.sh
# =============================================================================
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel)"
# ── Rust auto-format ──────────────────────────────────────────────────────────
if [[ -f "${REPO_ROOT}/Cargo.toml" ]]; then
echo "[pre-commit] Running cargo fmt --all ..."
cargo fmt --all --manifest-path "${REPO_ROOT}/Cargo.toml" 2>/dev/null
# Re-stage any files that cargo fmt reformatted
STAGED_RS=$(git diff --name-only --diff-filter=ACM -- '*.rs')
if [[ -n "${STAGED_RS}" ]]; then
git add ${STAGED_RS}
fi
fi
# ── Frontend checks ──────────────────────────────────────────────────────────
if [[ -f "${REPO_ROOT}/frontend/package.json" ]]; then
echo "[pre-commit] Running ESLint ..."
cd "${REPO_ROOT}/frontend"
npx eslint src/ --ext .ts,.tsx --max-warnings 0 2>/dev/null
echo "[pre-commit] Running TypeScript type check ..."
npx tsc --noEmit 2>/dev/null
fi
echo "[pre-commit] All checks passed ✓"

46
scripts/git-hooks/pre-push Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Pre-Push Hook
# =============================================================================
# Safety net: verifies cargo fmt, ESLint, and TypeScript pass before pushing.
# Install: ./scripts/git-hooks/install.sh
# =============================================================================
set -euo pipefail
REPO_ROOT="$(git rev-parse --show-toplevel)"
FAILED=0
# ── Rust format check ────────────────────────────────────────────────────────
if [[ -f "${REPO_ROOT}/Cargo.toml" ]]; then
echo "[pre-push] Checking Rust formatting ..."
if ! cargo fmt --all --manifest-path "${REPO_ROOT}/Cargo.toml" --check 2>/dev/null; then
echo "[pre-push] ❌ Rust formatting check FAILED. Run: cargo fmt --all"
FAILED=1
fi
fi
# ── Frontend ESLint ──────────────────────────────────────────────────────────
if [[ -f "${REPO_ROOT}/frontend/package.json" ]]; then
echo "[pre-push] Running ESLint ..."
if ! (cd "${REPO_ROOT}/frontend" && npx eslint src/ --ext .ts,.tsx --max-warnings 0 2>/dev/null); then
echo "[pre-push] ❌ ESLint check FAILED."
FAILED=1
fi
fi
# ── Frontend type check ─────────────────────────────────────────────────────
if [[ -f "${REPO_ROOT}/frontend/package.json" ]]; then
echo "[pre-push] Checking TypeScript types ..."
if ! (cd "${REPO_ROOT}/frontend" && npx tsc --noEmit 2>/dev/null); then
echo "[pre-push] ❌ TypeScript type check FAILED."
FAILED=1
fi
fi
if [[ ${FAILED} -ne 0 ]]; then
echo "[pre-push] Push rejected — fix errors above before pushing."
exit 1
fi
echo "[pre-push] All checks passed ✓"

488
scripts/integration-test.sh Executable file
View File

@ -0,0 +1,488 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — End-to-End Integration Test Suite
# =============================================================================
# Tests the full patch lifecycle across multiple simulated agents.
# Prerequisites:
# - pm-web and pm-worker running
# - At least 2 test agents registered (or use --mock mode)
# - JWT token with admin role
# =============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
PASS=0
FAIL=0
SKIP=0
BASE_URL="${BASE_URL:-https://localhost}"
ADMIN_USER="${ADMIN_USER:-admin}"
ADMIN_PASS="${ADMIN_PASS:-admin}" # Default seed password; change for production
info() { echo -e "${CYAN}[TEST]${NC} $*"; }
pass() { echo -e "${GREEN}[PASS]${NC} $*"; ((PASS++)); }
fail() { echo -e "${RED}[FAIL]${NC} $*"; ((FAIL++)); }
skip() { echo -e "${YELLOW}[SKIP]${NC} $*"; ((SKIP++)); }
# ---------------------------------------------------------------------------
# Helper: API call with JWT
# ---------------------------------------------------------------------------
JWT_TOKEN=""
REFRESH_TOKEN=""
api_call() {
local method="$1" endpoint="$2" shift; shift
curl -sk -X "${method}" "${BASE_URL}${endpoint}" \
-H "Authorization: Bearer ${JWT_TOKEN}" \
-H "Content-Type: application/json" \
"$@"
}
api_call_no_auth() {
local method="$1" endpoint="$2" shift; shift
curl -sk -X "${method}" "${BASE_URL}${endpoint}" \
-H "Content-Type: application/json" \
"$@"
}
# ---------------------------------------------------------------------------
# Suite 1: Authentication Flow
# ---------------------------------------------------------------------------
test_auth_flow() {
echo -e "\n${CYAN}=== Suite 1: Authentication Flow ===${NC}"
# 1.1 Login with password
info "1.1 Login with password"
LOGIN_RESP=$(api_call_no_auth POST /api/v1/auth/login \
-d "{\"username\": \"${ADMIN_USER}\", \"password\": \"${ADMIN_PASS}\"}")
if echo "${LOGIN_RESP}" | grep -q '"access_token"'; then
JWT_TOKEN=$(echo "${LOGIN_RESP}" | grep -oP '"access_token":"[^"]+"' | cut -d'"' -f4)
REFRESH_TOKEN=$(echo "${LOGIN_RESP}" | grep -oP '"refresh_token":"[^"]+"' | cut -d'"' -f4)
pass "1.1 Login successful, JWT obtained"
else
fail "1.1 Login failed: ${LOGIN_RESP}"
return 1
fi
# 1.2 Access protected endpoint
info "1.2 Access protected endpoint (fleet status)"
STATUS=$(api_call GET /api/v1/status/fleet -o /dev/null -w '%{http_code}')
if [[ "${STATUS}" == "200" ]]; then
pass "1.2 Protected endpoint accessible with JWT"
else
fail "1.2 Protected endpoint returned ${STATUS}"
fi
# 1.3 Refresh token rotation
info "1.3 Refresh token rotation"
REFRESH_RESP=$(api_call_no_auth POST /api/v1/auth/refresh \
-d "{\"refresh_token\": \"${REFRESH_TOKEN}\"}")
if echo "${REFRESH_RESP}" | grep -q '"access_token"'; then
NEW_REFRESH=$(echo "${REFRESH_RESP}" | grep -oP '"refresh_token":"[^"]+"' | cut -d'"' -f4)
JWT_TOKEN=$(echo "${REFRESH_RESP}" | grep -oP '"access_token":"[^"]+"' | cut -d'"' -f4)
if [[ "${NEW_REFRESH}" != "${REFRESH_TOKEN}" ]]; then
REFRESH_TOKEN="${NEW_REFRESH}"
pass "1.3 Refresh token rotated (new token issued)"
else
fail "1.3 Refresh token NOT rotated (security issue)"
fi
else
fail "1.3 Token refresh failed"
fi
# 1.4 Old refresh token rejected
info "1.4 Old refresh token rejected"
OLD_RESP=$(api_call_no_auth POST /api/v1/auth/refresh \
-d "{\"refresh_token\": \"${REFRESH_TOKEN}\"}" 2>/dev/null || true)
# After rotation, the old token should still work (it's the new one now)
# Re-test: use the first token after getting a second rotation
SECOND_REFRESH=$(api_call_no_auth POST /api/v1/auth/refresh \
-d "{\"refresh_token\": \"${REFRESH_TOKEN}\"}")
if echo "${SECOND_REFRESH}" | grep -q '"access_token"'; then
JWT_TOKEN=$(echo "${SECOND_REFRESH}" | grep -oP '"access_token":"[^"]+"' | cut -d'"' -f4)
REFRESH_TOKEN=$(echo "${SECOND_REFRESH}" | grep -oP '"refresh_token":"[^"]+"' | cut -d'"' -f4)
pass "1.4 Token rotation chain works correctly"
else
fail "1.4 Token rotation chain broken"
fi
# 1.5 RBAC enforcement
info "1.5 RBAC: unauthenticated request rejected"
UNAUTH=$(curl -sk -o /dev/null -w '%{http_code}' "${BASE_URL}/api/v1/hosts")
if [[ "${UNAUTH}" == "401" ]]; then
pass "1.5 Unauthenticated request returns 401"
else
fail "1.5 Unauthenticated request returned ${UNAUTH} (expected 401)"
fi
}
# ---------------------------------------------------------------------------
# Suite 2: Host Management
# ---------------------------------------------------------------------------
test_host_management() {
echo -e "\n${CYAN}=== Suite 2: Host Management ===${NC}"
# 2.1 List hosts
info "2.1 List hosts"
HOSTS_RESP=$(api_call GET "/api/v1/hosts")
HOST_COUNT=$(echo "${HOSTS_RESP}" | grep -oP '"total":\K[0-9]+' || echo "0")
pass "2.1 Hosts list retrieved (${HOST_COUNT} hosts)"
# 2.2 Add a test host
info "2.2 Add test host"
ADD_RESP=$(api_call POST /api/v1/hosts \
-d '{"fqdn": "test-agent-01.example.com", "ip_address": "10.0.0.101"}')
if echo "${ADD_RESP}" | grep -q '"id"'; then
TEST_HOST_ID=$(echo "${ADD_RESP}" | grep -oP '"id":\K[0-9a-f-]+' | head -1)
pass "2.2 Test host added (ID: ${TEST_HOST_ID})"
else
fail "2.2 Failed to add test host: ${ADD_RESP}"
TEST_HOST_ID=""
fi
# 2.3 Get host detail
if [[ -n "${TEST_HOST_ID}" ]]; then
info "2.3 Get host detail"
DETAIL=$(api_call GET "/api/v1/hosts/${TEST_HOST_ID}" -o /dev/null -w '%{http_code}')
if [[ "${DETAIL}" == "200" ]]; then
pass "2.3 Host detail retrieved"
else
fail "2.3 Host detail returned ${DETAIL}"
fi
else
skip "2.3 Host detail (no host ID)"
fi
# 2.4 Group management
info "2.4 Create test group"
GROUP_RESP=$(api_call POST /api/v1/groups \
-d '{"name": "integration-test-group", "description": "Integration test group"}')
if echo "${GROUP_RESP}" | grep -q '"id"'; then
TEST_GROUP_ID=$(echo "${GROUP_RESP}" | grep -oP '"id":\K[0-9a-f-]+' | head -1)
pass "2.4 Test group created (ID: ${TEST_GROUP_ID})"
else
fail "2.4 Failed to create group: ${GROUP_RESP}"
TEST_GROUP_ID=""
fi
# 2.5 Assign host to group
if [[ -n "${TEST_HOST_ID}" && -n "${TEST_GROUP_ID}" ]]; then
info "2.5 Assign host to group"
ASSIGN=$(api_call POST "/api/v1/hosts/${TEST_HOST_ID}/groups" \
-d "{\"group_id\": \"${TEST_GROUP_ID}\"}" -o /dev/null -w '%{http_code}')
if [[ "${ASSIGN}" == "200" || "${ASSIGN}" == "201" ]]; then
pass "2.5 Host assigned to group"
else
fail "2.5 Group assignment returned ${ASSIGN}"
fi
else
skip "2.5 Group assignment (missing host or group ID)"
fi
}
# ---------------------------------------------------------------------------
# Suite 3: Patch Job Lifecycle
# ---------------------------------------------------------------------------
test_patch_lifecycle() {
echo -e "\n${CYAN}=== Suite 3: Patch Job Lifecycle ===${NC}"
# 3.1 Create a patch job (queue for maintenance window)
info "3.1 Create patch job (queued)"
JOB_RESP=$(api_call POST /api/v1/jobs \
-d '{"host_ids": [], "action": "apply", "schedule": "queue", "description": "Integration test job"}')
if echo "${JOB_RESP}" | grep -q '"id"'; then
TEST_JOB_ID=$(echo "${JOB_RESP}" | grep -oP '"id":\K[0-9a-f-]+' | head -1)
pass "3.1 Patch job created (ID: ${TEST_JOB_ID})"
else
# May fail if no hosts available — that's acceptable in test
warn_msg=$(echo "${JOB_RESP}" | head -c 200)
skip "3.1 Patch job creation: ${warn_msg}"
TEST_JOB_ID=""
fi
# 3.2 List jobs
info "3.2 List jobs"
JOBS_LIST=$(api_call GET /api/v1/jobs -o /dev/null -w '%{http_code}')
if [[ "${JOBS_LIST}" == "200" ]]; then
pass "3.2 Jobs list retrieved"
else
fail "3.2 Jobs list returned ${JOBS_LIST}"
fi
# 3.3 Get job detail
if [[ -n "${TEST_JOB_ID}" ]]; then
info "3.3 Get job detail"
JOB_DETAIL=$(api_call GET "/api/v1/jobs/${TEST_JOB_ID}" -o /dev/null -w '%{http_code}')
if [[ "${JOB_DETAIL}" == "200" ]]; then
pass "3.3 Job detail retrieved"
else
fail "3.3 Job detail returned ${JOB_DETAIL}"
fi
else
skip "3.3 Job detail (no job ID)"
fi
# 3.4 Rollback attempt (should fail or succeed depending on job state)
if [[ -n "${TEST_JOB_ID}" ]]; then
info "3.4 Rollback job"
ROLLBACK=$(api_call POST "/api/v1/jobs/${TEST_JOB_ID}/rollback" -o /dev/null -w '%{http_code}')
if [[ "${ROLLBACK}" == "200" || "${ROLLBACK}" == "409" || "${ROLLBACK}" == "422" ]]; then
pass "3.4 Rollback endpoint responds (${ROLLBACK})"
else
fail "3.4 Rollback returned unexpected ${ROLLBACK}"
fi
else
skip "3.4 Rollback (no job ID)"
fi
}
# ---------------------------------------------------------------------------
# Suite 4: Maintenance Windows
# ---------------------------------------------------------------------------
test_maintenance_windows() {
echo -e "\n${CYAN}=== Suite 4: Maintenance Windows ===${NC}"
if [[ -z "${TEST_HOST_ID}" ]]; then
skip "4.1-4.3 Maintenance windows (no test host)"
return
fi
# 4.1 Create maintenance window
info "4.1 Create one-time maintenance window"
TOMORROW=$(date -u -d '+1 day' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v+1d +%Y-%m-%dT%H:%M:%SZ)
DAY_AFTER=$(date -u -d '+2 days' +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v+2d +%Y-%m-%dT%H:%M:%SZ)
MW_RESP=$(api_call POST "/api/v1/hosts/${TEST_HOST_ID}/maintenance-windows" \
-d "{\"schedule_type\": \"one-time\", \"start_time\": \"${TOMORROW}\", \"end_time\": \"${DAY_AFTER}\"}")
if echo "${MW_RESP}" | grep -q '"id"'; then
pass "4.1 Maintenance window created"
else
fail "4.1 Maintenance window creation failed: ${MW_RESP}"
fi
# 4.2 List maintenance windows
info "4.2 List maintenance windows for host"
MW_LIST=$(api_call GET "/api/v1/hosts/${TEST_HOST_ID}/maintenance-windows" -o /dev/null -w '%{http_code}')
if [[ "${MW_LIST}" == "200" ]]; then
pass "4.2 Maintenance windows list retrieved"
else
fail "4.2 Maintenance windows list returned ${MW_LIST}"
fi
}
# ---------------------------------------------------------------------------
# Suite 5: Reporting
# ---------------------------------------------------------------------------
test_reporting() {
echo -e "\n${CYAN}=== Suite 5: Reporting ===${NC}"
for report_type in compliance patch-history vulnerability audit; do
info "5.x Report: ${report_type} (CSV)"
CSV_STATUS=$(api_call GET "/api/v1/reports/${report_type}?format=csv" -o /dev/null -w '%{http_code}')
if [[ "${CSV_STATUS}" == "200" ]]; then
pass "Report ${report_type} CSV generated"
else
fail "Report ${report_type} CSV returned ${CSV_STATUS}"
fi
info "5.x Report: ${report_type} (PDF)"
PDF_STATUS=$(api_call GET "/api/v1/reports/${report_type}?format=pdf" -o /dev/null -w '%{http_code}')
if [[ "${PDF_STATUS}" == "200" ]]; then
pass "Report ${report_type} PDF generated"
else
fail "Report ${report_type} PDF returned ${PDF_STATUS}"
fi
done
}
# ---------------------------------------------------------------------------
# Suite 6: Settings & Configuration
# ---------------------------------------------------------------------------
test_settings() {
echo -e "\n${CYAN}=== Suite 6: Settings & Configuration ===${NC}"
# 6.1 Get settings
info "6.1 Get current settings"
SETTINGS=$(api_call GET /api/v1/settings -o /dev/null -w '%{http_code}')
if [[ "${SETTINGS}" == "200" ]]; then
pass "6.1 Settings retrieved"
else
fail "6.1 Settings returned ${SETTINGS}"
fi
# 6.2 Test SMTP connection (should fail gracefully without real SMTP)
info "6.2 SMTP test (expected failure without real server)"
SMTP_TEST=$(api_call POST /api/v1/settings/smtp/test -o /dev/null -w '%{http_code}')
if [[ "${SMTP_TEST}" == "200" || "${SMTP_TEST}" == "502" || "${SMTP_TEST}" == "422" ]]; then
pass "6.2 SMTP test endpoint responds (${SMTP_TEST})"
else
fail "6.2 SMTP test returned unexpected ${SMTP_TEST}"
fi
}
# ---------------------------------------------------------------------------
# Suite 7: Certificate Management
# ---------------------------------------------------------------------------
test_certificates() {
echo -e "\n${CYAN}=== Suite 7: Certificate Management ===${NC}"
# 7.1 Download root CA cert
info "7.1 Download root CA certificate"
CA_STATUS=$(api_call GET /api/v1/ca/root.crt -o /dev/null -w '%{http_code}')
if [[ "${CA_STATUS}" == "200" ]]; then
pass "7.1 Root CA certificate downloadable"
else
fail "7.1 Root CA cert returned ${CA_STATUS}"
fi
# 7.2 Client cert download (if test host exists)
if [[ -n "${TEST_HOST_ID}" ]]; then
info "7.2 Download client certificate for test host"
CERT_STATUS=$(api_call GET "/api/v1/hosts/${TEST_HOST_ID}/client.crt" -o /dev/null -w '%{http_code}')
if [[ "${CERT_STATUS}" == "200" || "${CERT_STATUS}" == "404" ]]; then
pass "7.2 Client cert endpoint responds (${CERT_STATUS})"
else
fail "7.2 Client cert returned ${CERT_STATUS}"
fi
else
skip "7.2 Client cert (no test host)"
fi
}
# ---------------------------------------------------------------------------
# Suite 8: Audit Logging
# ---------------------------------------------------------------------------
test_audit_logging() {
echo -e "\n${CYAN}=== Suite 8: Audit Logging ===${NC}"
# 8.1 Audit trail report includes recent operations
info "8.1 Audit trail contains recent operations"
AUDIT_RESP=$(api_call GET "/api/v1/reports/audit?format=csv")
if echo "${AUDIT_RESP}" | grep -qi "login\|host\|group\|job"; then
pass "8.1 Audit trail contains operation records"
else
# May be empty in fresh install
pass "8.1 Audit trail endpoint functional (may be empty in fresh install)"
fi
# 8.2 Audit integrity verification
info "8.2 Audit integrity verification"
INTEGRITY=$(api_call POST /api/v1/reports/audit/verify -o /dev/null -w '%{http_code}')
if [[ "${INTEGRITY}" == "200" ]]; then
pass "8.2 Audit integrity verification passed"
else
fail "8.2 Audit integrity returned ${INTEGRITY}"
fi
}
# ---------------------------------------------------------------------------
# Suite 9: WebSocket Relay
# ---------------------------------------------------------------------------
test_websocket() {
echo -e "\n${CYAN}=== Suite 9: WebSocket Relay ===${NC}"
# 9.1 Create WS ticket
info "9.1 Create WebSocket ticket"
TICKET_RESP=$(api_call POST /api/v1/ws/ticket)
if echo "${TICKET_RESP}" | grep -q '"ticket"'; then
pass "9.1 WebSocket ticket created"
else
fail "9.1 WebSocket ticket creation failed"
fi
# Note: Full WS testing requires a WebSocket client (e.g., wscat)
# This is a basic connectivity check
info "9.2 WebSocket connection test (requires wscat - skipped in CI)"
if command -v wscat &>/dev/null; then
WS_TICKET=$(echo "${TICKET_RESP}" | grep -oP '"ticket":"[^"]+"' | cut -d'"' -f4)
WS_RESULT=$(timeout 5 wscat -c "${BASE_URL}/api/v1/ws/jobs?ticket=${WS_TICKET}" --no-color 2>&1 || true)
if echo "${WS_RESULT}" | grep -qi "connected"; then
pass "9.2 WebSocket connection established"
else
fail "9.2 WebSocket connection failed: ${WS_RESULT}"
fi
else
skip "9.2 WebSocket connection (wscat not installed)"
fi
}
# ---------------------------------------------------------------------------
# Cleanup
# ---------------------------------------------------------------------------
cleanup() {
echo -e "\n${CYAN}=== Cleanup ===${NC}"
# Delete test host
if [[ -n "${TEST_HOST_ID:-}" ]]; then
info "Removing test host ${TEST_HOST_ID}"
api_call DELETE "/api/v1/hosts/${TEST_HOST_ID}" -o /dev/null 2>/dev/null || true
fi
# Delete test group
if [[ -n "${TEST_GROUP_ID:-}" ]]; then
info "Removing test group ${TEST_GROUP_ID}"
api_call DELETE "/api/v1/groups/${TEST_GROUP_ID}" -o /dev/null 2>/dev/null || true
fi
# Logout
if [[ -n "${REFRESH_TOKEN:-}" ]]; then
api_call_no_auth POST /api/v1/auth/logout \
-d "{\"refresh_token\": \"${REFRESH_TOKEN}\"}" -o /dev/null 2>/dev/null || true
fi
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
trap cleanup EXIT
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN}Linux Patch Manager — Integration Tests${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e "Target: ${BASE_URL}"
echo -e "Time: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo
# Health check first
info "Pre-flight: Health check"
HEALTH=$(curl -sk -o /dev/null -w '%{http_code}' "${BASE_URL}/status/health")
if [[ "${HEALTH}" != "200" ]]; then
fail "Pre-flight: Health check returned ${HEALTH}. Aborting."
echo -e "\n${RED}Ensure pm-web and pm-worker are running.${NC}"
exit 1
fi
pass "Pre-flight: Health check passed"
test_auth_flow
test_host_management
test_patch_lifecycle
test_maintenance_windows
test_reporting
test_settings
test_certificates
test_audit_logging
test_websocket
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo -e "\n${CYAN}========================================${NC}"
echo -e "${CYAN}Integration Test Summary${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e " ${GREEN}PASS${NC}: ${PASS}"
echo -e " ${RED}FAIL${NC}: ${FAIL}"
echo -e " ${YELLOW}SKIP${NC}: ${SKIP}"
echo -e " ${CYAN}TOTAL${NC}: $((PASS + FAIL + SKIP))"
if [[ ${FAIL} -eq 0 ]]; then
echo -e "\n${GREEN}All tests passed!${NC}"
exit 0
else
echo -e "\n${RED}${FAIL} test(s) failed.${NC}"
exit 1
fi

348
scripts/performance-test.sh Executable file
View File

@ -0,0 +1,348 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Performance Test Suite
# =============================================================================
# Validates NFR targets:
# - 500-host polling completes within health interval
# - Dashboard load < 5 seconds
# - CIDR scan < 10 seconds for /22
# - API response times under load
# Prerequisites:
# - pm-web and pm-worker running
# - JWT admin token (auto-obtained)
# - curl with timing support
# =============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
BASE_URL="${BASE_URL:-https://localhost}"
ADMIN_USER="${ADMIN_USER:-admin}"
ADMIN_PASS="${ADMIN_PASS:-admin}"
PASS=0
FAIL=0
SKIP=0
THRESHOLD_DASHBOARD=5.0 # seconds
THRESHOLD_CIDR=10.0 # seconds
THRESHOLD_API=2.0 # seconds for individual API calls
THRESHOLD_REPORTS=10.0 # seconds for report generation
info() { echo -e "${CYAN}[PERF]${NC} $*"; }
pass() { echo -e "${GREEN}[PASS]${NC} $*"; ((PASS++)); }
fail() { echo -e "${RED}[FAIL]${NC} $*"; ((FAIL++)); }
skip() { echo -e "${YELLOW}[SKIP]${NC} $*"; ((SKIP++)); }
# ---------------------------------------------------------------------------
# Authenticate
# ---------------------------------------------------------------------------
JWT_TOKEN=""
authenticate() {
info "Authenticating as ${ADMIN_USER}..."
LOGIN_RESP=$(curl -sk -X POST "${BASE_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d "{\"username\": \"${ADMIN_USER}\", \"password\": \"${ADMIN_PASS}\"}")
if echo "${LOGIN_RESP}" | grep -q '"access_token"'; then
JWT_TOKEN=$(echo "${LOGIN_RESP}" | grep -oP '"access_token":"[^"]+"' | cut -d'"' -f4)
pass "Authentication successful"
else
fail "Authentication failed: ${LOGIN_RESP}"
exit 1
fi
}
api_call() {
local method="$1" endpoint="$2" shift; shift
curl -sk -X "${method}" "${BASE_URL}${endpoint}" \
-H "Authorization: Bearer ${JWT_TOKEN}" \
-H "Content-Type: application/json" \
"$@"
}
# ---------------------------------------------------------------------------
# Measure time helper — returns seconds with millisecond precision
# ---------------------------------------------------------------------------
time_api_call() {
local method="$1" endpoint="$2" shift; shift
local start end elapsed
start=$(date +%s%N)
api_call "${method}" "${endpoint}" -o /dev/null "$@" 2>/dev/null || true
end=$(date +%s%N)
elapsed=$(( (end - start) / 1000000 )) # milliseconds
echo "$(echo "scale=3; ${elapsed}/1000" | bc)"
}
# ---------------------------------------------------------------------------
# Test 1: Dashboard Load Time
# ---------------------------------------------------------------------------
test_dashboard_load() {
echo -e "\n${CYAN}=== Test 1: Dashboard Load Time (target < ${THRESHOLD_DASHBOARD}s) ===${NC}"
info "Measuring /api/v1/status/fleet response time..."
DASHBOARD_TIME=$(time_api_call GET /api/v1/status/fleet)
info "Dashboard response time: ${DASHBOARD_TIME}s"
if (( $(echo "${DASHBOARD_TIME} < ${THRESHOLD_DASHBOARD}" | bc -l) )); then
pass "Dashboard loaded in ${DASHBOARD_TIME}s (< ${THRESHOLD_DASHBOARD}s)"
else
fail "Dashboard loaded in ${DASHBOARD_TIME}s (>= ${THRESHOLD_DASHBOARD}s)"
fi
# Also measure frontend static asset load
info "Measuring frontend index.html load time..."
start=$(date +%s%N)
curl -sk -o /dev/null "${BASE_URL}/" 2>/dev/null || true
end=$(date +%s%N)
elapsed=$(( (end - start) / 1000000 ))
FRONTEND_TIME=$(echo "scale=3; ${elapsed}/1000" | bc)
info "Frontend load time: ${FRONTEND_TIME}s"
pass "Frontend static load: ${FRONTEND_TIME}s"
}
# ---------------------------------------------------------------------------
# Test 2: API Response Times Under Load
# ---------------------------------------------------------------------------
test_api_response_times() {
echo -e "\n${CYAN}=== Test 2: API Response Times (target < ${THRESHOLD_API}s per call) ===${NC}"
local endpoints=(
"GET /api/v1/hosts"
"GET /api/v1/groups"
"GET /api/v1/jobs"
"GET /api/v1/settings"
"GET /api/v1/ca/root.crt"
)
for ep in "${endpoints[@]}"; do
local method=$(echo "${ep}" | cut -d' ' -f1)
local path=$(echo "${ep}" | cut -d' ' -f2)
local name=$(echo "${path}" | sed 's|/api/v1/||')
info "Testing ${ep}..."
local elapsed=$(time_api_call "${method}" "${path}")
if (( $(echo "${elapsed} < ${THRESHOLD_API}" | bc -l) )); then
pass "${name}: ${elapsed}s"
else
fail "${name}: ${elapsed}s (>= ${THRESHOLD_API}s)"
fi
done
}
# ---------------------------------------------------------------------------
# Test 3: Report Generation Performance
# ---------------------------------------------------------------------------
test_report_generation() {
echo -e "\n${CYAN}=== Test 3: Report Generation (target < ${THRESHOLD_REPORTS}s) ===${NC}"
for report_type in compliance patch-history vulnerability audit; do
for format in csv pdf; do
info "Generating ${report_type} (${format})..."
local elapsed=$(time_api_call GET "/api/v1/reports/${report_type}?format=${format}")
if (( $(echo "${elapsed} < ${THRESHOLD_REPORTS}" | bc -l) )); then
pass "${report_type} (${format}): ${elapsed}s"
else
fail "${report_type} (${format}): ${elapsed}s (>= ${THRESHOLD_REPORTS}s)"
fi
done
done
}
# ---------------------------------------------------------------------------
# Test 4: Host Bulk Operations
# ---------------------------------------------------------------------------
test_bulk_host_operations() {
echo -e "\n${CYAN}=== Test 4: Host Bulk Operations ===${NC}"
# 4.1 Bulk host listing with large page
info "4.1 List hosts (page size 500)"
local elapsed=$(time_api_call GET "/api/v1/hosts?page_size=500")
pass "Host list (500/page): ${elapsed}s"
# 4.2 Sequential host creation (measure throughput)
info "4.2 Sequential host creation (10 hosts)"
local start=$(date +%s%N)
for i in $(seq 1 10); do
api_call POST /api/v1/hosts \
-d "{\"fqdn\": \"perf-test-${i}.example.com\", \"ip_address\": \"10.99.0.${i}\"}" \
-o /dev/null 2>/dev/null || true
done
local end=$(date +%s%N)
local total_ms=$(( (end - start) / 1000000 ))
local total_s=$(echo "scale=3; ${total_ms}/1000" | bc)
local per_host=$(echo "scale=3; ${total_s}/10" | bc)
info "10 hosts created in ${total_s}s (${per_host}s per host)"
pass "Host creation throughput: ${per_host}s/host"
# Cleanup
info "Cleaning up test hosts..."
HOSTS_RESP=$(api_call GET "/api/v1/hosts?page_size=500")
for id in $(echo "${HOSTS_RESP}" | grep -oP '"id":"[0-9a-f-]+"' | cut -d'"' -f4 2>/dev/null || true); do
api_call DELETE "/api/v1/hosts/${id}" -o /dev/null 2>/dev/null || true
done
}
# ---------------------------------------------------------------------------
# Test 5: CIDR Scan Performance
# ---------------------------------------------------------------------------
test_cidr_scan() {
echo -e "\n${CYAN}=== Test 5: CIDR Scan (target < ${THRESHOLD_CIDR}s for /22) ===${NC}"
# Note: This test initiates a real CIDR scan which may not complete quickly
# without reachable hosts. We measure the API response time for initiating.
info "5.1 CIDR scan initiation time"
local start=$(date +%s%N)
SCAN_RESP=$(api_call POST /api/v1/discovery/cidr \
-d '{"cidr": "10.0.0.0/30", "timeout": 1.5}' 2>/dev/null || true)
local end=$(date +%s%N)
local elapsed_ms=$(( (end - start) / 1000000 ))
local elapsed_s=$(echo "scale=3; ${elapsed_ms}/1000" | bc)
info "CIDR scan initiation: ${elapsed_s}s"
pass "CIDR scan API response: ${elapsed_s}s"
# For a /22 scan, the actual scan runs asynchronously in the worker.
# We verify the scan was accepted and check progress.
if echo "${SCAN_RESP}" | grep -q '"scan_id"'; then
pass "CIDR scan accepted for processing"
# Poll for completion (with timeout)
info "5.2 Waiting for /30 scan completion (max 30s)..."
local scan_id=$(echo "${SCAN_RESP}" | grep -oP '"scan_id":"[^"]+"' | cut -d'"' -f4)
local waited=0
while [[ ${waited} -lt 30 ]]; do
local status=$(api_call GET "/api/v1/discovery/cidr/${scan_id}" -o /dev/null -w '%{http_code}' 2>/dev/null || echo "000")
if [[ "${status}" == "200" ]]; then
break
fi
sleep 2
waited=$((waited + 2))
done
info "Scan completed or timed out after ${waited}s"
else
skip "5.2 CIDR scan completion (scan not accepted)"
fi
}
# ---------------------------------------------------------------------------
# Test 6: Concurrent API Load
# ---------------------------------------------------------------------------
test_concurrent_load() {
echo -e "\n${CYAN}=== Test 6: Concurrent API Load ===${NC}"
# Fire 20 concurrent requests and measure total time
info "6.1 20 concurrent fleet status requests"
local start=$(date +%s%N)
for i in $(seq 1 20); do
api_call GET /api/v1/status/fleet -o /dev/null 2>/dev/null &
done
wait
local end=$(date +%s%N)
local total_ms=$(( (end - start) / 1000000 ))
local total_s=$(echo "scale=3; ${total_ms}/1000" | bc)
local per_req=$(echo "scale=3; ${total_s}/20" | bc)
info "20 concurrent requests completed in ${total_s}s (${per_req}s avg)"
if (( $(echo "${per_req} < ${THRESHOLD_API}" | bc -l) )); then
pass "Concurrent load: ${per_req}s avg per request"
else
fail "Concurrent load: ${per_req}s avg per request (>= ${THRESHOLD_API}s)"
fi
# 6.2 Mixed endpoint concurrent load
info "6.2 20 concurrent mixed-endpoint requests"
start=$(date +%s%N)
for i in $(seq 1 5); do
api_call GET /api/v1/hosts -o /dev/null 2>/dev/null &
api_call GET /api/v1/groups -o /dev/null 2>/dev/null &
api_call GET /api/v1/jobs -o /dev/null 2>/dev/null &
api_call GET /api/v1/status/fleet -o /dev/null 2>/dev/null &
done
wait
end=$(date +%s%N)
total_ms=$(( (end - start) / 1000000 ))
total_s=$(echo "scale=3; ${total_ms}/1000" | bc)
per_req=$(echo "scale=3; ${total_s}/20" | bc)
info "Mixed concurrent: ${total_s}s total, ${per_req}s avg"
pass "Mixed concurrent load: ${per_req}s avg"
}
# ---------------------------------------------------------------------------
# Test 7: WebSocket Ticket Performance
# ---------------------------------------------------------------------------
test_ws_ticket_performance() {
echo -e "\n${CYAN}=== Test 7: WebSocket Ticket Issuance ===${NC}"
info "7.1 Sequential ticket creation (10 tickets)"
local start=$(date +%s%N)
for i in $(seq 1 10); do
api_call POST /api/v1/ws/ticket -o /dev/null 2>/dev/null || true
done
local end=$(date +%s%N)
local total_ms=$(( (end - start) / 1000000 ))
local total_s=$(echo "scale=3; ${total_ms}/1000" | bc)
local per_ticket=$(echo "scale=3; ${total_s}/10" | bc)
info "10 tickets in ${total_s}s (${per_ticket}s per ticket)"
pass "WS ticket issuance: ${per_ticket}s/ticket"
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN}Linux Patch Manager — Performance Tests${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e "Target: ${BASE_URL}"
echo -e "Time: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo -e "\nNFR Thresholds:"
echo -e " Dashboard: < ${THRESHOLD_DASHBOARD}s"
echo -e " CIDR /22: < ${THRESHOLD_CIDR}s"
echo -e " API calls: < ${THRESHOLD_API}s"
echo -e " Reports: < ${THRESHOLD_REPORTS}s"
echo
# Pre-flight
info "Pre-flight: Health check"
HEALTH=$(curl -sk -o /dev/null -w '%{http_code}' "${BASE_URL}/status/health")
if [[ "${HEALTH}" != "200" ]]; then
fail "Health check returned ${HEALTH}. Aborting."
exit 1
fi
pass "Health check passed"
authenticate
test_dashboard_load
test_api_response_times
test_report_generation
test_bulk_host_operations
test_cidr_scan
test_concurrent_load
test_ws_ticket_performance
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo -e "\n${CYAN}========================================${NC}"
echo -e "${CYAN}Performance Test Summary${NC}"
echo -e "${CYAN}========================================${NC}"
echo -e " ${GREEN}PASS${NC}: ${PASS}"
echo -e " ${RED}FAIL${NC}: ${FAIL}"
echo -e " ${YELLOW}SKIP${NC}: ${SKIP}"
echo -e " ${CYAN}TOTAL${NC}: $((PASS + FAIL + SKIP))"
if [[ ${FAIL} -eq 0 ]]; then
echo -e "\n${GREEN}All performance tests passed!${NC}"
exit 0
else
echo -e "\n${RED}${FAIL} performance test(s) failed.${NC}"
exit 1
fi

288
scripts/setup.sh Normal file
View File

@ -0,0 +1,288 @@
#!/usr/bin/env bash
# =============================================================================
# Linux Patch Manager — Initial Host Setup Script
# =============================================================================
# Run as root on the Ubuntu 24.04 Patch Manager host.
# This script:
# - Creates the service user/group
# - Creates required directories with correct permissions
# - Installs PostgreSQL if not present
# - Creates the database and user
# - Copies configuration and binaries
# - Installs systemd units
# - Generates initial Ed25519 JWT keys
# - Generates internal CA and CA-signed web server certificate
# =============================================================================
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; }
[[ $EUID -ne 0 ]] && error "This script must be run as root."
SERVICE_USER="patch-manager"
SERVICE_GROUP="patch-manager"
CONFIG_DIR="/etc/patch-manager"
LOG_DIR="/var/log/patch-manager"
DATA_DIR="/opt/patch-manager"
FRONTEND_DIR="/usr/share/patch-manager/frontend"
BIN_DIR="/usr/local/bin"
BACKUP_DIR="/var/backups/patch-manager"
DB_NAME="patch_manager"
DB_USER="patch_manager"
SYSTEMD_DIR="/etc/systemd/system"
info "=== Linux Patch Manager Setup ==="
# -----------------------------------------------------------------------
# 1. Create service user
# -----------------------------------------------------------------------
info "Creating service user '${SERVICE_USER}'..."
if ! id "${SERVICE_USER}" &>/dev/null; then
useradd --system --no-create-home --shell /usr/sbin/nologin \
--comment "Linux Patch Manager service account" \
"${SERVICE_USER}"
info "User '${SERVICE_USER}' created."
else
warn "User '${SERVICE_USER}' already exists, skipping."
fi
# -----------------------------------------------------------------------
# 2. Create required directories
# -----------------------------------------------------------------------
info "Creating directories..."
mkdir -p \
"${CONFIG_DIR}/ca" \
"${CONFIG_DIR}/certs" \
"${CONFIG_DIR}/jwt" \
"${CONFIG_DIR}/tls" \
"${LOG_DIR}" \
"${DATA_DIR}" \
"${FRONTEND_DIR}" \
"${BACKUP_DIR}"
chown -R "${SERVICE_USER}:${SERVICE_GROUP}" \
"${CONFIG_DIR}" \
"${LOG_DIR}" \
"${DATA_DIR}" \
"${FRONTEND_DIR}"
chmod 750 "${CONFIG_DIR}/ca" "${CONFIG_DIR}/jwt"
chmod 700 "${BACKUP_DIR}"
info "Directories created."
# -----------------------------------------------------------------------
# 3. Install PostgreSQL 16 if not present
# -----------------------------------------------------------------------
if ! command -v psql &>/dev/null; then
info "Installing PostgreSQL 16..."
apt-get update -qq
apt-get install -y postgresql-16
systemctl enable --now postgresql
else
info "PostgreSQL already installed: $(psql --version)"
fi
# -----------------------------------------------------------------------
# 4. Create database and user
# -----------------------------------------------------------------------
info "Creating database '${DB_NAME}' and user '${DB_USER}'..."
DB_PASSWORD=$(openssl rand -base64 32)
sudo -u postgres psql -v ON_ERROR_STOP=1 <<SQL
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${DB_USER}') THEN
CREATE ROLE ${DB_USER} LOGIN PASSWORD '${DB_PASSWORD}';
END IF;
END
\$\$;
SELECT 'CREATE DATABASE ${DB_NAME} OWNER ${DB_USER}'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_NAME}')\\gexec
SQL
# Grant schema permissions (PostgreSQL 15+ requires explicit grants)
sudo -u postgres psql -v ON_ERROR_STOP=1 -d ${DB_NAME} <<SQL
GRANT USAGE ON SCHEMA public TO ${DB_USER};
GRANT CREATE ON SCHEMA public TO ${DB_USER};
GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
SQL
DB_URL="postgres://${DB_USER}:${DB_PASSWORD}@localhost/${DB_NAME}"
info "Database ready. Connection URL (save this!):"
echo " ${DB_URL}"
# -----------------------------------------------------------------------
# 5. Write connection URL to config if example exists
# -----------------------------------------------------------------------
CONFIG_DEST="${CONFIG_DIR}/config.toml"
if [[ ! -f "${CONFIG_DEST}" ]]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXAMPLE="${SCRIPT_DIR}/../config/config.example.toml"
if [[ -f "${EXAMPLE}" ]]; then
cp "${EXAMPLE}" "${CONFIG_DEST}"
sed -i "s|postgres://patch_manager:CHANGEME@localhost/patch_manager|${DB_URL}|" "${CONFIG_DEST}"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${CONFIG_DEST}"
chmod 640 "${CONFIG_DEST}"
info "Config written to ${CONFIG_DEST} with database URL."
else
warn "config.example.toml not found; create ${CONFIG_DEST} manually."
fi
else
warn "${CONFIG_DEST} already exists; database URL NOT updated automatically."
warn "Ensure the database URL is set: ${DB_URL}"
fi
# -----------------------------------------------------------------------
# 6. Generate Ed25519 JWT keys
# -----------------------------------------------------------------------
JWT_SIGNING="${CONFIG_DIR}/jwt/signing.pem"
JWT_VERIFY="${CONFIG_DIR}/jwt/verify.pem"
if [[ ! -f "${JWT_SIGNING}" ]]; then
info "Generating Ed25519 JWT signing key..."
openssl genpkey -algorithm ed25519 -out "${JWT_SIGNING}"
openssl pkey -in "${JWT_SIGNING}" -pubout -out "${JWT_VERIFY}"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${JWT_SIGNING}" "${JWT_VERIFY}"
chmod 600 "${JWT_SIGNING}"
chmod 644 "${JWT_VERIFY}"
info "JWT keys generated."
else
warn "JWT signing key already exists at ${JWT_SIGNING}, skipping."
fi
# -----------------------------------------------------------------------
# 6b. Generate CA and CA-signed TLS certificate for HTTPS
# -----------------------------------------------------------------------
CA_KEY="${CONFIG_DIR}/ca/ca.key"
CA_CERT="${CONFIG_DIR}/ca/ca.crt"
TLS_CERT="${CONFIG_DIR}/tls/web.crt"
TLS_KEY="${CONFIG_DIR}/tls/web.key"
TLS_CSR="${CONFIG_DIR}/tls/web.csr"
# Generate CA if not present (pm-ca will load this on startup)
if [[ ! -f "${CA_CERT}" ]]; then
info "Generating internal Certificate Authority (ECDSA P-256, 10-year validity)..."
openssl ecparam -genkey -name prime256v1 -noout -out "${CA_KEY}"
openssl req -new -x509 -key "${CA_KEY}" -out "${CA_CERT}" \
-days 3650 \
-subj "/CN=Patch Manager Root CA/O=Patch Manager" \
-addext "basicConstraints=critical,CA:true" \
-addext "keyUsage=critical,keyCertSign,cRLSign"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${CA_KEY}" "${CA_CERT}"
chmod 600 "${CA_KEY}" "${CA_CERT}"
info "Internal CA generated."
else
info "Internal CA already exists at ${CA_CERT}, skipping."
fi
# Generate web server certificate signed by the internal CA
if [[ ! -f "${TLS_CERT}" ]]; then
info "Generating CA-signed web server certificate (valid 365 days)..."
HOSTNAME_FQDN=$(hostname -f 2>/dev/null || echo "localhost")
HOSTNAME_SHORT=$(hostname -s 2>/dev/null || echo "localhost")
# Get the host's primary IP address for SAN
HOST_IP=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{print $7; exit}' || echo "127.0.0.1")
# Generate ECDSA P-256 private key for web server
openssl ecparam -genkey -name prime256v1 -noout -out "${TLS_KEY}"
# Generate CSR with SANs
openssl req -new -key "${TLS_KEY}" -out "${TLS_CSR}" \
-subj "/CN=${HOSTNAME_FQDN}/O=Patch Manager" \
-addext "subjectAltName=DNS:${HOSTNAME_FQDN},DNS:${HOSTNAME_SHORT},DNS:localhost,IP:${HOST_IP},IP:127.0.0.1,IP:::1"
# Sign with the internal CA
openssl x509 -req -in "${TLS_CSR}" -CA "${CA_CERT}" -CAkey "${CA_KEY}" \
-CAcreateserial -days 365 -out "${TLS_CERT}" \
-extfile <(printf "subjectAltName=DNS:${HOSTNAME_FQDN},DNS:${HOSTNAME_SHORT},DNS:localhost,IP:${HOST_IP},IP:127.0.0.1,IP:::1\nextendedKeyUsage=serverAuth")
# Clean up CSR
rm -f "${TLS_CSR}"
chown "${SERVICE_USER}:${SERVICE_GROUP}" "${TLS_CERT}" "${TLS_KEY}"
chmod 644 "${TLS_CERT}"
chmod 600 "${TLS_KEY}"
info "CA-signed web server certificate generated for ${HOSTNAME_FQDN}."
else
warn "TLS certificate already exists at ${TLS_CERT}, skipping."
fi
# -----------------------------------------------------------------------
# 7. Install systemd units
# -----------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Install systemd target
TARGET_SRC="${SCRIPT_DIR}/../systemd/patch-manager.target"
if [[ -f "${TARGET_SRC}" ]]; then
cp "${TARGET_SRC}" "${SYSTEMD_DIR}/patch-manager.target"
info "Installed systemd target: patch-manager.target"
fi
# Install service units
for unit in patch-manager-web.service patch-manager-worker.service; do
SRC="${SCRIPT_DIR}/../systemd/${unit}"
if [[ -f "${SRC}" ]]; then
cp "${SRC}" "${SYSTEMD_DIR}/${unit}"
info "Installed systemd unit: ${unit}"
else
warn "Systemd unit not found: ${SRC}"
fi
done
# Install backup script
BACKUP_SRC="${SCRIPT_DIR}/backup.sh"
if [[ -f "${BACKUP_SRC}" ]]; then
cp "${BACKUP_SRC}" "${BIN_DIR}/backup.sh"
chmod 700 "${BIN_DIR}/backup.sh"
info "Installed backup script to ${BIN_DIR}/backup.sh"
fi
systemctl daemon-reload
info "systemd units installed and daemon reloaded."
# -----------------------------------------------------------------------
# 8. Run seed migration (default admin account)
# -----------------------------------------------------------------------
SEED_MIGRATION="${SCRIPT_DIR}/../migrations/002_seed_admin.sql"
if [[ -f "${SEED_MIGRATION}" ]]; then
info "Running seed migration for default admin account..."
sudo -u postgres psql -d "${DB_NAME}" -f "${SEED_MIGRATION}" 2>/dev/null || \
warn "Seed migration already applied or failed (may be idempotent)."
else
warn "Seed migration not found: ${SEED_MIGRATION}"
fi
# -----------------------------------------------------------------------
# 9. Install backup cron job
# -----------------------------------------------------------------------
CRON_LINE="0 2 * * * /usr/local/bin/backup.sh >> /var/log/patch-manager/backup.log 2>&1"
if crontab -l 2>/dev/null | grep -qF "backup.sh"; then
warn "Backup cron job already installed, skipping."
else
(crontab -l 2>/dev/null; echo "${CRON_LINE}") | crontab -
info "Nightly backup cron installed (02:00 daily)."
fi
# -----------------------------------------------------------------------
# Done
# -----------------------------------------------------------------------
info "=== Setup complete ==="
info "Next steps:"
echo " 1. Build and install binaries: cargo build --release"
echo " cp target/release/pm-web target/release/pm-worker ${BIN_DIR}/"
echo " 2. Build and install frontend: scripts/build-frontend.sh"
echo " 3. Review ${CONFIG_DEST}"
echo " 4. Enable services:"
echo " systemctl enable --now patch-manager-web patch-manager-worker"
echo " 5. (Optional) Set GPG_RECIPIENT in backup.sh for encrypted backups"