-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
usagereporter.go
249 lines (207 loc) · 8.44 KB
/
usagereporter.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
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package usagereporter
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"slices"
"time"
"github.com/bufbuild/connect-go"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
prehogv1a "github.com/gravitational/teleport/gen/proto/go/prehog/v1alpha"
prehogv1ac "github.com/gravitational/teleport/gen/proto/go/prehog/v1alpha/prehogv1alphaconnect"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/observability/metrics"
"github.com/gravitational/teleport/lib/usagereporter"
"github.com/gravitational/teleport/lib/utils"
)
const (
// usageReporterMinBatchSize determines the size at which a batch is sent
// regardless of elapsed time
usageReporterMinBatchSize = 50
// usageReporterMaxBatchSize is the largest batch size that will be sent to
// the server; batches larger than this will be split into multiple
// requests. Matches the limit enforced by the server side for a single RPC.
usageReporterMaxBatchSize = 500
// usageReporterMaxBatchAge is the maximum age a batch may reach before
// being flushed, regardless of the batch size
usageReporterMaxBatchAge = time.Second * 5
// usageReporterMaxBufferSize is the maximum size to which the event buffer
// may grow. Events submitted once this limit is reached will be discarded.
// Events that were in the submission queue that fail to submit may also be
// discarded when requeued.
usageReporterMaxBufferSize = 2500
// usageReporterSubmitDelay is a mandatory delay added to each batch submission
// to avoid spamming the prehog instance.
usageReporterSubmitDelay = time.Second * 1
// usageReporterRetryAttempts is the max number of attempts that
// should be made to submit a particular event before it's dropped
usageReporterRetryAttempts = 5
)
// UsageReporter is a service that accepts Teleport usage events.
type UsageReporter interface {
// AnonymizeAndSubmit submits a usage event. The payload will be
// anonymized by the reporter implementation.
AnonymizeAndSubmit(event ...Anonymizable)
}
// GracefulStopper is a UsageReporter that needs to do some work before
// stopping; this is a separate interface because [UsageReporter] is embedded in
// auth.Server.Services, and we don't want to expose extraneous methods as part
// of auth.Server.
type GracefulStopper interface {
UsageReporter
// GracefulStop gracefully closes and runs any finalization needed by the
// UsageReporter; operations can run as long as the context is alive, but
// must terminate quickly (even losing data) if the context is closed.
// Returns nil if operations have completed cleanly.
GracefulStop(context.Context) error
}
// StreamingUsageReporter submits all Teleport usage events anonymized with the
// cluster name, with a very short buffer for batches and no persistency.
type StreamingUsageReporter struct {
// usageReporter is an actual reporter that batches and sends events
usageReporter *usagereporter.UsageReporter[prehogv1a.SubmitEventRequest]
// anonymizer is the anonymizer used for filtered audit events.
anonymizer utils.Anonymizer
// clusterName is the cluster's name, used for anonymization and as an event
// field.
clusterName types.ClusterName
clock clockwork.Clock
}
var _ UsageReporter = (*StreamingUsageReporter)(nil)
func (t *StreamingUsageReporter) AnonymizeAndSubmit(events ...Anonymizable) {
for _, e := range events {
req := e.Anonymize(t.anonymizer)
req.Timestamp = timestamppb.New(t.clock.Now())
req.ClusterName = t.anonymizer.AnonymizeString(t.clusterName.GetClusterName())
t.usageReporter.AddEventsToQueue(&req)
}
}
func (t *StreamingUsageReporter) Run(ctx context.Context) {
t.usageReporter.Run(ctx)
}
type SubmitFunc = usagereporter.SubmitFunc[prehogv1a.SubmitEventRequest]
func NewStreamingUsageReporter(log logrus.FieldLogger, clusterName types.ClusterName, anonymizationKey string, submitter SubmitFunc) (*StreamingUsageReporter, error) {
if log == nil {
log = logrus.StandardLogger()
}
if anonymizationKey == "" {
return nil, trace.BadParameter("anonymization key is required")
}
anonymizer, err := utils.NewHMACAnonymizer(anonymizationKey)
if err != nil {
return nil, trace.Wrap(err)
}
err = metrics.RegisterPrometheusCollectors(usagereporter.UsagePrometheusCollectors...)
if err != nil {
return nil, trace.Wrap(err)
}
clock := clockwork.NewRealClock()
reporter := usagereporter.NewUsageReporter(&usagereporter.Options[prehogv1a.SubmitEventRequest]{
Log: log,
Submit: submitter,
MinBatchSize: usageReporterMinBatchSize,
MaxBatchSize: usageReporterMaxBatchSize,
MaxBatchAge: usageReporterMaxBatchAge,
MaxBufferSize: usageReporterMaxBufferSize,
SubmitDelay: usageReporterSubmitDelay,
RetryAttempts: usageReporterRetryAttempts,
Clock: clock,
})
return &StreamingUsageReporter{
usageReporter: reporter,
anonymizer: anonymizer,
clusterName: clusterName,
clock: clock,
}, nil
}
func NewPrehogSubmitter(ctx context.Context, prehogEndpoint string, clientCert *tls.Certificate, caCertPEM []byte) (SubmitFunc, error) {
tlsConfig := &tls.Config{
// Self-signed test licenses may not have a proper issuer and won't be
// used if just passed in via Certificates, so we'll use this to
// explicitly set the client cert we want to use.
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return clientCert, nil
},
}
if len(caCertPEM) > 0 {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(caCertPEM)
tlsConfig.RootCAs = pool
}
httpClient, err := defaults.HTTPClient()
if err != nil {
return nil, trace.Wrap(err)
}
transport, ok := httpClient.Transport.(*http.Transport)
if !ok {
return nil, trace.BadParameter("invalid transport type %T", httpClient.Transport)
}
transport.Proxy = http.ProxyFromEnvironment
transport.ForceAttemptHTTP2 = true
transport.TLSClientConfig = tlsConfig
httpClient.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
httpClient.Timeout = 5 * time.Second
client := prehogv1ac.NewTeleportReportingServiceClient(httpClient, prehogEndpoint)
return func(reporter *usagereporter.UsageReporter[prehogv1a.SubmitEventRequest], events []*usagereporter.SubmittedEvent[prehogv1a.SubmitEventRequest]) ([]*usagereporter.SubmittedEvent[prehogv1a.SubmitEventRequest], error) {
evs := make([]*prehogv1a.SubmitEventRequest, 0, len(events))
for _, e := range events {
evs = append(evs, e.Event)
}
req := connect.NewRequest(&prehogv1a.SubmitEventsRequest{
Events: evs,
})
if _, err := client.SubmitEvents(ctx, req); err != nil {
return events, trace.Wrap(err)
}
return nil, nil
}, nil
}
// DiscardUsageReporter is a dummy usage reporter that drops all events.
type DiscardUsageReporter struct{}
var _ UsageReporter = DiscardUsageReporter{}
// AnonymizeAndSubmit implements [UsageReporter]
func (DiscardUsageReporter) AnonymizeAndSubmit(...Anonymizable) {
// do nothing
}
// EmitEditorChangeEvent emits an editor change event if the editor role was added or removed.
func EmitEditorChangeEvent(username string, prevRoles, newRoles []string, submit func(...Anonymizable)) {
prevEditor := slices.Contains(prevRoles, teleport.PresetEditorRoleName)
newEditor := slices.Contains(newRoles, teleport.PresetEditorRoleName)
// don't emit event if editor role wasn't added/removed
if prevEditor == newEditor {
return
}
eventType := prehogv1a.EditorChangeStatus_EDITOR_CHANGE_STATUS_ROLE_GRANTED
if prevEditor {
eventType = prehogv1a.EditorChangeStatus_EDITOR_CHANGE_STATUS_ROLE_REMOVED
}
submit(&EditorChangeEvent{
UserName: username,
Status: eventType,
})
}