Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate getCurrentUser calls in reporting to core security service #186913

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
security?: SecurityServiceStart;
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
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: any[] = []; // hack around type error: Argument of type 'CsvSearchSourceExportType|CsvV2ExportType|PdfV1ExportType|PdfExportType|PngExportType' is not assignable to parameter of type 'never'
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved

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,
security: core.security,
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
...plugins,
});

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 { security: securityService } = await reporting.getPluginStartDeps();
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
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) {
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
return securityService?.authc.getCurrentUser(request) ?? false;
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
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 } },
security: { authc: { getCurrentUser: () => undefined } }, // security comes from core here
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
},
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' }),
},
security: coreStartMock.security, // we need authc from core.security start
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
logger,
screenshotting: createMockScreenshottingStart(),
...startMock,
...startMock, // allows to override with test instances
};
};

Expand Down
10 changes: 4 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,8 @@ import type {
import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';

import { ExportTypesRegistry } from '@kbn/reporting-server/export_types_registry';
import type { SecurityServiceStart } from '@kbn/core-security-server';
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
import type { AuthenticatedUser } from '@kbn/core-security-common';

/**
* Plugin Setup Contract
Expand Down Expand Up @@ -70,7 +68,7 @@ export interface ReportingStartDeps {
licensing: LicensingPluginStart;
taskManager: TaskManagerStartContract;
screenshotting?: ScreenshottingStart;
security?: SecurityPluginStart;
security?: SecurityServiceStart;
TinaHeiligers marked this conversation as resolved.
Show resolved Hide resolved
}

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