Skip to content

Commit

Permalink
Revert changes in PR#175981 (elastic#177054)
Browse files Browse the repository at this point in the history
Reverts changes in elastic#175981
  • Loading branch information
ersin-erdal authored Feb 15, 2024
1 parent e136a93 commit 8e7e5b2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';
import { RuleTaskState } from '../../types';
import { taskInstanceToAlertTaskInstance } from '../../task_runner/alert_task_instance';
import { ReadOperations, AlertingAuthorizationEntity } from '../../authorization';
Expand All @@ -19,28 +18,18 @@ export async function getAlertState(
context: RulesClientContext,
{ id }: GetAlertStateParams
): Promise<RuleTaskState | void> {
const rule = await get(context, { id });
const alert = await get(context, { id });
await context.authorization.ensureAuthorized({
ruleTypeId: rule.alertTypeId,
consumer: rule.consumer,
ruleTypeId: alert.alertTypeId,
consumer: alert.consumer,
operation: ReadOperations.GetRuleState,
entity: AlertingAuthorizationEntity.Rule,
});
if (rule.scheduledTaskId) {
try {
const { state } = taskInstanceToAlertTaskInstance(
await context.taskManager.get(rule.scheduledTaskId),
rule
);
return state;
} catch (e) {
if (SavedObjectsErrorHelpers.isNotFoundError(e)) {
context.logger.warn(`Task (${rule.scheduledTaskId}) not found`);
} else {
context.logger.warn(
`An error occurred when getting the task state for (${rule.scheduledTaskId})`
);
}
}
if (alert.scheduledTaskId) {
const { state } = taskInstanceToAlertTaskInstance(
await context.taskManager.get(alert.scheduledTaskId),
alert
);
return state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { AlertingAuthorization } from '../../authorization/alerting_authorizatio
import { ActionsAuthorization } from '@kbn/actions-plugin/server';
import { getBeforeSetup } from './lib';
import { RULE_SAVED_OBJECT_TYPE } from '../../saved_objects';
import { SavedObjectsErrorHelpers } from '@kbn/core-saved-objects-server';

const taskManager = taskManagerMock.createStart();
const ruleTypeRegistry = ruleTypeRegistryMock.create();
Expand Down Expand Up @@ -176,70 +175,6 @@ describe('getAlertState()', () => {
expect(taskManager.get).toHaveBeenCalledWith(scheduledTaskId);
});

test('logs a warning if the task not found', async () => {
const rulesClient = new RulesClient(rulesClientParams);

const scheduledTaskId = 'task-123';

unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
id: '1',
type: RULE_SAVED_OBJECT_TYPE,
attributes: {
alertTypeId: '123',
schedule: { interval: '10s' },
params: {
bar: true,
},
actions: [],
enabled: true,
scheduledTaskId,
mutedInstanceIds: [],
muteAll: true,
},
references: [],
});

taskManager.get.mockRejectedValueOnce(SavedObjectsErrorHelpers.createGenericNotFoundError());

await rulesClient.getAlertState({ id: '1' });

expect(rulesClientParams.logger.warn).toHaveBeenCalledTimes(1);
expect(rulesClientParams.logger.warn).toHaveBeenCalledWith('Task (task-123) not found');
});

test('logs a warning if the taskManager throws an error', async () => {
const rulesClient = new RulesClient(rulesClientParams);

const scheduledTaskId = 'task-123';

unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
id: '1',
type: RULE_SAVED_OBJECT_TYPE,
attributes: {
alertTypeId: '123',
schedule: { interval: '10s' },
params: {
bar: true,
},
actions: [],
enabled: true,
scheduledTaskId,
mutedInstanceIds: [],
muteAll: true,
},
references: [],
});

taskManager.get.mockRejectedValueOnce(SavedObjectsErrorHelpers.createBadRequestError());

await rulesClient.getAlertState({ id: '1' });

expect(rulesClientParams.logger.warn).toHaveBeenCalledTimes(1);
expect(rulesClientParams.logger.warn).toHaveBeenCalledWith(
'An error occurred when getting the task state for (task-123)'
);
});

describe('authorization', () => {
beforeEach(() => {
unsecuredSavedObjectsClient.get.mockResolvedValueOnce({
Expand Down

0 comments on commit 8e7e5b2

Please sign in to comment.