mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-20 04:14:32 +09:00
1 parent
48416ce089
commit
579878ce21
5 files changed
+90
-7
No files matched your search
@@ -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",
|
||||
}
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user