Skip to content

Commit

Permalink
use custom document generator
Browse files Browse the repository at this point in the history
  • Loading branch information
szwarckonrad committed Apr 17, 2023
1 parent 12874a1 commit fcc702f
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface GetCustomEndpointMetadataGeneratorOptions {
version: string;
/** OS type for the generated endpoint hosts */
os: 'macOS' | 'windows' | 'linux';
isolation: boolean;
}

/**
Expand All @@ -33,6 +34,7 @@ export class EndpointMetadataGenerator extends BaseDataGenerator {
static custom({
version,
os,
isolation,
}: Partial<GetCustomEndpointMetadataGeneratorOptions> = {}): typeof EndpointMetadataGenerator {
return class extends EndpointMetadataGenerator {
generate(overrides: DeepPartial<HostMetadataInterface> = {}): HostMetadataInterface {
Expand All @@ -54,6 +56,9 @@ export class EndpointMetadataGenerator extends BaseDataGenerator {
set(overrides, 'host.os', EndpointMetadataGenerator.windowsOSFields);
}
}
if (isolation !== undefined) {
set(overrides, 'Endpoint.state.isolation', isolation);
}

return super.generate(overrides);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export async function indexEndpointHostDocs({
policyResponseIndex,
enrollFleet,
generator,
disableEndpointActionsForHost,
withResponseActions = true,
}: {
numDocs: number;
client: Client;
Expand All @@ -98,7 +98,7 @@ export async function indexEndpointHostDocs({
policyResponseIndex: string;
enrollFleet: boolean;
generator: EndpointDocGenerator;
disableEndpointActionsForHost?: boolean;
withResponseActions?: boolean;
}): Promise<IndexedHostsResponse> {
const timeBetweenDocs = 6 * 3600 * 1000; // 6 hours between metadata documents
const timestamp = new Date().getTime();
Expand Down Expand Up @@ -193,7 +193,7 @@ export async function indexEndpointHostDocs({
},
};

if (!disableEndpointActionsForHost) {
if (withResponseActions) {
// Create some fleet endpoint actions and .logs-endpoint actions for this Host
const actionsResponse = await indexEndpointAndFleetActionsForHost(
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,14 @@ export class EndpointDocGenerator extends BaseDataGenerator {
*
* @param seed either a string to seed the random number generator or a random number generator function
* @param MetadataGenerator
* @param endpointIsolated
*/
constructor(
seed: string | seedrandom.prng = Math.random().toString(),
MetadataGenerator: typeof EndpointMetadataGenerator = EndpointMetadataGenerator,
endpointIsolated?: boolean
MetadataGenerator: typeof EndpointMetadataGenerator = EndpointMetadataGenerator
) {
super(seed);
this.metadataGenerator = new MetadataGenerator(seed);
this.commonInfo = this.createHostData(endpointIsolated);
this.commonInfo = this.createHostData();
}

