diff --git a/cmd/headscale/cli/check.go b/cmd/headscale/cli/check.go new file mode 100644 index 000000000..9bc70daf1 --- /dev/null +++ b/cmd/headscale/cli/check.go @@ -0,0 +1,12 @@ +package cli + +import "github.com/spf13/cobra" + +func init() { + rootCmd.AddCommand(checkCmd) +} + +var checkCmd = &cobra.Command{ + Use: "check", + Short: "Check configuration or policy without a running server", +} diff --git a/cmd/headscale/cli/check_test.go b/cmd/headscale/cli/check_test.go new file mode 100644 index 000000000..0d61774aa --- /dev/null +++ b/cmd/headscale/cli/check_test.go @@ -0,0 +1,27 @@ +package cli + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestSkipsInitConfig(t *testing.T) { + tests := []struct { + name string + args []string + want bool + }{ + {"check policy", []string{"check", "policy", "-f", "p"}, true}, + {"check config with config flag", []string{"-c", "x.yaml", "check", "config"}, true}, + {"check", []string{"check"}, true}, + {"policy get", []string{"policy", "get"}, false}, + {"nodes list", []string{"nodes", "list"}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, skipsInitConfig(tt.args)) + }) + } +} diff --git a/cmd/headscale/cli/root.go b/cmd/headscale/cli/root.go index ff06ea24b..cfe32fea5 100644 --- a/cmd/headscale/cli/root.go +++ b/cmd/headscale/cli/root.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "os" "runtime" "slices" @@ -39,7 +40,16 @@ func init() { }) } -func initConfig() { +// skipsInitConfig reports whether args select the check command group, which +// must work without a loadable config. Find runs lazily because init order +// across files is not guaranteed to have registered every command. +func skipsInitConfig(args []string) bool { + cmd, _, err := rootCmd.Find(args) + + return err == nil && (cmd == checkCmd || cmd.HasParent() && cmd.Parent() == checkCmd) +} + +func loadConfig() error { if cfgFile == "" { cfgFile = os.Getenv("HEADSCALE_CONFIG") } @@ -47,13 +57,28 @@ func initConfig() { if cfgFile != "" { err := types.LoadConfig(cfgFile, true) if err != nil { - log.Fatal().Caller().Err(err).Msgf("error loading config file %s", cfgFile) - } - } else { - err := types.LoadConfig("", false) - if err != nil { - log.Fatal().Caller().Err(err).Msgf("error loading config") + return fmt.Errorf("loading config file %s: %w", cfgFile, err) } + + return nil + } + + err := types.LoadConfig("", false) + if err != nil { + return fmt.Errorf("loading config: %w", err) + } + + return nil +} + +func initConfig() { + if skipsInitConfig(os.Args[1:]) { + return + } + + err := loadConfig() + if err != nil { + log.Fatal().Caller().Err(err).Msg("error loading config") } machineOutput := hasMachineOutputFlag() diff --git a/hscontrol/policy/v2/types.go b/hscontrol/policy/v2/types.go index 2769b1252..6b12f96fb 100644 --- a/hscontrol/policy/v2/types.go +++ b/hscontrol/policy/v2/types.go @@ -3411,6 +3411,12 @@ func stripMetadataMembers(v *hujson.Value) { } } +// ParsePolicy parses and validates a policy without users or nodes: syntax, +// shape and cross-reference checks only. Empty input returns nil, nil. +func ParsePolicy(b []byte) (*Policy, error) { + return unmarshalPolicy(b) +} + // unmarshalPolicy takes a byte slice and unmarshals it into a [Policy] struct. // In addition to unmarshalling, it will also validate the policy. // This is the only entrypoint of reading a policy from a file or other source. diff --git a/hscontrol/policy/v2/types_test.go b/hscontrol/policy/v2/types_test.go index 0e4a18775..8976933d3 100644 --- a/hscontrol/policy/v2/types_test.go +++ b/hscontrol/policy/v2/types_test.go @@ -6902,3 +6902,16 @@ func TestSetPolicyBareAtToken(t *testing.T) { } } } + +func TestParsePolicy(t *testing.T) { + pol, err := ParsePolicy([]byte(`{"acls":[{"action":"accept","src":["*"],"dst":["*:*"]}]}`)) + require.NoError(t, err) + require.NotNil(t, pol) + + _, err = ParsePolicy([]byte(`{"acls":[{"action":"accept","src":["group:missing"],"dst":["*:*"]}]}`)) + require.ErrorIs(t, err, ErrGroupNotDefined) + + pol, err = ParsePolicy([]byte("")) + require.NoError(t, err) + require.Nil(t, pol) +}