forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
165 lines (141 loc) · 5.23 KB
/
debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package server
import (
"fmt"
"net/http"
"strings"
// Register the net/trace endpoint with http.DefaultServeMux.
"golang.org/x/net/trace"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/pkg/errors"
"github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/exp"
// This is imported for its side-effect of registering pprof endpoints with
// the http.DefaultServeMux.
_ "net/http/pprof"
)
// debugEndpoint is the prefix of golang's standard debug functionality
// for access to exported vars and pprof tools.
const debugEndpoint = "/debug/"
const debugLogSpyEndpoint = "/debug/logspy"
// We use the default http mux for the debug endpoint (as pprof and net/trace
// register to that via import, and go-metrics registers to that via exp.Exp())
var debugServeMux = http.DefaultServeMux
// DebugRemoteMode controls who can access /debug/requests.
type DebugRemoteMode string
const (
// DebugRemoteOff disallows access to /debug/requests.
DebugRemoteOff DebugRemoteMode = "off"
// DebugRemoteLocal allows only host-local access to /debug/requests.
DebugRemoteLocal DebugRemoteMode = "local"
// DebugRemoteAny allows all access to /debug/requests.
DebugRemoteAny DebugRemoteMode = "any"
)
var debugRemote = settings.RegisterValidatedStringSetting(
"server.remote_debugging.mode",
"set to enable remote debugging, localhost-only or disable (any, local, off)",
"local",
func(s string) error {
switch DebugRemoteMode(strings.ToLower(s)) {
case DebugRemoteOff, DebugRemoteLocal, DebugRemoteAny:
return nil
default:
return errors.Errorf("invalid mode: '%s'", s)
}
},
)
// authorizedHandler is a middleware http handler that checks that the caller
// is authorized to access the handler.
func authorizedHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if any, _ := authRequest(r); !any {
http.Error(w, "not allowed (due to the 'server.remote_debugging.mode' setting)",
http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
// handleDebug passes requests with the debugPathPrefix onto the default
// serve mux, which is preconfigured (by import of net/http/pprof and registration
// of go-metrics) to serve endpoints which access exported variables and pprof tools.
func handleDebug(w http.ResponseWriter, r *http.Request) {
handler, _ := debugServeMux.Handler(r)
handler.ServeHTTP(w, r)
}
// traceAuthRequest is the original trace.AuthRequest, populated in init().
var traceAuthRequest func(*http.Request) (bool, bool)
// ClusterSettings is populated by pkg/cli at init time. It's only used as a
// crutch for the auth handler for /debug/requests. Don't use this.
//
// FIXME(tschottdorf): remove this.
var ClusterSettings *cluster.Settings
// authRequest restricts access to /debug/*.
func authRequest(r *http.Request) (allow, sensitive bool) {
allow, sensitive = traceAuthRequest(r)
switch DebugRemoteMode(strings.ToLower(debugRemote.Get(&ClusterSettings.SV))) {
case DebugRemoteAny:
allow = true
case DebugRemoteLocal:
break
default:
allow = false
}
return allow, sensitive
}
func init() {
traceAuthRequest = trace.AuthRequest
// Tweak the authentication logic for the tracing endpoint. By default it's
// open for localhost only, but we want it to behave according to our
// settings.
//
// TODO(mberhault): properly secure this once we require client certs.
trace.AuthRequest = authRequest
debugServeMux.HandleFunc(debugEndpoint, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != debugEndpoint {
http.Redirect(w, r, debugEndpoint, http.StatusMovedPermanently)
return
}
// The explicit header is necessary or (at least Chrome) will try to
// download a gzipped file (Content-type comes back application/x-gzip).
w.Header().Add("Content-type", "text/html")
fmt.Fprint(w, `
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1; url=/#/debug">
<script type="text/javascript">
window.location.href = "/#/debug"
</script>
<title>Page Redirection</title>
</head>
<body>
This page has moved.
If you are not redirected automatically, follow this <a href='/#/debug'>link</a>.
</body>
</html>
`)
})
spy := logSpy{
setIntercept: log.Intercept,
}
debugServeMux.HandleFunc(debugLogSpyEndpoint, spy.handleDebugLogSpy)
// This registers a superset of the variables exposed through the /debug/vars endpoint
// onto the /debug/metrics endpoint. It includes all expvars registered globally and
// all metrics registered on the DefaultRegistry.
exp.Exp(metrics.DefaultRegistry)
}