/
http.go
650 lines (592 loc) · 18.8 KB
/
http.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 transport // import "go.elastic.co/apm/transport"
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path"
"runtime"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/pkg/errors"
"go.elastic.co/apm/apmconfig"
"go.elastic.co/apm/internal/apmversion"
"go.elastic.co/apm/internal/configutil"
)
const (
intakePath = "/intake/v2/events"
profilePath = "/intake/v2/profile"
configPath = "/config/v1/agents"
envAPIKey = "ELASTIC_APM_API_KEY"
envSecretToken = "ELASTIC_APM_SECRET_TOKEN"
envServerURLs = "ELASTIC_APM_SERVER_URLS"
envServerURL = "ELASTIC_APM_SERVER_URL"
envServerTimeout = "ELASTIC_APM_SERVER_TIMEOUT"
envServerCert = "ELASTIC_APM_SERVER_CERT"
envVerifyServerCert = "ELASTIC_APM_VERIFY_SERVER_CERT"
envCACert = "ELASTIC_APM_SERVER_CA_CERT_FILE"
)
var (
// Take a copy of the http.DefaultTransport pointer,
// in case another package replaces the value later.
defaultHTTPTransport = http.DefaultTransport.(*http.Transport)
defaultServerURL, _ = url.Parse("http://localhost:8200")
defaultServerTimeout = 30 * time.Second
)
// HTTPTransport is an implementation of Transport, sending payloads via
// a net/http client.
type HTTPTransport struct {
// Client exposes the http.Client used by the HTTPTransport for
// sending requests to the APM Server.
Client *http.Client
intakeHeaders http.Header
configHeaders http.Header
profileHeaders http.Header
shuffleRand *rand.Rand
urlIndex int32
intakeURLs []*url.URL
configURLs []*url.URL
profileURLs []*url.URL
}
// NewHTTPTransport returns a new HTTPTransport which can be used for
// streaming data to the APM Server. The returned HTTPTransport will be
// initialized using the following environment variables:
//
// - ELASTIC_APM_SERVER_URL: the APM Server URL used for sending
// requests. If no URL is specified, then the transport will use the
// default URL "http://localhost:8200".
//
// - ELASTIC_APM_SERVER_TIMEOUT: timeout for requests to the APM Server.
// If not specified, defaults to 30 seconds.
//
// - ELASTIC_APM_SECRET_TOKEN: used to authenticate the agent.
//
// - ELASTIC_APM_SERVER_CERT: path to a PEM-encoded certificate that
// must match the APM Server-supplied certificate. This can be used
// to pin a self signed certificate. If this is set, then
// ELASTIC_APM_VERIFY_SERVER_CERT is ignored.
//
// - ELASTIC_APM_VERIFY_SERVER_CERT: if set to "false", the transport
// will not verify the APM Server's TLS certificate. Only relevant
// when using HTTPS. By default, the transport will verify server
// certificates.
//
func NewHTTPTransport() (*HTTPTransport, error) {
verifyServerCert, err := configutil.ParseBoolEnv(envVerifyServerCert, true)
if err != nil {
return nil, err
}
serverTimeout, err := configutil.ParseDurationEnv(envServerTimeout, defaultServerTimeout)
if err != nil {
return nil, err
}
if serverTimeout < 0 {
serverTimeout = 0
}
serverURLs, err := initServerURLs()
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{InsecureSkipVerify: !verifyServerCert}
serverCertPath := os.Getenv(envServerCert)
if serverCertPath != "" {
serverCert, err := loadCertificate(serverCertPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to load certificate from %s", serverCertPath)
}
// Disable standard verification, we'll check that the
// server supplies the exact certificate provided.
tlsConfig.InsecureSkipVerify = true
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return verifyPeerCertificate(rawCerts, serverCert)
}
}
caCertPath := os.Getenv(envCACert)
if caCertPath != "" {
rootCAs := x509.NewCertPool()
additionalCerts, err := ioutil.ReadFile(caCertPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to load root CA file from %s", caCertPath)
}
if !rootCAs.AppendCertsFromPEM(additionalCerts) {
return nil, fmt.Errorf("failed to load CA certs from %s", caCertPath)
}
tlsConfig.RootCAs = rootCAs
}
client := &http.Client{
Timeout: serverTimeout,
Transport: &http.Transport{
Proxy: defaultHTTPTransport.Proxy,
DialContext: defaultHTTPTransport.DialContext,
MaxIdleConns: defaultHTTPTransport.MaxIdleConns,
IdleConnTimeout: defaultHTTPTransport.IdleConnTimeout,
TLSHandshakeTimeout: defaultHTTPTransport.TLSHandshakeTimeout,
ExpectContinueTimeout: defaultHTTPTransport.ExpectContinueTimeout,
TLSClientConfig: tlsConfig,
},
}
commonHeaders := make(http.Header)
commonHeaders.Set("User-Agent", defaultUserAgent())
intakeHeaders := copyHeaders(commonHeaders)
intakeHeaders.Set("Content-Type", "application/x-ndjson")
intakeHeaders.Set("Content-Encoding", "deflate")
intakeHeaders.Set("Transfer-Encoding", "chunked")
profileHeaders := copyHeaders(commonHeaders)
t := &HTTPTransport{
Client: client,
configHeaders: commonHeaders,
intakeHeaders: intakeHeaders,
profileHeaders: profileHeaders,
}
if apiKey := os.Getenv(envAPIKey); apiKey != "" {
t.SetAPIKey(apiKey)
} else if secretToken := os.Getenv(envSecretToken); secretToken != "" {
t.SetSecretToken(secretToken)
}
t.SetServerURL(serverURLs...)
return t, nil
}
// SetServerURL sets the APM Server URL (or URLs) for sending requests.
// At least one URL must be specified, or the method will panic. The
// list will be randomly shuffled.
func (t *HTTPTransport) SetServerURL(u ...*url.URL) {
if len(u) == 0 {
panic("SetServerURL expects at least one URL")
}
intakeURLs := make([]*url.URL, len(u))
configURLs := make([]*url.URL, len(u))
profileURLs := make([]*url.URL, len(u))
for i, u := range u {
intakeURLs[i] = urlWithPath(u, intakePath)
configURLs[i] = urlWithPath(u, configPath)
profileURLs[i] = urlWithPath(u, profilePath)
}
if n := len(intakeURLs); n > 0 {
if t.shuffleRand == nil {
t.shuffleRand = rand.New(rand.NewSource(time.Now().UnixNano()))
}
for i := n - 1; i > 0; i-- {
j := t.shuffleRand.Intn(i + 1)
intakeURLs[i], intakeURLs[j] = intakeURLs[j], intakeURLs[i]
configURLs[i], configURLs[j] = configURLs[j], configURLs[i]
profileURLs[i], profileURLs[j] = profileURLs[j], profileURLs[i]
}
}
t.intakeURLs = intakeURLs
t.configURLs = configURLs
t.profileURLs = profileURLs
t.urlIndex = 0
}
// SetUserAgent sets the User-Agent header that will be sent with each request.
func (t *HTTPTransport) SetUserAgent(ua string) {
t.setCommonHeader("User-Agent", ua)
}
// SetSecretToken sets the Authorization header with the given secret token.
//
// This overrides the value specified via the ELASTIC_APM_SECRET_TOKEN or
// ELASTIC_APM_API_KEY environment variables, if either are set.
func (t *HTTPTransport) SetSecretToken(secretToken string) {
if secretToken != "" {
t.setCommonHeader("Authorization", "Bearer "+secretToken)
} else {
t.deleteCommonHeader("Authorization")
}
}
// SetAPIKey sets the Authorization header with the given API Key.
//
// This overrides the value specified via the ELASTIC_APM_SECRET_TOKEN or
// ELASTIC_APM_API_KEY environment variables, if either are set.
func (t *HTTPTransport) SetAPIKey(apiKey string) {
if apiKey != "" {
t.setCommonHeader("Authorization", "ApiKey "+apiKey)
} else {
t.deleteCommonHeader("Authorization")
}
}
func (t *HTTPTransport) setCommonHeader(key, value string) {
t.configHeaders.Set(key, value)
t.intakeHeaders.Set(key, value)
t.profileHeaders.Set(key, value)
}
func (t *HTTPTransport) deleteCommonHeader(key string) {
t.configHeaders.Del(key)
t.intakeHeaders.Del(key)
t.profileHeaders.Del(key)
}
// SendStream sends the stream over HTTP. If SendStream returns an error and
// the transport is configured with more than one APM Server URL, then the
// following request will be sent to the next URL in the list.
func (t *HTTPTransport) SendStream(ctx context.Context, r io.Reader) error {
urlIndex := atomic.LoadInt32(&t.urlIndex)
intakeURL := t.intakeURLs[urlIndex]
req := t.newRequest("POST", intakeURL)
req = requestWithContext(ctx, req)
req.Header = t.intakeHeaders
req.Body = ioutil.NopCloser(r)
if err := t.sendStreamRequest(req); err != nil {
atomic.StoreInt32(&t.urlIndex, (urlIndex+1)%int32(len(t.intakeURLs)))
return err
}
return nil
}
func (t *HTTPTransport) sendStreamRequest(req *http.Request) error {
resp, err := t.Client.Do(req)
if err != nil {
return errors.Wrap(err, "sending event request failed")
}
switch resp.StatusCode {
case http.StatusOK, http.StatusAccepted:
resp.Body.Close()
return nil
}
defer resp.Body.Close()
result := newHTTPError(resp)
if resp.StatusCode == http.StatusNotFound && result.Message == "404 page not found" {
// This may be an old (pre-6.5) APM server
// that does not support the v2 intake API.
result.Message = fmt.Sprintf("%s not found (requires APM Server 6.5.0 or newer)", req.URL)
}
return result
}
// SendProfile sends a symbolised pprof profile, encoded as protobuf, and gzip-compressed.
//
// NOTE this is an experimental API, and may be removed in a future minor version, without
// being considered a breaking change.
func (t *HTTPTransport) SendProfile(
ctx context.Context,
metadataReader io.Reader,
profileReaders ...io.Reader,
) error {
urlIndex := atomic.LoadInt32(&t.urlIndex)
profileURL := t.profileURLs[urlIndex]
req := t.newRequest("POST", profileURL)
req = requestWithContext(ctx, req)
req.Header = t.profileHeaders
writeBody := func(w *multipart.Writer) error {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="metadata"`))
h.Set("Content-Type", "application/json")
part, err := w.CreatePart(h)
if err != nil {
return err
}
if _, err := io.Copy(part, metadataReader); err != nil {
return err
}
for _, profileReader := range profileReaders {
h = make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="profile"`))
h.Set("Content-Type", `application/x-protobuf; messageType="perftools.profiles.Profile"`)
part, err = w.CreatePart(h)
if err != nil {
return err
}
if _, err := io.Copy(part, profileReader); err != nil {
return err
}
}
return w.Close()
}
pipeR, pipeW := io.Pipe()
mpw := multipart.NewWriter(pipeW)
req.Header.Set("Content-Type", mpw.FormDataContentType())
req.Body = pipeR
go func() {
err := writeBody(mpw)
pipeW.CloseWithError(err)
}()
return t.sendProfileRequest(req)
}
func (t *HTTPTransport) sendProfileRequest(req *http.Request) error {
resp, err := t.Client.Do(req)
if err != nil {
return errors.Wrap(err, "sending profile request failed")
}
switch resp.StatusCode {
case http.StatusOK, http.StatusAccepted:
resp.Body.Close()
return nil
}
defer resp.Body.Close()
result := newHTTPError(resp)
if resp.StatusCode == http.StatusNotFound && result.Message == "404 page not found" {
// TODO(axw) correct minimum server version.
result.Message = fmt.Sprintf("%s not found (requires APM Server 7.5.0 or newer)", req.URL)
}
return result
}
// WatchConfig polls the APM Server for agent config changes, sending
// them over the returned channel.
func (t *HTTPTransport) WatchConfig(ctx context.Context, args apmconfig.WatchParams) <-chan apmconfig.Change {
// We have an initial delay to allow application initialisation code
// to close apm.DefaultTracer, which would cancel watching config.
const initialDelay = 1 * time.Second
changes := make(chan apmconfig.Change)
go func() {
defer close(changes)
var etag string
var out chan apmconfig.Change
var change apmconfig.Change
timer := time.NewTimer(initialDelay)
for {
select {
case <-ctx.Done():
return
case out <- change:
out = nil
change = apmconfig.Change{}
continue
case <-timer.C:
}
urlIndex := atomic.LoadInt32(&t.urlIndex)
query := make(url.Values)
query.Set("service.name", args.Service.Name)
if args.Service.Environment != "" {
query.Set("service.environment", args.Service.Environment)
}
url := *t.configURLs[urlIndex]
url.RawQuery = query.Encode()
req := t.newRequest("GET", &url)
req.Header = t.configHeaders
if etag != "" {
req.Header = copyHeaders(req.Header)
req.Header.Set("If-None-Match", strconv.QuoteToASCII(etag))
}
req = requestWithContext(ctx, req)
resp := t.configRequest(req)
var send bool
if resp.err != nil {
// The request will have failed if the context has been
// cancelled. No need to send a a change in this case.
send = ctx.Err() == nil
}
if !send && resp.attrs != nil {
etag = resp.etag
send = true
}
if send {
change = apmconfig.Change{Err: resp.err, Attrs: resp.attrs}
out = changes
}
timer.Reset(resp.maxAge)
}
}()
return changes
}
func (t *HTTPTransport) configRequest(req *http.Request) configResponse {
// defaultMaxAge is the default amount of time to wait between
// requests. This should only be used when the server does not
// respond with a Cache-Control header, or where the header is
// malformed.
const defaultMaxAge = 5 * time.Minute
resp, err := t.Client.Do(req)
if err != nil {
// TODO(axw) this might indicate that the APM Server is unavailable.
// In this case, we should allow a change in URL due to SendStream
// to cut the defaultMaxAge delay short.
return configResponse{
err: errors.Wrap(err, "sending config request failed"),
maxAge: defaultMaxAge,
}
}
defer resp.Body.Close()
var response configResponse
if etag, err := strconv.Unquote(resp.Header.Get("Etag")); err == nil {
response.etag = etag
}
cacheControl := parseCacheControl(resp.Header.Get("Cache-Control"))
response.maxAge = cacheControl.maxAge
if response.maxAge < 0 {
response.maxAge = defaultMaxAge
}
switch resp.StatusCode {
case http.StatusNotModified, http.StatusForbidden, http.StatusNotFound:
// 304 (Not Modified) is returned when the config has not changed since the previous query.
// 403 (Forbidden) is returned if the server does not have the connection to Kibana enabled.
// 404 (Not Found) is returned by old servers that do not implement the config endpoint.
return response
case http.StatusOK:
attrs := make(map[string]string)
// TODO(axw) handling EOF shouldn't be necessary, server currently responds with an empty
// body when there is no config.
if err := json.NewDecoder(resp.Body).Decode(&attrs); err != nil && err != io.EOF {
response.err = err
} else {
response.attrs = attrs
}
return response
}
response.err = newHTTPError(resp)
return response
}
func (t *HTTPTransport) newRequest(method string, url *url.URL) *http.Request {
req := &http.Request{
Method: method,
URL: url,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Host: url.Host,
}
return req
}
func urlWithPath(url *url.URL, p string) *url.URL {
urlCopy := *url
urlCopy.Path = path.Clean(urlCopy.Path + p)
if urlCopy.RawPath != "" {
urlCopy.RawPath = path.Clean(urlCopy.RawPath + p)
}
return &urlCopy
}
// HTTPError is an error returned by HTTPTransport methods when requests fail.
type HTTPError struct {
Response *http.Response
Message string
}
func newHTTPError(resp *http.Response) *HTTPError {
bodyContents, err := ioutil.ReadAll(resp.Body)
if err == nil {
resp.Body = ioutil.NopCloser(bytes.NewReader(bodyContents))
}
return &HTTPError{
Response: resp,
Message: strings.TrimSpace(string(bodyContents)),
}
}
func (e *HTTPError) Error() string {
msg := fmt.Sprintf("request failed with %s", e.Response.Status)
if e.Message != "" {
msg += ": " + e.Message
}
return msg
}
// initServerURLs parses ELASTIC_APM_SERVER_URLS if specified,
// otherwise parses ELASTIC_APM_SERVER_URL if specified. If
// neither are specified, then the default localhost URL is
// returned.
func initServerURLs() ([]*url.URL, error) {
key := envServerURLs
value := os.Getenv(key)
if value == "" {
key = envServerURL
value = os.Getenv(key)
}
var urls []*url.URL
for _, field := range strings.Split(value, ",") {
field = strings.TrimSpace(field)
if field == "" {
continue
}
u, err := url.Parse(field)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", key)
}
urls = append(urls, u)
}
if len(urls) == 0 {
urls = []*url.URL{defaultServerURL}
}
return urls, nil
}
func requestWithContext(ctx context.Context, req *http.Request) *http.Request {
url := req.URL
req.URL = nil
reqCopy := req.WithContext(ctx)
reqCopy.URL = url
req.URL = url
return reqCopy
}
func loadCertificate(path string) (*x509.Certificate, error) {
pemBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
for {
var certBlock *pem.Block
certBlock, pemBytes = pem.Decode(pemBytes)
if certBlock == nil {
return nil, errors.New("missing or invalid certificate")
}
if certBlock.Type == "CERTIFICATE" {
return x509.ParseCertificate(certBlock.Bytes)
}
}
}
func verifyPeerCertificate(rawCerts [][]byte, trusted *x509.Certificate) error {
if len(rawCerts) == 0 {
return errors.New("missing leaf certificate")
}
cert, err := x509.ParseCertificate(rawCerts[0])
if err != nil {
return errors.Wrap(err, "failed to parse certificate from server")
}
if !cert.Equal(trusted) {
return errors.New("failed to verify server certificate")
}
return nil
}
func defaultUserAgent() string {
return fmt.Sprintf("elasticapm-go/%s go/%s", apmversion.AgentVersion, runtime.Version())
}
func copyHeaders(in http.Header) http.Header {
out := make(http.Header, len(in))
for k, vs := range in {
vsCopy := make([]string, len(vs))
copy(vsCopy, vs)
out[k] = vsCopy
}
return out
}
type configResponse struct {
err error
attrs map[string]string
etag string
maxAge time.Duration
}
type cacheControl struct {
maxAge time.Duration
}
func parseCacheControl(s string) cacheControl {
fields := strings.SplitN(s, "max-age=", 2)
if len(fields) < 2 {
return cacheControl{maxAge: -1}
}
s = fields[1]
if i := strings.IndexRune(s, ','); i != -1 {
s = s[:i]
}
maxAge, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return cacheControl{maxAge: -1}
}
return cacheControl{maxAge: time.Duration(maxAge) * time.Second}
}