Skip to content

Commit

Permalink
migrate getCurrentUser calls in reporting to core security service (#…
Browse files Browse the repository at this point in the history
…186913)

## Summary

Part of #186574

Background: This PR is 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 Reporting plugin uses
`authc.getCurrentUser` from the security plugin's start contract on the
server side.

This PR migrates `authc.getCurrentUser` from the security plugin start
contract to the core security service.

### Checklist

- [X] [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]>
Co-authored-by: Tim Sullivan <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2024
1 parent 64a01ef commit bebb273
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 34 deletions.
9 changes: 5 additions & 4 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as Rx from 'rxjs';
import { map, take } from 'rxjs';

import {
import type {
AnalyticsServiceStart,
CoreSetup,
DocLinksServiceSetup,
Expand All @@ -19,6 +19,7 @@ import {
PackageInfo,
PluginInitializerContext,
SavedObjectsServiceStart,
SecurityServiceStart,
StatusServiceSetup,
UiSettingsServiceStart,
} from '@kbn/core/server';
Expand All @@ -38,7 +39,7 @@ import { PngExportType } from '@kbn/reporting-export-types-png';
import type { ReportingConfigType } from '@kbn/reporting-server';
import { ExportType } from '@kbn/reporting-server';
import { ScreenshottingStart } from '@kbn/screenshotting-plugin/server';
import type { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common';
import type { SpacesPluginSetup } from '@kbn/spaces-plugin/server';
import type {
Expand Down Expand Up @@ -82,7 +83,7 @@ export interface ReportingInternalStart {
licensing: LicensingPluginStart;
logger: Logger;
screenshotting?: ScreenshottingStart;
security?: SecurityPluginStart;
securityService: SecurityServiceStart;
taskManager: TaskManagerStartContract;
}

Expand Down Expand Up @@ -214,7 +215,7 @@ export class ReportingCore {
*/
private getExportTypes(): ExportType[] {
const { csv, pdf, png } = this.config.export_types;
const exportTypes = [];
const exportTypes: ExportType[] = [];

if (csv.enabled) {
// NOTE: CsvSearchSourceExportType should be deprecated and replaced with V2 in the UI: https://github.com/elastic/kibana/issues/151190
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class ReportingPlugin
savedObjects,
uiSettings,
store,
securityService: core.security,
...plugins,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('authorized_user_pre_routing', function () {

mockStartDeps = await createMockPluginStart(
{
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('authorized_user_pre_routing', function () {
security: { license: { isEnabled: () => true } },
});
mockStartDeps = await createMockPluginStart(
{ security: { authc: { getCurrentUser: () => null } } },
{ securityService: { authc: { getCurrentUser: () => null } } },
mockReportingConfig
);
mockCore = await createMockReportingCore(mockReportingConfig, mockSetupDeps, mockStartDeps);
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('authorized_user_pre_routing', function () {
it(`should return with 403 when security is enabled but user doesn't have the allowed role`, async function () {
mockStartDeps = await createMockPluginStart(
{
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['peasant'], username: 'Tom Riddle' }),
},
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('authorized_user_pre_routing', function () {
it('should return from handler when security is enabled and user has explicitly allowed role', async function () {
mockStartDeps = await createMockPluginStart(
{
security: {
securityService: {
authc: {
getCurrentUser: () => ({ username: 'friendlyuser', roles: ['reporting_user'] }),
},
Expand All @@ -176,7 +176,7 @@ describe('authorized_user_pre_routing', function () {
it('should return from handler when security is enabled and user has superuser role', async function () {
mockStartDeps = await createMockPluginStart(
{
security: {
securityService: {
authc: { getCurrentUser: () => ({ username: 'friendlyuser', roles: ['superuser'] }) },
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export const authorizedUserPreRouting = <P, Q, B>(
reporting: ReportingCore,
handler: RequestHandlerUser<P, Q, B>
): RequestHandler<P, Q, B, ReportingRequestHandlerContext, RouteMethod> => {
const { logger, security, docLinks } = reporting.getPluginSetupDeps();
const { logger, security: securitySetup, docLinks } = reporting.getPluginSetupDeps(); // ReportingInternalSetup.security?: SecurityPluginSetup | undefined

return async (context, req, res) => {
const { security: securityStart } = await reporting.getPluginStartDeps();
const { securityService } = await reporting.getPluginStartDeps();
try {
let user: ReportingRequestUser = false;
if (security && security.license.isEnabled()) {
// find the authenticated user, or null if security is not enabled
user = getUser(req, securityStart);
if (securitySetup && securitySetup.license.isEnabled()) {
// find the authenticated user, only if license is enabled
user = getUser(req, securityService);
if (!user) {
// security is enabled but the user is null
return res.unauthorized({ body: `Sorry, you aren't authenticated` });
Expand Down
7 changes: 3 additions & 4 deletions x-pack/plugins/reporting/server/routes/common/get_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* 2.0.
*/

import { KibanaRequest } from '@kbn/core/server';
import { SecurityPluginStart } from '@kbn/security-plugin/server';
import { KibanaRequest, SecurityServiceStart } from '@kbn/core/server';

export function getUser(request: KibanaRequest, security?: SecurityPluginStart) {
return security?.authc.getCurrentUser(request) ?? false;
export function getUser(request: KibanaRequest, securityService: SecurityServiceStart) {
return securityService.authc.getCurrentUser(request) ?? false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ beforeEach(async () => {

mockStartDeps = await createMockPluginStart(
{
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe(`POST ${INTERNAL_ROUTES.GENERATE_PREFIX}`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe(`Reporting Job Management Routes: Internal`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down Expand Up @@ -175,7 +175,7 @@ describe(`Reporting Job Management Routes: Internal`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: { authc: { getCurrentUser: () => undefined } },
securityService: { authc: { getCurrentUser: () => undefined } }, // security comes from core here
},
mockConfigSchema
);
Expand Down Expand Up @@ -389,7 +389,7 @@ describe(`Reporting Job Management Routes: Internal`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['peasant'], username: 'Tom Riddle' }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe(`POST ${PUBLIC_ROUTES.GENERATE_PREFIX}`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe(`Reporting Job Management Routes: Public`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: {
securityService: {
authc: {
getCurrentUser: () => ({ id: '123', roles: ['superuser'], username: 'Tom Riddle' }),
},
Expand Down Expand Up @@ -165,7 +165,7 @@ describe(`Reporting Job Management Routes: Public`, () => {
...licensingMock.createStart(),
license$: new BehaviorSubject({ isActive: true, isAvailable: true, type: 'gold' }),
},
security: { authc: { getCurrentUser: () => undefined } },
securityService: { authc: { getCurrentUser: () => undefined } },
},
mockConfigSchema
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { securityMock } from '@kbn/security-plugin/server/mocks';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';
import { ReportingCore } from '..';

import { ReportingInternalSetup, ReportingInternalStart } from '../core';
import type { ReportingInternalSetup, ReportingInternalStart } from '../core';
import { ReportingStore } from '../lib';

export const createMockPluginSetup = (
Expand All @@ -51,6 +51,7 @@ export const createMockPluginSetup = (
};

const coreSetupMock = coreMock.createSetup();
const coreStartMock = coreMock.createStart();
const logger = loggingSystemMock.createLogger();

const createMockReportingStore = async (config: ReportingConfigType) => {
Expand Down Expand Up @@ -81,9 +82,10 @@ export const createMockPluginStart = async (
...licensingMock.createStart(),
license$: new BehaviorSubject({ isAvailable: true, isActive: true, type: 'basic' }),
},
securityService: coreStartMock.security, // we need authc from core.security start
logger,
screenshotting: createMockScreenshottingStart(),
...startMock,
...startMock, // allows to override with test instances
};
};

Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/reporting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ import type {
PngScreenshotOptions as BasePngScreenshotOptions,
ScreenshottingStart,
} from '@kbn/screenshotting-plugin/server';
import type {
AuthenticatedUser,
SecurityPluginSetup,
SecurityPluginStart,
} from '@kbn/security-plugin/server';
import type { SecurityPluginSetup } from '@kbn/security-plugin/server';
import type { SpacesPluginSetup } from '@kbn/spaces-plugin/server';
import type {
TaskManagerSetupContract,
Expand All @@ -34,6 +30,7 @@ import type {
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';

import { ExportTypesRegistry } from '@kbn/reporting-server/export_types_registry';
import type { AuthenticatedUser } from '@kbn/core-security-common';

/**
* Plugin Setup Contract
Expand Down Expand Up @@ -70,7 +67,6 @@ export interface ReportingStartDeps {
licensing: LicensingPluginStart;
taskManager: TaskManagerStartContract;
screenshotting?: ScreenshottingStart;
security?: SecurityPluginStart;
}

export type ReportingRequestHandlerContext = CustomRequestHandlerContext<{
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/reporting/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@kbn/reporting-csv-share-panel",
"@kbn/react-kibana-context-render",
"@kbn/react-kibana-mount",
"@kbn/core-security-common",
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit bebb273

Please sign in to comment.