Skip to content

Commit

Permalink
Pass IEventLogClient as a dependency to RuleExecutionLogClient
Browse files Browse the repository at this point in the history
  • Loading branch information
banderror committed Nov 1, 2021
1 parent e6e84bf commit f4b16f4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class EventLogAdapter implements IRuleExecutionLogClient {

constructor(
eventLogService: IEventLogService,
eventLogClient: IEventLogClient,
eventLogClient: IEventLogClient | undefined,
savedObjectsClient: SavedObjectsClientContract
) {
this.eventLogClient = new EventLogClient(eventLogService, eventLogClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ interface IExecLogEventLogClient {
}

export class EventLogClient implements IExecLogEventLogClient {
private readonly eventLogClient: IEventLogClient;
private readonly eventLogClient: IEventLogClient | undefined;
private readonly eventLogger: IEventLogger;
private sequence = 0;

constructor(eventLogService: IEventLogService, eventLogClient: IEventLogClient) {
constructor(eventLogService: IEventLogService, eventLogClient: IEventLogClient | undefined) {
this.eventLogClient = eventLogClient;
this.eventLogger = eventLogService.getLogger({
event: { provider: RULE_EXECUTION_LOG_PROVIDER },
Expand All @@ -72,6 +72,10 @@ export class EventLogClient implements IExecLogEventLogClient {
public async getLastStatusChanges(
args: GetLastStatusChangesArgs
): Promise<IRuleStatusSOAttributes[]> {
if (!this.eventLogClient) {
throw new Error('Querying Event Log from a rule executor is not supported at this moment');
}

const soType = ALERT_SAVED_OBJECT_TYPE;
const soIds = [args.ruleId];
const count = args.count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import { truncateMessage } from './utils/normalization';

interface ConstructorParams {
underlyingClient: UnderlyingLogClient;
eventLogService: IEventLogService;
eventLogClient: IEventLogClient;
savedObjectsClient: SavedObjectsClientContract;
eventLogService: IEventLogService;
eventLogClient?: IEventLogClient;
}

export class RuleExecutionLogClient implements IRuleExecutionLogClient {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
const esClient = scopedClusterClient.asCurrentUser;

const ruleStatusClient = new RuleExecutionLogClient({
underlyingClient: config.ruleExecutionLog.underlyingClient,
savedObjectsClient,
eventLogService,
underlyingClient: config.ruleExecutionLog.underlyingClient,
});

const completeRule = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,67 @@

import { SavedObjectsFindResult } from 'kibana/server';
import {
LogExecutionMetricsArgs,
IRuleExecutionLogClient,
LogStatusChangeArgs,
LogExecutionMetricsArgs,
FindBulkExecutionLogArgs,
FindBulkExecutionLogResponse,
FindExecutionLogArgs,
LogStatusChangeArgs,
UpdateExecutionLogArgs,
GetLastFailuresArgs,
GetCurrentStatusArgs,
GetCurrentStatusBulkArgs,
GetCurrentStatusBulkResult,
} from '../../rule_execution_log';
import { IRuleStatusSOAttributes } from '../../rules/types';

export const createWarningsAndErrors = () => {
const warningsAndErrorsStore: LogStatusChangeArgs[] = [];

const previewRuleExecutionLogClient: IRuleExecutionLogClient = {
async delete(id: string): Promise<void> {
return Promise.resolve(undefined);
},
async find(
find(
args: FindExecutionLogArgs
): Promise<Array<SavedObjectsFindResult<IRuleStatusSOAttributes>>> {
return Promise.resolve([]);
},
async findBulk(args: FindBulkExecutionLogArgs): Promise<FindBulkExecutionLogResponse> {

findBulk(args: FindBulkExecutionLogArgs): Promise<FindBulkExecutionLogResponse> {
return Promise.resolve({});
},
async logStatusChange(args: LogStatusChangeArgs): Promise<void> {
warningsAndErrorsStore.push(args);
return Promise.resolve(undefined);

getLastFailures(args: GetLastFailuresArgs): Promise<IRuleStatusSOAttributes[]> {
return Promise.resolve([]);
},
async update(args: UpdateExecutionLogArgs): Promise<void> {
return Promise.resolve(undefined);

getCurrentStatus(args: GetCurrentStatusArgs): Promise<IRuleStatusSOAttributes> {
return Promise.resolve({
statusDate: new Date().toISOString(),
status: null,
lastFailureAt: null,
lastFailureMessage: null,
lastSuccessAt: null,
lastSuccessMessage: null,
lastLookBackDate: null,
gap: null,
bulkCreateTimeDurations: null,
searchAfterTimeDurations: null,
});
},
async logExecutionMetrics(args: LogExecutionMetricsArgs): Promise<void> {
return Promise.resolve(undefined);

getCurrentStatusBulk(args: GetCurrentStatusBulkArgs): Promise<GetCurrentStatusBulkResult> {
return Promise.resolve({});
},

deleteCurrentStatus(ruleId: string): Promise<void> {
return Promise.resolve();
},

logStatusChange(args: LogStatusChangeArgs): Promise<void> {
warningsAndErrorsStore.push(args);
return Promise.resolve();
},

logExecutionMetrics(args: LogExecutionMetricsArgs): Promise<void> {
return Promise.resolve();
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ export const signalRulesAlertType = ({
const searchAfterSize = Math.min(maxSignals, DEFAULT_SEARCH_AFTER_PAGE_SIZE);
let hasError: boolean = false;
let result = createSearchAfterReturnType();

const ruleStatusClient = ruleExecutionLogClientOverride
? ruleExecutionLogClientOverride
: new RuleExecutionLogClient({
eventLogService,
savedObjectsClient: services.savedObjectsClient,
underlyingClient: config.ruleExecutionLog.underlyingClient,
savedObjectsClient: services.savedObjectsClient,
eventLogService,
});

const completeRule: CompleteRule<RuleParams> = {
Expand Down
17 changes: 10 additions & 7 deletions x-pack/plugins/security_solution/server/request_context_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ export class RequestContextFactory implements IRequestContextFactory {
private readonly appClientFactory: AppClientFactory;

constructor(private readonly options: ConstructorOptions) {
const { config, plugins } = options;

this.appClientFactory = new AppClientFactory();
this.appClientFactory.setup({
getSpaceId: plugins.spaces?.spacesService?.getSpaceId,
config,
});
}

public async create(
context: RequestHandlerContext,
request: KibanaRequest
): Promise<SecuritySolutionApiRequestHandlerContext> {
const { options, appClientFactory } = this;
const { config, plugins } = options;
const { config, core, plugins } = options;
const { lists, ruleRegistry, security, spaces } = plugins;

appClientFactory.setup({
getSpaceId: plugins.spaces?.spacesService?.getSpaceId,
config,
});

const [, startPlugins] = await core.getStartServices();
const frameworkRequest = await buildFrameworkRequest(context, security, request);

return {
Expand All @@ -69,9 +71,10 @@ export class RequestContextFactory implements IRequestContextFactory {

getExecutionLogClient: () =>
new RuleExecutionLogClient({
underlyingClient: config.ruleExecutionLog.underlyingClient,
savedObjectsClient: context.core.savedObjects.client,
eventLogService: plugins.eventLog,
underlyingClient: config.ruleExecutionLog.underlyingClient,
eventLogClient: startPlugins.eventLog.getClient(request),
}),

getExceptionListClient: () => {
Expand Down

0 comments on commit f4b16f4

Please sign in to comment.