2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 20:12:41 +00:00
|
|
|
|
2019-12-06 14:25:26 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestACL_Enforce(t *testing.T) {
|
|
|
|
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",
|
|
|
|
},
|
2022-06-29 20:38:17 +00:00
|
|
|
{
|
2022-07-22 20:42:23 +00:00
|
|
|
method: "PeeringRead",
|
2022-06-29 20:38:17 +00:00
|
|
|
resource: ResourcePeering,
|
|
|
|
access: "read",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
|
|
|
{
|
2022-07-22 20:42:23 +00:00
|
|
|
method: "PeeringRead",
|
2022-06-29 20:38:17 +00:00
|
|
|
resource: ResourcePeering,
|
|
|
|
access: "read",
|
|
|
|
ret: Deny,
|
|
|
|
},
|
|
|
|
{
|
2022-07-22 20:42:23 +00:00
|
|
|
method: "PeeringWrite",
|
2022-06-29 20:38:17 +00:00
|
|
|
resource: ResourcePeering,
|
|
|
|
access: "write",
|
|
|
|
ret: Allow,
|
|
|
|
},
|
|
|
|
{
|
2022-07-22 20:42:23 +00:00
|
|
|
method: "PeeringWrite",
|
2022-06-29 20:38:17 +00:00
|
|
|
resource: ResourcePeering,
|
|
|
|
access: "write",
|
|
|
|
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: 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) {
|
2022-11-14 20:35:12 +00:00
|
|
|
m := &MockAuthorizer{}
|
2019-12-06 14:25:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|