-
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.
[Security Solution] [Endpoint] Add by policy to event filters generat…
…or (#121407) * Unify code and add by policy to event filters generator * Use new function in TA generator * Fix ts errors * Remove unused function * Remove unused import packages
- Loading branch information
1 parent
e7899ad
commit 0ad1b80
Showing
5 changed files
with
85 additions
and
102 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
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/security_solution/scripts/endpoint/common/random_policy_id_generator.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,61 @@ | ||
/* | ||
* 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 { ToolingLog } from '@kbn/dev-utils'; | ||
import { KbnClient } from '@kbn/test'; | ||
import { AxiosResponse } from 'axios'; | ||
import { indexFleetEndpointPolicy } from '../../../common/endpoint/data_loaders/index_fleet_endpoint_policy'; | ||
import { | ||
PACKAGE_POLICY_API_ROUTES, | ||
PACKAGE_POLICY_SAVED_OBJECT_TYPE, | ||
} from '../../../../fleet/common/constants'; | ||
import { setupFleetForEndpoint } from '../../../common/endpoint/data_loaders/setup_fleet_for_endpoint'; | ||
import { GetPolicyListResponse } from '../../../public/management/pages/policy/types'; | ||
|
||
const fetchEndpointPolicies = ( | ||
kbnClient: KbnClient | ||
): Promise<AxiosResponse<GetPolicyListResponse>> => { | ||
return kbnClient.request<GetPolicyListResponse>({ | ||
method: 'GET', | ||
path: PACKAGE_POLICY_API_ROUTES.LIST_PATTERN, | ||
query: { | ||
perPage: 100, | ||
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name: endpoint`, | ||
}, | ||
}); | ||
}; | ||
|
||
// Setup a list of real endpoint policies and return a method to randomly select one | ||
export const randomPolicyIdGenerator: ( | ||
kbn: KbnClient, | ||
log: ToolingLog | ||
) => Promise<() => string> = async (kbn, log) => { | ||
log.info('Setting up fleet'); | ||
const fleetResponse = await setupFleetForEndpoint(kbn); | ||
|
||
log.info('Generarting test policies...'); | ||
const randomN = (max: number): number => Math.floor(Math.random() * max); | ||
const policyIds: string[] = | ||
(await fetchEndpointPolicies(kbn)).data.items.map((policy) => policy.id) || []; | ||
|
||
// If the number of existing policies is less than 5, then create some more policies | ||
if (policyIds.length < 5) { | ||
for (let i = 0, t = 5 - policyIds.length; i < t; i++) { | ||
policyIds.push( | ||
( | ||
await indexFleetEndpointPolicy( | ||
kbn, | ||
`Policy for exceptions assignment ${i + 1}`, | ||
fleetResponse.endpointPackage.version | ||
) | ||
).integrationPolicies[0].id | ||
); | ||
} | ||
} | ||
|
||
return () => policyIds[randomN(policyIds.length)]; | ||
}; |
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