-
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] status api (#134356)
- Loading branch information
Showing
14 changed files
with
763 additions
and
242 deletions.
There are no files selected for viewing
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
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
76 changes: 76 additions & 0 deletions
76
x-pack/plugins/cloud_security_posture/server/lib/fleet_util.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,76 @@ | ||
/* | ||
* 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 { uniq, map } from 'lodash'; | ||
import type { SavedObjectsClientContract } from '@kbn/core/server'; | ||
import type { | ||
PackagePolicyServiceInterface, | ||
AgentPolicyServiceInterface, | ||
AgentService, | ||
} from '@kbn/fleet-plugin/server'; | ||
import type { | ||
GetAgentPoliciesResponseItem, | ||
PackagePolicy, | ||
AgentPolicy, | ||
ListResult, | ||
} from '@kbn/fleet-plugin/common'; | ||
import { | ||
BENCHMARK_PACKAGE_POLICY_PREFIX, | ||
BenchmarksQueryParams, | ||
} from '../../common/schemas/benchmark'; | ||
|
||
export const PACKAGE_POLICY_SAVED_OBJECT_TYPE = 'ingest-package-policies'; | ||
|
||
const getPackageNameQuery = (packageName: string, benchmarkFilter?: string): string => { | ||
const integrationNameQuery = `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${packageName}`; | ||
const kquery = benchmarkFilter | ||
? `${integrationNameQuery} AND ${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.name: *${benchmarkFilter}*` | ||
: integrationNameQuery; | ||
|
||
return kquery; | ||
}; | ||
|
||
export const addRunningAgentToAgentPolicy = async ( | ||
agentService: AgentService, | ||
agentPolicies: AgentPolicy[] | ||
): Promise<GetAgentPoliciesResponseItem[]> => { | ||
if (!agentPolicies.length) return []; | ||
|
||
return Promise.all( | ||
agentPolicies.map((agentPolicy) => | ||
agentService.asInternalUser | ||
.getAgentStatusForAgentPolicy(agentPolicy.id) | ||
.then((agentStatus) => ({ | ||
...agentPolicy, | ||
agents: agentStatus.total, | ||
})) | ||
) | ||
); | ||
}; | ||
|
||
export const getCspAgentPolicies = async ( | ||
soClient: SavedObjectsClientContract, | ||
packagePolicies: PackagePolicy[], | ||
agentPolicyService: AgentPolicyServiceInterface | ||
): Promise<AgentPolicy[]> => | ||
agentPolicyService.getByIds(soClient, uniq(map(packagePolicies, 'policy_id'))); | ||
|
||
export const getCspPackagePolicies = ( | ||
soClient: SavedObjectsClientContract, | ||
packagePolicyService: PackagePolicyServiceInterface, | ||
packageName: string, | ||
queryParams: Partial<BenchmarksQueryParams> | ||
): Promise<ListResult<PackagePolicy>> => { | ||
const sortField = queryParams.sort_field?.replaceAll(BENCHMARK_PACKAGE_POLICY_PREFIX, ''); | ||
|
||
return packagePolicyService.list(soClient, { | ||
kuery: getPackageNameQuery(packageName, queryParams.benchmark_name), | ||
page: queryParams.page, | ||
perPage: queryParams.per_page, | ||
sortField, | ||
sortOrder: queryParams.sort_order, | ||
}); | ||
}; |
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/cloud_security_posture/server/lib/is_latest_findings_index_exists.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,30 @@ | ||
/* | ||
* 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 { ElasticsearchClient } from '@kbn/core/server'; | ||
import { CspAppContext } from '../plugin'; | ||
import { LATEST_FINDINGS_INDEX_DEFAULT_NS } from '../../common/constants'; | ||
|
||
export const isLatestFindingsIndexExists = async ( | ||
esClient: ElasticsearchClient, | ||
logger: CspAppContext['logger'] | ||
): Promise<boolean> => { | ||
try { | ||
const queryResult = await esClient.search({ | ||
index: LATEST_FINDINGS_INDEX_DEFAULT_NS, | ||
query: { | ||
match_all: {}, | ||
}, | ||
size: 1, | ||
}); | ||
|
||
return !!queryResult.hits.hits.length; | ||
} catch (e) { | ||
logger.error(e.message); | ||
return false; | ||
} | ||
}; |
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.