Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/alerting/mappings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"alert": {
"properties": {
"apiKeyName": {
"type": "keyword"
},
"enabled": {
"type": "boolean"
},
Expand Down
22 changes: 15 additions & 7 deletions x-pack/legacy/plugins/alerting/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface ConstructorOptions {
alertTypeRegistry: AlertTypeRegistry;
spaceId?: string;
getUserName: () => Promise<string | null>;
createAPIKey: () => Promise<CreateAPIKeyResult>;
createAPIKey: (id: string) => Promise<CreateAPIKeyResult>;
}

export interface FindOptions {
Expand Down Expand Up @@ -107,7 +107,7 @@ export class AlertsClient {
private readonly taskManager: TaskManagerStartContract;
private readonly savedObjectsClient: SavedObjectsClientContract;
private readonly alertTypeRegistry: AlertTypeRegistry;
private readonly createAPIKey: () => Promise<CreateAPIKeyResult>;
private readonly createAPIKey: (id: string) => Promise<CreateAPIKeyResult>;

constructor({
alertTypeRegistry,
Expand Down Expand Up @@ -138,7 +138,7 @@ export class AlertsClient {
const { references, actions } = await this.denormalizeActions(data.actions);
const rawAlert: RawAlert = {
...data,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
...this.apiKeyAsAlertAttributes(await this.createAPIKey(alertType.id), username),
actions,
createdBy: username,
updatedBy: username,
Expand Down Expand Up @@ -236,7 +236,10 @@ export class AlertsClient {

const { actions, references } = await this.denormalizeActions(data.actions);
const username = await this.getUserName();
const apiKeyAttributes = this.apiKeyAsAlertAttributes(await this.createAPIKey(), username);
const apiKeyAttributes = this.apiKeyAsAlertAttributes(
await this.createAPIKey(alertType.id),
username
);

const updatedObject = await this.savedObjectsClient.update<RawAlert>(
'alert',
Expand All @@ -260,28 +263,31 @@ export class AlertsClient {
private apiKeyAsAlertAttributes(
apiKey: CreateAPIKeyResult,
username: string | null
): Pick<RawAlert, 'apiKey' | 'apiKeyOwner'> {
): Pick<RawAlert, 'apiKey' | 'apiKeyOwner' | 'apiKeyName'> {
return apiKey.created
? {
apiKeyOwner: username,
apiKey: Buffer.from(`${apiKey.result.id}:${apiKey.result.api_key}`).toString('base64'),
apiKeyName: apiKey.result.name,
}
: {
apiKeyOwner: null,
apiKey: null,
apiKeyName: null,
};
}

public async updateApiKey({ id }: { id: string }) {
const { version, attributes } = await this.savedObjectsClient.get('alert', id);
const alertType = this.alertTypeRegistry.get(attributes.alertTypeId);

const username = await this.getUserName();
await this.savedObjectsClient.update(
'alert',
id,
{
...attributes,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
...this.apiKeyAsAlertAttributes(await this.createAPIKey(alertType.id), username),
updatedBy: username,
},
{ version }
Expand All @@ -290,6 +296,8 @@ export class AlertsClient {

public async enable({ id }: { id: string }) {
const { attributes, version } = await this.savedObjectsClient.get('alert', id);
const alertType = this.alertTypeRegistry.get(attributes.alertTypeId);

if (attributes.enabled === false) {
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
const username = await this.getUserName();
Expand All @@ -299,7 +307,7 @@ export class AlertsClient {
{
...attributes,
enabled: true,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
...this.apiKeyAsAlertAttributes(await this.createAPIKey(alertType.id), username),
updatedBy: username,
scheduledTaskId: scheduledTask.id,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export class AlertsClientFactory {
const user = await securityPluginSetup.authc.getCurrentUser(request);
return user ? user.username : null;
},
async createAPIKey() {
async createAPIKey(id: string) {
if (!securityPluginSetup) {
return { created: false };
}
const createAPIKeyResult = await securityPluginSetup.authc.createAPIKey(request, {
name: `source: alerting, generated uuid: "${uuid.v4()}"`,
name: `{"alertTypeId": "${id}", "source": "alerting", "guid": "${uuid.v4()}"}`,
role_descriptors: {},
});
if (!createAPIKeyResult) {
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface Alert {
}

export interface RawAlert extends SavedObjectAttributes {
apiKeyName: string | null;
enabled: boolean;
name: string;
tags: string[];
Expand Down