-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_app.py
More file actions
169 lines (145 loc) · 7.01 KB
/
Copy pathfunction_app.py
File metadata and controls
169 lines (145 loc) · 7.01 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
import logging
import urllib.request
import gzip
import time
import json
import os
from typing import Optional
import azure.functions as func
DEFAULT_ACTIVITY_LOGS_COLLECTION = 'AZActivityLogs'
ACTIVITY_LOGS_CATEGORIES = ['Administrative', 'Policy']
ENTRA_ID_CATEGORIES = ['AuditLogs', 'ProvisioningLogs', 'UserRiskEvents', 'SignInLogs']
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
FORWARDER_NAME = os.environ.get('FORWARDER_NAME', 'brontoForwarder')
class ConfigurationException(Exception):
pass
class Config:
def __init__(self):
self.default_collection = os.environ.get('BRONTO_COLLECTION')
self.default_dataset = os.environ.get('BRONTO_DATASET')
log_attributes_raw = os.environ.get('LOG_ATTRIBUTES', '')
log_attributes_split = list(map(lambda x : x.split('='), log_attributes_raw.split(',')
if log_attributes_raw else []))
self.log_attributes = {}
self.log_attributes = {item[0]: item[1] for item in log_attributes_split
if len(item) == 2 and item[0] and item[1]}
self.bronto_ingestion_endpoint = os.environ.get('BRONTO_INGESTION_ENDPOINT')
if not self.bronto_ingestion_endpoint:
raise ConfigurationException('BRONTO_INGESTION_ENDPOINT is not a valid Bronto endpoint')
self.bronto_api_key = os.environ.get('BRONTO_API_KEY')
if not self.bronto_api_key:
raise ConfigurationException('BRONTO_API_KEY is not a valid API Key')
self.max_payload_size = os.environ.get('MAX_BRONTO_PAYLOAD_SIZE_BYTES', 5_000_000)
class BrontoDestinationProvider:
def __init__(self, default_collection=None, default_dataset=None):
self.collection = default_collection
self.dataset = default_dataset
def get_collection(self, entry) -> Optional[str]:
category = entry.get('Category')
if category is None:
category = entry.get('category')
if category is None:
return self.collection
if category in ACTIVITY_LOGS_CATEGORIES:
return DEFAULT_ACTIVITY_LOGS_COLLECTION
elif category == 'FunctionAppLogs':
return category
elif category.lower().startswith('nsp'):
return 'NetworkSecurityPerimeter'
elif category in ENTRA_ID_CATEGORIES:
return 'EntraID'
elif category.lower().startswith('advancedhunting'):
return 'MSDefender'
return self.collection
def get_dataset(self, entry) -> Optional[str]:
category = entry.get('Category')
if category is None:
category = entry.get('category')
if category is None:
return self.dataset
if category in ACTIVITY_LOGS_CATEGORIES:
return category
elif category == 'FunctionAppLogs':
return entry.get('appName')
elif category.startswith('Nsp'):
return 'AccessLogs'
elif category.startswith('nsp'):
return 'ProfilesLogs'
elif category in ENTRA_ID_CATEGORIES:
return category
elif category.lower().startswith('advancedhunting'):
return category
return self.dataset
class BrontoClient:
def __init__(self, destination_provider: BrontoDestinationProvider, config: Config):
self.dest_provider = destination_provider
self.max_payload_size = config.max_payload_size
self.ingestion_endpoint = config.bronto_ingestion_endpoint
self.api_key = config.bronto_api_key
self.log_attributes = config.log_attributes
self.headers = {
'Content-Encoding': 'gzip',
'Content-Type': 'application/json',
'User-Agent': 'Fluent-Bit', # TODO: update ingestion to support a dedicated user agent, e.g. bronto-azure-forwarder
'x-bronto-api-key': self.api_key
}
def _send_batch(self, compressed_batch):
request = urllib.request.Request(self.ingestion_endpoint, data=compressed_batch, headers=self.headers)
attempt = 0
max_attempts = 5
with urllib.request.urlopen(request) as resp:
if resp.status != 200 and attempt < max_attempts:
attempt += 1
delay_sec = attempt * 10
logging.warning('Data sending failed. attempt=%s, max_attempts=%s, status=%s, reason=%s',
attempt, max_attempts, resp.status, resp.reason)
time.sleep(delay_sec)
self._send_batch(compressed_batch)
def enrich(self, entry):
entry_w_attrs = {'log': entry}
entry_w_attrs.update({k: v for k,v in self.log_attributes})
collection = self.dest_provider.get_collection(entry)
if collection:
entry_w_attrs.update({'service.namespace': collection})
dataset = self.dest_provider.get_dataset(entry)
if dataset:
entry_w_attrs.update({'service.name': dataset})
return entry_w_attrs
def send_data(self, batch):
data = '\n'.join([json.dumps(self.enrich(entry)) for entry in batch])
compressed_data = gzip.compress(data.encode())
if len(compressed_data) < self.max_payload_size:
logging.info('Batch compressed. compressed_batch_size=%s', len(compressed_data))
self._send_batch(compressed_data)
return
logging.warning('Compressed batch is too large. Splitting in half. compressed_batch_size=%s, max_compressed_batch=%s',
len(compressed_data), self.max_payload_size)
batch_size = len(batch)
half = int(batch_size / 2)
self.send_data(batch[:half])
self.send_data(batch[half:])
@app.function_name(name=FORWARDER_NAME)
@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="%EVENTHUB_NAME%",
connection="EVENTHUB_CONNECT_STRING")
def forward(azeventhub: func.EventHubEvent):
config = Config()
data = azeventhub.get_body().decode('utf-8')
try:
data = json.loads(data)
except json.decoder.JSONDecodeError as _:
logging.info('EVENT=%s', data)
data = {'records' : [{'Category': 'unsupported', 'Message': data}]}
if type(data) == list:
logging.warning('Unexpected payload type: lists are not accepted. Maybe some unsupported input sources are generating log data.')
return
default_collection = config.default_collection
default_dataset = config.default_dataset
destination_provider = BrontoDestinationProvider(default_collection, default_dataset)
bronto_client = BrontoClient(destination_provider, config)
records = data.get('records')
if records is None:
logging.warning('No records in message')
return
elif type(records) != list:
logging.warning('list type expected for `records`. found_type=%s', type(records).__name__)
bronto_client.send_data(records)