compile: Support periods in decision paths

This commit updates the internal/ref package to support periods in
decision paths. This allows the opa build command to specify
entrypoints with periods in them (eg., foo/bar.baz/qux). This change
also improves the decision logger and HTTP server that rely on
internal/ref to parse mask, authorization, and default decision paths (respectively).

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
This commit is contained in:
Torin Sandall
2020-11-03 16:07:31 -05:00
parent f44a10aa8a
commit ee7f12357c
3 changed files with 11 additions and 16 deletions
-4
View File
@@ -247,10 +247,6 @@ func (c *Compiler) init() error {
return fmt.Errorf("entrypoint %v not valid: use <package>/<rule>", e)
}
if len(r) <= 2 {
return fmt.Errorf("entrypoint %v too short: use <package>/<rule>", e)
}
c.entrypointrefs = append(c.entrypointrefs, ast.NewTerm(r))
}
-10
View File
@@ -43,16 +43,6 @@ func TestCompilerInitErrors(t *testing.T) {
c: New().WithTarget("deadbeef"),
want: fmt.Errorf("invalid target \"deadbeef\""),
},
{
note: "entrypoint parse error",
c: New().WithEntrypoints("foo%bar"),
want: fmt.Errorf("entrypoint foo%%bar not valid: use <package>/<rule>"),
},
{
note: "entrypoint too short error",
c: New().WithEntrypoints("foo"),
want: fmt.Errorf("entrypoint foo too short: use <package>/<rule>"),
},
{
note: "optimizations require entrypoint",
c: New().WithOptimizationLevel(1),
+11 -2
View File
@@ -6,14 +6,23 @@
package ref
import (
"errors"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/storage"
)
// ParseDataPath returns a ref from the slash separated path s rooted at data.
// All path segments are treated as identifier strings.
func ParseDataPath(s string) (ast.Ref, error) {
s = strings.Replace(strings.Trim(s, "/"), "/", ".", -1)
return ast.ParseRef("data." + s)
s = "/" + strings.TrimPrefix(s, "/")
path, ok := storage.ParsePath(s)
if !ok {
return nil, errors.New("invalid path")
}
return path.Ref(ast.DefaultRootDocument), nil
}