@@ -15,6 +15,29 @@ limitations under the License.
15
15
*/
16
16
const axios = require ( 'axios' ) ;
17
17
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 = / P R O V I D E R S \/ ( .* ?\/ .* ?) (?: \/ ) / ;
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
+
18
41
const getEpochTime = function ( timeString ) {
19
42
try {
20
43
let epochTime = new Date ( timeString ) . getTime ( ) ;
@@ -32,6 +55,7 @@ const getTimeStamp = function(message) {
32
55
}
33
56
34
57
const getHECPayload = async function ( message , sourcetype ) {
58
+
35
59
try {
36
60
jsonMessage = JSON . parse ( message ) ;
37
61
} catch ( err ) {
@@ -40,17 +64,25 @@ const getHECPayload = async function(message, sourcetype) {
40
64
"sourcetype" : sourcetype ,
41
65
"event" : message
42
66
}
43
- return payload
67
+ return payload ;
44
68
}
45
69
46
70
// If the JSON contains a records[] array, batch the events for HEC.
47
71
if ( jsonMessage . hasOwnProperty ( 'records' ) ) {
48
72
let payload = ''
73
+
49
74
jsonMessage . records . forEach ( function ( record ) {
50
- recordEvent = {
75
+
76
+ let recordEvent = {
51
77
"sourcetype" : sourcetype ,
52
78
"event" : JSON . stringify ( record )
53
79
}
80
+
81
+ if ( ( record . hasOwnProperty ( 'resourceId' ) ) && ( record . hasOwnProperty ( 'category' ) ) ) {
82
+ // Get the sourcetype
83
+ recordEvent [ "sourcetype" ] = getSourceType ( sourcetype , record . resourceId , record . category ) ;
84
+ }
85
+
54
86
let eventTimeStamp = getTimeStamp ( record ) ;
55
87
if ( eventTimeStamp ) { recordEvent [ "time" ] = eventTimeStamp ; }
56
88
payload += JSON . stringify ( recordEvent ) ;
@@ -75,11 +107,11 @@ const sendToHEC = async function(message, sourcetype) {
75
107
}
76
108
77
109
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 ;
83
115
} ) ;
84
116
}
85
117
0 commit comments