6
6
"os"
7
7
8
8
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
9
+ "github.com/juanfont/headscale/hscontrol/policy"
9
10
"github.com/rs/zerolog/log"
10
11
"github.com/spf13/cobra"
11
12
)
@@ -19,6 +20,12 @@ func init() {
19
20
log .Fatal ().Err (err ).Msg ("" )
20
21
}
21
22
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 )
22
29
}
23
30
24
31
var policyCmd = & cobra.Command {
@@ -85,3 +92,30 @@ var setPolicy = &cobra.Command{
85
92
SuccessOutput (nil , "Policy updated." , "" )
86
93
},
87
94
}
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