diff --git a/agent/cache-types/connect_ca.go b/agent/cache-types/connect_ca_leaf.go similarity index 84% rename from agent/cache-types/connect_ca.go rename to agent/cache-types/connect_ca_leaf.go index 22549ed498..d90bc19bb5 100644 --- a/agent/cache-types/connect_ca.go +++ b/agent/cache-types/connect_ca_leaf.go @@ -15,42 +15,7 @@ import ( ) // Recommended name for registration. -const ( - ConnectCARootName = "connect-ca-root" - ConnectCALeafName = "connect-ca-leaf" -) - -// ConnectCARoot supports fetching the Connect CA roots. This is a -// straightforward cache type since it only has to block on the given -// index and return the data. -type ConnectCARoot struct { - RPC RPC -} - -func (c *ConnectCARoot) Fetch(opts cache.FetchOptions, req cache.Request) (cache.FetchResult, error) { - var result cache.FetchResult - - // The request should be a DCSpecificRequest. - reqReal, ok := req.(*structs.DCSpecificRequest) - if !ok { - return result, fmt.Errorf( - "Internal cache failure: request wrong type: %T", req) - } - - // Set the minimum query index to our current index so we block - reqReal.QueryOptions.MinQueryIndex = opts.MinIndex - reqReal.QueryOptions.MaxQueryTime = opts.Timeout - - // Fetch - var reply structs.IndexedCARoots - if err := c.RPC.RPC("ConnectCA.Roots", reqReal, &reply); err != nil { - return result, err - } - - result.Value = &reply - result.Index = reply.QueryMeta.Index - return result, nil -} +const ConnectCALeafName = "connect-ca-leaf" // ConnectCALeaf supports fetching and generating Connect leaf // certificates. diff --git a/agent/cache-types/connect_ca_test.go b/agent/cache-types/connect_ca_leaf_test.go similarity index 82% rename from agent/cache-types/connect_ca_test.go rename to agent/cache-types/connect_ca_leaf_test.go index 43953e7f8b..0612aed21e 100644 --- a/agent/cache-types/connect_ca_test.go +++ b/agent/cache-types/connect_ca_leaf_test.go @@ -12,52 +12,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestConnectCARoot(t *testing.T) { - require := require.New(t) - rpc := TestRPC(t) - defer rpc.AssertExpectations(t) - typ := &ConnectCARoot{RPC: rpc} - - // Expect the proper RPC call. This also sets the expected value - // since that is return-by-pointer in the arguments. - var resp *structs.IndexedCARoots - rpc.On("RPC", "ConnectCA.Roots", mock.Anything, mock.Anything).Return(nil). - Run(func(args mock.Arguments) { - req := args.Get(1).(*structs.DCSpecificRequest) - require.Equal(uint64(24), req.QueryOptions.MinQueryIndex) - require.Equal(1*time.Second, req.QueryOptions.MaxQueryTime) - - reply := args.Get(2).(*structs.IndexedCARoots) - reply.QueryMeta.Index = 48 - resp = reply - }) - - // Fetch - result, err := typ.Fetch(cache.FetchOptions{ - MinIndex: 24, - Timeout: 1 * time.Second, - }, &structs.DCSpecificRequest{Datacenter: "dc1"}) - require.Nil(err) - require.Equal(cache.FetchResult{ - Value: resp, - Index: 48, - }, result) -} - -func TestConnectCARoot_badReqType(t *testing.T) { - require := require.New(t) - rpc := TestRPC(t) - defer rpc.AssertExpectations(t) - typ := &ConnectCARoot{RPC: rpc} - - // Fetch - _, err := typ.Fetch(cache.FetchOptions{}, cache.TestRequest( - t, cache.RequestInfo{Key: "foo", MinIndex: 64})) - require.NotNil(err) - require.Contains(err.Error(), "wrong type") - -} - // Test that after an initial signing, new CA roots (new ID) will // trigger a blocking query to execute. func TestConnectCALeaf_changingRoots(t *testing.T) { diff --git a/agent/cache-types/connect_ca_root.go b/agent/cache-types/connect_ca_root.go new file mode 100644 index 0000000000..036cf53d22 --- /dev/null +++ b/agent/cache-types/connect_ca_root.go @@ -0,0 +1,43 @@ +package cachetype + +import ( + "fmt" + + "github.com/hashicorp/consul/agent/cache" + "github.com/hashicorp/consul/agent/structs" +) + +// Recommended name for registration. +const ConnectCARootName = "connect-ca-root" + +// ConnectCARoot supports fetching the Connect CA roots. This is a +// straightforward cache type since it only has to block on the given +// index and return the data. +type ConnectCARoot struct { + RPC RPC +} + +func (c *ConnectCARoot) Fetch(opts cache.FetchOptions, req cache.Request) (cache.FetchResult, error) { + var result cache.FetchResult + + // The request should be a DCSpecificRequest. + reqReal, ok := req.(*structs.DCSpecificRequest) + if !ok { + return result, fmt.Errorf( + "Internal cache failure: request wrong type: %T", req) + } + + // Set the minimum query index to our current index so we block + reqReal.QueryOptions.MinQueryIndex = opts.MinIndex + reqReal.QueryOptions.MaxQueryTime = opts.Timeout + + // Fetch + var reply structs.IndexedCARoots + if err := c.RPC.RPC("ConnectCA.Roots", reqReal, &reply); err != nil { + return result, err + } + + result.Value = &reply + result.Index = reply.QueryMeta.Index + return result, nil +} diff --git a/agent/cache-types/connect_ca_root_test.go b/agent/cache-types/connect_ca_root_test.go new file mode 100644 index 0000000000..24c37f3139 --- /dev/null +++ b/agent/cache-types/connect_ca_root_test.go @@ -0,0 +1,57 @@ +package cachetype + +import ( + "testing" + "time" + + "github.com/hashicorp/consul/agent/cache" + "github.com/hashicorp/consul/agent/structs" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" +) + +func TestConnectCARoot(t *testing.T) { + require := require.New(t) + rpc := TestRPC(t) + defer rpc.AssertExpectations(t) + typ := &ConnectCARoot{RPC: rpc} + + // Expect the proper RPC call. This also sets the expected value + // since that is return-by-pointer in the arguments. + var resp *structs.IndexedCARoots + rpc.On("RPC", "ConnectCA.Roots", mock.Anything, mock.Anything).Return(nil). + Run(func(args mock.Arguments) { + req := args.Get(1).(*structs.DCSpecificRequest) + require.Equal(uint64(24), req.QueryOptions.MinQueryIndex) + require.Equal(1*time.Second, req.QueryOptions.MaxQueryTime) + + reply := args.Get(2).(*structs.IndexedCARoots) + reply.QueryMeta.Index = 48 + resp = reply + }) + + // Fetch + result, err := typ.Fetch(cache.FetchOptions{ + MinIndex: 24, + Timeout: 1 * time.Second, + }, &structs.DCSpecificRequest{Datacenter: "dc1"}) + require.Nil(err) + require.Equal(cache.FetchResult{ + Value: resp, + Index: 48, + }, result) +} + +func TestConnectCARoot_badReqType(t *testing.T) { + require := require.New(t) + rpc := TestRPC(t) + defer rpc.AssertExpectations(t) + typ := &ConnectCARoot{RPC: rpc} + + // Fetch + _, err := typ.Fetch(cache.FetchOptions{}, cache.TestRequest( + t, cache.RequestInfo{Key: "foo", MinIndex: 64})) + require.NotNil(err) + require.Contains(err.Error(), "wrong type") + +}