Skip to content

Commit

Permalink
[Search Session] Use server-side authc.getCurrentUser from core.secur…
Browse files Browse the repository at this point in the history
…ity (#186919)

Part of #186574

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. The Search Session service uses the
`authc.getCurrentUser` method as a means to create a scoped client with
methods that filters saved search sessions to the current user.

### 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
  • Loading branch information
tsullivan authored Jun 27, 2024
1 parent 1224a05 commit 3d99878
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/plugins/data/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"management"
],
"optionalPlugins": [
"usageCollection",
"security"
"usageCollection"
],
"requiredBundles": [
"kibanaUtils",
Expand Down
5 changes: 1 addition & 4 deletions src/plugins/data/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { BfetchServerSetup } from '@kbn/bfetch-plugin/server';
import { PluginStart as DataViewsServerPluginStart } from '@kbn/data-views-plugin/server';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { FieldFormatsSetup, FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';
import { ConfigSchema } from '../config';
import type { ISearchSetup, ISearchStart } from './search';
import { DatatableUtilitiesService } from './datatable_utilities';
Expand Down Expand Up @@ -51,7 +50,6 @@ export interface DataPluginSetupDependencies {
expressions: ExpressionsServerSetup;
usageCollection?: UsageCollectionSetup;
fieldFormats: FieldFormatsSetup;
security?: SecurityPluginSetup;
}

export interface DataPluginStartDependencies {
Expand Down Expand Up @@ -86,7 +84,7 @@ export class DataServerPlugin

public setup(
core: CoreSetup<DataPluginStartDependencies, DataPluginStart>,
{ bfetch, expressions, usageCollection, fieldFormats, security }: DataPluginSetupDependencies
{ bfetch, expressions, usageCollection, fieldFormats }: DataPluginSetupDependencies
) {
this.scriptsService.setup(core);
const querySetup = this.queryService.setup(core);
Expand All @@ -98,7 +96,6 @@ export class DataServerPlugin
bfetch,
expressions,
usageCollection,
security,
});

return {
Expand Down
6 changes: 2 additions & 4 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { ExpressionsServerSetup } from '@kbn/expressions-plugin/server';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { KbnServerError } from '@kbn/kibana-utils-plugin/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';
import type { DataViewsServerPluginStart } from '@kbn/data-views-plugin/server';
import type {
DataRequestHandlerContext,
Expand Down Expand Up @@ -110,7 +109,6 @@ export interface SearchServiceSetupDependencies {
bfetch: BfetchServerSetup;
expressions: ExpressionsServerSetup;
usageCollection?: UsageCollectionSetup;
security?: SecurityPluginSetup;
}

/** @internal */
Expand Down Expand Up @@ -147,7 +145,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {

public setup(
core: CoreSetup<DataPluginStartDependencies, DataPluginStart>,
{ bfetch, expressions, usageCollection, security }: SearchServiceSetupDependencies
{ bfetch, expressions, usageCollection }: SearchServiceSetupDependencies
): ISearchSetup {
core.savedObjects.registerType(searchSessionSavedObjectType);
const usage = usageCollection ? usageProvider(core) : undefined;
Expand All @@ -156,7 +154,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
registerSearchRoute(router);
registerSessionRoutes(router, this.logger);

this.sessionService.setup(core, { security });
this.sessionService.setup(core, {});

core.http.registerRouteHandlerContext<DataRequestHandlerContext, 'search'>(
'search',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createRequestHash } from './utils';
import moment from 'moment';
import { coreMock } from '@kbn/core/server/mocks';
import { ConfigSchema } from '../../../config';
import type { AuthenticatedUser } from '@kbn/security-plugin/common';
import type { AuthenticatedUser } from '@kbn/core/server';
import { SEARCH_SESSION_TYPE, SearchSessionStatus } from '../../../common';
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';

Expand Down
15 changes: 5 additions & 10 deletions src/plugins/data/server/search/session/session_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SavedObjectsFindOptions,
ElasticsearchClient,
} from '@kbn/core/server';
import type { AuthenticatedUser, SecurityPluginSetup } from '@kbn/security-plugin/server';
import type { AuthenticatedUser } from '@kbn/core/server';
import { defer } from '@kbn/kibana-utils-plugin/common';
import type { IKibanaSearchRequest, ISearchOptions } from '@kbn/search-types';
import { debounce } from 'lodash';
Expand All @@ -43,10 +43,8 @@ export interface SearchSessionStatusDependencies extends SearchSessionDependenci
internalElasticsearchClient: ElasticsearchClient;
}

interface SetupDependencies {
security?: SecurityPluginSetup;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface SetupDependencies {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface StartDependencies {}

Expand All @@ -68,7 +66,6 @@ interface TrackIdQueueEntry {

export class SearchSessionService implements ISearchSessionService {
private sessionConfig: SearchSessionsConfigSchema;
private security?: SecurityPluginSetup;
private setupCompleted = false;

constructor(
Expand All @@ -80,8 +77,6 @@ export class SearchSessionService implements ISearchSessionService {
}

public setup(core: CoreSetup, deps: SetupDependencies) {
this.security = deps.security;

this.setupCompleted = true;
}

Expand Down Expand Up @@ -419,9 +414,9 @@ export class SearchSessionService implements ISearchSessionService {
return session.attributes.idMapping[requestHash].id;
};

public asScopedProvider = ({ savedObjects, elasticsearch }: CoreStart) => {
public asScopedProvider = ({ security, savedObjects, elasticsearch }: CoreStart) => {
return (request: KibanaRequest) => {
const user = this.security?.authc.getCurrentUser(request) ?? null;
const user = security.authc.getCurrentUser(request) ?? null;
const savedObjectsClient = savedObjects.getScopedClient(request, {
includedHiddenTypes: [SEARCH_SESSION_TYPE],
});
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"@kbn/field-formats-plugin",
"@kbn/data-views-plugin",
"@kbn/screenshot-mode-plugin",
"@kbn/security-plugin",
"@kbn/expressions-plugin",
"@kbn/field-types",
"@kbn/es-query",
Expand Down

0 comments on commit 3d99878

Please sign in to comment.