forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.16] [ResponseOps][Rules] Remove unintended internal Find routes AP…
…I with public access (elastic#193757) (elastic#197358) # Backport This will backport the following commits from `main` to `8.16`: - [[ResponseOps][Rules] Remove unintended internal Find routes API with public access (elastic#193757)](elastic#193757) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Zacqary Adam Xeper","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-23T04:33:31Z","message":"[ResponseOps][Rules] Remove unintended internal Find routes API with public access (elastic#193757)\n\n## Summary\r\n\r\nFixes elastic#192957 \r\n\r\nRemoves the `internal/_find` route from public access by moving the\r\nhard-coded `options` into the route builder functions.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"196cabad9bbd0511219fc7833a62cb8a0bb61514","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:ResponseOps","v9.0.0","Feature:Alerting/RulesFramework","backport:prev-minor","ci:project-deploy-observability","v8.16.0"],"title":"[ResponseOps][Rules] Remove unintended internal Find routes API with public access","number":193757,"url":"https://github.com/elastic/kibana/pull/193757","mergeCommit":{"message":"[ResponseOps][Rules] Remove unintended internal Find routes API with public access (elastic#193757)\n\n## Summary\r\n\r\nFixes elastic#192957 \r\n\r\nRemoves the `internal/_find` route from public access by moving the\r\nhard-coded `options` into the route builder functions.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"196cabad9bbd0511219fc7833a62cb8a0bb61514"}},"sourceBranch":"main","suggestedTargetBranches":["8.16"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193757","number":193757,"mergeCommit":{"message":"[ResponseOps][Rules] Remove unintended internal Find routes API with public access (elastic#193757)\n\n## Summary\r\n\r\nFixes elastic#192957 \r\n\r\nRemoves the `internal/_find` route from public access by moving the\r\nhard-coded `options` into the route builder functions.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"196cabad9bbd0511219fc7833a62cb8a0bb61514"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Zacqary Adam Xeper <[email protected]>
- Loading branch information
1 parent
30127c0
commit d77d619
Showing
19 changed files
with
1,450 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/alerting/server/routes/rule/apis/find/find_internal_rules_route.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}); | ||
}); |
87 changes: 87 additions & 0 deletions
87
x-pack/plugins/alerting/server/routes/rule/apis/find/find_internal_rules_route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.