-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Posture] CSPM telemetry accounts stats
- Loading branch information
Showing
7 changed files
with
358 additions
and
39 deletions.
There are no files selected for viewing
241 changes: 241 additions & 0 deletions
241
...lugins/cloud_security_posture/server/lib/telemetry/collectors/accounts_stats_collector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import type { Logger } from '@kbn/core/server'; | ||
import type { SearchRequest } from '@elastic/elasticsearch/lib/api/types'; | ||
import { calculatePostureScore } from '../../../routes/compliance_dashboard/get_stats'; | ||
import type { CspmAccountsStats } from './types'; | ||
import { LATEST_FINDINGS_INDEX_DEFAULT_NS } from '../../../../common/constants'; | ||
|
||
interface Value { | ||
value: number; | ||
} | ||
|
||
interface DocCount { | ||
doc_count: number; | ||
} | ||
|
||
interface BenchmarkName { | ||
metrics: { 'rule.benchmark.name': string }; | ||
} | ||
|
||
interface BenchmarkId { | ||
metrics: { 'rule.benchmark.id': string }; | ||
} | ||
|
||
interface BenchmarkVersion { | ||
metrics: { 'rule.benchmark.version': string }; | ||
} | ||
|
||
interface AccountsStats { | ||
accounts: { | ||
buckets: AccountEntity[]; | ||
}; | ||
} | ||
interface AccountEntity { | ||
key: string; // account_id | ||
doc_count: number; // latest findings doc count | ||
passed_findings_count: DocCount; | ||
failed_findings_count: DocCount; | ||
benchmark_name: { top: BenchmarkName[] }; | ||
benchmark_id: { top: BenchmarkId[] }; | ||
benchmark_version: { top: BenchmarkVersion[] }; | ||
agents_count: Value; | ||
nodes_count: Value; | ||
pods_count: Value; | ||
resources: { | ||
pods_count: Value; | ||
}; | ||
} | ||
|
||
const getAccountsStatsQuery = (index: string): SearchRequest => ({ | ||
index, | ||
query: { | ||
match_all: {}, | ||
}, | ||
aggs: { | ||
accounts: { | ||
terms: { | ||
field: 'cluster_id', | ||
order: { | ||
_count: 'desc', | ||
}, | ||
size: 100, | ||
}, | ||
aggs: { | ||
nodes_count: { | ||
cardinality: { | ||
field: 'host.name', | ||
}, | ||
}, | ||
agents_count: { | ||
cardinality: { | ||
field: 'agent.id', | ||
}, | ||
}, | ||
benchmark_id: { | ||
top_metrics: { | ||
metrics: { | ||
field: 'rule.benchmark.id', | ||
}, | ||
size: 1, | ||
sort: { | ||
'@timestamp': 'desc', | ||
}, | ||
}, | ||
}, | ||
benchmark_version: { | ||
top_metrics: { | ||
metrics: { | ||
field: 'rule.benchmark.version', | ||
}, | ||
size: 1, | ||
sort: { | ||
'@timestamp': 'desc', | ||
}, | ||
}, | ||
}, | ||
benchmark_name: { | ||
top_metrics: { | ||
metrics: { | ||
field: 'rule.benchmark.name', | ||
}, | ||
size: 1, | ||
sort: { | ||
'@timestamp': 'desc', | ||
}, | ||
}, | ||
}, | ||
passed_findings_count: { | ||
filter: { | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
term: { | ||
'result.evaluation': 'passed', | ||
}, | ||
}, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
failed_findings_count: { | ||
filter: { | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
term: { | ||
'result.evaluation': 'failed', | ||
}, | ||
}, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
resources: { | ||
filter: { | ||
bool: { | ||
filter: [ | ||
{ | ||
bool: { | ||
should: [ | ||
{ | ||
term: { | ||
'resource.sub_type': 'Pod', | ||
}, | ||
}, | ||
], | ||
minimum_should_match: 1, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
aggs: { | ||
pods_count: { | ||
cardinality: { | ||
field: 'resource.id', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
size: 0, | ||
_source: false, | ||
}); | ||
|
||
const getCspmAccountsStats = ( | ||
aggregatedResourcesStats: AccountsStats, | ||
logger: Logger | ||
): CspmAccountsStats[] => { | ||
const accounts = aggregatedResourcesStats.accounts.buckets; | ||
|
||
const cspmAccountsStats = accounts.map((account) => ({ | ||
account_id: account.key, | ||
latest_findings_doc_count: account.doc_count, | ||
posture_score: calculatePostureScore( | ||
account.passed_findings_count.doc_count, | ||
account.failed_findings_count.doc_count | ||
), | ||
passed_findings_count: account.passed_findings_count.doc_count, | ||
failed_findings_count: account.failed_findings_count.doc_count, | ||
benchmark_name: account.benchmark_name.top[0].metrics['rule.benchmark.name'], | ||
benchmark_id: account.benchmark_id.top[0].metrics['rule.benchmark.id'], | ||
benchmark_version: account.benchmark_version.top[0].metrics['rule.benchmark.version'], | ||
agents_count: account.agents_count.value, | ||
nodes_count: account.nodes_count.value, | ||
pods_count: account.resources.pods_count.value, | ||
})); | ||
logger.info('CSPM telemetry: accounts stats was sent'); | ||
|
||
return cspmAccountsStats; | ||
}; | ||
|
||
export const getAccountsStats = async ( | ||
esClient: ElasticsearchClient, | ||
logger: Logger | ||
): Promise<CspmAccountsStats[]> => { | ||
try { | ||
const isIndexExists = await esClient.indices.exists({ | ||
index: LATEST_FINDINGS_INDEX_DEFAULT_NS, | ||
}); | ||
|
||
if (isIndexExists) { | ||
const accountsStatsResponse = await esClient.search<unknown, AccountsStats>( | ||
getAccountsStatsQuery(LATEST_FINDINGS_INDEX_DEFAULT_NS) | ||
); | ||
|
||
const cspmAccountsStats = accountsStatsResponse.aggregations | ||
? getCspmAccountsStats(accountsStatsResponse.aggregations, logger) | ||
: []; | ||
|
||
return cspmAccountsStats; | ||
} | ||
|
||
return []; | ||
} catch (e) { | ||
logger.error(`Failed to get resources stats ${e}`); | ||
return []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.