A free open source IT asset/license management system
Fixed #19754 - handle two IMEI numbers, format MAC addresses
snipe70aa7302c308
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.phpindex 1d45e0a01a..49d8acf0d4 100644--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php@@ -247,7 +247,11 @@ class AppleBusinessManagerAdapter extends SyncAdapter'abm_wifi_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_wifi_mac'],'abm_bluetooth_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_bluetooth_mac'],'abm_ethernet_mac' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_ethernet_mac'],- 'abm_imei' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_imei'],+ // Apple returns IMEIs as an array (dual-SIM iPhones carry+ // two, older devices one). Positional split so admins can+ // map each to its own custom field. #19754.+ 'abm_imei_1' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_imei_1'],+ 'abm_imei_2' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_imei_2'],'abm_meid' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_meid'],'abm_eid' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_eid'],'abm_added_to_org' => ['label_key' => 'admin/settings/sync_adapters.abm_extra_added_to_org'],@@ -869,13 +873,13 @@ class AppleBusinessManagerAdapter extends SyncAdapter*/private static function pickMdmPrimaryMac(array $attrs, ?string $fallback): ?string{- $wifi = Arr::get($attrs, 'wifiMacAddress');- if (is_string($wifi) && $wifi !== '') {+ $wifi = self::normalizeMacAddress(Arr::get($attrs, 'wifiMacAddress'));+ if ($wifi !== null) {return $wifi;}- $ethernet = Arr::get($attrs, 'ethernetMacAddress');- if (is_string($ethernet) && $ethernet !== '') {+ $ethernet = self::normalizeMacAddress(Arr::get($attrs, 'ethernetMacAddress'));+ if ($ethernet !== null) {return $ethernet;}@@ -921,23 +925,88 @@ class AppleBusinessManagerAdapter extends SyncAdapter$extra['abm_mdm_storage_total'] = Arr::get($attrs, 'storageTotalCapacity');$extra['abm_mdm_storage_free'] = Arr::get($attrs, 'storageFreeCapacity');- $sharedMap = [+ $macMap = ['wifiMacAddress' => 'abm_wifi_mac','bluetoothMacAddress' => 'abm_bluetooth_mac','ethernetMacAddress' => 'abm_ethernet_mac',- 'imei' => 'abm_imei',- 'meid' => 'abm_meid',];- foreach ($sharedMap as $mdmKey => $extraKey) {- $val = Arr::get($attrs, $mdmKey);- if ($val !== null && $val !== '') {+ foreach ($macMap as $mdmKey => $extraKey) {+ $val = self::normalizeMacAddress(Arr::get($attrs, $mdmKey));+ if ($val !== null) {$extra[$extraKey] = $val;}}+ // IMEI is an array in both orgDevices and MDM-detail shapes.+ // Overwrite each positional slot only when MDM has a value so+ // the orgDevices value stays as the fallback.+ $mdmImei = Arr::get($attrs, 'imei');+ $first = self::extractImeiAtIndex($mdmImei, 0);+ if ($first !== null) {+ $extra['abm_imei_1'] = $first;+ }+ $second = self::extractImeiAtIndex($mdmImei, 1);+ if ($second !== null) {+ $extra['abm_imei_2'] = $second;+ }++ $meid = Arr::get($attrs, 'meid');+ if ($meid !== null && $meid !== '') {+ $extra['abm_meid'] = $meid;+ }+return $extra;}+ /**+ * Insert the colons Snipe-IT's MAC custom-field validator+ * requires. Apple's APIs return bare 12-hex-char strings+ * (`0123456789AB`) and occasionally the 16-char EUI-64 shape for+ * Bluetooth. Both get normalized to colon-separated uppercase+ * pairs. Non-hex characters are stripped so an already-formatted+ * value round-trips cleanly. Returns null for null, empty, or+ * non-string input, or when the stripped hex isn't a valid length.+ */+ private static function normalizeMacAddress(mixed $raw): ?string+ {+ if (! is_string($raw) || $raw === '') {+ return null;+ }++ $hex = strtoupper((string) preg_replace('/[^0-9A-Fa-f]/', '', $raw));+ $length = strlen($hex);+ if ($length !== 12 && $length !== 16) {+ return null;+ }++ return rtrim(chunk_split($hex, 2, ':'), ':');+ }++ /**+ * Pick a single IMEI out of Apple's `imei` field, which is+ * typically an array (dual-SIM carries two, single-SIM carries a+ * one-element array). A bare-scalar shape is tolerated as a+ * fallback so vendor-side drift doesn't drop a valid IMEI into+ * nothing - a scalar fills slot 0 and leaves slot 1 null.+ * Returns null when the slot is absent, when the value isn't a+ * non-empty string, or when a scalar is requested for slot 1.+ */+ private static function extractImeiAtIndex(mixed $value, int $index): ?string+ {+ if (is_string($value)) {+ return $index === 0 && $value !== '' ? $value : null;+ }+ if (! is_array($value)) {+ return null;+ }+ $imei = $value[$index] ?? null;+ if (! is_string($imei) || $imei === '') {+ return null;+ }++ return $imei;+ }+/*** Apple's productFamily enum to the runtime OS name customers* expect to see in the assets table. Returns null for families@@ -1067,8 +1136,9 @@ class AppleBusinessManagerAdapter extends SyncAdapter// is the one every enrolled device has, so it fills the// normalized primaryMac field. Ethernet and Bluetooth// stay in extras for admins who want to route them to- // custom fields.- primaryMac: Arr::get($attrs, 'wifiMacAddress'),+ // custom fields. normalizeMacAddress() inserts the colons+ // Snipe-IT's MAC custom-field validator requires.+ primaryMac: self::normalizeMacAddress(Arr::get($attrs, 'wifiMacAddress')),primaryIp: null,os: null,osVersion: null,@@ -1088,12 +1158,15 @@ class AppleBusinessManagerAdapter extends SyncAdapter'abm_purchase_source_id' => Arr::get($attrs, 'purchaseSourceId'),'abm_mdm_server' => $deviceToServer[$id] ?? null,'abm_device_capacity' => Arr::get($attrs, 'deviceCapacity'),- 'abm_wifi_mac' => Arr::get($attrs, 'wifiMacAddress'),- 'abm_bluetooth_mac' => Arr::get($attrs, 'bluetoothMacAddress'),- 'abm_ethernet_mac' => Arr::get($attrs, 'ethernetMacAddress'),+ 'abm_wifi_mac' => self::normalizeMacAddress(Arr::get($attrs, 'wifiMacAddress')),+ 'abm_bluetooth_mac' => self::normalizeMacAddress(Arr::get($attrs, 'bluetoothMacAddress')),+ 'abm_ethernet_mac' => self::normalizeMacAddress(Arr::get($attrs, 'ethernetMacAddress')),// Cellular-only identifiers. Populated for iPhones and// cellular iPads, null on Macs / non-cellular devices.- 'abm_imei' => Arr::get($attrs, 'imei'),+ // IMEI is an array in Apple's shape; _1/_2 split by+ // index so admins can map each to its own custom field.+ 'abm_imei_1' => self::extractImeiAtIndex(Arr::get($attrs, 'imei'), 0),+ 'abm_imei_2' => self::extractImeiAtIndex(Arr::get($attrs, 'imei'), 1),'abm_meid' => Arr::get($attrs, 'meid'),'abm_eid' => Arr::get($attrs, 'eid'),'abm_added_to_org' => self::parseOrderDate(Arr::get($attrs, 'addedToOrgDateTime')),diff --git a/resources/lang/en-US/admin/settings/sync_adapters.php b/resources/lang/en-US/admin/settings/sync_adapters.phpindex 09011ab088..4f9865c7f3 100644--- a/resources/lang/en-US/admin/settings/sync_adapters.php+++ b/resources/lang/en-US/admin/settings/sync_adapters.php@@ -422,7 +422,8 @@ return ['abm_extra_wifi_mac' => 'AxM Wi-Fi MAC','abm_extra_bluetooth_mac' => 'AxM Bluetooth MAC','abm_extra_ethernet_mac' => 'AxM Ethernet MAC',- 'abm_extra_imei' => 'AxM IMEI',+ 'abm_extra_imei_1' => 'AxM IMEI 1',+ 'abm_extra_imei_2' => 'AxM IMEI 2','abm_extra_meid' => 'AxM MEID','abm_extra_eid' => 'AxM EID','abm_extra_added_to_org' => 'AxM Added to Org',diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.phpindex b30573ac15..80ebd05fcc 100644--- a/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/ActivationLockEnrichmentTest.php@@ -124,7 +124,7 @@ class ActivationLockEnrichmentTest extends TestCase'wifiMacAddress' => 'AA:BB:CC:00:11:22','bluetoothMacAddress' => 'AA:BB:CC:00:11:23','ethernetMacAddress' => null,- 'imei' => '351234567890123',+ 'imei' => ['351234567890123'],'meid' => '35123456789012','eid' => '89012345678901234567890123456789','addedToOrgDateTime' => '2024-11-01T14:22:00Z',@@ -142,7 +142,8 @@ class ActivationLockEnrichmentTest extends TestCase$this->assertSame('AA:BB:CC:00:11:22', $record->extra['abm_wifi_mac']);$this->assertSame('AA:BB:CC:00:11:23', $record->extra['abm_bluetooth_mac']);$this->assertNull($record->extra['abm_ethernet_mac']);- $this->assertSame('351234567890123', $record->extra['abm_imei']);+ $this->assertSame('351234567890123', $record->extra['abm_imei_1']);+ $this->assertNull($record->extra['abm_imei_2']);$this->assertSame('35123456789012', $record->extra['abm_meid']);$this->assertSame('89012345678901234567890123456789', $record->extra['abm_eid']);$this->assertSame('2024-11-01', $record->extra['abm_added_to_org']);diff --git a/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.php b/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.phpnew file mode 100644index 0000000000..f6199819c0--- /dev/null+++ b/tests/Feature/SyncAdapters/AppleBusinessManager/MacAndImeiNormalizationTest.php@@ -0,0 +1,233 @@+<?php++namespace Tests\Feature\SyncAdapters\AppleBusinessManager;++use App\Models\Statuslabel;+use App\Models\SyncAdapterConfig;+use App\Models\SyncAdapterInstance;+use App\SyncAdapters\AppleBusinessManager\AppleBusinessManagerAdapter;+use Illuminate\Support\Facades\Crypt;+use Illuminate\Support\Facades\Http;+use Tests\TestCase;++/**+ * Issue #19754: Apple returns MAC addresses without colons (which+ * Snipe-IT's `mac_address` custom-field validator rejects) and IMEIs+ * as a JSON array (dual-SIM iPhones carry two, older devices one).+ * The adapter normalizes both before yielding records.+ */+class MacAndImeiNormalizationTest extends TestCase+{+ private string $privateKeyPem;++ protected function setUp(): void+ {+ parent::setUp();+ Statuslabel::factory()->rtd()->create();++ $key = openssl_pkey_new([+ 'private_key_type' => OPENSSL_KEYTYPE_EC,+ 'curve_name' => 'prime256v1',+ ]);+ $pem = '';+ openssl_pkey_export($key, $pem);+ $this->privateKeyPem = $pem;+ }++ public function test_bare_hex_mac_addresses_normalize_to_colon_separated_uppercase()+ {+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-dual-sim-iphone',+ 'attributes' => [+ 'serialNumber' => 'SN-IPHONE-1',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'wifiMacAddress' => '0123456789ab',+ 'bluetoothMacAddress' => 'aabbccddeeff',+ 'ethernetMacAddress' => '112233445566',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('01:23:45:67:89:AB', $record->primaryMac);+ $this->assertSame('01:23:45:67:89:AB', $record->extra['abm_wifi_mac']);+ $this->assertSame('AA:BB:CC:DD:EE:FF', $record->extra['abm_bluetooth_mac']);+ $this->assertSame('11:22:33:44:55:66', $record->extra['abm_ethernet_mac']);+ }++ public function test_already_colon_separated_mac_round_trips_unchanged()+ {+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-already-formatted',+ 'attributes' => [+ 'serialNumber' => 'SN-ALREADY',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'wifiMacAddress' => 'aa:bb:cc:00:11:22',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('AA:BB:CC:00:11:22', $record->primaryMac);+ }++ public function test_malformed_mac_falls_through_as_null()+ {+ // A short / junk value shouldn't crash and shouldn't poison the+ // custom field with an invalid value either. Null is the right+ // outcome so the record saves and the admin sees the empty slot.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-bad-mac',+ 'attributes' => [+ 'serialNumber' => 'SN-BAD-MAC',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'wifiMacAddress' => 'not-a-mac',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertNull($record->primaryMac);+ $this->assertNull($record->extra['abm_wifi_mac']);+ }++ public function test_dual_sim_iphone_populates_both_imei_slots()+ {+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-dual-imei',+ 'attributes' => [+ 'serialNumber' => 'SN-DUAL-IMEI',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'imei' => ['353912345678901', '353912345678902'],+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('353912345678901', $record->extra['abm_imei_1']);+ $this->assertSame('353912345678902', $record->extra['abm_imei_2']);+ }++ public function test_single_imei_iphone_populates_only_slot_one()+ {+ // Older single-SIM iPhones still return a one-element array.+ // _1 fills, _2 stays null.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-single-imei',+ 'attributes' => [+ 'serialNumber' => 'SN-SINGLE-IMEI',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'imei' => ['353912345678901'],+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('353912345678901', $record->extra['abm_imei_1']);+ $this->assertNull($record->extra['abm_imei_2']);+ }++ public function test_scalar_imei_falls_back_to_slot_one()+ {+ // Defensive: Apple's documented shape is an array, but if a+ // response ever drops back to a bare scalar we populate slot 1+ // rather than silently losing the value.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-scalar-imei',+ 'attributes' => [+ 'serialNumber' => 'SN-SCALAR-IMEI',+ 'partNumber' => 'MQ8T3LL/A',+ 'productFamily' => 'iPhone',+ 'imei' => '353912345678901',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertSame('353912345678901', $record->extra['abm_imei_1']);+ $this->assertNull($record->extra['abm_imei_2']);+ }++ public function test_non_cellular_device_leaves_both_imei_slots_null()+ {+ // Macs and non-cellular iPads don't carry an IMEI at all.+ $adapter = $this->configuredAdapter();++ Http::fake([+ 'account.apple.com/*' => Http::response(['access_token' => 'stub-bearer']),+ 'api-business.apple.com/v1/orgDevices*' => Http::response([+ 'data' => [[+ 'id' => 'abm-mac',+ 'attributes' => [+ 'serialNumber' => 'SN-MAC',+ 'partNumber' => 'MK1E3LL/A',+ 'productFamily' => 'Mac',+ ],+ ]],+ ]),+ ]);++ $record = iterator_to_array($adapter->pull())[0];++ $this->assertNull($record->extra['abm_imei_1']);+ $this->assertNull($record->extra['abm_imei_2']);+ }++ private function configuredAdapter(): AppleBusinessManagerAdapter+ {+ $instance = SyncAdapterInstance::where('slug', 'abm')->firstOrFail();+ SyncAdapterConfig::put($instance->id, 'mode', 'business');+ SyncAdapterConfig::put($instance->id, 'client_id', 'stub-client-id');+ SyncAdapterConfig::put($instance->id, 'key_id', 'stub-key-id');+ SyncAdapterConfig::put($instance->id, 'private_key', Crypt::encrypt($this->privateKeyPem));++ return new AppleBusinessManagerAdapter($instance->fresh());+ }+}