-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathplugin.ts
More file actions
225 lines (201 loc) · 7.55 KB
/
Copy pathplugin.ts
File metadata and controls
225 lines (201 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* 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 {
DEFAULT_APP_CATEGORIES,
type CoreSetup,
type CoreStart,
type KibanaRequest,
type Logger,
type Plugin,
type PluginInitializerContext,
} from '@kbn/core/server';
import { DEFAULT_SPACE_ID } from '@kbn/core-spaces-common';
import type { WorkflowsServerPluginSetup } from '@kbn/workflows-management-plugin/server';
import {
ALERTZERO_API_PRIVILEGE_READ,
ALERTZERO_API_PRIVILEGE_WRITE,
ALERTZERO_FEATURE_ID,
ALERTZERO_PLUGIN_NAME,
} from '../common/constants';
import type { AlertZeroConfig } from './config';
import type {
AlertZeroPluginSetup,
AlertZeroPluginStart,
AlertZeroSetupDependencies,
AlertZeroStartDependencies,
} from './types';
import { registerRoutes } from './routes/register_routes';
import { registerOwner } from './managed_workflows/register_owner';
import { initializeManagedWorkflows } from './managed_workflows/initialize_managed_workflows';
import { WatchesService } from './services/watches/watches_service';
import { WorkersService } from './services/workers/workers_service';
import { ConversationProposalsService } from './services/conversation_proposals/conversation_proposals_service';
import { WatchWorkflowsManagementClientImpl } from './services/watches/watch_workflows_management_client';
import { ActionsService } from './services/actions/actions_service';
import { listActionsTool } from './agent_builder_tools/list_actions_tool';
import { agentType, ensureAgent, ensureAgentSafe, registerAgentType } from './agent';
export class AlertZeroPlugin
implements
Plugin<
AlertZeroPluginSetup,
AlertZeroPluginStart,
AlertZeroSetupDependencies,
AlertZeroStartDependencies
>
{
private readonly logger: Logger;
private readonly config: AlertZeroConfig;
private spaces?: AlertZeroStartDependencies['spaces'];
private workflowsManagementApi?: WorkflowsServerPluginSetup['management'];
/** Created during `start`; routes resolve them lazily after managed-workflow initialization. */
private watchesService?: WatchesService;
private actionsService?: ActionsService;
private workersService?: WorkersService;
private conversationProposalsService?: ConversationProposalsService;
constructor(context: PluginInitializerContext<AlertZeroConfig>) {
this.logger = context.logger.get();
this.config = context.config.get();
}
setup(
coreSetup: CoreSetup<AlertZeroStartDependencies, AlertZeroPluginStart>,
{
agentBuilder,
agenticInvestigations: _agenticInvestigationsSetup,
features,
workflowsExtensions,
workflowsManagement,
}: AlertZeroSetupDependencies
): AlertZeroPluginSetup {
if (!this.config.enabled) {
this.logger.info('AlertZero plugin is disabled');
return {};
}
this.logger.info('Setting up AlertZero plugin');
this.workflowsManagementApi = workflowsManagement.management;
registerOwner({ workflowsExtensions });
registerAgentType(agentBuilder);
// Registered in setup so the builtin tool is available to Agent Builder before
// the first agent run; the handler resolves the service lazily like the routes do.
agentBuilder.tools.register({
...listActionsTool(() => this.requireActionsService()),
});
features.registerKibanaFeature({
id: ALERTZERO_FEATURE_ID,
name: ALERTZERO_PLUGIN_NAME,
order: 1101,
category: DEFAULT_APP_CATEGORIES.security,
app: ['kibana', ALERTZERO_FEATURE_ID],
privileges: {
all: {
app: ['kibana', ALERTZERO_FEATURE_ID],
api: [ALERTZERO_API_PRIVILEGE_READ, ALERTZERO_API_PRIVILEGE_WRITE],
savedObject: { all: [], read: [] },
ui: ['show', 'write'],
},
read: {
app: ['kibana', ALERTZERO_FEATURE_ID],
api: [ALERTZERO_API_PRIVILEGE_READ],
savedObject: { all: [], read: [] },
ui: ['show'],
},
},
});
const router = coreSetup.http.createRouter();
registerRoutes({
router,
logger: this.logger,
config: this.config,
getSpaceId: (request) => this.getSpaceId(request),
getWatchesService: () => this.requireWatchesService(),
getWorkersService: () => this.requireWorkersService(),
getConversationProposalsService: () => this.requireConversationProposalsService(),
getActionsService: () => this.requireActionsService(),
});
return {};
}
start(_core: CoreStart, plugins: AlertZeroStartDependencies): AlertZeroPluginStart {
this.spaces = plugins.spaces;
if (!this.config.enabled) {
return {};
}
void ensureAgentSafe({
agentBuilder: plugins.agentBuilder,
spaceId: DEFAULT_SPACE_ID,
logger: this.logger,
});
const management = this.workflowsManagementApi
? new WatchWorkflowsManagementClientImpl(this.workflowsManagementApi)
: undefined;
const managedWorkflows = initializeManagedWorkflows({
workflowsExtensions: plugins.workflowsExtensions,
logger: this.logger,
ensureAgentForSpace: plugins.agentBuilder
? (spaceId) => ensureAgent({ agentBuilder: plugins.agentBuilder!, spaceId })
: undefined,
}).catch((error) => {
this.logger.error(
`AlertZero managed workflow initialization failed: ${
error instanceof Error ? error.message : String(error)
}`
);
return undefined;
});
this.conversationProposalsService = new ConversationProposalsService(
plugins.agenticInvestigations.getProposalsService(),
plugins.agentBuilder,
this.logger
);
// Mock mode changes presentation data only; durable Worker settings and enablement still use Workflows.
this.watchesService = new WatchesService();
this.actionsService = new ActionsService(
() =>
this.workflowsManagementApi
? new WatchWorkflowsManagementClientImpl(this.workflowsManagementApi)
: undefined,
this.logger
);
this.workersService = new WorkersService(management, managedWorkflows, this.logger, {
ensureAgentForSpace: plugins.agentBuilder
? (spaceId) =>
ensureAgentSafe({ agentBuilder: plugins.agentBuilder!, spaceId, logger: this.logger })
: undefined,
agentBuilder: plugins.agentBuilder,
agentTypes: [agentType],
});
return {};
}
private requireWatchesService(): WatchesService {
if (!this.watchesService) {
throw new Error('Watches service is not available until the AlertZero plugin has started');
}
return this.watchesService;
}
private requireActionsService(): ActionsService {
if (!this.actionsService) {
throw new Error('Actions service is not available until the AlertZero plugin has started');
}
return this.actionsService;
}
private requireWorkersService(): WorkersService {
if (!this.workersService) {
throw new Error('Workers service is not available until the AlertZero plugin has started');
}
return this.workersService;
}
private requireConversationProposalsService(): ConversationProposalsService {
if (!this.conversationProposalsService) {
throw new Error(
'ConversationProposalsService is not available until the AlertZero plugin has started'
);
}
return this.conversationProposalsService;
}
private getSpaceId(request: KibanaRequest): string {
return this.spaces?.spacesService.getSpaceId(request) ?? 'default';
}
stop() {}
}