2022-10-24 16:41:40 +02:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-24 17:03:59 +02:00
|
|
|
"sort"
|
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
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
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"
|
2022-10-24 16:41:40 +02: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamespaceCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
"namespace1": 0,
|
|
|
|
"namespace2": 0,
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clins"))
|
2022-10-24 16:41:40 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-24 16:41:40 +02:00
|
|
|
var listNamespaces []v1.Namespace
|
2022-11-14 14:27:02 +01:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 16:41:40 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"namespaces",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listNamespaces,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-24 17:03:59 +02:00
|
|
|
result := []string{listNamespaces[0].Name, listNamespaces[1].Name}
|
|
|
|
sort.Strings(result)
|
|
|
|
|
2022-10-24 16:41:40 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]string{"namespace1", "namespace2"},
|
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",
|
|
|
|
"namespaces",
|
|
|
|
"rename",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"namespace2",
|
|
|
|
"newname",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var listAfterRenameNamespaces []v1.Namespace
|
2022-11-14 14:27:02 +01:00
|
|
|
err = executeAndUnmarshal(headscale,
|
2022-10-24 16:41:40 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"namespaces",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listAfterRenameNamespaces,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-24 17:03:59 +02:00
|
|
|
result = []string{listAfterRenameNamespaces[0].Name, listAfterRenameNamespaces[1].Name}
|
|
|
|
sort.Strings(result)
|
|
|
|
|
2022-10-24 16:41:40 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]string{"namespace1", "newname"},
|
2022-10-24 17:03:59 +02:00
|
|
|
result,
|
2022-10-24 16:41:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-10-24 16:42:09 +02:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommand(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
namespace := "preauthkeyspace"
|
|
|
|
count := 3
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
namespace: 0,
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipak"))
|
2022-10-24 16:42:09 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-10-24 16:42:09 +02:00
|
|
|
keys := make([]*v1.PreAuthKey, count)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--tags",
|
|
|
|
"tag:test1,tag:test2",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// 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 14:27:02 +01:00
|
|
|
_, err = headscale.Execute(
|
2022-10-24 16:42:09 +02:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"expire",
|
|
|
|
listedPreAuthKeys[1].Key,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeysAfterExpire,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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()))
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-10-24 17:31:15 +02:00
|
|
|
|
|
|
|
func TestPreAuthKeyCommandWithoutExpiry(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
namespace := "pre-auth-key-without-exp-namespace"
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
namespace: 0,
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipaknaexp"))
|
2022-10-24 17:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthKey,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// 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)),
|
|
|
|
)
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreAuthKeyCommandReusableEphemeral(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
namespace := "pre-auth-key-reus-ephm-namespace"
|
|
|
|
|
|
|
|
scenario, err := NewScenario()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
spec := map[string]int{
|
|
|
|
namespace: 0,
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:10:03 +00:00
|
|
|
err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clipakresueeph"))
|
2022-10-24 17:31:15 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"create",
|
|
|
|
"--reusable=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthReusableKey,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"create",
|
|
|
|
"--ephemeral=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&preAuthEphemeralKey,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
&listedPreAuthKeys,
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// There is one key created by "scenario.CreateHeadscaleEnv"
|
|
|
|
assert.Len(t, listedPreAuthKeys, 3)
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|