-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
193 lines (158 loc) · 4.52 KB
/
app.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
package streamer
import (
"fmt"
"os"
"time"
"github.com/fsnotify/fsnotify"
"github.com/puzpuzpuz/xsync/v3"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
const (
GitlabVersionManifestPath = "/opt/gitlab/version-manifest.txt"
PreloadEventsPeriodDays = 30
CleanAuthLogEventsPeriod = 60 * time.Hour * 24 // time to keep the auth events in the DB
)
type Config struct {
AuditLogForwardingEndpoint string
AuthLogForwardingEndpoint string
GitlabHostname string
AuditLogPath string
AuthLogPath string
DBpath string
SyslogServerAddr string
SyslogProtocol string
UseLEEF bool // Use QRadar propietary LEEF format
}
type GitLabLogStreamer struct {
cfg Config
db *gorm.DB
latestAuditLogEvents *xsync.MapOf[string, AuditEvent]
latestAuthEvents *xsync.MapOf[string, AuthEvent]
currentGitlabVersion string
}
func NewGitLabLogStreamer(config Config) (*GitLabLogStreamer, error) {
// we check if the file exists
// if not, we return an error
if _, err := os.Stat(config.AuditLogPath); os.IsNotExist(err) {
return nil, err
}
streamer := &GitLabLogStreamer{
cfg: config,
latestAuditLogEvents: xsync.NewMapOf[string, AuditEvent](),
latestAuthEvents: xsync.NewMapOf[string, AuthEvent](),
}
// run updateCurrentGitlabVersion() every 5 mins
go func() {
for {
err := streamer.updateCurrentGitlabVersion()
if err != nil {
log.Error().Caller().Err(err).Msgf("Error while updating current Gitlab version")
}
time.Sleep(5 * time.Minute)
}
}()
err := streamer.initDB()
if err != nil {
return nil, err
}
err = streamer.preloadDBRecentData()
if err != nil {
return nil, err
}
err = streamer.readAuditLogFile()
if err != nil {
return nil, err
}
err = streamer.readAuthLogFile()
if err != nil {
return nil, err
}
go streamer.cleanOldEvents()
return streamer, nil
}
func (s *GitLabLogStreamer) preloadDBRecentData() error {
// load the audit log events from the last preloadEventsPeriodDays from s.db
// and insert them into s.latestAuditLogEvents
auditEvents := []*AuditEvent{}
err := s.db.Where("time > ?", time.Now().AddDate(0, 0, -PreloadEventsPeriodDays)).Find(&auditEvents).Error
if err != nil {
return err
}
for _, event := range auditEvents {
s.latestAuditLogEvents.Store(fmt.Sprintf("%s,%d", event.CorrelationID, event.Time.UnixNano()), *event)
}
// load the auth log events from the last preloadEventsPeriodDays from s.db
// and insert them into s.latestAuthEvents
authEvent := []*AuthEvent{}
err = s.db.Where("time > ?", time.Now().AddDate(0, 0, -PreloadEventsPeriodDays)).Find(&authEvent).Error
if err != nil {
return err
}
for _, event := range authEvent {
s.latestAuthEvents.Store(fmt.Sprintf("%s,%d", event.CorrelationID, event.Time.UnixNano()), *event)
}
return nil
}
func (s *GitLabLogStreamer) Watch() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal().Caller().Err(err).Msgf("Error creating fsnotify watcher")
}
defer watcher.Close()
log.Info().Caller().
Str("audit_log_path", s.cfg.AuditLogPath).
Str("auth_log_path", s.cfg.AuthLogPath).
Msgf("Watching for changes in the files")
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
s.handleFileEvent(event)
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Warn().Caller().Err(err).Msgf("Error while watching")
}
}
}()
err = watcher.Add(s.cfg.AuditLogPath)
if err != nil {
log.Fatal().Caller().Err(err).Msg("Error adding audit log file to watcher")
}
err = watcher.Add(s.cfg.AuthLogPath)
if err != nil {
log.Fatal().Caller().Err(err).Msg("Error adding auth log file to watcher")
}
<-done
return nil
}
func (s *GitLabLogStreamer) handleFileEvent(event fsnotify.Event) {
switch event.Name {
case s.cfg.AuditLogPath:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info().Caller().Msgf("Audit log file %s modified", event.Name)
s.readAuditLogFile()
}
case s.cfg.AuthLogPath:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info().Caller().Msgf("Auth log file %s modified", event.Name)
s.readAuthLogFile()
}
}
}
func (s *GitLabLogStreamer) cleanOldEvents() {
// remove events older than CleanAuthLogEventsPeriod from s.db
for {
err := s.db.Where("time < ?", time.Now().Add(-CleanAuthLogEventsPeriod)).Delete(&AuthEvent{}).Error
if err != nil {
log.Error().Caller().Err(err).Msg("Error while deleting old auth events")
}
time.Sleep(1 * time.Hour)
}
}