Skip to content

Commit 77848b3

Browse files
Encode the index of the alert in the id response
1 parent 84e06af commit 77848b3

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

x-pack/plugins/endpoint/common/alert_constants.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export class AlertConstants {
1313
* The path for the Alert's Index Pattern API.
1414
*/
1515
static INDEX_PATTERN_ROUTE = `${AlertConstants.BASE_API_URL}/index_pattern`;
16-
/**
17-
* Alert's Index pattern
18-
*/
19-
static ALERT_INDEX_NAME = 'events-endpoint-1';
2016
/**
2117
* A paramter passed to Alert's Index Pattern.
2218
*/

x-pack/plugins/endpoint/server/routes/alerts/details/handlers.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
import { GetResponse } from 'elasticsearch';
77
import { KibanaRequest, RequestHandler } from 'kibana/server';
88
import { AlertEvent } from '../../../../common/types';
9-
import { AlertConstants } from '../../../../common/alert_constants';
109
import { EndpointAppContext } from '../../../types';
1110
import { AlertDetailsRequestParams } from '../types';
1211
import { AlertDetailsPagination } from './lib';
1312
import { getHostData } from '../../metadata';
13+
import { AlertId } from '../lib';
1414

1515
export const alertDetailsHandlerWrapper = function(
1616
endpointAppContext: EndpointAppContext
@@ -21,10 +21,10 @@ export const alertDetailsHandlerWrapper = function(
2121
res
2222
) => {
2323
try {
24-
const alertId = req.params.id;
24+
const alertId = AlertId.fromEncoded(req.params.id);
2525
const response = (await ctx.core.elasticsearch.dataClient.callAsCurrentUser('get', {
26-
index: AlertConstants.ALERT_INDEX_NAME,
27-
id: alertId,
26+
index: alertId.index,
27+
id: alertId.id,
2828
})) as GetResponse<AlertEvent>;
2929

3030
const indexPattern = await endpointAppContext.service
@@ -50,7 +50,7 @@ export const alertDetailsHandlerWrapper = function(
5050

5151
return res.ok({
5252
body: {
53-
id: response._id,
53+
id: alertId.toString(),
5454
...response._source,
5555
state: {
5656
host_metadata: currentHostInfo?.metadata,
@@ -60,6 +60,9 @@ export const alertDetailsHandlerWrapper = function(
6060
},
6161
});
6262
} catch (err) {
63+
const logger = endpointAppContext.logFactory.get('alerts');
64+
logger.warn(err);
65+
6366
if (err.status === 404) {
6467
return res.notFound({ body: err });
6568
}

x-pack/plugins/endpoint/server/routes/alerts/lib/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,41 @@ export const searchESForAlerts = async (
158158

159159
return response;
160160
};
161+
162+
/**
163+
* Abstraction over alert IDs.
164+
*/
165+
export class AlertId {
166+
protected readonly _index: string;
167+
protected readonly _id: string;
168+
169+
constructor(index: string, id: string) {
170+
this._index = index;
171+
this._id = id;
172+
}
173+
174+
public get index() {
175+
return this._index;
176+
}
177+
178+
public get id() {
179+
return this._id;
180+
}
181+
182+
static fromEncoded(encoded: string): AlertId {
183+
const value = encoded.replace(/\-/g, '+').replace(/_/g, '/');
184+
const data = Buffer.from(value, 'base64').toString('utf8');
185+
const { index, id } = JSON.parse(data);
186+
return new AlertId(index, id);
187+
}
188+
189+
toString(): string {
190+
const value = JSON.stringify({ index: this.index, id: this.id });
191+
// replace invalid URL characters with valid ones
192+
return Buffer.from(value, 'utf8')
193+
.toString('base64')
194+
.replace(/\+/g, '-')
195+
.replace(/\//g, '_')
196+
.replace(/=+$/g, '');
197+
}
198+
}

x-pack/plugins/endpoint/server/routes/alerts/list/lib/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { AlertConstants } from '../../../../../common/alert_constants';
2020
import { EndpointAppContext } from '../../../../types';
2121
import { AlertSearchQuery } from '../../types';
2222
import { AlertListPagination } from './pagination';
23+
import { AlertId } from '../../lib';
2324

2425
export const getRequestData = async (
2526
request: KibanaRequest<unknown, AlertingIndexGetQueryResult, unknown>,
@@ -105,8 +106,9 @@ export async function mapToAlertResultList(
105106
const pagination: AlertListPagination = new AlertListPagination(config, reqCtx, reqData, hits);
106107

107108
function mapHit(entry: AlertHits[0]): AlertData {
109+
const alertId = new AlertId(entry._index, entry._id);
108110
return {
109-
id: entry._id,
111+
id: alertId.toString(),
110112
...entry._source,
111113
prev: null,
112114
next: null,

0 commit comments

Comments
 (0)