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

[ResponseOps][Rules] Remove unintended internal Find routes API with public access #193757

Merged
merged 24 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8e116ea
[ResponseOps][Rules] Remove unintended internal Find routes API with …
Zacqary Sep 18, 2024
5bcccb3
Merge branch 'main' into 192957-remove-internal-public-access
elasticmachine Sep 23, 2024
b1dd035
Wrap get/post registrations in conditional
Zacqary Sep 24, 2024
ac11af5
Add unit tests for proper access calls
Zacqary Sep 24, 2024
1057825
Change internal/find get to post in tests
Zacqary Sep 25, 2024
49ba566
Merge remote-tracking branch 'upstream/main' into 192957-remove-inter…
Zacqary Oct 1, 2024
c45f20c
Fix test post queries
Zacqary Oct 1, 2024
ece2d5c
Merge remote-tracking branch 'upstream/main' into 192957-remove-inter…
Zacqary Oct 3, 2024
35f830e
Add kbn-xssrf to post tests
Zacqary Oct 3, 2024
ef4bcdb
Fix obs functional tests
Zacqary Oct 4, 2024
7add9ec
Merge remote-tracking branch 'upstream/main' into 192957-remove-inter…
Zacqary Oct 4, 2024
233f439
Change security solution internal/_find find call to POST
Zacqary Oct 4, 2024
5e32657
Fix security api POST body
Zacqary Oct 14, 2024
a5cb489
Merge remote-tracking branch 'upstream/main' into 192957-remove-inter…
Zacqary Oct 14, 2024
022b75d
Fix security api jest
Zacqary Oct 14, 2024
ddae6e8
Change internal find intercept in cypress test
Zacqary Oct 14, 2024
de002fa
Move int internal find route to own file
Zacqary Oct 15, 2024
2934c11
Merge branch 'main' into 192957-remove-internal-public-access
elasticmachine Oct 16, 2024
ef2f711
Split puSplit public and internal test suites
Zacqary Oct 21, 2024
1d9f67b
Merge branch '192957-remove-internal-public-access' of https://github…
Zacqary Oct 21, 2024
c350629
Merge remote-tracking branch 'upstream/main' into 192957-remove-inter…
Zacqary Oct 21, 2024
1593634
Fix internal test suite split
Zacqary Oct 21, 2024
b64b9a9
Fix incorrect public test urls
Zacqary Oct 21, 2024
92d2a02
Fix expected 200, change to 400
Zacqary Oct 21, 2024
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: 2 additions & 1 deletion x-pack/plugins/alerting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { deleteRuleRoute } from './rule/apis/delete/delete_rule_route';
import { aggregateRulesRoute } from './rule/apis/aggregate/aggregate_rules_route';
import { disableRuleRoute } from './rule/apis/disable/disable_rule_route';
import { enableRuleRoute } from './rule/apis/enable/enable_rule_route';
import { findRulesRoute, findInternalRulesRoute } from './rule/apis/find/find_rules_route';
import { findRulesRoute } from './rule/apis/find/find_rules_route';
import { findInternalRulesRoute } from './rule/apis/find/find_internal_rules_route';
import { getRuleAlertSummaryRoute } from './get_rule_alert_summary';
import { getRuleExecutionLogRoute } from './get_rule_execution_log';
import { getGlobalExecutionLogRoute } from './get_global_execution_logs';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { httpServiceMock } from '@kbn/core/server/mocks';
import { licenseStateMock } from '../../../../lib/license_state.mock';
import { findInternalRulesRoute } from './find_internal_rules_route';

jest.mock('../../../../lib/license_api_access', () => ({
verifyApiAccess: jest.fn(),
}));

jest.mock('../../../lib/track_legacy_terminology', () => ({
trackLegacyTerminology: jest.fn(),
}));

beforeEach(() => {
jest.resetAllMocks();
});

