-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_log.go
132 lines (106 loc) · 2.97 KB
/
auth_log.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
package streamer
import (
"bufio"
"encoding/json"
"os"
"strings"
"github.com/rs/zerolog/log"
)
func (s *GitLabLogStreamer) readAuthLogFile() error {
content, err := os.ReadFile(s.cfg.AuthLogPath)
if err != nil {
return err
}
scanner := bufio.NewScanner(strings.NewReader(string(content)))
scanner.Split(bufio.ScanLines)
authEvents := []*AuthEvent{}
for scanner.Scan() {
line := scanner.Text()
if line == "" {
log.Warn().Msg("Empty line in auth log")
continue
}
authEvent, err := s.parseAuthEvent(line)
if err != nil {
log.Warn().Err(err).Msgf("Failed to parse auth log entry. Content: %s", line)
continue
}
authEvents = append(authEvents, authEvent)
}
newEvents, err := s.processNewAuthEvents(authEvents)
if err != nil {
return err
}
if len(newEvents) == 0 {
log.Warn().
Msg("No new auth events to forward, but a full read was requested")
}
err = s.forwardNewAuthEvents(newEvents)
if err != nil {
return err
}
return nil
}
func (s *GitLabLogStreamer) forwardNewAuthEvents(authEvents []*AuthEvent) error {
if s.cfg.AuditLogForwardingEndpoint != "" {
log.Info().
Int("events", len(authEvents)).
Str("endpoint", s.cfg.AuditLogForwardingEndpoint).
Msgf("Forwarding auth events to HTTP endpoint")
err := s.forwardNewAuthEventsHTTP(authEvents)
if err != nil {
log.Warn().
Err(err).
Msg("Failed to forward auth events to HTTP endpoint")
}
}
if s.cfg.SyslogServerAddr != "" {
err := s.forwardNewAuthEventsSyslog(authEvents)
if err != nil {
log.Warn().
Err(err).
Msg("Failed to forward audit events to syslog server")
}
}
return nil
}
func (s *GitLabLogStreamer) processNewAuthEvents(authEvents []*AuthEvent) ([]*AuthEvent, error) {
newEvents := []*AuthEvent{}
for _, authEvent := range authEvents {
// check if the auditEvent correlation ID already exists
// if it does, we skip it
// if it doesn't, we insert it
_, ok := s.latestAuthEvents.Load(authEvent.CorrelationID)
if ok {
log.Debug().Msgf("Auth event with correlation ID %s already exists. Skipping", authEvent.CorrelationID)
continue
}
err := s.db.Create(authEvent).Error
if err != nil {
log.Error().Err(err).Msgf("Failed to insert auth event with correlation ID %s", authEvent.CorrelationID)
return newEvents, err
}
s.latestAuthEvents.Store(authEvent.CorrelationID, *authEvent)
newEvents = append(newEvents, authEvent)
log.Info().Msgf("Inserted auth event with correlation ID %s", authEvent.CorrelationID)
}
log.Info().Msgf("Inserted %d new auth events", len(newEvents))
return newEvents, nil
}
func (s *GitLabLogStreamer) parseAuthEvent(line string) (*AuthEvent, error) {
authEvent := &AuthEvent{}
err := json.Unmarshal([]byte(line), authEvent)
if err != nil {
return nil, err
}
return authEvent, nil
}
func getAuthEventMessageType(event *AuthEvent) string {
if event.Message != nil {
return *event.Message
}
if event.MetaFeatureCategory != nil {
return *event.MetaFeatureCategory
}
return "unknown"
}