-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (98 loc) · 3.87 KB
/
index.js
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
/*This code is a Cloudflare Worker that listens for incoming HTTP POST requests.
When a request is received, it calls the parseAlerts function to parse the request body and extract certain pieces of data.
It then formats this data into a string and uploads it to an Amazon S3 bucket as a file named alert-${alertId}.txt, where alertId is the alert_id field in the event data.
*/
import { AwsClient } from 'aws4fetch';
async function postToS3(init, alertId) {
// Set AWS credentials and region
const ACCESS_KEY_ID = AWS_ACCESS_KEY_ID;
const SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY;
const REGION = 'us-east-1';
// Create a new instance of AwsClient
const clientConfig = {
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
region: REGION
};
const awsClient = new AwsClient(clientConfig);
// Set up request options
const bucketName = 'area1alertsbucket';
const filePath = `alert-${alertId}.txt`;
const url = `https://${bucketName}.s3.${REGION}.amazonaws.com/${filePath}`;
const requestOptions = {
method: 'PUT',
body: init
};
// Send request using awsClient
const response = await awsClient.fetch(url, requestOptions);
}
async function parseAlerts(request) {
// Parse the request body and extract event data
const jsonBodyRequest = JSON.stringify(await request.json());
const jsonBody = JSON.parse(jsonBodyRequest);
const event = jsonBody.event;
//console.log(event); // log the event object to the console
const finalDisposition = event.final_disposition;
const serverName = event.smtp_helo_server_name;
const envelopeToArray = event.envelope_to;
const toArray = event.to;
// Create an array of recipients in the format "display name" <email>
let recipientsArray = [];
if (event.envelope_to && event.to && event.envelope_to.length > 0 && event.to.length > 0) {
for (let i = 0; i < event.to.length; i++) {
let recipient = event.to[i];
if (event.envelope_to[i]) {
recipient = `"${event.to[i]}" <${event.envelope_to[i]}>`;
}
recipientsArray.push(recipient);
}
}
const alertReasons = event.alert_reasons ? event.alert_reasons.join('\n') : '';
const envelopeFrom = event.envelope_from;
const from = event.from;
const fromName = event.from_name;
// Set sender based on whether a display name is provided
const sender = fromName ? `"${fromName}" <${from}>` : `<${from}>`;
const subject = event.subject;
const messageId = event.message_id;
const ts = event.ts;
const alertId = event.alert_id;
// Format event data for output
const output = `
Disposition:${finalDisposition}
Date:${ts && ts.replace ? ts.replace('T', ' ') : ''}
From:${sender}
To:${recipientsArray.join(', ')}
Subject:${subject}
Message-ID:${messageId}
Alert Reasons:${alertReasons}
Alert ID:${alertId}
`;
// Parse the output string and create a JSON object
const body = {
text: output
};
const init = {
body: JSON.stringify(body),
headers: {
'content-type': 'application/json;charset=UTF-8'
}
};
// Upload data to S3
await postToS3(init, alertId);
return new Response('');
}
//The code is written in JavaScript and uses the fetch function, which is a standard part of the Web API available to Cloudflare Workers.
//In addition, the code has an addEventListener function with the first argument set to 'fetch', which is a common pattern for handling HTTP requests in Cloudflare Workers.
//This is the Event Listener waiting for alerts to arrive from Area 1
addEventListener('fetch', (event) => {
if (event && event.request && event.request.method) {
const { request } = event;
if (request.method === 'POST') {
return event.respondWith(parseAlerts(request));
} else if (request.method === 'GET') {
// Return a blank page if you try to load the workers page directly and do nothing
return event.respondWith(new Response(''));
}
}
});