kibana/x-pack/plugins/spaces/server/spaces_service/spaces_service.ts
Larry Gregory 064fd8ef84
[7.x] Removing circular dependency between spaces and security (#81891) (#83841)
* Removing circular dependency between spaces and security

* Apply suggestions from code review

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* Tests refactor

- Reorganize top level describes into 3 space-based blocks into based on spaces:
  - space disabled
  - spaces plugin unavailable
  - space enabled (most previous tests go under this new block) with new beforeEach

- wrote new tests for uncovered lines 58, 66-69

* Review1: address PR feedback

* changing fake requests for alerts/actions

* Fixing tests

* fixing more tests

* Additional testing and refactoring

* Apply suggestions from code review

Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>

* Review 2: Address feedback

* Make ESLint happy again

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>
Co-authored-by: Constance Chen <constance.chen.3@gmail.com>

Co-authored-by: Constance <constancecchen@users.noreply.github.com>
Co-authored-by: Aleh Zasypkin <aleh.zasypkin@gmail.com>
Co-authored-by: Constance Chen <constance.chen.3@gmail.com>
2020-11-19 15:56:05 -05:00

131 lines
3.8 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import type { KibanaRequest, IBasePath } from 'src/core/server';
import { SpacesClientServiceStart } from '../spaces_client';
import { getSpaceIdFromPath } from '../../common';
import { DEFAULT_SPACE_ID } from '../../common/constants';
import { spaceIdToNamespace, namespaceToSpaceId } from '../lib/utils/namespace';
import { Space } from '..';
export interface SpacesServiceSetup {
/**
* Retrieves the space id associated with the provided request.
* @param request
*
* @deprecated Use `getSpaceId` from the `SpacesServiceStart` contract instead.
*/
getSpaceId(request: KibanaRequest): string;
/**
* Converts the provided space id into the corresponding Saved Objects `namespace` id.
* @param spaceId
*
* @deprecated use `spaceIdToNamespace` from the `SpacesServiceStart` contract instead.
*/
spaceIdToNamespace(spaceId: string): string | undefined;
/**
* Converts the provided namespace into the corresponding space id.
* @param namespace
*
* @deprecated use `namespaceToSpaceId` from the `SpacesServiceStart` contract instead.
*/
namespaceToSpaceId(namespace: string | undefined): string;
}
export interface SpacesServiceStart {
/**
* Creates a scoped instance of the SpacesClient.
*/
createSpacesClient: SpacesClientServiceStart['createSpacesClient'];
/**
* Retrieves the space id associated with the provided request.
* @param request
*/
getSpaceId(request: KibanaRequest): string;
/**
* Indicates if the provided request is executing within the context of the `default` space.
* @param request
*/
isInDefaultSpace(request: KibanaRequest): boolean;
/**
* Retrieves the Space associated with the provided request.
* @param request
*/
getActiveSpace(request: KibanaRequest): Promise<Space>;
/**
* Converts the provided space id into the corresponding Saved Objects `namespace` id.
* @param spaceId
*/
spaceIdToNamespace(spaceId: string): string | undefined;
/**
* Converts the provided namespace into the corresponding space id.
* @param namespace
*/
namespaceToSpaceId(namespace: string | undefined): string;
}
interface SpacesServiceSetupDeps {
basePath: IBasePath;
}
interface SpacesServiceStartDeps {
basePath: IBasePath;
spacesClientService: SpacesClientServiceStart;
}
export class SpacesService {
public setup({ basePath }: SpacesServiceSetupDeps): SpacesServiceSetup {
return {
getSpaceId: (request: KibanaRequest) => {
return this.getSpaceId(request, basePath);
},
spaceIdToNamespace,
namespaceToSpaceId,
};
}
public start({ basePath, spacesClientService }: SpacesServiceStartDeps) {
return {
getSpaceId: (request: KibanaRequest) => {
return this.getSpaceId(request, basePath);
},
getActiveSpace: (request: KibanaRequest) => {
const spaceId = this.getSpaceId(request, basePath);
return spacesClientService.createSpacesClient(request).get(spaceId);
},
isInDefaultSpace: (request: KibanaRequest) => {
const spaceId = this.getSpaceId(request, basePath);
return spaceId === DEFAULT_SPACE_ID;
},
createSpacesClient: (request: KibanaRequest) =>
spacesClientService.createSpacesClient(request),
spaceIdToNamespace,
namespaceToSpaceId,
};
}
public stop() {}
private getSpaceId(request: KibanaRequest, basePathService: IBasePath) {
const basePath = basePathService.get(request);
const { spaceId } = getSpaceIdFromPath(basePath, basePathService.serverBasePath);
return spaceId;
}
}