Patch Management
10 min
1. Endpoint Agent Logic: Data Collection & Caching AGENT
The agent's primary role is to efficiently gather patch information from the host OS and report only the necessary changes to the backend. Using a local SQLite database for caching is crucial for minimizing network traffic and resource usage.
Initial Scan & Cache Creation
This process runs the first time the agent is installed or when its cache is cleared.
- Data Collection: Use OS-native APIs to get a complete list of installed and applicable (needed) patches.
- Windows: Utilize the Windows Update Agent (WUA) API.
- Linux: Use package managers like dpkg/apt (Debian/Ubuntu) or rpm/yum/dnf (Red Hat/CentOS).
- Data Points: For each patch, collect:
- KB ID or Package Name (maps to patch_release.kb_id)
- Title/Description (maps to patch_release.title)
- Current Status ('Installed', 'Needed', 'Failed')
- Installation Date (if available)
- Local Caching (SQLite): Create a simple local SQLite database on the device. Store the full scan results in a table that mirrors the essential fields of the backend's device_patch_state table (e.g., kb_id, status).
- Initial Upload: Serialize the entire list of discovered patches into a JSON payload and send it to the backend's ingestion endpoint. This is the largest data transmission the agent will typically perform.
Delta Scans & Differential Uploads
All subsequent scans will be delta scans to report only changes.
- Perform New Scan: Execute the same OS-native data collection commands to get the current patch state.
- Compare with Cache: Compare the results of the new scan against the records stored in the local SQLite database.
- Identify Changes (Deltas):
- New Patches: Any patch present in the new scan but not in the local cache.
- Status Changes: Any patch where the status has changed (e.g., from 'Needed' to 'Installed').
- Removed Patches: Any patch in the cache that is no longer reported by the OS (could indicate supersedence or uninstallation).
- Construct Payload: Create a JSON payload containing only these identified changes. Each item should clearly state the patch identifier and its new status.
- Upload Delta: Send the smaller delta payload to the backend API.
- Update Cache: Upon receiving a successful (HTTP 200) response from the backend, update the local SQLite cache to reflect the state of the most recent scan.
2. Backend API & Data Processing 🖥️
The backend is the central hub that processes agent data, manages policies, and serves information to the frontend.
Data Ingestion Endpoint
- Endpoint: POST /api/devices/{deviceId}/patch_state
- Logic:
- Authenticate the agent and validate the deviceId.
- Accept the JSON payload (both full and delta payloads are handled by the same logic).
- Create a record in device_patch_scan to log the raw scan event for auditing.
- Iterate through each patch in the payload:
- Look up the patch_release_id from the patch_release table using the kb_id and OS/product info. If the patch is unknown, it may be added to the catalog in a pending/unverified state for later review.
- Look up the status_id from the ref_patch_status table.
- Perform an UPSERT operation on the device_patch_state table for the given device_id and patch_release_id. This will either INSERT a new record or UPDATE an existing one with the new status_id, last_seen, and last_change_at timestamps.
- After processing, trigger an asynchronous background job to recalculate the summary data for this device.
Summary & KPI Calculation
This logic runs in a background job to avoid slowing down the API response.
- device_patch_summary Calculation:
- For a given device_id, query its records in device_patch_state.
- Join with patch_release and ref_patch_severity to count pending patches where severity is 'Critical' or 'Important'.
- Calculate the compliance_pct by dividing the count of 'Installed' patches by the total count of applicable ('Installed' + 'Needed' + 'Failed') patches.
- Update the last_scan_at from the most recent device_patch_scan record.
- UPSERT the calculated data into the device_patch_summary table for that device_id.
API Endpoints for Frontend
- Dashboard Summary: GET /api/patch_management/summary
- Aggregates data from device_patch_summary to get total compliance and missing patch counts.
- Counts devices from the device table where reboot_pending = true.
- Dashboard Patch List: GET /api/patch_management/patches
- Queries patch_release and uses subqueries or LEFT JOINs with device_patch_state to aggregate the counts of devices in 'Installed', 'Needed', and 'Failed' states for each patch.
- Joins patch_approval to retrieve the current approval status.
- Device-Specific Patch List: GET /api/devices/{deviceId}/patches?status=<status>
- Queries device_patch_state filtered by the deviceId.
- Joins patch_release and its related reference tables (ref_patch_severity, ref_patch_classification) to get descriptive details.
- Uses the status query parameter to filter for the tabs in the UI.
- Policy Management (CRUD): GET, POST, PUT /api/patch_policies/{policyId}
- These endpoints will handle all create, read, update, and delete operations for the policy-related tables (patch_policy, patch_policy_rule, patch_policy_assignment, etc.).
3. Frontend UI Implementation 🎨
This section details how to build the user interface components based on the backend APIs.
Patch Management Dashboard (Screenshot 1)
- KPI Cards: On page load, fetch data from /api/patch_management/summary. Populate the summary cards with the response. Use a timer to refresh this data periodically.
- Patches by Severity Chart: Use the summary data to render a donut or bar chart with a library like Chart.js or D3.js.
- Patch Status Grid:
- Fetch data from /api/patch_management/patches.
- Use a robust data grid component that supports server-side pagination, sorting, and filtering to display the results.
- Render the "Approval Status" as a color-coded badge.
- Make the count columns ("Installed", "Needed", "Failed") clickable, linking to a filtered view of the affected devices.
Device Patching View (Screenshot 2)
- This is a device-specific view, routed via a URL like /devices/{deviceId}/patching.
- Header: On load, fetch device details and the patch summary from /api/devices/{deviceId}/patch_summary to populate the header information.
- Tabs and Grid:
- Implement tabs for "Needed", "Installed", "Failed", etc.
- When a tab is selected, call the /api/devices/{deviceId}/patches?status=<status_code> endpoint.
- Display the returned list in the data grid.
- Include action buttons like "Deploy" which would trigger API calls to create patch_deployment records, or "Create Exception" which would create a patch_exception.
Patch Policy Editor (Screenshot 3)
- This is a form-based interface for creating and editing policies.
- Data Population: When editing, fetch the full policy configuration from /api/patch_policies/{policyId}. Use this data to populate the form fields, including the rules and assignments.
- Dynamic Rule Builder: The "Auto-Approval Rules" section requires a dynamic form component. Users should be able to add or remove rule blocks. The dropdowns within these blocks (e.g., Classification, Severity) should be populated from reference data endpoints (e.g., /api/ref/patch_classifications).
- Saving: On save, serialize the entire form state into a single JSON object. Send this object in a POST (for new policies) or PUT (for existing policies) request to the /api/patch_policies endpoint. The backend will handle the logic of updating all the related database tables.