From d40d09832139093fe055bd83fcabcb07c7ba3ecb Mon Sep 17 00:00:00 2001 From: "R.B. Boyer" <4903+rboyer@users.noreply.github.com> Date: Thu, 28 Oct 2021 16:44:38 -0500 Subject: [PATCH] agent: for various /v1/agent endpoints parse the partition parameter on the request (#11444) Also update the corresponding CLI commands to send the parameter appropriately. NOTE: Behavioral changes are not happening in this PR. --- .changelog/11444.txt | 3 ++ agent/agent_endpoint.go | 46 +++++++++++++++++++++++---- command/forceleave/forceleave.go | 4 ++- command/forceleave/forceleave_test.go | 2 ++ command/join/join.go | 4 ++- command/join/join_test.go | 4 ++- command/members/members.go | 6 ++-- command/members/members_test.go | 5 ++- 8 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 .changelog/11444.txt diff --git a/.changelog/11444.txt b/.changelog/11444.txt new file mode 100644 index 000000000..d96e6baba --- /dev/null +++ b/.changelog/11444.txt @@ -0,0 +1,3 @@ +```release-note:improvement +agent: for various /v1/agent endpoints parse the partition parameter on the request +``` diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go index d97fdcb38..bb45fc541 100644 --- a/agent/agent_endpoint.go +++ b/agent/agent_endpoint.go @@ -306,6 +306,7 @@ func (s *HTTPHandlers) AgentServices(resp http.ResponseWriter, req *http.Request var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? var entMeta structs.EnterpriseMeta if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil { return nil, err @@ -371,6 +372,7 @@ func (s *HTTPHandlers) AgentService(resp http.ResponseWriter, req *http.Request) var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? var entMeta structs.EnterpriseMeta if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil { return nil, err @@ -447,6 +449,7 @@ func (s *HTTPHandlers) AgentChecks(resp http.ResponseWriter, req *http.Request) var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? var entMeta structs.EnterpriseMeta if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil { return nil, err @@ -515,17 +518,26 @@ func (s *HTTPHandlers) AgentMembers(resp http.ResponseWriter, req *http.Request) } } - // TODO(partitions): likely partitions+segment integration will take care of this + // Get the request partition and default to that of the agent. + entMeta := s.agent.AgentEnterpriseMeta() + if err := s.parseEntMetaPartition(req, entMeta); err != nil { + return nil, err + } var members []serf.Member if wan { members = s.agent.WANMembers() } else { filter := consul.LANMemberFilter{ - // TODO(partitions): insert the partition from the request + Partition: entMeta.PartitionOrDefault(), } if segment == api.AllSegments { - filter.AllSegments = true + // Older 'consul members' calls will default to adding segment=_all + // so we only choose to use that request argument in the case where + // the partition is also the default and ignore it the rest of the time. + if structs.IsDefaultPartition(filter.Partition) { + filter.AllSegments = true + } } else { filter.Segment = segment } @@ -557,6 +569,12 @@ func (s *HTTPHandlers) AgentJoin(resp http.ResponseWriter, req *http.Request) (i return nil, acl.ErrPermissionDenied } + // Get the request partition and default to that of the agent. + entMeta := s.agent.AgentEnterpriseMeta() + if err := s.parseEntMetaPartition(req, entMeta); err != nil { + return nil, err + } + // Check if the WAN is being queried wan := false if other := req.URL.Query().Get("wan"); other != "" { @@ -573,8 +591,7 @@ func (s *HTTPHandlers) AgentJoin(resp http.ResponseWriter, req *http.Request) (i } _, err = s.agent.JoinWAN([]string{addr}) } else { - // TODO(partitions): use the request entmeta - _, err = s.agent.JoinLAN([]string{addr}, nil) + _, err = s.agent.JoinLAN([]string{addr}, entMeta) } return nil, err } @@ -614,12 +631,17 @@ func (s *HTTPHandlers) AgentForceLeave(resp http.ResponseWriter, req *http.Reque return nil, acl.ErrPermissionDenied } + // Get the request partition and default to that of the agent. + entMeta := s.agent.AgentEnterpriseMeta() + if err := s.parseEntMetaPartition(req, entMeta); err != nil { + return nil, err + } + // Check the value of the prune query _, prune := req.URL.Query()["prune"] addr := strings.TrimPrefix(req.URL.Path, "/v1/agent/force-leave/") - // TODO(partitions): use the request entmeta - return nil, s.agent.ForceLeave(addr, prune, nil) + return nil, s.agent.ForceLeave(addr, prune, entMeta) } // syncChanges is a helper function which wraps a blocking call to sync @@ -635,6 +657,7 @@ func (s *HTTPHandlers) AgentRegisterCheck(resp http.ResponseWriter, req *http.Re var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? var args structs.CheckDefinition if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil { return nil, err @@ -711,6 +734,7 @@ func (s *HTTPHandlers) AgentDeregisterCheck(resp http.ResponseWriter, req *http. var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &checkID.EnterpriseMeta); err != nil { return nil, err } @@ -803,6 +827,7 @@ func (s *HTTPHandlers) agentCheckUpdate(resp http.ResponseWriter, req *http.Requ var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &cid.EnterpriseMeta); err != nil { return nil, err } @@ -878,6 +903,7 @@ func (s *HTTPHandlers) AgentHealthServiceByID(resp http.ResponseWriter, req *htt return nil, &BadRequestError{Reason: "Missing serviceID"} } + // TODO(partitions): should this default to the agent's partition? var entMeta structs.EnterpriseMeta if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil { return nil, err @@ -936,6 +962,7 @@ func (s *HTTPHandlers) AgentHealthServiceByName(resp http.ResponseWriter, req *h return nil, &BadRequestError{Reason: "Missing service Name"} } + // TODO(partitions): should this default to the agent's partition? var entMeta structs.EnterpriseMeta if err := s.parseEntMetaNoWildcard(req, &entMeta); err != nil { return nil, err @@ -999,6 +1026,7 @@ func (s *HTTPHandlers) AgentRegisterService(resp http.ResponseWriter, req *http. var args structs.ServiceDefinition // Fixup the type decode of TTL or Interval if a check if provided. + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil { return nil, err } @@ -1165,6 +1193,7 @@ func (s *HTTPHandlers) AgentDeregisterService(resp http.ResponseWriter, req *htt var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil { return nil, err } @@ -1222,6 +1251,7 @@ func (s *HTTPHandlers) AgentServiceMaintenance(resp http.ResponseWriter, req *ht var token string s.parseToken(req, &token) + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &sid.EnterpriseMeta); err != nil { return nil, err } @@ -1480,6 +1510,7 @@ func (s *HTTPHandlers) AgentConnectCALeafCert(resp http.ResponseWriter, req *htt } var qOpts structs.QueryOptions + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil { return nil, err } @@ -1527,6 +1558,7 @@ func (s *HTTPHandlers) AgentConnectAuthorize(resp http.ResponseWriter, req *http var authReq structs.ConnectAuthorizeRequest + // TODO(partitions): should this default to the agent's partition? if err := s.parseEntMetaNoWildcard(req, &authReq.EnterpriseMeta); err != nil { return nil, err } diff --git a/command/forceleave/forceleave.go b/command/forceleave/forceleave.go index fcf5cefe5..6b3672d39 100644 --- a/command/forceleave/forceleave.go +++ b/command/forceleave/forceleave.go @@ -4,8 +4,9 @@ import ( "flag" "fmt" - "github.com/hashicorp/consul/command/flags" "github.com/mitchellh/cli" + + "github.com/hashicorp/consul/command/flags" ) func New(ui cli.Ui) *cmd { @@ -30,6 +31,7 @@ func (c *cmd) init() { "Remove agent completely from list of members") c.http = &flags.HTTPFlags{} flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.PartitionFlag()) c.help = flags.Usage(help, c.flags) } diff --git a/command/forceleave/forceleave_test.go b/command/forceleave/forceleave_test.go index f69059ca0..e13808854 100644 --- a/command/forceleave/forceleave_test.go +++ b/command/forceleave/forceleave_test.go @@ -18,6 +18,7 @@ func TestForceLeaveCommand_noTabs(t *testing.T) { } } +// TODO(partitions): split this test and verify it works in partitions func TestForceLeaveCommand(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short") @@ -61,6 +62,7 @@ func TestForceLeaveCommand(t *testing.T) { }) } +// TODO(partitions): split this test and verify it works in partitions func TestForceLeaveCommand_NoNodeWithName(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short") diff --git a/command/join/join.go b/command/join/join.go index 306e0836e..5e94adcd1 100644 --- a/command/join/join.go +++ b/command/join/join.go @@ -4,8 +4,9 @@ import ( "flag" "fmt" - "github.com/hashicorp/consul/command/flags" "github.com/mitchellh/cli" + + "github.com/hashicorp/consul/command/flags" ) func New(ui cli.Ui) *cmd { @@ -29,6 +30,7 @@ func (c *cmd) init() { c.http = &flags.HTTPFlags{} flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.PartitionFlag()) c.help = flags.Usage(help, c.flags) } diff --git a/command/join/join_test.go b/command/join/join_test.go index 6bb9235d4..555014a5d 100644 --- a/command/join/join_test.go +++ b/command/join/join_test.go @@ -4,8 +4,9 @@ import ( "strings" "testing" - "github.com/hashicorp/consul/agent" "github.com/mitchellh/cli" + + "github.com/hashicorp/consul/agent" ) func TestJoinCommand_noTabs(t *testing.T) { @@ -15,6 +16,7 @@ func TestJoinCommand_noTabs(t *testing.T) { } } +// TODO(partitions): split this test and verify it works in partitions func TestJoinCommandJoin_lan(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short") diff --git a/command/members/members.go b/command/members/members.go index c76ae45d7..5ee054530 100644 --- a/command/members/members.go +++ b/command/members/members.go @@ -8,11 +8,12 @@ import ( "sort" "strings" - consulapi "github.com/hashicorp/consul/api" - "github.com/hashicorp/consul/command/flags" "github.com/hashicorp/serf/serf" "github.com/mitchellh/cli" "github.com/ryanuber/columnize" + + consulapi "github.com/hashicorp/consul/api" + "github.com/hashicorp/consul/command/flags" ) // cmd is a Command implementation that queries a running @@ -52,6 +53,7 @@ func (c *cmd) init() { c.http = &flags.HTTPFlags{} flags.Merge(c.flags, c.http.ClientFlags()) + flags.Merge(c.flags, c.http.PartitionFlag()) c.help = flags.Usage(help, c.flags) } diff --git a/command/members/members_test.go b/command/members/members_test.go index a3be2b221..694eb40a3 100644 --- a/command/members/members_test.go +++ b/command/members/members_test.go @@ -5,10 +5,13 @@ import ( "strings" "testing" - "github.com/hashicorp/consul/agent" "github.com/mitchellh/cli" + + "github.com/hashicorp/consul/agent" ) +// TODO(partitions): split these tests + func TestMembersCommand_noTabs(t *testing.T) { t.Parallel() if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {