From fae0a8f8510a125e3bd150d7d511cf5dc33178e2 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 21 Jun 2021 19:22:01 -0400 Subject: [PATCH] ca: move generateCASignRequest to the delegate This method on Server was only used by the caDelegateWithState, so move it there until we can move it entirely into CAManager. --- agent/consul/leader_connect.go | 8 -------- agent/consul/leader_connect_ca.go | 8 ++++++++ agent/consul/leader_connect_ca_test.go | 15 ++++++++++++--- agent/consul/leader_connect_test.go | 8 -------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/agent/consul/leader_connect.go b/agent/consul/leader_connect.go index 894133b40..e25edf394 100644 --- a/agent/consul/leader_connect.go +++ b/agent/consul/leader_connect.go @@ -181,11 +181,3 @@ func lessThanHalfTimePassed(now, notBefore, notAfter time.Time) bool { t := notBefore.Add(halfTime(notBefore, notAfter)) return t.Sub(now) > 0 } - -func (s *Server) generateCASignRequest(csr string) *structs.CASignRequest { - return &structs.CASignRequest{ - Datacenter: s.config.PrimaryDatacenter, - CSR: csr, - WriteRequest: structs.WriteRequest{Token: s.tokens.ReplicationToken()}, - } -} diff --git a/agent/consul/leader_connect_ca.go b/agent/consul/leader_connect_ca.go index c3179af3e..2aac0a434 100644 --- a/agent/consul/leader_connect_ca.go +++ b/agent/consul/leader_connect_ca.go @@ -83,6 +83,14 @@ func (c *caDelegateWithState) ApplyCARequest(req *structs.CARequest) (interface{ return c.Server.raftApplyMsgpack(structs.ConnectCARequestType, req) } +func (c *caDelegateWithState) generateCASignRequest(csr string) *structs.CASignRequest { + return &structs.CASignRequest{ + Datacenter: c.Server.config.PrimaryDatacenter, + CSR: csr, + WriteRequest: structs.WriteRequest{Token: c.Server.tokens.ReplicationToken()}, + } +} + func NewCAManager(delegate caServerDelegate, leaderRoutineManager *routine.Manager, logger hclog.Logger, config *Config) *CAManager { return &CAManager{ delegate: delegate, diff --git a/agent/consul/leader_connect_ca_test.go b/agent/consul/leader_connect_ca_test.go index c69d983e8..643cc1983 100644 --- a/agent/consul/leader_connect_ca_test.go +++ b/agent/consul/leader_connect_ca_test.go @@ -8,15 +8,17 @@ import ( "testing" "time" + "github.com/hashicorp/go-version" + "github.com/hashicorp/serf/serf" + "github.com/stretchr/testify/require" + "github.com/hashicorp/consul/agent/connect" ca "github.com/hashicorp/consul/agent/connect/ca" "github.com/hashicorp/consul/agent/consul/state" "github.com/hashicorp/consul/agent/metadata" "github.com/hashicorp/consul/agent/structs" + "github.com/hashicorp/consul/agent/token" "github.com/hashicorp/consul/sdk/testutil" - "github.com/hashicorp/go-version" - "github.com/hashicorp/serf/serf" - "github.com/stretchr/testify/require" ) // TODO(kyhavlov): replace with t.Deadline() @@ -293,3 +295,10 @@ func TestCAManager_UpdateConfigWhileRenewIntermediate(t *testing.T) { require.EqualValues(t, caStateInitialized, manager.state) } + +func TestCADelegateWithState_GenerateCASignRequest(t *testing.T) { + s := Server{config: &Config{PrimaryDatacenter: "east"}, tokens: new(token.Store)} + d := &caDelegateWithState{Server: &s} + req := d.generateCASignRequest("A") + require.Equal(t, "east", req.RequestDatacenter()) +} diff --git a/agent/consul/leader_connect_test.go b/agent/consul/leader_connect_test.go index 7b51e1528..a1e0c4a9e 100644 --- a/agent/consul/leader_connect_test.go +++ b/agent/consul/leader_connect_test.go @@ -14,7 +14,6 @@ import ( uuid "github.com/hashicorp/go-uuid" msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/hashicorp/consul/agent/connect" @@ -1009,13 +1008,6 @@ func getTestRoots(s *Server, datacenter string) (*structs.IndexedCARoots, *struc return &rootList, active, nil } -func TestLeader_GenerateCASignRequest(t *testing.T) { - csr := "A" - s := Server{config: &Config{PrimaryDatacenter: "east"}, tokens: new(token.Store)} - req := s.generateCASignRequest(csr) - assert.Equal(t, "east", req.RequestDatacenter()) -} - func TestLeader_CARootPruning(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short")