-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathlambda_function.py
More file actions
485 lines (429 loc) · 14.4 KB
/
Copy pathlambda_function.py
File metadata and controls
485 lines (429 loc) · 14.4 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Unless explicitly stated otherwise all files in this repository are licensed
# under the Apache License Version 2.0.
# This product includes software developed at Datadog (https://www.datadoghq.com/).
# Copyright 2021 Datadog, Inc.
import logging
import os
import gzip
import json
import time
import base64
from io import BufferedReader, BytesIO
from collections import defaultdict, Counter
from urllib.request import Request, urlopen
from urllib.parse import urlencode
import boto3
import botocore
logger = logging.getLogger()
logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper()))
logger.info("Loading function")
DD_SITE = os.getenv("DD_SITE", default="datadoghq.com")
def _datadog_keys():
if "kmsEncryptedKeys" in os.environ:
KMS_ENCRYPTED_KEYS = os.environ["kmsEncryptedKeys"]
kms = boto3.client("kms")
# kmsEncryptedKeys should be created through the Lambda's encryption
# helpers and as such will have the EncryptionContext
return json.loads(
kms.decrypt(
CiphertextBlob=base64.b64decode(KMS_ENCRYPTED_KEYS),
EncryptionContext={
"LambdaFunctionName": os.environ["AWS_LAMBDA_FUNCTION_NAME"]
},
)["Plaintext"]
)
if "DD_API_KEY_SECRET_ARN" in os.environ:
SECRET_ARN = os.environ["DD_API_KEY_SECRET_ARN"]
DD_API_KEY = boto3.client("secretsmanager").get_secret_value(
SecretId=SECRET_ARN
)["SecretString"]
return {"api_key": DD_API_KEY}
if "DD_API_KEY_SSM_NAME" in os.environ:
SECRET_NAME = os.environ["DD_API_KEY_SSM_NAME"]
DD_API_KEY = boto3.client("ssm").get_parameter(
Name=SECRET_NAME, WithDecryption=True
)["Parameter"]["Value"]
return {"api_key": DD_API_KEY}
if "DD_KMS_API_KEY" in os.environ:
ENCRYPTED = os.environ["DD_KMS_API_KEY"]
# For interop with other DD Lambdas taking in DD_KMS_API_KEY, we'll
# optionally try the EncryptionContext associated with this Lambda.
try:
DD_API_KEY = boto3.client("kms").decrypt(
CiphertextBlob=base64.b64decode(ENCRYPTED),
EncryptionContext={
"LambdaFunctionName": os.environ["AWS_LAMBDA_FUNCTION_NAME"]
},
)["Plaintext"]
except botocore.exceptions.ClientError:
DD_API_KEY = boto3.client("kms").decrypt(
CiphertextBlob=base64.b64decode(ENCRYPTED),
)["Plaintext"]
if type(DD_API_KEY) is bytes:
# If the CiphertextBlob was encrypted with AWS CLI, we
# need to re-encode this in base64
try:
DD_API_KEY = DD_API_KEY.decode("utf-8")
except UnicodeDecodeError as e:
print(
"INFO DD_KMS_API_KEY: Could not decode key in utf-8, encoding in b64. Exception:",
e,
)
DD_API_KEY = base64.b64encode(DD_API_KEY)
DD_API_KEY = DD_API_KEY.decode("utf-8")
except Exception as e:
print("ERROR DD_KMS_API_KEY Unknown exception decoding key:", e)
return {"api_key": DD_API_KEY}
if "DD_API_KEY" in os.environ:
DD_API_KEY = os.environ["DD_API_KEY"]
return {"api_key": DD_API_KEY}
raise ValueError(
"Datadog API key is not defined, see documentation for environment variable options"
)
# Preload the keys so we can bail out early if they're misconfigured
# Alternatively set datadog keys directly
# datadog_keys = {
# "api_key": "abcd",
# "app_key": "efgh",
# }
datadog_keys = _datadog_keys()
logger.info("Lambda function initialized, ready to send metrics")
def process_message(message, tags, timestamp, node_ip):
(
_,
_,
interface_id,
srcaddr,
dstaddr,
_,
_,
protocol,
packets,
_bytes,
start,
end,
action,
log_status,
) = message.split(" ")
detailed_tags = [
"interface_id:%s" % interface_id,
"protocol:%s" % protocol_id_to_name(protocol),
"ip:%s" % node_ip,
] + tags
if srcaddr == node_ip:
detailed_tags.append("direction:outbound")
if dstaddr == node_ip:
detailed_tags.append("direction:inbound")
process_log_status(log_status, detailed_tags, timestamp)
if log_status == "NODATA":
return
process_action(action, detailed_tags, timestamp)
process_duration(start, end, detailed_tags, timestamp)
process_packets(packets, detailed_tags, timestamp)
process_bytes(_bytes, detailed_tags, timestamp)
def compute_node_ip(events):
ip_count = Counter()
for event in events:
src_ip, dest_ip = event["message"].split(" ", 5)[3:5]
if len(src_ip) > 1 and len(dest_ip) > 1: # account for '-'
ip_count[src_ip] += 1
ip_count[dest_ip] += 1
most_comm = ip_count.most_common()
if most_comm:
if most_comm[0][1] > 1: # we have several events
return ip_count.most_common()[0][0]
return "unknown"
def protocol_id_to_name(protocol):
if protocol == "-":
return protocol
protocol_map = {
0: "HOPOPT",
1: "ICMP",
2: "IGMP",
3: "GGP",
4: "IPv4",
5: "ST",
6: "TCP",
7: "CBT",
8: "EGP",
9: "IGP",
10: "BBN-RCC-MON",
11: "NVP-II",
12: "PUP",
13: "ARGUS",
14: "EMCON",
15: "XNET",
16: "CHAOS",
17: "UDP",
18: "MUX",
19: "DCN-MEAS",
20: "HMP",
21: "PRM",
22: "XNS-IDP",
23: "TRUNK-1",
24: "TRUNK-2",
25: "LEAF-1",
26: "LEAF-2",
27: "RDP",
28: "IRTP",
29: "ISO-TP4",
30: "NETBLT",
31: "MFE-NSP",
32: "MERIT-INP",
33: "DCCP",
34: "3PC",
35: "IDPR",
36: "XTP",
37: "DDP",
38: "IDPR-CMTP",
39: "TP++",
40: "IL",
41: "IPv6",
42: "SDRP",
43: "IPv6-Route",
44: "IPv6-Frag",
45: "IDRP",
46: "RSVP",
47: "GRE",
48: "DSR",
49: "BNA",
50: "ESP",
51: "AH",
52: "I-NLSP",
53: "SWIPE",
54: "NARP",
55: "MOBILE",
56: "TLSP",
57: "SKIP",
58: "IPv6-ICMP",
59: "IPv6-NoNxt",
60: "IPv6-Opts",
62: "CFTP",
64: "SAT-EXPAK",
65: "KRYPTOLAN",
66: "RVD",
67: "IPPC",
69: "SAT-MON",
70: "VISA",
71: "IPCV",
72: "CPNX",
73: "CPHB",
74: "WSN",
75: "PVP",
76: "BR-SAT-MON",
77: "SUN-ND",
78: "WB-MON",
79: "WB-EXPAK",
80: "ISO-IP",
81: "VMTP",
82: "SECURE-VMTP",
83: "VINES",
84: "IPTM",
85: "NSFNET-IGP",
86: "DGP",
87: "TCF",
88: "EIGRP",
89: "OSPFIGP",
90: "Sprite-RPC",
91: "LARP",
92: "MTP",
93: "AX.25",
94: "IPIP",
95: "MICP",
96: "SCC-SP",
97: "ETHERIP",
98: "ENCAP",
100: "GMTP",
101: "IFMP",
102: "PNNI",
103: "PIM",
104: "ARIS",
105: "SCPS",
106: "QNX",
107: "A/N",
108: "IPComp",
109: "SNP",
110: "Compaq-Peer",
111: "IPX-in-IP",
112: "VRRP",
113: "PGM",
115: "L2TP",
116: "DDX",
117: "IATP",
118: "STP",
119: "SRP",
120: "UTI",
121: "SMP",
122: "SM",
123: "PTP",
124: "ISIS",
125: "FIRE",
126: "CRTP",
127: "CRUDP",
128: "SSCOPMCE",
129: "IPLT",
130: "SPS",
131: "PIPE",
132: "SCTP",
133: "FC",
134: "RSVP-E2E-IGNORE",
135: "Mobility",
136: "UDPLite",
137: "MPLS-in-IP",
138: "manet",
139: "HIP",
140: "Shim6",
141: "WESP",
142: "ROHC",
}
return protocol_map.get(int(protocol), protocol)
def process_log_status(log_status, tags, timestamp):
stats.increment(
"log_status", tags=["status:%s" % log_status] + tags, timestamp=timestamp
)
def process_action(action, tags, timestamp):
stats.increment("action", tags=["action:%s" % action] + tags, timestamp=timestamp)
def process_duration(start, end, tags, timestamp):
stats.histogram(
"duration.per_request",
int(int(end) - int(start)),
tags=tags,
timestamp=timestamp,
)
def process_packets(packets, tags, timestamp):
try:
stats.histogram(
"packets.per_request", int(packets), tags=tags, timestamp=timestamp
)
stats.increment("packets.total", int(packets), tags=tags, timestamp=timestamp)
except ValueError:
pass
def process_bytes(_bytes, tags, timestamp):
try:
stats.histogram(
"bytes.per_request", int(_bytes), tags=tags, timestamp=timestamp
)
stats.increment("bytes.total", int(_bytes), tags=tags, timestamp=timestamp)
except ValueError:
pass
class Stats(object):
def _initialize(self):
self.counts = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
self.histograms = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))
def __init__(self):
self._initialize()
self.metric_prefix = "aws.vpc.flowlogs"
# limit input size at 3.2MB see https://docs.datadoghq.com/api/latest/metrics/#submit-metrics
# set to 3MB to account for the overhead of the json encoding
self.max_batch_size_bytes = 3000000
def increment(self, metric, value=1, timestamp=None, tags=None):
metric_name = "%s.%s" % (self.metric_prefix, metric)
timestamp = timestamp or int(time.time())
_tags = ",".join(sorted(tags))
self.counts[metric_name][_tags][timestamp] += value
def histogram(self, metric, value=1, timestamp=None, tags=None):
metric_name = "%s.%s" % (self.metric_prefix, metric)
timestamp = timestamp or int(time.time())
_tags = ",".join(sorted(tags))
self.histograms[metric_name][_tags][timestamp].append(value)
def flush(self):
percentiles_to_submit = [0, 50, 90, 95, 99, 100]
series = []
for metric_name, count_payload in self.counts.items():
for tag_set, datapoints in count_payload.items():
points = [(ts, val) for ts, val in datapoints.items()]
series.append(
{
"metric": metric_name,
"points": points,
"type": "count",
"tags": tag_set.split(","),
}
)
for metric_name, histogram_payload in self.histograms.items():
for tag_set, datapoints in histogram_payload.items():
percentiles = defaultdict(list)
for ts, values in datapoints.items():
values.sort()
total_points = len(values)
for pct in percentiles_to_submit:
percentiles[pct].append(
(ts, values[max(0, int((pct - 1) * total_points / 100))])
)
for pct, points in percentiles.items():
metric_suffix = "p%s" % pct
if pct == 0:
metric_suffix = "min"
if pct == 50:
metric_suffix = "median"
if pct == 100:
metric_suffix = "max"
series.append(
{
"metric": "%s.%s" % (metric_name, metric_suffix),
"points": points,
"type": "gauge",
"tags": tag_set.split(","),
}
)
self._initialize()
creds = urlencode(datadog_keys)
url = "%s?%s" % (
datadog_keys.get("api_host", "https://api.%s/api/v1/series" % DD_SITE),
creds,
)
batches = self.batch_series(series)
for batch in batches:
metrics_dict = {
"series": batch,
}
data = json.dumps(metrics_dict).encode("utf-8")
req = Request(url, data, {"Content-Type": "application/json"})
response = urlopen(req)
logger.info(f"INFO Submitted data with status {response.getcode()}")
def batch_series(self, series):
batches = []
batch = []
size_bytes = 0
for s in series:
item_size_bytes = self._sizeof_bytes(s)
if size_bytes + item_size_bytes > self.max_batch_size_bytes:
batches.append(batch)
batch = []
size_bytes = 0
# all items exceeding max_item_size_bytes are dropped here
if item_size_bytes > self.max_batch_size_bytes:
continue
batch.append(s)
size_bytes += item_size_bytes
# add the last batch if it's not empty
if size_bytes > 0:
batches.append(batch)
return batches
def _sizeof_bytes(self, item):
return len(str(item).encode("UTF-8"))
stats = Stats()
def lambda_handler(event, context):
# event is a dict containing a base64 string gzipped
with gzip.GzipFile(
fileobj=BytesIO(base64.b64decode(event["awslogs"]["data"]))
) as decompress_stream:
data = b"".join(BufferedReader(decompress_stream))
event = json.loads(data)
function_arn = context.invoked_function_arn
# 'arn:aws:lambda:us-east-1:1234123412:function:VPCFlowLogs'
region, account = function_arn.split(":", 5)[3:5]
tags = ["region:%s" % region, "aws_account:%s" % account]
unsupported_messages = 0
node_ip = compute_node_ip(event["logEvents"])
for event in event["logEvents"]:
message = event["message"]
if message[0] != "2":
unsupported_messages += 1
continue
timestamp = event["timestamp"] / 1000
process_message(message, tags, timestamp, node_ip)
if unsupported_messages:
logger.info("Unsupported vpc flowlog message type, please contact Datadog")
stats.increment("unsupported_message", value=unsupported_messages, tags=tags)
stats.flush()