2016-12-14 07:21:14 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
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"
|
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.
|
|
|
|
// 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()
|
|
|
|
|
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)
|
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
|
|
|
}
|
2020-08-08 01:08:43 +00:00
|
|
|
bd, err := NewBaseDeps(loader, logBuffer)
|
2020-05-13 17:00:08 +00:00
|
|
|
require.NoError(t, err)
|
2016-12-14 07:21:14 +00:00
|
|
|
|
2020-08-08 01:08:43 +00:00
|
|
|
bd.Logger = hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Name: name,
|
|
|
|
Level: hclog.Debug,
|
|
|
|
Output: logBuffer,
|
|
|
|
TimeFormat: "04:05.000",
|
|
|
|
})
|
2020-08-17 18:12:04 +00:00
|
|
|
bd.MetricsHandler = 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
|
|
|
|
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) {
|
2020-05-13 17:00:08 +00:00
|
|
|
if a.resolveAuthzFn == nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TestACLAgent) ResolveTokenToIdentityAndAuthorizer(secretID string) (structs.ACLIdentity, acl.Authorizer, error) {
|
2020-05-13 17:00:08 +00:00
|
|
|
if a.resolveAuthzFn == nil {
|
|
|
|
return nil, nil, fmt.Errorf("ResolveTokenToIdentityAndAuthorizer call is unexpected - no authz resolver callback set")
|
2019-12-18 18:46:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
return a.resolveAuthzFn(secretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TestACLAgent) ResolveTokenToIdentity(secretID string) (structs.ACLIdentity, error) {
|
|
|
|
if a.resolveIdentFn == nil {
|
|
|
|
return nil, fmt.Errorf("ResolveTokenToIdentity call is unexpected - no ident resolver callback set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.resolveIdentFn(secretID)
|
2016-12-14 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
func (a *TestACLAgent) ResolveTokenAndDefaultMeta(secretID string, entMeta *structs.EnterpriseMeta, authzContext *acl.AuthorizerContext) (acl.Authorizer, error) {
|
|
|
|
identity, authz, err := a.ResolveTokenToIdentityAndAuthorizer(secretID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
return authz, err
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
2019-12-18 18:46:53 +00:00
|
|
|
type testToken struct {
|
|
|
|
token structs.ACLToken
|
|
|
|
// yes the rules can exist on the token itself but that is legacy behavior
|
|
|
|
// that I would prefer these tests not rely on
|
|
|
|
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"
|
|
|
|
|
|
|
|
testTokens = map[string]testToken{
|
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
|
|
|
)
|
|
|
|
|
|
|
|
func catalogPolicy(token string) (structs.ACLIdentity, acl.Authorizer, error) {
|
|
|
|
tok, ok := testTokens[token]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := acl.NewPolicyFromSource("", 0, tok.rules, acl.SyntaxCurrent, nil, nil)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
func catalogIdent(token string) (structs.ACLIdentity, error) {
|
|
|
|
tok, ok := testTokens[token]
|
|
|
|
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.
|
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",
|
|
|
|
}, "")
|
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.
|
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",
|
|
|
|
}, "")
|
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.
|
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",
|
|
|
|
}, "")
|
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"),
|
|
|
|
}, "")
|
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.
|
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",
|
|
|
|
}, "")
|
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"),
|
|
|
|
}, "")
|
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-07-30 17:08:50 +00:00
|
|
|
filterServices := func(token string, services *map[structs.ServiceID]*structs.NodeService) error {
|
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.filterServicesWithAuthorizer(authz, services)
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
services := make(map[structs.ServiceID]*structs.NodeService)
|
2021-07-30 17:08:50 +00:00
|
|
|
require.NoError(t, filterServices(nodeROSecret, &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"}
|
2021-07-30 17:08:50 +00:00
|
|
|
require.NoError(t, filterServices(serviceROSecret, &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
|
|
|
}
|
|
|
|
|
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-07-30 17:08:50 +00:00
|
|
|
filterChecks := func(token string, checks *map[structs.CheckID]*structs.HealthCheck) error {
|
|
|
|
authz, err := a.delegate.ResolveTokenAndDefaultMeta(token, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.filterChecksWithAuthorizer(authz, checks)
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
checks := make(map[structs.CheckID]*structs.HealthCheck)
|
2021-07-30 17:08:50 +00:00
|
|
|
require.NoError(t, filterChecks(nodeROSecret, &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"}
|
2021-07-30 17:08:50 +00:00
|
|
|
require.NoError(t, filterChecks(serviceROSecret, &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"}
|
2021-07-30 17:08:50 +00:00
|
|
|
require.NoError(t, filterChecks(nodeROSecret, &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
|
|
|
}
|
2020-05-13 17:00:08 +00:00
|
|
|
|
2020-11-17 22:10:21 +00:00
|
|
|
// TODO: remove?
|
2020-05-13 17:00:08 +00:00
|
|
|
func TestACL_ResolveIdentity(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestACLAgent(t, t.Name(), TestACLConfig(), nil, catalogIdent)
|
|
|
|
|
|
|
|
// this test is meant to ensure we are calling the correct function
|
|
|
|
// which is ResolveTokenToIdentity on the Agent delegate. Our
|
|
|
|
// nil authz resolver will cause it to emit an error if used
|
2020-11-17 22:10:21 +00:00
|
|
|
ident, err := a.delegate.ResolveTokenToIdentity(nodeROSecret)
|
2020-05-13 17:00:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, ident)
|
|
|
|
|
|
|
|
// just double checkingto ensure if we had used the wrong function
|
|
|
|
// that an error would be produced
|
2021-04-14 16:39:35 +00:00
|
|
|
_, err = a.delegate.ResolveTokenAndDefaultMeta(nodeROSecret, nil, nil)
|
2020-05-13 17:00:08 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
}
|