forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usageevents.go
93 lines (77 loc) · 2.76 KB
/
usageevents.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
/*
* 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 usageevents
import (
"context"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport"
apievents "github.com/gravitational/teleport/api/types/events"
usagereporter "github.com/gravitational/teleport/lib/usagereporter/teleport"
)
// UsageLogger is a trivial audit log sink that forwards an anonymized subset of
// audit log events to Teleport.
type UsageLogger struct {
// Entry is a log entry
*logrus.Entry
// inner is a wrapped audit log implementation
inner apievents.Emitter
// reporter is a usage reporter, where filtered audit events will be sent
reporter usagereporter.UsageReporter
}
var _ apievents.Emitter = (*UsageLogger)(nil)
// reportAuditEvent tries to convert the audit event into a usage event, and
// submits the usage event if successful, but silently ignores events if no
// reporter is configured.
func (u *UsageLogger) reportAuditEvent(ctx context.Context, event apievents.AuditEvent) error {
if u.reporter == nil {
return nil
}
if a := usagereporter.ConvertAuditEvent(event); a != nil {
u.reporter.AnonymizeAndSubmit(a)
}
return nil
}
func (u *UsageLogger) EmitAuditEvent(ctx context.Context, event apievents.AuditEvent) error {
ctx = context.WithoutCancel(ctx)
if err := u.reportAuditEvent(ctx, event); err != nil {
// We don't ever want this to fail or bubble up errors, so the best we
// can do is complain to the logs.
u.Warnf("Failed to filter audit event: %+v", err)
}
if u.inner != nil {
return u.inner.EmitAuditEvent(ctx, event)
}
return nil
}
// New creates a new usage event IAuditLog impl, which wraps another IAuditLog
// impl and forwards a subset of audit log events to the cluster UsageReporter
// service.
func New(reporter usagereporter.UsageReporter, log logrus.FieldLogger, inner apievents.Emitter) (*UsageLogger, error) {
if log == nil {
log = logrus.StandardLogger()
}
return &UsageLogger{
Entry: log.WithField(
trace.Component,
teleport.Component(teleport.ComponentUsageReporting),
),
reporter: reporter,
inner: inner,
}, nil
}