2022-04-05 14:26:14 +00:00
|
|
|
package connectca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-06-17 09:24:43 +00:00
|
|
|
resolver "github.com/hashicorp/consul/acl/resolver"
|
2022-04-05 14:26:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/grpc/public"
|
2022-03-27 10:59:30 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc/public/testutils"
|
2022-04-05 14:26:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/proto-public/pbconnectca"
|
2022-04-12 13:47:42 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2022-04-05 14:26:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const testACLToken = "acl-token"
|
|
|
|
|
2022-04-14 13:26:14 +00:00
|
|
|
func TestWatchRoots_ConnectDisabled(t *testing.T) {
|
|
|
|
server := NewServer(Config{ConnectEnabled: false})
|
|
|
|
|
|
|
|
// Begin the stream.
|
|
|
|
client := testClient(t, server)
|
2022-05-23 14:37:52 +00:00
|
|
|
stream, err := client.WatchRoots(context.Background(), &pbconnectca.WatchRootsRequest{})
|
2022-04-14 13:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleRootsStream(t, stream)
|
|
|
|
|
|
|
|
err = mustGetError(t, rspCh)
|
|
|
|
require.Equal(t, codes.FailedPrecondition.String(), status.Code(err).String())
|
|
|
|
require.Contains(t, status.Convert(err).Message(), "Connect")
|
|
|
|
}
|
|
|
|
|
2022-04-05 14:26:14 +00:00
|
|
|
func TestWatchRoots_Success(t *testing.T) {
|
2022-04-12 13:47:42 +00:00
|
|
|
fsm, publisher := setupFSMAndPublisher(t)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Set the initial roots and CA configuration.
|
|
|
|
rootA := connect.TestCA(t, nil)
|
2022-04-12 13:47:42 +00:00
|
|
|
_, err := fsm.GetStore().CARootSetCAS(1, 0, structs.CARoots{rootA})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-12 13:47:42 +00:00
|
|
|
err = fsm.GetStore().CASetConfig(0, &structs.CAConfiguration{ClusterID: "cluster-id"})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Mock the ACL Resolver to return an authorizer with `service:write`.
|
|
|
|
aclResolver := &MockACLResolver{}
|
|
|
|
aclResolver.On("ResolveTokenAndDefaultMeta", testACLToken, mock.Anything, mock.Anything).
|
2022-04-20 00:24:21 +00:00
|
|
|
Return(testutils.TestAuthorizerServiceWriteAny(t), nil)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
ctx := public.ContextWithToken(context.Background(), testACLToken)
|
|
|
|
|
|
|
|
server := NewServer(Config{
|
2022-04-14 13:26:14 +00:00
|
|
|
Publisher: publisher,
|
|
|
|
GetStore: func() StateStore { return fsm.GetStore() },
|
|
|
|
Logger: testutil.Logger(t),
|
|
|
|
ACLResolver: aclResolver,
|
|
|
|
ConnectEnabled: true,
|
2022-04-05 14:26:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Begin the stream.
|
|
|
|
client := testClient(t, server)
|
2022-05-23 14:37:52 +00:00
|
|
|
stream, err := client.WatchRoots(ctx, &pbconnectca.WatchRootsRequest{})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleRootsStream(t, stream)
|
|
|
|
|
|
|
|
// Expect an initial message containing current roots (provided by the snapshot).
|
|
|
|
roots := mustGetRoots(t, rspCh)
|
|
|
|
require.Equal(t, "cluster-id.consul", roots.TrustDomain)
|
|
|
|
require.Equal(t, rootA.ID, roots.ActiveRootId)
|
|
|
|
require.Len(t, roots.Roots, 1)
|
|
|
|
require.Equal(t, rootA.ID, roots.Roots[0].Id)
|
|
|
|
|
|
|
|
// Rotate the roots.
|
|
|
|
rootB := connect.TestCA(t, nil)
|
2022-04-12 13:47:42 +00:00
|
|
|
_, err = fsm.GetStore().CARootSetCAS(2, 1, structs.CARoots{rootB})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect another event containing the new roots.
|
|
|
|
roots = mustGetRoots(t, rspCh)
|
|
|
|
require.Equal(t, "cluster-id.consul", roots.TrustDomain)
|
|
|
|
require.Equal(t, rootB.ID, roots.ActiveRootId)
|
|
|
|
require.Len(t, roots.Roots, 1)
|
|
|
|
require.Equal(t, rootB.ID, roots.Roots[0].Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRoots_InvalidACLToken(t *testing.T) {
|
2022-04-12 13:47:42 +00:00
|
|
|
fsm, publisher := setupFSMAndPublisher(t)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Set the initial CA configuration.
|
2022-04-12 13:47:42 +00:00
|
|
|
err := fsm.GetStore().CASetConfig(0, &structs.CAConfiguration{ClusterID: "cluster-id"})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Mock the ACL resolver to return ErrNotFound.
|
|
|
|
aclResolver := &MockACLResolver{}
|
|
|
|
aclResolver.On("ResolveTokenAndDefaultMeta", mock.Anything, mock.Anything, mock.Anything).
|
2022-06-17 09:24:43 +00:00
|
|
|
Return(resolver.Result{}, acl.ErrNotFound)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
ctx := public.ContextWithToken(context.Background(), testACLToken)
|
|
|
|
|
|
|
|
server := NewServer(Config{
|
2022-04-14 13:26:14 +00:00
|
|
|
Publisher: publisher,
|
|
|
|
GetStore: func() StateStore { return fsm.GetStore() },
|
|
|
|
Logger: testutil.Logger(t),
|
|
|
|
ACLResolver: aclResolver,
|
|
|
|
ConnectEnabled: true,
|
2022-04-05 14:26:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Start the stream.
|
|
|
|
client := testClient(t, server)
|
2022-05-23 14:37:52 +00:00
|
|
|
stream, err := client.WatchRoots(ctx, &pbconnectca.WatchRootsRequest{})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleRootsStream(t, stream)
|
|
|
|
|
|
|
|
// Expect to get an Unauthenticated error immediately.
|
|
|
|
err = mustGetError(t, rspCh)
|
|
|
|
require.Equal(t, codes.Unauthenticated.String(), status.Code(err).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRoots_ACLTokenInvalidated(t *testing.T) {
|
2022-04-12 13:47:42 +00:00
|
|
|
fsm, publisher := setupFSMAndPublisher(t)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Set the initial roots and CA configuration.
|
|
|
|
rootA := connect.TestCA(t, nil)
|
2022-04-12 13:47:42 +00:00
|
|
|
_, err := fsm.GetStore().CARootSetCAS(1, 0, structs.CARoots{rootA})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-12 13:47:42 +00:00
|
|
|
err = fsm.GetStore().CASetConfig(2, &structs.CAConfiguration{ClusterID: "cluster-id"})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Mock the ACL Resolver to return an authorizer with `service:write` the
|
|
|
|
// first two times it is called (initial connect and first re-auth).
|
|
|
|
aclResolver := &MockACLResolver{}
|
|
|
|
aclResolver.On("ResolveTokenAndDefaultMeta", testACLToken, mock.Anything, mock.Anything).
|
2022-04-20 00:24:21 +00:00
|
|
|
Return(testutils.TestAuthorizerServiceWriteAny(t), nil).Twice()
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
ctx := public.ContextWithToken(context.Background(), testACLToken)
|
|
|
|
|
|
|
|
server := NewServer(Config{
|
2022-04-14 13:26:14 +00:00
|
|
|
Publisher: publisher,
|
|
|
|
GetStore: func() StateStore { return fsm.GetStore() },
|
|
|
|
Logger: testutil.Logger(t),
|
|
|
|
ACLResolver: aclResolver,
|
|
|
|
ConnectEnabled: true,
|
2022-04-05 14:26:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Start the stream.
|
|
|
|
client := testClient(t, server)
|
2022-05-23 14:37:52 +00:00
|
|
|
stream, err := client.WatchRoots(ctx, &pbconnectca.WatchRootsRequest{})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleRootsStream(t, stream)
|
|
|
|
|
|
|
|
// Consume the initial response.
|
|
|
|
mustGetRoots(t, rspCh)
|
|
|
|
|
|
|
|
// Update the ACL token to cause the subscription to be force-closed.
|
|
|
|
accessorID, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
2022-04-12 13:47:42 +00:00
|
|
|
err = fsm.GetStore().ACLTokenSet(1, &structs.ACLToken{
|
2022-04-05 14:26:14 +00:00
|
|
|
AccessorID: accessorID,
|
|
|
|
SecretID: testACLToken,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Update the roots.
|
|
|
|
rootB := connect.TestCA(t, nil)
|
2022-04-12 13:47:42 +00:00
|
|
|
_, err = fsm.GetStore().CARootSetCAS(3, 1, structs.CARoots{rootB})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect the stream to remain open and to receive the new roots.
|
|
|
|
mustGetRoots(t, rspCh)
|
|
|
|
|
|
|
|
// Simulate removing the `service:write` permission.
|
|
|
|
aclResolver.On("ResolveTokenAndDefaultMeta", testACLToken, mock.Anything, mock.Anything).
|
2022-06-17 09:24:43 +00:00
|
|
|
Return(testutils.TestAuthorizerDenyAll(t), nil)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Update the ACL token to cause the subscription to be force-closed.
|
2022-04-12 13:47:42 +00:00
|
|
|
err = fsm.GetStore().ACLTokenSet(1, &structs.ACLToken{
|
2022-04-05 14:26:14 +00:00
|
|
|
AccessorID: accessorID,
|
|
|
|
SecretID: testACLToken,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Expect the stream to be terminated.
|
|
|
|
err = mustGetError(t, rspCh)
|
|
|
|
require.Equal(t, codes.PermissionDenied.String(), status.Code(err).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRoots_StateStoreAbandoned(t *testing.T) {
|
2022-04-12 13:47:42 +00:00
|
|
|
fsm, publisher := setupFSMAndPublisher(t)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Set the initial roots and CA configuration.
|
|
|
|
rootA := connect.TestCA(t, nil)
|
2022-04-12 13:47:42 +00:00
|
|
|
_, err := fsm.GetStore().CARootSetCAS(1, 0, structs.CARoots{rootA})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-12 13:47:42 +00:00
|
|
|
err = fsm.GetStore().CASetConfig(0, &structs.CAConfiguration{ClusterID: "cluster-a"})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Mock the ACL Resolver to return an authorizer with `service:write`.
|
|
|
|
aclResolver := &MockACLResolver{}
|
|
|
|
aclResolver.On("ResolveTokenAndDefaultMeta", testACLToken, mock.Anything, mock.Anything).
|
2022-04-20 00:24:21 +00:00
|
|
|
Return(testutils.TestAuthorizerServiceWriteAny(t), nil)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
ctx := public.ContextWithToken(context.Background(), testACLToken)
|
|
|
|
|
|
|
|
server := NewServer(Config{
|
2022-04-14 13:26:14 +00:00
|
|
|
Publisher: publisher,
|
|
|
|
GetStore: func() StateStore { return fsm.GetStore() },
|
|
|
|
Logger: testutil.Logger(t),
|
|
|
|
ACLResolver: aclResolver,
|
|
|
|
ConnectEnabled: true,
|
2022-04-05 14:26:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Begin the stream.
|
|
|
|
client := testClient(t, server)
|
2022-05-23 14:37:52 +00:00
|
|
|
stream, err := client.WatchRoots(ctx, &pbconnectca.WatchRootsRequest{})
|
2022-04-05 14:26:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
rspCh := handleRootsStream(t, stream)
|
|
|
|
|
|
|
|
// Consume the initial roots.
|
|
|
|
mustGetRoots(t, rspCh)
|
|
|
|
|
|
|
|
// Simulate a snapshot restore.
|
2022-04-21 16:56:18 +00:00
|
|
|
storeB := testutils.TestStateStore(t, publisher)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
rootB := connect.TestCA(t, nil)
|
|
|
|
_, err = storeB.CARootSetCAS(1, 0, structs.CARoots{rootB})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = storeB.CASetConfig(0, &structs.CAConfiguration{ClusterID: "cluster-b"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-12 13:47:42 +00:00
|
|
|
fsm.ReplaceStore(storeB)
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Expect to get the new store's roots.
|
|
|
|
newRoots := mustGetRoots(t, rspCh)
|
|
|
|
require.Equal(t, "cluster-b.consul", newRoots.TrustDomain)
|
|
|
|
require.Len(t, newRoots.Roots, 1)
|
|
|
|
require.Equal(t, rootB.ID, newRoots.ActiveRootId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetRoots(t *testing.T, ch <-chan rootsOrError) *pbconnectca.WatchRootsResponse {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case rsp := <-ch:
|
|
|
|
require.NoError(t, rsp.err)
|
|
|
|
return rsp.rsp
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for WatchRootsResponse")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetError(t *testing.T, ch <-chan rootsOrError) error {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case rsp := <-ch:
|
|
|
|
require.Error(t, rsp.err)
|
|
|
|
return rsp.err
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for WatchRootsResponse")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleRootsStream(t *testing.T, stream pbconnectca.ConnectCAService_WatchRootsClient) <-chan rootsOrError {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
rspCh := make(chan rootsOrError)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
rsp, err := stream.Recv()
|
|
|
|
if errors.Is(err, io.EOF) ||
|
|
|
|
errors.Is(err, context.Canceled) ||
|
|
|
|
errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rspCh <- rootsOrError{
|
|
|
|
rsp: rsp,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return rspCh
|
|
|
|
}
|
|
|
|
|
|
|
|
type rootsOrError struct {
|
|
|
|
rsp *pbconnectca.WatchRootsResponse
|
|
|
|
err error
|
|
|
|
}
|