| 124 | } |
| 125 | |
| 126 | func (r *Engine) Verify(ctx context.Context, policy *engine.Policy, input []byte, args map[string]any) (*engine.EvaluationResult, error) { |
| 127 | policyString := string(policy.Source) |
| 128 | parsedModule, err := ast.ParseModule(policy.Name, policyString) |
| 129 | if err != nil { |
| 130 | return nil, fmt.Errorf("failed to parse rego policy: %w", err) |
| 131 | } |
| 132 | |
| 133 | // Decode input as json |
| 134 | decoder := json.NewDecoder(bytes.NewReader(input)) |
| 135 | decoder.UseNumber() |
| 136 | var decodedInput interface{} |
| 137 | if err := decoder.Decode(&decodedInput); err != nil { |
| 138 | return nil, fmt.Errorf("failed to parse input: %w", err) |
| 139 | } |
| 140 | |
| 141 | // if input is an array, transform it to an object |
| 142 | if array, ok := decodedInput.([]interface{}); ok { |
| 143 | inputMap := make(map[string]interface{}) |
| 144 | inputMap[inputElements] = array |
| 145 | decodedInput = inputMap |
| 146 | } |
| 147 | |
| 148 | if inputMap, ok := decodedInput.(map[string]interface{}); ok { |
| 149 | decodedInput = r.injectProjectMetadata(inputMap) |
| 150 | } |
| 151 | |
| 152 | // put arguments embedded in the input object |
| 153 | if args != nil { |
| 154 | inputMap, ok := decodedInput.(map[string]interface{}) |
| 155 | if !ok { |
| 156 | return nil, fmt.Errorf("unexpected input arguments") |
| 157 | } |
| 158 | inputMap[inputArgs] = args |
| 159 | decodedInput = inputMap |
| 160 | } |
| 161 | |
| 162 | // add input |
| 163 | regoInput := rego.Input(decodedInput) |
| 164 | |
| 165 | // add module |
| 166 | regoFunc := rego.ParsedModule(parsedModule) |
| 167 | |
| 168 | var res rego.ResultSet |
| 169 | // Function to execute the query with appropriate parameters |
| 170 | executeQuery := func(rule string, strict bool) error { |
| 171 | options := []func(r *rego.Rego){regoInput, regoFunc, rego.Capabilities(r.Capabilities())} |
| 172 | |
| 173 | // Add print support if enabled |
| 174 | if r.EnablePrint { |
| 175 | options = append(options, |
| 176 | rego.EnablePrintStatements(true), |
| 177 | rego.PrintHook(®oOutputHook{}), |
| 178 | ) |
| 179 | } |
| 180 | |
| 181 | if strict { |
| 182 | options = append(options, rego.StrictBuiltinErrors(true)) |
| 183 | } |