Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "context"
- "fmt"
- "github.com/open-policy-agent/opa/v1/rego"
- _ "github.com/open-policy-agent/opa/v1/features/wasm"
- )
- func main() {
- ctx := context.Background()
- httpSendRegoPolicy(ctx)
- }
- func httpSendRegoPolicy(ctx context.Context) {
- fmt.Println("\nRunning http send Rego policy:\n")
- mod := `
- package example
- result contains response if {
- request := {
- "method": "GET",
- "url": "https://httpbin.org/get",
- "headers": {"Accept": "application/json"},
- }
- response := http.send(request)
- }
- `
- pq, err := rego.New(
- rego.Query("data.example.result"),
- rego.Target("wasm"),
- rego.Module("policy.rego", mod),
- ).PrepareForEval(ctx)
- if err != nil {
- fmt.Errorf("Unexpected error: %s", err)
- }
- input := map[string]any{}
- fmt.Printf("Input: %+v\n", input)
- assertPreparedEvalQueryEval(pq, []rego.EvalOption{
- rego.EvalInput(input),
- })
- }
- func assertPreparedEvalQueryEval(pq rego.PreparedEvalQuery, options []rego.EvalOption) {
- rs, err := pq.Eval(context.Background(), options...)
- if err != nil {
- fmt.Errorf("Unexpected error: %s", err)
- }
- fmt.Printf("Result: %+v\n\n", rs)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement