2019-10-15 20:58:50 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
"github.com/armon/go-radix"
|
2019-10-15 20:58:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Note that many of the policy authorizer tests still live in acl_test.go. These utilize a default policy or layer
|
|
|
|
// up multiple authorizers just like before the latest overhaul of the ACL package. To reduce the code diff and to
|
|
|
|
// ensure compatibility from version to version those tests have been only minimally altered. The tests in this
|
|
|
|
// file are specific to the newer functionality.
|
|
|
|
func TestPolicyAuthorizer(t *testing.T) {
|
|
|
|
type aclCheck struct {
|
|
|
|
name string
|
|
|
|
prefix string
|
2019-12-18 18:43:24 +00:00
|
|
|
check func(t *testing.T, authz Authorizer, prefix string, entCtx *AuthorizerContext)
|
2019-10-15 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type aclTest struct {
|
2022-11-14 18:36:27 +00:00
|
|
|
policy *Policy
|
|
|
|
authzContext *AuthorizerContext
|
|
|
|
checks []aclCheck
|
2019-10-15 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]aclTest{
|
|
|
|
// This test ensures that if the policy doesn't define a rule then the policy authorizer will
|
|
|
|
// return no concrete enforcement decision. This allows deferring to some defaults in another
|
|
|
|
// authorizer including usage of a default overall policy of "deny"
|
2020-06-16 17:19:31 +00:00
|
|
|
"Defaults": {
|
2019-10-15 20:58:50 +00:00
|
|
|
policy: &Policy{},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "DefaultACLRead", prefix: "foo", check: checkDefaultACLRead},
|
|
|
|
{name: "DefaultACLWrite", prefix: "foo", check: checkDefaultACLWrite},
|
|
|
|
{name: "DefaultAgentRead", prefix: "foo", check: checkDefaultAgentRead},
|
|
|
|
{name: "DefaultAgentWrite", prefix: "foo", check: checkDefaultAgentWrite},
|
|
|
|
{name: "DefaultEventRead", prefix: "foo", check: checkDefaultEventRead},
|
|
|
|
{name: "DefaultEventWrite", prefix: "foo", check: checkDefaultEventWrite},
|
|
|
|
{name: "DefaultIntentionDefaultAllow", prefix: "foo", check: checkDefaultIntentionDefaultAllow},
|
|
|
|
{name: "DefaultIntentionRead", prefix: "foo", check: checkDefaultIntentionRead},
|
|
|
|
{name: "DefaultIntentionWrite", prefix: "foo", check: checkDefaultIntentionWrite},
|
|
|
|
{name: "DefaultKeyRead", prefix: "foo", check: checkDefaultKeyRead},
|
|
|
|
{name: "DefaultKeyList", prefix: "foo", check: checkDefaultKeyList},
|
|
|
|
{name: "DefaultKeyringRead", prefix: "foo", check: checkDefaultKeyringRead},
|
|
|
|
{name: "DefaultKeyringWrite", prefix: "foo", check: checkDefaultKeyringWrite},
|
|
|
|
{name: "DefaultKeyWrite", prefix: "foo", check: checkDefaultKeyWrite},
|
|
|
|
{name: "DefaultKeyWritePrefix", prefix: "foo", check: checkDefaultKeyWritePrefix},
|
|
|
|
{name: "DefaultNodeRead", prefix: "foo", check: checkDefaultNodeRead},
|
|
|
|
{name: "DefaultNodeWrite", prefix: "foo", check: checkDefaultNodeWrite},
|
2021-08-20 22:11:01 +00:00
|
|
|
{name: "DefaultMeshRead", prefix: "foo", check: checkDefaultMeshRead},
|
|
|
|
{name: "DefaultMeshWrite", prefix: "foo", check: checkDefaultMeshWrite},
|
2022-07-22 20:42:23 +00:00
|
|
|
{name: "DefaultPeeringRead", prefix: "foo", check: checkDefaultPeeringRead},
|
|
|
|
{name: "DefaultPeeringWrite", prefix: "foo", check: checkDefaultPeeringWrite},
|
2019-10-15 20:58:50 +00:00
|
|
|
{name: "DefaultOperatorRead", prefix: "foo", check: checkDefaultOperatorRead},
|
|
|
|
{name: "DefaultOperatorWrite", prefix: "foo", check: checkDefaultOperatorWrite},
|
|
|
|
{name: "DefaultPreparedQueryRead", prefix: "foo", check: checkDefaultPreparedQueryRead},
|
|
|
|
{name: "DefaultPreparedQueryWrite", prefix: "foo", check: checkDefaultPreparedQueryWrite},
|
|
|
|
{name: "DefaultServiceRead", prefix: "foo", check: checkDefaultServiceRead},
|
|
|
|
{name: "DefaultServiceWrite", prefix: "foo", check: checkDefaultServiceWrite},
|
2022-03-24 11:25:05 +00:00
|
|
|
{name: "DefaultServiceWriteAny", prefix: "", check: checkDefaultServiceWriteAny},
|
2019-10-15 20:58:50 +00:00
|
|
|
{name: "DefaultSessionRead", prefix: "foo", check: checkDefaultSessionRead},
|
|
|
|
{name: "DefaultSessionWrite", prefix: "foo", check: checkDefaultSessionWrite},
|
|
|
|
{name: "DefaultSnapshot", prefix: "foo", check: checkDefaultSnapshot},
|
|
|
|
},
|
|
|
|
},
|
2022-11-14 18:36:27 +00:00
|
|
|
"Defaults - from peer": {
|
|
|
|
policy: &Policy{},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "DefaultNodeRead", prefix: "foo", check: checkDefaultNodeRead},
|
|
|
|
{name: "DefaultServiceRead", prefix: "foo", check: checkDefaultServiceRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - ServiceRead allowed with service:write": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "ServiceWriteAny", prefix: "imported-svc", check: checkAllowServiceRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - ServiceRead allowed with service:read on all": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
ServicePrefixes: []*ServiceRule{
|
|
|
|
{
|
|
|
|
Name: "",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Intentions: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "ServiceReadAll", prefix: "imported-svc", check: checkAllowServiceRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - ServiceRead not allowed with service:read on single service": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
|
|
|
{
|
|
|
|
Name: "same-name-as-imported",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Intentions: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "ServiceReadAll", prefix: "same-name-as-imported", check: checkDefaultServiceRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - NodeRead allowed with service:write": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "ServiceWriteAny", prefix: "imported-svc", check: checkAllowNodeRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - NodeRead allowed with node:read on all": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
NodePrefixes: []*NodeRule{
|
|
|
|
{
|
|
|
|
Name: "",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "NodeReadAll", prefix: "imported-svc", check: checkAllowNodeRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Peering - NodeRead not allowed with node:read on single service": {
|
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Nodes: []*NodeRule{
|
|
|
|
{
|
|
|
|
Name: "same-name-as-imported",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
authzContext: &AuthorizerContext{Peer: "some-peer"},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "NodeReadAll", prefix: "same-name-as-imported", check: checkDefaultNodeRead},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Prefer Exact Matches": {
|
2019-10-15 20:58:50 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Agents: []*AgentRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AgentPrefixes: []*AgentRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Keys: []*KeyRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KeyPrefixes: []*KeyRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Nodes: []*NodeRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NodePrefixes: []*NodeRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServicePrefixes: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Intentions: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Name: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Intentions: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Sessions: []*SessionRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SessionPrefixes: []*SessionRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Node: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Events: []*EventRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Event: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Event: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
EventPrefixes: []*EventRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Event: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Event: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PreparedQueries: []*PreparedQueryRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "football",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PreparedQueryPrefixes: []*PreparedQueryRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "foot",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-10-15 20:58:50 +00:00
|
|
|
Prefix: "fo",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AgentReadPrefixAllowed", prefix: "fo", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWritePrefixDenied", prefix: "fo", check: checkDenyAgentWrite},
|
|
|
|
{name: "AgentReadPrefixAllowed", prefix: "for", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWritePrefixDenied", prefix: "for", check: checkDenyAgentWrite},
|
|
|
|
{name: "AgentReadAllowed", prefix: "foo", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWriteAllowed", prefix: "foo", check: checkAllowAgentWrite},
|
|
|
|
{name: "AgentReadPrefixAllowed", prefix: "foot", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWritePrefixDenied", prefix: "foot", check: checkDenyAgentWrite},
|
|
|
|
{name: "AgentReadPrefixAllowed", prefix: "foot2", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWritePrefixDenied", prefix: "foot2", check: checkDenyAgentWrite},
|
|
|
|
{name: "AgentReadPrefixAllowed", prefix: "food", check: checkAllowAgentRead},
|
|
|
|
{name: "AgentWritePrefixDenied", prefix: "food", check: checkDenyAgentWrite},
|
|
|
|
{name: "AgentReadDenied", prefix: "football", check: checkDenyAgentRead},
|
|
|
|
{name: "AgentWriteDenied", prefix: "football", check: checkDenyAgentWrite},
|
|
|
|
|
|
|
|
{name: "KeyReadPrefixAllowed", prefix: "fo", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWritePrefixDenied", prefix: "fo", check: checkDenyKeyWrite},
|
|
|
|
{name: "KeyReadPrefixAllowed", prefix: "for", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWritePrefixDenied", prefix: "for", check: checkDenyKeyWrite},
|
|
|
|
{name: "KeyReadAllowed", prefix: "foo", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWriteAllowed", prefix: "foo", check: checkAllowKeyWrite},
|
|
|
|
{name: "KeyReadPrefixAllowed", prefix: "foot", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWritePrefixDenied", prefix: "foot", check: checkDenyKeyWrite},
|
|
|
|
{name: "KeyReadPrefixAllowed", prefix: "foot2", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWritePrefixDenied", prefix: "foot2", check: checkDenyKeyWrite},
|
|
|
|
{name: "KeyReadPrefixAllowed", prefix: "food", check: checkAllowKeyRead},
|
|
|
|
{name: "KeyWritePrefixDenied", prefix: "food", check: checkDenyKeyWrite},
|
|
|
|
{name: "KeyReadDenied", prefix: "football", check: checkDenyKeyRead},
|
|
|
|
{name: "KeyWriteDenied", prefix: "football", check: checkDenyKeyWrite},
|
|
|
|
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "fo", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "fo", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "for", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "for", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadAllowed", prefix: "foo", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWriteAllowed", prefix: "foo", check: checkAllowNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "foot", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "foot", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "foot2", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "foot2", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "food", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "food", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadDenied", prefix: "football", check: checkDenyNodeRead},
|
|
|
|
{name: "NodeWriteDenied", prefix: "football", check: checkDenyNodeWrite},
|
|
|
|
|
|
|
|
{name: "ServiceReadPrefixAllowed", prefix: "fo", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWritePrefixDenied", prefix: "fo", check: checkDenyServiceWrite},
|
|
|
|
{name: "ServiceReadPrefixAllowed", prefix: "for", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWritePrefixDenied", prefix: "for", check: checkDenyServiceWrite},
|
|
|
|
{name: "ServiceReadAllowed", prefix: "foo", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWriteAllowed", prefix: "foo", check: checkAllowServiceWrite},
|
|
|
|
{name: "ServiceReadPrefixAllowed", prefix: "foot", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWritePrefixDenied", prefix: "foot", check: checkDenyServiceWrite},
|
|
|
|
{name: "ServiceReadPrefixAllowed", prefix: "foot2", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWritePrefixDenied", prefix: "foot2", check: checkDenyServiceWrite},
|
|
|
|
{name: "ServiceReadPrefixAllowed", prefix: "food", check: checkAllowServiceRead},
|
|
|
|
{name: "ServiceWritePrefixDenied", prefix: "food", check: checkDenyServiceWrite},
|
|
|
|
{name: "ServiceReadDenied", prefix: "football", check: checkDenyServiceRead},
|
|
|
|
{name: "ServiceWriteDenied", prefix: "football", check: checkDenyServiceWrite},
|
2022-03-24 11:25:05 +00:00
|
|
|
{name: "ServiceWriteAnyAllowed", prefix: "", check: checkAllowServiceWriteAny},
|
2019-10-15 20:58:50 +00:00
|
|
|
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "fo", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "fo", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "for", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "for", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadAllowed", prefix: "foo", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWriteAllowed", prefix: "foo", check: checkAllowNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "foot", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "foot", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "foot2", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "foot2", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadPrefixAllowed", prefix: "food", check: checkAllowNodeRead},
|
|
|
|
{name: "NodeWritePrefixDenied", prefix: "food", check: checkDenyNodeWrite},
|
|
|
|
{name: "NodeReadDenied", prefix: "football", check: checkDenyNodeRead},
|
|
|
|
{name: "NodeWriteDenied", prefix: "football", check: checkDenyNodeWrite},
|
|
|
|
|
|
|
|
{name: "IntentionReadPrefixAllowed", prefix: "fo", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWritePrefixDenied", prefix: "fo", check: checkDenyIntentionWrite},
|
|
|
|
{name: "IntentionReadPrefixAllowed", prefix: "for", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWritePrefixDenied", prefix: "for", check: checkDenyIntentionWrite},
|
|
|
|
{name: "IntentionReadAllowed", prefix: "foo", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWriteAllowed", prefix: "foo", check: checkAllowIntentionWrite},
|
|
|
|
{name: "IntentionReadPrefixAllowed", prefix: "foot", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWritePrefixDenied", prefix: "foot", check: checkDenyIntentionWrite},
|
|
|
|
{name: "IntentionReadPrefixAllowed", prefix: "foot2", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWritePrefixDenied", prefix: "foot2", check: checkDenyIntentionWrite},
|
|
|
|
{name: "IntentionReadPrefixAllowed", prefix: "food", check: checkAllowIntentionRead},
|
|
|
|
{name: "IntentionWritePrefixDenied", prefix: "food", check: checkDenyIntentionWrite},
|
|
|
|
{name: "IntentionReadDenied", prefix: "football", check: checkDenyIntentionRead},
|
|
|
|
{name: "IntentionWriteDenied", prefix: "football", check: checkDenyIntentionWrite},
|
|
|
|
|
|
|
|
{name: "SessionReadPrefixAllowed", prefix: "fo", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWritePrefixDenied", prefix: "fo", check: checkDenySessionWrite},
|
|
|
|
{name: "SessionReadPrefixAllowed", prefix: "for", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWritePrefixDenied", prefix: "for", check: checkDenySessionWrite},
|
|
|
|
{name: "SessionReadAllowed", prefix: "foo", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWriteAllowed", prefix: "foo", check: checkAllowSessionWrite},
|
|
|
|
{name: "SessionReadPrefixAllowed", prefix: "foot", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWritePrefixDenied", prefix: "foot", check: checkDenySessionWrite},
|
|
|
|
{name: "SessionReadPrefixAllowed", prefix: "foot2", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWritePrefixDenied", prefix: "foot2", check: checkDenySessionWrite},
|
|
|
|
{name: "SessionReadPrefixAllowed", prefix: "food", check: checkAllowSessionRead},
|
|
|
|
{name: "SessionWritePrefixDenied", prefix: "food", check: checkDenySessionWrite},
|
|
|
|
{name: "SessionReadDenied", prefix: "football", check: checkDenySessionRead},
|
|
|
|
{name: "SessionWriteDenied", prefix: "football", check: checkDenySessionWrite},
|
|
|
|
|
|
|
|
{name: "EventReadPrefixAllowed", prefix: "fo", check: checkAllowEventRead},
|
|
|
|
{name: "EventWritePrefixDenied", prefix: "fo", check: checkDenyEventWrite},
|
|
|
|
{name: "EventReadPrefixAllowed", prefix: "for", check: checkAllowEventRead},
|
|
|
|
{name: "EventWritePrefixDenied", prefix: "for", check: checkDenyEventWrite},
|
|
|
|
{name: "EventReadAllowed", prefix: "foo", check: checkAllowEventRead},
|
|
|
|
{name: "EventWriteAllowed", prefix: "foo", check: checkAllowEventWrite},
|
|
|
|
{name: "EventReadPrefixAllowed", prefix: "foot", check: checkAllowEventRead},
|
|
|
|
{name: "EventWritePrefixDenied", prefix: "foot", check: checkDenyEventWrite},
|
|
|
|
{name: "EventReadPrefixAllowed", prefix: "foot2", check: checkAllowEventRead},
|
|
|
|
{name: "EventWritePrefixDenied", prefix: "foot2", check: checkDenyEventWrite},
|
|
|
|
{name: "EventReadPrefixAllowed", prefix: "food", check: checkAllowEventRead},
|
|
|
|
{name: "EventWritePrefixDenied", prefix: "food", check: checkDenyEventWrite},
|
|
|
|
{name: "EventReadDenied", prefix: "football", check: checkDenyEventRead},
|
|
|
|
{name: "EventWriteDenied", prefix: "football", check: checkDenyEventWrite},
|
|
|
|
|
|
|
|
{name: "PreparedQueryReadPrefixAllowed", prefix: "fo", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWritePrefixDenied", prefix: "fo", check: checkDenyPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadPrefixAllowed", prefix: "for", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWritePrefixDenied", prefix: "for", check: checkDenyPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadAllowed", prefix: "foo", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWriteAllowed", prefix: "foo", check: checkAllowPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadPrefixAllowed", prefix: "foot", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWritePrefixDenied", prefix: "foot", check: checkDenyPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadPrefixAllowed", prefix: "foot2", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWritePrefixDenied", prefix: "foot2", check: checkDenyPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadPrefixAllowed", prefix: "food", check: checkAllowPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWritePrefixDenied", prefix: "food", check: checkDenyPreparedQueryWrite},
|
|
|
|
{name: "PreparedQueryReadDenied", prefix: "football", check: checkDenyPreparedQueryRead},
|
|
|
|
{name: "PreparedQueryWriteDenied", prefix: "football", check: checkDenyPreparedQueryWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Intention Wildcards - prefix denied": {
|
2020-01-13 20:51:40 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServicePrefixes: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "",
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
Intentions: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AnyAllowed", prefix: "*", check: checkAllowIntentionRead},
|
|
|
|
{name: "AllDenied", prefix: "*", check: checkDenyIntentionWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Intention Wildcards - prefix allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServicePrefixes: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AnyAllowed", prefix: "*", check: checkAllowIntentionRead},
|
|
|
|
{name: "AllDenied", prefix: "*", check: checkDenyIntentionWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Intention Wildcards - all allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ServicePrefixes: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AnyAllowed", prefix: "*", check: checkAllowIntentionRead},
|
|
|
|
{name: "AllAllowed", prefix: "*", check: checkAllowIntentionWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Intention Wildcards - all default": {
|
2020-01-13 20:51:40 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyWrite,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AnyAllowed", prefix: "*", check: checkAllowIntentionRead},
|
|
|
|
{name: "AllDefault", prefix: "*", check: checkDefaultIntentionWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"Intention Wildcards - any default": {
|
2020-01-13 20:51:40 +00:00
|
|
|
policy: &Policy{PolicyRules: PolicyRules{
|
|
|
|
Services: []*ServiceRule{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
Name: "foo",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Intentions: PolicyDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
checks: []aclCheck{
|
|
|
|
{name: "AnyDefault", prefix: "*", check: checkDefaultIntentionRead},
|
|
|
|
{name: "AllDenied", prefix: "*", check: checkDenyIntentionWrite},
|
|
|
|
},
|
|
|
|
},
|
2019-10-15 20:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
name := name
|
|
|
|
tcase := tcase
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
authz, err := NewPolicyAuthorizer([]*Policy{tcase.policy}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, check := range tcase.checks {
|
|
|
|
checkName := check.name
|
|
|
|
if check.prefix != "" {
|
|
|
|
checkName = fmt.Sprintf("%s.Prefix(%s)", checkName, check.prefix)
|
|
|
|
}
|
|
|
|
t.Run(checkName, func(t *testing.T) {
|
|
|
|
check := check
|
|
|
|
|
2022-11-14 18:36:27 +00:00
|
|
|
check.check(t, authz, check.prefix, tcase.authzContext)
|
2019-10-15 20:58:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 20:51:40 +00:00
|
|
|
|
|
|
|
func TestAnyAllowed(t *testing.T) {
|
|
|
|
type radixInsertion struct {
|
|
|
|
segment string
|
|
|
|
value *policyAuthorizerRadixLeaf
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
insertions []radixInsertion
|
|
|
|
|
|
|
|
readEnforcement EnforcementDecision
|
|
|
|
listEnforcement EnforcementDecision
|
|
|
|
writeEnforcement EnforcementDecision
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"no-rules-default": {
|
2020-01-13 20:51:40 +00:00
|
|
|
readEnforcement: Default,
|
|
|
|
listEnforcement: Default,
|
|
|
|
writeEnforcement: Default,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-write-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// this shouldn't affect whether anyAllowed returns things are allowed
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-list-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-read-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-write-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-write-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-list-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-list-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-read-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-read-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-deny-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny-other-deny-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tree := radix.New()
|
|
|
|
|
|
|
|
for _, insertion := range tcase.insertions {
|
|
|
|
tree.Insert(insertion.segment, insertion.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var authz policyAuthorizer
|
|
|
|
require.Equal(t, tcase.readEnforcement, authz.anyAllowed(tree, AccessRead))
|
|
|
|
require.Equal(t, tcase.listEnforcement, authz.anyAllowed(tree, AccessList))
|
|
|
|
require.Equal(t, tcase.writeEnforcement, authz.anyAllowed(tree, AccessWrite))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllAllowed(t *testing.T) {
|
|
|
|
type radixInsertion struct {
|
|
|
|
segment string
|
|
|
|
value *policyAuthorizerRadixLeaf
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
insertions []radixInsertion
|
|
|
|
|
|
|
|
readEnforcement EnforcementDecision
|
|
|
|
listEnforcement EnforcementDecision
|
|
|
|
writeEnforcement EnforcementDecision
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"no-rules-default": {
|
2020-01-13 20:51:40 +00:00
|
|
|
readEnforcement: Default,
|
|
|
|
listEnforcement: Default,
|
|
|
|
writeEnforcement: Default,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-write-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-list-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-read-allowed": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-deny": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-write-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-write-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Allow,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-list-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-list-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessList},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Allow,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-read-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-read-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessRead},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Allow,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-deny-prefix": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"prefix-allow-other-deny-exact": {
|
2020-01-13 20:51:40 +00:00
|
|
|
insertions: []radixInsertion{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
prefix: &policyAuthorizerRule{access: AccessWrite},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-01-13 20:51:40 +00:00
|
|
|
segment: "foo",
|
|
|
|
value: &policyAuthorizerRadixLeaf{
|
|
|
|
exact: &policyAuthorizerRule{access: AccessDeny},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
readEnforcement: Deny,
|
|
|
|
listEnforcement: Deny,
|
|
|
|
writeEnforcement: Deny,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tree := radix.New()
|
|
|
|
|
|
|
|
for _, insertion := range tcase.insertions {
|
|
|
|
tree.Insert(insertion.segment, insertion.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var authz policyAuthorizer
|
|
|
|
require.Equal(t, tcase.readEnforcement, authz.allAllowed(tree, AccessRead))
|
|
|
|
require.Equal(t, tcase.listEnforcement, authz.allAllowed(tree, AccessList))
|
|
|
|
require.Equal(t, tcase.writeEnforcement, authz.allAllowed(tree, AccessWrite))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|