2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-03-27 10:59:30 +00:00
|
|
|
package testutils
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-27 10:59:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-05 21:10:06 +00:00
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-06-17 09:24:43 +00:00
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2023-01-04 12:40:34 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-04-05 14:26:14 +00:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
func ACLAnonymous(t *testing.T) resolver.Result {
|
2022-06-17 09:24:43 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
return resolver.Result{
|
|
|
|
Authorizer: acl.DenyAll(),
|
|
|
|
ACLIdentity: &structs.ACLToken{
|
2023-01-09 18:28:53 +00:00
|
|
|
AccessorID: acl.AnonymousTokenID,
|
2023-01-04 12:40:34 +00:00
|
|
|
},
|
|
|
|
}
|
2022-06-17 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:31:18 +00:00
|
|
|
func ACLsDisabled(t *testing.T) resolver.Result {
|
2022-06-17 09:24:43 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
return resolver.Result{
|
2023-01-05 16:31:18 +00:00
|
|
|
Authorizer: acl.ManageAll(),
|
2023-01-04 12:40:34 +00:00
|
|
|
}
|
2022-06-17 09:24:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
func ACLNoPermissions(t *testing.T) resolver.Result {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return resolver.Result{
|
|
|
|
Authorizer: acl.DenyAll(),
|
|
|
|
ACLIdentity: randomACLIdentity(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ACLServiceWriteAny(t *testing.T) resolver.Result {
|
2022-04-05 14:26:14 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
policy, err := acl.NewPolicyFromSource(`
|
|
|
|
service "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
2023-02-06 15:35:52 +00:00
|
|
|
`, nil, nil)
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
authz, err := acl.NewPolicyAuthorizerWithDefaults(acl.DenyAll(), []*acl.Policy{policy}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
return resolver.Result{
|
|
|
|
Authorizer: authz,
|
|
|
|
ACLIdentity: randomACLIdentity(t),
|
|
|
|
}
|
2022-04-05 14:26:14 +00:00
|
|
|
}
|
2022-04-20 00:24:21 +00:00
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
func ACLServiceRead(t *testing.T, serviceName string) resolver.Result {
|
2022-04-20 00:24:21 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
aclRule := &acl.Policy{
|
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
Services: []*acl.ServiceRule{
|
|
|
|
{
|
|
|
|
Name: serviceName,
|
|
|
|
Policy: acl.PolicyRead,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
authz, err := acl.NewPolicyAuthorizerWithDefaults(acl.DenyAll(), []*acl.Policy{aclRule}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-01-04 12:40:34 +00:00
|
|
|
return resolver.Result{
|
|
|
|
Authorizer: authz,
|
|
|
|
ACLIdentity: randomACLIdentity(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomACLIdentity(t *testing.T) structs.ACLIdentity {
|
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return &structs.ACLToken{AccessorID: id}
|
2022-04-20 00:24:21 +00:00
|
|
|
}
|