Skip to content

Commit 9faef0b

Browse files
committed
CR: add missing _bulk_edit route error handlers
1 parent 0b53e91 commit 9faef0b

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

x-pack/plugins/alerting/server/routes/bulk_edit_rules.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { httpServiceMock } from '@kbn/core/server/mocks';
1010
import { bulkEditInternalRulesRoute } from './bulk_edit_rules';
1111
import { licenseStateMock } from '../lib/license_state.mock';
1212
import { verifyApiAccess } from '../lib/license_api_access';
13+
import { RuleTypeDisabledError } from '../lib/errors/rule_type_disabled';
1314
import { mockHandlerArguments } from './_mock_handler_arguments';
1415
import { rulesClientMock } from '../rules_client.mock';
1516
import { SanitizedRule } from '../types';
@@ -164,4 +165,24 @@ describe('bulkEditInternalRulesRoute', () => {
164165

165166
expect(handler(context, req, res)).rejects.toMatchInlineSnapshot(`[Error: Failure]`);
166167
});
168+
169+
it('ensures the rule type gets validated for the license', async () => {
170+
const licenseState = licenseStateMock.create();
171+
const router = httpServiceMock.createRouter();
172+
173+
bulkEditInternalRulesRoute(router, licenseState);
174+
175+
const [, handler] = router.post.mock.calls[0];
176+
177+
rulesClient.bulkEdit.mockRejectedValue(new RuleTypeDisabledError('Fail', 'license_invalid'));
178+
179+
const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [
180+
'ok',
181+
'forbidden',
182+
]);
183+
184+
await handler(context, req, res);
185+
186+
expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } });
187+
});
167188
});

x-pack/plugins/alerting/server/routes/bulk_edit_rules.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import { schema } from '@kbn/config-schema';
99
import { IRouter } from '@kbn/core/server';
1010

11-
import { ILicenseState } from '../lib';
12-
import { verifyAccessAndContext, rewriteRule } from './lib';
11+
import { ILicenseState, RuleTypeDisabledError } from '../lib';
12+
import { verifyAccessAndContext, rewriteRule, handleDisabledApiKeysError } from './lib';
1313
import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH } from '../types';
1414

1515
const ruleActionSchema = schema.object({
@@ -58,20 +58,29 @@ const buildBulkEditRulesRoute = ({ licenseState, path, router }: BuildBulkEditRu
5858
body: bodySchema,
5959
},
6060
},
61-
router.handleLegacyErrors(
62-
verifyAccessAndContext(licenseState, async function (context, req, res) {
63-
const rulesClient = (await context.alerting).getRulesClient();
64-
const { filter, operations, ids } = req.body;
61+
handleDisabledApiKeysError(
62+
router.handleLegacyErrors(
63+
verifyAccessAndContext(licenseState, async function (context, req, res) {
64+
const rulesClient = (await context.alerting).getRulesClient();
65+
const { filter, operations, ids } = req.body;
6566

66-
const bulkEditResults = await rulesClient.bulkEdit({
67-
filter,
68-
ids: ids as string[],
69-
operations,
70-
});
71-
return res.ok({
72-
body: { ...bulkEditResults, rules: bulkEditResults.rules.map(rewriteRule) },
73-
});
74-
})
67+
try {
68+
const bulkEditResults = await rulesClient.bulkEdit({
69+
filter,
70+
ids: ids as string[],
71+
operations,
72+
});
73+
return res.ok({
74+
body: { ...bulkEditResults, rules: bulkEditResults.rules.map(rewriteRule) },
75+
});
76+
} catch (e) {
77+
if (e instanceof RuleTypeDisabledError) {
78+
return e.sendResponse(res);
79+
}
80+
throw e;
81+
}
82+
})
83+
)
7584
)
7685
);
7786
};

0 commit comments

Comments
 (0)