Skip to content

A free open source IT asset/license management system

Fixed #19935 - Handle new external ID when adopt_by_serial is enabled

snipe8d767fc90dd8
diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.php
index 673fec1e49..0f9cf88e00 100644
--- a/app/SyncAdapters/SyncsHostFromRecord.php
+++ b/app/SyncAdapters/SyncsHostFromRecord.php
@@ -210,9 +210,21 @@ trait SyncsHostFromRecord
* Look up an existing Snipe-IT asset with a matching serial to
* adopt into this adapter's external-sources link, when the
* admin has turned on adopt_by_serial for this adapter instance.
- * Skips assets that already have an external_sources row for
- * this source so re-runs are idempotent and multi-adapter
- * installs don't accidentally re-link.
+ *
+ * Re-enrollment: some vendors rotate the device's external_id
+ * (Intune managedDevice ID, Kandji device UDID, etc)
+ * when the device is reinstalled or re-enrolled. On the
+ * next sync the record's new external_id does not match the
+ * asset's prior asset_external_sources row, so the (source,
+ * external_id) lookup in provisionAsset misses. If the hardware
+ * serial still matches and adopt_by_serial is on, treat the
+ * prior row's external_id as stale and update it to the new one
+ * rather than creating a duplicate asset.
+ *
+ * The genuine case of two actively-enrolled devices
+ * legitimately sharing a serial (vendor cloning bug / BYOD) is
+ * not handled here: the external_id will flap between the two
+ * records across syncs. True fix is pull-level reconciliation :(
*/
private static function adoptExistingAssetBySerial(HostInventoryRecord $record, ?SyncAdapterInstance $instance): ?Asset
{
@@ -230,17 +242,32 @@ trait SyncsHostFromRecord
$candidate = Asset::query()
->where('serial', $record->hardwareSerial)
- ->whereNotIn('id', function ($q) use ($record) {
- $q->select('asset_id')
- ->from('asset_external_sources')
- ->where('source', $record->sourceKey);
- })
->first();
if ($candidate === null) {
return null;
}
+ $existing = DB::table('asset_external_sources')
+ ->where('asset_id', $candidate->id)
+ ->where('source', $record->sourceKey)
+ ->first();
+
+ if ($existing !== null) {
+ DB::table('asset_external_sources')
+ ->where('id', $existing->id)
+ ->update([
+ 'external_id' => $record->sourceId,
+ 'updated_at' => now(),
+ ]);
+
+ Log::channel('sync-adapters')->info(
+ "{$instance->label} re-linked asset #{$candidate->id} (serial {$record->hardwareSerial}): external_id {$existing->external_id} -> {$record->sourceId}. Prior enrollment id appears to have rotated."
+ );
+
+ return $candidate;
+ }
+
DB::table('asset_external_sources')->insert([
'asset_id' => $candidate->id,
'company_id' => $instance->company_id,
diff --git a/tests/Feature/SyncAdapters/ReenrollmentReadoptTest.php b/tests/Feature/SyncAdapters/ReenrollmentReadoptTest.php
new file mode 100644
index 0000000000..62fe6f9287
--- /dev/null
+++ b/tests/Feature/SyncAdapters/ReenrollmentReadoptTest.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Tests\Feature\SyncAdapters;
+
+use App\Models\Asset;
+use App\Models\Statuslabel;
+use App\Models\SyncAdapterConfig;
+use App\Models\SyncAdapterInstance;
+use App\SyncAdapters\HostInventoryRecord;
+use App\SyncAdapters\SyncAdapter;
+use Illuminate\Support\Facades\Crypt;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+class ReenrollmentReadoptTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_re_enrollment_updates_stale_external_id_on_matching_serial()
+ {
+ $instance = $this->configuredAdapter();
+ SyncAdapterConfig::put($instance->id, 'adopt_by_serial', '1');
+
+ // First sync: establishes the asset and its initial external-source row.
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'intune',
+ sourceId: 'intune-managed-id-OLD',
+ hostname: 'HMR-2240',
+ hardwareSerial: '7MWDXL4',
+ hardwareModel: 'Dell Pro 16 Plus PB16250',
+ ));
+
+ $asset = Asset::query()->where('serial', '7MWDXL4')->firstOrFail();
+ $this->assertSame(1, Asset::query()->count());
+ $this->assertDatabaseHas('asset_external_sources', [
+ 'asset_id' => $asset->id,
+ 'source' => 'intune',
+ 'external_id' => 'intune-managed-id-OLD',
+ ]);
+
+ // Second sync: the device re-enrolled, Intune issued a new managedDevice ID. Serial unchanged.
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'intune',
+ sourceId: 'intune-managed-id-NEW',
+ hostname: 'HMR-2240',
+ hardwareSerial: '7MWDXL4',
+ hardwareModel: 'Dell Pro 16 Plus PB16250',
+ ));
+
+ $this->assertSame(1, Asset::query()->count(), 'No duplicate asset was created');
+ $this->assertSame(
+ 1,
+ DB::table('asset_external_sources')
+ ->where('asset_id', $asset->id)
+ ->where('source', 'intune')
+ ->count(),
+ 'Exactly one external-source row for the asset',
+ );
+ $this->assertDatabaseHas('asset_external_sources', [
+ 'asset_id' => $asset->id,
+ 'source' => 'intune',
+ 'external_id' => 'intune-managed-id-NEW',
+ ]);
+ $this->assertDatabaseMissing('asset_external_sources', [
+ 'source' => 'intune',
+ 'external_id' => 'intune-managed-id-OLD',
+ ]);
+ }
+
+ public function test_re_link_does_not_happen_when_adopt_by_serial_is_off()
+ {
+ // adopt_by_serial OFF. The existing asset's external-source row MUST stay on the OLD id (the fix never kicks in without the opt-in). Downstream behavior of the re-enrollment record (duplicate shell, validation failure) is out of scope for this test.
+ $instance = $this->configuredAdapter();
+ SyncAdapterConfig::put($instance->id, 'adopt_by_serial', '0');
+
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'intune',
+ sourceId: 'intune-managed-id-OLD',
+ hostname: 'HMR-2240',
+ hardwareSerial: '7MWDXL4',
+ hardwareModel: 'Dell Pro 16 Plus PB16250',
+ ));
+
+ $asset = Asset::query()->where('serial', '7MWDXL4')->firstOrFail();
+
+ try {
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'intune',
+ sourceId: 'intune-managed-id-NEW',
+ hostname: 'HMR-2240',
+ hardwareSerial: '7MWDXL4',
+ hardwareModel: 'Dell Pro 16 Plus PB16250',
+ ));
+ } catch (\Throwable) {
+ // Downstream fallout from the toggle being off is incidental.
+ }
+
+ $this->assertDatabaseHas('asset_external_sources', [
+ 'asset_id' => $asset->id,
+ 'source' => 'intune',
+ 'external_id' => 'intune-managed-id-OLD',
+ ]);
+ $this->assertDatabaseMissing('asset_external_sources', [
+ 'asset_id' => $asset->id,
+ 'external_id' => 'intune-managed-id-NEW',
+ ]);
+ }
+
+ private function configuredAdapter(): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', 'intune')->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'tenant_id', 'fake-tenant-id');
+ SyncAdapterConfig::put($instance->id, 'client_id', 'fake-client-id');
+ SyncAdapterConfig::put($instance->id, 'client_secret', Crypt::encrypt('fake-client-secret'));
+
+ return $instance->fresh();
+ }
+}