-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
198 lines (173 loc) · 6.24 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// 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"
"net/http"
"strings"
// Register the net/trace endpoint with http.DefaultServeMux.
"golang.org/x/net/context"
"golang.org/x/net/trace"
"google.golang.org/grpc/metadata"
"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)
}
// authRequest restricts access to /debug/*.
func authRequest(r *http.Request) (allow, sensitive bool) {
allow = authRequestImpl(r.RemoteAddr, settings.TODO())
sensitive = allow
return allow, sensitive
}
// authRequestImpl restricts access according to the debugRemote setting.
func authRequestImpl(remoteAddr string, st *settings.Values) bool {
switch DebugRemoteMode(strings.ToLower(debugRemote.Get(st))) {
case DebugRemoteAny:
return true
case DebugRemoteLocal:
return isLocalhost(remoteAddr)
default:
return false
}
}
// isLocalhost returns true if the remoteAddr represents a client talking to
// us via localhost.
func isLocalhost(remoteAddr string) bool {
// RemoteAddr is commonly in the form "IP" or "IP:port".
// If it is in the form "IP:port", split off the port.
host, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
host = remoteAddr
}
switch host {
case "localhost", "127.0.0.1", "::1":
return true
default:
return false
}
}
// GatewayRemoteAllowed returns whether a request that has been passed through
// the grpc gateway should be allowed accessed to privileged debugging
// information. Because this function assumes the presence of a context field
// populated by the grpc gateway, it's not applicable for other uses.
func GatewayRemoteAllowed(ctx context.Context, st *cluster.Settings) bool {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
// This should only happen for direct grpc connections, which are allowed.
return true
}
peerAddr, ok := md["x-forwarded-for"]
if !ok || len(peerAddr) == 0 {
// This should only happen for direct grpc connections, which are allowed.
return true
}
return authRequestImpl(peerAddr[0], &st.SV)
}
func init() {
// 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)
}