mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user