2022-10-24 16:41:40 +02:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2024-12-10 16:23:55 +01:00
|
|
|
"cmp"
|
2022-10-24 16:41:40 +02:00
|
|
|
"encoding/json"
|
2022-11-28 15:54:23 +00:00
|
|
|
"fmt"
|
2024-09-07 09:23:58 +02:00
|
|
|
"strings"
|
2022-10-24 16:41:40 +02:00
|
|
|
"testing"
|
2022-10-24 16:42:09 +02:00
|
|
|
"time"
|
2022-10-24 16:41:40 +02:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
tcmp "github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2022-10-24 16:41:40 +02:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2024-01-04 21:26:49 +01:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
2022-11-14 15:01:31 +01:00
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
2022-11-08 15:10:03 +00:00
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
2024-07-22 08:56:00 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-12-10 16:23:55 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-11-26 15:16:06 +01:00
|
|
|
"golang.org/x/exp/slices"
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func executeAndUnmarshal[T any](headscale ControlServer, command []string, result T) error {
|
|
|
|
str, err := headscale.Execute(command)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(str), result)
|
|
|
|
if err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
return fmt.Errorf("failed to unmarshal: %s\n command err: %s", err, str)
|
2022-10-24 16:41:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
// Interface ensuring that we can sort structs from gRPC that
|
|
|
|
// have an ID field.
|
|
|
|
type GRPCSortable interface {
|
|
|
|
GetId() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortWithID[T GRPCSortable](a, b T) int {
|
|
|
|
return cmp.Compare(a.GetId(), b.GetId())
|
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
func TestUserCommand(t *testing.T) {
|
2022-10-24 16:41:40 +02:00
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-10-24 16:41:40 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 17:43:44 +01:00
|
|
|
"user1": 0,
|
|
|
|
"user2": 0,
|
2022-10-24 16:41:40 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:41:40 +02:00
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 14:27:02 +01:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
var listUsers []*v1.User
|
2022-11-14 14:27:02 +01:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 16:41:40 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 17:43:44 +01:00
|
|
|
"users",
|
2022-10-24 16:41:40 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-01-17 17:43:44 +01:00
|
|
|
&listUsers,
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:41:40 +02:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
slices.SortFunc(listUsers, sortWithID)
|
2023-11-23 08:31:33 +01:00
|
|
|
result := []string{listUsers[0].GetName(), listUsers[1].GetName()}
|
2022-10-24 17:03:59 +02:00
|
|
|
|
2022-10-24 16:41:40 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-01-17 17:43:44 +01:00
|
|
|
[]string{"user1", "user2"},
|
2022-10-24 17:03:59 +02:00
|
|
|
result,
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
_, err = headscale.Execute(
|
2022-10-24 16:41:40 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 17:43:44 +01:00
|
|
|
"users",
|
2022-10-24 16:41:40 +02:00
|
|
|
"rename",
|
2024-12-10 16:23:55 +01:00
|
|
|
"--output=json",
|
|
|
|
fmt.Sprintf("--identifier=%d", listUsers[1].GetId()),
|
|
|
|
"--new-name=newname",
|
2022-10-24 16:41:40 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:41:40 +02:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
var listAfterRenameUsers []*v1.User
|
2022-11-14 14:27:02 +01:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 16:41:40 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 17:43:44 +01:00
|
|
|
"users",
|
2022-10-24 16:41:40 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-01-17 17:43:44 +01:00
|
|
|
&listAfterRenameUsers,
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:41:40 +02:00
|
|
|
|
2024-12-10 16:23:55 +01:00
|
|
|
slices.SortFunc(listUsers, sortWithID)
|
2023-11-23 08:31:33 +01:00
|
|
|
result = []string{listAfterRenameUsers[0].GetName(), listAfterRenameUsers[1].GetName()}
|
2022-10-24 17:03:59 +02:00
|
|
|
|
2022-10-24 16:41:40 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
2024-12-10 16:23:55 +01:00
|
|
|
[]string{"user1", "newname"},
|
2022-10-24 17:03:59 +02:00
|
|
|
result,
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
2024-12-10 16:23:55 +01:00
|
|
|
|
|
|
|
var listByUsername []*v1.User
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--name=user1",
|
|
|
|
},
|
|
|
|
&listByUsername,
|
|
|
|
)
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
slices.SortFunc(listByUsername, sortWithID)
|
|
|
|
want := []*v1.User{
|
|
|
|
{
|
2024-12-19 13:10:10 +01:00
|
|
|
Id: 1,
|
|
|
|
Name: "user1",
|
|
|
|
Email: "user1@test.no",
|
2024-12-10 16:23:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := tcmp.Diff(want, listByUsername, cmpopts.IgnoreUnexported(v1.User{}), cmpopts.IgnoreFields(v1.User{}, "CreatedAt")); diff != "" {
|
|
|
|
t.Errorf("unexpected users (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
var listByID []*v1.User
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--identifier=1",
|
|
|
|
},
|
|
|
|
&listByID,
|
|
|
|
)
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
slices.SortFunc(listByID, sortWithID)
|
|
|
|
want = []*v1.User{
|
|
|
|
{
|
2024-12-19 13:10:10 +01:00
|
|
|
Id: 1,
|
|
|
|
Name: "user1",
|
|
|
|
Email: "user1@test.no",
|
2024-12-10 16:23:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := tcmp.Diff(want, listByID, cmpopts.IgnoreUnexported(v1.User{}), cmpopts.IgnoreFields(v1.User{}, "CreatedAt")); diff != "" {
|
|
|
|
t.Errorf("unexpected users (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteResult, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"destroy",
|
|
|
|
"--force",
|
|
|
|
// Delete "user1"
|
|
|
|
"--identifier=1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, deleteResult, "User destroyed")
|
|
|
|
|
|
|
|
var listAfterIDDelete []*v1.User
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAfterIDDelete,
|
|
|
|
)
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
slices.SortFunc(listAfterIDDelete, sortWithID)
|
|
|
|
want = []*v1.User{
|
|
|
|
{
|
2024-12-19 13:10:10 +01:00
|
|
|
Id: 2,
|
|
|
|
Name: "newname",
|
|
|
|
Email: "user2@test.no",
|
2024-12-10 16:23:55 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := tcmp.Diff(want, listAfterIDDelete, cmpopts.IgnoreUnexported(v1.User{}), cmpopts.IgnoreFields(v1.User{}, "CreatedAt")); diff != "" {
|
|
|
|
t.Errorf("unexpected users (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteResult, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"destroy",
|
|
|
|
"--force",
|
|
|
|
"--name=newname",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, deleteResult, "User destroyed")
|
|
|
|
|
|
|
|
var listAfterNameDelete []v1.User
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"users",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAfterNameDelete,
|
|
|
|
)
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
require.Len(t, listAfterNameDelete, 0)
|
2022-10-24 16:41:40 +02:00
|
|
|
}
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "preauthkeyspace"
|
2022-10-24 16:42:09 +02:00
|
|
|
count := 3
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 17:43:44 +01:00
|
|
|
user: 0,
|
2022-10-24 16:42:09 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipak"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 14:27:02 +01:00
|
|
|
|
2022-10-24 16:42:09 +02:00
|
|
|
keys := make([]*v1.PreAuthKey, count)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
for index := 0; index < count; index++ {
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err := executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 16:42:09 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 16:42:09 +02:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--tags",
|
|
|
|
"tag:test1,tag:test2",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
keys[index] = &preAuthKey
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, keys, 3)
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 16:42:09 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 16:42:09 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 4)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
[]string{keys[0].GetId(), keys[1].GetId(), keys[2].GetId()},
|
2023-12-09 18:09:24 +01:00
|
|
|
[]string{
|
|
|
|
listedPreAuthKeys[1].GetId(),
|
|
|
|
listedPreAuthKeys[2].GetId(),
|
|
|
|
listedPreAuthKeys[3].GetId(),
|
|
|
|
},
|
2022-10-24 16:42:09 +02:00
|
|
|
)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[1].GetKey())
|
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[2].GetKey())
|
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[3].GetKey())
|
2022-10-24 16:42:09 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listedPreAuthKeys[1].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeys[2].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeys[3].GetExpiration().AsTime().After(time.Now()))
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedPreAuthKeys[1].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2022-10-24 16:42:09 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedPreAuthKeys[2].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2022-10-24 16:42:09 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedPreAuthKeys[3].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2022-10-24 16:42:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
for index := range listedPreAuthKeys {
|
|
|
|
if index == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, listedPreAuthKeys[index].GetAclTags(), []string{"tag:test1", "tag:test2"})
|
2022-10-24 16:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test key expiry
|
2022-11-14 14:27:02 +01:00
|
|
|
_, err = headscale.Execute(
|
2022-10-24 16:42:09 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 16:42:09 +02:00
|
|
|
"expire",
|
2023-11-23 08:31:33 +01:00
|
|
|
listedPreAuthKeys[1].GetKey(),
|
2022-10-24 16:42:09 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
var listedPreAuthKeysAfterExpire []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 16:42:09 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 16:42:09 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeysAfterExpire,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 16:42:09 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[1].GetExpiration().AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[2].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[3].GetExpiration().AsTime().After(time.Now()))
|
2022-10-24 16:42:09 +02:00
|
|
|
}
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommandWithoutExpiry(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "pre-auth-key-without-exp-user"
|
2022-10-24 17:31:15 +02:00
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 17:43:44 +01:00
|
|
|
user: 0,
|
2022-10-24 17:31:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipaknaexp"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 14:27:02 +01:00
|
|
|
|
2022-10-24 17:31:15 +02:00
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 17:31:15 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 17:31:15 +02:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 17:31:15 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 17:31:15 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 2)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listedPreAuthKeys[1].GetExpiration().AsTime().After(time.Now()))
|
2022-10-24 17:31:15 +02:00
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedPreAuthKeys[1].GetExpiration().AsTime().Before(time.Now().Add(time.Minute*70)),
|
2022-10-24 17:31:15 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreAuthKeyCommandReusableEphemeral(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "pre-auth-key-reus-ephm-user"
|
2022-10-24 17:31:15 +02:00
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 17:43:44 +01:00
|
|
|
user: 0,
|
2022-10-24 17:31:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipakresueeph"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 14:27:02 +01:00
|
|
|
|
2022-10-24 17:31:15 +02:00
|
|
|
var preAuthReusableKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 17:31:15 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 17:31:15 +02:00
|
|
|
"create",
|
|
|
|
"--reusable=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthReusableKey,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
var preAuthEphemeralKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 17:31:15 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 17:31:15 +02:00
|
|
|
"create",
|
|
|
|
"--ephemeral=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthEphemeralKey,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
assert.True(t, preAuthEphemeralKey.GetEphemeral())
|
|
|
|
assert.False(t, preAuthEphemeralKey.GetReusable())
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale,
|
2022-10-24 17:31:15 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 17:31:15 +02:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 3)
|
|
|
|
}
|
2022-11-28 15:54:23 +00:00
|
|
|
|
2024-05-02 11:53:16 +02:00
|
|
|
func TestPreAuthKeyCorrectUserLoggedInCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
user1 := "user1"
|
|
|
|
user2 := "user2"
|
|
|
|
|
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
user1: 1,
|
|
|
|
user2: 0,
|
|
|
|
}
|
|
|
|
|
2024-10-15 15:38:43 +03:00
|
|
|
err = scenario.CreateHeadscaleEnv(
|
|
|
|
spec,
|
|
|
|
[]tsic.Option{},
|
|
|
|
hsic.WithTestName("clipak"),
|
|
|
|
hsic.WithEmbeddedDERPServerOnly(),
|
|
|
|
hsic.WithTLS(),
|
|
|
|
hsic.WithHostnameAsServerURL(),
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
var user2Key v1.PreAuthKey
|
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
|
|
|
"--user",
|
|
|
|
user2,
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--tags",
|
|
|
|
"tag:test1,tag:test2",
|
|
|
|
},
|
|
|
|
&user2Key,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErrListClients(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
assert.Len(t, allClients, 1)
|
|
|
|
|
|
|
|
client := allClients[0]
|
|
|
|
|
|
|
|
// Log out from user1
|
|
|
|
err = client.Logout()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleLogout()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
status, err := client.Status()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
if status.BackendState == "Starting" || status.BackendState == "Running" {
|
|
|
|
t.Fatalf("expected node to be logged out, backend state: %s", status.BackendState)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Login(headscale.GetEndpoint(), user2Key.GetKey())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
|
|
|
|
status, err = client.Status()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
if status.BackendState != "Running" {
|
|
|
|
t.Fatalf("expected node to be logged in, backend state: %s", status.BackendState)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.Self.UserID.String() != "userid:2" {
|
|
|
|
t.Fatalf("expected node to be logged in as userid:2, got: %s", status.Self.UserID.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
var listNodes []v1.Node
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listNodes,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2024-05-02 11:53:16 +02:00
|
|
|
assert.Len(t, listNodes, 1)
|
|
|
|
|
2024-07-22 08:56:00 +02:00
|
|
|
assert.Equal(t, "user2", listNodes[0].GetUser().GetName())
|
2024-05-02 11:53:16 +02:00
|
|
|
}
|
|
|
|
|
2023-05-10 12:10:33 +02:00
|
|
|
func TestApiKeyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
count := 5
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"user1": 0,
|
|
|
|
"user2": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
keys := make([]string, count)
|
|
|
|
|
|
|
|
for idx := 0; idx < count; idx++ {
|
|
|
|
apiResult, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"create",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
assert.NotEmpty(t, apiResult)
|
|
|
|
|
|
|
|
keys[idx] = apiResult
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, keys, 5)
|
|
|
|
|
|
|
|
var listedAPIKeys []v1.ApiKey
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedAPIKeys,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listedAPIKeys, 5)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, uint64(1), listedAPIKeys[0].GetId())
|
|
|
|
assert.Equal(t, uint64(2), listedAPIKeys[1].GetId())
|
|
|
|
assert.Equal(t, uint64(3), listedAPIKeys[2].GetId())
|
|
|
|
assert.Equal(t, uint64(4), listedAPIKeys[3].GetId())
|
|
|
|
assert.Equal(t, uint64(5), listedAPIKeys[4].GetId())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.NotEmpty(t, listedAPIKeys[0].GetPrefix())
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[1].GetPrefix())
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[2].GetPrefix())
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[3].GetPrefix())
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[4].GetPrefix())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listedAPIKeys[0].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[1].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[2].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[3].GetExpiration().AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[4].GetExpiration().AsTime().After(time.Now()))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[0].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[1].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[2].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[3].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[4].GetExpiration().AsTime().Before(time.Now().Add(time.Hour*26)),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
expiredPrefixes := make(map[string]bool)
|
|
|
|
|
|
|
|
// Expire three keys
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"expire",
|
|
|
|
"--prefix",
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAPIKeys[idx].GetPrefix(),
|
2023-05-10 12:10:33 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
expiredPrefixes[listedAPIKeys[idx].GetPrefix()] = true
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var listedAfterExpireAPIKeys []v1.ApiKey
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedAfterExpireAPIKeys,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index := range listedAfterExpireAPIKeys {
|
2023-11-23 08:31:33 +01:00
|
|
|
if _, ok := expiredPrefixes[listedAfterExpireAPIKeys[index].GetPrefix()]; ok {
|
2023-05-10 12:10:33 +02:00
|
|
|
// Expired
|
|
|
|
assert.True(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAfterExpireAPIKeys[index].GetExpiration().AsTime().Before(time.Now()),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Not expired
|
|
|
|
assert.False(
|
|
|
|
t,
|
2023-11-23 08:31:33 +01:00
|
|
|
listedAfterExpireAPIKeys[index].GetExpiration().AsTime().Before(time.Now()),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-02-12 16:01:21 +05:30
|
|
|
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"delete",
|
|
|
|
"--prefix",
|
|
|
|
listedAPIKeys[0].GetPrefix(),
|
|
|
|
})
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2024-02-12 16:01:21 +05:30
|
|
|
|
|
|
|
var listedAPIKeysAfterDelete []v1.ApiKey
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedAPIKeysAfterDelete,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2024-02-12 16:01:21 +05:30
|
|
|
|
|
|
|
assert.Len(t, listedAPIKeysAfterDelete, 4)
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeTagCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"user1": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
machineKeys := []string{
|
2023-11-19 22:37:04 +01:00
|
|
|
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes := make([]*v1.Node, len(machineKeys))
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"user1",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"user1",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes[index] = &node
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Len(t, nodes, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "1",
|
|
|
|
"-t", "tag:test",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, []string{"tag:test"}, node.GetForcedTags())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
_, err = headscale.Execute(
|
2023-05-10 12:10:33 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "2",
|
|
|
|
"-t", "wrong-tag",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.ErrorContains(t, err, "tag must start with the string 'tag:'")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
2023-09-24 13:42:05 +02:00
|
|
|
resultMachines := make([]*v1.Node, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
&resultMachines,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
found := false
|
2023-09-24 13:42:05 +02:00
|
|
|
for _, node := range resultMachines {
|
2023-11-23 08:31:33 +01:00
|
|
|
if node.GetForcedTags() != nil {
|
|
|
|
for _, tag := range node.GetForcedTags() {
|
2023-05-10 12:10:33 +02:00
|
|
|
if tag == "tag:test" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(
|
2023-05-10 12:10:33 +02:00
|
|
|
t,
|
2024-11-26 15:16:06 +01:00
|
|
|
true,
|
2023-05-10 12:10:33 +02:00
|
|
|
found,
|
2023-09-24 13:42:05 +02:00
|
|
|
"should find a node with the tag 'tag:test' in the list of nodes",
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
func TestNodeAdvertiseTagCommand(t *testing.T) {
|
2024-01-04 21:26:49 +01:00
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
policy *policy.ACLPolicy
|
|
|
|
wantTag bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-policy",
|
|
|
|
wantTag: false,
|
2024-01-04 21:26:49 +01:00
|
|
|
},
|
2024-11-26 15:16:06 +01:00
|
|
|
{
|
2024-12-19 13:10:10 +01:00
|
|
|
name: "with-policy-email",
|
|
|
|
policy: &policy.ACLPolicy{
|
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TagOwners: map[string][]string{
|
|
|
|
"tag:test": {"user1@test.no"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantTag: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with-policy-username",
|
2024-11-26 15:16:06 +01:00
|
|
|
policy: &policy.ACLPolicy{
|
2024-11-22 16:54:58 +01:00
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TagOwners: map[string][]string{
|
2024-11-26 15:16:06 +01:00
|
|
|
"tag:test": {"user1"},
|
2024-01-04 21:26:49 +01:00
|
|
|
},
|
|
|
|
},
|
2024-11-26 15:16:06 +01:00
|
|
|
wantTag: true,
|
|
|
|
},
|
2024-12-19 13:10:10 +01:00
|
|
|
{
|
|
|
|
name: "with-policy-groups",
|
|
|
|
policy: &policy.ACLPolicy{
|
|
|
|
Groups: policy.Groups{
|
|
|
|
"group:admins": []string{"user1"},
|
|
|
|
},
|
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TagOwners: map[string][]string{
|
|
|
|
"tag:test": {"group:admins"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantTag: true,
|
|
|
|
},
|
2024-11-26 15:16:06 +01:00
|
|
|
}
|
2024-01-04 21:26:49 +01:00
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
|
|
|
assertNoErr(t, err)
|
2024-12-19 13:10:10 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2024-01-04 21:26:49 +01:00
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
spec := map[string]int{
|
|
|
|
"user1": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec,
|
|
|
|
[]tsic.Option{tsic.WithTags([]string{"tag:test"})},
|
|
|
|
hsic.WithTestName("cliadvtags"),
|
|
|
|
hsic.WithACLPolicy(tt.policy),
|
|
|
|
)
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
assertNoErr(t, err)
|
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
|
|
|
resultMachines := make([]*v1.Node, spec["user1"])
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--tags",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
&resultMachines,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
found := false
|
|
|
|
for _, node := range resultMachines {
|
|
|
|
if tags := node.GetValidTags(); tags != nil {
|
|
|
|
found = slices.Contains(tags, "tag:test")
|
2024-01-04 21:26:49 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equalf(
|
|
|
|
t,
|
|
|
|
tt.wantTag,
|
|
|
|
found,
|
|
|
|
"'tag:test' found(%t) is the list of nodes, expected %t", found, tt.wantTag,
|
|
|
|
)
|
|
|
|
})
|
2024-01-04 21:26:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 12:10:33 +02:00
|
|
|
func TestNodeCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-user": 0,
|
|
|
|
"other-user": 0,
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-19 22:37:04 +01:00
|
|
|
// Pregenerated machine keys
|
2023-05-10 12:10:33 +02:00
|
|
|
machineKeys := []string{
|
2023-11-19 22:37:04 +01:00
|
|
|
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes := make([]*v1.Node, len(machineKeys))
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-user",
|
2023-05-10 12:10:33 +02:00
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-user",
|
2023-05-10 12:10:33 +02:00
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes[index] = &node
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Len(t, nodes, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAll []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, uint64(1), listAll[0].GetId())
|
|
|
|
assert.Equal(t, uint64(2), listAll[1].GetId())
|
|
|
|
assert.Equal(t, uint64(3), listAll[2].GetId())
|
|
|
|
assert.Equal(t, uint64(4), listAll[3].GetId())
|
|
|
|
assert.Equal(t, uint64(5), listAll[4].GetId())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, "node-1", listAll[0].GetName())
|
|
|
|
assert.Equal(t, "node-2", listAll[1].GetName())
|
|
|
|
assert.Equal(t, "node-3", listAll[2].GetName())
|
|
|
|
assert.Equal(t, "node-4", listAll[3].GetName())
|
|
|
|
assert.Equal(t, "node-5", listAll[4].GetName())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
otherUserMachineKeys := []string{
|
2023-11-19 22:37:04 +01:00
|
|
|
"mkey:b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e",
|
|
|
|
"mkey:dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584",
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
otherUserMachines := make([]*v1.Node, len(otherUserMachineKeys))
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index, machineKey := range otherUserMachineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("otherUser-node-%d", index+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
otherUserMachines[index] = &node
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, otherUserMachines, len(otherUserMachineKeys))
|
|
|
|
|
|
|
|
// Test list all nodes after added otherUser
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAllWithotherUser []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllWithotherUser,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// All nodes, nodes + otherUser
|
2023-05-10 12:10:33 +02:00
|
|
|
assert.Len(t, listAllWithotherUser, 7)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, uint64(6), listAllWithotherUser[5].GetId())
|
|
|
|
assert.Equal(t, uint64(7), listAllWithotherUser[6].GetId())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, "otherUser-node-1", listAllWithotherUser[5].GetName())
|
|
|
|
assert.Equal(t, "otherUser-node-2", listAllWithotherUser[6].GetName())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
// Test list all nodes after added otherUser
|
2023-09-24 13:42:05 +02:00
|
|
|
var listOnlyotherUserMachineUser []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listOnlyotherUserMachineUser,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listOnlyotherUserMachineUser, 2)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, uint64(6), listOnlyotherUserMachineUser[0].GetId())
|
|
|
|
assert.Equal(t, uint64(7), listOnlyotherUserMachineUser[1].GetId())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-09-24 13:42:05 +02:00
|
|
|
"otherUser-node-1",
|
2023-11-23 08:31:33 +01:00
|
|
|
listOnlyotherUserMachineUser[0].GetName(),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-09-24 13:42:05 +02:00
|
|
|
"otherUser-node-2",
|
2023-11-23 08:31:33 +01:00
|
|
|
listOnlyotherUserMachineUser[1].GetName(),
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// Delete a nodes
|
2023-05-10 12:10:33 +02:00
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"delete",
|
|
|
|
"--identifier",
|
|
|
|
// Delete the last added machine
|
|
|
|
"4",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--force",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// Test: list main user after node is deleted
|
|
|
|
var listOnlyMachineUserAfterDelete []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-user",
|
2023-05-10 12:10:33 +02:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listOnlyMachineUserAfterDelete,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listOnlyMachineUserAfterDelete, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeExpireCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-expire-user": 0,
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-19 22:37:04 +01:00
|
|
|
// Pregenerated machine keys
|
2023-05-10 12:10:33 +02:00
|
|
|
machineKeys := []string{
|
2023-11-19 22:37:04 +01:00
|
|
|
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes := make([]*v1.Node, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-expire-user",
|
2023-05-10 12:10:33 +02:00
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-expire-user",
|
2023-05-10 12:10:33 +02:00
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes[index] = &node
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Len(t, nodes, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAll []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listAll[0].GetExpiry().AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[1].GetExpiry().AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[2].GetExpiry().AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[3].GetExpiry().AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[4].GetExpiry().AsTime().IsZero())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"expire",
|
|
|
|
"--identifier",
|
2023-11-23 08:31:33 +01:00
|
|
|
fmt.Sprintf("%d", listAll[idx].GetId()),
|
2023-05-10 12:10:33 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAllAfterExpiry []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterExpiry,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAllAfterExpiry, 5)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.True(t, listAllAfterExpiry[0].GetExpiry().AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[1].GetExpiry().AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[2].GetExpiry().AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[3].GetExpiry().AsTime().IsZero())
|
|
|
|
assert.True(t, listAllAfterExpiry[4].GetExpiry().AsTime().IsZero())
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRenameCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-rename-command": 0,
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-19 22:37:04 +01:00
|
|
|
// Pregenerated machine keys
|
2023-05-10 12:10:33 +02:00
|
|
|
machineKeys := []string{
|
2023-11-19 22:37:04 +01:00
|
|
|
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes := make([]*v1.Node, len(machineKeys))
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-rename-command",
|
2023-05-10 12:10:33 +02:00
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
2023-09-24 13:42:05 +02:00
|
|
|
"node-rename-command",
|
2023-05-10 12:10:33 +02:00
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
nodes[index] = &node
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Len(t, nodes, len(machineKeys))
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAll []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Contains(t, listAll[0].GetGivenName(), "node-1")
|
|
|
|
assert.Contains(t, listAll[1].GetGivenName(), "node-2")
|
|
|
|
assert.Contains(t, listAll[2].GetGivenName(), "node-3")
|
|
|
|
assert.Contains(t, listAll[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(t, listAll[4].GetGivenName(), "node-5")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
2024-02-08 17:28:19 +01:00
|
|
|
res, err := headscale.Execute(
|
2023-05-10 12:10:33 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
2023-11-23 08:31:33 +01:00
|
|
|
fmt.Sprintf("%d", listAll[idx].GetId()),
|
2023-09-24 13:42:05 +02:00
|
|
|
fmt.Sprintf("newnode-%d", idx+1),
|
2023-05-10 12:10:33 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2024-02-08 17:28:19 +01:00
|
|
|
|
|
|
|
assert.Contains(t, res, "Node renamed")
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAllAfterRename []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterRename,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAllAfterRename, 5)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Equal(t, "newnode-1", listAllAfterRename[0].GetGivenName())
|
|
|
|
assert.Equal(t, "newnode-2", listAllAfterRename[1].GetGivenName())
|
|
|
|
assert.Equal(t, "newnode-3", listAllAfterRename[2].GetGivenName())
|
|
|
|
assert.Contains(t, listAllAfterRename[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(t, listAllAfterRename[4].GetGivenName(), "node-5")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
// Test failure for too long names
|
2024-09-07 09:23:58 +02:00
|
|
|
_, err = headscale.Execute(
|
2023-05-10 12:10:33 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
2023-11-23 08:31:33 +01:00
|
|
|
fmt.Sprintf("%d", listAll[4].GetId()),
|
2024-09-07 09:23:58 +02:00
|
|
|
strings.Repeat("t", 64),
|
2023-05-10 12:10:33 +02:00
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.ErrorContains(t, err, "not be over 63 chars")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var listAllAfterRenameAttempt []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterRenameAttempt,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, listAllAfterRenameAttempt, 5)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
assert.Equal(t, "newnode-1", listAllAfterRenameAttempt[0].GetGivenName())
|
|
|
|
assert.Equal(t, "newnode-2", listAllAfterRenameAttempt[1].GetGivenName())
|
|
|
|
assert.Equal(t, "newnode-3", listAllAfterRenameAttempt[2].GetGivenName())
|
|
|
|
assert.Contains(t, listAllAfterRenameAttempt[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(t, listAllAfterRenameAttempt[4].GetGivenName(), "node-5")
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeMoveCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"old-user": 0,
|
|
|
|
"new-user": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// Randomly generated node key
|
2023-11-19 22:37:04 +01:00
|
|
|
machineKey := "mkey:688411b767663479632d44140f08a9fde87383adc7cdeb518f62ce28a17ef0aa"
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-09-24 13:42:05 +02:00
|
|
|
"nomad-node",
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var node v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, uint64(1), node.GetId())
|
|
|
|
assert.Equal(t, "nomad-node", node.GetName())
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, node.GetUser().GetName(), "old-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
nodeID := fmt.Sprintf("%d", node.GetId())
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-09-24 13:42:05 +02:00
|
|
|
nodeID,
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"new-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, node.GetUser().GetName(), "new-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var allNodes []v1.Node
|
2023-05-10 12:10:33 +02:00
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&allNodes,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
assert.Len(t, allNodes, 1)
|
|
|
|
|
2023-11-23 08:31:33 +01:00
|
|
|
assert.Equal(t, allNodes[0].GetId(), node.GetId())
|
|
|
|
assert.Equal(t, allNodes[0].GetUser(), node.GetUser())
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, allNodes[0].GetUser().GetName(), "new-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2024-09-07 09:23:58 +02:00
|
|
|
_, err = headscale.Execute(
|
2023-05-10 12:10:33 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-09-24 13:42:05 +02:00
|
|
|
nodeID,
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"non-existing-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.ErrorContains(
|
2023-05-10 12:10:33 +02:00
|
|
|
t,
|
2024-09-07 09:23:58 +02:00
|
|
|
err,
|
2023-06-28 11:56:35 +01:00
|
|
|
"user not found",
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, node.GetUser().GetName(), "new-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-09-24 13:42:05 +02:00
|
|
|
nodeID,
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, node.GetUser().GetName(), "old-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-09-24 13:42:05 +02:00
|
|
|
nodeID,
|
2023-05-10 12:10:33 +02:00
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-09-24 13:42:05 +02:00
|
|
|
&node,
|
2023-05-10 12:10:33 +02:00
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Nil(t, err)
|
2023-05-10 12:10:33 +02:00
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, node.GetUser().GetName(), "old-user")
|
2023-05-10 12:10:33 +02:00
|
|
|
}
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
func TestPolicyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"policy-user": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(
|
|
|
|
spec,
|
|
|
|
[]tsic.Option{},
|
|
|
|
hsic.WithTestName("clins"),
|
|
|
|
hsic.WithConfigEnv(map[string]string{
|
|
|
|
"HEADSCALE_POLICY_MODE": "database",
|
|
|
|
}),
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
p := policy.ACLPolicy{
|
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
Action: "accept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TagOwners: map[string][]string{
|
|
|
|
"tag:exists": {"policy-user"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pBytes, _ := json.Marshal(p)
|
|
|
|
|
|
|
|
policyFilePath := "/etc/headscale/policy.json"
|
|
|
|
|
|
|
|
err = headscale.WriteFile(policyFilePath, pBytes)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
// No policy is present at this time.
|
|
|
|
// Add a new policy from a file.
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"policy",
|
|
|
|
"set",
|
|
|
|
"-f",
|
|
|
|
policyFilePath,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
// Get the current policy and check
|
|
|
|
// if it is the same as the one we set.
|
|
|
|
var output *policy.ACLPolicy
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"policy",
|
|
|
|
"get",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&output,
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-07-18 11:08:25 +05:30
|
|
|
|
|
|
|
assert.Len(t, output.TagOwners, 1)
|
|
|
|
assert.Len(t, output.ACLs, 1)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.Equal(t, output.TagOwners["tag:exists"], []string{"policy-user"})
|
2024-07-18 11:08:25 +05:30
|
|
|
}
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
func TestPolicyBrokenConfigCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-09-17 10:44:55 +01:00
|
|
|
defer scenario.ShutdownAssertNoPanics(t)
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"policy-user": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(
|
|
|
|
spec,
|
|
|
|
[]tsic.Option{},
|
|
|
|
hsic.WithTestName("clins"),
|
|
|
|
hsic.WithConfigEnv(map[string]string{
|
|
|
|
"HEADSCALE_POLICY_MODE": "database",
|
|
|
|
}),
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
p := policy.ACLPolicy{
|
|
|
|
ACLs: []policy.ACL{
|
|
|
|
{
|
|
|
|
// This is an unknown action, so it will return an error
|
|
|
|
// and the config will not be applied.
|
|
|
|
Action: "acccept",
|
|
|
|
Sources: []string{"*"},
|
|
|
|
Destinations: []string{"*:*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TagOwners: map[string][]string{
|
|
|
|
"tag:exists": {"policy-user"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pBytes, _ := json.Marshal(p)
|
|
|
|
|
|
|
|
policyFilePath := "/etc/headscale/policy.json"
|
|
|
|
|
|
|
|
err = headscale.WriteFile(policyFilePath, pBytes)
|
2024-11-26 15:16:06 +01:00
|
|
|
assertNoErr(t, err)
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
// No policy is present at this time.
|
|
|
|
// Add a new policy from a file.
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"policy",
|
|
|
|
"set",
|
|
|
|
"-f",
|
|
|
|
policyFilePath,
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.ErrorContains(t, err, "compiling filter rules: invalid action")
|
2024-08-30 16:58:29 +02:00
|
|
|
|
|
|
|
// The new policy was invalid, the old one should still be in place, which
|
|
|
|
// is none.
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"policy",
|
|
|
|
"get",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
2024-11-26 15:16:06 +01:00
|
|
|
assert.ErrorContains(t, err, "acl policy not found")
|
2024-08-30 16:58:29 +02:00
|
|
|
}
|