forked from crowdsecurity/go-cs-bouncer
/
stream_bouncer.go
269 lines (222 loc) · 6.66 KB
/
stream_bouncer.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package csbouncer
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/asians-cloud/crowdsec/pkg/apiclient"
"github.com/asians-cloud/crowdsec/pkg/models"
)
var TotalLAPIError prometheus.Counter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "lapi_requests_failures_total",
Help: "The total number of failed calls to CrowdSec LAPI",
},
)
var TotalLAPICalls prometheus.Counter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "lapi_requests_total",
Help: "The total number of calls to CrowdSec LAPI",
},
)
type StreamBouncer struct {
APIKey string `yaml:"api_key"`
APIUrl string `yaml:"api_url"`
InsecureSkipVerify *bool `yaml:"insecure_skip_verify"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
CAPath string `yaml:"ca_cert_path"`
TickerInterval string `yaml:"update_frequency"`
Scopes []string `yaml:"scopes"`
ScenariosContaining []string `yaml:"scenarios_containing"`
ScenariosNotContaining []string `yaml:"scenarios_not_containing"`
Origins []string `yaml:"origins"`
Startup bool `yaml:"startup"`
TickerIntervalDuration time.Duration
Stream chan *models.DecisionsStreamResponse
APIClient *apiclient.ApiClient
STREAMClient *Client
UserAgent string
Opts apiclient.DecisionsStreamOpts
maxBufferSize int
}
// Config() fills the struct with configuration values from a file. It is not
// aware of .yaml.local files so it is recommended to use ConfigReader() instead.
func (b *StreamBouncer) Config(configPath string) error {
reader, err := os.Open(configPath)
if err != nil {
return fmt.Errorf("unable to read config file '%s': %w", configPath, err)
}
return b.ConfigReader(reader)
}
func (b *StreamBouncer) ConfigReader(configReader io.Reader) error {
content, err := io.ReadAll(configReader)
if err != nil {
return fmt.Errorf("unable to read configuration: %w", err)
}
err = yaml.Unmarshal(content, b)
if err != nil {
return fmt.Errorf("unable to unmarshal config file: %w", err)
}
return nil
}
func (b *StreamBouncer) Init() error {
var err error
// validate the configuration
if b.APIUrl == "" {
return fmt.Errorf("config does not contain LAPI url")
}
if !strings.HasSuffix(b.APIUrl, "/") {
b.APIUrl += "/"
}
if b.APIKey == "" && b.CertPath == "" && b.KeyPath == "" {
return fmt.Errorf("config does not contain LAPI key or certificate")
}
// scopes, origins, etc.
if b.Scopes != nil {
b.Opts.Scopes = strings.Join(b.Scopes, ",")
}
if b.ScenariosContaining != nil {
b.Opts.ScenariosContaining = strings.Join(b.ScenariosContaining, ",")
}
if b.ScenariosNotContaining != nil {
b.Opts.ScenariosNotContaining = strings.Join(b.ScenariosNotContaining, ",")
}
if b.Origins != nil {
b.Opts.Origins = strings.Join(b.Origins, ",")
}
// update_frequency or however it's called in the .yaml of the specific bouncer
if b.TickerInterval == "" {
log.Warningf("lapi update interval is not defined, using default value of 10s")
b.TickerInterval = "10s"
}
b.TickerIntervalDuration, err = time.ParseDuration(b.TickerInterval)
if err != nil {
return fmt.Errorf("unable to parse lapi update interval '%s': %w", b.TickerInterval, err)
}
if b.TickerIntervalDuration <= 0 {
return fmt.Errorf("lapi update interval must be positive")
}
// prepare the client object for the lapi
b.Stream = make(chan *models.DecisionsStreamResponse)
b.APIClient, err = getApiClient(b.APIUrl, b.UserAgent, b.APIKey, b.CAPath, b.CertPath, b.KeyPath, b.InsecureSkipVerify, log.StandardLogger())
if err != nil {
return fmt.Errorf("api client init: %w", err)
}
b.STREAMClient = &Client{
URL: b.APIUrl,
APIKey: b.APIKey,
UserAgent: b.UserAgent,
maxBufferSize: 1 << 16,
}
return nil
}
func (b *StreamBouncer) Run(ctx context.Context) {
ticker := time.NewTicker(b.TickerIntervalDuration)
b.Opts.Startup = b.Startup
getDecisionStream := func() (*models.DecisionsStreamResponse, *apiclient.Response, error) {
data, resp, err := b.APIClient.Decisions.GetStream(context.Background(), b.Opts)
TotalLAPICalls.Inc()
if err != nil {
TotalLAPIError.Inc()
}
return data, resp, err
}
data, resp, err := getDecisionStream()
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
if err != nil {
log.Error(err)
// close the stream
// this may cause the bouncer to exit
close(b.Stream)
return
}
b.Stream <- data
b.Opts.Startup = false
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
data, resp, err := getDecisionStream()
if err != nil {
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
log.Errorf(err.Error())
continue
}
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
b.Stream <- data
}
}
}
func (b *StreamBouncer) RunStream(ctx context.Context) {
// getDecoder creates or re-creates the connection to SSE as necessary
getDecoder := func(ctx context.Context) (*EventStreamReader, *http.Response, error) {
resp, err := b.STREAMClient.StreamDecisionConnect(ctx, b.Opts)
TotalLAPICalls.Inc()
if err != nil {
TotalLAPIError.Inc()
return nil, nil, err
}
reader := NewEventStreamReader(resp.Body, b.STREAMClient.maxBufferSize)
return reader, resp, err
}
// g, _ := errgroup.WithContext(ctx)
// this is the init case, so we have to call it once
reader, resp, err := getDecoder(ctx)
if err != nil {
log.Error(err)
return
} else if resp.StatusCode != 200 {
log.Errorf("Response status is %d", resp.StatusCode)
}
// Produce events
// g.Go(func() error {
defer close(b.Stream)
defer resp.Body.Close()
for {
if evt, err := reader.ReadEvent(); err != nil {
if err == io.EOF {
continue
}
log.Errorf("Error while reading event, retrying later.. %v", err)
time.Sleep(500 * time.Millisecond)
reader, resp, err = getDecoder(ctx)
continue
} else {
if reflect.DeepEqual(evt, []byte("[]")) {
continue
}
data := &models.DecisionsStreamResponse{
New: []*models.Decision{},
Deleted: []*models.Decision{},
}
err := json.Unmarshal(evt, &data)
if err != nil {
log.Errorf("Error while parsing event, retrying later.. %v", err)
time.Sleep(500 * time.Millisecond)
reader, resp, err = getDecoder(ctx)
continue
}
select {
case <-ctx.Done():
log.Printf("Received context shutdown, returning..")
return
case b.Stream <- data:
}
}
}
// })
}