Skip to content

Commit 93afb03

Browse files
authored
cmd: add policy check command (#2553)
1 parent e4d10ad commit 93afb03

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ working in v1 and not tested might be broken in v2 (and vice versa).
7171
**We do need help testing this code**
7272

7373

74-
#### Other breaking
75-
76-
- Disallow `server_url` and `base_domain` to be equal
77-
[#2544](https://github.com/juanfont/headscale/pull/2544)
7874

7975
#### Other breaking changes
8076

77+
- Disallow `server_url` and `base_domain` to be equal
78+
[#2544](https://github.com/juanfont/headscale/pull/2544)
8179
- Return full user in API for pre auth keys instead of string
8280
[#2542](https://github.com/juanfont/headscale/pull/2542)
8381
- Pre auth key API/CLI now uses ID over username
@@ -86,6 +84,8 @@ working in v1 and not tested might be broken in v2 (and vice versa).
8684
### Changes
8785

8886
- Use Go 1.24 [#2427](https://github.com/juanfont/headscale/pull/2427)
87+
- Add `headscale policy check` command to check policy
88+
[#2553](https://github.com/juanfont/headscale/pull/2553)
8989
- `oidc.map_legacy_users` and `oidc.strip_email_domain` has been removed
9090
[#2411](https://github.com/juanfont/headscale/pull/2411)
9191
- Add more information to `/debug` endpoint

cmd/headscale/cli/policy.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
9+
"github.com/juanfont/headscale/hscontrol/policy"
910
"github.com/rs/zerolog/log"
1011
"github.com/spf13/cobra"
1112
)
@@ -19,6 +20,12 @@ func init() {
1920
log.Fatal().Err(err).Msg("")
2021
}
2122
policyCmd.AddCommand(setPolicy)
23+
24+
checkPolicy.Flags().StringP("file", "f", "", "Path to a policy file in HuJSON format")
25+
if err := checkPolicy.MarkFlagRequired("file"); err != nil {
26+
log.Fatal().Err(err).Msg("")
27+
}
28+
policyCmd.AddCommand(checkPolicy)
2229
}
2330

2431
var policyCmd = &cobra.Command{
@@ -85,3 +92,30 @@ var setPolicy = &cobra.Command{
8592
SuccessOutput(nil, "Policy updated.", "")
8693
},
8794
}
95+
96+
var checkPolicy = &cobra.Command{
97+
Use: "check",
98+
Short: "Check the Policy file for errors",
99+
Run: func(cmd *cobra.Command, args []string) {
100+
output, _ := cmd.Flags().GetString("output")
101+
policyPath, _ := cmd.Flags().GetString("file")
102+
103+
f, err := os.Open(policyPath)
104+
if err != nil {
105+
ErrorOutput(err, fmt.Sprintf("Error opening the policy file: %s", err), output)
106+
}
107+
defer f.Close()
108+
109+
policyBytes, err := io.ReadAll(f)
110+
if err != nil {
111+
ErrorOutput(err, fmt.Sprintf("Error reading the policy file: %s", err), output)
112+
}
113+
114+
_, err = policy.NewPolicyManager(policyBytes, nil, nil)
115+
if err != nil {
116+
ErrorOutput(err, fmt.Sprintf("Error parsing the policy file: %s", err), output)
117+
}
118+
119+
SuccessOutput(nil, "Policy is valid", "")
120+
},
121+
}

0 commit comments

Comments
 (0)