-
Notifications
You must be signed in to change notification settings - Fork 181
/
client.go
370 lines (324 loc) · 12.7 KB
/
client.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"
crdClient "sigs.k8s.io/wg-policy-prototypes/policy-report/pkg/generated/v1alpha2/clientset/versioned"
gcpfunctions "cloud.google.com/go/functions/apiv1"
amqp "github.com/rabbitmq/amqp091-go"
wavefront "github.com/wavefronthq/wavefront-sdk-go/senders"
"cloud.google.com/go/pubsub"
"cloud.google.com/go/storage"
"github.com/DataDog/datadog-go/statsd"
"github.com/aws/aws-sdk-go/aws/session"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/segmentio/kafka-go"
"k8s.io/client-go/kubernetes"
mqtt "github.com/eclipse/paho.mqtt.golang"
timescaledb "github.com/jackc/pgx/v5/pgxpool"
redis "github.com/redis/go-redis/v9"
"github.com/falcosecurity/falcosidekick/types"
)
// ErrHeaderMissing = 400
var ErrHeaderMissing = errors.New("header missing")
// ErrClientAuthenticationError = 401
var ErrClientAuthenticationError = errors.New("authentication error")
// ErrForbidden = 403
var ErrForbidden = errors.New("access denied")
// ErrNotFound = 404
var ErrNotFound = errors.New("resource not found")
// ErrUnprocessableEntityError = 422
var ErrUnprocessableEntityError = errors.New("bad request")
// ErrTooManyRequest = 429
var ErrTooManyRequest = errors.New("exceeding post rate limit")
// ErrInternalServer = 500
var ErrInternalServer = errors.New("internal server error")
// ErrBadGateway = 502
var ErrBadGateway = errors.New("bad gateway")
// ErrClientCreation is returned if client can't be created
var ErrClientCreation = errors.New("client creation error")
var ErrSASLAuthCreation = errors.New("sasl auth: wrong mechanism")
// EnabledOutputs list all enabled outputs
var EnabledOutputs []string
// DefaultContentType is the default Content-Type header to send along with the Client's POST Request
const DefaultContentType = "application/json; charset=utf-8"
// Some common header values that may be needed in other files
const ContentTypeHeaderKey = "Content-Type"
const UserAgentHeaderKey = "User-Agent"
const AuthorizationHeaderKey = "Authorization"
const UserAgentHeaderValue = "Falcosidekick"
const Bearer = "Bearer"
// files names are static fo the shake of helm and single docker compatibility
const MutualTLSClientCertFilename = "/client.crt"
const MutualTLSClientKeyFilename = "/client.key"
const MutualTLSCacertFilename = "/ca.crt"
// HTTP Methods
const HttpPost = "POST"
const HttpPut = "PUT"
// Headers to add to the client before sending the request
type Header struct {
Key string
Value string
}
// Client communicates with the different API.
type Client struct {
OutputType string
EndpointURL *url.URL
MutualTLSEnabled bool
CheckCert bool
HeaderList []Header
ContentType string
ShutDownFunc func()
Config *types.Configuration
Stats *types.Statistics
PromStats *types.PromStatistics
AWSSession *session.Session
StatsdClient *statsd.Client
DogstatsdClient *statsd.Client
GCPTopicClient *pubsub.Topic
GCPCloudFunctionsClient *gcpfunctions.CloudFunctionsClient
// FIXME: this lock requires a per-output usage lock currently if headers are used -- needs to be refactored
httpClientLock sync.Mutex
GCSStorageClient *storage.Client
KafkaProducer *kafka.Writer
CloudEventsClient cloudevents.Client
KubernetesClient kubernetes.Interface
RabbitmqClient *amqp.Channel
WavefrontSender *wavefront.Sender
Crdclient *crdClient.Clientset
MQTTClient mqtt.Client
TimescaleDBClient *timescaledb.Pool
RedisClient *redis.Client
}
// InitClient returns a new output.Client for accessing the different API.
func NewClient(outputType string, defaultEndpointURL string, mutualTLSEnabled bool, checkCert bool, params types.InitClientArgs) (*Client, error) {
reg := regexp.MustCompile(`(http|nats)(s?)://.*`)
if !reg.MatchString(defaultEndpointURL) {
log.Printf("[ERROR] : %v - %v\n", outputType, "Bad Endpoint")
return nil, ErrClientCreation
}
if _, err := url.ParseRequestURI(defaultEndpointURL); err != nil {
log.Printf("[ERROR] : %v - %v\n", outputType, err.Error())
return nil, ErrClientCreation
}
endpointURL, err := url.Parse(defaultEndpointURL)
if err != nil {
log.Printf("[ERROR] : %v - %v\n", outputType, err.Error())
return nil, ErrClientCreation
}
return &Client{OutputType: outputType, EndpointURL: endpointURL, MutualTLSEnabled: mutualTLSEnabled, CheckCert: checkCert, HeaderList: []Header{}, ContentType: DefaultContentType, Config: params.Config, Stats: params.Stats, PromStats: params.PromStats, StatsdClient: params.StatsdClient, DogstatsdClient: params.DogstatsdClient}, nil
}
// Get get a payload from Output with GET http method.
func (c *Client) Get() error {
return c.sendRequest("GET", nil)
}
// Post sends event (payload) to Output with POST http method.
func (c *Client) Post(payload interface{}) error {
return c.sendRequest("POST", payload)
}
// Put sends event (payload) to Output with PUT http method.
func (c *Client) Put(payload interface{}) error {
return c.sendRequest("PUT", payload)
}
// Get the response body as inlined string
func getInlinedBodyAsString(resp *http.Response) string {
body, _ := io.ReadAll(resp.Body)
contentType := resp.Header.Get("Content-Type")
if contentType == "application/json" {
var compactedBody bytes.Buffer
err := json.Compact(&compactedBody, body)
if err == nil {
return compactedBody.String()
}
}
return string(body)
}
// Post sends event (payload) to Output.
func (c *Client) sendRequest(method string, payload interface{}) error {
// defer + recover to catch panic if output doesn't respond
defer func(c *Client) {
if err := recover(); err != nil {
go c.CountMetric("outputs", 1, []string{"output:" + strings.ToLower(c.OutputType), "status:connectionrefused"})
log.Printf("[ERROR] : %v - %s", c.OutputType, err)
}
}(c)
body := new(bytes.Buffer)
switch payload.(type) {
case influxdbPayload:
fmt.Fprintf(body, "%v", payload)
if c.Config.Debug {
log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, body)
}
case spyderbatPayload:
zipper := gzip.NewWriter(body)
if err := json.NewEncoder(zipper).Encode(payload); err != nil {
log.Printf("[ERROR] : %v - %s", c.OutputType, err)
}
zipper.Close()
if c.Config.Debug {
debugBody := new(bytes.Buffer)
if err := json.NewEncoder(debugBody).Encode(payload); err == nil {
log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, debugBody)
}
}
default:
if err := json.NewEncoder(body).Encode(payload); err != nil {
log.Printf("[ERROR] : %v - %s", c.OutputType, err)
}
if c.Config.Debug {
log.Printf("[DEBUG] : %v payload : %v\n", c.OutputType, body)
}
}
customTransport := http.DefaultTransport.(*http.Transport).Clone()
if customTransport.TLSClientConfig == nil {
customTransport.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
if customTransport.TLSClientConfig.RootCAs == nil {
pool, err := x509.SystemCertPool()
if err != nil {
pool = x509.NewCertPool()
}
customTransport.TLSClientConfig.RootCAs = pool
}
if c.Config.TLSClient.CaCertFile != "" {
caCert, err := os.ReadFile(c.Config.TLSClient.CaCertFile)
if err != nil {
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
}
customTransport.TLSClientConfig.RootCAs.AppendCertsFromPEM(caCert)
}
if c.MutualTLSEnabled {
// Load client cert
var MutualTLSClientCertPath, MutualTLSClientKeyPath, MutualTLSClientCaCertPath string
if c.Config.MutualTLSClient.CertFile != "" {
MutualTLSClientCertPath = c.Config.MutualTLSClient.CertFile
} else {
MutualTLSClientCertPath = c.Config.MutualTLSFilesPath + MutualTLSClientCertFilename
}
if c.Config.MutualTLSClient.KeyFile != "" {
MutualTLSClientKeyPath = c.Config.MutualTLSClient.KeyFile
} else {
MutualTLSClientKeyPath = c.Config.MutualTLSFilesPath + MutualTLSClientKeyFilename
}
if c.Config.MutualTLSClient.CaCertFile != "" {
MutualTLSClientCaCertPath = c.Config.MutualTLSClient.CaCertFile
} else {
MutualTLSClientCaCertPath = c.Config.MutualTLSFilesPath + MutualTLSCacertFilename
}
cert, err := tls.LoadX509KeyPair(MutualTLSClientCertPath, MutualTLSClientKeyPath)
if err != nil {
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
}
// Load CA cert
caCert, err := os.ReadFile(MutualTLSClientCaCertPath)
if err != nil {
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
}
customTransport.TLSClientConfig.RootCAs.AppendCertsFromPEM(caCert)
customTransport.TLSClientConfig.Certificates = []tls.Certificate{cert}
} else {
// With MutualTLS enabled, the check cert flag is ignored
if !c.CheckCert {
customTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, // #nosec G402 This is only set as a result of explicit configuration
}
}
}
client := &http.Client{
Transport: customTransport,
}
req := new(http.Request)
var err error
if method == "GET" {
req, err = http.NewRequest(method, c.EndpointURL.String(), nil)
} else {
req, err = http.NewRequest(method, c.EndpointURL.String(), body)
}
if err != nil {
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
}
req.Header.Add(ContentTypeHeaderKey, c.ContentType)
req.Header.Add(UserAgentHeaderKey, UserAgentHeaderValue)
for _, headerObj := range c.HeaderList {
req.Header.Set(headerObj.Key, headerObj.Value)
}
resp, err := client.Do(req)
if err != nil {
c.HeaderList = []Header{}
log.Printf("[ERROR] : %v - %v\n", c.OutputType, err.Error())
go c.CountMetric("outputs", 1, []string{"output:" + strings.ToLower(c.OutputType), "status:connectionrefused"})
return err
}
defer resp.Body.Close()
// Clear out headers - they will be set for the next request.
c.HeaderList = []Header{}
go c.CountMetric("outputs", 1, []string{"output:" + strings.ToLower(c.OutputType), "status:" + strings.ToLower(http.StatusText(resp.StatusCode))})
switch resp.StatusCode {
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent: //200, 201, 202, 204
log.Printf("[INFO] : %v - %v OK (%v)\n", c.OutputType, method, resp.StatusCode)
if ot := c.OutputType; ot == Kubeless || ot == Openfaas || ot == Fission {
log.Printf("[INFO] : %v - Function Response : %s\n", ot, getInlinedBodyAsString(resp))
}
return nil
case http.StatusBadRequest: //400
msg := getInlinedBodyAsString(resp)
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrHeaderMissing, resp.StatusCode, msg)
if msg != "" {
return fmt.Errorf(msg)
}
return ErrHeaderMissing
case http.StatusUnauthorized: //401
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrClientAuthenticationError, resp.StatusCode, getInlinedBodyAsString(resp))
return ErrClientAuthenticationError
case http.StatusForbidden: //403
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrForbidden, resp.StatusCode, getInlinedBodyAsString(resp))
return ErrForbidden
case http.StatusNotFound: //404
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrNotFound, resp.StatusCode, getInlinedBodyAsString(resp))
return ErrNotFound
case http.StatusUnprocessableEntity: //422
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrUnprocessableEntityError, resp.StatusCode, getInlinedBodyAsString(resp))
return ErrUnprocessableEntityError
case http.StatusTooManyRequests: //429
log.Printf("[ERROR] : %v - %v (%v): %s\n", c.OutputType, ErrTooManyRequest, resp.StatusCode, getInlinedBodyAsString(resp))
return ErrTooManyRequest
case http.StatusInternalServerError: //500
log.Printf("[ERROR] : %v - %v (%v)\n", c.OutputType, ErrTooManyRequest, resp.StatusCode)
return ErrInternalServer
case http.StatusBadGateway: //502
log.Printf("[ERROR] : %v - %v (%v)\n", c.OutputType, ErrTooManyRequest, resp.StatusCode)
return ErrBadGateway
default:
log.Printf("[ERROR] : %v - unexpected Response (%v)\n", c.OutputType, resp.StatusCode)
return errors.New(resp.Status)
}
}
// BasicAuth adds an HTTP Basic Authentication compliant header to the Client.
func (c *Client) BasicAuth(username, password string) {
// Check out RFC7617 for the specifics on this code.
// https://datatracker.ietf.org/doc/html/rfc7617
// This might break I18n, but we can cross that bridge when we come to it.
userPass := username + ":" + password
b64UserPass := base64.StdEncoding.EncodeToString([]byte(userPass))
c.AddHeader(AuthorizationHeaderKey, "Basic "+b64UserPass)
}
// AddHeader adds an HTTP Header to the Client.
func (c *Client) AddHeader(key, value string) {
c.HeaderList = append(c.HeaderList, Header{Key: key, Value: value})
}