describe('findInternalRulesRoute', () => {
it('registers the route without public access', async () => {
const licenseState = licenseStateMock.create();
const router = httpServiceMock.createRouter();

findInternalRulesRoute(router, licenseState);
expect(router.post).toHaveBeenCalledWith(
expect.not.objectContaining({
options: expect.objectContaining({ access: 'public' }),
}),
expect.any(Function)
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { IRouter } from '@kbn/core/server';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import type {
FindRulesRequestQueryV1,
FindRulesResponseV1,
} from '../../../../../common/routes/rule/apis/find';
import { findRulesRequestQuerySchemaV1 } from '../../../../../common/routes/rule/apis/find';
import { RuleParamsV1 } from '../../../../../common/routes/rule/response';
import { ILicenseState } from '../../../../lib';
import {
AlertingRequestHandlerContext,
INTERNAL_ALERTING_API_FIND_RULES_PATH,
} from '../../../../types';
import { verifyAccessAndContext } from '../../../lib';
import { trackLegacyTerminology } from '../../../lib/track_legacy_terminology';
import { transformFindRulesBodyV1, transformFindRulesResponseV1 } from './transforms';

export const findInternalRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
router.post(
{
path: INTERNAL_ALERTING_API_FIND_RULES_PATH,
options: { access: 'internal' },
validate: {
body: findRulesRequestQuerySchemaV1,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();

const body: FindRulesRequestQueryV1 = req.body;

trackLegacyTerminology(
[req.body.search, req.body.search_fields, req.body.sort_field].filter(
Boolean
) as string[],
usageCounter
);

const options = transformFindRulesBodyV1({
...body,
has_reference: body.has_reference || undefined,
search_fields: searchFieldsAsArray(body.search_fields),
});

if (req.body.fields) {
usageCounter?.incrementCounter({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({
options,
excludeFromPublicApi: false,
includeSnoozeData: true,
});

const responseBody: FindRulesResponseV1<RuleParamsV1>['body'] =
transformFindRulesResponseV1<RuleParamsV1>(findResult, options.fields);

return res.ok({
body: responseBody,
});
})
)
);
};

function searchFieldsAsArray(searchFields: string | string[] | undefined): string[] | undefined {
if (!searchFields) {
return;
}
return Array.isArray(searchFields) ? searchFields : [searchFields];
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ beforeEach(() => {
});

describe('findRulesRoute', () => {
it('registers the route with public access', async () => {
const licenseState = licenseStateMock.create();
const router = httpServiceMock.createRouter();

findRulesRoute(router, licenseState);
expect(router.get).toHaveBeenCalledWith(
expect.objectContaining({
options: expect.objectContaining({ access: 'public' }),
}),
expect.any(Function)
);
});
it('finds rules with proper parameters', async () => {
const licenseState = licenseStateMock.create();
const router = httpServiceMock.createRouter();
Expand Down
116 changes: 11 additions & 105 deletions x-pack/plugins/alerting/server/routes/rule/apis/find/find_rules_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,26 @@

import { IRouter } from '@kbn/core/server';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { ILicenseState } from '../../../../lib';
import { verifyAccessAndContext } from '../../../lib';
import { findRulesRequestQuerySchemaV1 } from '../../../../../common/routes/rule/apis/find';
import type {
FindRulesRequestQueryV1,
FindRulesResponseV1,
} from '../../../../../common/routes/rule/apis/find';
import { findRulesRequestQuerySchemaV1 } from '../../../../../common/routes/rule/apis/find';
import { RuleParamsV1, ruleResponseSchemaV1 } from '../../../../../common/routes/rule/response';
import {
AlertingRequestHandlerContext,
BASE_ALERTING_API_PATH,
INTERNAL_ALERTING_API_FIND_RULES_PATH,
} from '../../../../types';
import { ILicenseState } from '../../../../lib';
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types';
import { verifyAccessAndContext } from '../../../lib';
import { trackLegacyTerminology } from '../../../lib/track_legacy_terminology';
import { transformFindRulesBodyV1, transformFindRulesResponseV1 } from './transforms';

interface BuildFindRulesRouteParams {
licenseState: ILicenseState;
path: string;
router: IRouter<AlertingRequestHandlerContext>;
excludeFromPublicApi?: boolean;
usageCounter?: UsageCounter;
}

const buildFindRulesRoute = ({
licenseState,
path,
router,
excludeFromPublicApi = false,
usageCounter,
}: BuildFindRulesRouteParams) => {
export const findRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
router.get(
cnasikas marked this conversation as resolved.
Show resolved Hide resolved
{
path,
path: `${BASE_ALERTING_API_PATH}/rules/_find`,
options: {
access: 'public',
summary: 'Get information about rules',
Expand Down Expand Up @@ -91,7 +77,7 @@ const buildFindRulesRoute = ({

const findResult = await rulesClient.find({
options,
excludeFromPublicApi,
excludeFromPublicApi: true,
includeSnoozeData: true,
});

Expand All @@ -104,86 +90,6 @@ const buildFindRulesRoute = ({
})
)
);
if (path === INTERNAL_ALERTING_API_FIND_RULES_PATH) {
router.post(
{
path,
options: { access: 'internal' },
validate: {
body: findRulesRequestQuerySchemaV1,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();

const body: FindRulesRequestQueryV1 = req.body;

trackLegacyTerminology(
[req.body.search, req.body.search_fields, req.body.sort_field].filter(
Boolean
) as string[],
usageCounter
);

const options = transformFindRulesBodyV1({
...body,
has_reference: body.has_reference || undefined,
search_fields: searchFieldsAsArray(body.search_fields),
});

if (req.body.fields) {
usageCounter?.incrementCounter({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({
options,
excludeFromPublicApi,
includeSnoozeData: true,
});

const responseBody: FindRulesResponseV1<RuleParamsV1>['body'] =
transformFindRulesResponseV1<RuleParamsV1>(findResult, options.fields);

return res.ok({
body: responseBody,
});
})
)
);
}
};

export const findRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
buildFindRulesRoute({
excludeFromPublicApi: true,
licenseState,
path: `${BASE_ALERTING_API_PATH}/rules/_find`,
router,
usageCounter,
});
};

export const findInternalRulesRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
buildFindRulesRoute({
excludeFromPublicApi: false,
licenseState,
path: INTERNAL_ALERTING_API_FIND_RULES_PATH,
router,
usageCounter,
});
};

function searchFieldsAsArray(searchFields: string | string[] | undefined): string[] | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,7 @@ describe('Detections Rules API', () => {
expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
query: expect.objectContaining({
filter: 'alert.id:"alert:id1" or alert.id:"alert:id2"',
}),
body: '{"filter":"alert.id:\\"alert:id1\\" or alert.id:\\"alert:id2\\"","fields":"[\\"muteAll\\",\\"activeSnoozes\\",\\"isSnoozedUntil\\",\\"snoozeSchedule\\"]","per_page":2}',
})
);
});
Expand All @@ -867,9 +865,7 @@ describe('Detections Rules API', () => {
expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
query: expect.objectContaining({
per_page: 2,
}),
body: '{"filter":"alert.id:\\"alert:id1\\" or alert.id:\\"alert:id2\\"","fields":"[\\"muteAll\\",\\"activeSnoozes\\",\\"isSnoozedUntil\\",\\"snoozeSchedule\\"]","per_page":2}',
})
);
});
Expand All @@ -880,14 +876,7 @@ describe('Detections Rules API', () => {
expect(fetchMock).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
query: expect.objectContaining({
fields: JSON.stringify([
'muteAll',
'activeSnoozes',
'isSnoozedUntil',
'snoozeSchedule',
]),
}),
body: '{"filter":"alert.id:\\"alert:id1\\" or alert.id:\\"alert:id2\\"","fields":"[\\"muteAll\\",\\"activeSnoozes\\",\\"isSnoozedUntil\\",\\"snoozeSchedule\\"]","per_page":2}',
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ export const fetchRulesSnoozeSettings = async ({
const response = await KibanaServices.get().http.fetch<RulesSnoozeSettingsBatchResponse>(
INTERNAL_ALERTING_API_FIND_RULES_PATH,
{
method: 'GET',
query: {
method: 'POST',
cnasikas marked this conversation as resolved.
Show resolved Hide resolved
body: JSON.stringify({
filter: ids.map((x) => `alert.id:"alert:${x}"`).join(' or '),
fields: JSON.stringify(['muteAll', 'activeSnoozes', 'isSnoozedUntil', 'snoozeSchedule']),
per_page: ids.length,
},
}),
signal,
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { RulesSnoozeSettingsMap } from '../../logic';
import { fetchRulesSnoozeSettings } from '../api';
import { DEFAULT_QUERY_OPTIONS } from './constants';

const FETCH_RULE_SNOOZE_SETTINGS_QUERY_KEY = ['GET', INTERNAL_ALERTING_API_FIND_RULES_PATH];
const FETCH_RULE_SNOOZE_SETTINGS_QUERY_KEY = ['POST', INTERNAL_ALERTING_API_FIND_RULES_PATH];

/**
* A wrapper around useQuery provides default values to the underlying query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ export default function createFindTests({ getService }: FtrProviderContext) {
});

findTestUtils('public', objectRemover, supertest, supertestWithoutAuth);
findTestUtils('internal', objectRemover, supertest, supertestWithoutAuth);
cnasikas marked this conversation as resolved.
Show resolved Hide resolved

describe('stack alerts', () => {
const ruleTypes = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,14 @@ const findTestUtils = (
expect(response.body.data.map((alert: any) => alert.id)).to.eql(firstPage);

const secondResponse = await supertestWithoutAuth
.get(
`${getUrlPrefix(space.id)}/${
describeType === 'public' ? 'api' : 'internal'
}/alerting/rules/_find?per_page=${perPage}&sort_field=createdAt&page=2`
)
.auth(user.username, user.password);
.post(`${getUrlPrefix(space.id)}/internal/alerting/rules/_find`)
.set('kbn-xsrf', 'kibana')
.auth(user.username, user.password)
.send({
per_page: perPage,
sort_field: 'createdAt',
page: 2,
});

expect(secondResponse.body.data.map((alert: any) => alert.id)).to.eql(secondPage);
}
Expand Down
Loading