-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…66949) * Encode the index of the alert in the id response * Fixing tests * Adding missed test
- Loading branch information
1 parent
3dbe9dc
commit 48e049e
Showing
8 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
x-pack/plugins/endpoint/server/routes/alerts/lib/alert_id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { AlertIdError } from './error'; | ||
|
||
/** | ||
* Abstraction over alert IDs. | ||
*/ | ||
export class AlertId { | ||
protected readonly _index: string; | ||
protected readonly _id: string; | ||
|
||
constructor(index: string, id: string) { | ||
this._index = index; | ||
this._id = id; | ||
} | ||
|
||
public get index() { | ||
return this._index; | ||
} | ||
|
||
public get id() { | ||
return this._id; | ||
} | ||
|
||
static fromEncoded(encoded: string): AlertId { | ||
try { | ||
const value = encoded.replace(/\-/g, '+').replace(/_/g, '/'); | ||
const data = Buffer.from(value, 'base64').toString('utf8'); | ||
const { index, id } = JSON.parse(data); | ||
return new AlertId(index, id); | ||
} catch (error) { | ||
throw new AlertIdError(`Unable to decode alert id: ${encoded}`); | ||
} | ||
} | ||
|
||
toString(): string { | ||
const value = JSON.stringify({ index: this.index, id: this.id }); | ||
// replace invalid URL characters with valid ones | ||
return Buffer.from(value, 'utf8') | ||
.toString('base64') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_') | ||
.replace(/=+$/g, ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export class AlertIdError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters