-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathroutes.tsx
More file actions
269 lines (253 loc) · 9.83 KB
/
Copy pathroutes.tsx
File metadata and controls
269 lines (253 loc) · 9.83 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* 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 React, { useMemo } from 'react';
import { Redirect, useLocation, useParams } from 'react-router-dom';
import { Routes, Route } from '@kbn/shared-ux-router';
import type { Capabilities } from '@kbn/core-capabilities-common';
import {
CUSTOM_HIGHLIGHTED_FIELDS_UI_EDIT_PRIVILEGES,
INVESTIGATION_GUIDE_UI_EDIT_PRIVILEGES,
RULES_UI_EDIT_PRIVILEGE,
RULES_UI_READ_PRIVILEGE,
} from '@kbn/security-solution-features/constants';
import * as i18n from './translations';
import {
ALERT_ANALYSIS_WORKFLOW_PATH,
COVERAGE_OVERVIEW_PATH,
DE_RULE_HEALTH_PATH,
DE_SPACE_RULES_HEALTH_PATH,
ENABLE_DE_HEALTH_UI_SETTING,
ENABLE_RULE_CHANGES_HISTORY_SETTING,
RULES_LANDING_PATH,
RULES_PATH,
SecurityPageName,
} from '../../common/constants';
import { useIsExperimentalFeatureEnabled } from '../common/hooks/use_experimental_features';
import { NotFoundPage } from '../app/404';
import { RulesPage } from '../detection_engine/rule_management_ui/pages/rule_management';
import { CreateRulePage } from '../detection_engine/rule_creation_ui/pages/rule_creation';
import { RuleDetailsPage } from '../detection_engine/rule_details_ui/pages/rule_details';
import { RuleChangesHistoryPage } from '../detection_engine/rule_details_ui/pages/rule_changes_history';
import { EditRulePage } from '../detection_engine/rule_creation_ui/pages/rule_editing';
import { useReadonlyHeader } from '../use_readonly_header';
import { PluginTemplateWrapper } from '../common/components/plugin_template_wrapper';
import { SpyRoute } from '../common/utils/route/spy_routes';
import { AllRulesTabs } from '../detection_engine/rule_management_ui/components/rules_table/rules_table_toolbar';
import { AddRulesPage } from '../detection_engine/rule_management_ui/pages/add_rules';
import {
DetectionEngineRuleHealthPage,
DetectionEngineSpaceRulesHealthPage,
} from '../detection_engine/rule_monitoring_ui/pages';
import type { SecuritySubPluginRoutes } from '../app/types';
import { RulesLandingPage } from './landing';
import { CoverageOverviewPage } from '../detection_engine/rule_management_ui/pages/coverage_overview';
import { AlertAnalysisWorkflowPage } from '../detection_engine/rule_management_ui/pages/alert_analysis_workflow';
import { RuleDetailTabs } from '../detection_engine/rule_details_ui/pages/rule_details/use_rule_details_tabs';
import { withSecurityRoutePageWrapper } from '../common/components/security_route_page_wrapper';
import { hasCapabilities } from '../common/lib/capabilities';
import { useKibana, useUiSetting$ } from '../common/lib/kibana/kibana_react';
import { useUserPrivileges } from '../common/components/user_privileges';
import { useEndpointExceptionsCapability } from '../exceptions/hooks/use_endpoint_exceptions_capability';
import { getRuleDetailsTabUrl } from '../common/components/link_to/redirect_to_detection_engine';
/**
* Component to redirect to rule details with the appropriate landing tab.
* This is a separate component because hooks can only be called at the top level of a React component.
*/
export const RuleDetailsRedirect: React.FC = () => {
const { detailName } = useParams<{ detailName: string }>();
const location = useLocation();
const defaultLandingPageWithTab = getRuleDetailsTabUrl(detailName, RuleDetailTabs.overview);
return (
<Redirect
to={{
...location,
pathname: `/rules${defaultLandingPageWithTab}`,
search: location.search,
}}
/>
);
};
export const RuleDetailsTabGuard: React.FC = () => {
const { tabName } = useParams<{ detailName: string; tabName: string }>();
const { alertsPrivileges, rulesPrivileges } = useUserPrivileges();
const canReadEndpointExceptions = useEndpointExceptionsCapability('showEndpointExceptions');
const canReadAlerts = alertsPrivileges.alerts.read;
const canReadExceptions = rulesPrivileges.exceptions.read;
const canAccessTab = (() => {
switch (tabName) {
case RuleDetailTabs.alerts:
return canReadAlerts;
case RuleDetailTabs.exceptions:
return canReadExceptions;
case RuleDetailTabs.endpointExceptions:
return canReadEndpointExceptions;
default:
return true;
}
})();
// Redirect if no access to the requested tab
if (!canAccessTab) {
return <RuleDetailsRedirect />;
}
return <RuleDetailsPage />;
};
interface Features {
deHealthUIEnabled: boolean;
ruleHealthUIEnabled: boolean;
endpointExceptionsTabEnabled: boolean;
}
const getRulesSubRoutes = (
capabilities: Capabilities,
{ deHealthUIEnabled, ruleHealthUIEnabled, endpointExceptionsTabEnabled }: Features
) => [
...(hasCapabilities(capabilities, RULES_UI_READ_PRIVILEGE) // regular detection rules are enabled
? [
{
path: endpointExceptionsTabEnabled
? `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.endpointExceptions}|${RuleDetailTabs.executionResults})`
: `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.executionResults})`,
main: RuleDetailsTabGuard,
exact: true,
},
{
path: `/rules/:tabName(${AllRulesTabs.management}|${AllRulesTabs.monitoring}|${AllRulesTabs.updates})`,
main: RulesPage,
exact: true,
},
{
path: '/rules/add_rules',
main: withSecurityRoutePageWrapper(AddRulesPage, SecurityPageName.rulesAdd, {
omitSpyRoute: true,
}),
exact: true,
},
{
path: ALERT_ANALYSIS_WORKFLOW_PATH,
main: withSecurityRoutePageWrapper(
AlertAnalysisWorkflowPage,
SecurityPageName.alertAnalysisWorkflow,
{ omitSpyRoute: true, skipLinkAuthorization: true }
),
exact: true,
},
// Detection Engine Health UI Routes
...(deHealthUIEnabled
? [
{
path: DE_SPACE_RULES_HEALTH_PATH,
main: DetectionEngineSpaceRulesHealthPage,
exact: true,
},
]
: []),
...(ruleHealthUIEnabled
? [
{
path: DE_RULE_HEALTH_PATH,
main: DetectionEngineRuleHealthPage,
exact: true,
},
]
: []),
]
: []),
...(hasCapabilities(capabilities, RULES_UI_EDIT_PRIVILEGE)
? [
{
path: '/rules/create',
main: withSecurityRoutePageWrapper(CreateRulePage, SecurityPageName.rulesCreate, {
omitSpyRoute: true,
}),
exact: true,
},
]
: []),
...(hasCapabilities(capabilities, [
RULES_UI_EDIT_PRIVILEGE,
[RULES_UI_READ_PRIVILEGE, INVESTIGATION_GUIDE_UI_EDIT_PRIVILEGES],
[RULES_UI_READ_PRIVILEGE, CUSTOM_HIGHLIGHTED_FIELDS_UI_EDIT_PRIVILEGES],
])
? [
{
path: '/rules/id/:detailName/edit',
main: EditRulePage,
exact: true,
},
]
: []),
];
const RulesContainerComponent: React.FC = () => {
useReadonlyHeader(i18n.READ_ONLY_BADGE_TOOLTIP);
const { capabilities } = useKibana().services.application;
const deHealthUiFFEnabled = useIsExperimentalFeatureEnabled('deHealthUIEnabled');
const ruleHealthUiFFEnabled = useIsExperimentalFeatureEnabled('ruleHealthUIEnabled');
const [deHealthUIAdvancedSetting] = useUiSetting$<boolean>(ENABLE_DE_HEALTH_UI_SETTING, false);
const deHealthUIEnabled = deHealthUiFFEnabled && deHealthUIAdvancedSetting;
const ruleHealthUIEnabled = ruleHealthUiFFEnabled && deHealthUIAdvancedSetting;
const isEndpointExceptionsMovedFFEnabled = useIsExperimentalFeatureEnabled(
'endpointExceptionsMovedUnderManagement'
);
const [isRuleChangesHistoryEnabled] = useUiSetting$<boolean>(ENABLE_RULE_CHANGES_HISTORY_SETTING);
const subRoutes = useMemo(() => {
return getRulesSubRoutes(capabilities, {
deHealthUIEnabled,
ruleHealthUIEnabled,
endpointExceptionsTabEnabled: !isEndpointExceptionsMovedFFEnabled,
}).map((route) => (
<Route key={`rules-route-${route.path}`} path={route.path} exact={route?.exact ?? false}>
<route.main />
</Route>
));
}, [capabilities, deHealthUIEnabled, ruleHealthUIEnabled, isEndpointExceptionsMovedFFEnabled]);
return (
<PluginTemplateWrapper>
<Routes>
<Route path="/rules/id/:detailName" exact>
<RuleDetailsRedirect />
</Route>
<Route path="/rules" exact>
<Redirect to={`/rules/${AllRulesTabs.management}`} />
</Route>
{isRuleChangesHistoryEnabled && hasCapabilities(capabilities, RULES_UI_READ_PRIVILEGE) && (
<Route path="/rules/id/:ruleId/changes-history" exact>
<RuleChangesHistoryPage />
</Route>
)}
{subRoutes}
<Route component={NotFoundPage} />
<SpyRoute pageName={SecurityPageName.rules} />
</Routes>
</PluginTemplateWrapper>
);
};
const Rules = React.memo(RulesContainerComponent);
const CoverageOverviewRoutes = () => (
<PluginTemplateWrapper>
<CoverageOverviewPage />
</PluginTemplateWrapper>
);
export const routes: SecuritySubPluginRoutes = [
{
path: RULES_LANDING_PATH,
component: withSecurityRoutePageWrapper(RulesLandingPage, SecurityPageName.rulesLanding, {
omitSpyRoute: true,
}),
},
{
path: RULES_PATH,
component: withSecurityRoutePageWrapper(Rules, SecurityPageName.rules, {
omitSpyRoute: true,
}),
},
{
path: COVERAGE_OVERVIEW_PATH,
component: withSecurityRoutePageWrapper(
CoverageOverviewRoutes,
SecurityPageName.coverageOverview
),
},
];