Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when a standby node receives a metrics request #8280

Merged
merged 3 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/NYTimes/gziphandler"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-sockaddr"
cleanhttp "github.com/hashicorp/go-cleanhttp"
sockaddr "github.com/hashicorp/go-sockaddr"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/helper/jsonutil"
Expand Down Expand Up @@ -160,6 +160,8 @@ func Handler(props *vault.HandlerProperties) http.Handler {
// Register metrics path without authentication if enabled
if props.UnauthenticatedMetricsAccess {
mux.Handle("/v1/sys/metrics", handleMetricsUnauthenticated(core))
} else {
mux.Handle("/v1/sys/metrics", handleLogicalNoForward(core))
}

additionalRoutes(mux, core)
Expand Down
10 changes: 9 additions & 1 deletion http/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-uuid"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/vault/helper/namespace"
"github.com/hashicorp/vault/sdk/helper/consts"
"github.com/hashicorp/vault/sdk/logical"
Expand Down Expand Up @@ -332,6 +332,14 @@ func handleLogicalInternal(core *vault.Core, injectDataIntoTopLevel bool, noForw
}
}
}

// Prevent any metrics requests to be forwarded from a standby node.
// Instead, we return an error since we cannot be sure if we have an
// active token store to validate the provided token.
case strings.HasPrefix(req.Path, "sys/metrics"):
if isStandby, _ := core.Standby(); isStandby {
respondError(w, http.StatusBadRequest, vault.ErrCannotForwardLocalOnly)
}
}

// Make the internal request. We attach the connection info
Expand Down
6 changes: 6 additions & 0 deletions http/sys_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ func handleMetricsUnauthenticated(core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := &logical.Request{Headers: r.Header}

switch r.Method {
case "GET":
default:
respondError(w, http.StatusMethodNotAllowed, nil)
}

// Parse form
if err := r.ParseForm(); err != nil {
respondError(w, http.StatusBadRequest, err)
Expand Down
6 changes: 6 additions & 0 deletions website/pages/docs/configuration/telemetry.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ These `telemetry` parameters apply to

### `prometheus`

~> **Note:** The `/v1/sys/metrics` endpoint is only accessible on active nodes
and automatically disabled on standby nodes. You can enable the `/v1/sys/metrics`
endpoint on standby nodes by [enabling unauthenticated metrics access][telemetry-tcp].

These `telemetry` parameters apply to
[prometheus](https://prometheus.io).

Expand Down Expand Up @@ -206,3 +210,5 @@ telemetry {
enable_hostname_label = true
}
```

[telemetry-tcp]: /docs/configuration/listener/tcp#telemetry