2022-10-24 14:41:40 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-11-28 15:54:23 +00:00
|
|
|
"fmt"
|
2022-10-24 15:03:59 +00:00
|
|
|
"sort"
|
2022-11-28 15:54:23 +00:00
|
|
|
"strconv"
|
2022-10-24 14:41:40 +00:00
|
|
|
"testing"
|
2022-10-24 14:42:09 +00:00
|
|
|
"time"
|
2022-10-24 14:41:40 +00:00
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2022-11-14 14:01:31 +00:00
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
2022-11-08 15:10:03 +00:00
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
2022-10-24 14:41:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
func TestUserCommand(t *testing.T) {
|
2022-10-24 14:41:40 +00:00
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-24 14:41:40 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 16:43:44 +00:00
|
|
|
"user1": 0,
|
|
|
|
"user2": 0,
|
2022-10-24 14:41:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:41:40 +00:00
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 13:27:02 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
var listUsers []v1.User
|
2022-11-14 13:27:02 +00:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 14:41:40 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 16:43:44 +00:00
|
|
|
"users",
|
2022-10-24 14:41:40 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-01-17 16:43:44 +00:00
|
|
|
&listUsers,
|
2022-10-24 14:41:40 +00:00
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:41:40 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
result := []string{listUsers[0].Name, listUsers[1].Name}
|
2022-10-24 15:03:59 +00:00
|
|
|
sort.Strings(result)
|
|
|
|
|
2022-10-24 14:41:40 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-01-17 16:43:44 +00:00
|
|
|
[]string{"user1", "user2"},
|
2022-10-24 15:03:59 +00:00
|
|
|
result,
|
2022-10-24 14:41:40 +00:00
|
|
|
)
|
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
_, err = headscale.Execute(
|
2022-10-24 14:41:40 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 16:43:44 +00:00
|
|
|
"users",
|
2022-10-24 14:41:40 +00:00
|
|
|
"rename",
|
|
|
|
"--output",
|
|
|
|
"json",
|
2023-01-17 16:43:44 +00:00
|
|
|
"user2",
|
2022-10-24 14:41:40 +00:00
|
|
|
"newname",
|
|
|
|
},
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:41:40 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
var listAfterRenameUsers []v1.User
|
2022-11-14 13:27:02 +00:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 14:41:40 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 16:43:44 +00:00
|
|
|
"users",
|
2022-10-24 14:41:40 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
2023-01-17 16:43:44 +00:00
|
|
|
&listAfterRenameUsers,
|
2022-10-24 14:41:40 +00:00
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:41:40 +00:00
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
result = []string{listAfterRenameUsers[0].Name, listAfterRenameUsers[1].Name}
|
2022-10-24 15:03:59 +00:00
|
|
|
sort.Strings(result)
|
|
|
|
|
2022-10-24 14:41:40 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
2023-01-30 09:20:08 +00:00
|
|
|
[]string{"newname", "user1"},
|
2022-10-24 15:03:59 +00:00
|
|
|
result,
|
2022-10-24 14:41:40 +00:00
|
|
|
)
|
|
|
|
}
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
user := "preauthkeyspace"
|
2022-10-24 14:42:09 +00:00
|
|
|
count := 3
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 16:43:44 +00:00
|
|
|
user: 0,
|
2022-10-24 14:42:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipak"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 13:27:02 +00:00
|
|
|
|
2022-10-24 14:42:09 +00:00
|
|
|
keys := make([]*v1.PreAuthKey, count)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
for index := 0; index < count; index++ {
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err := executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 14:42:09 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 14:42:09 +00:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--tags",
|
|
|
|
"tag:test1,tag:test2",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
keys[index] = &preAuthKey
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, keys, 3)
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 14:42:09 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 14:42:09 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 4)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]string{keys[0].Id, keys[1].Id, keys[2].Id},
|
|
|
|
[]string{listedPreAuthKeys[1].Id, listedPreAuthKeys[2].Id, listedPreAuthKeys[3].Id},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[1].Key)
|
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[2].Key)
|
|
|
|
assert.NotEmpty(t, listedPreAuthKeys[3].Key)
|
|
|
|
|
|
|
|
assert.True(t, listedPreAuthKeys[1].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeys[2].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeys[3].Expiration.AsTime().After(time.Now()))
|
|
|
|
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedPreAuthKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedPreAuthKeys[2].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedPreAuthKeys[3].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
|
|
|
|
for index := range listedPreAuthKeys {
|
|
|
|
if index == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, listedPreAuthKeys[index].AclTags, []string{"tag:test1", "tag:test2"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test key expiry
|
2022-11-14 13:27:02 +00:00
|
|
|
_, err = headscale.Execute(
|
2022-10-24 14:42:09 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 14:42:09 +00:00
|
|
|
"expire",
|
|
|
|
listedPreAuthKeys[1].Key,
|
|
|
|
},
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
var listedPreAuthKeysAfterExpire []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 14:42:09 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 14:42:09 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeysAfterExpire,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 14:42:09 +00:00
|
|
|
|
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[1].Expiration.AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[2].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedPreAuthKeysAfterExpire[3].Expiration.AsTime().After(time.Now()))
|
|
|
|
}
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommandWithoutExpiry(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
user := "pre-auth-key-without-exp-user"
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 16:43:44 +00:00
|
|
|
user: 0,
|
2022-10-24 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipaknaexp"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 13:27:02 +00:00
|
|
|
|
2022-10-24 15:31:15 +00:00
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 15:31:15 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 15:31:15 +00:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 15:31:15 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 15:31:15 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 2)
|
|
|
|
|
|
|
|
assert.True(t, listedPreAuthKeys[1].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedPreAuthKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Minute*70)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreAuthKeyCommandReusableEphemeral(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
user := "pre-auth-key-reus-ephm-user"
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 16:43:44 +00:00
|
|
|
user: 0,
|
2022-10-24 15:31:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipakresueeph"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-14 13:27:02 +00:00
|
|
|
|
2022-10-24 15:31:15 +00:00
|
|
|
var preAuthReusableKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 15:31:15 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 15:31:15 +00:00
|
|
|
"create",
|
|
|
|
"--reusable=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthReusableKey,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
var preAuthEphemeralKey v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 15:31:15 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 15:31:15 +00:00
|
|
|
"create",
|
|
|
|
"--ephemeral=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthEphemeralKey,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
assert.True(t, preAuthEphemeralKey.GetEphemeral())
|
|
|
|
assert.False(t, preAuthEphemeralKey.GetReusable())
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = executeAndUnmarshal(
|
2022-11-14 13:27:02 +00:00
|
|
|
headscale,
|
2022-10-24 15:31:15 +00:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-24 15:31:15 +00:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-10-24 15:31:15 +00:00
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 3)
|
|
|
|
}
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
func TestEnablingRoutes(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
2023-01-17 16:43:44 +00:00
|
|
|
user := "enable-routing"
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrf(t, "failed to create scenario: %s", err)
|
|
|
|
defer scenario.Shutdown()
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2023-01-17 16:43:44 +00:00
|
|
|
user: 3,
|
2022-11-28 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clienableroute"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrHeadscaleEnv(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrListClients(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrSync(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrGetHeadscale(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
// advertise routes using the up command
|
|
|
|
for i, client := range allClients {
|
|
|
|
routeStr := fmt.Sprintf("10.0.%d.0/24", i)
|
|
|
|
hostname, _ := client.FQDN()
|
|
|
|
_, _, err = client.Execute([]string{
|
|
|
|
"tailscale",
|
|
|
|
"up",
|
|
|
|
fmt.Sprintf("--advertise-routes=%s", routeStr),
|
|
|
|
"-login-server", headscale.GetEndpoint(),
|
|
|
|
"--hostname", hostname,
|
|
|
|
})
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrf(t, "failed to advertise route: %s", err)
|
2022-11-28 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErrSync(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
var routes []*v1.Route
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"routes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&routes,
|
|
|
|
)
|
|
|
|
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
assert.Len(t, routes, 3)
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
assert.Equal(t, route.Advertised, true)
|
|
|
|
assert.Equal(t, route.Enabled, false)
|
|
|
|
assert.Equal(t, route.IsPrimary, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"routes",
|
|
|
|
"enable",
|
|
|
|
"--route",
|
|
|
|
strconv.Itoa(int(route.Id)),
|
|
|
|
})
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var enablingRoutes []*v1.Route
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"routes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&enablingRoutes,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
for _, route := range enablingRoutes {
|
|
|
|
assert.Equal(t, route.Advertised, true)
|
|
|
|
assert.Equal(t, route.Enabled, true)
|
|
|
|
assert.Equal(t, route.IsPrimary, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
routeIDToBeDisabled := enablingRoutes[0].Id
|
|
|
|
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"routes",
|
|
|
|
"disable",
|
|
|
|
"--route",
|
|
|
|
strconv.Itoa(int(routeIDToBeDisabled)),
|
|
|
|
})
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
var disablingRoutes []*v1.Route
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"routes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&disablingRoutes,
|
|
|
|
)
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2022-11-28 15:54:23 +00:00
|
|
|
|
|
|
|
for _, route := range disablingRoutes {
|
|
|
|
assert.Equal(t, true, route.Advertised)
|
|
|
|
|
|
|
|
if route.Id == routeIDToBeDisabled {
|
|
|
|
assert.Equal(t, route.Enabled, false)
|
|
|
|
assert.Equal(t, route.IsPrimary, false)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, route.Enabled, true)
|
|
|
|
assert.Equal(t, route.IsPrimary, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
func TestApiKeyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
count := 5
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"user1": 0,
|
|
|
|
"user2": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
keys := make([]string, count)
|
|
|
|
|
|
|
|
for idx := 0; idx < count; idx++ {
|
|
|
|
apiResult, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"create",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listedAPIKeys, 5)
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(1), listedAPIKeys[0].Id)
|
|
|
|
assert.Equal(t, uint64(2), listedAPIKeys[1].Id)
|
|
|
|
assert.Equal(t, uint64(3), listedAPIKeys[2].Id)
|
|
|
|
assert.Equal(t, uint64(4), listedAPIKeys[3].Id)
|
|
|
|
assert.Equal(t, uint64(5), listedAPIKeys[4].Id)
|
|
|
|
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[0].Prefix)
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[1].Prefix)
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[2].Prefix)
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[3].Prefix)
|
|
|
|
assert.NotEmpty(t, listedAPIKeys[4].Prefix)
|
|
|
|
|
|
|
|
assert.True(t, listedAPIKeys[0].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[1].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[2].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[3].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(t, listedAPIKeys[4].Expiration.AsTime().After(time.Now()))
|
|
|
|
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAPIKeys[0].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAPIKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAPIKeys[2].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAPIKeys[3].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAPIKeys[4].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
|
|
|
|
expiredPrefixes := make(map[string]bool)
|
|
|
|
|
|
|
|
// Expire three keys
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"expire",
|
|
|
|
"--prefix",
|
|
|
|
listedAPIKeys[idx].Prefix,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
expiredPrefixes[listedAPIKeys[idx].Prefix] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var listedAfterExpireAPIKeys []v1.ApiKey
|
|
|
|
err = executeAndUnmarshal(headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedAfterExpireAPIKeys,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for index := range listedAfterExpireAPIKeys {
|
|
|
|
if _, ok := expiredPrefixes[listedAfterExpireAPIKeys[index].Prefix]; ok {
|
|
|
|
// Expired
|
|
|
|
assert.True(
|
|
|
|
t,
|
|
|
|
listedAfterExpireAPIKeys[index].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Not expired
|
|
|
|
assert.False(
|
|
|
|
t,
|
|
|
|
listedAfterExpireAPIKeys[index].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeTagCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"user1": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
machineKeys := []string{
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
}
|
|
|
|
machines := make([]*v1.Machine, len(machineKeys))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
fmt.Sprintf("machine-%d", index+1),
|
|
|
|
"--user",
|
|
|
|
"user1",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"user1",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
machines[index] = &machine
|
|
|
|
}
|
|
|
|
assert.Len(t, machines, len(machineKeys))
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "1",
|
|
|
|
"-t", "tag:test",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"tag:test"}, machine.ForcedTags)
|
|
|
|
|
|
|
|
// try to set a wrong tag and retrieve the error
|
|
|
|
type errOutput struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
var errorOutput errOutput
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "2",
|
|
|
|
"-t", "wrong-tag",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
&errorOutput,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, errorOutput.Error, "tag must start with the string 'tag:'")
|
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
|
|
|
resultMachines := make([]*v1.Machine, len(machineKeys))
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
&resultMachines,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
found := false
|
|
|
|
for _, machine := range resultMachines {
|
|
|
|
if machine.ForcedTags != nil {
|
|
|
|
for _, tag := range machine.ForcedTags {
|
|
|
|
if tag == "tag:test" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
true,
|
|
|
|
found,
|
|
|
|
"should find a machine with the tag 'tag:test' in the list of machines",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"machine-user": 0,
|
|
|
|
"other-user": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
// Randomly generated machine keys
|
|
|
|
machineKeys := []string{
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
}
|
|
|
|
machines := make([]*v1.Machine, len(machineKeys))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
fmt.Sprintf("machine-%d", index+1),
|
|
|
|
"--user",
|
|
|
|
"machine-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"machine-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
machines[index] = &machine
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, machines, len(machineKeys))
|
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(1), listAll[0].Id)
|
|
|
|
assert.Equal(t, uint64(2), listAll[1].Id)
|
|
|
|
assert.Equal(t, uint64(3), listAll[2].Id)
|
|
|
|
assert.Equal(t, uint64(4), listAll[3].Id)
|
|
|
|
assert.Equal(t, uint64(5), listAll[4].Id)
|
|
|
|
|
|
|
|
assert.Equal(t, "machine-1", listAll[0].Name)
|
|
|
|
assert.Equal(t, "machine-2", listAll[1].Name)
|
|
|
|
assert.Equal(t, "machine-3", listAll[2].Name)
|
|
|
|
assert.Equal(t, "machine-4", listAll[3].Name)
|
|
|
|
assert.Equal(t, "machine-5", listAll[4].Name)
|
|
|
|
|
|
|
|
otherUserMachineKeys := []string{
|
|
|
|
"nodekey:b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e",
|
|
|
|
"nodekey:dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584",
|
|
|
|
}
|
|
|
|
otherUserMachines := make([]*v1.Machine, len(otherUserMachineKeys))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for index, machineKey := range otherUserMachineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
fmt.Sprintf("otherUser-machine-%d", index+1),
|
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
otherUserMachines[index] = &machine
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, otherUserMachines, len(otherUserMachineKeys))
|
|
|
|
|
|
|
|
// Test list all nodes after added otherUser
|
|
|
|
var listAllWithotherUser []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllWithotherUser,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// All nodes, machines + otherUser
|
|
|
|
assert.Len(t, listAllWithotherUser, 7)
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(6), listAllWithotherUser[5].Id)
|
|
|
|
assert.Equal(t, uint64(7), listAllWithotherUser[6].Id)
|
|
|
|
|
|
|
|
assert.Equal(t, "otherUser-machine-1", listAllWithotherUser[5].Name)
|
|
|
|
assert.Equal(t, "otherUser-machine-2", listAllWithotherUser[6].Name)
|
|
|
|
|
|
|
|
// Test list all nodes after added otherUser
|
|
|
|
var listOnlyotherUserMachineUser []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--user",
|
|
|
|
"other-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listOnlyotherUserMachineUser,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listOnlyotherUserMachineUser, 2)
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(6), listOnlyotherUserMachineUser[0].Id)
|
|
|
|
assert.Equal(t, uint64(7), listOnlyotherUserMachineUser[1].Id)
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"otherUser-machine-1",
|
|
|
|
listOnlyotherUserMachineUser[0].Name,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"otherUser-machine-2",
|
|
|
|
listOnlyotherUserMachineUser[1].Name,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Delete a machines
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"delete",
|
|
|
|
"--identifier",
|
|
|
|
// Delete the last added machine
|
|
|
|
"4",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--force",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// Test: list main user after machine is deleted
|
|
|
|
var listOnlyMachineUserAfterDelete []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--user",
|
|
|
|
"machine-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listOnlyMachineUserAfterDelete,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listOnlyMachineUserAfterDelete, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeExpireCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"machine-expire-user": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
// Randomly generated machine keys
|
|
|
|
machineKeys := []string{
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
}
|
|
|
|
machines := make([]*v1.Machine, len(machineKeys))
|
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
fmt.Sprintf("machine-%d", index+1),
|
|
|
|
"--user",
|
|
|
|
"machine-expire-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"machine-expire-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
machines[index] = &machine
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, machines, len(machineKeys))
|
|
|
|
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
|
|
|
assert.True(t, listAll[0].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[1].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[2].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[3].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(t, listAll[4].Expiry.AsTime().IsZero())
|
|
|
|
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"expire",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[idx].Id),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var listAllAfterExpiry []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterExpiry,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAllAfterExpiry, 5)
|
|
|
|
|
|
|
|
assert.True(t, listAllAfterExpiry[0].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[1].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[2].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(t, listAllAfterExpiry[3].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(t, listAllAfterExpiry[4].Expiry.AsTime().IsZero())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRenameCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"machine-rename-command": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
// Randomly generated machine keys
|
|
|
|
machineKeys := []string{
|
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
}
|
|
|
|
machines := make([]*v1.Machine, len(machineKeys))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
for index, machineKey := range machineKeys {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
fmt.Sprintf("machine-%d", index+1),
|
|
|
|
"--user",
|
|
|
|
"machine-rename-command",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"machine-rename-command",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
machines[index] = &machine
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, machines, len(machineKeys))
|
|
|
|
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAll,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAll, 5)
|
|
|
|
|
|
|
|
assert.Contains(t, listAll[0].GetGivenName(), "machine-1")
|
|
|
|
assert.Contains(t, listAll[1].GetGivenName(), "machine-2")
|
|
|
|
assert.Contains(t, listAll[2].GetGivenName(), "machine-3")
|
|
|
|
assert.Contains(t, listAll[3].GetGivenName(), "machine-4")
|
|
|
|
assert.Contains(t, listAll[4].GetGivenName(), "machine-5")
|
|
|
|
|
|
|
|
for idx := 0; idx < 3; idx++ {
|
|
|
|
_, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[idx].Id),
|
|
|
|
fmt.Sprintf("newmachine-%d", idx+1),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var listAllAfterRename []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterRename,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAllAfterRename, 5)
|
|
|
|
|
|
|
|
assert.Equal(t, "newmachine-1", listAllAfterRename[0].GetGivenName())
|
|
|
|
assert.Equal(t, "newmachine-2", listAllAfterRename[1].GetGivenName())
|
|
|
|
assert.Equal(t, "newmachine-3", listAllAfterRename[2].GetGivenName())
|
|
|
|
assert.Contains(t, listAllAfterRename[3].GetGivenName(), "machine-4")
|
|
|
|
assert.Contains(t, listAllAfterRename[4].GetGivenName(), "machine-5")
|
|
|
|
|
|
|
|
// Test failure for too long names
|
|
|
|
result, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[4].Id),
|
|
|
|
"testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine12345678901234567890",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, result, "not be over 63 chars")
|
|
|
|
|
|
|
|
var listAllAfterRenameAttempt []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAllAfterRenameAttempt,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, listAllAfterRenameAttempt, 5)
|
|
|
|
|
|
|
|
assert.Equal(t, "newmachine-1", listAllAfterRenameAttempt[0].GetGivenName())
|
|
|
|
assert.Equal(t, "newmachine-2", listAllAfterRenameAttempt[1].GetGivenName())
|
|
|
|
assert.Equal(t, "newmachine-3", listAllAfterRenameAttempt[2].GetGivenName())
|
|
|
|
assert.Contains(t, listAllAfterRenameAttempt[3].GetGivenName(), "machine-4")
|
|
|
|
assert.Contains(t, listAllAfterRenameAttempt[4].GetGivenName(), "machine-5")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeMoveCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"old-user": 0,
|
|
|
|
"new-user": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
headscale, err := scenario.Headscale()
|
2023-08-29 06:33:33 +00:00
|
|
|
assertNoErr(t, err)
|
2023-05-10 10:10:33 +00:00
|
|
|
|
|
|
|
// Randomly generated machine key
|
|
|
|
machineKey := "nodekey:688411b767663479632d44140f08a9fde87383adc7cdeb518f62ce28a17ef0aa"
|
|
|
|
|
|
|
|
_, err = headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
|
|
|
"nomad-machine",
|
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
var machine v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"register",
|
|
|
|
"--key",
|
|
|
|
machineKey,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, uint64(1), machine.Id)
|
|
|
|
assert.Equal(t, "nomad-machine", machine.Name)
|
|
|
|
assert.Equal(t, machine.User.Name, "old-user")
|
|
|
|
|
|
|
|
machineID := fmt.Sprintf("%d", machine.Id)
|
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
|
|
|
machineID,
|
|
|
|
"--user",
|
|
|
|
"new-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, machine.User.Name, "new-user")
|
|
|
|
|
|
|
|
var allNodes []v1.Machine
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&allNodes,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, allNodes, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, allNodes[0].Id, machine.Id)
|
|
|
|
assert.Equal(t, allNodes[0].User, machine.User)
|
|
|
|
assert.Equal(t, allNodes[0].User.Name, "new-user")
|
|
|
|
|
|
|
|
moveToNonExistingNSResult, err := headscale.Execute(
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
|
|
|
machineID,
|
|
|
|
"--user",
|
|
|
|
"non-existing-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Contains(
|
|
|
|
t,
|
|
|
|
moveToNonExistingNSResult,
|
2023-06-28 10:56:35 +00:00
|
|
|
"user not found",
|
2023-05-10 10:10:33 +00:00
|
|
|
)
|
|
|
|
assert.Equal(t, machine.User.Name, "new-user")
|
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
|
|
|
machineID,
|
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, machine.User.Name, "old-user")
|
|
|
|
|
|
|
|
err = executeAndUnmarshal(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
|
|
|
machineID,
|
|
|
|
"--user",
|
|
|
|
"old-user",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&machine,
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, machine.User.Name, "old-user")
|
|
|
|
}
|