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.
[ResponseOps][Rules] Remove unintended internal Find routes API with …
…public access (elastic#193757) ## Summary Fixes elastic#192957 Removes the `internal/_find` route from public access by moving the hard-coded `options` into the route builder functions. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
75a6406
commit 196caba
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.