-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathsend_logs_api_gateway.py
More file actions
65 lines (54 loc) · 2.59 KB
/
send_logs_api_gateway.py
File metadata and controls
65 lines (54 loc) · 2.59 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
import json
import urllib3
import base64
from botocore.exceptions import ClientError
from datetime import datetime
http = urllib3.PoolManager()
def send_logs_api_gateway(message):
url = message.get('logsHttpEndpoint')
apiKey = message.get('logsHttpEndpointKey')
streamName = message.get('logsHttpEndpointStreamName')
streamPartitionKey = message.get('logsHttpEndpointStreamPartitionKey')
if ((url is not None) and (apiKey is not None) and (streamName is not None) and (streamPartitionKey is not None)):
findingKey = message.get('findingKey')
execution_time = datetime.now()
dome9AccountId = message.get('dome9AccountId')
vendor = message.get('vendor')
accountId = message.get('Account id')
executionId = message.get('executionId')
for bot in message.get('Rules violations found', []):
if "Execution status" in bot:
bot["ExecutionStatus"] = bot.pop("Execution status")
if "Bot message" in bot:
bot["BotMessage"] = bot.pop("Bot message")
logMessage = {
"logType": "feedback",
"dome9AccountId": dome9AccountId,
"vendor": vendor,
"findingKey": findingKey,
"envCloudAccountId": accountId,
"executionId": executionId,
"remediationInfo": bot,
"executionTime": str(execution_time)
}
json_bytes = (json.dumps(logMessage) + '\n').encode('utf-8')
# Encode the JSON bytes as base64
base64_bytes = base64.b64encode(json_bytes)
# Convert the base64-encoded bytes to a string
base64_string = base64_bytes.decode('utf-8')
for bot in message.get('Rules violations found', []):
del bot['ID']
del bot['Name']
headers = {"Content-Type": "application/json", "x-api-key": apiKey}
data = {"Data": base64_string,
"PartitionKey": streamPartitionKey,
"StreamName": streamName}
try:
response = http.request("POST", url, headers=headers, body=json.dumps(data))
except ClientError as e:
print(f'bot feedback Failed set post request-' + e)
resp = json.loads(response.data.decode('utf-8'))
if (response.status == 200 and "SequenceNumber" in resp and "ShardId" in resp):
print(f'{findingKey} - bot feedback was reported successfully')
else:
print(f'{findingKey} bot feedback Failed {response.text}')