2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2017-09-25 18:40:42 +00:00
|
|
|
"os"
|
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"
|
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
|
|
|
|
func NewTestACLAgent(name string, hcl string, resolveFn func(string) (acl.Authorizer, error)) *TestACLAgent {
|
|
|
|
a := &TestACLAgent{Name: name, HCL: hcl, resolveTokenFn: resolveFn}
|
|
|
|
hclDataDir := `data_dir = "acl-agent"`
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a.Config = TestConfig(
|
|
|
|
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-06-27 20:22:07 +00:00
|
|
|
agent, err := New(a.Config, nil)
|
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
|
|
|
logOutput := a.LogOutput
|
|
|
|
if logOutput == nil {
|
|
|
|
logOutput = os.Stderr
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
agent.LogOutput = logOutput
|
|
|
|
agent.LogWriter = a.LogWriter
|
|
|
|
agent.logger = log.New(logOutput, a.Name+" - ", log.LstdFlags|log.Lmicroseconds)
|
|
|
|
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
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig()+`
|
|
|
|
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
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig()+`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(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
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
require.True(t, authz.AgentRead(a.config.NodeName))
|
|
|
|
require.True(t, authz.AgentWrite(a.config.NodeName))
|
|
|
|
require.True(t, authz.NodeRead("foobarbaz"))
|
|
|
|
require.False(t, authz.NodeWrite("foobarbaz", nil))
|
|
|
|
}
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig(), resolveFn)
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
func authzFromPolicy(policy *acl.Policy) (acl.Authorizer, error) {
|
|
|
|
return acl.NewPolicyAuthorizer(acl.DenyAll(), []*acl.Policy{policy}, nil)
|
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{
|
|
|
|
NodePrefixes: []*acl.NodePolicy{
|
|
|
|
&acl.NodePolicy{Name: "Node", Policy: "read"},
|
|
|
|
},
|
|
|
|
})
|
2016-12-15 03:28:09 +00:00
|
|
|
case "node-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
|
|
|
NodePrefixes: []*acl.NodePolicy{
|
|
|
|
&acl.NodePolicy{Name: "Node", Policy: "write"},
|
|
|
|
},
|
|
|
|
})
|
2016-12-15 03:28:09 +00:00
|
|
|
case "service-ro":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
|
|
|
ServicePrefixes: []*acl.ServicePolicy{
|
|
|
|
&acl.ServicePolicy{Name: "service", Policy: "read"},
|
|
|
|
},
|
|
|
|
})
|
2016-12-15 03:28:09 +00:00
|
|
|
case "service-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
|
|
|
ServicePrefixes: []*acl.ServicePolicy{
|
|
|
|
&acl.ServicePolicy{Name: "service", Policy: "write"},
|
|
|
|
},
|
|
|
|
})
|
2016-12-15 03:28:09 +00:00
|
|
|
case "other-rw":
|
2018-10-19 16:04:07 +00:00
|
|
|
return authzFromPolicy(&acl.Policy{
|
|
|
|
ServicePrefixes: []*acl.ServicePolicy{
|
|
|
|
&acl.ServicePolicy{Name: "other", Policy: "write"},
|
|
|
|
},
|
|
|
|
})
|
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()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(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()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update a service that doesn't exist.
|
2017-05-21 07:11:09 +00:00
|
|
|
err := a.vetServiceUpdate("service-rw", "my-service")
|
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",
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetServiceUpdate("service-rw", "my-service")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update without write privs.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetServiceUpdate("service-ro", "my-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_vetCheckRegister(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(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()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update a check that doesn't exist.
|
2017-05-21 07:11:09 +00:00
|
|
|
err := a.vetCheckUpdate("node-rw", "my-check")
|
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",
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckUpdate("service-rw", "my-service-check")
|
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.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckUpdate("service-ro", "my-service-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
|
|
|
|
|
|
|
// 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"),
|
|
|
|
}, "")
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckUpdate("node-rw", "my-node-check")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
// Update without write privs.
|
2017-05-21 07:11:09 +00:00
|
|
|
err = a.vetCheckUpdate("node-ro", "my-node-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
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_filterMembers(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(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()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
services := make(map[string]*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
|
|
|
|
|
|
|
services["my-service"] = &structs.NodeService{ID: "my-service", Service: "service"}
|
|
|
|
services["my-other"] = &structs.NodeService{ID: "my-other", Service: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterServices("service-ro", &services))
|
|
|
|
require.Contains(t, services, "my-service")
|
|
|
|
require.NotContains(t, services, "my-other")
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestACL_filterChecks(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
a := NewTestACLAgent(t.Name(), TestACLConfig(), catalogPolicy)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
checks := make(map[types.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
|
|
|
|
|
|
|
checks["my-node"] = &structs.HealthCheck{}
|
|
|
|
checks["my-service"] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks["my-other"] = &structs.HealthCheck{ServiceName: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterChecks("service-ro", &checks))
|
|
|
|
fmt.Printf("filtered: %#v", checks)
|
|
|
|
_, ok := checks["my-node"]
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = checks["my-service"]
|
|
|
|
require.True(t, ok)
|
|
|
|
_, ok = checks["my-other"]
|
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
|
|
|
|
checks["my-node"] = &structs.HealthCheck{}
|
|
|
|
checks["my-service"] = &structs.HealthCheck{ServiceName: "service"}
|
|
|
|
checks["my-other"] = &structs.HealthCheck{ServiceName: "other"}
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, a.filterChecks("node-ro", &checks))
|
|
|
|
_, ok = checks["my-node"]
|
|
|
|
require.True(t, ok)
|
|
|
|
_, ok = checks["my-service"]
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = checks["my-other"]
|
|
|
|
require.False(t, ok)
|
2016-12-15 03:28:09 +00:00
|
|
|
}
|