Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/Support/Integrations/Tailscale/TailscaleDevices.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ public function link(Integration $integration): void
$taken = $rows->pluck('machine_id')->filter()->all();
$free = $machines->reject(fn (Machine $machine) => in_array($machine->id, $taken, true))->values();

// Count hostname candidates before linking rows, so row order can't
// turn multiple possible devices into an apparently unique match.
$hostnameMatches = [];
foreach ($rows->filter(fn (TailscaleDevice $candidate) => $candidate->machine_id === null && ! $candidate->external) as $candidate) {
foreach ($machines as $machine) {
if (self::byHostname($candidate, $machine)) {
$hostnameMatches[$machine->id] = ($hostnameMatches[$machine->id] ?? 0) + 1;
}
}
}

foreach ($rows->whereNull('machine_id') as $row) {
if ($row->external) {
continue;
Expand All @@ -102,6 +113,9 @@ public function link(Integration $integration): void
foreach ($tests as $test) {
$found = $free->filter(fn (Machine $machine) => $test($row, $machine))->values();
if ($found->count() === 1) {
if ($how === TailscaleDevice::HOSTNAME && ($hostnameMatches[$found->first()->id] ?? 0) !== 1) {
continue;
}
$match = $found->first();
break;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/Integrations/TailscaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ function tsAccountProps(Workspace $workspace, Integration $integration): array
expect(TailscaleDevice::query()->where('machine_id', $db->id)->exists())->toBeFalse();
});

test('duplicate Tailscale hostnames do not link an arbitrary device to one machine', function () {
$workspace = Workspace::factory()->create();
$integration = tsAccount($workspace);
$machine = tsMachine($workspace, 'web-1');
$this->tailscale->device('web-1', '100.64.0.7');
$this->tailscale->device('web-1', '100.64.0.8');

$props = tsAccountProps($workspace, $integration);

expect(collect($props['devices'])->pluck('machine')->filter()->all())->toBe([])
->and(collect($props['machines'])->pluck('id')->all())->toContain($machine->id);
});

test('add to tailnet makes a one-off key, deploys Tailscale with it, and links the device that turns up', function () {
$workspace = Workspace::factory()->create();
$integration = tsAccount($workspace);
Expand Down