Skip to content

Commit b098562

Browse files
committed
fix: log in with different use should create new machine entry
1 parent 46cce89 commit b098562

File tree

7 files changed

+56
-6
lines changed

7 files changed

+56
-6
lines changed

internal/domain/machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ func (r *repository) GetNextMachineNameIndex(ctx context.Context, tailnetID uint
389389
return m.NameIdx + 1, nil
390390
}
391391

392-
func (r *repository) GetMachineByKey(ctx context.Context, tailnetID uint64, machineKey string) (*Machine, error) {
392+
func (r *repository) GetMachineByKeyAndUser(ctx context.Context, machineKey string, userID uint64) (*Machine, error) {
393393
var m Machine
394-
tx := r.withContext(ctx).Preload("Tailnet").Preload("User").Take(&m, "tailnet_id = ? AND machine_key = ?", tailnetID, machineKey)
394+
tx := r.withContext(ctx).Preload("Tailnet").Preload("User").Take(&m, "machine_key = ? AND user_id = ?", machineKey, userID)
395395

396396
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
397397
return nil, nil

internal/domain/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Repository interface {
5858
SaveMachine(ctx context.Context, m *Machine) error
5959
DeleteMachine(ctx context.Context, id uint64) (bool, error)
6060
GetMachine(ctx context.Context, id uint64) (*Machine, error)
61-
GetMachineByKey(ctx context.Context, tailnetID uint64, key string) (*Machine, error)
61+
GetMachineByKeyAndUser(ctx context.Context, key string, userID uint64) (*Machine, error)
6262
GetMachineByKeys(ctx context.Context, machineKey string, nodeKey string) (*Machine, error)
6363
CountMachinesWithIPv4(ctx context.Context, ip string) (int64, error)
6464
GetNextMachineNameIndex(ctx context.Context, tailnetID uint64, name string) (uint64, error)

internal/handlers/authentication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func (h *AuthenticationHandlers) endMachineRegistrationFlow(c echo.Context, form
446446

447447
var m *domain.Machine
448448

449-
m, err := h.repository.GetMachineByKey(ctx, tailnet.ID, machineKey)
449+
m, err := h.repository.GetMachineByKeyAndUser(ctx, machineKey, user.ID)
450450
if err != nil {
451451
return logError(err)
452452
}

internal/handlers/registration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (h *RegistrationHandlers) authenticateMachineWithAuthKey(c echo.Context, ma
173173

174174
var m *domain.Machine
175175

176-
m, err = h.repository.GetMachineByKey(ctx, tailnet.ID, machineKey)
176+
m, err = h.repository.GetMachineByKeyAndUser(ctx, machineKey, user.ID)
177177
if err != nil {
178178
return logError(err)
179179
}

tests/switch_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tests
2+
3+
import (
4+
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
5+
"github.com/jsiebens/ionscale/tests/sc"
6+
"github.com/jsiebens/ionscale/tests/tsn"
7+
"github.com/stretchr/testify/require"
8+
"net/http"
9+
"testing"
10+
)
11+
12+
func TestSwitchAccounts(t *testing.T) {
13+
sc.Run(t, func(s *sc.Scenario) {
14+
s.PushOIDCUser("123", "[email protected]", "john")
15+
s.PushOIDCUser("124", "[email protected]", "jane")
16+
17+
tailnet := s.CreateTailnet()
18+
s.SetIAMPolicy(tailnet.Id, &api.IAMPolicy{Filters: []string{"domain == localtest.me"}})
19+
20+
node := s.NewTailscaleNode(sc.WithName("switch"))
21+
22+
code, err := node.LoginWithOidc()
23+
require.NoError(t, err)
24+
require.Equal(t, http.StatusOK, code)
25+
26+
require.NoError(t, node.WaitFor(tsn.Connected()))
27+
require.NoError(t, node.Check(tsn.HasUser("[email protected]")))
28+
require.NoError(t, node.Check(tsn.HasName("switch")))
29+
30+
code, err = node.LoginWithOidc()
31+
require.NoError(t, err)
32+
require.Equal(t, http.StatusOK, code)
33+
34+
require.NoError(t, node.WaitFor(tsn.Connected()))
35+
require.NoError(t, node.Check(tsn.HasUser("[email protected]")))
36+
require.NoError(t, node.Check(tsn.HasName("switch-1")))
37+
38+
machines := s.ListMachines(tailnet.Id)
39+
require.Equal(t, 2, len(machines))
40+
require.Equal(t, "switch", machines[0].Name)
41+
require.Equal(t, "switch-1", machines[1].Name)
42+
})
43+
}

tests/tsn/conditions.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tsn
22

33
import (
44
"slices"
5+
"strings"
56
"tailscale.com/ipn/ipnstate"
67
"tailscale.com/tailcfg"
78
"tailscale.com/types/views"
@@ -27,6 +28,12 @@ func HasTag(tag string) Condition {
2728
}
2829
}
2930

31+
func HasName(name string) Condition {
32+
return func(status *ipnstate.Status) bool {
33+
return status.Self != nil && strings.HasPrefix(status.Self.DNSName, name)
34+
}
35+
}
36+
3037
func NeedsMachineAuth() Condition {
3138
return func(status *ipnstate.Status) bool {
3239
return status.BackendState == "NeedsMachineAuth"

tests/tsn/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (t *TailscaleNode) LoginWithOidc(flags ...UpFlag) (int, error) {
4747
return strings.Contains(stderr, "To authenticate, visit:")
4848
}
4949

50-
cmd := []string{"up", "--login-server", t.loginServer}
50+
cmd := []string{"login", "--login-server", t.loginServer}
5151
for _, f := range flags {
5252
cmd = append(cmd, f...)
5353
}

0 commit comments

Comments
 (0)