@@ -16,107 +16,120 @@ package cmd
16
16
17
17
import (
18
18
"encoding/json"
19
+ "reflect"
20
+ "regexp"
21
+ "strconv"
19
22
20
23
"github.com/casbin/casbin/v2"
21
24
"github.com/spf13/cobra"
25
+ "golang.org/x/text/cases"
26
+ "golang.org/x/text/language"
22
27
)
23
28
24
29
type ResponseBody struct {
25
30
Allow bool `json:"allow"`
26
31
Explain []string `json:"explain"`
27
32
}
28
33
29
- // enforceExCmd represents the enforceEx command.
30
- var enforceExCmd = & cobra.Command {
31
- Use : "enforceEx" ,
32
- Short : "Test if a 'subject' can access a 'object' with a given 'action' based on the policy" ,
33
- Long : `Test if a 'subject' can access a 'object' with a given 'action' based on the policy` ,
34
- Run : func (cmd * cobra.Command , args []string ) {
35
- modelPath , _ := cmd .Flags ().GetString ("model" )
36
- policyPath , _ := cmd .Flags ().GetString ("policy" )
37
-
38
- e , err := casbin .NewEnforcer (modelPath , policyPath )
39
- if err != nil {
40
- panic (err )
41
- }
34
+ // Function to handle enforcement results.
35
+ func handleEnforceResult (cmd * cobra.Command , res bool , explain []string , err error ) {
36
+ if err != nil {
37
+ cmd .PrintErrf ("Error during enforcement: %v\n " , err )
38
+ return
39
+ }
40
+
41
+ response := ResponseBody {
42
+ Allow : res ,
43
+ Explain : explain ,
44
+ }
45
+
46
+ encoder := json .NewEncoder (cmd .OutOrStdout ())
47
+ encoder .SetEscapeHTML (false )
48
+ encoder .Encode (response )
49
+ }
42
50
43
- params := make ([]interface {}, len (args ))
44
- for i , v := range args {
45
- params [i ] = v
51
+ // Function to parse parameters and execute policy check.
52
+ func executeEnforce (cmd * cobra.Command , args []string , isEnforceEx bool ) {
53
+ modelPath , _ := cmd .Flags ().GetString ("model" )
54
+ policyPath , _ := cmd .Flags ().GetString ("policy" )
55
+
56
+ e , err := casbin .NewEnforcer (modelPath , policyPath )
57
+ if err != nil {
58
+ panic (err )
59
+ }
60
+
61
+ // Define regex pattern to match format like {field: value}.
62
+ paramRegex := regexp .MustCompile (`{\s*"?(\w+)"?\s*:\s*(\d+)\s*}` )
63
+
64
+ params := make ([]interface {}, len (args ))
65
+ for i , v := range args {
66
+ // Using regex pattern to match parameters.
67
+ if matches := paramRegex .FindStringSubmatch (v ); len (matches ) == 3 {
68
+ fieldName := matches [1 ]
69
+ valueStr := matches [2 ]
70
+
71
+ // Convert value to integer.
72
+ if val , err := strconv .Atoi (valueStr ); err == nil {
73
+ // Dynamically create struct type.
74
+ caser := cases .Title (language .English )
75
+ structType := reflect .StructOf ([]reflect.StructField {
76
+ {
77
+ Name : caser .String (fieldName ),
78
+ Type : reflect .TypeOf (0 ),
79
+ },
80
+ })
81
+
82
+ // Create struct instance and set value.
83
+ structValue := reflect .New (structType ).Elem ()
84
+ structValue .Field (0 ).SetInt (int64 (val ))
85
+
86
+ params [i ] = structValue .Interface ()
87
+ continue
88
+ }
46
89
}
90
+ params [i ] = v
91
+ }
47
92
93
+ if isEnforceEx {
48
94
res , explain , err := e .EnforceEx (params ... )
49
- if err != nil {
50
- cmd .PrintErrf ("Error during enforcement: %v\n " , err )
51
- return
52
- }
53
-
54
- response := ResponseBody {
55
- Allow : res ,
56
- Explain : explain ,
57
- }
58
-
59
- jsonResponse , err := json .Marshal (response )
60
- if err != nil {
61
- cmd .PrintErrf ("Error marshaling JSON: %v\n " , err )
62
- return
63
- }
64
-
65
- cmd .Println (string (jsonResponse ))
66
- },
95
+ handleEnforceResult (cmd , res , explain , err )
96
+ } else {
97
+ res , err := e .Enforce (params ... )
98
+ handleEnforceResult (cmd , res , []string {}, err )
99
+ }
67
100
}
68
101
69
102
// enforceCmd represents the enforce command.
70
103
var enforceCmd = & cobra.Command {
71
104
Use : "enforce" ,
72
- Short : "Test if a 'subject' can access a 'object' with a given 'action' based on the policy" ,
73
- Long : `Test if a 'subject' can access a 'object' with a given 'action' based on the policy` ,
105
+ Short : "Test if a 'subject' can access a 'object' with a given 'action' based on the policy. " ,
106
+ Long : `Test if a 'subject' can access a 'object' with a given 'action' based on the policy. ` ,
74
107
Run : func (cmd * cobra.Command , args []string ) {
75
- modelPath , _ := cmd .Flags ().GetString ("model" )
76
- policyPath , _ := cmd .Flags ().GetString ("policy" )
77
-
78
- e , err := casbin .NewEnforcer (modelPath , policyPath )
79
- if err != nil {
80
- panic (err )
81
- }
82
-
83
- params := make ([]interface {}, len (args ))
84
- for i , v := range args {
85
- params [i ] = v
86
- }
87
-
88
- res , err := e .Enforce (params ... )
89
- if err != nil {
90
- cmd .PrintErrf ("Error during enforcement: %v\n " , err )
91
- return
92
- }
93
-
94
- response := ResponseBody {
95
- Allow : res ,
96
- Explain : []string {},
97
- }
98
-
99
- jsonResponse , err := json .Marshal (response )
100
- if err != nil {
101
- cmd .PrintErrf ("Error marshaling response: %v\n " , err )
102
- return
103
- }
108
+ executeEnforce (cmd , args , false )
109
+ },
110
+ }
104
111
105
- cmd .Println (string (jsonResponse ))
112
+ // enforceExCmd represents the enforceEx command.
113
+ var enforceExCmd = & cobra.Command {
114
+ Use : "enforceEx" ,
115
+ Short : "Test if a 'subject' can access a 'object' with a given 'action' based on the policy." ,
116
+ Long : `Test if a 'subject' can access a 'object' with a given 'action' based on the policy.` ,
117
+ Run : func (cmd * cobra.Command , args []string ) {
118
+ executeEnforce (cmd , args , true )
106
119
},
107
120
}
108
121
109
122
func init () {
110
123
rootCmd .AddCommand (enforceExCmd )
111
124
rootCmd .AddCommand (enforceCmd )
112
125
113
- enforceExCmd .Flags ().StringP ("model" , "m" , "" , "Path to the model file" )
126
+ enforceExCmd .Flags ().StringP ("model" , "m" , "" , "Path to the model file. " )
114
127
_ = enforceExCmd .MarkFlagRequired ("model" )
115
- enforceExCmd .Flags ().StringP ("policy" , "p" , "" , "Path to the policy file" )
128
+ enforceExCmd .Flags ().StringP ("policy" , "p" , "" , "Path to the policy file. " )
116
129
_ = enforceExCmd .MarkFlagRequired ("policy" )
117
130
118
- enforceCmd .Flags ().StringP ("model" , "m" , "" , "Path to the model file" )
131
+ enforceCmd .Flags ().StringP ("model" , "m" , "" , "Path to the model file. " )
119
132
_ = enforceCmd .MarkFlagRequired ("model" )
120
- enforceCmd .Flags ().StringP ("policy" , "p" , "" , "Path to the policy file" )
133
+ enforceCmd .Flags ().StringP ("policy" , "p" , "" , "Path to the policy file. " )
121
134
_ = enforceCmd .MarkFlagRequired ("policy" )
122
135
}
0 commit comments