Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Alerting] retry internal OCC calls within alertsClient (#77838) #78688

Merged
merged 1 commit into from
Sep 28, 2020
Merged
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
35 changes: 25 additions & 10 deletions x-pack/plugins/alerts/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,14 +1696,22 @@ describe('muteAll()', () => {
muteAll: false,
},
references: [],
version: '123',
});

await alertsClient.muteAll({ id: '1' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
muteAll: true,
mutedInstanceIds: [],
updatedBy: 'elastic',
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
muteAll: true,
mutedInstanceIds: [],
updatedBy: 'elastic',
},
{
version: '123',
}
);
});

describe('authorization', () => {
Expand Down Expand Up @@ -1785,11 +1793,18 @@ describe('unmuteAll()', () => {
});

await alertsClient.unmuteAll({ id: '1' });
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
muteAll: false,
mutedInstanceIds: [],
updatedBy: 'elastic',
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith(
'alert',
'1',
{
muteAll: false,
mutedInstanceIds: [],
updatedBy: 'elastic',
},
{
version: '123',
}
);
});

describe('authorization', () => {
Expand Down
126 changes: 103 additions & 23 deletions x-pack/plugins/alerts/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { parseIsoOrRelativeDate } from './lib/iso_or_relative_date';
import { alertInstanceSummaryFromEventLog } from './lib/alert_instance_summary_from_event_log';
import { IEvent } from '../../event_log/server';
import { parseDuration } from '../common/parse_duration';
import { retryIfConflicts } from './lib/retry_if_conflicts';
import { partiallyUpdateAlert } from './saved_objects';

export interface RegistryAlertTypeWithAuth extends RegistryAlertType {
authorizedConsumers: string[];
Expand Down Expand Up @@ -421,6 +423,14 @@ export class AlertsClient {
}

public async update({ id, data }: UpdateOptions): Promise<PartialAlert> {
return await retryIfConflicts(
this.logger,
`alertsClient.update('${id}')`,
async () => await this.updateWithOCC({ id, data })
);
}

private async updateWithOCC({ id, data }: UpdateOptions): Promise<PartialAlert> {
let alertSavedObject: SavedObject<RawAlert>;

try {
Expand Down Expand Up @@ -529,7 +539,15 @@ export class AlertsClient {
};
}

public async updateApiKey({ id }: { id: string }) {
public async updateApiKey({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.updateApiKey('${id}')`,
async () => await this.updateApiKeyWithOCC({ id })
);
}

private async updateApiKeyWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -597,7 +615,15 @@ export class AlertsClient {
}
}

public async enable({ id }: { id: string }) {
public async enable({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.enable('${id}')`,
async () => await this.enableWithOCC({ id })
);
}

private async enableWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -658,7 +684,15 @@ export class AlertsClient {
}
}

public async disable({ id }: { id: string }) {
public async disable({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.disable('${id}')`,
async () => await this.disableWithOCC({ id })
);
}

private async disableWithOCC({ id }: { id: string }) {
let apiKeyToInvalidate: string | null = null;
let attributes: RawAlert;
let version: string | undefined;
Expand Down Expand Up @@ -711,8 +745,19 @@ export class AlertsClient {
}
}

public async muteAll({ id }: { id: string }) {
const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id);
public async muteAll({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.muteAll('${id}')`,
async () => await this.muteAllWithOCC({ id })
);
}

private async muteAllWithOCC({ id }: { id: string }) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>(
'alert',
id
);
await this.authorization.ensureAuthorized(
attributes.alertTypeId,
attributes.consumer,
Expand All @@ -723,19 +768,34 @@ export class AlertsClient {
await this.actionsAuthorization.ensureAuthorized('execute');
}

await this.unsecuredSavedObjectsClient.update(
'alert',
const updateAttributes = this.updateMeta({
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
});
const updateOptions = { version };

await partiallyUpdateAlert(
this.unsecuredSavedObjectsClient,
id,
this.updateMeta({
muteAll: true,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
})
updateAttributes,
updateOptions
);
}

public async unmuteAll({ id }: { id: string }): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.unmuteAll('${id}')`,
async () => await this.unmuteAllWithOCC({ id })
);
}

public async unmuteAll({ id }: { id: string }) {
const { attributes } = await this.unsecuredSavedObjectsClient.get<RawAlert>('alert', id);
private async unmuteAllWithOCC({ id }: { id: string }) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<RawAlert>(
'alert',
id
);
await this.authorization.ensureAuthorized(
attributes.alertTypeId,
attributes.consumer,
Expand All @@ -746,18 +806,30 @@ export class AlertsClient {
await this.actionsAuthorization.ensureAuthorized('execute');
}

await this.unsecuredSavedObjectsClient.update(
'alert',
const updateAttributes = this.updateMeta({
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
});
const updateOptions = { version };

await partiallyUpdateAlert(
this.unsecuredSavedObjectsClient,
id,
this.updateMeta({
muteAll: false,
mutedInstanceIds: [],
updatedBy: await this.getUserName(),
})
updateAttributes,
updateOptions
);
}

public async muteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.muteInstance('${alertId}')`,
async () => await this.muteInstanceWithOCC({ alertId, alertInstanceId })
);
}

public async muteInstance({ alertId, alertInstanceId }: MuteOptions) {
private async muteInstanceWithOCC({ alertId, alertInstanceId }: MuteOptions) {
const { attributes, version } = await this.unsecuredSavedObjectsClient.get<Alert>(
'alert',
alertId
Expand Down Expand Up @@ -788,7 +860,15 @@ export class AlertsClient {
}
}

public async unmuteInstance({
public async unmuteInstance({ alertId, alertInstanceId }: MuteOptions): Promise<void> {
return await retryIfConflicts(
this.logger,
`alertsClient.unmuteInstance('${alertId}')`,
async () => await this.unmuteInstanceWithOCC({ alertId, alertInstanceId })
);
}

private async unmuteInstanceWithOCC({
alertId,
alertInstanceId,
}: {
Expand Down
Loading