/
cf_audit_events_to_splunk_shipper.go
225 lines (186 loc) · 5.06 KB
/
cf_audit_events_to_splunk_shipper.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
package shippers
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"code.cloudfoundry.org/lager"
cfclient "github.com/cloudfoundry-community/go-cfclient"
"github.com/gojektech/heimdall"
"github.com/gojektech/heimdall/httpclient"
"github.com/alphagov/paas-auditor/pkg/db"
)
const (
cfAuditEventsToSplunkShipperName = "cf-audit-events-to-splunk"
)
type splunkEvent struct {
SourceType string `json:"sourcetype"`
Source string `json:"source"`
Event interface{} `json:"event"`
}
type splunkHTTPClient struct {
client http.Client
splunkAPIKey string
}
func (c *splunkHTTPClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Splunk %s", c.splunkAPIKey))
req.Header.Set("Content-Type", "application/json")
return c.client.Do(req)
}
type CFAuditEventsToSplunkShipper struct {
schedule time.Duration
logger lager.Logger
eventDB db.EventDB
deployEnv string
client *httpclient.Client
splunkURL string
eventsShipped int
}
func NewCFAuditEventsToSplunkShipper(
schedule time.Duration,
logger lager.Logger,
eventDB db.EventDB,
deployEnv string,
splunkAPIKey string,
splunkURL string,
) *CFAuditEventsToSplunkShipper {
logger = logger.Session("cf-audit-events-to-splunk-shipper")
var (
requestTimeout = 2 * time.Second
initalTimeout = 100 * time.Millisecond
maxTimeout = 2 * time.Second
exponent float64 = 2
jitter = 500 * time.Millisecond
maxRetries = 3
backoff = heimdall.NewExponentialBackoff(
initalTimeout, maxTimeout,
exponent, jitter,
)
retrier = heimdall.NewRetrier(backoff)
)
client := httpclient.NewClient(
httpclient.WithHTTPClient(&splunkHTTPClient{
client: *http.DefaultClient,
splunkAPIKey: splunkAPIKey,
}),
httpclient.WithHTTPTimeout(requestTimeout),
httpclient.WithRetrier(retrier),
httpclient.WithRetryCount(maxRetries),
)
return &CFAuditEventsToSplunkShipper{
schedule, logger, eventDB, deployEnv, client, splunkURL, 0,
}
}
func (s *CFAuditEventsToSplunkShipper) Run(ctx context.Context) error {
lsession := s.logger.Session("run")
lsession.Info("start")
defer lsession.Info("end")
for {
select {
case <-ctx.Done():
lsession.Info("done")
return nil
case <-time.After(s.schedule):
startTime := time.Now()
eventsToShip, err := s.eventDB.GetUnshippedCFAuditEventsForShipper(
cfAuditEventsToSplunkShipperName,
)
if err != nil {
lsession.Error("err-get-unshipped-cf-audit-events-for-shipper", err)
CFAuditEventsToSplunkShipperErrorsTotal.Inc()
continue
}
var (
shippedEvents = make([]cfclient.Event, 0)
allEventsShipped = true
)
for _, event := range eventsToShip {
err := s.shipEvent(event)
if err != nil {
lsession.Error("err-ship-event", err)
allEventsShipped = false
CFAuditEventsToSplunkShipperErrorsTotal.Inc()
break
}
shippedEvents = append(shippedEvents, event)
s.eventsShipped++
CFAuditEventsToSplunkShipperEventsShippedTotal.Inc()
}
if len(shippedEvents) > 0 {
lastEvent := shippedEvents[len(shippedEvents)-1]
err := s.eventDB.UpdateShipperCursor(
cfAuditEventsToSplunkShipperName,
lastEvent.CreatedAt, lastEvent.GUID,
)
if err != nil {
lsession.Error("err-update-shipper-cursor", err, lager.Data{
"shipper": cfAuditEventsToSplunkShipperName,
})
CFAuditEventsToSplunkShipperErrorsTotal.Inc()
continue
}
lsession.Info("updated-shipper-cursor", lager.Data{
"shipper": cfAuditEventsToSplunkShipperName,
"events-shipped": len(shippedEvents),
})
lastEventCreatedAt, err := time.Parse(time.RFC3339, lastEvent.CreatedAt)
if err != nil {
// Not fatal
lsession.Error("err-parse-event-time", err, lager.Data{
"raw-created-at": lastEvent.CreatedAt,
})
CFAuditEventsToSplunkShipperErrorsTotal.Inc()
continue
}
CFAuditEventsToSplunkShipperLatestEventTimestamp.Set(
float64(lastEventCreatedAt.Unix()),
)
}
duration := time.Since(startTime)
lsession.Info(
"shipped-events",
lager.Data{
"duration": duration,
"events-shipped": len(shippedEvents),
"total-events-shipped": s.eventsShipped,
"all-events-shipped": allEventsShipped,
},
)
CFAuditEventsToSplunkShipperShipDurationTotal.Add(duration.Seconds())
}
}
}
func (s *CFAuditEventsToSplunkShipper) shipEvent(event cfclient.Event) error {
bytesToShip, err := json.Marshal(splunkEvent{
SourceType: "cf-audit-event",
Source: s.deployEnv,
Event: event,
})
if err != nil {
return err
}
resp, err := s.client.Post(
s.splunkURL,
bytes.NewReader(bytesToShip),
http.Header{},
)
defer func() {
if resp.Body != nil {
resp.Body.Close()
}
}()
if err != nil {
return err
}
if 200 <= resp.StatusCode && resp.StatusCode < 300 {
return nil
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("Status: %d Body: %s", resp.StatusCode, body)
}