forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logical_system_pprof.go
212 lines (180 loc) · 6.84 KB
/
logical_system_pprof.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package vault
import (
"context"
"errors"
"fmt"
"net/http/pprof"
"strconv"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func (b *SystemBackend) pprofPaths() []*framework.Path {
return []*framework.Path{
{
Pattern: "pprof/$",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofIndex,
Summary: "Returns an HTML page listing the available profiles.",
Description: `Returns an HTML page listing the available
profiles. This should be mainly accessed via browsers or applications that can
render pages.`,
},
},
},
{
Pattern: "pprof/cmdline",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofCmdline,
Summary: "Returns the running program's command line.",
Description: "Returns the running program's command line, with arguments separated by NUL bytes.",
},
},
},
{
Pattern: "pprof/goroutine",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofGoroutine,
Summary: "Returns stack traces of all current goroutines.",
Description: "Returns stack traces of all current goroutines.",
},
},
},
{
Pattern: "pprof/heap",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofHeap,
Summary: "Returns a sampling of memory allocations of live object.",
Description: "Returns a sampling of memory allocations of live object.",
},
},
},
{
Pattern: "pprof/profile",
Fields: map[string]*framework.FieldSchema{
"seconds": {
Type: framework.TypeInt,
Description: "If provided, specifies the duration to run the profiling command.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofProfile,
Summary: "Returns a pprof-formatted cpu profile payload.",
Description: "Returns a pprof-formatted cpu profile payload. Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.",
},
},
},
{
Pattern: "pprof/symbol",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofSymbol,
Summary: "Returns the program counters listed in the request.",
Description: "Returns the program counters listed in the request.",
},
},
},
{
Pattern: "pprof/trace",
Fields: map[string]*framework.FieldSchema{
"seconds": {
Type: framework.TypeInt,
Description: "If provided, specifies the duration to run the tracing command.",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.handlePprofTrace,
Summary: "Returns the execution trace in binary form.",
Description: "Returns the execution trace in binary form. Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified.",
},
},
},
}
}
func (b *SystemBackend) handlePprofIndex(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
pprof.Index(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofCmdline(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
pprof.Cmdline(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofGoroutine(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
pprof.Handler("goroutine").ServeHTTP(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofHeap(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
pprof.Handler("heap").ServeHTTP(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofProfile(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
// Return an error if seconds exceeds max request duration. This follows a
// similar behavior to how pprof treats seconds > WriteTimeout (i.e. it
// error with a 400), and avoids drift between what gets audited vs what
// ends up happening.
if secQueryVal := req.HTTPRequest.FormValue("seconds"); secQueryVal != "" {
maxDur := int64(DefaultMaxRequestDuration.Seconds())
sec, _ := strconv.ParseInt(secQueryVal, 10, 64)
if sec > maxDur {
return logical.ErrorResponse(fmt.Sprintf("seconds %d exceeds max request duration of %d", sec, maxDur)), nil
}
}
pprof.Profile(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofSymbol(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
pprof.Symbol(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
func (b *SystemBackend) handlePprofTrace(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
if err := checkRequestHandlerParams(req); err != nil {
return nil, err
}
// Return an error if seconds exceeds max request duration. This follows a
// similar behavior to how pprof treats seconds > WriteTimeout (i.e. it
// error with a 400), and avoids drift between what gets audited vs what
// ends up happening.
if secQueryVal := req.HTTPRequest.FormValue("seconds"); secQueryVal != "" {
maxDur := int64(DefaultMaxRequestDuration.Seconds())
sec, _ := strconv.ParseInt(secQueryVal, 10, 64)
if sec > maxDur {
return logical.ErrorResponse(fmt.Sprintf("seconds %d exceeds max request duration of %d", sec, maxDur)), nil
}
}
pprof.Trace(req.ResponseWriter, req.HTTPRequest)
return nil, nil
}
// checkRequestHandlerParams is a helper that checks for the existence of the
// HTTP request and response writer in a logical.Request.
func checkRequestHandlerParams(req *logical.Request) error {
if req.ResponseWriter == nil {
return errors.New("no writer for request")
}
if req.HTTPRequest == nil || req.HTTPRequest.Body == nil {
return errors.New("no reader for request")
}
return nil
}