2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-01-02 15:03:05 +00:00
|
|
|
"sync"
|
2019-12-06 20:46:46 +00:00
|
|
|
"testing"
|
2020-01-02 15:03:05 +00:00
|
|
|
"time"
|
2019-12-06 20:46:46 +00:00
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-12-06 20:46:46 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/consul"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-01-02 15:03:05 +00:00
|
|
|
"golang.org/x/time/rate"
|
2019-12-06 20:46:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ ConsulACLsAPI = (*consulACLsAPI)(nil)
|
|
|
|
var _ ConsulACLsAPI = (*mockConsulACLsAPI)(nil)
|
2020-07-28 20:12:08 +00:00
|
|
|
var _ ConsulConfigsAPI = (*consulConfigsAPI)(nil)
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
func TestConsulConfigsAPI_SetCE(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
try := func(t *testing.T, expect error, f func(ConsulConfigsAPI) error) {
|
2020-07-28 20:12:08 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2020-12-15 20:38:33 +00:00
|
|
|
configsAPI := consul.NewMockConfigsAPI(logger)
|
|
|
|
configsAPI.SetError(expect)
|
2020-07-28 20:12:08 +00:00
|
|
|
|
|
|
|
c := NewConsulConfigsAPI(configsAPI, logger)
|
2020-12-15 20:38:33 +00:00
|
|
|
err := f(c) // set the config entry
|
2020-07-28 20:12:08 +00:00
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
switch expect {
|
|
|
|
case nil:
|
2020-07-28 20:12:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-15 20:38:33 +00:00
|
|
|
default:
|
|
|
|
require.Equal(t, expect, err)
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-04-19 17:29:36 +00:00
|
|
|
// existing behavior is no set namespace
|
|
|
|
consulNamespace := ""
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
ingressCE := new(structs.ConsulIngressConfigEntry)
|
|
|
|
t.Run("ingress ok", func(t *testing.T) {
|
|
|
|
try(t, nil, func(c ConsulConfigsAPI) error {
|
2021-04-19 17:29:36 +00:00
|
|
|
return c.SetIngressCE(ctx, consulNamespace, "ig", ingressCE)
|
2020-12-15 20:38:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ingress fail", func(t *testing.T) {
|
|
|
|
try(t, errors.New("consul broke"), func(c ConsulConfigsAPI) error {
|
2021-04-19 17:29:36 +00:00
|
|
|
return c.SetIngressCE(ctx, consulNamespace, "ig", ingressCE)
|
2020-12-15 20:38:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
terminatingCE := new(structs.ConsulTerminatingConfigEntry)
|
|
|
|
t.Run("terminating ok", func(t *testing.T) {
|
|
|
|
try(t, nil, func(c ConsulConfigsAPI) error {
|
2021-04-19 17:29:36 +00:00
|
|
|
return c.SetTerminatingCE(ctx, consulNamespace, "tg", terminatingCE)
|
2020-12-15 20:38:33 +00:00
|
|
|
})
|
2020-07-28 20:12:08 +00:00
|
|
|
})
|
|
|
|
|
2020-12-15 20:38:33 +00:00
|
|
|
t.Run("terminating fail", func(t *testing.T) {
|
|
|
|
try(t, errors.New("consul broke"), func(c ConsulConfigsAPI) error {
|
2021-04-19 17:29:36 +00:00
|
|
|
return c.SetTerminatingCE(ctx, consulNamespace, "tg", terminatingCE)
|
2020-12-15 20:38:33 +00:00
|
|
|
})
|
2020-07-28 20:12:08 +00:00
|
|
|
})
|
2020-12-15 20:38:33 +00:00
|
|
|
|
|
|
|
// also mesh
|
2020-07-28 20:12:08 +00:00
|
|
|
}
|
2019-12-06 20:46:46 +00:00
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
type revokeRequest struct {
|
|
|
|
accessorID string
|
|
|
|
committed bool
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
type mockConsulACLsAPI struct {
|
2020-01-02 15:03:05 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
revokeRequests []revokeRequest
|
|
|
|
stopped bool
|
2019-12-06 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
func (m *mockConsulACLsAPI) CheckPermissions(context.Context, string, *structs.ConsulUsage, string) error {
|
2019-12-06 20:46:46 +00:00
|
|
|
panic("not implemented yet")
|
|
|
|
}
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
func (m *mockConsulACLsAPI) CreateToken(context.Context, ServiceIdentityRequest) (*structs.SIToken, error) {
|
2019-12-06 20:46:46 +00:00
|
|
|
panic("not implemented yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConsulACLsAPI) ListTokens() ([]string, error) {
|
|
|
|
panic("not implemented yet")
|
|
|
|
}
|
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
func (m *mockConsulACLsAPI) Stop() {
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.stopped = true
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockPurgingServer struct {
|
|
|
|
purgedAccessorIDs []string
|
|
|
|
failure error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mps *mockPurgingServer) purgeFunc(accessors []*structs.SITokenAccessor) error {
|
|
|
|
if mps.failure != nil {
|
|
|
|
return mps.failure
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
for _, accessor := range accessors {
|
2020-01-02 15:03:05 +00:00
|
|
|
mps.purgedAccessorIDs = append(mps.purgedAccessorIDs, accessor.AccessorID)
|
2019-12-06 20:46:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
func (m *mockConsulACLsAPI) RevokeTokens(_ context.Context, accessors []*structs.SITokenAccessor, committed bool) bool {
|
2020-05-21 12:18:12 +00:00
|
|
|
return m.storeForRevocation(accessors, committed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConsulACLsAPI) MarkForRevocation(accessors []*structs.SITokenAccessor) {
|
|
|
|
m.storeForRevocation(accessors, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockConsulACLsAPI) storeForRevocation(accessors []*structs.SITokenAccessor, committed bool) bool {
|
2020-01-02 15:03:05 +00:00
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
|
|
|
for _, accessor := range accessors {
|
|
|
|
m.revokeRequests = append(m.revokeRequests, revokeRequest{
|
|
|
|
accessorID: accessor.AccessorID,
|
|
|
|
committed: committed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
func TestConsulACLsAPI_CreateToken(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-06 20:46:46 +00:00
|
|
|
|
|
|
|
try := func(t *testing.T, expErr error) {
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
aclAPI := consul.NewMockACLsAPI(logger)
|
|
|
|
aclAPI.SetError(expErr)
|
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
c := NewConsulACLsAPI(aclAPI, logger, nil)
|
2019-12-06 20:46:46 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-05-18 19:21:12 +00:00
|
|
|
sii := ServiceIdentityRequest{
|
2021-03-16 18:22:21 +00:00
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
ClusterID: uuid.Generate(),
|
|
|
|
TaskName: "my-task1-sidecar-proxy",
|
|
|
|
TaskKind: structs.NewTaskKind(structs.ConnectProxyPrefix, "my-service"),
|
2019-12-06 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token, err := c.CreateToken(ctx, sii)
|
|
|
|
|
|
|
|
if expErr != nil {
|
|
|
|
require.Equal(t, expErr, err)
|
|
|
|
require.Nil(t, token)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2021-03-16 18:22:21 +00:00
|
|
|
require.Equal(t, "foo-namespace", token.ConsulNamespace)
|
2020-05-18 19:21:12 +00:00
|
|
|
require.Equal(t, "my-task1-sidecar-proxy", token.TaskName)
|
2019-12-06 20:46:46 +00:00
|
|
|
require.True(t, helper.IsUUID(token.AccessorID))
|
|
|
|
require.True(t, helper.IsUUID(token.SecretID))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("create token success", func(t *testing.T) {
|
|
|
|
try(t, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create token error", func(t *testing.T) {
|
|
|
|
try(t, errors.New("consul broke"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulACLsAPI_RevokeTokens(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-06 20:46:46 +00:00
|
|
|
|
|
|
|
setup := func(t *testing.T, exp error) (context.Context, ConsulACLsAPI, *structs.SIToken) {
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
aclAPI := consul.NewMockACLsAPI(logger)
|
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
c := NewConsulACLsAPI(aclAPI, logger, nil)
|
2019-12-06 20:46:46 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2020-05-18 19:21:12 +00:00
|
|
|
generated, err := c.CreateToken(ctx, ServiceIdentityRequest{
|
2021-03-16 18:22:21 +00:00
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
ClusterID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
TaskName: "task1-sidecar-proxy",
|
|
|
|
TaskKind: structs.NewTaskKind(structs.ConnectProxyPrefix, "service1"),
|
2019-12-06 20:46:46 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// set the mock error after calling CreateToken for setting up
|
|
|
|
aclAPI.SetError(exp)
|
|
|
|
|
|
|
|
return context.Background(), c, generated
|
|
|
|
}
|
|
|
|
|
|
|
|
accessors := func(ids ...string) (result []*structs.SITokenAccessor) {
|
|
|
|
for _, id := range ids {
|
2021-03-16 18:22:21 +00:00
|
|
|
result = append(result, &structs.SITokenAccessor{
|
|
|
|
AccessorID: id,
|
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
})
|
2019-12-06 20:46:46 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("revoke token success", func(t *testing.T) {
|
|
|
|
ctx, c, token := setup(t, nil)
|
2020-01-02 15:03:05 +00:00
|
|
|
retryLater := c.RevokeTokens(ctx, accessors(token.AccessorID), false)
|
|
|
|
require.False(t, retryLater)
|
2019-12-06 20:46:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("revoke token non-existent", func(t *testing.T) {
|
|
|
|
ctx, c, _ := setup(t, nil)
|
2020-01-02 15:03:05 +00:00
|
|
|
retryLater := c.RevokeTokens(ctx, accessors(uuid.Generate()), false)
|
|
|
|
require.False(t, retryLater)
|
2019-12-06 20:46:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("revoke token error", func(t *testing.T) {
|
|
|
|
exp := errors.New("consul broke")
|
|
|
|
ctx, c, token := setup(t, exp)
|
2020-01-02 15:03:05 +00:00
|
|
|
retryLater := c.RevokeTokens(ctx, accessors(token.AccessorID), false)
|
|
|
|
require.True(t, retryLater)
|
2019-12-06 20:46:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:18:12 +00:00
|
|
|
func TestConsulACLsAPI_MarkForRevocation(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-05-21 12:18:12 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
aclAPI := consul.NewMockACLsAPI(logger)
|
|
|
|
|
|
|
|
c := NewConsulACLsAPI(aclAPI, logger, nil)
|
|
|
|
|
|
|
|
generated, err := c.CreateToken(context.Background(), ServiceIdentityRequest{
|
2021-03-16 18:22:21 +00:00
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
ClusterID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
TaskName: "task1-sidecar-proxy",
|
|
|
|
TaskKind: structs.NewTaskKind(structs.ConnectProxyPrefix, "service1"),
|
2020-05-21 12:18:12 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// set the mock error after calling CreateToken for setting up
|
|
|
|
aclAPI.SetError(nil)
|
|
|
|
|
2021-03-16 18:22:21 +00:00
|
|
|
accessors := []*structs.SITokenAccessor{{
|
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
AccessorID: generated.AccessorID,
|
|
|
|
}}
|
2020-05-21 12:18:12 +00:00
|
|
|
c.MarkForRevocation(accessors)
|
|
|
|
require.Len(t, c.bgRetryRevocation, 1)
|
|
|
|
require.Contains(t, c.bgRetryRevocation, accessors[0])
|
|
|
|
}
|
|
|
|
|
2020-01-02 15:03:05 +00:00
|
|
|
func TestConsulACLsAPI_bgRetryRevoke(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-02 15:03:05 +00:00
|
|
|
|
|
|
|
// manually create so the bg daemon does not run, letting us explicitly
|
|
|
|
// call and test bgRetryRevoke
|
|
|
|
setup := func(t *testing.T) (*consulACLsAPI, *mockPurgingServer) {
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
aclAPI := consul.NewMockACLsAPI(logger)
|
|
|
|
server := new(mockPurgingServer)
|
|
|
|
shortWait := rate.Limit(1 * time.Millisecond)
|
|
|
|
|
|
|
|
return &consulACLsAPI{
|
|
|
|
aclClient: aclAPI,
|
|
|
|
purgeFunc: server.purgeFunc,
|
|
|
|
limiter: rate.NewLimiter(shortWait, int(shortWait)),
|
|
|
|
stopC: make(chan struct{}),
|
|
|
|
logger: logger,
|
|
|
|
}, server
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("retry revoke no items", func(t *testing.T) {
|
|
|
|
c, server := setup(t)
|
|
|
|
c.bgRetryRevoke()
|
|
|
|
require.Empty(t, server)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("retry revoke success", func(t *testing.T) {
|
|
|
|
c, server := setup(t)
|
|
|
|
accessorID := uuid.Generate()
|
|
|
|
c.bgRetryRevocation = append(c.bgRetryRevocation, &structs.SITokenAccessor{
|
2021-03-16 18:22:21 +00:00
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
AccessorID: accessorID,
|
|
|
|
TaskName: "task1",
|
2020-01-02 15:03:05 +00:00
|
|
|
})
|
|
|
|
require.Empty(t, server.purgedAccessorIDs)
|
|
|
|
c.bgRetryRevoke()
|
|
|
|
require.Equal(t, 1, len(server.purgedAccessorIDs))
|
|
|
|
require.Equal(t, accessorID, server.purgedAccessorIDs[0])
|
|
|
|
require.Empty(t, c.bgRetryRevocation) // should be empty now
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("retry revoke failure", func(t *testing.T) {
|
|
|
|
c, server := setup(t)
|
|
|
|
server.failure = errors.New("revocation fail")
|
|
|
|
accessorID := uuid.Generate()
|
|
|
|
c.bgRetryRevocation = append(c.bgRetryRevocation, &structs.SITokenAccessor{
|
2021-03-16 18:22:21 +00:00
|
|
|
ConsulNamespace: "foo-namespace",
|
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
AccessorID: accessorID,
|
|
|
|
TaskName: "task1",
|
2020-01-02 15:03:05 +00:00
|
|
|
})
|
|
|
|
require.Empty(t, server.purgedAccessorIDs)
|
|
|
|
c.bgRetryRevoke()
|
|
|
|
require.Equal(t, 1, len(c.bgRetryRevocation)) // non-empty because purge failed
|
|
|
|
require.Equal(t, accessorID, c.bgRetryRevocation[0].AccessorID)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConsulACLsAPI_Stop(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-02 15:03:05 +00:00
|
|
|
|
|
|
|
setup := func(t *testing.T) *consulACLsAPI {
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
return NewConsulACLsAPI(nil, logger, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := setup(t)
|
|
|
|
c.Stop()
|
2020-05-18 19:21:12 +00:00
|
|
|
_, err := c.CreateToken(context.Background(), ServiceIdentityRequest{
|
2020-01-02 15:03:05 +00:00
|
|
|
ClusterID: "",
|
|
|
|
AllocID: "",
|
|
|
|
TaskName: "",
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|