---
title: Registration & Duplicate Prevention
slug: registration-and-duplicate-prevention
createdAt: 2025-04-03T01:45:59.072Z
---

### 1. Introduction

Accurately identifying unique devices and preventing the creation of duplicate records is critical for maintaining CMDB integrity, accurate licensing, and reliable management within CommandIT. This is especially challenging in environments where agents might be reinstalled on the same hardware or where system images containing an agent are deployed to new, distinct hardware (cloning).

This document outlines the agent registration process and the backend logic designed to handle these scenarios robustly.

### 2. Registration Process (Initial Installation / No Existing Identity)

This process occurs when the CommandIT Service starts for the first time on a machine or after its local identity (`device_id`, `device_secret`) has been cleared.

1. **Collect Hardware Identifiers:** The agent Service gathers multiple pieces of hardware information expected to be relatively stable and unique:
   - **Primary:** SMBIOS UUID (from BIOS/UEFI)
   - **Secondary:** System Serial Number (from Chassis/BIOS)
   - **Tertiary:** Baseboard (Motherboard) Serial Number
   - **Quaternary:** Primary Non-Virtual Network Adapter MAC Address
     *(Agent retrieves these via OS APIs, WMI, system\_profiler, etc.)*
2. **Call Registration API:** The agent makes a `POST /api/v1/agent/register` call to the backend.
   - **Payload:** Includes the collected hardware identifiers (UUID, System Serial, Baseboard Serial, MAC Address), the assigned `org_id` and `location_id` (provided during installation/configuration), and potentially a transient Agent GUID for tracking the request.
3. **Backend Matching Logic:** The backend API receives the registration request and performs a prioritized matching sequence **within the specified&#x20;**`org_id` to find an existing `Devices` record representing this hardware:
   - **a. Match on SMBIOS UUID:** Query `Devices` WHERE `org_id = ?` AND `smbios_uuid = ?`.
     - If **one unique, active** record is found -> Match successful.
     - If *multiple* active records match (indicates potential existing duplicates or non-unique UUIDs) -> **Conflict: Requires Manual Review**.
     - If `smbios_uuid` is NULL, invalid (e.g., all zeros/Fs), or no match -> Proceed to next step.
   - **b. Match on System Serial Number:** Query `Devices` WHERE `org_id = ?` AND `serial_number = ?`.
     - If **one unique, active** record is found -> Match successful.
     - If *multiple* active records match -> **Conflict: Requires Manual Review**.
     - If `serial_number` is NULL, unreliable, or no match -> Proceed to next step.
   - **c. Match on Baseboard Serial Number:** Query `DeviceHardware` for the Baseboard Serial, join to `Devices` WHERE `org_id = ?`.
     - If **one unique, active** record is found -> Match successful.
     - If *multiple* active records match -> **Conflict: Requires Manual Review**.
     - If Baseboard Serial not found or no match -> Proceed to next step.
   - **d. Match on Primary MAC Address (Fallback - Use Cautiously):** Query `DeviceNetworkAdapters` for the MAC, join to `Devices` WHERE `org_id = ?`.
     - Because MACs can change (adapters replaced, VMs cloned, docking stations), matching *solely* on MAC is unreliable. This step should ideally be combined with other less unique identifiers (e.g., Manufacturer + Model + MAC) or used only as a last resort if enabled by configuration.
     - If a potential match is found -> May require manual review or have lower confidence.
4. **Backend Action:**
   - **Unique Match Found:** The backend associates the registration request with the existing `device_id`. It generates a *new* secure `device_secret` for the agent and returns the existing `device_id` and new secret. (Invalidating any previous secret for that device).
   - **No Match Found:** The backend creates a **new** record in the `Devices` table, populating `org_id`, `smbios_uuid`, `serial_number`, and other basic info provided. It generates a new `device_id` (UUID) and a new `device_secret`, returning these to the agent.
   - **Conflict/Multiple Matches Found:** The backend logs the conflict, potentially creates a temporary placeholder record requiring admin intervention, and returns a specific status code indicating manual review is needed. It does *not* return a standard `device_id`/secret.
5. **Agent Action on Response:**
   - **Success (New or Matched):** The agent securely stores the received `device_id` and `device_secret` (using DPAPI/Keychain) for all future communication.
   - **Failure/Conflict:** The agent logs the error and enters a retry loop for registration.

### 3. Re-imaging / Cloning Detection (During Check-in)

This process detects when an agent with a *valid existing identity* checks in from hardware that *doesn't match* the database record, typically after an image deployment.

1. **Agent Check-in:** The agent Service initiates its regular `POST /api/v1/agent/{deviceId}/checkin` call, authenticating with its stored `deviceId` and `device_secret`. **Crucially, the payload includes the *current* key hardware identifiers** (SMBIOS UUID, System Serial, Baseboard Serial, Primary MAC).
2. **Backend Check-in Validation:** The backend API performs these checks:
   - **Authenticate:** Verifies the `deviceId` and `device_secret`.
   - **Retrieve Stored HW IDs:** Fetches the hardware identifiers (`smbios_uuid`, `serial_number`, etc.) associated with the requesting `deviceId` from the database.
   - **Compare HW IDs:** Compares the IDs received in the check-in payload against the stored IDs.
3. **Backend Response:**
   - **Match:** If the key identifiers match (within acceptable tolerance defined by business logic, prioritizing UUID/Serials), the backend processes the check-in normally, updates `Devices.last_agent_checkin`, and returns any pending commands.
   - **Mismatch:** If a significant mismatch is detected (e.g., different SMBIOS UUID or System Serial), the backend determines this is likely a cloned agent on new hardware. It **rejects the check-in** for the original `deviceId` and responds with a specific status/error code: **"Hardware Mismatch - Re-register Required"**. It **does not** update `last_agent_checkin` for the original `deviceId`.
4. **Agent Response to Mismatch:** The agent Service, upon receiving "Re-register Required":
   - **Clears Identity:** Deletes its local `device_id` and `device_secret`.
   - **Initiates Registration:** Immediately restarts the full registration process described in Section 2, sending its current (new) hardware IDs. This results in a new `Devices` record being created for the new hardware instance.

### 4. Conclusion

This two-pronged approach – robust hardware matching during initial registration and continuous hardware validation during check-in – combined with the agent's ability to clear its identity and re-register upon backend instruction, provides a strong mechanism to prevent duplicate device entries caused by agent reinstalls or OS imaging/cloning onto new hardware. The accuracy depends on the quality of hardware identifiers collected by the agent and the strictness of the backend matching logic.
