Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "context"
  5. "fmt"
  6.  
  7. "github.com/open-policy-agent/opa/v1/rego"
  8. _ "github.com/open-policy-agent/opa/v1/features/wasm"
  9. )
  10.  
  11. func main() {
  12. ctx := context.Background()
  13. httpSendRegoPolicy(ctx)
  14. }
  15.  
  16. func httpSendRegoPolicy(ctx context.Context) {
  17. fmt.Println("\nRunning http send Rego policy:\n")
  18.  
  19. mod := `
  20. package example
  21.  
  22. result contains response if {
  23. request := {
  24. "method": "GET",
  25. "url": "https://httpbin.org/get",
  26. "headers": {"Accept": "application/json"},
  27. }
  28.  
  29. response := http.send(request)
  30. }
  31. `
  32.  
  33. pq, err := rego.New(
  34. rego.Query("data.example.result"),
  35. rego.Target("wasm"),
  36. rego.Module("policy.rego", mod),
  37. ).PrepareForEval(ctx)
  38.  
  39. if err != nil {
  40. fmt.Errorf("Unexpected error: %s", err)
  41. }
  42.  
  43. input := map[string]any{}
  44. fmt.Printf("Input: %+v\n", input)
  45. assertPreparedEvalQueryEval(pq, []rego.EvalOption{
  46. rego.EvalInput(input),
  47. })
  48. }
  49.  
  50. func assertPreparedEvalQueryEval(pq rego.PreparedEvalQuery, options []rego.EvalOption) {
  51. rs, err := pq.Eval(context.Background(), options...)
  52. if err != nil {
  53. fmt.Errorf("Unexpected error: %s", err)
  54. }
  55.  
  56. fmt.Printf("Result: %+v\n\n", rs)
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement