From 63831caba348b2a2d45afeb84f6c130d8a8c3569 Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Fri, 7 Nov 2025 08:20:42 +0000 Subject: [PATCH] fixed tfa route for handling insertion of tfa number Better handling of existing systems already enrolled, done via checking if the config.yml file exists and ping through its credentials as opposed to checking for machine_ID UI justification improvements on repositories pages --- agents/patchmon_install.sh | 58 +++++++++++++++-------------- agents/proxmox_auto_enroll.sh | 44 ++++++++++++++++++++-- backend/src/routes/tfaRoutes.js | 15 ++++++-- frontend/src/pages/Repositories.jsx | 22 +++++++---- package-lock.json | 8 ++-- 5 files changed, 101 insertions(+), 46 deletions(-) diff --git a/agents/patchmon_install.sh b/agents/patchmon_install.sh index 315f117..c41ba0b 100644 --- a/agents/patchmon_install.sh +++ b/agents/patchmon_install.sh @@ -311,6 +311,37 @@ else mkdir -p /etc/patchmon fi +# Check if agent is already configured and working (before we overwrite anything) +info "๐Ÿ” Checking if agent is already configured..." + +if [[ -f /etc/patchmon/config.yml ]] && [[ -f /etc/patchmon/credentials.yml ]]; then + if [[ -f /usr/local/bin/patchmon-agent ]]; then + info "๐Ÿ“‹ Found existing agent configuration" + info "๐Ÿงช Testing existing configuration with ping..." + + if /usr/local/bin/patchmon-agent ping >/dev/null 2>&1; then + success "โœ… Agent is already configured and ping successful" + info "๐Ÿ“‹ Existing configuration is working - skipping installation" + info "" + info "If you want to reinstall, remove the configuration files first:" + info " sudo rm -f /etc/patchmon/config.yml /etc/patchmon/credentials.yml" + echo "" + exit 0 + else + warning "โš ๏ธ Agent configuration exists but ping failed" + warning "โš ๏ธ Will move existing configuration and reinstall" + echo "" + fi + else + warning "โš ๏ธ Configuration files exist but agent binary is missing" + warning "โš ๏ธ Will move existing configuration and reinstall" + echo "" + fi +else + success "โœ… Agent not yet configured - proceeding with installation" + echo "" +fi + # Step 2: Create configuration files info "๐Ÿ” Creating configuration files..." @@ -426,33 +457,6 @@ if [[ -f "/etc/patchmon/logs/patchmon-agent.log" ]]; then fi # Step 4: Test the configuration -# Check if this machine is already enrolled -info "๐Ÿ” Checking if machine is already enrolled..." -existing_check=$(curl $CURL_FLAGS -s -X POST \ - -H "X-API-ID: $API_ID" \ - -H "X-API-KEY: $API_KEY" \ - -H "Content-Type: application/json" \ - -d "{\"machine_id\": \"$MACHINE_ID\"}" \ - "$PATCHMON_URL/api/v1/hosts/check-machine-id" \ - -w "\n%{http_code}" 2>&1) - -http_code=$(echo "$existing_check" | tail -n 1) -response_body=$(echo "$existing_check" | sed '$d') - -if [[ "$http_code" == "200" ]]; then - already_enrolled=$(echo "$response_body" | jq -r '.exists' 2>/dev/null || echo "false") - if [[ "$already_enrolled" == "true" ]]; then - warning "โš ๏ธ This machine is already enrolled in PatchMon" - info "Machine ID: $MACHINE_ID" - info "Existing host: $(echo "$response_body" | jq -r '.host.friendly_name' 2>/dev/null)" - info "" - info "The agent will be reinstalled/updated with existing credentials." - echo "" - else - success "โœ… Machine not yet enrolled - proceeding with installation" - fi -fi - info "๐Ÿงช Testing API credentials and connectivity..." if /usr/local/bin/patchmon-agent ping; then success "โœ… TEST: API credentials are valid and server is reachable" diff --git a/agents/proxmox_auto_enroll.sh b/agents/proxmox_auto_enroll.sh index 01c4158..04516f4 100755 --- a/agents/proxmox_auto_enroll.sh +++ b/agents/proxmox_auto_enroll.sh @@ -230,6 +230,40 @@ while IFS= read -r line; do info " โœ“ Host enrolled successfully: $api_id" + # Check if agent is already installed and working + info " Checking if agent is already configured..." + config_check=$(timeout 10 pct exec "$vmid" -- bash -c " + if [[ -f /etc/patchmon/config.yml ]] && [[ -f /etc/patchmon/credentials.yml ]]; then + if [[ -f /usr/local/bin/patchmon-agent ]]; then + # Try to ping using existing configuration + if /usr/local/bin/patchmon-agent ping >/dev/null 2>&1; then + echo 'ping_success' + else + echo 'ping_failed' + fi + else + echo 'binary_missing' + fi + else + echo 'not_configured' + fi + " 2>/dev/null /dev/null 2>&1 && echo 'installed' || echo 'missing'" 2>/dev/null { try { @@ -71,7 +76,11 @@ router.post( return res.status(400).json({ errors: errors.array() }); } - const { token } = req.body; + // Ensure token is a string (convert if needed) + let { token } = req.body; + if (typeof token !== "string") { + token = String(token); + } const userId = req.user.id; // Get user's TFA secret diff --git a/frontend/src/pages/Repositories.jsx b/frontend/src/pages/Repositories.jsx index 5bcf845..77d0300 100644 --- a/frontend/src/pages/Repositories.jsx +++ b/frontend/src/pages/Repositories.jsx @@ -237,8 +237,14 @@ const Repositories = () => { // Handle special cases if (sortField === "security") { - aValue = a.isSecure ? "Secure" : "Insecure"; - bValue = b.isSecure ? "Secure" : "Insecure"; + // Use the same logic as filtering to determine isSecure + const aIsSecure = + a.isSecure !== undefined ? a.isSecure : a.url.startsWith("https://"); + const bIsSecure = + b.isSecure !== undefined ? b.isSecure : b.url.startsWith("https://"); + // Sort by boolean: true (Secure) comes before false (Insecure) when ascending + aValue = aIsSecure ? 1 : 0; + bValue = bIsSecure ? 1 : 0; } else if (sortField === "status") { aValue = a.is_active ? "Active" : "Inactive"; bValue = b.is_active ? "Active" : "Inactive"; @@ -535,12 +541,12 @@ const Repositories = () => { {visibleColumns.map((column) => (