---
title: Patch Management
slug: patch-management
createdAt: 2025-08-20T06:50:39.994Z
---

## 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.

1. **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).
2. **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)
3. **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).
4. **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.

1. **Perform New Scan**: Execute the same OS-native data collection commands to get the current patch state.
2. **Compare with Cache**: Compare the results of the new scan against the records stored in the local SQLite database.
3. **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).
4. **Construct Payload**: Create a JSON payload containing only these identified changes. Each item should clearly state the patch identifier and its new status.
5. **Upload Delta**: Send the smaller delta payload to the backend API.
6. **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/&#x7B;deviceId}/patch\_state
- **Logic**:
  1. Authenticate the agent and validate the deviceId.
  2. Accept the JSON payload (both full and delta payloads are handled by the same logic).
  3. Create a record in device\_patch\_scan to log the raw scan event for auditing.
  4. 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.
  5. 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**:
  1. For a given device\_id, query its records in device\_patch\_state.
  2. Join with patch\_release and ref\_patch\_severity to count pending patches where severity is 'Critical' or 'Important'.
  3. Calculate the compliance\_pct by dividing the count of 'Installed' patches by the total count of applicable ('Installed' + 'Needed' + 'Failed') patches.
  4. Update the last\_scan\_at from the most recent device\_patch\_scan record.
  5. 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/&#x7B;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/&#x7B;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/&#x7B;deviceId}/patching.
- **Header**: On load, fetch device details and the patch summary from /api/devices/&#x7B;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/&#x7B;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/&#x7B;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.