/**
Expand Down Expand Up @@ -410,11 +408,10 @@ export class EndpointDocGenerator extends BaseDataGenerator {
};
}

private createHostData(endpointIsolated?: boolean): CommonHostInfo {
private createHostData(): CommonHostInfo {
const { agent, elastic, host, Endpoint } = this.metadataGenerator.generate({
Endpoint: {
policy: { applied: this.randomChoice(APPLIED_POLICIES) },
state: { isolation: endpointIsolated },
},
});

Expand Down
13 changes: 4 additions & 9 deletions x-pack/plugins/security_solution/common/endpoint/index_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ export type IndexedHostsAndAlertsResponse = IndexedHostsResponse;
* @param fleet
* @param options
* @param DocGenerator
* @param disableEndpointActionsForHost
* @param bothIsolatedAndNormalEndpoints
* @param endpointIsolated
* @param withResponseActions
*/
export async function indexHostsAndAlerts(
client: Client,
Expand All @@ -66,9 +64,7 @@ export async function indexHostsAndAlerts(
fleet: boolean,
options: TreeOptions = {},
DocGenerator: typeof EndpointDocGenerator = EndpointDocGenerator,
disableEndpointActionsForHost = false,
bothIsolatedAndNormalEndpoints = false,
endpointIsolated?: boolean
withResponseActions = true
): Promise<IndexedHostsAndAlertsResponse> {
const random = seedrandom(seed);
const epmEndpointPackage = await getEndpointPackageInfo(kbnClient);
Expand Down Expand Up @@ -109,8 +105,7 @@ export async function indexHostsAndAlerts(
}

for (let i = 0; i < numHosts; i++) {
const isolateHost = bothIsolatedAndNormalEndpoints && i % 2 === 0;
const generator = new DocGenerator(random, undefined, isolateHost ? true : endpointIsolated);
const generator = new DocGenerator(random, undefined);
const indexedHosts = await indexEndpointHostDocs({
numDocs,
client,
Expand All @@ -121,7 +116,7 @@ export async function indexHostsAndAlerts(
policyResponseIndex,
enrollFleet: fleet,
generator,
disableEndpointActionsForHost,
withResponseActions,
});

mergeAndAppendArrays(response, indexedHosts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

import type { CasePostRequest } from '@kbn/cases-plugin/common/api';
import type { IndexedEndpointPolicyResponse } from '../../../common/endpoint/data_loaders/index_endpoint_policy_response';
import type { HostPolicyResponse } from '../../../common/endpoint/types';
import type { IndexEndpointHostsCyTaskOptions } from './types';
import type {
HostPolicyResponse,
LogsEndpointActionResponse,
} from '../../../common/endpoint/types';
import type { IndexEndpointHostsCyTaskOptions, HostActionResponse } from './types';
import type {
DeleteIndexedFleetEndpointPoliciesResponse,
IndexedFleetEndpointPolicyResponse,
Expand Down Expand Up @@ -115,6 +118,12 @@ declare global {
arg: IndexedEndpointPolicyResponse,
options?: Partial<Loggable & Timeoutable>
): Chainable<null>;

task(
name: 'sendHostActionResponse',
arg: HostActionResponse,
options?: Partial<Loggable & Timeoutable>
): Chainable<LogsEndpointActionResponse>;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ import { indexEndpointRuleAlerts } from '../../tasks/index_endpoint_rule_alerts'
describe('Isolate command', () => {
describe('from Manage', () => {
let endpointData: ReturnTypeFromChainable<typeof indexEndpointHosts>;
let isolatedEndpointData: ReturnTypeFromChainable<typeof indexEndpointHosts>;

before(() => {
indexEndpointHosts({
count: 4,
disableEndpointActionsForHost: true,
endpointIsolated: false,
bothIsolatedAndNormalEndpoints: true,
count: 2,
withResponseActions: false,
isolation: false,
}).then((indexEndpoints) => {
endpointData = indexEndpoints;
});

indexEndpointHosts({
count: 2,
withResponseActions: false,
isolation: true,
}).then((indexEndpoints) => {
isolatedEndpointData = indexEndpoints;
});
});

after(() => {
Expand All @@ -45,6 +53,12 @@ describe('Isolate command', () => {
// @ts-expect-error ignore setting to undefined
endpointData = undefined;
}

if (isolatedEndpointData) {
isolatedEndpointData.cleanup();
// @ts-expect-error ignore setting to undefined
isolatedEndpointData = undefined;
}
});
beforeEach(() => {
login();
Expand All @@ -71,7 +85,7 @@ describe('Isolate command', () => {
let hostname: string;

before(() => {
indexEndpointHosts({ disableEndpointActionsForHost: true, endpointIsolated: false })
indexEndpointHosts({ withResponseActions: false, isolation: false })
.then((indexEndpoints) => {
endpointData = indexEndpoints;
hostname = endpointData.data.hosts[0].host.name;
Expand Down Expand Up @@ -196,7 +210,7 @@ describe('Isolate command', () => {
caseUrlPath = `${APP_CASES_PATH}/${indexCase.data.id}`;
});

indexEndpointHosts({ disableEndpointActionsForHost: true })
indexEndpointHosts({ withResponseActions: false, isolation: false })
.then((indexEndpoints) => {
endpointData = indexEndpoints;
hostname = endpointData.data.hosts[0].host.name;
Expand Down Expand Up @@ -270,7 +284,7 @@ describe('Isolate command', () => {

cy.getByTestSubj('euiFlyoutCloseButton').click();

cy.getByTestSubj('user-actions').within(() => {
cy.getByTestSubj('user-actions-list').within(() => {
cy.contains(isolateComment);
cy.get('[aria-label="lock"]').should('exist');
cy.get('[aria-label="lockOpen"]').should('not.exist');
Expand All @@ -293,7 +307,7 @@ describe('Isolate command', () => {
cy.contains(`Release on host ${hostname} successfully submitted`);
cy.getByTestSubj('euiFlyoutCloseButton').click();

cy.getByTestSubj('user-actions').within(() => {
cy.getByTestSubj('user-actions-list').within(() => {
cy.contains(releaseComment);
cy.contains(isolateComment);
cy.get('[aria-label="lock"]').should('exist');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
// / <reference types="cypress" />

import type { CasePostRequest } from '@kbn/cases-plugin/common/api';
import { sendEndpointActionResponse } from '../../../../scripts/endpoint/agent_emulator/services/endpoint_response_actions';
import type { IndexedEndpointPolicyResponse } from '../../../../common/endpoint/data_loaders/index_endpoint_policy_response';
import {
deleteIndexedEndpointPolicyResponse,
indexEndpointPolicyResponse,
} from '../../../../common/endpoint/data_loaders/index_endpoint_policy_response';
import type { HostPolicyResponse } from '../../../../common/endpoint/types';
import type {
ActionDetails,
HostPolicyResponse,
LogsEndpointActionResponse,
} from '../../../../common/endpoint/types';
import type { IndexEndpointHostsCyTaskOptions } from '../types';
import type {
IndexedEndpointRuleAlerts,
Expand Down Expand Up @@ -95,12 +100,14 @@ export const dataLoaders = (

indexEndpointHosts: async (options: IndexEndpointHostsCyTaskOptions = {}) => {
const { kbnClient, esClient } = await stackServicesPromise;
const { count: numHosts, version, os } = options;
const { count: numHosts, version, os, isolation, withResponseActions } = options;

return cyLoadEndpointDataHandler(esClient, kbnClient, {
numHosts,
version,
os,
isolation,
withResponseActions,
});
},

Expand Down Expand Up @@ -140,5 +147,13 @@ export const dataLoaders = (
const { esClient } = await stackServicesPromise;
return deleteIndexedEndpointPolicyResponse(esClient, indexedData).then(() => null);
},

sendHostActionResponse: async (data: {
action: ActionDetails;
state: { state?: 'success' | 'failure' };
}): Promise<LogsEndpointActionResponse> => {
const { esClient } = await stackServicesPromise;
return sendEndpointActionResponse(esClient, data.action, { state: data.state.state });
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export interface CyLoadEndpointDataOptions
enableFleetIntegration: boolean;
generatorSeed: string;
waitUntilTransformed: boolean;
disableEndpointActionsForHost?: boolean;
endpointIsolated?: boolean;
withResponseActions: boolean;
isolation: boolean;
bothIsolatedAndNormalEndpoints?: boolean;
}

Expand All @@ -61,13 +61,12 @@ export const cyLoadEndpointDataHandler = async (
waitUntilTransformed = true,
version = kibanaPackageJson.version,
os,
disableEndpointActionsForHost,
bothIsolatedAndNormalEndpoints,
endpointIsolated,
withResponseActions,
isolation,
} = options;

const DocGenerator = EndpointDocGenerator.custom({
CustomMetadataGenerator: EndpointMetadataGenerator.custom({ version, os }),
CustomMetadataGenerator: EndpointMetadataGenerator.custom({ version, os, isolation }),
});

if (waitUntilTransformed) {
Expand All @@ -92,9 +91,7 @@ export const cyLoadEndpointDataHandler = async (
enableFleetIntegration,
undefined,
DocGenerator,
disableEndpointActionsForHost,
bothIsolatedAndNormalEndpoints,
endpointIsolated
withResponseActions
);

if (waitUntilTransformed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import type { ActionDetails } from '../../../common/endpoint/types';
import type { CyLoadEndpointDataOptions } from './support/plugin_handlers/endpoint_data_loader';

type PossibleChainable =
Expand Down Expand Up @@ -41,5 +42,15 @@ export type ReturnTypeFromChainable<C extends PossibleChainable> = C extends Cyp
: never;

export type IndexEndpointHostsCyTaskOptions = Partial<
{ count: number } & Pick<CyLoadEndpointDataOptions, 'version' | 'os'>
{ count: number; withResponseActions: boolean } & Pick<
CyLoadEndpointDataOptions,
'version' | 'os' | 'isolation'
>
>;

export interface HostActionResponse {
data: {
action: ActionDetails;
state: { state?: 'success' | 'failure' };
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,15 @@ describe('endpoint list middleware', () => {
query: {
agent_ids: [
'0dc3661d-6e67-46b0-af39-6f12b025fcb0',
'34634c58-24b4-4448-80f4-107fb9918494',
'5a1298e3-e607-4bc0-8ef6-6d6a811312f2',
'78c54b13-596d-4891-95f4-80092d04454b',
'445f1fd2-5f81-4ddd-bdb6-f0d1bf2efe90',
'd77a3fc6-3096-4852-a6ee-f6b09278fbc6',
'892fcccf-1bd8-45a2-a9cc-9a7860a3cb81',
'693a3110-5ba0-4284-a264-5d78301db08c',
'554db084-64fa-4e4a-ba47-2ba713f9932b',
'c217deb6-674d-4f97-bb1d-a3a04238e6d7',
'fe16dda9-7f34-434c-9824-b4844880f410',
'f412728b-929c-48d5-bdb6-5a1298e3e607',
'd0405ddc-1e7c-48f0-93d7-d55f954bd745',
'46d78dd2-aedf-4d3f-b3a9-da445f1fd25f',
'5aafa558-26b8-4bb4-80e2-ac0644d77a3f',
'edac2c58-1748-40c3-853c-8fab48c333d7',
'06b7223a-bb2a-428a-9021-f1c0d2267ada',
'b8daa43b-7f73-4684-9221-dbc8b769405e',
'fbc06310-7d41-46b8-a5ea-ceed8a993b1a',
],
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const ES_INDEX_OPTIONS = { headers: { 'X-elastic-product-origin': 'fleet' } };

export const fleetActionGenerator = new FleetActionGenerator();

export const endpointActionGenerator = new EndpointActionGenerator();

export const sleep = (ms: number = 1000) => new Promise((r) => setTimeout(r, ms));

export const fetchEndpointActionList = async (
Expand Down Expand Up @@ -116,7 +118,6 @@ export const sendEndpointActionResponse = async (
action: ActionDetails,
{ state }: { state?: 'success' | 'failure' } = {}
): Promise<LogsEndpointActionResponse> => {
const endpointActionGenerator = new EndpointActionGenerator();
const endpointResponse = endpointActionGenerator.generateResponse({
agent: { id: action.agents[0] },
EndpointActions: {
Expand Down
Loading

0 comments on commit fcc702f

Please sign in to comment.