opa icon indicating copy to clipboard operation
opa copied to clipboard

`opa build` panics when rule is named `or`

Open anbrsap opened this issue 2 years ago • 5 comments

Expected Behavior

The following rego code should compile, run and opa build.

testcase.rego:

package testcase

# rule name `or` seems to trigger the issue
or(param) {
    true # real impl omitted
}

use_or := value {
    value := [
        # opa build crashes on next line
        or("dummy")
    ]
}

Actual Behavior

opa build panics:

$ opa build testcase.rego 
panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
github.com/open-policy-agent/opa/format.(*writer).writeCall(0xc000100ea0, 0x0, {0xc000163320, 0x2, 0x2}, {0x0, 0x0, 0x0})
	/src/format/format.go:611 +0x287
github.com/open-policy-agent/opa/format.(*writer).writeTermParens(0xc000100ea0, 0x20, 0xc000169c98, {0xc00000e618, 0xc000045a80, 0xc00029ef88})
	/src/format/format.go:548 +0x405
github.com/open-policy-agent/opa/format.(*writer).writeTerm(0x203000, 0xcc3700, {0xc00000e618, 0x1, 0x1})
	/src/format/format.go:513 +0x2c
github.com/open-policy-agent/opa/format.(*writer).listWriter.func1({0xcc3700, 0xc000169c98}, {0xc00000e618, 0xc000045800, 0xc000163460})
	/src/format/format.go:809 +0x3b
github.com/open-policy-agent/opa/format.(*writer).writeIterableLine(0xc000100ea0, {0xc000163460, 0x1, 0x7fbf384f8108}, {0xc00000e618, 0x13eb120, 0xc000163450}, 0xc000163450)
	/src/format/format.go:795 +0x10d
github.com/open-policy-agent/opa/format.(*writer).writeIterable(0xc000100ea0, {0xc000163440, 0x1, 0x7fbf38501328}, 0x2, 0x0, {0xc00000e618, 0x1, 0x1}, 0xc000163450)
	/src/format/format.go:771 +0x1a6
github.com/open-policy-agent/opa/format.(*writer).writeArray(0xc000100ea0, 0xe3fad8, 0xc000164c00, {0xc00000e618, 0x1, 0x1})
	/src/format/format.go:638 +0x1ba
github.com/open-policy-agent/opa/format.(*writer).writeTermParens(0xc000100ea0, 0xa8, 0xc000169c80, {0xc00000e610, 0x0, 0xe188c0})
	/src/format/format.go:528 +0x17f
github.com/open-policy-agent/opa/format.(*writer).writeTerm(...)
	/src/format/format.go:513
github.com/open-policy-agent/opa/format.(*writer).writeFunctionCall(0xc000100ea0, 0xc00000e600, {0xc00000e608, 0x1, 0x1})
	/src/format/format.go:477 +0x1c8
github.com/open-policy-agent/opa/format.(*writer).writeExpr(0xc000100ea0, 0xc0000459c0, {0xc00000e600, 0xc000169e90, 0xc0000455c0})
	/src/format/format.go:411 +0x12d
github.com/open-policy-agent/opa/format.(*writer).writeBody(0xc000100ea0, {0xc00000e578, 0x1, 0xc00000e5f0}, {0xc00000e5f8, 0x1, 0x86db1c})
	/src/format/format.go:391 +0x7c
github.com/open-policy-agent/opa/format.(*writer).writeRule(0xc000100ea0, 0xc000045980, 0x0, {0xc00000e5f0, 0x1, 0x1})
	/src/format/format.go:259 +0x2da
github.com/open-policy-agent/opa/format.(*writer).writeRules(0xc000164c40, {0xc0001633d0, 0x2, 0x0}, {0xc000164ce0, 0x0, 0xc00029f5f8})
	/src/format/format.go:223 +0x72
github.com/open-policy-agent/opa/format.(*writer).writeModule(0xc000100ea0, 0xc000152230)
	/src/format/format.go:190 +0x2d3
github.com/open-policy-agent/opa/format.Ast({0xc75320, 0xc0001521c0})
	/src/format/format.go:74 +0x193
github.com/open-policy-agent/opa/format.Source({0x7ffcac56e2bb, 0x16}, {0xc000144200, 0xffffffffffffffff, 0x4a2c91})
	/src/format/format.go:25 +0x7d
github.com/open-policy-agent/opa/bundle.(*Bundle).FormatModules(0xc00011c840, 0x0)
	/src/bundle/bundle.go:778 +0xeb
github.com/open-policy-agent/opa/compile.(*Compiler).Build(0xc00029fb98, {0xe3a790, 0xc0001aa000})
	/src/compile/compile.go:200 +0x11f
github.com/open-policy-agent/opa/cmd.dobuild({0xc00000c180, 0xc0000303f0, 0x0, 0x0, {{0x0, 0x0, 0x0}, 0x0}, {0xd0f294, 0xd}, ...}, ...)
	/src/cmd/build.go:293 +0x70d
github.com/open-policy-agent/opa/cmd.init.1.func2(0xc000114280, {0xc00007a570, 0x1, 0x1})
	/src/cmd/build.go:210 +0x6c
github.com/spf13/cobra.(*Command).execute(0xc000114280, {0xc00007a540, 0x1, 0x1})
	/src/vendor/github.com/spf13/cobra/command.go:860 +0x5f8
github.com/spf13/cobra.(*Command).ExecuteC(0x13ddb80)
	/src/vendor/github.com/spf13/cobra/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
	/src/vendor/github.com/spf13/cobra/command.go:902
main.main()
	/src/main.go:15 +0x25

However, the code compiles and runs:

$ opa eval -d testcase.rego 'data.testcase.use_or'
{
  "result": [
    {
      "expressions": [
        {
          "value": [
            true
          ],
          "text": "data.testcase.use_or",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

Steps to Reproduce the Problem

Save testcase.rego to disk and run the commands shown above.

  • OPA version: 0.32.1

Additional Info

anbrsap avatar Sep 29 '21 15:09 anbrsap

or is the prefix name of the built-in function that backs s0 | s1 set union, IIRC. and backs set intersection and would behave similarly.

srenatus avatar Sep 29 '21 18:09 srenatus

The issue seems to be when we format the Rego module, the writer thinks the user defined function call or is the set union builtin function (|) and hence attempts to format it as a builtin which results in a crash.

ashutosh-narkar avatar Sep 29 '21 18:09 ashutosh-narkar

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days.

stale[bot] avatar Nov 22 '21 18:11 stale[bot]

#4912 seems to match this, too.

srenatus avatar Jul 20 '22 13:07 srenatus

This issue has been automatically marked as inactive because it has not had any activity in the last 30 days.

stale[bot] avatar Aug 30 '22 22:08 stale[bot]

Running this against OPA v0.58.0, compilation fails with:

$ opa build p.rego
error: p.rego: 1 error occurred: p.rego:11: rego_type_error: or: arity mismatch
	have: (string)
	want: (set[any],set[any])

Whether accidentally "fixed" or not, I think failing on overriding an operator like or is correct. Regal would be a good option for getting notified of accidentally shadowed built-in functions.

anderseknert avatar Nov 08 '23 21:11 anderseknert

Isn't there also an issue here that build behaves differently from eval?

johanfylling avatar Nov 09 '23 10:11 johanfylling