-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config parameter to allow unauthenticated metrics access (#7550)
* Implement config parameter to allow unathenticated metricss access * Add unit test for unauthenticated metrics access parameter * go mod tidy
- Loading branch information
1 parent
338afe2
commit 993a1ae
Showing
8 changed files
with
138 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/hashicorp/vault/helper/metricsutil" | ||
"github.com/hashicorp/vault/sdk/logical" | ||
"github.com/hashicorp/vault/vault" | ||
) | ||
|
||
func handleMetricsUnauthenticated(core *vault.Core) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
req := &logical.Request{Headers: r.Header} | ||
format := r.Form.Get("format") | ||
if format == "" { | ||
format = metricsutil.FormatFromRequest(req) | ||
} | ||
|
||
// Define response | ||
resp, err := core.MetricsHelper().ResponseForFormat(format) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
// Manually extract the logical response and send back the information | ||
w.WriteHeader(resp.Data[logical.HTTPStatusCode].(int)) | ||
w.Header().Set("Content-Type", resp.Data[logical.HTTPContentType].(string)) | ||
w.Write(resp.Data[logical.HTTPRawBody].([]byte)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/hashicorp/vault/helper/metricsutil" | ||
"github.com/hashicorp/vault/vault" | ||
) | ||
|
||
func TestSysMetricsUnauthenticated(t *testing.T) { | ||
inm := metrics.NewInmemSink(10*time.Second, time.Minute) | ||
metrics.DefaultInmemSignal(inm) | ||
conf := &vault.CoreConfig{ | ||
BuiltinRegistry: vault.NewMockBuiltinRegistry(), | ||
MetricsHelper: metricsutil.NewMetricsHelper(inm, false), | ||
} | ||
core, _, token := vault.TestCoreUnsealedWithConfig(t, conf) | ||
ln, addr := TestServer(t, core) | ||
TestServerAuth(t, addr, token) | ||
|
||
// Default: Only authenticated access | ||
resp := testHttpGet(t, "", addr+"/v1/sys/metrics") | ||
testResponseStatus(t, resp, 400) | ||
resp = testHttpGet(t, token, addr+"/v1/sys/metrics") | ||
testResponseStatus(t, resp, 200) | ||
|
||
// Close listener | ||
ln.Close() | ||
|
||
// Setup new custom listener with unauthenticated metrics access | ||
ln, addr = TestListener(t) | ||
props := &vault.HandlerProperties{ | ||
Core: core, | ||
MaxRequestSize: DefaultMaxRequestSize, | ||
UnauthenticatedMetricsAccess: true, | ||
} | ||
TestServerWithListenerAndProperties(t, ln, addr, core, props) | ||
defer ln.Close() | ||
TestServerAuth(t, addr, token) | ||
|
||
// Test without token | ||
resp = testHttpGet(t, "", addr+"/v1/sys/metrics") | ||
testResponseStatus(t, resp, 200) | ||
|
||
// Should also work with token | ||
resp = testHttpGet(t, token, addr+"/v1/sys/metrics") | ||
testResponseStatus(t, resp, 200) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters