mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 20:04:32 +09:00
1 parent
890adee7fa
commit
f1cbdcfacb
155 files changed
+5918
-5381
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -174,24 +175,33 @@ func fileCheckCmd(use, short, valid string, check func(string) error) *cobra.Com
|
||||
}
|
||||
|
||||
// loadCheckedConfig loads the server config from --file, or finds it as
|
||||
// serve does, and names where it came from.
|
||||
// serve does, logs what loading and the config warn about at its log.level
|
||||
// and in its log.format, and names where it came from.
|
||||
func loadCheckedConfig(cmd *cobra.Command) (*types.Config, string, error) {
|
||||
path, _ := cmd.Flags().GetString("file")
|
||||
|
||||
err := loadConfig(path)
|
||||
|
||||
src := types.ConfigFile()
|
||||
if src == "" {
|
||||
src = "(defaults and environment)"
|
||||
l, err := loadConfig(path)
|
||||
if err == nil {
|
||||
// As initConfig does; a config that failed keeps the console
|
||||
// logger and the default level.
|
||||
useLogConfig(l.Config, cmd.ErrOrStderr())
|
||||
}
|
||||
|
||||
logWarnings(l.Warnings)
|
||||
|
||||
src := cmp.Or(l.File, "(defaults and environment)")
|
||||
if err != nil {
|
||||
return nil, src, err
|
||||
}
|
||||
|
||||
err = config.Validate(l.Config)
|
||||
logWarnings(config.Warnings(l.Config))
|
||||
|
||||
if err != nil {
|
||||
return nil, src, err
|
||||
}
|
||||
|
||||
cfg, err := types.LoadServerConfig()
|
||||
|
||||
return cfg, src, err
|
||||
return l.Config, src, nil
|
||||
}
|
||||
|
||||
// printCheckLine prints one result; an error goes indented below it.
|
||||
|
||||
@@ -2,14 +2,19 @@ package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
policyv2 "github.com/juanfont/headscale/hscontrol/policy/v2"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -368,3 +373,100 @@ func TestCheckFileCommands(t *testing.T) {
|
||||
_, err = runCheck(t, checkDERPMapCmd, writeFile(t, dir, "d.yaml", "{}"))
|
||||
require.ErrorContains(t, err, "d.yaml")
|
||||
}
|
||||
|
||||
// TestCheckConfigWarnings: the check commands report what the environment
|
||||
// warns about, with a valid file and with one that fails, and on a valid
|
||||
// file the configuration's advisories, in its log.format and at its
|
||||
// log.level.
|
||||
func TestCheckConfigWarnings(t *testing.T) {
|
||||
const (
|
||||
unknown = "unknown environment variable HEADSCALE_BOGUS"
|
||||
removed = "configuration variable HEADSCALE_DB_TYPE was removed"
|
||||
advisory = "listen_addr should probably end in :443"
|
||||
acme = "tls:\n acme:\n hostname: example.com\n challenge_type: TLS-ALPN-01\n"
|
||||
)
|
||||
|
||||
t.Setenv("HEADSCALE_BOGUS", "1")
|
||||
t.Setenv("HEADSCALE_DB_TYPE", "sqlite")
|
||||
|
||||
dir := t.TempDir()
|
||||
text := writeServerConfig(t, t.TempDir(), acme)
|
||||
jsonLogs := writeServerConfig(t, t.TempDir(), acme+"log:\n format: json\n")
|
||||
errorLevel := writeServerConfig(t, t.TempDir(), acme+"log:\n level: error\n")
|
||||
disabled := writeServerConfig(t, t.TempDir(), acme+"log:\n level: disabled\n")
|
||||
invalid := writeFile(t, dir, "invalid.yaml", "log:\n format: json\nbogus_key: 1\n")
|
||||
|
||||
oldLogger, oldLevel := log.Logger, zerolog.GlobalLevel()
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
t.Cleanup(func() {
|
||||
log.Logger = oldLogger
|
||||
|
||||
zerolog.SetGlobalLevel(oldLevel)
|
||||
})
|
||||
|
||||
for _, c := range []*cobra.Command{checkConfigCmd, checkAllCmd} {
|
||||
for _, tt := range []struct {
|
||||
name, file string
|
||||
wantErr bool
|
||||
// json: the warnings are JSON on the command's stderr, not on
|
||||
// the logger the command started with.
|
||||
json bool
|
||||
// quiet: log.level hides every warning.
|
||||
quiet bool
|
||||
}{
|
||||
{name: "text", file: text},
|
||||
{name: "json", file: jsonLogs, json: true},
|
||||
{name: "invalid", file: invalid, wantErr: true},
|
||||
{name: "level error", file: errorLevel, quiet: true},
|
||||
{name: "level disabled", file: disabled, quiet: true},
|
||||
} {
|
||||
t.Run(c.Name()+" "+tt.name, func(t *testing.T) {
|
||||
var logged, stderr bytes.Buffer
|
||||
|
||||
// As initConfig leaves it for the check commands.
|
||||
log.Logger = zerolog.New(&logged)
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
|
||||
cmd := &cobra.Command{}
|
||||
cmd.Flags().String("file", tt.file, "")
|
||||
cmd.SetOut(io.Discard)
|
||||
cmd.SetErr(&stderr)
|
||||
|
||||
err := c.RunE(cmd, nil)
|
||||
require.Equal(t, tt.wantErr, err != nil, "%v", err)
|
||||
|
||||
out := logged.String()
|
||||
if tt.quiet {
|
||||
assert.Empty(t, out)
|
||||
assert.Empty(t, stderr.String())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if tt.json {
|
||||
assert.Empty(t, out)
|
||||
|
||||
out = stderr.String()
|
||||
for l := range strings.SplitSeq(strings.TrimSpace(out), "\n") {
|
||||
var rec struct{ Level, Message string }
|
||||
require.NoError(t, json.Unmarshal([]byte(l), &rec), l)
|
||||
assert.Equal(t, "warn", rec.Level)
|
||||
}
|
||||
} else {
|
||||
assert.Empty(t, stderr.String())
|
||||
}
|
||||
|
||||
assert.Contains(t, out, unknown)
|
||||
assert.Contains(t, out, removed)
|
||||
|
||||
if tt.wantErr {
|
||||
assert.NotContains(t, out, advisory)
|
||||
} else {
|
||||
assert.Contains(t, out, advisory)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/juanfont/headscale/hscontrol/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -17,7 +17,7 @@ var dumpConfigCmd = &cobra.Command{
|
||||
Short: "dump current config to /etc/headscale/config.dump.yaml, integration test only",
|
||||
Hidden: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
out, err := types.DumpConfig()
|
||||
out, err := config.Marshal(loaded.Config, ".yaml")
|
||||
if err != nil {
|
||||
return fmt.Errorf("dumping config: %w", err)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"strings"
|
||||
|
||||
clientv2 "github.com/juanfont/headscale/gen/client/v2"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -175,10 +174,7 @@ var deleteOAuthClientCmd = &cobra.Command{
|
||||
// (local trust); a remote address injects the configured API key as a bearer
|
||||
// token.
|
||||
func newV2Client() (context.Context, *clientv2.ClientWithResponses, context.CancelFunc, error) {
|
||||
cfg, err := types.LoadCLIConfig()
|
||||
if err != nil {
|
||||
return nil, nil, nil, fmt.Errorf("loading configuration: %w", err)
|
||||
}
|
||||
cfg := loaded.Config
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.CLI.Timeout)
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
clientv1 "github.com/juanfont/headscale/gen/client/v1"
|
||||
"github.com/juanfont/headscale/hscontrol/db"
|
||||
"github.com/juanfont/headscale/hscontrol/policy"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -23,7 +22,7 @@ var errAborted = errors.New("command aborted by user")
|
||||
// bypassDatabase opens the database directly, bypassing the running server.
|
||||
// The caller must close the returned handle.
|
||||
func bypassDatabase() (*db.HSDatabase, error) {
|
||||
cfg, err := types.LoadServerConfig()
|
||||
cfg, err := serverConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading config: %w", err)
|
||||
}
|
||||
|
||||
+53
-25
@@ -1,12 +1,15 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/config"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -14,7 +17,13 @@ import (
|
||||
"github.com/tcnksm/go-latest"
|
||||
)
|
||||
|
||||
var cfgFile string = ""
|
||||
var (
|
||||
cfgFile string = ""
|
||||
|
||||
// loaded is what initConfig loaded; nil for the commands that skip it:
|
||||
// check, version, mockoidc and completion.
|
||||
loaded *config.Loaded
|
||||
)
|
||||
|
||||
func init() {
|
||||
if len(os.Args) > 1 &&
|
||||
@@ -55,27 +64,35 @@ func skipsInitConfig(args []string) bool {
|
||||
// loadConfig loads path, else the file -c names, else $HEADSCALE_CONFIG,
|
||||
// else the first config.* in /etc/headscale, $XDG_CONFIG_HOME/headscale,
|
||||
// headscale under each $XDG_CONFIG_DIRS entry, ~/.headscale or ".". Without
|
||||
// a file it loads defaults and environment only.
|
||||
func loadConfig(path string) error {
|
||||
for _, p := range []string{path, cfgFile, os.Getenv("HEADSCALE_CONFIG")} {
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
// a file it loads defaults and environment only. The result is never nil.
|
||||
func loadConfig(path string) (*config.Loaded, error) {
|
||||
path = cmp.Or(path, cfgFile, os.Getenv("HEADSCALE_CONFIG"))
|
||||
|
||||
err := types.LoadConfig(p, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading config file %s: %w", p, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
l, err := config.Load(path, path != "", os.Environ())
|
||||
switch {
|
||||
case err != nil && path != "":
|
||||
return l, fmt.Errorf("loading config file %s: %w", path, err)
|
||||
case err != nil:
|
||||
return l, fmt.Errorf("loading config: %w", err)
|
||||
}
|
||||
|
||||
err := types.LoadConfig("", false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading config: %w", err)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
// useLogConfig applies cfg's log.level, and logs to w as JSON when cfg sets
|
||||
// log.format: json.
|
||||
func useLogConfig(cfg *types.Config, w io.Writer) {
|
||||
zerolog.SetGlobalLevel(cfg.Log.Level.Zerolog())
|
||||
|
||||
if cfg.Log.Format == types.JSONLogFormat {
|
||||
log.Logger = log.Output(w)
|
||||
}
|
||||
}
|
||||
|
||||
func logWarnings(warnings []string) {
|
||||
for _, w := range warnings {
|
||||
log.Warn().Msg(w)
|
||||
}
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
@@ -86,12 +103,27 @@ func initConfig() {
|
||||
return
|
||||
}
|
||||
|
||||
err := loadConfig("")
|
||||
machineOutput := hasMachineOutputFlag()
|
||||
|
||||
l, err := loadConfig("")
|
||||
if err != nil {
|
||||
// No log.format applies: main's console logger, on stderr.
|
||||
logWarnings(l.Warnings)
|
||||
log.Fatal().Caller().Err(err).Msg("error loading config")
|
||||
}
|
||||
|
||||
machineOutput := hasMachineOutputFlag()
|
||||
loaded = l
|
||||
cfg := l.Config
|
||||
|
||||
// Machine output owns stdout.
|
||||
logOut := os.Stdout
|
||||
if machineOutput {
|
||||
logOut = os.Stderr
|
||||
}
|
||||
|
||||
useLogConfig(cfg, logOut)
|
||||
|
||||
logWarnings(l.Warnings)
|
||||
|
||||
// If the user has requested a "node" readable format,
|
||||
// then disable login so the output remains valid.
|
||||
@@ -99,11 +131,7 @@ func initConfig() {
|
||||
zerolog.SetGlobalLevel(zerolog.Disabled)
|
||||
}
|
||||
|
||||
if types.LogFormatSetting() == types.JSONLogFormat {
|
||||
log.Logger = log.Output(os.Stdout)
|
||||
}
|
||||
|
||||
if !types.UpdateCheckDisabled() && !machineOutput {
|
||||
if !cfg.DisableUpdateCheck && !machineOutput {
|
||||
versionInfo := types.GetVersionInfo()
|
||||
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&
|
||||
!versionInfo.Dirty {
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/db"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -366,9 +374,9 @@ dns:
|
||||
override_local_dns: false
|
||||
`, filepath.Join(dir, "headscale.db"))
|
||||
require.NoError(t, os.WriteFile(configPath, []byte(config), 0o600))
|
||||
require.NoError(t, types.LoadConfig(configPath, true))
|
||||
useLoaded(t, configPath)
|
||||
|
||||
cfg, err := types.LoadServerConfig()
|
||||
cfg, err := serverConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
database, err := db.NewHeadscaleDatabase(cfg)
|
||||
@@ -414,6 +422,19 @@ dns:
|
||||
}
|
||||
}
|
||||
|
||||
// useLoaded loads path as initConfig does, until the test ends.
|
||||
func useLoaded(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
|
||||
l, err := loadConfig(path)
|
||||
require.NoError(t, err)
|
||||
|
||||
old := loaded
|
||||
loaded = l
|
||||
|
||||
t.Cleanup(func() { loaded = old })
|
||||
}
|
||||
|
||||
func TestInitConfigSkippedLogLevel(t *testing.T) {
|
||||
oldArgs, oldLevel := os.Args, zerolog.GlobalLevel()
|
||||
|
||||
@@ -459,8 +480,140 @@ func TestLoadConfig(t *testing.T) {
|
||||
|
||||
t.Cleanup(func() { cfgFile = old })
|
||||
|
||||
require.NoError(t, loadConfig(tt.path))
|
||||
require.Equal(t, tt.want, types.ConfigFile())
|
||||
l, err := loadConfig(tt.path)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.want, l.File)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCLIOnlyConfig: a file with only what client commands read, as the
|
||||
// NixOS module writes to /etc/headscale/config.yaml, serves them; check
|
||||
// config reports what the server lacks.
|
||||
func TestCLIOnlyConfig(t *testing.T) {
|
||||
path := writeFile(t, t.TempDir(), "config.yaml", `cli:
|
||||
address: headscale.example.com:443
|
||||
api_key: abc
|
||||
unix_socket: /run/headscale/headscale.sock
|
||||
`)
|
||||
|
||||
useLoaded(t, path)
|
||||
|
||||
_, _, cancel, err := newHeadscaleCLIWithConfig()
|
||||
require.NoError(t, err)
|
||||
cancel()
|
||||
|
||||
_, err = runCheck(t, checkConfigCmd, path)
|
||||
require.ErrorContains(t, err, "server_url")
|
||||
}
|
||||
|
||||
// runHeadscale runs the CLI as main does, in a child process: loading a
|
||||
// config can exit it.
|
||||
func runHeadscale(t *testing.T, env []string, args ...string) (string, string, error) {
|
||||
t.Helper()
|
||||
|
||||
cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=^TestHeadscaleProcess$") //nolint:gosec // this test binary
|
||||
|
||||
cmd.Env = append([]string{"HEADSCALE_TEST_ARGS=" + strings.Join(args, "\n")}, env...)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
|
||||
cmd.Stdout, cmd.Stderr = &stdout, &stderr
|
||||
err := cmd.Run()
|
||||
|
||||
return stdout.String(), stderr.String(), err
|
||||
}
|
||||
|
||||
// TestHeadscaleProcess is the child of runHeadscale.
|
||||
func TestHeadscaleProcess(t *testing.T) {
|
||||
args := os.Getenv("HEADSCALE_TEST_ARGS")
|
||||
if args == "" {
|
||||
t.Skip("run by runHeadscale")
|
||||
}
|
||||
|
||||
os.Args = append([]string{"headscale"}, strings.Split(args, "\n")...)
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: true})
|
||||
|
||||
zerolog.SetGlobalLevel(zerolog.TraceLevel)
|
||||
|
||||
Execute()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// TestInitConfigWarnings: what loading warns about follows log.format, never
|
||||
// reaches stdout under machine output, and is reported with a load error.
|
||||
func TestInitConfigWarnings(t *testing.T) {
|
||||
const warning = "unknown environment variable HEADSCALE_BOGUS"
|
||||
|
||||
env := []string{"HEADSCALE_BOGUS=1", "HEADSCALE_DISABLE_CHECK_UPDATES=true"}
|
||||
dir := t.TempDir()
|
||||
jsonLogs := writeFile(t, dir, "json.yaml", "log:\n format: json\n")
|
||||
invalid := writeFile(t, dir, "invalid.yaml", "bogus_key: 1\n")
|
||||
|
||||
// warnLine is the line holding the warning, as JSON when it is one.
|
||||
warnLine := func(t *testing.T, out string) map[string]any {
|
||||
t.Helper()
|
||||
|
||||
for l := range strings.SplitSeq(out, "\n") {
|
||||
if strings.Contains(l, warning) {
|
||||
var m map[string]any
|
||||
|
||||
_ = json.Unmarshal([]byte(l), &m)
|
||||
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
t.Fatalf("no %q in %q", warning, out)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
t.Run("json log format", func(t *testing.T) {
|
||||
stdout, stderr, err := runHeadscale(t, env, "-c", jsonLogs, "generate", "private-key")
|
||||
require.NoError(t, err, stderr)
|
||||
assert.Equal(t, "warn", warnLine(t, stdout+stderr)["level"])
|
||||
})
|
||||
|
||||
// Every spelling Cobra accepts for --output.
|
||||
for _, format := range []string{"json", "json-line", "yaml"} {
|
||||
for _, flag := range [][]string{
|
||||
{"-o", format}, {"--output", format}, {"--output=" + format}, {"-o=" + format}, {"-o" + format},
|
||||
} {
|
||||
t.Run("machine output "+strings.Join(flag, " "), func(t *testing.T) {
|
||||
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "generate", "private-key"}, flag...)...)
|
||||
require.NoError(t, err, stderr)
|
||||
|
||||
// JSON is YAML too; the result is one object.
|
||||
var out map[string]string
|
||||
require.NoError(t, yaml.Unmarshal([]byte(stdout), &out), stdout)
|
||||
assert.Equal(t, []string{"private_key"}, slices.Collect(maps.Keys(out)))
|
||||
assert.Equal(t, "warn", warnLine(t, stderr)["level"])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// version has its own --output, which shadows the root's.
|
||||
for _, flag := range [][]string{{"-o", "json"}, {"--output=json"}} {
|
||||
t.Run("version "+strings.Join(flag, " "), func(t *testing.T) {
|
||||
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", jsonLogs, "version"}, flag...)...)
|
||||
require.NoError(t, err, stderr)
|
||||
|
||||
var out map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout)
|
||||
assert.Contains(t, out, "version")
|
||||
assert.Equal(t, "warn", warnLine(t, stderr)["level"])
|
||||
})
|
||||
}
|
||||
|
||||
for _, output := range [][]string{nil, {"-o", "json"}} {
|
||||
t.Run("load error "+strings.Join(output, " "), func(t *testing.T) {
|
||||
stdout, stderr, err := runHeadscale(t, env, append([]string{"-c", invalid, "generate", "private-key"}, output...)...)
|
||||
require.Error(t, err)
|
||||
assert.Empty(t, stdout)
|
||||
warnLine(t, stderr)
|
||||
assert.Contains(t, stderr, "unknown configuration key bogus_key")
|
||||
assert.NotContains(t, stderr, "panic")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
clientv1 "github.com/juanfont/headscale/gen/client/v1"
|
||||
"github.com/juanfont/headscale/hscontrol"
|
||||
"github.com/juanfont/headscale/hscontrol/config"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/juanfont/headscale/hscontrol/util"
|
||||
"github.com/juanfont/headscale/hscontrol/util/zlog/zf"
|
||||
@@ -87,7 +88,7 @@ func mustMarkRequired(cmd *cobra.Command, names ...string) {
|
||||
}
|
||||
|
||||
func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) {
|
||||
cfg, err := types.LoadServerConfig()
|
||||
cfg, err := serverConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"loading configuration: %w",
|
||||
@@ -103,6 +104,25 @@ func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) {
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// serverConfig is the loaded configuration validated for the server, its
|
||||
// advisories logged.
|
||||
func serverConfig() (*types.Config, error) {
|
||||
cfg := loaded.Config
|
||||
|
||||
err := config.Validate(cfg)
|
||||
logWarnings(config.Warnings(cfg))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The server never uses the CLI's key, which may be an unread file:
|
||||
// reference.
|
||||
cfg.CLI.APIKey = ""
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// clientRunE wraps a [cobra.Command.RunE] func, injecting a ready API client
|
||||
// and a context whose timeout/cancel the wrapper owns.
|
||||
func clientRunE(
|
||||
@@ -141,10 +161,7 @@ func withClient(
|
||||
// Otherwise it talks to the remote TCP address over HTTPS and injects the
|
||||
// configured API key as a bearer token.
|
||||
func newHeadscaleCLIWithConfig() (context.Context, *clientv1.ClientWithResponses, context.CancelFunc, error) {
|
||||
cfg, err := types.LoadCLIConfig()
|
||||
if err != nil {
|
||||
return nil, nil, nil, fmt.Errorf("loading configuration: %w", err)
|
||||
}
|
||||
cfg := loaded.Config
|
||||
|
||||
log.Debug().
|
||||
Dur("timeout", cfg.CLI.Timeout).
|
||||
@@ -408,8 +425,16 @@ func printError(err error, outputFormat string) {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", out)
|
||||
}
|
||||
|
||||
// hasMachineOutputFlag reports whether --output, as Cobra parsed it in any
|
||||
// spelling for the command being run, asks for a machine-readable format.
|
||||
// The command's own flags hold it: version shadows the root's --output.
|
||||
func hasMachineOutputFlag() bool {
|
||||
return slices.ContainsFunc(os.Args, func(arg string) bool {
|
||||
return arg == outputFormatJSON || arg == outputFormatJSONLine || arg == outputFormatYAML
|
||||
})
|
||||
cmd, _, err := rootCmd.Find(os.Args[1:])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
output, _ := cmd.Flags().GetString("output")
|
||||
|
||||
return slices.Contains([]string{outputFormatJSON, outputFormatJSONLine, outputFormatYAML}, output)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -68,17 +67,15 @@ func TestCLIAPIKey(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfgDir := t.TempDir()
|
||||
require.NoError(t, os.WriteFile(filepath.Join(cfgDir, "config.yaml"),
|
||||
cfgPath := filepath.Join(cfgDir, "config.yaml")
|
||||
require.NoError(t, os.WriteFile(cfgPath,
|
||||
[]byte("unix_socket: "+filepath.Join(cfgDir, "headscale.sock")+"\n"), 0o600))
|
||||
t.Setenv("HEADSCALE_CLI_ADDRESS", tt.address)
|
||||
t.Setenv("HEADSCALE_CLI_API_KEY", tt.apiKey)
|
||||
require.NoError(t, types.LoadConfig(cfgDir, false))
|
||||
useLoaded(t, cfgPath)
|
||||
|
||||
if tt.address != "" && tt.wantErr == "" {
|
||||
cfg, err := types.LoadCLIConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
got, err := cliAPIKey(cfg.CLI)
|
||||
got, err := cliAPIKey(loaded.Config.CLI)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.want, got)
|
||||
}
|
||||
@@ -101,3 +98,14 @@ func TestCLIAPIKey(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerConfigDropsCLIKey: the server never holds the CLI's key, which
|
||||
// may be a file only the CLI can read.
|
||||
func TestServerConfigDropsCLIKey(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
useLoaded(t, writeServerConfig(t, dir, "cli:\n api_key: file:"+filepath.Join(dir, "missing")+"\n"))
|
||||
|
||||
cfg, err := serverConfig()
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, cfg.CLI.APIKey)
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/config"
|
||||
"github.com/juanfont/headscale/hscontrol/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -26,10 +27,10 @@ func TestConfigFileLoading(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Load example config, it should load without validation errors
|
||||
err = types.LoadConfig(cfgFile, true)
|
||||
l, err := config.Load(cfgFile, true, os.Environ())
|
||||
require.NoError(t, err)
|
||||
|
||||
assertExampleConfig(t)
|
||||
assertExampleConfig(t, l.Config)
|
||||
}
|
||||
|
||||
func TestConfigLoading(t *testing.T) {
|
||||
@@ -46,19 +47,18 @@ func TestConfigLoading(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Load example config, it should load without validation errors
|
||||
err = types.LoadConfig(tmpDir, false)
|
||||
l, err := config.Load(tmpDir, false, os.Environ())
|
||||
require.NoError(t, err)
|
||||
|
||||
assertExampleConfig(t)
|
||||
assertExampleConfig(t, l.Config)
|
||||
}
|
||||
|
||||
// assertExampleConfig checks the loaded config-example.yaml was interpreted
|
||||
// correctly.
|
||||
func assertExampleConfig(t *testing.T) {
|
||||
func assertExampleConfig(t *testing.T, cfg *types.Config) {
|
||||
t.Helper()
|
||||
|
||||
cfg, err := types.LoadServerConfig()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, config.Validate(cfg))
|
||||
|
||||
assert.Equal(t, "http://127.0.0.1:8080", cfg.ServerURL)
|
||||
assert.Equal(t, types.ListenAddr("127.0.0.1:8080"), cfg.Addr)
|
||||
|
||||
Reference in new issue
Block a user