-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
216 lines (186 loc) · 5.72 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package go_policy_enforcer
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
)
// Policy represents a set of rules that define access control or behavior.
// It is used to enforce policies on resources, such as data or actions.
//
// The Policy struct has the following fields:
// - Name: A string representing the name of the policy.
// - Rules: A slice of Rule structs representing the rules that define the policy.
type Policy struct {
Name string
Rules []Rule
}
// Evaluate checks if the given resource adheres to the policy's rules.
// The resource must be a struct, and its fields are evaluated against the policy's rules.
// If any rule fails, the function returns false. Otherwise, it returns true.
//
// Parameters:
// - resource: The resource to be evaluated. It must be a struct.
//
// Return:
// - bool: Returns true if the resource adheres to all policy rules, false otherwise.
func (p *Policy) Evaluate(resource any) bool {
v := reflect.ValueOf(resource)
// Handle pointers by dereferencing them
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
// Ensure we're working with a struct
if v.Kind() != reflect.Struct {
return false
}
for _, rule := range p.Rules {
// Handle nested rules
if nestedRules, ok := rule.Value.([]Rule); ok {
fieldValue, err := getNestedField(v, rule.Field)
if err != nil {
return false
}
if fieldValue.Kind() == reflect.Slice {
matched := false
for i := 0; i < fieldValue.Len(); i++ {
elem := fieldValue.Index(i).Interface()
for _, nestedRule := range nestedRules {
ok, err := evaluatePolicyCheckOperator(
nestedRule.Operator,
reflect.ValueOf(elem).FieldByName(nestedRule.Field).Interface(),
nestedRule.Value,
)
if err != nil {
log.Println(err)
return false
}
// Perform the pattern match
if ok {
matched = true
break
}
}
if matched {
break
}
}
if !matched {
return false
}
continue
}
}
// Handle regular policy checks
fieldValue, err := getNestedField(v, rule.Field)
if err != nil {
return false
}
if !fieldValue.CanInterface() {
return false
}
ok, err := evaluatePolicyCheckOperator(rule.Operator, fieldValue.Interface(), rule.Value)
if err != nil || !ok {
return false
}
}
return true
}
func getNestedField(v reflect.Value, fieldPath string) (reflect.Value, error) {
fields := strings.Split(fieldPath, ".")
for i, field := range fields {
// Handle map access
if v.Kind() == reflect.Map {
key := reflect.ValueOf(field)
v = v.MapIndex(key)
if !v.IsValid() {
return reflect.Value{}, fmt.Errorf("key %s not found in map", field)
}
} else {
// Handle slice indexing (e.g., Field[0])
if strings.Contains(field, "[") && strings.Contains(field, "]") {
parts := strings.Split(field, "[")
fieldName := parts[0]
indexStr := strings.TrimSuffix(parts[1], "]")
// Get the field by name
v = v.FieldByName(fieldName)
if !v.IsValid() {
return reflect.Value{}, fmt.Errorf("field %s not found", fieldName)
}
// Ensure it's a slice or array
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return reflect.Value{}, fmt.Errorf("field %s is not a slice or array", fieldName)
}
// Convert the index to an integer
index, err := strconv.Atoi(indexStr)
if err != nil {
return reflect.Value{}, fmt.Errorf("invalid slice index %s", indexStr)
}
// Check if the index is within bounds
if index >= v.Len() {
return reflect.Value{}, fmt.Errorf("index %d out of bounds for slice %s", index, fieldName)
}
// Get the indexed value
v = v.Index(index)
} else {
// Regular struct field access
v = v.FieldByName(field)
if !v.IsValid() {
return reflect.Value{}, fmt.Errorf("field %s not found", field)
}
}
}
// Check if the field is exported (CanInterface returns false for unexported fields)
if !v.CanInterface() {
return reflect.Value{}, fmt.Errorf("field %s is unexported and cannot be accessed", field)
}
// Only dereference if this is not the last field in the path
if v.Kind() == reflect.Ptr && !v.IsNil() && i < len(fields)-1 {
v = v.Elem()
}
}
return v, nil
}
// LoadPolicy reads a policy from a JSON file and returns a Policy struct.
// If the file cannot be read or the JSON is invalid, an error is returned.
//
// Parameters:
// - policyFile: A string representing the path to the policy JSON file.
//
// Return:
// - *Policy: A pointer to a Policy struct representing the loaded policy.
// - error: An error if the file cannot be read or the JSON is invalid.
func LoadPolicy(policyFile string) (*Policy, error) {
var (
err error
policyString []byte
policy = &Policy{}
)
// Can't read the file
if policyString, err = os.ReadFile(policyFile); err != nil {
return nil, fmt.Errorf("failed to read policy file: %v", err)
}
// Invalid policy JSON
if err = json.Unmarshal(policyString, policy); err != nil {
return nil, fmt.Errorf("invalid policy json: %v", err)
}
return policy, nil
}
// getPolicyCheckOperator retrieves a PolicyCheckOperator function based on the given operator string.
// It uses a map of predefined operators to find the corresponding function.
//
// Parameters:
// - operator: A string representing the operator to retrieve the function for.
//
// Return:
// - PolicyCheckOperator[any]: A function that performs the policy check operation.
// - error: An error if the operator does not exist in the predefined map.
func getPolicyCheckOperator(operator string) (PolicyCheckOperator[any], error) {
if fn, ok := policyCheckOperatorMap[operator]; ok {
return fn, nil
}
return nil, fmt.Errorf("operator %s does not exist", operator)
}