A free open source IT asset/license management system
Use the v2 System Insights API for system info/hardware model
snipe0cab82e4d016
diff --git a/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php b/app/SyncAdapters/JumpCloud/JumpCloudAdapter.phpindex a1880e7dff..6de583dc18 100644--- a/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php+++ b/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php@@ -5,7 +5,9 @@ namespace App\SyncAdapters\JumpCloud;use App\SyncAdapters\HostInventoryRecord;use App\SyncAdapters\SyncAdapter;use Carbon\Carbon;+use Illuminate\Http\Client\RequestException;use Illuminate\Support\Arr;+use Illuminate\Support\Facades\Log;/*** JumpCloud adapter. Pulls managed-system inventory via the JumpCloud@@ -61,9 +63,65 @@ class JumpCloudAdapter extends SyncAdapter{$client = new JumpCloudClient(baseUrl: $this->url(), apiKey: $this->credential('token'));+ // V2 System Insights carries the authoritative hardware_model+ // and hardware_vendor strings for every OS. V1 /systems only+ // has `systemType` (free-form, often null for non-Mac) and no+ // vendor. Prefetch once, join on system_id while iterating V1.+ //+ // System Insights is included on every paid JumpCloud plan,+ // but a scoped API key without the right permission bits can+ // still 403. A 403 (or any transport error) here is not fatal,+ // we fall back to V1-only normalization and the record keeps+ // the same way it worked before this adapter added V2 support.+ $insightsBySystemId = $this->fetchSystemInsights($client);+foreach ($client->systems() as $system) {- yield $this->normalize($system);+ $systemId = (string) Arr::get($system, '_id');+ $insight = $insightsBySystemId[$systemId] ?? null;+ yield $this->normalize($system, $insight);+ }+ }++ /**+ * Load every V2 System Insights `system_info` record into a map+ * keyed by system_id. Returns an empty array when the API key is+ * missing System Insights permission (403) or when the endpoint+ * fails for any other reason. The adapter then falls back to V1+ * fields on every record.+ *+ * @return array<string, array<string, mixed>>+ */+ private function fetchSystemInsights(JumpCloudClient $client): array+ {+ $map = [];++ try {+ foreach ($client->systemInsights() as $insight) {+ $systemId = Arr::get($insight, 'system_id');+ if (is_string($systemId) && $systemId !== '') {+ $map[$systemId] = $insight;+ }+ }+ } catch (RequestException $e) {+ // 403 is the signal that this tenant's plan does not+ // include System Insights, which is a design-expected+ // outcome, not a configuration bug. Lower the log level+ // and keep going. Other 4xx/5xx get warning-level since+ // they likely need operator attention.+ $status = $e->response?->status();+ if ($status === 403) {+ Log::channel('sync-adapters')->info(+ $this->name().': API key does not have System Insights permission (403), falling back to V1 /systems fields only.',+ );+ } else {+ Log::channel('sync-adapters')->warning(+ $this->name().': V2 System Insights fetch failed (HTTP '.($status ?? 'n/a').'), falling back to V1 /systems fields only.',+ ['exception' => $e->getMessage()],+ );+ }}++ return $map;}/**@@ -71,22 +129,31 @@ class JumpCloudAdapter extends SyncAdapter* shape. JumpCloud's `_id` is stable per enrolled system and is* what we key asset_external_sources on.*+ * When a V2 System Insights record is available for the same+ * system_id, prefer its hardware_model and hardware_vendor since+ * those are the fields JumpCloud actually populates for every OS.+ * V1 systemType / the darwin => Apple guess are the fallbacks.+ ** The bulk /systems endpoint doesn't return network interfaces, so* primaryMac stays null. A future enrichment pass could hit* /systems/{id}/networkinterfaces per record to fill it in, at the* cost of N+1 requests per sync run.** @param array<string, mixed> $system+ * @param array<string, mixed>|null $insight*/- private function normalize(array $system): HostInventoryRecord+ private function normalize(array $system, ?array $insight = null): HostInventoryRecord{+ $insightModel = $insight !== null ? Arr::get($insight, 'hardware_model') : null;+ $insightVendor = $insight !== null ? Arr::get($insight, 'hardware_vendor') : null;+return new HostInventoryRecord(sourceKey: $this->name(),sourceId: (string) Arr::get($system, '_id'),hostname: Arr::get($system, 'hostname') ?? Arr::get($system, 'displayName'),hardwareSerial: Arr::get($system, 'serialNumber'),- hardwareModel: Arr::get($system, 'systemType'),- manufacturer: $this->guessManufacturer(Arr::get($system, 'osFamily')),+ hardwareModel: $this->firstNonEmptyString($insightModel, Arr::get($system, 'systemType')),+ manufacturer: $this->firstNonEmptyString($insightVendor, $this->guessManufacturer(Arr::get($system, 'osFamily'))),primaryMac: null,primaryIp: null,os: Arr::get($system, 'os'),@@ -102,8 +169,26 @@ class JumpCloudAdapter extends SyncAdapter}/**- * JumpCloud doesn't return a manufacturer field. Infer from- * osFamily so downstream mapping still gets a reasonable value.+ * Pick the first non-empty string from the arguments, or null when+ * none qualify. Lets normalize() express "prefer V2, fall back to+ * V1" without triggering on empty strings that V2 sometimes emits+ * for fields the agent could not collect.+ */+ private function firstNonEmptyString(mixed ...$candidates): ?string+ {+ foreach ($candidates as $candidate) {+ if (is_string($candidate) && $candidate !== '') {+ return $candidate;+ }+ }++ return null;+ }++ /**+ * JumpCloud V1 doesn't return a manufacturer field. Infer from+ * osFamily so downstream mapping still gets a reasonable value+ * when the V2 hardware_vendor fallback also isn't available.* darwin => Apple, windows / linux stay unknown because the vendor* varies per hardware.*/diff --git a/app/SyncAdapters/JumpCloud/JumpCloudClient.php b/app/SyncAdapters/JumpCloud/JumpCloudClient.phpindex 518def209f..2887bed8f5 100644--- a/app/SyncAdapters/JumpCloud/JumpCloudClient.php+++ b/app/SyncAdapters/JumpCloud/JumpCloudClient.php@@ -61,6 +61,49 @@ class JumpCloudClient} while (! $done);}+ /**+ * Iterate the V2 System Insights `system_info` collection. This is+ * the only JumpCloud endpoint that returns actual hardware_model+ * and hardware_vendor strings. V1 `/systems` has `systemType` which+ * is a free-form label (often null for non-Mac enrollments) and+ * no vendor at all.+ *+ * System Insights is a paid-tier feature on some JumpCloud plans.+ * A tenant without the entitlement gets 403 here, which the caller+ * is expected to translate into "fall back to V1 shape" rather+ * than aborting the sync. This method does not catch the 403 so+ * the adapter can decide the recovery policy explicitly.+ *+ * Returns the V2 flat shape unchanged. The adapter joins to V1+ * `/systems` on `system_id`.+ *+ * @return iterable<array<string, mixed>>+ */+ public function systemInsights(int $limit = 100): iterable+ {+ $skip = 0;++ do {+ $response = $this->request()+ ->get('/v2/systeminsights/system_info', [+ 'limit' => $limit,+ 'skip' => $skip,+ ])+ ->throw()+ ->json();++ // V2 System Insights returns a flat array of records (no+ // envelope / totalCount), so pagination terminates on a+ // short page.+ $results = is_array($response) ? $response : [];+ foreach ($results as $insight) {+ yield $insight;+ }++ $skip += $limit;+ } while (count($results) >= $limit);+ }+private function request(): PendingRequest{return $this->withTransientRetry(diff --git a/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php b/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.phpindex 67dc014fd5..d8f4d621c0 100644--- a/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php+++ b/tests/Feature/SyncAdapters/JumpCloud/JumpCloudAdapterTest.php@@ -31,6 +31,7 @@ class JumpCloudAdapterTest extends TestCase$adapter = $this->configuredJumpCloudAdapter();Http::fake([+ '*/v2/systeminsights/system_info*' => Http::response([]),'*/systems*' => Http::response(['totalCount' => 2,'results' => [@@ -55,6 +56,7 @@ class JumpCloudAdapterTest extends TestCase$adapter = $this->configuredJumpCloudAdapter();Http::fake([+ '*/v2/systeminsights/system_info*' => Http::response([]),'*/systems*' => Http::response(['totalCount' => 0, 'results' => []]),]);@@ -75,6 +77,7 @@ class JumpCloudAdapterTest extends TestCase$adapter = $this->configuredJumpCloudAdapter();Http::fake([+ '*/v2/systeminsights/system_info*' => Http::response([]),'*/systems*' => Http::response(['totalCount' => 1,'results' => [@@ -107,6 +110,114 @@ class JumpCloudAdapterTest extends TestCase$this->assertNotNull($record->lastSeen);}+ public function test_v2_system_insights_populates_hardware_model_and_vendor()+ {+ // Regression for the Reddit report where V1 /systems returned+ // null systemType, leaving hardwareModel null and asset+ // creation failing on the model_id required rule. V2 System+ // Insights carries the real hardware_model and hardware_vendor+ // and the adapter prefers them over the V1 fallbacks.+ $adapter = $this->configuredJumpCloudAdapter();++ Http::fake([+ '*/v2/systeminsights/system_info*' => Http::sequence()+ ->push([+ [+ 'system_id' => 'sys-win',+ 'hardware_model' => 'Latitude 5540',+ 'hardware_vendor' => 'Dell Inc.',+ ],+ [+ 'system_id' => 'sys-mac',+ 'hardware_model' => 'MacBookPro18,3',+ 'hardware_vendor' => 'Apple Inc.',+ ],+ ])+ ->push([]),+ '*/systems*' => Http::response([+ 'totalCount' => 2,+ 'results' => [+ $this->jumpcloudSystem(id: 'sys-win', hostname: 'win-laptop', systemType: null, osFamily: 'windows', os: 'Windows'),+ $this->jumpcloudSystem(id: 'sys-mac', hostname: 'mac-laptop', systemType: 'Mac', osFamily: 'darwin', os: 'Mac OS X'),+ ],+ ]),+ ]);++ $records = iterator_to_array($adapter->pull());+ $this->assertCount(2, $records);++ $win = collect($records)->firstWhere('sourceId', 'sys-win');+ $this->assertSame('Latitude 5540', $win->hardwareModel);+ $this->assertSame('Dell Inc.', $win->manufacturer);++ $mac = collect($records)->firstWhere('sourceId', 'sys-mac');+ $this->assertSame('MacBookPro18,3', $mac->hardwareModel, 'V2 hardware_model wins over V1 systemType.');+ $this->assertSame('Apple Inc.', $mac->manufacturer, 'V2 hardware_vendor wins over the darwin => Apple guess.');+ }++ public function test_v2_system_insights_403_falls_back_to_v1_fields()+ {+ // An API key without System Insights permission returns 403.+ // The adapter must log and keep going with V1-only normalization+ // so the sync still produces usable records for assets that have+ // a non-null V1 systemType, instead of aborting the whole run.+ $adapter = $this->configuredJumpCloudAdapter();++ Http::fake([+ '*/v2/systeminsights/system_info*' => Http::response([], 403),+ '*/systems*' => Http::response([+ 'totalCount' => 1,+ 'results' => [+ $this->jumpcloudSystem(id: 'sys-fallback', hostname: 'fallback-mac', systemType: 'Mac', osFamily: 'darwin'),+ ],+ ]),+ ]);++ $records = iterator_to_array($adapter->pull());+ $this->assertCount(1, $records);+ $this->assertSame('Mac', $records[0]->hardwareModel);+ $this->assertSame('Apple', $records[0]->manufacturer);+ }++ public function test_v2_insights_missing_for_a_system_falls_back_to_v1_for_that_record_only()+ {+ // Mixed scenario: one system is in both V1 and V2, the other+ // is only in V1 (newly enrolled, agent has not reported system+ // info yet). The second record must still produce a usable row+ // via the V1 fallbacks instead of a null model.+ $adapter = $this->configuredJumpCloudAdapter();++ Http::fake([+ '*/v2/systeminsights/system_info*' => Http::sequence()+ ->push([+ [+ 'system_id' => 'sys-known',+ 'hardware_model' => 'Precision 5570',+ 'hardware_vendor' => 'Dell Inc.',+ ],+ ])+ ->push([]),+ '*/systems*' => Http::response([+ 'totalCount' => 2,+ 'results' => [+ $this->jumpcloudSystem(id: 'sys-known', hostname: 'known-host', systemType: null, osFamily: 'linux'),+ $this->jumpcloudSystem(id: 'sys-pending', hostname: 'pending-host', systemType: 'Mac', osFamily: 'darwin'),+ ],+ ]),+ ]);++ $records = iterator_to_array($adapter->pull());+ $this->assertCount(2, $records);++ $known = collect($records)->firstWhere('sourceId', 'sys-known');+ $this->assertSame('Precision 5570', $known->hardwareModel);+ $this->assertSame('Dell Inc.', $known->manufacturer);++ $pending = collect($records)->firstWhere('sourceId', 'sys-pending');+ $this->assertSame('Mac', $pending->hardwareModel);+ $this->assertSame('Apple', $pending->manufacturer);+ }+private function configuredJumpCloudAdapter(): JumpCloudAdapter{$instance = SyncAdapterInstance::where('slug', 'jumpcloud')->firstOrFail();@@ -122,7 +233,7 @@ class JumpCloudAdapterTest extends TestCaseprivate function jumpcloudSystem(string $id,string $hostname = 'system',- string $systemType = 'Mac',+ ?string $systemType = 'Mac',?string $osFamily = 'darwin',?string $os = 'Mac OS X',?string $version = null,