mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 11:57:07 +09:00
1 parent
4dbf981679
commit
e9abc6ae35
22 files changed
+732
-257
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
|
||||
clientv1 "github.com/juanfont/headscale/gen/client/v1"
|
||||
"github.com/juanfont/headscale/hscontrol/config"
|
||||
"github.com/juanfont/headscale/hscontrol/db"
|
||||
"github.com/juanfont/headscale/hscontrol/policy"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -22,7 +23,12 @@ 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 := serverConfig()
|
||||
// Validated here: the server, which would, is not started.
|
||||
cfg := serverConfig()
|
||||
|
||||
err := config.Validate(cfg)
|
||||
logWarnings(config.Warnings(cfg))
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading config: %w", err)
|
||||
}
|
||||
|
||||
@@ -376,10 +376,7 @@ dns:
|
||||
require.NoError(t, os.WriteFile(configPath, []byte(config), 0o600))
|
||||
useLoaded(t, configPath)
|
||||
|
||||
cfg, err := serverConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
database, err := db.NewHeadscaleDatabase(cfg)
|
||||
database, err := db.NewHeadscaleDatabase(serverConfig())
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
|
||||
@@ -16,7 +16,6 @@ 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,16 +86,10 @@ func mustMarkRequired(cmd *cobra.Command, names ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// newHeadscaleServerWithConfig builds the server, which validates the
|
||||
// configuration.
|
||||
func newHeadscaleServerWithConfig() (*hscontrol.Headscale, error) {
|
||||
cfg, err := serverConfig()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"loading configuration: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
app, err := hscontrol.NewHeadscale(cfg)
|
||||
app, err := hscontrol.NewHeadscale(serverConfig())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating new headscale: %w", err)
|
||||
}
|
||||
@@ -104,23 +97,16 @@ 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) {
|
||||
// serverConfig is the loaded configuration as the server takes it,
|
||||
// unvalidated.
|
||||
func serverConfig() *types.Config {
|
||||
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
|
||||
return cfg
|
||||
}
|
||||
|
||||
// clientRunE wraps a [cobra.Command.RunE] func, injecting a ready API client
|
||||
|
||||
@@ -105,7 +105,17 @@ 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)
|
||||
assert.Empty(t, serverConfig().CLI.APIKey)
|
||||
}
|
||||
|
||||
// TestBypassDatabaseValidates: the policy commands' --bypass path, which
|
||||
// starts no server, validates the configuration before opening the
|
||||
// database.
|
||||
func TestBypassDatabaseValidates(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
useLoaded(t, writeServerConfig(t, dir, "node:\n ephemeral:\n inactivity_timeout: 1s\n"))
|
||||
|
||||
_, err := bypassDatabase()
|
||||
require.ErrorContains(t, err, "node.ephemeral.inactivity_timeout is below the minimum")
|
||||
assert.NoFileExists(t, filepath.Join(dir, "headscale.db"))
|
||||
}
|
||||
Reference in new issue
Block a user