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
13 changes: 3 additions & 10 deletions internal/apiserver/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql"
"errors"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -652,18 +651,12 @@ func (db *database) getNextAvailableIPv4(ctx context.Context) (string, error) {
}

func (db *database) getNextAvailableIPv6(ctx context.Context) (string, error) {
lastUsedIP, err := db.queries.GetLastUsedIPV6(ctx)
allocated, err := db.queries.GetAllocatedIPv6(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) || strings.Contains(err.Error(), "converting NULL to string is unsupported") {
return db.ipv6Allocator.NextIP(nil)
}
return "", fmt.Errorf("getting last used ipv6: %w", err)
}
if lastUsedIP == "" {
return db.ipv6Allocator.NextIP(nil)
return "", fmt.Errorf("read allocated IPv6 addresses: %w", err)
}

return db.ipv6Allocator.NextIP([]string{lastUsedIP})
return db.ipv6Allocator.NextIP(allocated)
}

func (db *database) RemoveExpiredSessions(ctx context.Context) error {
Expand Down
108 changes: 108 additions & 0 deletions internal/apiserver/database/ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package database

import (
"context"
"fmt"
"net/netip"
"path/filepath"
"testing"

"github.com/nais/device/internal/apiserver/ip"
"github.com/nais/device/pkg/pb"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestEnrollmentWithLegacyIPv6(t *testing.T) {
tests := []struct {
name string
devices []string
gateways []string
want string
}{
{
name: "only legacy device",
devices: []string{"fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::1",
},
{
name: "only legacy gateway",
gateways: []string{"fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::1",
},
{
name: "current device hidden by legacy gateway",
devices: []string{"2600:1900:4150:f548::9"},
gateways: []string{"fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::a",
},
{
name: "current gateway hidden by legacy device",
devices: []string{"fd75:568f:d24:6::11d"},
gateways: []string{"2600:1900:4150:f548::9"},
want: "2600:1900:4150:f548::a",
},
{
name: "numeric maximum across both tables",
devices: []string{"2600:1900:4150:f548::f"},
gateways: []string{"2600:1900:4150:f548::10"},
want: "2600:1900:4150:f548::11",
},
{
name: "current devices without gateways",
devices: []string{"2600:1900:4150:f548::9"},
want: "2600:1900:4150:f548::a",
},
{
name: "current gateways without devices",
gateways: []string{"2600:1900:4150:f548::9"},
want: "2600:1900:4150:f548::a",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
prefix := netip.MustParsePrefix("2600:1900:4150:f548::/64")
v4 := ip.NewV4Allocator(netip.MustParsePrefix("10.0.0.0/24"), []string{"10.0.0.1"})
db, err := New(filepath.Join(t.TempDir(), "device.db"), v4, ip.NewV6Allocator(&prefix), false, logrus.New())
require.NoError(t, err)
conn := db.queries.(*Queries).db
t.Cleanup(func() { require.NoError(t, conn.Close()) })

for i, addr := range tt.devices {
_, err := conn.Exec(`INSERT INTO devices (username, serial, platform, public_key, ipv4, ipv6)
VALUES ('legacy-user', ?, 'linux', ?, ?, ?)`, fmt.Sprint(i), fmt.Sprintf("device-key-%d", i), fmt.Sprintf("10.0.0.%d", 10+i), addr)
require.NoError(t, err)
}
for i, addr := range tt.gateways {
_, err := conn.Exec(`INSERT INTO gateways (name, endpoint, public_key, ipv4, ipv6, password_hash)
VALUES (?, 'gateway:51820', ?, ?, ?, 'hash')`, fmt.Sprintf("legacy-%d", i), fmt.Sprintf("gateway-key-%d", i), fmt.Sprintf("10.0.0.%d", 20+i), addr)
require.NoError(t, err)
}

require.NoError(t, db.AddGateway(ctx, &pb.Gateway{Name: "new", PublicKey: "new-gateway-key"}))
gateway, err := db.ReadGateway(ctx, "new")
require.NoError(t, err)
want := netip.MustParseAddr(tt.want)
assert.Equal(t, want.StringExpanded(), gateway.Ipv6)

require.NoError(t, db.AddDevice(ctx, &pb.Device{Serial: "new", Platform: "linux", PublicKey: "new-device-key"}))
device, err := db.ReadDeviceBySerialPlatform(ctx, "new", "linux")
require.NoError(t, err)
assert.Equal(t, want.Next().StringExpanded(), device.Ipv6)

for i, addr := range tt.devices {
var stored string
require.NoError(t, conn.QueryRow("SELECT ipv6 FROM devices WHERE serial = ?", fmt.Sprint(i)).Scan(&stored))
assert.Equal(t, addr, stored)
}
for i, addr := range tt.gateways {
gateway, err := db.ReadGateway(ctx, fmt.Sprintf("legacy-%d", i))
require.NoError(t, err)
assert.Equal(t, addr, gateway.Ipv6)
}
})
}
}
7 changes: 4 additions & 3 deletions internal/apiserver/database/queries/ip.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
-- name: GetLastUsedIPV6 :one
SELECT CAST(MAX(MAX(devices.ipv6), MAX(gateways.ipv6)) AS text) AS ipv6
FROM devices, gateways;
-- name: GetAllocatedIPv6 :many
SELECT ipv6 FROM devices WHERE ipv6 != ''
UNION ALL
SELECT ipv6 FROM gateways WHERE ipv6 != '';
27 changes: 14 additions & 13 deletions internal/apiserver/ip/v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ip
import (
"fmt"
"net/netip"
"slices"
)

type v6Allocator struct {
Expand All @@ -21,21 +20,23 @@ func (a v6Allocator) NextIP(taken []string) (string, error) {
return "", fmt.Errorf("no prefix specified in allocator")
}

if len(taken) < 1 {
return a.prefix.Addr().Next().StringExpanded(), nil
last := a.prefix.Addr()
for _, raw := range taken {
if raw == "" {
continue
}
addr, err := netip.ParseAddr(raw)
if err != nil {
return "", fmt.Errorf("parse allocated IPv6 address %q: %w", raw, err)
}
if a.prefix.Contains(addr) && addr.Compare(last) > 0 {
last = addr
}
}

slices.Sort(taken)
last := taken[len(taken)-1]

addr, err := netip.ParseAddr(last)
if err != nil {
return "", fmt.Errorf("invalid ip address: %s", last)
}

next := addr.Next()
next := last.Next()
if !a.prefix.Contains(next) {
return "", fmt.Errorf("ip address %s is not in prefix %s", next, a.prefix)
return "", fmt.Errorf("no available IPv6 addresses after %s in prefix %s", last, a.prefix)
}

return next.StringExpanded(), nil
Expand Down
87 changes: 86 additions & 1 deletion internal/apiserver/ip/v6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindAvailableIPv6(t *testing.T) {
Expand All @@ -27,9 +28,93 @@ func TestFindAvailableIPv6(t *testing.T) {
assert.Equal(t, "fd75:568f:0d24:0001:0000:0000:0000:0004", availableIP)
})

t.Run("next ip is assigned based on largest ip in list (sorted as string)", func(t *testing.T) {
t.Run("next ip is assigned based on largest ip in list", func(t *testing.T) {
availableIP, err := ipAllocator.NextIP([]string{"fd75:568f:d24:1:0000:0000:0000:0002", "fd75:568f:d24:1:0000:0000:0000:0001"})
assert.NoError(t, err)
assert.Equal(t, "fd75:568f:0d24:0001:0000:0000:0000:0003", availableIP)
})
}

func TestNextIPv6WithLegacyAddresses(t *testing.T) {
prefix := netip.MustParsePrefix("2600:1900:4150:f548::/64")
allocator := NewV6Allocator(&prefix)
tests := []struct {
name string
taken []string
want string
}{
{
name: "only legacy addresses",
taken: []string{"fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::1",
},
{
name: "mixed prefixes",
taken: []string{"2600:1900:4150:f548::9", "fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::a",
},
{
name: "numeric ordering with compressed addresses",
taken: []string{"2600:1900:4150:f548::f", "2600:1900:4150:f548::10"},
want: "2600:1900:4150:f548::11",
},
{
name: "mixed compressed and expanded addresses",
taken: []string{"2600:1900:4150:f548::2", "2600:1900:4150:f548:0000:0000:0000:0010"},
want: "2600:1900:4150:f548::11",
},
{
name: "unassigned addresses",
taken: []string{"", "fd75:568f:d24:6::11d"},
want: "2600:1900:4150:f548::1",
},
{
name: "other global prefix",
taken: []string{"2600:1900:4150:f549::10"},
want: "2600:1900:4150:f548::1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := allocator.NextIP(tt.taken)
require.NoError(t, err)
assert.Equal(t, netip.MustParseAddr(tt.want).StringExpanded(), got)
})
}
}

func TestNextIPv6ReservesAPIServerAddress(t *testing.T) {
prefix := netip.MustParsePrefix("2600:1900:4150:f548::1/64")
got, err := NewV6Allocator(&prefix).NextIP([]string{"fd75:568f:d24:6::11d", "2600:1900:4150:f548::"})
require.NoError(t, err)
assert.Equal(t, netip.MustParseAddr("2600:1900:4150:f548::2").StringExpanded(), got)
}

func TestNextIPv6Errors(t *testing.T) {
t.Run("no prefix", func(t *testing.T) {
_, err := NewV6Allocator(nil).NextIP(nil)
require.ErrorContains(t, err, "no prefix specified")
})
t.Run("malformed allocated address", func(t *testing.T) {
prefix := netip.MustParsePrefix("2600:1900:4150:f548::/64")
_, err := NewV6Allocator(&prefix).NextIP([]string{"not-an-ip"})
require.ErrorContains(t, err, "parse allocated IPv6 address")
})
t.Run("no room after apiserver address", func(t *testing.T) {
prefix := netip.MustParsePrefix("2600:1900:4150:f548::1/128")
_, err := NewV6Allocator(&prefix).NextIP(nil)
require.ErrorContains(t, err, "no available IPv6 addresses")
})
}

func TestNextIPv6Exhaustion(t *testing.T) {
for _, cidr := range []string{"2600:1900:4150:f548::/126", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc/126"} {
t.Run(cidr, func(t *testing.T) {
prefix := netip.MustParsePrefix(cidr)
allocator := NewV6Allocator(&prefix)
last := prefix.Addr().Next().Next().Next()
_, err := allocator.NextIP([]string{last.String()})
require.Error(t, err)
})
}
}
20 changes: 10 additions & 10 deletions internal/apiserver/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading