Skip to content

Commit

Permalink
fix more types, define SO
Browse files Browse the repository at this point in the history
  • Loading branch information
mattapperson committed Jul 23, 2019
1 parent 8b6320f commit a8d5111
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import { AutocompleteSuggestion } from 'ui/autocomplete_providers';

import { FrontendLibs } from '../lib/types';
import { RendererFunction } from '../utils/typed_react';
import { RendererFunction } from '../../common/types/helpers';

interface WithKueryAutocompletionLifecycleProps {
libs: FrontendLibs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SODatabaseAdapter } from '../so_database/default';

import {
ConfigurationFile,
NewConfigurationFile,
Expand All @@ -12,6 +14,8 @@ import {
} from './adapter_types';

export class ConfigAdapter {
constructor(private readonly so: SODatabaseAdapter) {}

public async create(
configuration: NewConfigurationFile
): Promise<{ id: string; shared_id: string; version: number }> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,32 @@

import { Lifecycle, ResponseToolkit } from 'hapi';
import * as t from 'io-ts';
import { LICENSES } from '../../../../common/constants/security';
import { Legacy } from 'kibana';
import { Cluster, ClusterConfig } from 'src/legacy/core_plugins/elasticsearch';
import { ApmOssPlugin } from 'src/legacy/core_plugins/apm_oss';
import { Request } from 'src/legacy/server/kbn_server';
import { XPackInfo } from '../../../../../xpack_main/server/lib/xpack_info';
import {
Feature,
FeatureWithAllOrReadPrivileges,
} from '../../../../../xpack_main/server/lib/feature_registry/feature_registry';
import { SecurityPlugin } from '../../../../../security';

export const internalAuthData = Symbol('internalAuthData');
export const internalUser: FrameworkInternalUser = {
kind: 'internal',
};

export interface XpackInfo {
license: {
getType: () => typeof LICENSES[number];
/** Is the license expired */
isActive: () => boolean;
getExpiryDateInMillis: () => number;
};
feature: (pluginId: string) => any;
isAvailable: () => boolean;
}

export interface KibanaLegacyServer {
export interface KibanaLegacyServer extends Legacy.Server {
plugins: {
xpack_main: {
status: {
on: (status: 'green' | 'yellow' | 'red', callback: () => void) => void;
};
info: XpackInfo;
info: XPackInfo;
createXPackInfo(options: any): any;
getFeatures(): Feature[];
registerFeature(feature: FeatureWithAllOrReadPrivileges): void;
};
kibana: {
status: {
Expand All @@ -41,18 +42,20 @@ export interface KibanaLegacyServer {
};
};
};
security: {
getUser: (request: KibanaServerRequest) => any;
};
security: SecurityPlugin;
elasticsearch: {
status: {
on: (status: 'green' | 'yellow' | 'red', callback: () => void) => void;
};
getCluster: () => any;
getCluster(name: string): Cluster;
createCluster(name: string, config: ClusterConfig): Cluster;
waitUntilReady(): Promise<void>;
};
ingest: {};
spaces: any;
apm_oss: ApmOssPlugin;
ingest: any;
};
expose: (name: string, value: any) => void;
expose: { (key: string, value: any): void; (obj: object): void };
config: () => any;
route: (routeConfig: any) => void;
log: (message: string) => void;
Expand Down Expand Up @@ -97,7 +100,6 @@ export const RuntimeKibanaServerRequest = t.interface(
},
'KibanaServerRequest'
);
export interface KibanaServerRequest extends t.TypeOf<typeof RuntimeKibanaServerRequest> {}

export const RuntimeKibanaUser = t.interface(
{
Expand Down Expand Up @@ -133,32 +135,27 @@ export type FrameworkUser<AuthDataType = any> =
| FrameworkAuthenticatedUser<AuthDataType>
| FrameworkUnAuthenticatedUser
| FrameworkInternalUser;
export interface FrameworkRequest<
KibanaServerRequestGenaric extends Partial<KibanaServerRequest> = any
> {
export interface FrameworkRequest<KibanaServerRequestGenaric extends Partial<Request> = Request> {
user: FrameworkUser<KibanaServerRequestGenaric['headers']>;
headers: KibanaServerRequestGenaric['headers'];
info: KibanaServerRequest['info'];
info: Request['info'];
payload: KibanaServerRequestGenaric['payload'];
params: KibanaServerRequestGenaric['params'];
query: KibanaServerRequestGenaric['query'];
}

export interface FrameworkRouteOptions<
RouteRequest extends FrameworkRequest = FrameworkRequest,
RouteResponse extends FrameworkResponse = any
> {
export interface FrameworkRouteOptions<RouteResponse extends FrameworkResponse = any> {
path: string;
method: string | string[];
vhost?: string;
licenseRequired?: string[];
requiredRoles?: string[];
handler: FrameworkRouteHandler<RouteRequest, RouteResponse>;
handler: FrameworkRouteHandler<Request, RouteResponse>;
config?: {};
}

export type FrameworkRouteHandler<
RouteRequest extends KibanaServerRequest,
RouteRequest extends Request,
RouteResponse extends FrameworkResponse
> = (request: FrameworkRequest<RouteRequest>, h: ResponseToolkit) => Promise<RouteResponse>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

import { PathReporter } from 'io-ts/lib/PathReporter';
import { get } from 'lodash';
import { Request } from 'src/legacy/server/kbn_server';
import { XPackInfo } from '../../../../../xpack_main/server/lib/xpack_info';
// @ts-ignore
import { mirrorPluginStatus } from '../../../../../../server/lib/mirror_plugin_status';
import {
FrameworkInfo,
internalUser,
KibanaLegacyServer,
KibanaServerRequest,
KibanaUser,
RuntimeFrameworkInfo,
RuntimeKibanaUser,
XpackInfo,
} from './adapter_types';

export class BackendFrameworkAdapter {
Expand Down Expand Up @@ -64,7 +64,7 @@ export class BackendFrameworkAdapter {
this.server.expose(name, method);
}

public async getUser(request: KibanaServerRequest): Promise<KibanaUser | null> {
public async getUser(request: Request): Promise<KibanaUser | null> {
let user;
try {
user = await this.server.plugins.security.getUser(request);
Expand All @@ -86,7 +86,7 @@ export class BackendFrameworkAdapter {
return user;
}

private xpackInfoWasUpdatedHandler = (xpackInfo: XpackInfo) => {
private xpackInfoWasUpdatedHandler = (xpackInfo: XPackInfo) => {
let xpackInfoUnpacked: FrameworkInfo;

// If, for some reason, we cannot get the license information
Expand All @@ -102,12 +102,11 @@ export class BackendFrameworkAdapter {
version: get(this.server, 'plugins.kibana.status.plugin.version', 'unknown'),
},
license: {
type: xpackInfo.license.getType(),
type: xpackInfo.license.getType() || 'oss',
expired: !xpackInfo.license.isActive(),
expiry_date_in_millis:
xpackInfo.license.getExpiryDateInMillis() !== undefined
? xpackInfo.license.getExpiryDateInMillis()
: -1,
expiry_date_in_millis: (xpackInfo.license.getExpiryDateInMillis() !== undefined
? xpackInfo.license.getExpiryDateInMillis()
: -1) as number,
},
security: {
enabled: !!xpackInfo.feature('security') && xpackInfo.feature('security').isEnabled(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* 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.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { SavedObjectsService, SavedObjectsClient as SavedObjectsClientType } from 'src/core/server';
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';

export class SODatabaseAdapter {
private client: SavedObjectsClientType;
constructor(savedObjects: SavedObjectsService, elasticsearch: ElasticsearchPlugin) {
const { SavedObjectsClient, getSavedObjectsRepository } = savedObjects;
const { callWithInternalUser } = elasticsearch.getCluster('admin');
const internalRepository = getSavedObjectsRepository(callWithInternalUser);

this.client = new SavedObjectsClient(internalRepository);
}
}
6 changes: 4 additions & 2 deletions x-pack/legacy/plugins/ingest/server/libs/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import { PLUGIN } from '../../../common/constants';
import { CONFIG_PREFIX } from '../../../common/constants/plugin';
import { DatabaseKbnESPlugin } from '../adapters/es_database/adapter_types';
import { ESDatabaseAdapter } from '../adapters/es_database/default';
import { KibanaLegacyServer } from '../adapters/framework/adapter_types';
import { BackendFrameworkAdapter } from '../adapters/framework/default';
import { ServerLibs } from '../types';
import { BackendFrameworkLib } from './../framework';
import { ConfigurationLib } from '../configuration';
import { ConfigAdapter } from '../adapters/configurations/default';
import { SODatabaseAdapter } from '../adapters/so_database/default';
import { KibanaLegacyServer } from '../adapters/framework/adapter_types';

export function compose(server: KibanaLegacyServer): ServerLibs {
const framework = new BackendFrameworkLib(
new BackendFrameworkAdapter(camelCase(PLUGIN.ID), server, CONFIG_PREFIX)
);
const database = new ESDatabaseAdapter(server.plugins.elasticsearch as DatabaseKbnESPlugin);
const soDatabase = new SODatabaseAdapter(server.savedObjects, server.plugins.elasticsearch);

const configAdapter = new ConfigAdapter();
const configAdapter = new ConfigAdapter(soDatabase);
const configuration = new ConfigurationLib(configAdapter);

const libs: ServerLibs = {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/ingest/server/libs/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { KibanaServerRequest } from './adapters/framework/adapter_types';
import { Request } from 'src/legacy/server/kbn_server';
import { BackendFrameworkAdapter } from './adapters/framework/default';

export class BackendFrameworkLib {
Expand All @@ -21,7 +21,7 @@ export class BackendFrameworkLib {
public internalUser = this.adapter.internalUser;
constructor(private readonly adapter: BackendFrameworkAdapter) {}

public getCurrentUser(request: KibanaServerRequest) {
public getCurrentUser(request: Request) {
return this.adapter.getUser(request);
}
public getSetting(setting: 'defaultUserRoles'): string[];
Expand Down

0 comments on commit a8d5111

Please sign in to comment.