Skip to content

Commit

Permalink
Add version endpoint (hashicorp#17506)
Browse files Browse the repository at this point in the history
* add FIPS verison info

* separate out feature functionality from build identification

* split out ent test

* add version endpoint
  • Loading branch information
skpratt authored May 30, 2023
1 parent 04a0d01 commit e559c59
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
13 changes: 12 additions & 1 deletion agent/agent_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"strings"
"time"

"github.com/hashicorp/consul/envoyextensions/xdscommon"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-memdb"
"github.com/mitchellh/hashstructure"

"github.com/hashicorp/consul/envoyextensions/xdscommon"
"github.com/hashicorp/consul/version"

"github.com/hashicorp/go-bexpr"
"github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf"
Expand Down Expand Up @@ -1683,3 +1685,12 @@ func (s *HTTPHandlers) AgentHost(resp http.ResponseWriter, req *http.Request) (i

return debug.CollectHostInfo(), nil
}

// AgentVersion
//
// GET /v1/agent/version
//
// Retrieves Consul version information.
func (s *HTTPHandlers) AgentVersion(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
return version.GetBuildInfo(), nil
}
29 changes: 29 additions & 0 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
"time"

"github.com/armon/go-metrics"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/version"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/serf/serf"
Expand Down Expand Up @@ -8081,6 +8084,32 @@ func TestAgent_HostBadACL(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.Code)
}

func TestAgent_Version(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()

dc1 := "dc1"
a := NewTestAgent(t, `
primary_datacenter = "`+dc1+`"
`)
defer a.Shutdown()

testrpc.WaitForLeader(t, a.RPC, "dc1")
req, _ := http.NewRequest("GET", "/v1/agent/version", nil)
// req.Header.Add("X-Consul-Token", "initial-management")
resp := httptest.NewRecorder()
respRaw, err := a.srv.AgentVersion(resp, req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.Code)
assert.NotNil(t, respRaw)

obj := respRaw.(*version.BuildInfo)
assert.NotNil(t, obj.HumanVersion)
}

// Thie tests that a proxy with an ExposeConfig is returned as expected.
func TestAgent_Services_ExposeConfig(t *testing.T) {
if testing.Short() {
Expand Down
1 change: 1 addition & 0 deletions agent/http_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
registerEndpoint("/v1/agent/token/", []string{"PUT"}, (*HTTPHandlers).AgentToken)
registerEndpoint("/v1/agent/self", []string{"GET"}, (*HTTPHandlers).AgentSelf)
registerEndpoint("/v1/agent/host", []string{"GET"}, (*HTTPHandlers).AgentHost)
registerEndpoint("/v1/agent/version", []string{"GET"}, (*HTTPHandlers).AgentVersion)
registerEndpoint("/v1/agent/maintenance", []string{"PUT"}, (*HTTPHandlers).AgentNodeMaintenance)
registerEndpoint("/v1/agent/reload", []string{"PUT"}, (*HTTPHandlers).AgentReload)
registerEndpoint("/v1/agent/monitor", []string{"GET"}, (*HTTPHandlers).AgentMonitor)
Expand Down
18 changes: 18 additions & 0 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,24 @@ func (a *Agent) Host() (map[string]interface{}, error) {
return out, nil
}

// Version is used to retrieve information about the running Consul version and build.
func (a *Agent) Version() (map[string]interface{}, error) {
r := a.c.newRequest("GET", "/v1/agent/version")
_, resp, err := a.c.doRequest(r)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
if err := requireOK(resp); err != nil {
return nil, err
}
var out map[string]interface{}
if err := decodeBody(resp, &out); err != nil {
return nil, err
}
return out, nil
}

// Metrics is used to query the agent we are speaking to for
// its current internal metric data
func (a *Agent) Metrics() (*MetricsInfo, error) {
Expand Down
18 changes: 18 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ var (
BuildDate string = "1970-01-01T00:00:01Z"
)

// BuildInfo includes all available version info for this build
type BuildInfo struct {
SHA string
BuildDate string
HumanVersion string
FIPS string
}

// GetHumanVersion composes the parts of the version in a way that's suitable
// for displaying to humans.
func GetHumanVersion() string {
Expand All @@ -51,3 +59,13 @@ func GetHumanVersion() string {
// Strip off any single quotes added by the git information.
return strings.ReplaceAll(version, "'", "")
}

// GetBuildInfo returns all available version information for this build.
func GetBuildInfo() *BuildInfo {
return &BuildInfo{
SHA: GitCommit,
BuildDate: BuildDate,
HumanVersion: GetHumanVersion(),
FIPS: GetFIPSInfo(),
}
}
18 changes: 18 additions & 0 deletions website/content/api-docs/agent/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ $ curl \
}
```

## Retrieve version information

This endpoint returns version information about Consul.

| Method | Path | Produces |
| ------ | ---------------- | ------------------ |
| `GET` | `/agent/version` | `application/json` |

The table below shows this endpoint's support for
[blocking queries](/consul/api-docs/features/blocking),
[consistency modes](/consul/api-docs/features/consistency),
[agent caching](/consul/api-docs/features/caching), and
[required ACLs](/consul/api-docs/api-structure#authentication).

| Blocking Queries | Consistency Modes | Agent Caching | ACL Required |
| ---------------- | ----------------- | ------------- | ------------ |
| `NO` | `none` | `none` | `none` |

## List Members

This endpoint returns the members the agent sees in the cluster gossip pool. Due
Expand Down

0 comments on commit e559c59

Please sign in to comment.