1- const { Client } = require ( ' @elastic/elasticsearch' ) ;
1+ const { Client } = require ( " @elastic/elasticsearch" ) ;
22
3- const flatMap = require ( ' lodash.flatmap' ) ;
4- const chunk = require ( ' lodash.chunk' ) ;
3+ const flatMap = require ( " lodash.flatmap" ) ;
4+ const chunk = require ( " lodash.chunk" ) ;
55
6- const client = new Client ( { node : process . env [ 'ELASTICSEARCH_ADDRESS' ] } ) ;
6+ const authParams = { } ;
7+
8+ const username = process . env [ "ELASTICSEARCH_USERNAME" ] ;
9+ const password = process . env [ "ELASTICSEARCH_PASSWORD" ] ;
10+ const apiKeyId = process . env [ "ELASTICSEARCH_APIKEY_ID" ] ;
11+ const apiKey = process . env [ "ELASTICSEARCH_APIKEY" ] ;
12+
13+ if ( apiKeyId !== "" && apiKey !== "" ) {
14+ console . log ( "Using API Key for Authentication" ) ;
15+ authParams . auth = {
16+ id : apiKeyId ,
17+ api_key : apiKey ,
18+ } ;
19+ } else if ( username !== "" && password !== "" ) {
20+ console . log ( "Using Username/Password for Authentication" ) ;
21+ authParams . auth = {
22+ username,
23+ password,
24+ } ;
25+ } else {
26+ console . log (
27+ "No Authentication credentials provided. Assuming Elasticsearch doesn't require Auth."
28+ ) ;
29+ }
30+
31+ const client = new Client ( {
32+ node : process . env [ "ELASTICSEARCH_ADDRESS" ] ,
33+ ...authParams ,
34+ } ) ;
735
836async function persist ( {
937 getFindings,
1038 scan,
1139 now = new Date ( ) ,
12- tenant = process . env [ ' NAMESPACE' ] ,
40+ tenant = process . env [ " NAMESPACE" ] ,
1341} ) {
1442 const findings = await getFindings ( ) ;
1543
1644 console . log ( `Persisting ${ findings . length } findings to Elasticsearch` ) ;
45+ console . log (
46+ `Using Elasticsearch Instance at "${ process . env [ "ELASTICSEARCH_ADDRESS" ] } "`
47+ ) ;
1748
1849 const timeStamp = now . toISOString ( ) . substr ( 0 , 10 ) ;
1950 const indexName = `securecodebox_${ tenant } _${ timeStamp } ` ;
@@ -31,8 +62,8 @@ async function persist({
3162 await client . index ( {
3263 index : indexName ,
3364 body : {
34- ' @timestamp' : now ,
35- type : ' scan' ,
65+ " @timestamp" : now ,
66+ type : " scan" ,
3667 id : scan . metadata . uid ,
3768 name : scan . metadata . name ,
3869 scan_type : scan . spec . scanType ,
@@ -51,12 +82,12 @@ async function persist({
5182 findingChunk . length
5283 } findings to Elasticsearch`
5384 ) ;
54- const body = flatMap ( findingChunk , doc => [
85+ const body = flatMap ( findingChunk , ( doc ) => [
5586 { index : { _index : indexName } } ,
5687 {
5788 ...doc ,
58- ' @timestamp' : now ,
59- type : ' finding' ,
89+ " @timestamp" : now ,
90+ type : " finding" ,
6091 scan_id : scan . metadata . uid ,
6192 scan_name : scan . metadata . name ,
6293 scan_type : scan . spec . scanType ,
@@ -67,7 +98,7 @@ async function persist({
6798 const { body : bulkResponse } = await client . bulk ( { refresh : true , body } ) ;
6899
69100 if ( bulkResponse . errors ) {
70- console . error ( ' Bulk Request had errors:' ) ;
101+ console . error ( " Bulk Request had errors:" ) ;
71102 console . log ( bulkResponse ) ;
72103 }
73104 }
0 commit comments