-
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 unit test for unauthenticated metrics access parameter
- Loading branch information
1 parent
8cc3cee
commit 77db87e
Showing
4 changed files
with
84 additions
and
25 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
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