-
Notifications
You must be signed in to change notification settings - Fork 525
/
fleet_store.go
137 lines (121 loc) · 3.64 KB
/
fleet_store.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
// 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 sourcemap
import (
"compress/zlib"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/apm-server/beater/config"
logs "github.com/elastic/apm-server/log"
)
type fleetStore struct {
apikey string
c *http.Client
fleetURLs map[key]string
}
type key struct {
ServiceName string
ServiceVersion string
BundleFilepath string
}
// NewFleetStore returns an instance of Store for interacting with sourcemaps
// stored in Fleet-Server.
func NewFleetStore(
c *http.Client,
fleetCfg *config.Fleet,
cfgs []config.SourceMapMetadata,
expiration time.Duration,
) (*Store, error) {
if len(fleetCfg.Hosts) < 1 {
return nil, errors.New("no fleet hosts present for fleet store")
}
logger := logp.NewLogger(logs.Sourcemap)
s, err := newFleetStore(c, fleetCfg, cfgs)
if err != nil {
return nil, err
}
return newStore(s, logger, expiration)
}
func newFleetStore(
c *http.Client,
fleetCfg *config.Fleet,
cfgs []config.SourceMapMetadata,
) (fleetStore, error) {
// TODO(stn): Add support for multiple fleet hosts
// cf. https://github.com/elastic/apm-server/issues/5514
host := fleetCfg.Hosts[0]
fleetURLs := make(map[key]string)
for _, cfg := range cfgs {
k := key{cfg.ServiceName, cfg.ServiceVersion, cfg.BundleFilepath}
u, err := common.MakeURL(fleetCfg.Protocol, cfg.SourceMapURL, host, 8220)
if err != nil {
return fleetStore{}, err
}
fleetURLs[k] = u
}
return fleetStore{
apikey: "ApiKey " + fleetCfg.AccessAPIKey,
fleetURLs: fleetURLs,
c: c,
}, nil
}
func (f fleetStore) fetch(ctx context.Context, name, version, path string) (string, error) {
k := key{name, version, path}
fleetURL, ok := f.fleetURLs[k]
if !ok {
return "", fmt.Errorf("unable to find sourcemap.url for service.name=%s service.version=%s bundle.path=%s",
name, version, path,
)
}
req, err := http.NewRequest(http.MethodGet, fleetURL, nil)
if err != nil {
return "", err
}
req.Header.Add("Authorization", f.apikey)
resp, err := f.c.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
// Verify that we should only get 200 back from fleet-server
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failure querying fleet: statuscode=%d response=(failed to read body)", resp.StatusCode)
}
return "", fmt.Errorf("failure querying fleet: statuscode=%d response=%s", resp.StatusCode, body)
}
// Looking at the index in elasticsearch, currently
// - no encryption
// - zlib compression
r, err := zlib.NewReader(resp.Body)
if err != nil {
return "", err
}
var m map[string]json.RawMessage
if err := json.NewDecoder(r).Decode(&m); err != nil {
return "", err
}
return string(m["sourceMap"]), nil
}