Skip to content

Commit

Permalink
[Logstash Plugin] Migrate authc.getCurrentUser usage to coreContext.s…
Browse files Browse the repository at this point in the history
…ecurity (#187180)

Part of #186574

## Summary

This PR migrates the Logstash Plugin's route handler for saving a
pipeline, which consumes `authc.getCurrentUser`, to use
`coreContext.security`.

Background: This PR serves as an example of a plugin migrating away from
depending on the Security plugin, which is a high priority effort for
the last release before 9.0.

### Checklist

Delete any items that are not applicable to this PR.

- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
tsullivan and kibanamachine authored Jul 3, 2024
1 parent 0a0bb14 commit 482f2a9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
4 changes: 1 addition & 3 deletions x-pack/plugins/logstash/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import { CoreSetup, CoreStart, Logger, Plugin, PluginInitializerContext } from '@kbn/core/server';
import { LicensingPluginSetup } from '@kbn/licensing-plugin/server';
import { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-plugin/server';
import { SecurityPluginSetup } from '@kbn/security-plugin/server';
import { registerRoutes } from './routes';

interface SetupDeps {
licensing: LicensingPluginSetup;
security?: SecurityPluginSetup;
features: FeaturesPluginSetup;
}

Expand All @@ -27,7 +25,7 @@ export class LogstashPlugin implements Plugin {
setup(core: CoreSetup, deps: SetupDeps) {
this.logger.debug('Setting up Logstash plugin');

registerRoutes(core.http.createRouter(), deps.security);
registerRoutes(core.http.createRouter());

deps.features.registerElasticsearchFeature({
id: 'pipelines',
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/logstash/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* 2.0.
*/

import { SecurityPluginSetup } from '@kbn/security-plugin/server';
import type { LogstashPluginRouter } from '../types';
import { registerClusterLoadRoute } from './cluster';
import {
Expand All @@ -15,12 +14,12 @@ import {
} from './pipeline';
import { registerPipelinesListRoute, registerPipelinesDeleteRoute } from './pipelines';

export function registerRoutes(router: LogstashPluginRouter, security?: SecurityPluginSetup) {
export function registerRoutes(router: LogstashPluginRouter) {
registerClusterLoadRoute(router);

registerPipelineDeleteRoute(router);
registerPipelineLoadRoute(router);
registerPipelineSaveRoute(router, security);
registerPipelineSaveRoute(router);

registerPipelinesListRoute(router);
registerPipelinesDeleteRoute(router);
Expand Down
16 changes: 5 additions & 11 deletions x-pack/plugins/logstash/server/routes/pipeline/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import { schema } from '@kbn/config-schema';
import { i18n } from '@kbn/i18n';

import { wrapRouteWithLicenseCheck } from '@kbn/licensing-plugin/server';
import { SecurityPluginSetup } from '@kbn/security-plugin/server';
import { Pipeline } from '../../models/pipeline';
import { checkLicense } from '../../lib/check_license';
import type { LogstashPluginRouter } from '../../types';

export function registerPipelineSaveRoute(
router: LogstashPluginRouter,
security?: SecurityPluginSetup
) {
export function registerPipelineSaveRoute(router: LogstashPluginRouter) {
router.put(
{
path: '/api/logstash/pipeline/{id}',
Expand All @@ -39,14 +35,12 @@ export function registerPipelineSaveRoute(
wrapRouteWithLicenseCheck(
checkLicense,
router.handleLegacyErrors(async (context, request, response) => {
const coreContext = await context.core;
try {
let username: string | undefined;
if (security) {
const user = await security.authc.getCurrentUser(request);
username = user?.username;
}
const user = coreContext.security.authc.getCurrentUser();
const username = user?.username;

const { client } = (await context.core).elasticsearch;
const { client } = coreContext.elasticsearch;
const pipeline = Pipeline.fromDownstreamJSON(request.body, request.params.id, username);

await client.asCurrentUser.logstash.putPipeline({
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/logstash/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

"@kbn/features-plugin",
"@kbn/licensing-plugin",
"@kbn/security-plugin",
"@kbn/i18n",
"@kbn/i18n-react",
"@kbn/test-jest-helpers",
Expand Down

0 comments on commit 482f2a9

Please sign in to comment.