Skip to content

Commit 8781545

Browse files
committed
Added sourcetype handling
1 parent 69cfc2d commit 8781545

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

event-hubs-hec/helpers/splunk.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ limitations under the License.
1515
*/
1616
const axios = require('axios');
1717

18+
const getSourceType = function(sourcetype, resourceId, category) {
19+
20+
// If this is an AAD sourcetype, append the category to the sourcetype and return
21+
let aadSourcetypes = [process.env["AAD_LOG_SOURCETYPE"], process.env["AAD_NON_INTERACTIVE_SIGNIN_LOG_SOURCETYPE"], process.env["AAD_SERVICE_PRINCIPAL_SIGNIN_LOG_SOURCETYPE"], process.env["AAD_PROVISIONING_LOG_SOURCETYPE"]];
22+
if(aadSourcetypes.indexOf(sourcetype) > -1) {
23+
return `${sourcetype}:${category.toLowerCase()}`;
24+
}
25+
26+
// Set the sourcetype based on the resourceId
27+
let sourcetypePattern = /PROVIDERS\/(.*?\/.*?)(?:\/)/;
28+
try {
29+
let st = resourceId.match(sourcetypePattern)[1]
30+
.replace("MICROSOFT.", "azure:")
31+
.replace('.', ':')
32+
.replace('/', ':')
33+
.toLowerCase();
34+
return `${st}:${category.toLowerCase()}`;
35+
} catch(err) {
36+
// Could not detrmine the sourcetype from the resourceId
37+
return sourcetype;
38+
}
39+
}
40+
1841
const getEpochTime = function(timeString) {
1942
try {
2043
let epochTime = new Date(timeString).getTime();
@@ -32,6 +55,7 @@ const getTimeStamp = function(message) {
3255
}
3356

3457
const getHECPayload = async function(message, sourcetype) {
58+
3559
try {
3660
jsonMessage = JSON.parse(message);
3761
} catch (err) {
@@ -40,17 +64,25 @@ const getHECPayload = async function(message, sourcetype) {
4064
"sourcetype": sourcetype,
4165
"event": message
4266
}
43-
return payload
67+
return payload;
4468
}
4569

4670
// If the JSON contains a records[] array, batch the events for HEC.
4771
if(jsonMessage.hasOwnProperty('records')) {
4872
let payload = ''
73+
4974
jsonMessage.records.forEach(function(record) {
50-
recordEvent = {
75+
76+
let recordEvent = {
5177
"sourcetype": sourcetype,
5278
"event": JSON.stringify(record)
5379
}
80+
81+
if((record.hasOwnProperty('resourceId')) && (record.hasOwnProperty('category'))) {
82+
// Get the sourcetype
83+
recordEvent["sourcetype"] = getSourceType(sourcetype, record.resourceId, record.category);
84+
}
85+
5486
let eventTimeStamp = getTimeStamp(record);
5587
if(eventTimeStamp) { recordEvent["time"] = eventTimeStamp; }
5688
payload += JSON.stringify(recordEvent);
@@ -75,11 +107,11 @@ const sendToHEC = async function(message, sourcetype) {
75107
}
76108

77109
await getHECPayload(message, sourcetype)
78-
.then(payload => {
79-
return axios.post(process.env["SPLUNK_HEC_URL"], payload, {headers: headers});
80-
})
81-
.catch(err => {
82-
throw err;
110+
.then(payload => {
111+
return axios.post(process.env["SPLUNK_HEC_URL"], payload, {headers: headers});
112+
})
113+
.catch(err => {
114+
throw err;
83115
});
84116
}
85117

0 commit comments

Comments
 (0)