From a8780f750cb50e324d4ed6f315f4a9df2ab1c270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 24 Oct 2024 11:17:38 +0200 Subject: [PATCH] policy: remove remnant of policy matching, that concept doesn't really work with complex policies Maybe it will be revived later. --- pkg/policy/match.go | 78 ------------------------------ pkg/policy/match_test.go | 100 --------------------------------------- 2 files changed, 178 deletions(-) diff --git a/pkg/policy/match.go b/pkg/policy/match.go index 8ae254a..308229d 100644 --- a/pkg/policy/match.go +++ b/pkg/policy/match.go @@ -20,20 +20,6 @@ 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 -// } - func matchStatement(statement Statement, node ipld.Node) bool { switch statement.Kind() { case KindEqual: @@ -163,70 +149,6 @@ 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())) -// } -// } - 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 { a := must.Int(actual) diff --git a/pkg/policy/match_test.go b/pkg/policy/match_test.go index 6118754..9e3de4a 100644 --- a/pkg/policy/match_test.go +++ b/pkg/policy/match_test.go @@ -512,103 +512,3 @@ func FuzzMatch(f *testing.F) { policy.Match(dataNode) }) } - -// 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()) -// }) -// } -// }