Skip to content

Commit

Permalink
checking for extra entries in an ACL
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuetler committed Apr 18, 2024
1 parent e5b1f72 commit a7c922c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
21 changes: 20 additions & 1 deletion private/path/pathpol/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
var (
// ErrNoDefault indicates that there is no default acl entry.
ErrNoDefault = errors.New("ACL does not have a default")
// ErrExtraEntries indicates that there extra entries after the default entry.
ErrExtraEntries = errors.New("ACL has unused extra entries after a default entry")
)

type ACL struct {
Expand Down Expand Up @@ -97,9 +99,26 @@ func (a *ACL) evalInterface(iface snet.PathInterface, ingress bool) ACLAction {
}

func validateACL(entries []*ACLEntry) error {
if len(entries) == 0 || !entries[len(entries)-1].Rule.matchesAll() {
if len(entries) == 0 {
return ErrNoDefault
}

foundAt := -1
for i, e := range entries {
if e.Rule.matchesAll() {
foundAt = i
break
}
}

if foundAt < 0 {
return ErrNoDefault
}

if foundAt != len(entries)-1 {
return ErrExtraEntries
}

return nil
}

Expand Down
21 changes: 18 additions & 3 deletions private/path/pathpol/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package pathpol

import (
"encoding/json"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/private/common"
Expand Down Expand Up @@ -60,7 +62,7 @@ func TestNewACL(t *testing.T) {
}
}

func TestUnmarshalJSON(t *testing.T) {
func TestUnmarshal(t *testing.T) {
tests := map[string]struct {
Input string
ExpectedErr error
Expand All @@ -79,11 +81,24 @@ func TestUnmarshalJSON(t *testing.T) {
"Entry with hop predicates": {
Input: `["+ 42", "-"]`,
},
"Extra entries (first)": {
Input: `["-", "+ 27"]`,
ExpectedErr: ErrExtraEntries,
},
"Extra entries (in the middle)": {
Input: `["+ 42", "-", "+ 27", "- 30"]`,
ExpectedErr: ErrExtraEntries,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Run("json: "+name, func(t *testing.T) {
var acl ACL
err := json.Unmarshal([]byte(test.Input), &acl)
assert.ErrorIs(t, err, test.ExpectedErr)
})
t.Run("yaml: "+name, func(t *testing.T) {
var acl ACL
err := acl.UnmarshalJSON([]byte(test.Input))
err := yaml.Unmarshal([]byte(test.Input), &acl)
assert.ErrorIs(t, err, test.ExpectedErr)
})
}
Expand Down
13 changes: 7 additions & 6 deletions private/path/pathpol/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,17 @@ func TestFilterOpt(t *testing.T) {
}

func TestPolicyJsonConversion(t *testing.T) {
acl, err := NewACL([]*ACLEntry{
{Action: Allow, Rule: mustHopPredicate(t, "42-0#0")},
denyEntry,
}...)
require.NoError(t, err)

policy := NewPolicy("", nil, nil, []Option{
{
Policy: &ExtPolicy{
Policy: &Policy{
ACL: &ACL{
Entries: []*ACLEntry{
{Action: Allow, Rule: mustHopPredicate(t, "0-0#0")},
denyEntry,
},
},
ACL: acl,
LocalISDAS: &LocalISDAS{
AllowedIAs: []addr.IA{
xtest.MustParseIA("64-123"),
Expand Down

0 comments on commit a7c922c

Please sign in to comment.