Skip to content

Commit

Permalink
SpacesService/SpacesClient should still support legacy/fake requests …
Browse files Browse the repository at this point in the history
…for the time being
  • Loading branch information
legrego committed Oct 11, 2019
1 parent 1048229 commit 8aa701b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/spaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ export const spaces = (kibana: Record<string, any>) =>
});

server.expose('getSpaceId', (request: Legacy.Request) =>
spacesPlugin.spacesService.getSpaceId(KibanaRequest.from(request))
spacesPlugin.spacesService.getSpaceId(request)
);
server.expose('getActiveSpace', (request: Legacy.Request) =>
spacesPlugin.spacesService.getActiveSpace(KibanaRequest.from(request))
spacesPlugin.spacesService.getActiveSpace(request)
);
server.expose('spaceIdToNamespace', spacesPlugin.spacesService.spaceIdToNamespace);
server.expose('namespaceToSpaceId', spacesPlugin.spacesService.namespaceToSpaceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import Boom from 'boom';
import { omit } from 'lodash';
import { Legacy } from 'kibana';
import { KibanaRequest } from 'src/core/server';
import { AuthorizationService } from '../../../../../legacy/plugins/security/server/lib/authorization/service';
import { isReservedSpace } from '../../../common/is_reserved_space';
Expand All @@ -13,6 +14,8 @@ import { SpacesAuditLogger } from '../audit_logger';
import { ConfigType } from '../../config';
import { GetSpacePurpose } from '../../../common/model/types';

type SpacesClientRequestFacade = Legacy.Request | KibanaRequest;

const SUPPORTED_GET_SPACE_PURPOSES: GetSpacePurpose[] = ['any', 'copySavedObjectsIntoSpace'];

const PURPOSE_PRIVILEGE_MAP: Record<
Expand All @@ -32,7 +35,7 @@ export class SpacesClient {
private readonly callWithRequestSavedObjectRepository: any,
private readonly config: ConfigType,
private readonly internalSavedObjectRepository: any,
private readonly request: KibanaRequest
private readonly request: SpacesClientRequestFacade
) {}

public async canEnumerateSpaces(): Promise<boolean> {
Expand Down
19 changes: 11 additions & 8 deletions x-pack/plugins/spaces/server/spaces_service/spaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { map, take } from 'rxjs/operators';
import { Observable, Subscription, combineLatest } from 'rxjs';
import { Legacy } from 'kibana';
import { Logger, KibanaRequest, CoreSetup } from 'src/core/server';
import { SecurityPlugin } from '../../../../legacy/plugins/security';
import { OptionalPlugin } from '../../../../legacy/server/lib/optional_plugin';
Expand All @@ -17,20 +18,22 @@ import { DEFAULT_SPACE_ID } from '../../common/constants';
import { spaceIdToNamespace, namespaceToSpaceId } from '../lib/utils/namespace';
import { Space } from '../../common/model/space';

type RequestFacade = KibanaRequest | Legacy.Request;

export interface SpacesServiceSetup {
scopedClient(request: KibanaRequest): Promise<SpacesClient>;
scopedClient(request: RequestFacade): Promise<SpacesClient>;

getSpaceId(request: KibanaRequest): string;
getSpaceId(request: RequestFacade): string;

getBasePath(spaceId: string): string;

isInDefaultSpace(request: KibanaRequest): boolean;
isInDefaultSpace(request: RequestFacade): boolean;

spaceIdToNamespace(spaceId: string): string | undefined;

namespaceToSpaceId(namespace: string | undefined): string;

getActiveSpace(request: KibanaRequest): Promise<Space>;
getActiveSpace(request: RequestFacade): Promise<Space>;
}

interface SpacesServiceDeps {
Expand All @@ -53,7 +56,7 @@ export class SpacesService {
config$,
getSpacesAuditLogger,
}: SpacesServiceDeps): Promise<SpacesServiceSetup> {
const getSpaceId = (request: KibanaRequest) => {
const getSpaceId = (request: RequestFacade) => {
// Currently utilized by reporting
const isFakeRequest = typeof (request as any).getBasePath === 'function';

Expand All @@ -66,7 +69,7 @@ export class SpacesService {
return spaceId;
};

const getScopedClient = async (request: KibanaRequest) => {
const getScopedClient = async (request: RequestFacade) => {
return combineLatest(elasticsearch.adminClient$, config$)
.pipe(
map(([clusterClient, config]) => {
Expand Down Expand Up @@ -111,15 +114,15 @@ export class SpacesService {
}
return addSpaceIdToPath(http.basePath.serverBasePath, spaceId);
},
isInDefaultSpace: (request: KibanaRequest) => {
isInDefaultSpace: (request: RequestFacade) => {
const spaceId = getSpaceId(request);

return spaceId === DEFAULT_SPACE_ID;
},
spaceIdToNamespace,
namespaceToSpaceId,
scopedClient: getScopedClient,
getActiveSpace: async (request: KibanaRequest) => {
getActiveSpace: async (request: RequestFacade) => {
const spaceId = getSpaceId(request);
const spacesClient = await getScopedClient(request);
return spacesClient.get(spaceId);
Expand Down

0 comments on commit 8aa701b

Please sign in to comment.