mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-20 12:24:32 +09:00
cli: add check config and check all
check config validates only the config; check all adds one ok/FAIL/skip line per named file. Fixes #3443
This commit is contained in:
1 parent
0208434691
commit
ae6ea2cce4
7 files changed
+459
-35
No files matched your search
@@ -2,12 +2,15 @@ package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -19,6 +22,8 @@ func TestSkipsInitConfig(t *testing.T) {
|
||||
}{
|
||||
{"check policy", []string{"check", "policy", "-f", "p"}, true},
|
||||
{"check policy with config flag", []string{"-c", "x.yaml", "check", "policy"}, true},
|
||||
{"check config with config flag", []string{"-c", "x.yaml", "check", "config"}, true},
|
||||
{"check all with file", []string{"check", "all", "-f", "x.yaml"}, true},
|
||||
{"check", []string{"check"}, true},
|
||||
{"help check policy", []string{"help", "check", "policy"}, true},
|
||||
{"policy get", []string{"policy", "get"}, false},
|
||||
@@ -108,3 +113,177 @@ func TestCheckPolicy(t *testing.T) {
|
||||
require.NoError(t, checkPolicyCmd.RunE(cmd, nil))
|
||||
})
|
||||
}
|
||||
|
||||
// writeServerConfig writes a valid server config with relative key and
|
||||
// database paths, plus extra, to dir/config.yaml.
|
||||
func writeServerConfig(t *testing.T, dir, extra string) string {
|
||||
t.Helper()
|
||||
|
||||
p := filepath.Join(dir, "config.yaml")
|
||||
require.NoError(t, os.WriteFile(p, []byte(`server_url: https://headscale.example.com
|
||||
noise:
|
||||
private_key_path: noise.key
|
||||
prefixes:
|
||||
v4: 100.64.0.0/10
|
||||
database:
|
||||
type: sqlite3
|
||||
sqlite:
|
||||
path: headscale.db
|
||||
dns:
|
||||
magic_dns: false
|
||||
override_local_dns: false
|
||||
`+extra), 0o600))
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, dir, name, content string) string {
|
||||
t.Helper()
|
||||
|
||||
p := filepath.Join(dir, name)
|
||||
require.NoError(t, os.WriteFile(p, []byte(content), 0o600))
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func runCheck(t *testing.T, c *cobra.Command, file string) (string, error) {
|
||||
t.Helper()
|
||||
|
||||
viper.Reset()
|
||||
t.Cleanup(viper.Reset)
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
cmd.Flags().String("file", file, "")
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
cmd.SetOut(&buf)
|
||||
|
||||
err := c.RunE(cmd, nil)
|
||||
|
||||
return buf.String(), err
|
||||
}
|
||||
|
||||
func dirNames(t *testing.T, dir string) []string {
|
||||
t.Helper()
|
||||
|
||||
entries, err := os.ReadDir(dir)
|
||||
require.NoError(t, err)
|
||||
|
||||
names := make([]string, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
|
||||
return names
|
||||
}
|
||||
|
||||
const badGroupPolicy = `{"acls":[{"action":"accept","src":["group:missing"],"dst":["*:*"]}]}`
|
||||
|
||||
func TestCheckConfig(t *testing.T) {
|
||||
t.Run("valid creates nothing", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := writeServerConfig(t, dir, "")
|
||||
|
||||
out, err := runCheck(t, checkConfigCmd, cfg)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "Configuration "+cfg+" is valid\n", out)
|
||||
require.Equal(t, []string{"config.yaml"}, dirNames(t, dir))
|
||||
})
|
||||
|
||||
t.Run("named files are not checked", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "policy.hujson", badGroupPolicy)
|
||||
|
||||
_, err := runCheck(t, checkConfigCmd, writeServerConfig(t, dir, "policy:\n path: policy.hujson\n"))
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("bad server_url", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := writeFile(t, dir, "config.yaml", "server_url: ftp://x\n")
|
||||
|
||||
_, err := runCheck(t, checkConfigCmd, cfg)
|
||||
require.ErrorContains(t, err, "server_url")
|
||||
})
|
||||
|
||||
t.Run("derp server without key path", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
_, err := runCheck(t, checkConfigCmd, writeServerConfig(t, dir,
|
||||
"derp:\n server:\n enabled: true\n stun_listen_addr: \"0.0.0.0:3478\"\n"))
|
||||
require.ErrorContains(t, err, "derp.server.private_key_path is required")
|
||||
})
|
||||
|
||||
t.Run("arguments are rejected", func(t *testing.T) {
|
||||
require.Error(t, checkConfigCmd.Args(checkConfigCmd, []string{"other.yaml"}))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCheckAll(t *testing.T) {
|
||||
t.Run("one line per file", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, dir, "policy.hujson", badGroupPolicy)
|
||||
derpMap := writeFile(t, dir, "derp.yaml", `regions:
|
||||
900:
|
||||
regionid: 900
|
||||
regioncode: test
|
||||
nodes:
|
||||
- name: 900a
|
||||
regionid: 900
|
||||
hostname: derp.example.com
|
||||
`)
|
||||
cfg := writeServerConfig(t, dir, fmt.Sprintf("policy:\n path: policy.hujson\nderp:\n paths: [%q]\n", derpMap))
|
||||
|
||||
out, err := runCheck(t, checkAllCmd, cfg)
|
||||
require.ErrorIs(t, err, errCheckFailed)
|
||||
require.ErrorContains(t, err, "1 of 3 files")
|
||||
|
||||
// Keys are padded to the longest, noise.private_key_path.
|
||||
line := func(status, key, path string) string {
|
||||
return fmt.Sprintf("%-4s %-22s %s", status, key, path)
|
||||
}
|
||||
lines := strings.Split(out, "\n")
|
||||
require.Equal(t, line("ok", "config", cfg), lines[0])
|
||||
require.Equal(t, line("FAIL", "policy.path", filepath.Join(dir, "policy.hujson")), lines[1])
|
||||
require.True(t, strings.HasPrefix(lines[2], " "), "error indented under its file: %q", lines[2])
|
||||
require.Contains(t, lines[2], "group:missing")
|
||||
require.Contains(t, lines, line("ok", "derp.paths", derpMap))
|
||||
require.Contains(t, lines, line("skip", "noise.private_key_path", filepath.Join(dir, "noise.key")+" (missing; startup creates it)"))
|
||||
|
||||
require.Equal(t, []string{"config.yaml", "derp.yaml", "policy.hujson"}, dirNames(t, dir))
|
||||
})
|
||||
|
||||
t.Run("all files valid", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
out, err := runCheck(t, checkAllCmd, writeServerConfig(t, dir, ""))
|
||||
require.NoError(t, err)
|
||||
require.NotContains(t, out, "FAIL")
|
||||
})
|
||||
|
||||
t.Run("config fails and files are not checked", func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := writeFile(t, dir, "config.yaml", "server_url: ftp://x\n")
|
||||
|
||||
out, err := runCheck(t, checkAllCmd, cfg)
|
||||
require.ErrorIs(t, err, errCheckFailed)
|
||||
require.True(t, strings.HasPrefix(out, "FAIL config "+cfg+"\n"), out)
|
||||
require.Contains(t, out, "server_url")
|
||||
})
|
||||
}
|
||||
|
||||
func TestCheckFileCommands(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
out, err := runCheck(t, checkExtraRecordsCmd,
|
||||
writeFile(t, dir, "r.json", `[{"name":"a.example.com","type":"A","value":"100.64.0.1"}]`))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "Extra records are valid\n", out)
|
||||
|
||||
_, err = runCheck(t, checkExtraRecordsCmd, writeFile(t, dir, "bad.json", "{"))
|
||||
require.ErrorContains(t, err, "bad.json")
|
||||
|
||||
_, err = runCheck(t, checkDERPMapCmd, writeFile(t, dir, "d.yaml", "{}"))
|
||||
require.ErrorContains(t, err, "d.yaml")
|
||||
}
|
||||
Reference in new issue
Block a user