policy: comment out "filtering" of policies, concept that doesn't really work

This commit is contained in:
Michael Muré
2024-10-22 16:27:28 +02:00
parent 5abb870462
commit 570bcdcb6c
2 changed files with 175 additions and 176 deletions

View File

@@ -20,19 +20,19 @@ func (p Policy) Match(node datamodel.Node) bool {
return true
}
// Filter performs a recursive filtering of the Statement, and prunes what doesn't match the given path
func (p Policy) Filter(path ...string) Policy {
var filtered Policy
for _, stmt := range p {
newChild, remain := filter(stmt, path)
if newChild != nil && len(remain) == 0 {
filtered = append(filtered, newChild)
}
}
return filtered
}
// // Filter performs a recursive filtering of the Statement, and prunes what doesn't match the given path
// func (p Policy) Filter(path ...string) Policy {
// var filtered Policy
//
// for _, stmt := range p {
// newChild, remain := filter(stmt, path)
// if newChild != nil && len(remain) == 0 {
// filtered = append(filtered, newChild)
// }
// }
//
// return filtered
// }
func matchStatement(statement Statement, node ipld.Node) bool {
switch statement.Kind() {
@@ -163,69 +163,69 @@ func matchStatement(statement Statement, node ipld.Node) bool {
panic(fmt.Errorf("unimplemented statement kind: %s", statement.Kind()))
}
// filter performs a recursive filtering of the Statement, and prunes what doesn't match the given path
func filter(stmt Statement, path []string) (Statement, []string) {
// For each kind, we do some of the following if it applies:
// - test the path against the selector, consuming segments
// - for terminal statements (equality, wildcard), require all the segments to have been consumed
// - recursively filter child (negation, quantifier) or children (connective) statements with the remaining path
switch stmt.(type) {
case equality:
match, remain := stmt.(equality).selector.MatchPath(path...)
if match && len(remain) == 0 {
return stmt, remain
}
return nil, nil
case negation:
newChild, remain := filter(stmt.(negation).statement, path)
if newChild != nil && len(remain) == 0 {
return negation{
statement: newChild,
}, nil
}
return nil, nil
case connective:
var newChildren []Statement
for _, child := range stmt.(connective).statements {
newChild, remain := filter(child, path)
if newChild != nil && len(remain) == 0 {
newChildren = append(newChildren, newChild)
}
}
if len(newChildren) == 0 {
return nil, nil
}
return connective{
kind: stmt.(connective).kind,
statements: newChildren,
}, nil
case wildcard:
match, remain := stmt.(wildcard).selector.MatchPath(path...)
if match && len(remain) == 0 {
return stmt, remain
}
return nil, nil
case quantifier:
match, remain := stmt.(quantifier).selector.MatchPath(path...)
if match && len(remain) == 0 {
return stmt, remain
}
if !match {
return nil, nil
}
newChild, remain := filter(stmt.(quantifier).statement, remain)
if newChild != nil && len(remain) == 0 {
return quantifier{
kind: stmt.(quantifier).kind,
selector: stmt.(quantifier).selector,
statement: newChild,
}, nil
}
return nil, nil
default:
panic(fmt.Errorf("unimplemented statement kind: %s", stmt.Kind()))
}
}
// // filter performs a recursive filtering of the Statement, and prunes what doesn't match the given path
// func filter(stmt Statement, path []string) (Statement, []string) {
// // For each kind, we do some of the following if it applies:
// // - test the path against the selector, consuming segments
// // - for terminal statements (equality, wildcard), require all the segments to have been consumed
// // - recursively filter child (negation, quantifier) or children (connective) statements with the remaining path
// switch stmt.(type) {
// case equality:
// match, remain := stmt.(equality).selector.MatchPath(path...)
// if match && len(remain) == 0 {
// return stmt, remain
// }
// return nil, nil
// case negation:
// newChild, remain := filter(stmt.(negation).statement, path)
// if newChild != nil && len(remain) == 0 {
// return negation{
// statement: newChild,
// }, nil
// }
// return nil, nil
// case connective:
// var newChildren []Statement
// for _, child := range stmt.(connective).statements {
// newChild, remain := filter(child, path)
// if newChild != nil && len(remain) == 0 {
// newChildren = append(newChildren, newChild)
// }
// }
// if len(newChildren) == 0 {
// return nil, nil
// }
// return connective{
// kind: stmt.(connective).kind,
// statements: newChildren,
// }, nil
// case wildcard:
// match, remain := stmt.(wildcard).selector.MatchPath(path...)
// if match && len(remain) == 0 {
// return stmt, remain
// }
// return nil, nil
// case quantifier:
// match, remain := stmt.(quantifier).selector.MatchPath(path...)
// if match && len(remain) == 0 {
// return stmt, remain
// }
// if !match {
// return nil, nil
// }
// newChild, remain := filter(stmt.(quantifier).statement, remain)
// if newChild != nil && len(remain) == 0 {
// return quantifier{
// kind: stmt.(quantifier).kind,
// selector: stmt.(quantifier).selector,
// statement: newChild,
// }, nil
// }
// return nil, nil
// default:
// panic(fmt.Errorf("unimplemented statement kind: %s", stmt.Kind()))
// }
// }
func isOrdered(expected ipld.Node, actual ipld.Node, satisfies func(order int) bool) bool {
if expected.Kind() == ipld.Kind_Int && actual.Kind() == ipld.Kind_Int {

View File

@@ -2,7 +2,6 @@ package policy
import (
"fmt"
"strings"
"testing"
"github.com/ipfs/go-cid"
@@ -514,102 +513,102 @@ func FuzzMatch(f *testing.F) {
})
}
func TestPolicyFilter(t *testing.T) {
pol := MustConstruct(
Any(".http", And(
Equal(".method", literal.String("GET")),
Equal(".path", literal.String("/foo")),
)),
Equal(".http", literal.String("foobar")),
All(".jsonrpc.foo", Or(
Not(Equal(".bar", literal.String("foo"))),
Equal(".", literal.String("foo")),
Like(".boo", "abcd"),
Like(".boo", "*bcd"),
)),
)
for _, tc := range []struct {
path string
expected Policy
}{
{
path: "http",
expected: MustConstruct(
Any(".http", And(
Equal(".method", literal.String("GET")),
Equal(".path", literal.String("/foo")),
)),
Equal(".http", literal.String("foobar")),
),
},
{
path: "http,method",
expected: MustConstruct(
Any(".http", And(
Equal(".method", literal.String("GET")),
)),
),
},
{
path: "http,path",
expected: MustConstruct(
Any(".http", And(
Equal(".path", literal.String("/foo")),
)),
),
},
{
path: "http,foo",
expected: Policy{},
},
{
path: "jsonrpc",
expected: MustConstruct(
All(".jsonrpc.foo", Or(
Not(Equal(".bar", literal.String("foo"))),
Equal(".", literal.String("foo")),
Like(".boo", "abcd"),
Like(".boo", "*bcd"),
)),
),
},
{
path: "jsonrpc,baz",
expected: Policy{},
},
{
path: "jsonrpc,foo",
expected: MustConstruct(
All(".jsonrpc.foo", Or(
Not(Equal(".bar", literal.String("foo"))),
Equal(".", literal.String("foo")),
Like(".boo", "abcd"),
Like(".boo", "*bcd"),
)),
),
},
{
path: "jsonrpc,foo,bar",
expected: MustConstruct(
All(".jsonrpc.foo", Or(
Not(Equal(".bar", literal.String("foo"))),
)),
),
},
{
path: "jsonrpc,foo,boo",
expected: MustConstruct(
All(".jsonrpc.foo", Or(
Like(".boo", "abcd"),
Like(".boo", "*bcd"),
)),
),
},
} {
t.Run(tc.path, func(t *testing.T) {
res := pol.Filter(strings.Split(tc.path, ",")...)
require.Equal(t, tc.expected.String(), res.String())
})
}
}
// func TestPolicyFilter(t *testing.T) {
// pol := MustConstruct(
// Any(".http", And(
// Equal(".method", literal.String("GET")),
// Equal(".path", literal.String("/foo")),
// )),
// Equal(".http", literal.String("foobar")),
// All(".jsonrpc.foo", Or(
// Not(Equal(".bar", literal.String("foo"))),
// Equal(".", literal.String("foo")),
// Like(".boo", "abcd"),
// Like(".boo", "*bcd"),
// )),
// )
//
// for _, tc := range []struct {
// path string
// expected Policy
// }{
// {
// path: "http",
// expected: MustConstruct(
// Any(".http", And(
// Equal(".method", literal.String("GET")),
// Equal(".path", literal.String("/foo")),
// )),
// Equal(".http", literal.String("foobar")),
// ),
// },
// {
// path: "http,method",
// expected: MustConstruct(
// Any(".http", And(
// Equal(".method", literal.String("GET")),
// )),
// ),
// },
// {
// path: "http,path",
// expected: MustConstruct(
// Any(".http", And(
// Equal(".path", literal.String("/foo")),
// )),
// ),
// },
// {
// path: "http,foo",
// expected: Policy{},
// },
// {
// path: "jsonrpc",
// expected: MustConstruct(
// All(".jsonrpc.foo", Or(
// Not(Equal(".bar", literal.String("foo"))),
// Equal(".", literal.String("foo")),
// Like(".boo", "abcd"),
// Like(".boo", "*bcd"),
// )),
// ),
// },
// {
// path: "jsonrpc,baz",
// expected: Policy{},
// },
// {
// path: "jsonrpc,foo",
// expected: MustConstruct(
// All(".jsonrpc.foo", Or(
// Not(Equal(".bar", literal.String("foo"))),
// Equal(".", literal.String("foo")),
// Like(".boo", "abcd"),
// Like(".boo", "*bcd"),
// )),
// ),
// },
// {
// path: "jsonrpc,foo,bar",
// expected: MustConstruct(
// All(".jsonrpc.foo", Or(
// Not(Equal(".bar", literal.String("foo"))),
// )),
// ),
// },
// {
// path: "jsonrpc,foo,boo",
// expected: MustConstruct(
// All(".jsonrpc.foo", Or(
// Like(".boo", "abcd"),
// Like(".boo", "*bcd"),
// )),
// ),
// },
// } {
// t.Run(tc.path, func(t *testing.T) {
// res := pol.Filter(strings.Split(tc.path, ",")...)
// require.Equal(t, tc.expected.String(), res.String())
// })
// }
// }