forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.go
234 lines (215 loc) · 7.16 KB
/
transformer.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package transformer
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"errors"
"io"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go-v2/service/kinesis/types"
"github.com/awslabs/kinesis-aggregation/go/v2/deaggregator"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/elastic-agent-libs/mapstr"
)
// Centralize anything related to ECS into a common file.
// TODO: Look at the fields to align them with ECS.
// TODO: how to keep the fields in sync with AWS?
// TODO: api gateway proxy a lot more information is available.
// CloudwatchLogs takes an CloudwatchLogsData and transform it into a beat event.
func CloudwatchLogs(request events.CloudwatchLogsData) []beat.Event {
beatEvents := make([]beat.Event, len(request.LogEvents))
for idx, logEvent := range request.LogEvents {
beatEvents[idx] = beat.Event{
Timestamp: time.Unix(0, logEvent.Timestamp*1000000),
Fields: mapstr.M{
"event": mapstr.M{
"kind": "event",
},
"cloud": mapstr.M{
"provider": "aws",
},
"message": logEvent.Message,
"id": logEvent.ID,
"owner": request.Owner,
"log_stream": request.LogStream,
"log_group": request.LogGroup,
"message_type": request.MessageType,
"subscription_filters": request.SubscriptionFilters,
},
}
}
return beatEvents
}
// APIGatewayProxyRequest takes a web request on the api gateway proxy and transform it into a beat event.
func APIGatewayProxyRequest(request events.APIGatewayProxyRequest) beat.Event {
return beat.Event{
Timestamp: time.Now(),
Fields: mapstr.M{
"event": mapstr.M{
"kind": "event",
"category": []string{"network"},
"type": []string{"connection", "protocol"},
},
"cloud": mapstr.M{
"provider": "aws",
"account.id": request.RequestContext.AccountID,
},
"network": mapstr.M{
"transport": "tcp",
"protocol": "http",
},
"resource": request.Resource,
"path": request.Path,
"method": request.HTTPMethod,
"headers": request.Headers, // TODO: ECS map[string]
"query_string": request.QueryStringParameters, // TODO: map[string], might conflict with ECS
"path_parameters": request.PathParameters,
"body": request.Body, // TODO: could be JSON, json processor? could be used by other functions.
"is_base64_encoded": request.IsBase64Encoded,
},
}
}
// KinesisEvent takes a kinesis event and create multiples beat events.
// DOCS: https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html
func KinesisEvent(request events.KinesisEvent) ([]beat.Event, error) {
var beatEvents []beat.Event
for _, record := range request.Records {
kr := types.Record{
ApproximateArrivalTimestamp: &record.Kinesis.ApproximateArrivalTimestamp.Time,
Data: record.Kinesis.Data,
EncryptionType: types.EncryptionType(record.Kinesis.EncryptionType),
PartitionKey: &record.Kinesis.PartitionKey,
SequenceNumber: &record.Kinesis.SequenceNumber,
}
deaggRecords, err := deaggregator.DeaggregateRecords([]types.Record{kr})
if err != nil {
return nil, err
}
for _, deaggRecord := range deaggRecords {
beatEvents = append(beatEvents, beat.Event{
Timestamp: *deaggRecord.ApproximateArrivalTimestamp,
Fields: mapstr.M{
"event": mapstr.M{
"kind": "event",
},
"cloud": mapstr.M{
"provider": "aws",
"region": record.AwsRegion,
},
"event_id": record.EventID,
"event_name": record.EventName,
"event_source": record.EventSource,
"event_source_arn": record.EventSourceArn,
"event_version": record.EventVersion,
"aws_region": record.AwsRegion,
"message": string(deaggRecord.Data),
"kinesis_partition_key": *deaggRecord.PartitionKey,
"kinesis_schema_version": record.Kinesis.KinesisSchemaVersion,
"kinesis_sequence_number": *deaggRecord.SequenceNumber,
"kinesis_encryption_type": deaggRecord.EncryptionType,
},
})
}
}
return beatEvents, nil
}
// CloudwatchKinesisEvent takes a Kinesis event containing Cloudwatch logs and creates events for all
// Cloudwatch logs.
func CloudwatchKinesisEvent(request events.KinesisEvent, base64Encoded, compressed bool) ([]beat.Event, error) {
var evts []beat.Event
for _, record := range request.Records {
envelopeFields := mapstr.M{
"event": mapstr.M{
"kind": "event",
},
"cloud": mapstr.M{
"provider": "aws",
"region": record.AwsRegion,
},
"event_id": record.EventID,
"event_name": record.EventName,
"event_source": record.EventSource,
"event_source_arn": record.EventSourceArn,
"event_version": record.EventVersion,
"aws_region": record.AwsRegion,
"kinesis_partition_key": record.Kinesis.PartitionKey,
"kinesis_schema_version": record.Kinesis.KinesisSchemaVersion,
"kinesis_sequence_number": record.Kinesis.SequenceNumber,
"kinesis_encryption_type": record.Kinesis.EncryptionType,
}
kinesisData := record.Kinesis.Data
if base64Encoded {
var err error
kinesisData, err = base64.StdEncoding.DecodeString(string(kinesisData))
if err != nil {
return nil, err
}
}
if compressed {
inBuf := bytes.NewBuffer(record.Kinesis.Data)
r, err := gzip.NewReader(inBuf)
if err != nil {
return nil, err
}
var outBuf bytes.Buffer
for {
_, err := io.CopyN(&outBuf, r, 1024)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
_ = r.Close()
return nil, err
}
}
err = r.Close()
if err != nil {
return nil, err
}
kinesisData = outBuf.Bytes()
}
var cloudwatchEvents events.CloudwatchLogsData
err := json.Unmarshal(kinesisData, &cloudwatchEvents)
if err != nil {
return nil, err
}
cwEvts := CloudwatchLogs(cloudwatchEvents)
for _, cwe := range cwEvts {
cwe.Fields.DeepUpdate(envelopeFields)
evts = append(evts, cwe)
}
}
return evts, nil
}
// SQS takes a SQS event and create multiples beat events.
func SQS(request events.SQSEvent) []beat.Event {
beatEvents := make([]beat.Event, len(request.Records))
for idx, record := range request.Records {
beatEvents[idx] = beat.Event{
Timestamp: time.Now(),
Fields: mapstr.M{
"event": mapstr.M{
"kind": "event",
},
"cloud": mapstr.M{
"provider": "aws",
"region": record.AWSRegion,
},
"message_id": record.MessageId,
"receipt_handle": record.ReceiptHandle,
"message": record.Body,
"attributes": record.Attributes,
"event_source": record.EventSource,
"event_source_arn": record.EventSourceARN,
"aws_region": record.AWSRegion,
},
// TODO: SQS message attributes missing, need to check doc
}
}
return beatEvents
}