3

It's not clear to me how to run the Simple Example from the Open Policy Agent Playground from the CLI.

play.rego

package play
default hello = false

hello {
    m := input.message
    m == "world"
}

input.json

{
    "message": "world"
}

I tried using:

opa eval -i input.json -d play.rego "data.play.hello"

But, I could not get this result

{
    "hello": true
}

Unfortunately, I got this: :(

{
  "result": [
    {
      "expressions": [
        {
          "value": true,
          "text": "data.play.hello",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

I thought others might find it useful to understand how to run these example from the CLI, so I had to ask.

user284503
  • 368
  • 3
  • 11
  • 23

1 Answers1

3

That's a great question! Unless "evaluate selection" is selected, the Rego Playground always evaluates the entire policy, i.e. all rules included. When you query a policy using opa eval you can either choose to do the same, or as you do in your example - query just a single rule for its value.

If you change the query from "data.play.hello" to just "data.play" it will evaluate the full policy just like the playground:

$ opa eval -i input.json -d play.rego "data.play"
{
  "result": [
    {
      "expressions": [
        {
          "value": {
            "hello": true
          },
          "text": "data.play",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

If you only want to display the actual output without all the details around it, you could use one of the formatting options available for opa eval such as --format raw:

$ opa eval --format raw -i input.json -d play.rego "data.play"
{"hello":true}
Devoops
  • 2,018
  • 8
  • 21
  • 1
    Thanks. It turns out --format raw is not supported, but --format pretty works ! This worked for me [opa eval --format pretty -i input.json -d play.rego "data.play"]. I put up a github repo if anyone might want to try this. https://github.com/pmcdowell-okta/opa-example – user284503 May 12 '21 at 17:49
  • Ah, yeah, the raw output format was added in a recent version of OPA. – Devoops May 13 '21 at 05:40