2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2016-12-14 07:21:14 +00:00
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io"
|
2016-12-14 07:21:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2020-11-17 22:10:21 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-06-17 09:24:43 +00:00
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2017-09-25 18:40:42 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul"
|
|
|
|
"github.com/hashicorp/consul/agent/local"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-12-03 20:36:28 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2019-12-06 19:01:34 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2016-12-15 03:28:09 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-14 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
type authzResolver func(string) (structs.ACLIdentity, acl.Authorizer, error)
|
|
|
|
type identResolver func(string) (structs.ACLIdentity, error)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
type TestACLAgent struct {
|
2020-05-13 17:00:08 +00:00
|
|
|
resolveAuthzFn authzResolver
|
|
|
|
resolveIdentFn identResolver
|
2017-09-25 18:40:42 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
*Agent
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
// NewTestACLAgent does just enough so that all the code within agent/acl.go can work
|
2018-10-19 16:04:07 +00:00
|
|
|
// Basically it needs a local state for some of the vet* functions, a logger and a delegate.
|
2023-02-07 20:19:09 +00:00
|
|
|
// The key is that we are the delegate so we can control the ResolveToken responses
|
2020-05-13 17:00:08 +00:00
|
|
|
func NewTestACLAgent(t *testing.T, name string, hcl string, resolveAuthz authzResolver, resolveIdent identResolver) *TestACLAgent {
|
2020-08-08 01:08:43 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2022-02-02 17:07:29 +00:00
|
|
|
if resolveIdent == nil {
|
|
|
|
resolveIdent = func(s string) (structs.ACLIdentity, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 21:03:55 +00:00
|
|
|
a := &TestACLAgent{resolveAuthzFn: resolveAuthz, resolveIdentFn: resolveIdent}
|
|
|
|
|
|
|
|
dataDir := testutil.TempDir(t, "acl-agent")
|
2019-12-06 19:01:34 +00:00
|
|
|
|
2020-08-08 01:08:43 +00:00
|
|
|
logBuffer := testutil.NewLogBuffer(t)
|
2022-10-24 22:02:38 +00:00
|
|
|
|
|
|
|
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Name: name,
|
|
|
|
Level: testutil.TestLogLevel,
|
|
|
|
Output: logBuffer,
|
|
|
|
TimeFormat: "04:05.000",
|
|
|
|
})
|
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
loader := func(source config.Source) (config.LoadResult, error) {
|
2020-08-08 01:08:43 +00:00
|
|
|
dataDir := fmt.Sprintf(`data_dir = "%s"`, dataDir)
|
2020-12-21 18:25:32 +00:00
|
|
|
opts := config.LoadOpts{
|
|
|
|
HCL: []string{TestConfigHCL(NodeID()), hcl, dataDir},
|
|
|
|
DefaultConfig: source,
|
2020-08-08 01:08:43 +00:00
|
|
|
}
|
2020-12-21 18:25:32 +00:00
|
|
|
result, err := config.Load(opts)
|
|
|
|
if result.RuntimeConfig != nil {
|
|
|
|
result.RuntimeConfig.Telemetry.Disable = true
|
2020-08-17 18:12:04 +00:00
|
|
|
}
|
2020-12-21 18:25:32 +00:00
|
|
|
return result, err
|
2020-06-10 20:47:35 +00:00
|
|
|
}
|
2022-10-24 22:02:38 +00:00
|
|
|
bd, err := NewBaseDeps(loader, logBuffer, logger)
|
2020-05-13 17:00:08 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2022-05-19 20:03:46 +00:00
|
|
|
bd.MetricsConfig = &lib.MetricsConfig{
|
|
|
|
Handler: metrics.NewInmemSink(1*time.Second, time.Minute),
|
|
|
|
}
|
2020-08-08 01:08:43 +00:00
|
|
|
|
|
|
|
agent, err := New(bd)
|
|
|
|
require.NoError(t, err)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2020-08-08 01:08:43 +00:00
|
|
|
agent.delegate = a
|
|
|
|
agent.State = local.NewState(LocalConfig(bd.RuntimeConfig), bd.Logger, bd.Tokens)
|
|
|
|
agent.State.TriggerSyncChanges = func() {}
|
|
|
|
a.Agent = agent
|
2018-10-19 16:04:07 +00:00
|
|
|
return a
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2023-02-07 20:19:09 +00:00
|
|
|
func (a *TestACLAgent) ResolveToken(secretID string) (acl.Authorizer, error) {
|
2020-05-13 17:00:08 +00:00
|
|
|
if a.resolveAuthzFn == nil {
|
2023-02-07 20:19:09 +00:00
|
|
|
return nil, fmt.Errorf("ResolveToken call is unexpected - no authz resolver callback set")
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
_, authz, err := a.resolveAuthzFn(secretID)
|
2019-12-18 18:46:53 +00:00
|
|
|
return authz, err
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:24:43 +00:00
|
|
|
func (a *TestACLAgent) ResolveTokenAndDefaultMeta(secretID string, entMeta *acl.EnterpriseMeta, authzContext *acl.AuthorizerContext) (resolver.Result, error) {
|
2023-02-07 20:19:09 +00:00
|
|
|
authz, err := a.ResolveToken(secretID)
|
2022-01-23 17:31:48 +00:00
|
|
|
if err != nil {
|
2022-06-17 09:24:43 +00:00
|
|
|
return resolver.Result{}, err
|
2019-12-18 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
identity, err := a.resolveIdentFn(secretID)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
2022-06-17 09:24:43 +00:00
|
|
|
return resolver.Result{}, err
|
2019-12-18 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default the EnterpriseMeta based on the Tokens meta or actual defaults
|
|
|
|
// in the case of unknown identity
|
|
|
|
if identity != nil {
|
|
|
|
entMeta.Merge(identity.EnterpriseMetadata())
|
|
|
|
} else {
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta.Merge(structs.DefaultEnterpriseMetaInDefaultPartition())
|
2019-12-18 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use the meta to fill in the ACL authorization context
|
|
|
|
entMeta.FillAuthzContext(authzContext)
|
|
|
|
|
2022-06-17 09:24:43 +00:00
|
|
|
return resolver.Result{Authorizer: authz, ACLIdentity: identity}, err
|
2019-12-18 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// All of these are stubs to satisfy the interface
|
|
|
|
func (a *TestACLAgent) GetLANCoordinate() (lib.CoordinateSet, error) {
|
|
|
|
return nil, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) Leave() error {
|
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2021-10-26 20:08:55 +00:00
|
|
|
func (a *TestACLAgent) LANMembersInAgentPartition() []serf.Member {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-26 20:08:55 +00:00
|
|
|
func (a *TestACLAgent) LANMembers(f consul.LANMemberFilter) ([]serf.Member, error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2021-10-26 20:08:55 +00:00
|
|
|
func (a *TestACLAgent) AgentLocalMember() serf.Member {
|
2018-10-19 16:04:07 +00:00
|
|
|
return serf.Member{}
|
|
|
|
}
|
2022-04-05 21:10:06 +00:00
|
|
|
func (a *TestACLAgent) JoinLAN(addrs []string, entMeta *acl.EnterpriseMeta) (n int, err error) {
|
2018-10-19 16:04:07 +00:00
|
|
|
return 0, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2022-04-05 21:10:06 +00:00
|
|
|
func (a *TestACLAgent) RemoveFailedNode(node string, prune bool, entMeta *acl.EnterpriseMeta) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2022-12-14 15:24:22 +00:00
|
|
|
func (a *TestACLAgent) RPC(ctx context.Context, method string, args interface{}, reply interface{}) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) SnapshotRPC(args *structs.SnapshotRequest, in io.Reader, out io.Writer, replyFn structs.SnapshotReplyFn) error {
|
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) Shutdown() error {
|
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) Stats() map[string]map[string]string {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-16 17:28:03 +00:00
|
|
|
func (a *TestACLAgent) ReloadConfig(_ consul.ReloadableConfig) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Unimplemented")
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
func TestACL_Version8EnabledByDefault(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2020-05-29 21:16:03 +00:00
|
|
|
called := false
|
|
|
|
resolveFn := func(string) (structs.ACLIdentity, acl.Authorizer, error) {
|
|
|
|
called = true
|
|
|
|
return nil, nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), resolveFn, nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2021-04-14 16:39:35 +00:00
|
|
|
_, err := a.delegate.ResolveTokenAndDefaultMeta("nope", nil, nil)
|
2020-05-29 21:16:03 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, called)
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:44:32 +00:00
|
|
|
func authzFromPolicy(policy *acl.Policy, cfg *acl.Config) (acl.Authorizer, error) {
|
2019-12-06 19:01:34 +00:00
|
|
|
return acl.NewPolicyAuthorizerWithDefaults(acl.DenyAll(), []*acl.Policy{policy}, cfg)
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
type testTokenRules struct {
|
2019-12-18 18:46:53 +00:00
|
|
|
token structs.ACLToken
|
2023-01-27 15:17:07 +00:00
|
|
|
// rules to create associated policy
|
2019-12-18 18:46:53 +00:00
|
|
|
rules string
|
|
|
|
}
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
var (
|
|
|
|
nodeROSecret = "7e80d017-bccc-492f-8dec-65f03aeaebf3"
|
|
|
|
nodeRWSecret = "e3586ee5-02a2-4bf4-9ec3-9c4be7606e8c"
|
|
|
|
serviceROSecret = "3d2c8552-df3b-4da7-9890-36885cbf56ac"
|
|
|
|
serviceRWSecret = "4a1017a2-f788-4be3-93f2-90566f1340bb"
|
|
|
|
otherRWSecret = "a38e8016-91b6-4876-b3e7-a307abbb2002"
|
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
testACLs = map[string]testTokenRules{
|
2020-05-13 17:00:08 +00:00
|
|
|
nodeROSecret: {
|
2019-12-18 18:46:53 +00:00
|
|
|
token: structs.ACLToken{
|
|
|
|
AccessorID: "9df2d1a4-2d07-414e-8ead-6053f56ed2eb",
|
|
|
|
SecretID: nodeROSecret,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-18 18:46:53 +00:00
|
|
|
rules: `node_prefix "Node" { policy = "read" }`,
|
|
|
|
},
|
2020-05-13 17:00:08 +00:00
|
|
|
nodeRWSecret: {
|
2019-12-18 18:46:53 +00:00
|
|
|
token: structs.ACLToken{
|
|
|
|
AccessorID: "efb6b7d5-d343-47c1-b4cb-aa6b94d2f490",
|
2020-06-10 20:47:35 +00:00
|
|
|
SecretID: nodeRWSecret,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-18 18:46:53 +00:00
|
|
|
rules: `node_prefix "Node" { policy = "write" }`,
|
|
|
|
},
|
2020-05-13 17:00:08 +00:00
|
|
|
serviceROSecret: {
|
2019-12-18 18:46:53 +00:00
|
|
|
token: structs.ACLToken{
|
|
|
|
AccessorID: "0da53edb-36e5-4603-9c31-79965bad45f5",
|
|
|
|
SecretID: serviceROSecret,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-18 18:46:53 +00:00
|
|
|
rules: `service_prefix "service" { policy = "read" }`,
|
|
|
|
},
|
2020-05-13 17:00:08 +00:00
|
|
|
serviceRWSecret: {
|
2019-12-18 18:46:53 +00:00
|
|
|
token: structs.ACLToken{
|
|
|
|
AccessorID: "52504258-137a-41e6-9326-01f40e80872e",
|
|
|
|
SecretID: serviceRWSecret,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-18 18:46:53 +00:00
|
|
|
rules: `service_prefix "service" { policy = "write" }`,
|
|
|
|
},
|
2020-05-13 17:00:08 +00:00
|
|
|
otherRWSecret: {
|
2019-12-18 18:46:53 +00:00
|
|
|
token: structs.ACLToken{
|
|
|
|
AccessorID: "5e032c5b-c39e-4552-b5ad-8a9365b099c4",
|
|
|
|
SecretID: otherRWSecret,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-18 18:46:53 +00:00
|
|
|
rules: `service_prefix "other" { policy = "write" }`,
|
|
|
|
},
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
)
|
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
func catalogPolicy(testACL string) (structs.ACLIdentity, acl.Authorizer, error) {
|
|
|
|
tok, ok := testACLs[testACL]
|
2019-12-18 18:46:53 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2023-02-06 15:35:52 +00:00
|
|
|
policy, err := acl.NewPolicyFromSource(tok.rules, nil, nil)
|
2019-12-18 18:46:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
authz, err := authzFromPolicy(policy, nil)
|
|
|
|
return &tok.token, authz, err
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 15:17:07 +00:00
|
|
|
func catalogIdent(testACL string) (structs.ACLIdentity, error) {
|
|
|
|
tok, ok := testACLs[testACL]
|
2020-05-13 17:00:08 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tok.token, nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:28:09 +00:00
|
|
|
func TestACL_vetServiceRegister(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new service, with permission.
|
2019-12-18 18:46:53 +00:00
|
|
|
err := a.vetServiceRegister(serviceRWSecret, &structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new service without write privs.
|
2019-12-18 18:46:53 +00:00
|
|
|
err = a.vetServiceRegister(serviceROSecret, &structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Try to register over a service without write privs to the existing
|
|
|
|
// service.
|
2022-07-26 20:54:53 +00:00
|
|
|
a.State.AddServiceWithChecks(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "other",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, nil, "", false)
|
2019-12-18 18:46:53 +00:00
|
|
|
err = a.vetServiceRegister(serviceRWSecret, &structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
func TestACL_vetServiceUpdateWithAuthorizer(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
vetServiceUpdate := func(token string, serviceID structs.ServiceID) error {
|
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.vetServiceUpdateWithAuthorizer(authz, serviceID)
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:28:09 +00:00
|
|
|
// Update a service that doesn't exist.
|
2021-07-30 17:08:50 +00:00
|
|
|
err := vetServiceUpdate(serviceRWSecret, structs.NewServiceID("my-service", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Unknown service")
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update with write privs.
|
2022-07-26 20:54:53 +00:00
|
|
|
a.State.AddServiceWithChecks(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, nil, "", false)
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetServiceUpdate(serviceRWSecret, structs.NewServiceID("my-service", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update without write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetServiceUpdate(serviceROSecret, structs.NewServiceID("my-service", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
func TestACL_vetCheckRegisterWithAuthorizer(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
vetCheckRegister := func(token string, check *structs.HealthCheck) error {
|
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return a.vetCheckRegisterWithAuthorizer(authz, check)
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:28:09 +00:00
|
|
|
// Register a new service check with write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err := vetCheckRegister(serviceRWSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new service check without write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckRegister(serviceROSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new node check with write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckRegister(nodeRWSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new node check without write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckRegister(nodeROSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Try to register over a service check without write privs to the
|
|
|
|
// existing service.
|
2022-07-26 20:54:53 +00:00
|
|
|
a.State.AddServiceWithChecks(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, nil, "", false)
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddCheck(&structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "other",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, "", false)
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckRegister(serviceRWSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Try to register over a node check without write privs to the node.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddCheck(&structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-node-check"),
|
2023-01-10 16:24:02 +00:00
|
|
|
}, "", false)
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckRegister(serviceRWSecret, &structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-node-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "service",
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
func TestACL_vetCheckUpdateWithAuthorizer(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
vetCheckUpdate := func(token string, checkID structs.CheckID) error {
|
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.vetCheckUpdateWithAuthorizer(authz, checkID)
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:28:09 +00:00
|
|
|
// Update a check that doesn't exist.
|
2021-07-30 17:08:50 +00:00
|
|
|
err := vetCheckUpdate(nodeRWSecret, structs.NewCheckID("my-check", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Unknown check")
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update service check with write privs.
|
2022-07-26 20:54:53 +00:00
|
|
|
a.State.AddServiceWithChecks(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, nil, "", false)
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddCheck(&structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-service-check"),
|
|
|
|
ServiceID: "my-service",
|
|
|
|
ServiceName: "service",
|
2023-01-10 16:24:02 +00:00
|
|
|
}, "", false)
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckUpdate(serviceRWSecret, structs.NewCheckID("my-service-check", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update service check without write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckUpdate(serviceROSecret, structs.NewCheckID("my-service-check", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
2019-12-10 02:26:41 +00:00
|
|
|
require.True(t, acl.IsErrPermissionDenied(err), "not permission denied: %s", err.Error())
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update node check with write privs.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddCheck(&structs.HealthCheck{
|
2016-12-15 03:28:09 +00:00
|
|
|
CheckID: types.CheckID("my-node-check"),
|
2023-01-10 16:24:02 +00:00
|
|
|
}, "", false)
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckUpdate(nodeRWSecret, structs.NewCheckID("my-node-check", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update without write privs.
|
2021-07-30 17:08:50 +00:00
|
|
|
err = vetCheckUpdate(nodeROSecret, structs.NewCheckID("my-node-check", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_filterMembers(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
var members []serf.Member
|
2019-12-18 18:46:53 +00:00
|
|
|
require.NoError(t, a.filterMembers(nodeROSecret, &members))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Len(t, members, 0)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
members = []serf.Member{
|
2020-06-16 17:19:31 +00:00
|
|
|
{Name: "Node 1"},
|
|
|
|
{Name: "Nope"},
|
|
|
|
{Name: "Node 2"},
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
2019-12-18 18:46:53 +00:00
|
|
|
require.NoError(t, a.filterMembers(nodeROSecret, &members))
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Len(t, members, 2)
|
|
|
|
require.Equal(t, members[0].Name, "Node 1")
|
|
|
|
require.Equal(t, members[1].Name, "Node 2")
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
func TestACL_filterServicesWithAuthorizer(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
filterServices := func(token string, services map[string]*api.AgentService) error {
|
2021-07-30 17:08:50 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.filterServicesWithAuthorizer(authz, services)
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
services := make(map[string]*api.AgentService)
|
|
|
|
require.NoError(t, filterServices(nodeROSecret, services))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
services[structs.NewServiceID("my-service", nil).String()] = &api.AgentService{ID: "my-service", Service: "service"}
|
|
|
|
services[structs.NewServiceID("my-other", nil).String()] = &api.AgentService{ID: "my-other", Service: "other"}
|
|
|
|
require.NoError(t, filterServices(serviceROSecret, services))
|
|
|
|
|
|
|
|
require.Contains(t, services, structs.NewServiceID("my-service", nil).String())
|
|
|
|
require.NotContains(t, services, structs.NewServiceID("my-other", nil).String())
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:08:50 +00:00
|
|
|
func TestACL_filterChecksWithAuthorizer(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2020-05-13 17:00:08 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy, catalogIdent)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
filterChecks := func(token string, checks map[types.CheckID]*structs.HealthCheck) error {
|
2021-07-30 17:08:50 +00:00
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.filterChecksWithAuthorizer(authz, checks)
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
checks := make(map[types.CheckID]*structs.HealthCheck)
|
|
|
|
require.NoError(t, filterChecks(nodeROSecret, checks))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
checks["my-node"] = &structs.HealthCheck{}
|
|
|
|
checks["my-service"] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks["my-other"] = &structs.HealthCheck{ServiceName: "other"}
|
|
|
|
require.NoError(t, filterChecks(serviceROSecret, checks))
|
|
|
|
_, ok := checks["my-node"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2021-12-03 20:36:28 +00:00
|
|
|
_, ok = checks["my-service"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, ok)
|
2021-12-03 20:36:28 +00:00
|
|
|
_, ok = checks["my-other"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2021-12-03 20:36:28 +00:00
|
|
|
checks["my-node"] = &structs.HealthCheck{}
|
|
|
|
checks["my-service"] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks["my-other"] = &structs.HealthCheck{ServiceName: "other"}
|
|
|
|
require.NoError(t, filterChecks(nodeROSecret, checks))
|
|
|
|
_, ok = checks["my-node"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, ok)
|
2021-12-03 20:36:28 +00:00
|
|
|
_, ok = checks["my-service"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2021-12-03 20:36:28 +00:00
|
|
|
_, ok = checks["my-other"]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|