2021-09-13 07:39:38 +00:00
|
|
|
package eacl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-05-18 11:46:28 +00:00
|
|
|
func checkAction(t *testing.T, expected Action, v *Validator, vu *ValidationUnit) {
|
|
|
|
action, ok := v.CalculateAction(vu)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, expected, action)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDefaultAction(t *testing.T, v *Validator, vu *ValidationUnit) {
|
|
|
|
action, ok := v.CalculateAction(vu)
|
|
|
|
require.False(t, ok)
|
|
|
|
require.Equal(t, ActionAllow, action)
|
|
|
|
}
|
|
|
|
|
2021-09-13 09:30:34 +00:00
|
|
|
func TestFilterMatch(t *testing.T) {
|
2022-03-11 09:02:53 +00:00
|
|
|
tgt := *NewTarget()
|
2021-09-13 09:30:34 +00:00
|
|
|
tgt.SetRole(RoleOthers)
|
|
|
|
|
|
|
|
t.Run("simple header match", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
|
|
|
|
r := newRecord(ActionDeny, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(HeaderFromObject, MatchStringEqual, "a", "xxx")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
|
|
|
|
r = newRecord(ActionDeny, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(HeaderFromRequest, MatchStringNotEqual, "b", "yyy")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
|
|
|
|
tb.AddRecord(newRecord(ActionAllow, OperationUnknown, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 09:30:34 +00:00
|
|
|
hs := headers{}
|
|
|
|
vu.hdrSrc = &hs
|
|
|
|
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = makeHeaders("b", "yyy")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = makeHeaders("a", "xxx")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = nil
|
|
|
|
hs.req = makeHeaders("b", "yyy")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.req = makeHeaders("b", "abc")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("all filters must match", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
r := newRecord(ActionDeny, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(HeaderFromObject, MatchStringEqual, "a", "xxx")
|
|
|
|
r.AddFilter(HeaderFromRequest, MatchStringEqual, "b", "yyy")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
tb.AddRecord(newRecord(ActionAllow, OperationUnknown, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 09:30:34 +00:00
|
|
|
hs := headers{}
|
|
|
|
vu.hdrSrc = &hs
|
|
|
|
|
|
|
|
hs.obj = makeHeaders("a", "xxx")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.req = makeHeaders("b", "yyy")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = nil
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("filters with unknown type are skipped", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
r := newRecord(ActionDeny, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(HeaderTypeUnknown, MatchStringEqual, "a", "xxx")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
|
|
|
|
r = newRecord(ActionDeny, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(0xFF, MatchStringEqual, "b", "yyy")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
|
|
|
|
tb.AddRecord(newRecord(ActionDeny, OperationUnknown, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 09:30:34 +00:00
|
|
|
hs := headers{}
|
|
|
|
vu.hdrSrc = &hs
|
|
|
|
|
2022-05-18 11:46:28 +00:00
|
|
|
checkDefaultAction(t, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = makeHeaders("a", "xxx")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkDefaultAction(t, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = nil
|
|
|
|
hs.req = makeHeaders("b", "yyy")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkDefaultAction(t, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("filters with match function are skipped", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
r := newRecord(ActionAllow, OperationUnknown, tgt)
|
|
|
|
r.AddFilter(HeaderFromObject, 0xFF, "a", "xxx")
|
|
|
|
tb.AddRecord(r)
|
|
|
|
tb.AddRecord(newRecord(ActionDeny, OperationUnknown, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 09:30:34 +00:00
|
|
|
hs := headers{}
|
|
|
|
vu.hdrSrc = &hs
|
|
|
|
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
|
|
|
|
hs.obj = makeHeaders("a", "xxx")
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 09:30:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-13 08:27:33 +00:00
|
|
|
func TestOperationMatch(t *testing.T) {
|
2022-03-11 09:02:53 +00:00
|
|
|
tgt := *NewTarget()
|
2021-09-13 08:27:33 +00:00
|
|
|
tgt.SetRole(RoleOthers)
|
|
|
|
|
|
|
|
t.Run("single operation", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
tb.AddRecord(newRecord(ActionDeny, OperationPut, tgt))
|
|
|
|
tb.AddRecord(newRecord(ActionAllow, OperationGet, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 08:27:33 +00:00
|
|
|
|
|
|
|
vu.op = OperationPut
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionDeny, v, vu)
|
2021-09-13 08:27:33 +00:00
|
|
|
|
|
|
|
vu.op = OperationGet
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 08:27:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown operation", func(t *testing.T) {
|
|
|
|
tb := NewTable()
|
|
|
|
tb.AddRecord(newRecord(ActionDeny, OperationUnknown, tgt))
|
|
|
|
tb.AddRecord(newRecord(ActionAllow, OperationGet, tgt))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
v := NewValidator()
|
|
|
|
vu := newValidationUnit(RoleOthers, nil, tb)
|
2021-09-13 08:27:33 +00:00
|
|
|
|
|
|
|
// TODO discuss if both next tests should result in DENY
|
|
|
|
vu.op = OperationPut
|
2022-05-18 11:46:28 +00:00
|
|
|
checkDefaultAction(t, v, vu)
|
2021-09-13 08:27:33 +00:00
|
|
|
|
|
|
|
vu.op = OperationGet
|
2022-05-18 11:46:28 +00:00
|
|
|
checkAction(t, ActionAllow, v, vu)
|
2021-09-13 08:27:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-13 07:39:38 +00:00
|
|
|
func TestTargetMatches(t *testing.T) {
|
|
|
|
pubs := makeKeys(t, 3)
|
|
|
|
|
|
|
|
tgt1 := NewTarget()
|
|
|
|
tgt1.SetBinaryKeys(pubs[0:2])
|
|
|
|
tgt1.SetRole(RoleUser)
|
|
|
|
|
|
|
|
tgt2 := NewTarget()
|
|
|
|
tgt2.SetRole(RoleOthers)
|
|
|
|
|
|
|
|
r := NewRecord()
|
2022-03-11 09:02:53 +00:00
|
|
|
r.SetTargets(*tgt1, *tgt2)
|
2021-09-13 07:39:38 +00:00
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
u := newValidationUnit(RoleUser, pubs[0], nil)
|
2021-09-13 07:39:38 +00:00
|
|
|
require.True(t, targetMatches(u, r))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
u = newValidationUnit(RoleUser, pubs[2], nil)
|
2021-09-13 07:39:38 +00:00
|
|
|
require.False(t, targetMatches(u, r))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
u = newValidationUnit(RoleUnknown, pubs[1], nil)
|
2021-09-13 07:39:38 +00:00
|
|
|
require.True(t, targetMatches(u, r))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
u = newValidationUnit(RoleOthers, pubs[2], nil)
|
2021-09-13 07:39:38 +00:00
|
|
|
require.True(t, targetMatches(u, r))
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
u = newValidationUnit(RoleSystem, pubs[2], nil)
|
2021-09-13 07:39:38 +00:00
|
|
|
require.False(t, targetMatches(u, r))
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeKeys(t *testing.T, n int) [][]byte {
|
|
|
|
pubs := make([][]byte, n)
|
|
|
|
for i := range pubs {
|
|
|
|
pubs[i] = make([]byte, 33)
|
|
|
|
pubs[i][0] = 0x02
|
|
|
|
|
|
|
|
_, err := rand.Read(pubs[i][1:])
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
return pubs
|
|
|
|
}
|
|
|
|
|
2021-09-13 09:30:34 +00:00
|
|
|
type (
|
|
|
|
hdr struct {
|
|
|
|
key, value string
|
|
|
|
}
|
|
|
|
|
|
|
|
headers struct {
|
|
|
|
obj []Header
|
|
|
|
req []Header
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h hdr) Key() string { return h.key }
|
|
|
|
func (h hdr) Value() string { return h.value }
|
|
|
|
|
|
|
|
func makeHeaders(kv ...string) []Header {
|
|
|
|
hs := make([]Header, len(kv)/2)
|
|
|
|
for i := 0; i < len(kv); i += 2 {
|
|
|
|
hs[i/2] = hdr{kv[i], kv[i+1]}
|
|
|
|
}
|
|
|
|
return hs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h headers) HeadersOfType(ht FilterHeaderType) ([]Header, bool) {
|
|
|
|
switch ht {
|
|
|
|
case HeaderFromRequest:
|
|
|
|
return h.req, true
|
|
|
|
case HeaderFromObject:
|
|
|
|
return h.obj, true
|
|
|
|
default:
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:02:53 +00:00
|
|
|
func newRecord(a Action, op Operation, tgt ...Target) *Record {
|
2021-09-13 08:27:33 +00:00
|
|
|
r := NewRecord()
|
|
|
|
r.SetAction(a)
|
|
|
|
r.SetOperation(op)
|
|
|
|
r.SetTargets(tgt...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-10-05 12:24:04 +00:00
|
|
|
func newValidationUnit(role Role, key []byte, table *Table) *ValidationUnit {
|
|
|
|
return new(ValidationUnit).
|
|
|
|
WithRole(role).
|
|
|
|
WithSenderKey(key).
|
|
|
|
WithEACLTable(table)
|
2021-09-13 07:39:38 +00:00
|
|
|
}
|