Skip to content

Commit

Permalink
# This is a combination of 9 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

init without tests

# This is the commit message #2:

change log

# This is the commit message #3:

fix tests

# This is the commit message #4:

fix tests

# This is the commit message #5:

added tests

# This is the commit message #6:

change log breaking change

# This is the commit message #7:

removed breaking change

# This is the commit message #8:

fix test

# This is the commit message #9:

keeping the test behaviour same
  • Loading branch information
absolutelightning committed Jun 21, 2023
1 parent 00c8575 commit 10f500e
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .changelog/17565.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
reloadable config: Made enable_debug config reloadable and enable pprof command to work when config toggles to true
```
6 changes: 4 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,13 +1126,13 @@ func (a *Agent) listenHTTP() ([]apiServer, error) {
httpServer := &http.Server{
Addr: l.Addr().String(),
TLSConfig: tlscfg,
Handler: srv.handler(a.config.EnableDebug),
Handler: srv.handler(),
MaxHeaderBytes: a.config.HTTPMaxHeaderBytes,
}

if scada.IsCapability(l.Addr()) {
// wrap in http2 server handler
httpServer.Handler = h2c.NewHandler(srv.handler(a.config.EnableDebug), &http2.Server{})
httpServer.Handler = h2c.NewHandler(srv.handler(), &http2.Server{})
}

// Load the connlimit helper into the server
Expand Down Expand Up @@ -4290,6 +4290,8 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {

a.proxyConfig.SetUpdateRateLimit(newCfg.XDSUpdateRateLimit)

a.config.EnableDebug = newCfg.EnableDebug

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1623,7 +1623,7 @@ func TestHTTPHandlers_AgentMetricsStream_ACLDeny(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/agent/metrics/stream", nil)
require.NoError(t, err)
handle := h.handler(false)
handle := h.handler()
handle.ServeHTTP(resp, req)
require.Equal(t, http.StatusForbidden, resp.Code)
require.Contains(t, resp.Body.String(), "Permission denied")
Expand Down Expand Up @@ -1660,7 +1660,7 @@ func TestHTTPHandlers_AgentMetricsStream(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/v1/agent/metrics/stream", nil)
require.NoError(t, err)
handle := h.handler(false)
handle := h.handler()
handle.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)

Expand Down Expand Up @@ -6009,7 +6009,7 @@ func TestAgent_Monitor(t *testing.T) {
req = req.WithContext(cancelCtx)

resp := httptest.NewRecorder()
handler := a.srv.handler(true)
handler := a.srv.handler()
go handler.ServeHTTP(resp, req)

args := &structs.ServiceDefinition{
Expand Down
33 changes: 33 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4193,6 +4193,39 @@ func TestAgent_ReloadConfig_XDSUpdateRateLimit(t *testing.T) {
require.Equal(t, rate.Limit(1000), a.proxyConfig.UpdateRateLimit())
}

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

cfg := fmt.Sprintf(`data_dir = %q`, testutil.TempDir(t, "agent"))

a := NewTestAgent(t, cfg)
defer a.Shutdown()

c := TestConfig(
testutil.Logger(t),
config.FileSource{
Name: t.Name(),
Format: "hcl",
Data: cfg + ` enable_debug = true`,
},
)
require.NoError(t, a.reloadConfigInternal(c))
require.Equal(t, true, a.config.EnableDebug)

c = TestConfig(
testutil.Logger(t),
config.FileSource{
Name: t.Name(),
Format: "hcl",
Data: cfg + ` enable_debug = false`,
},
)
require.NoError(t, a.reloadConfigInternal(c))
require.Equal(t, false, a.config.EnableDebug)
}

func TestAgent_consulConfig_AutoEncryptAllowTLS(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
Expand Down
23 changes: 14 additions & 9 deletions agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *HTTPHandlers) ReloadConfig(newCfg *config.RuntimeConfig) error {
//
// The first call must not be concurrent with any other call. Subsequent calls
// may be concurrent with HTTP requests since no state is modified.
func (s *HTTPHandlers) handler(enableDebug bool) http.Handler {
func (s *HTTPHandlers) handler() http.Handler {
// Memoize multiple calls.
if s.h != nil {
return s.h
Expand Down Expand Up @@ -210,7 +210,15 @@ func (s *HTTPHandlers) handler(enableDebug bool) http.Handler {
// handlePProf takes the given pattern and pprof handler
// and wraps it to add authorization and metrics
handlePProf := func(pattern string, handler http.HandlerFunc) {

wrapper := func(resp http.ResponseWriter, req *http.Request) {

// If enableDebug or ACL enabled, register wrapped pprof handlers
if !s.agent.config.EnableDebug && s.checkACLDisabled() {
resp.WriteHeader(http.StatusNotFound)
return
}

var token string
s.parseToken(req, &token)

Expand Down Expand Up @@ -245,14 +253,11 @@ func (s *HTTPHandlers) handler(enableDebug bool) http.Handler {
handleFuncMetrics(pattern, s.wrap(bound, methods))
}

// If enableDebug or ACL enabled, register wrapped pprof handlers
if enableDebug || !s.checkACLDisabled() {
handlePProf("/debug/pprof/", pprof.Index)
handlePProf("/debug/pprof/cmdline", pprof.Cmdline)
handlePProf("/debug/pprof/profile", pprof.Profile)
handlePProf("/debug/pprof/symbol", pprof.Symbol)
handlePProf("/debug/pprof/trace", pprof.Trace)
}
handlePProf("/debug/pprof/", pprof.Index)
handlePProf("/debug/pprof/cmdline", pprof.Cmdline)
handlePProf("/debug/pprof/profile", pprof.Profile)
handlePProf("/debug/pprof/symbol", pprof.Symbol)
handlePProf("/debug/pprof/trace", pprof.Trace)

if s.IsUIEnabled() {
// Note that we _don't_ support reloading ui_config.{enabled, content_dir,
Expand Down
6 changes: 4 additions & 2 deletions agent/http_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func TestHTTPAPI_OptionMethod_OSS(t *testing.T) {
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
req, _ := http.NewRequest("OPTIONS", uri, nil)
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
allMethods := append([]string{"OPTIONS"}, methods...)

if resp.Code != http.StatusOK {
Expand Down Expand Up @@ -190,7 +191,8 @@ func TestHTTPAPI_AllowedNets_OSS(t *testing.T) {
req, _ := http.NewRequest(method, uri, nil)
req.RemoteAddr = "192.168.1.2:5555"
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)

require.Equal(t, http.StatusForbidden, resp.Code, "%s %s", method, path)
})
Expand Down
32 changes: 21 additions & 11 deletions agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ func TestSetupHTTPServer_HTTP2(t *testing.T) {
err = setupHTTPS(httpServer, noopConnState, time.Second)
require.NoError(t, err)

srvHandler := a.srv.handler(true)
a.config.EnableDebug = true
srvHandler := a.srv.handler()
mux, ok := srvHandler.(*wrappedMux)
require.True(t, ok, "expected a *wrappedMux, got %T", handler)
mux.mux.HandleFunc("/echo", handler)
Expand Down Expand Up @@ -483,7 +484,8 @@ func TestHTTPAPI_Ban_Nonprintable_Characters(t *testing.T) {
t.Fatal(err)
}
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
if got, want := resp.Code, http.StatusBadRequest; got != want {
t.Fatalf("bad response code got %d want %d", got, want)
}
Expand All @@ -506,7 +508,8 @@ func TestHTTPAPI_Allow_Nonprintable_Characters_With_Flag(t *testing.T) {
t.Fatal(err)
}
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
// Key doesn't actually exist so we should get 404
if got, want := resp.Code, http.StatusNotFound; got != want {
t.Fatalf("bad response code got %d want %d", got, want)
Expand Down Expand Up @@ -645,7 +648,8 @@ func requireHasHeadersSet(t *testing.T, a *TestAgent, path string) {

resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", path, nil)
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)

hdrs := resp.Header()
require.Equal(t, "*", hdrs.Get("Access-Control-Allow-Origin"),
Expand Down Expand Up @@ -706,14 +710,16 @@ func TestAcceptEncodingGzip(t *testing.T) {
// negotiation, but since this call doesn't go through a real
// transport, the header has to be set manually
req.Header["Accept-Encoding"] = []string{"gzip"}
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
require.Equal(t, "", resp.Header().Get("Content-Encoding"))

resp = httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/v1/kv/long", nil)
req.Header["Accept-Encoding"] = []string{"gzip"}
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, 200, resp.Code)
require.Equal(t, "gzip", resp.Header().Get("Content-Encoding"))
}
Expand Down Expand Up @@ -1068,8 +1074,9 @@ func TestHTTPServer_PProfHandlers_EnableDebug(t *testing.T) {
resp := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/debug/pprof/profile?seconds=1", nil)

a.config.EnableDebug = true
httpServer := &HTTPHandlers{agent: a.Agent}
httpServer.handler(true).ServeHTTP(resp, req)
httpServer.handler().ServeHTTP(resp, req)

require.Equal(t, http.StatusOK, resp.Code)
}
Expand All @@ -1087,7 +1094,7 @@ func TestHTTPServer_PProfHandlers_DisableDebugNoACLs(t *testing.T) {
req, _ := http.NewRequest("GET", "/debug/pprof/profile", nil)

httpServer := &HTTPHandlers{agent: a.Agent}
httpServer.handler(false).ServeHTTP(resp, req)
httpServer.handler().ServeHTTP(resp, req)

require.Equal(t, http.StatusNotFound, resp.Code)
}
Expand Down Expand Up @@ -1168,7 +1175,8 @@ func TestHTTPServer_PProfHandlers_ACLs(t *testing.T) {
t.Run(fmt.Sprintf("case %d (%#v)", i, c), func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s?token=%s", c.endpoint, c.token), nil)
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
assert.Equal(t, c.code, resp.Code)
})
}
Expand Down Expand Up @@ -1478,7 +1486,8 @@ func TestEnableWebUI(t *testing.T) {

req, _ := http.NewRequest("GET", "/ui/", nil)
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)

// Validate that it actually sent the index page we expect since an error
Expand Down Expand Up @@ -1507,7 +1516,8 @@ func TestEnableWebUI(t *testing.T) {
{
req, _ := http.NewRequest("GET", "/ui/", nil)
resp := httptest.NewRecorder()
a.srv.handler(true).ServeHTTP(resp, req)
a.config.EnableDebug = true
a.srv.handler().ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
require.Contains(t, resp.Body.String(), `<!-- CONSUL_VERSION:`)
require.Contains(t, resp.Body.String(), `valid-but-unlikely-metrics-provider-name`)
Expand Down
3 changes: 2 additions & 1 deletion agent/ui_endpoint_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func TestUIEndpoint_MetricsProxy_ACLDeny(t *testing.T) {
`, backendURL))
defer a.Shutdown()

h := a.srv.handler(true)
a.config.EnableDebug = true
h := a.srv.handler()

testrpc.WaitForLeader(t, a.RPC, "dc1")

Expand Down
3 changes: 2 additions & 1 deletion agent/ui_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,8 @@ func TestUIEndpoint_MetricsProxy(t *testing.T) {
require.NoError(t, a.Agent.reloadConfigInternal(&cfg))

// Now fetch the API handler to run requests against
h := a.srv.handler(true)
h := a.srv.handler()
a.config.EnableDebug = true

req := httptest.NewRequest("GET", tc.path, nil)
rec := httptest.NewRecorder()
Expand Down

0 comments on commit 10f500e

Please sign in to comment.