-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstream_bouncer.go
200 lines (161 loc) · 4.99 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
package csbouncer
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
"github.com/crowdsecurity/crowdsec/pkg/models"
)
var TotalLAPIError = prometheus.NewCounter(prometheus.CounterOpts{
Name: "lapi_requests_failures_total",
Help: "The total number of failed calls to CrowdSec LAPI",
})
var TotalLAPICalls = 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"`
RetryInitialConnect bool `yaml:"retry_initial_connect"`
TickerInterval string `yaml:"update_frequency"`
Scopes []string `yaml:"scopes"`
ScenariosContaining []string `yaml:"scenarios_containing"`
ScenariosNotContaining []string `yaml:"scenarios_not_containing"`
Origins []string `yaml:"origins"`
TickerIntervalDuration time.Duration
Stream chan *models.DecisionsStreamResponse
APIClient *apiclient.ApiClient
UserAgent string
Opts apiclient.DecisionsStreamOpts
}
// 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)
}
return nil
}
func (b *StreamBouncer) Run(ctx context.Context) {
ticker := time.NewTicker(b.TickerIntervalDuration)
b.Opts.Startup = true
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
}
// Initial connection
for {
data, resp, err := getDecisionStream()
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
if err != nil {
if b.RetryInitialConnect {
log.Errorf("failed to connect to LAPI, retrying in 10s: %s", err)
select {
case <-ctx.Done():
// context cancellation, possibly a SIGTERM
return
case <-time.After(10 * time.Second):
continue
}
}
log.Error(err)
// close the stream
// this may cause the bouncer to exit
close(b.Stream)
return
}
b.Stream <- data
break
}
b.Opts.Startup = false
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
data, resp, err := getDecisionStream()
if resp != nil && resp.Response != nil {
resp.Response.Body.Close()
}
if err != nil {
log.Error(err)
continue
}
b.Stream <- data
}
}
}