-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] [Endpoint] Add by policy to event filters generator #121407
Merged
dasansol92
merged 5 commits into
elastic:main
from
dasansol92:feat/olm-add_by_policy_to_event_filters_data_generator
Dec 17, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0eb7b63
Unify code and add by policy to event filters generator
dasansol92 769f286
Use new function in TA generator
dasansol92 4bdb86a
Fix ts errors
dasansol92 e5791c7
Remove unused function
dasansol92 22c6b7b
Remove unused import packages
dasansol92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,9 @@ import { | |
EXCEPTION_LIST_URL, | ||
} from '@kbn/securitysolution-list-constants'; | ||
import { KbnClient } from '@kbn/test'; | ||
import { AxiosError, 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 { AxiosError } from 'axios'; | ||
import { HostIsolationExceptionGenerator } from '../../../common/endpoint/data_generators/host_isolation_exception_generator'; | ||
import { setupFleetForEndpoint } from '../../../common/endpoint/data_loaders/setup_fleet_for_endpoint'; | ||
import { GetPolicyListResponse } from '../../../public/management/pages/policy/types'; | ||
import { randomPolicyIdGenerator } from '../common/random_policy_id_generator'; | ||
|
||
export const cli = () => { | ||
run( | ||
|
@@ -74,36 +68,10 @@ const createHostIsolationException: RunFn = async ({ flags, log }) => { | |
const exceptionGenerator = new HostIsolationExceptionGenerator(); | ||
const kbn = new KbnClient({ log, url: flags.kibana as string }); | ||
|
||
log.info('Setting up fleet'); | ||
const fleetResponse = await setupFleetForEndpoint(kbn); | ||
|
||
log.info('Creating Host isolation exceptions list'); | ||
await ensureCreateEndpointHostIsolationExceptionList(kbn); | ||
|
||
// Setup a list of real endpoint policies and return a method to randomly select one | ||
const randomPolicyId: () => string = await (async () => { | ||
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 Host Isolation Exceptions assignment ${i + 1}`, | ||
fleetResponse.endpointPackage.version | ||
) | ||
).integrationPolicies[0].id | ||
); | ||
} | ||
} | ||
|
||
return () => policyIds[randomN(policyIds.length)]; | ||
})(); | ||
const randomPolicyId = await randomPolicyIdGenerator(kbn, log); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
|
||
log.info('Generating exceptions....'); | ||
await Promise.all( | ||
|
@@ -154,16 +122,3 @@ const ensureCreateEndpointHostIsolationExceptionList = async (kbn: KbnClient) => | |
} | ||
}); | ||
}; | ||
|
||
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`, | ||
}, | ||
}); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 🔥 🔥
🙏