2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2016-12-14 07:21:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/consul/acl"
|
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"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
|
|
|
"github.com/hashicorp/consul/logger"
|
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"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-14 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
type TestACLAgent struct {
|
|
|
|
// Name is an optional name of the agent.
|
|
|
|
Name string
|
2017-09-25 18:40:42 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
HCL string
|
2017-09-25 18:40:42 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Config is the agent configuration. If Config is nil then
|
|
|
|
// TestConfig() is used. If Config.DataDir is set then it is
|
|
|
|
// the callers responsibility to clean up the data directory.
|
|
|
|
// Otherwise, a temporary data directory is created and removed
|
|
|
|
// when Shutdown() is called.
|
|
|
|
Config *config.RuntimeConfig
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// LogOutput is the sink for the logs. If nil, logs are written
|
|
|
|
// to os.Stderr.
|
|
|
|
LogOutput io.Writer
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// LogWriter is used for streaming logs.
|
|
|
|
LogWriter *logger.LogWriter
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// DataDir is the data directory which is used when Config.DataDir
|
|
|
|
// is not set. It is created automatically and removed when
|
|
|
|
// Shutdown() is called.
|
|
|
|
DataDir string
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
resolveTokenFn func(string) (acl.Authorizer, error)
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// NewTestACLAGent does just enough so that all the code within agent/acl.go can work
|
|
|
|
// Basically it needs a local state for some of the vet* functions, a logger and a delegate.
|
|
|
|
// The key is that we are the delegate so we can control the ResolveToken responses
|
2019-12-06 19:01:34 +00:00
|
|
|
func NewTestACLAgent(t *testing.T, name string, hcl string, resolveFn func(string) (acl.Authorizer, error)) *TestACLAgent {
|
2018-10-19 16:04:07 +00:00
|
|
|
a := &TestACLAgent{Name: name, HCL: hcl, resolveTokenFn: resolveFn}
|
|
|
|
hclDataDir := `data_dir = "acl-agent"`
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
logOutput := testutil.TestWriter(t)
|
|
|
|
logger := log.New(logOutput, a.Name+" - ", log.LstdFlags|log.Lmicroseconds)
|
|
|
|
|
|
|
|
a.Config = TestConfig(logger,
|
2018-10-19 16:04:07 +00:00
|
|
|
config.Source{Name: a.Name, Format: "hcl", Data: a.HCL},
|
|
|
|
config.Source{Name: a.Name + ".data_dir", Format: "hcl", Data: hclDataDir},
|
|
|
|
)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
agent, err := New(a.Config, logger)
|
2018-10-19 16:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Error creating agent: %v", err))
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
a.Agent = agent
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
agent.LogOutput = logOutput
|
|
|
|
agent.LogWriter = a.LogWriter
|
2019-12-06 19:01:34 +00:00
|
|
|
agent.logger = logger
|
2018-10-19 16:04:07 +00:00
|
|
|
agent.MemSink = metrics.NewInmemSink(1*time.Second, time.Minute)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a.Agent.delegate = a
|
|
|
|
a.Agent.State = local.NewState(LocalConfig(a.Config), a.Agent.logger, a.Agent.tokens)
|
|
|
|
a.Agent.State.TriggerSyncChanges = func() {}
|
|
|
|
return a
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *TestACLAgent) ACLsEnabled() bool {
|
|
|
|
// the TestACLAgent always has ACLs enabled
|
|
|
|
return true
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *TestACLAgent) UseLegacyACLs() bool {
|
|
|
|
return false
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *TestACLAgent) ResolveToken(secretID string) (acl.Authorizer, error) {
|
|
|
|
if a.resolveTokenFn == nil {
|
|
|
|
panic("This agent is useless without providing a token resolution function")
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
return a.resolveTokenFn(secretID)
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// All of these are stubs to satisfy the interface
|
|
|
|
func (a *TestACLAgent) Encrypted() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) GetLANCoordinate() (lib.CoordinateSet, error) {
|
|
|
|
return nil, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) Leave() error {
|
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) LANMembers() []serf.Member {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) LANMembersAllSegments() ([]serf.Member, error) {
|
|
|
|
return nil, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) LANSegmentMembers(segment string) ([]serf.Member, error) {
|
|
|
|
return nil, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) LocalMember() serf.Member {
|
|
|
|
return serf.Member{}
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) JoinLAN(addrs []string) (n int, err error) {
|
|
|
|
return 0, fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2019-10-04 21:10:02 +00:00
|
|
|
func (a *TestACLAgent) RemoveFailedNode(node string, prune bool) error {
|
2018-10-19 16:04:07 +00:00
|
|
|
return fmt.Errorf("Unimplemented")
|
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (a *TestACLAgent) RPC(method string, args interface{}, reply interface{}) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
func (a *TestACLAgent) ReloadConfig(config *consul.Config) error {
|
|
|
|
return fmt.Errorf("Unimplemented")
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACL_Version8(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("version 8 disabled", func(t *testing.T) {
|
|
|
|
resolveFn := func(string) (acl.Authorizer, error) {
|
|
|
|
require.Fail(t, "should not have called delegate.ResolveToken")
|
|
|
|
return nil, fmt.Errorf("should not have called delegate.ResolveToken")
|
2018-07-01 10:50:53 +00:00
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig()+`
|
2018-10-19 16:04:07 +00:00
|
|
|
acl_enforce_version_8 = false
|
|
|
|
`, resolveFn)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
token, err := a.resolveToken("nope")
|
|
|
|
require.Nil(t, token)
|
|
|
|
require.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("version 8 enabled", func(t *testing.T) {
|
|
|
|
called := false
|
|
|
|
resolveFn := func(string) (acl.Authorizer, error) {
|
|
|
|
called = true
|
|
|
|
return nil, acl.ErrNotFound
|
2018-07-01 10:50:53 +00:00
|
|
|
}
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig()+`
|
2018-10-19 16:04:07 +00:00
|
|
|
acl_enforce_version_8 = true
|
|
|
|
`, resolveFn)
|
|
|
|
|
|
|
|
_, err := a.resolveToken("nope")
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, called)
|
|
|
|
})
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACL_AgentMasterToken(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
resolveFn := func(string) (acl.Authorizer, error) {
|
|
|
|
require.Fail(t, "should not have called delegate.ResolveToken")
|
|
|
|
return nil, fmt.Errorf("should not have called delegate.ResolveToken")
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), resolveFn)
|
2019-02-27 19:28:31 +00:00
|
|
|
a.loadTokens(a.config)
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := a.resolveToken("towel")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Nil(t, err)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.AgentRead(a.config.NodeName, nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.AgentWrite(a.config.NodeName, nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeRead("foobarbaz", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foobarbaz", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACL_RootAuthorizersDenied(t *testing.T) {
|
|
|
|
t.Parallel()
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
resolveFn := func(string) (acl.Authorizer, error) {
|
|
|
|
require.Fail(t, "should not have called delegate.ResolveToken")
|
|
|
|
return nil, fmt.Errorf("should not have called delegate.ResolveToken")
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), resolveFn)
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := a.resolveToken("deny")
|
|
|
|
require.Nil(t, authz)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
|
|
|
authz, err = a.resolveToken("allow")
|
|
|
|
require.Nil(t, authz)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
|
|
|
authz, err = a.resolveToken("manage")
|
|
|
|
require.Nil(t, authz)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
|
|
|
}
|
|
|
|
|
2019-12-06 19:01:34 +00:00
|
|
|
func authzFromPolicy(policy *acl.Policy, cfg *acl.EnterpriseACLConfig) (acl.Authorizer, error) {
|
|
|
|
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
|
|
|
|
|
|
|
// catalogPolicy supplies some standard policies to help with testing the
|
|
|
|
// catalog-related vet and filter functions.
|
2018-10-19 16:04:07 +00:00
|
|
|
func catalogPolicy(token string) (acl.Authorizer, error) {
|
|
|
|
switch token {
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
case "node-ro":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
NodePrefixes: []*acl.NodeRule{
|
|
|
|
&acl.NodeRule{Name: "Node", Policy: "read"},
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-06 19:01:34 +00:00
|
|
|
}, nil)
|
2016-12-15 03:28:09 +00:00
|
|
|
case "node-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
NodePrefixes: []*acl.NodeRule{
|
|
|
|
&acl.NodeRule{Name: "Node", Policy: "write"},
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-06 19:01:34 +00:00
|
|
|
}, nil)
|
2016-12-15 03:28:09 +00:00
|
|
|
case "service-ro":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
ServicePrefixes: []*acl.ServiceRule{
|
|
|
|
&acl.ServiceRule{Name: "service", Policy: "read"},
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-06 19:01:34 +00:00
|
|
|
}, nil)
|
2016-12-15 03:28:09 +00:00
|
|
|
case "service-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
ServicePrefixes: []*acl.ServiceRule{
|
|
|
|
&acl.ServiceRule{Name: "service", Policy: "write"},
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-06 19:01:34 +00:00
|
|
|
}, nil)
|
2016-12-15 03:28:09 +00:00
|
|
|
case "other-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
2019-10-15 20:58:50 +00:00
|
|
|
PolicyRules: acl.PolicyRules{
|
|
|
|
ServicePrefixes: []*acl.ServiceRule{
|
|
|
|
&acl.ServiceRule{Name: "other", Policy: "write"},
|
|
|
|
},
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2019-12-06 19:01:34 +00:00
|
|
|
}, nil)
|
2016-12-15 03:28:09 +00:00
|
|
|
default:
|
2018-10-19 16:04:07 +00:00
|
|
|
return nil, fmt.Errorf("unknown token %q", token)
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_vetServiceRegister(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new service, with permission.
|
2017-05-21 07:11:09 +00:00
|
|
|
err := a.vetServiceRegister("service-rw", &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.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetServiceRegister("service-ro", &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.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddService(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "other",
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetServiceRegister("service-rw", &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
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_vetServiceUpdate(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update a service that doesn't exist.
|
2019-12-10 02:26:41 +00:00
|
|
|
err := a.vetServiceUpdate("service-rw", 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.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddService(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
}, "")
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetServiceUpdate("service-rw", 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.
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetServiceUpdate("service-ro", 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
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_vetCheckRegister(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Register a new service check with write privs.
|
2017-05-21 07:11:09 +00:00
|
|
|
err := a.vetCheckRegister("service-rw", &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.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckRegister("service-ro", &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.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckRegister("node-rw", &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.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckRegister("node-ro", &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.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddService(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
}, "")
|
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",
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckRegister("service-rw", &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"),
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckRegister("service-rw", &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
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_vetCheckUpdate(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update a check that doesn't exist.
|
2019-12-10 02:26:41 +00:00
|
|
|
err := a.vetCheckUpdate("node-rw", 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.
|
2017-08-28 12:17:13 +00:00
|
|
|
a.State.AddService(&structs.NodeService{
|
2016-12-15 03:28:09 +00:00
|
|
|
ID: "my-service",
|
|
|
|
Service: "service",
|
|
|
|
}, "")
|
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",
|
|
|
|
}, "")
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetCheckUpdate("service-rw", 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.
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetCheckUpdate("service-ro", 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"),
|
|
|
|
}, "")
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetCheckUpdate("node-rw", 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.
|
2019-12-10 02:26:41 +00:00
|
|
|
err = a.vetCheckUpdate("node-ro", 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()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
var members []serf.Member
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterMembers("node-ro", &members))
|
|
|
|
require.Len(t, members, 0)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
members = []serf.Member{
|
|
|
|
serf.Member{Name: "Node 1"},
|
|
|
|
serf.Member{Name: "Nope"},
|
|
|
|
serf.Member{Name: "Node 2"},
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterMembers("node-ro", &members))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_filterServices(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
services := make(map[structs.ServiceID]*structs.NodeService)
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterServices("node-ro", &services))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
services[structs.NewServiceID("my-service", nil)] = &structs.NodeService{ID: "my-service", Service: "service"}
|
|
|
|
services[structs.NewServiceID("my-other", nil)] = &structs.NodeService{ID: "my-other", Service: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterServices("service-ro", &services))
|
2019-12-10 02:26:41 +00:00
|
|
|
require.Contains(t, services, structs.NewServiceID("my-service", nil))
|
|
|
|
require.NotContains(t, services, structs.NewServiceID("my-other", nil))
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_filterChecks(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-12-06 19:01:34 +00:00
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
checks := make(map[structs.CheckID]*structs.HealthCheck)
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterChecks("node-ro", &checks))
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
checks[structs.NewCheckID("my-node", nil)] = &structs.HealthCheck{}
|
|
|
|
checks[structs.NewCheckID("my-service", nil)] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks[structs.NewCheckID("my-other", nil)] = &structs.HealthCheck{ServiceName: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterChecks("service-ro", &checks))
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok := checks[structs.NewCheckID("my-node", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok = checks[structs.NewCheckID("my-service", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, ok)
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok = checks[structs.NewCheckID("my-other", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
checks[structs.NewCheckID("my-node", nil)] = &structs.HealthCheck{}
|
|
|
|
checks[structs.NewCheckID("my-service", nil)] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks[structs.NewCheckID("my-other", nil)] = &structs.HealthCheck{ServiceName: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterChecks("node-ro", &checks))
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok = checks[structs.NewCheckID("my-node", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, ok)
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok = checks[structs.NewCheckID("my-service", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2019-12-10 02:26:41 +00:00
|
|
|
_, ok = checks[structs.NewCheckID("my-other", nil)]
|
2018-10-19 16:04:07 +00:00
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|