Skip to content

A free open source IT asset/license management system

Fixed #19880 - added source key to upsert

snipe55c5c4c5243c
diff --git a/app/SyncAdapters/SyncsHostFromRecord.php b/app/SyncAdapters/SyncsHostFromRecord.php
index 95311feac5..1e9f941fb9 100644
--- a/app/SyncAdapters/SyncsHostFromRecord.php
+++ b/app/SyncAdapters/SyncsHostFromRecord.php
@@ -49,7 +49,7 @@ trait SyncsHostFromRecord
self::applyStandardFieldMappings($asset, $externalUpdates, $record, $instance, $mapping);
self::applyExtraFieldMappings($asset, $record, $instance, $mapping);
- $previousExternal = $isNew ? [] : self::loadExternalSourceInventory($asset->id);
+ $previousExternal = $isNew ? [] : self::loadExternalSourceInventory($asset->id, $record->sourceKey);
$wasDirty = $asset->isDirty();
// Heartbeat-only asset-side write: Skip logging when heartbeat-logging is turned off,
@@ -83,7 +83,7 @@ trait SyncsHostFromRecord
// loop and go out together so we make one row-write per
// sync instead of N updates.
if ($externalUpdates !== []) {
- self::upsertExternalSource($asset, $externalUpdates);
+ self::upsertExternalSource($asset, $record->sourceKey, $externalUpdates);
}
// Sync-driven user assignment. Runs after saveOrFail so
@@ -427,9 +427,16 @@ trait SyncsHostFromRecord
*
* @return array<string, mixed>
*/
- private static function loadExternalSourceInventory(int $assetId): array
+ private static function loadExternalSourceInventory(int $assetId, string $source): array
{
- $row = DB::table('asset_external_sources')->where('asset_id', $assetId)->first();
+ // Filter by (asset_id, source), not asset_id alone. Multi-
+ // adapter assets have one row per source and the pre-sync
+ // snapshot must come from THIS adapter's row for the diff
+ // to be meaningful. See issue #19880.
+ $row = DB::table('asset_external_sources')
+ ->where('asset_id', $assetId)
+ ->where('source', $source)
+ ->first();
if ($row === null) {
return [];
}
@@ -971,15 +978,21 @@ trait SyncsHostFromRecord
/**
* Write the inventory columns onto the asset's external-source
- * row. The identity columns (source, external_id, company_id)
- * are already written at create time and never change here.
- * updateOrInsert covers the edge case where the identity row was
- * somehow deleted between the create step and this write.
+ * row for this specific adapter. The identity columns (source,
+ * external_id, company_id) are already written at create time and
+ * never change here. updateOrInsert covers the edge case where the
+ * identity row was somehow deleted between the create step and
+ * this write.
+ *
+ * Match key is (asset_id, source) because an asset synced by two
+ * adapters has two rows in asset_external_sources, one per source.
+ * Keying on asset_id alone would overwrite whichever row MySQL
+ * returned first regardless of adapter. See issue #19880.
*/
- private static function upsertExternalSource(Asset $asset, array $updates): void
+ private static function upsertExternalSource(Asset $asset, string $source, array $updates): void
{
DB::table('asset_external_sources')->updateOrInsert(
- ['asset_id' => $asset->id],
+ ['asset_id' => $asset->id, 'source' => $source],
$updates + ['updated_at' => now()],
);
}
diff --git a/tests/Feature/SyncAdapters/MultiAdapterExternalSourceIsolationTest.php b/tests/Feature/SyncAdapters/MultiAdapterExternalSourceIsolationTest.php
new file mode 100644
index 0000000000..f59a2de885
--- /dev/null
+++ b/tests/Feature/SyncAdapters/MultiAdapterExternalSourceIsolationTest.php
@@ -0,0 +1,96 @@
+<?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;
+
+/**
+ * Regression test for issue #19880. Before the fix,
+ * upsertExternalSource and loadExternalSourceInventory only keyed on
+ * asset_id, so when adapter B synced an asset already owned by
+ * adapter A, B's inventory columns (mac / ip / os / os_version /
+ * last_seen) overwrote A's row and B's own row stayed empty.
+ */
+class MultiAdapterExternalSourceIsolationTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ Statuslabel::factory()->rtd()->create();
+ }
+
+ public function test_second_adapter_writes_to_its_own_row_not_the_first_adapters()
+ {
+ $fleet = $this->configuredAdapter('fleet');
+ $sophos = $this->configuredAdapter('sophos_central');
+
+ // Adapter 1 creates the asset and seeds its own aes row.
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'fleet',
+ sourceId: 'fleet-ext-1',
+ hostname: 'shared-mac',
+ hardwareSerial: 'SHARED-SN',
+ hardwareModel: 'MacBook Pro',
+ primaryMac: 'AA:AA:AA:AA:AA:AA',
+ primaryIp: '10.0.0.1',
+ os: 'macOS',
+ osVersion: '14.5',
+ ));
+
+ $asset = Asset::where('serial', 'SHARED-SN')->firstOrFail();
+
+ // Adapter 2 opts into adopt_by_serial so it matches the existing
+ // Snipe-IT asset by serial instead of creating a new one.
+ SyncAdapterConfig::put($sophos->id, 'adopt_by_serial', '1');
+
+ SyncAdapter::syncFromRecord(new HostInventoryRecord(
+ sourceKey: 'sophos_central',
+ sourceId: 'sophos-ext-1',
+ hostname: 'shared-mac',
+ hardwareSerial: 'SHARED-SN',
+ hardwareModel: 'MacBook Pro',
+ primaryMac: 'BB:BB:BB:BB:BB:BB',
+ primaryIp: '10.0.0.2',
+ os: 'macOS',
+ osVersion: '15.0',
+ ));
+
+ $fleetRow = DB::table('asset_external_sources')
+ ->where('asset_id', $asset->id)
+ ->where('source', 'fleet')
+ ->first();
+ $sophosRow = DB::table('asset_external_sources')
+ ->where('asset_id', $asset->id)
+ ->where('source', 'sophos_central')
+ ->first();
+
+ // Fleet's row keeps Fleet's values. Pre-fix, Sophos clobbered
+ // these to BB:... / 10.0.0.2 / 15.0.
+ $this->assertSame('AA:AA:AA:AA:AA:AA', $fleetRow->primary_mac);
+ $this->assertSame('10.0.0.1', $fleetRow->primary_ip);
+ $this->assertSame('14.5', $fleetRow->os_version);
+
+ // Sophos's row has Sophos's values. Pre-fix, these were null.
+ $this->assertSame('BB:BB:BB:BB:BB:BB', $sophosRow->primary_mac);
+ $this->assertSame('10.0.0.2', $sophosRow->primary_ip);
+ $this->assertSame('15.0', $sophosRow->os_version);
+ }
+
+ private function configuredAdapter(string $slug): SyncAdapterInstance
+ {
+ $instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
+ SyncAdapterConfig::put($instance->id, 'url', 'https://example.com/'.$slug);
+ SyncAdapterConfig::put($instance->id, 'token', Crypt::encrypt('fake-token'));
+
+ return $instance->fresh();
+ }
+}