Skip to content

Commit

Permalink
cli: Test API access using /status/leader in consul watch
Browse files Browse the repository at this point in the history
Replace call to /agent/self with /status/leader to verify agent
reachability before initializing a watch. This endpoint is not guarded
by ACLs, and as such can be queried by any unauthenticated API client.

Fixes #9353
  • Loading branch information
blake committed Aug 8, 2021
1 parent e940168 commit e28bed1
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions command/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ func (c *cmd) Run(args []string) int {
return 1
}

// Create and test the HTTP client
// Create and test that the API is accessible before starting a blocking
// loop for the watch.
//
// Consul does not have a /ping endpoint, so the /status/leader endpoint
// will be used as a substitute since it does not require an ACL token to
// query, and will always return a response to the client, unless there is a
// network communication error.
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
_, err = client.Agent().NodeName()
_, err = client.Status().Leader()
if err != nil {
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
return 1
Expand Down

0 comments on commit e28bed1

Please sign in to comment.