2019-12-06 14:25:26 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockAuthorizer struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
var _ Authorizer = (*mockAuthorizer)(nil)
|
|
|
|
|
2019-12-06 14:25:26 +00:00
|
|
|
// ACLRead checks for permission to list all the ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) ACLRead(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACLWrite checks for permission to manipulate ACLs
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) ACLWrite(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentRead checks for permission to read from agent endpoints for a
|
|
|
|
// given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) AgentRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentWrite checks for permission to make changes via agent endpoints
|
|
|
|
// for a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) AgentWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventRead determines if a specific event can be queried.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) EventRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventWrite determines if a specific event may be fired.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) EventWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionDefaultAllow determines the default authorized behavior
|
|
|
|
// when no intentions match a Connect request.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) IntentionDefaultAllow(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionRead determines if a specific intention can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) IntentionRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntentionWrite determines if a specific intention can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) IntentionWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyList checks for permission to list keys under a prefix
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyList(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyRead checks for permission to read a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyWrite checks for permission to write a given key
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyWritePrefix checks for permission to write to an
|
|
|
|
// entire key prefix. This means there must be no sub-policies
|
|
|
|
// that deny a write.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyWritePrefix(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringRead determines if the encryption keyring used in
|
|
|
|
// the gossip layer can be read.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyringRead(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyringWrite determines if the keyring can be manipulated
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) KeyringWrite(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeRead checks for permission to read (discover) a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) NodeRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
2020-11-04 18:50:03 +00:00
|
|
|
func (m *mockAuthorizer) NodeReadAll(ctx *AuthorizerContext) EnforcementDecision {
|
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
2019-12-06 14:25:26 +00:00
|
|
|
// NodeWrite checks for permission to create or update (register) a
|
|
|
|
// given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) NodeWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorRead determines if the read-only Consul operator functions
|
|
|
|
// can be used. ret := m.Called(segment, ctx)
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) OperatorRead(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OperatorWrite determines if the state-changing Consul operator
|
|
|
|
// functions can be used.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) OperatorWrite(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreparedQueryRead determines if a specific prepared query can be read
|
|
|
|
// to show its contents (this is not used for execution).
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) PreparedQueryRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreparedQueryWrite determines if a specific prepared query can be
|
|
|
|
// created, modified, or deleted.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) PreparedQueryWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceRead checks for permission to read a given service
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) ServiceRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
2020-11-04 18:50:03 +00:00
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockAuthorizer) ServiceReadAll(ctx *AuthorizerContext) EnforcementDecision {
|
|
|
|
ret := m.Called(ctx)
|
2019-12-06 14:25:26 +00:00
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceWrite checks for permission to create or update a given
|
|
|
|
// service
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) ServiceWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionRead checks for permission to read sessions for a given node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) SessionRead(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionWrite checks for permission to create sessions for a given
|
|
|
|
// node.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) SessionWrite(segment string, ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(segment, ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot checks for permission to take and restore snapshots.
|
2019-12-18 18:43:24 +00:00
|
|
|
func (m *mockAuthorizer) Snapshot(ctx *AuthorizerContext) EnforcementDecision {
|
2019-12-06 14:25:26 +00:00
|
|
|
ret := m.Called(ctx)
|
|
|
|
return ret.Get(0).(EnforcementDecision)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_Enforce(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
method string
|
|
|
|
resource Resource
|
|
|
|
segment string
|
|
|
|
access string
|
|
|
|
ret EnforcementDecision
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
testName := func(t testCase) string {
|
|
|
|
if t.segment != "" {
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s", t.resource, t.segment, t.access, t.ret.String())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s/%s", t.resource, t.access, t.ret.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ACLRead",
|
|
|
|
resource: ResourceACL,
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ACLRead",
|
|
|
|
resource: ResourceACL,
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ACLWrite",
|
|
|
|
resource: ResourceACL,
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ACLWrite",
|
|
|
|
resource: ResourceACL,
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceACL,
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "OperatorRead",
|
|
|
|
resource: ResourceOperator,
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "OperatorRead",
|
|
|
|
resource: ResourceOperator,
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "OperatorWrite",
|
|
|
|
resource: ResourceOperator,
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "OperatorWrite",
|
|
|
|
resource: ResourceOperator,
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceOperator,
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyringRead",
|
|
|
|
resource: ResourceKeyring,
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyringRead",
|
|
|
|
resource: ResourceKeyring,
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyringWrite",
|
|
|
|
resource: ResourceKeyring,
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyringWrite",
|
|
|
|
resource: ResourceKeyring,
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceKeyring,
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "AgentRead",
|
|
|
|
resource: ResourceAgent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "AgentRead",
|
|
|
|
resource: ResourceAgent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "AgentWrite",
|
|
|
|
resource: ResourceAgent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "AgentWrite",
|
|
|
|
resource: ResourceAgent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceAgent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "EventRead",
|
|
|
|
resource: ResourceEvent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "EventRead",
|
|
|
|
resource: ResourceEvent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "EventWrite",
|
|
|
|
resource: ResourceEvent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "EventWrite",
|
|
|
|
resource: ResourceEvent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceEvent,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "IntentionRead",
|
|
|
|
resource: ResourceIntention,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "IntentionRead",
|
|
|
|
resource: ResourceIntention,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "IntentionWrite",
|
|
|
|
resource: ResourceIntention,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "IntentionWrite",
|
|
|
|
resource: ResourceIntention,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceIntention,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "NodeRead",
|
|
|
|
resource: ResourceNode,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "NodeRead",
|
|
|
|
resource: ResourceNode,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "NodeWrite",
|
|
|
|
resource: ResourceNode,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "NodeWrite",
|
|
|
|
resource: ResourceNode,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceNode,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "PreparedQueryRead",
|
|
|
|
resource: ResourceQuery,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "PreparedQueryRead",
|
|
|
|
resource: ResourceQuery,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "PreparedQueryWrite",
|
|
|
|
resource: ResourceQuery,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "PreparedQueryWrite",
|
|
|
|
resource: ResourceQuery,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceQuery,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ServiceRead",
|
|
|
|
resource: ResourceService,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ServiceRead",
|
|
|
|
resource: ResourceService,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ServiceWrite",
|
|
|
|
resource: ResourceService,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "ServiceWrite",
|
|
|
|
resource: ResourceService,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "SessionRead",
|
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "SessionRead",
|
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "SessionWrite",
|
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "SessionWrite",
|
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceSession,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyRead",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyRead",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyWrite",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyWrite",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyList",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
method: "KeyList",
|
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "list",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: ResourceKey,
|
|
|
|
segment: "foo",
|
|
|
|
access: "deny",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid access level",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-12-06 14:25:26 +00:00
|
|
|
resource: "not-a-real-resource",
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
err: "Invalid ACL resource requested:",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tcase := range cases {
|
|
|
|
t.Run(testName(tcase), func(t *testing.T) {
|
|
|
|
m := &mockAuthorizer{}
|
|
|
|
|
|
|
|
if tcase.err == "" {
|
2019-12-18 18:43:24 +00:00
|
|
|
var nilCtx *AuthorizerContext
|
2019-12-06 14:25:26 +00:00
|
|
|
if tcase.segment != "" {
|
|
|
|
m.On(tcase.method, tcase.segment, nilCtx).Return(tcase.ret)
|
|
|
|
} else {
|
|
|
|
m.On(tcase.method, nilCtx).Return(tcase.ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := Enforce(m, tcase.resource, tcase.segment, tcase.access, nil)
|
|
|
|
if tcase.err == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tcase.err)
|
|
|
|
}
|
|
|
|
require.Equal(t, tcase.ret, ret)
|
|
|
|
m.AssertExpectations(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|