Don't expose Elasticsearch client as Observable (#53824) (#54228)

* expose ES clients without observables

* expose observable-less api to plugins

* update core api and mocks

* update plugins

* NP SO & legacy use updated API

* update SO tests

* update TSDocs

* update types

* update docs

* document createCluster analog in np

* typo
This commit is contained in:
Mikhail Shustov 2020-01-08 13:26:02 +03:00 committed by GitHub
parent d1df426889
commit 5aad2f6a8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 353 additions and 202 deletions

View file

@ -9,5 +9,5 @@ Search for objects
<b>Signature:</b>
```typescript
find: <T extends SavedObjectAttributes>(options: Pick<SavedObjectFindOptionsServer, "search" | "filter" | "type" | "page" | "fields" | "searchFields" | "defaultSearchOperator" | "hasReference" | "sortField" | "perPage">) => Promise<SavedObjectsFindResponsePublic<T>>;
find: <T extends SavedObjectAttributes>(options: Pick<SavedObjectFindOptionsServer, "search" | "filter" | "type" | "page" | "perPage" | "sortField" | "fields" | "searchFields" | "hasReference" | "defaultSearchOperator">) => Promise<SavedObjectsFindResponsePublic<T>>;
```

View file

@ -24,7 +24,7 @@ The constructor for this class is marked as internal. Third-party code should no
| [bulkGet](./kibana-plugin-public.savedobjectsclient.bulkget.md) | | <code>(objects?: {</code><br/><code> id: string;</code><br/><code> type: string;</code><br/><code> }[]) =&gt; Promise&lt;SavedObjectsBatchResponse&lt;SavedObjectAttributes&gt;&gt;</code> | Returns an array of objects by id |
| [create](./kibana-plugin-public.savedobjectsclient.create.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(type: string, attributes: T, options?: SavedObjectsCreateOptions) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Persists an object |
| [delete](./kibana-plugin-public.savedobjectsclient.delete.md) | | <code>(type: string, id: string) =&gt; Promise&lt;{}&gt;</code> | Deletes an object |
| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(options: Pick&lt;SavedObjectFindOptionsServer, &quot;search&quot; &#124; &quot;filter&quot; &#124; &quot;type&quot; &#124; &quot;page&quot; &#124; &quot;fields&quot; &#124; &quot;searchFields&quot; &#124; &quot;defaultSearchOperator&quot; &#124; &quot;hasReference&quot; &#124; &quot;sortField&quot; &#124; &quot;perPage&quot;&gt;) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T&gt;&gt;</code> | Search for objects |
| [find](./kibana-plugin-public.savedobjectsclient.find.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(options: Pick&lt;SavedObjectFindOptionsServer, &quot;search&quot; &#124; &quot;filter&quot; &#124; &quot;type&quot; &#124; &quot;page&quot; &#124; &quot;perPage&quot; &#124; &quot;sortField&quot; &#124; &quot;fields&quot; &#124; &quot;searchFields&quot; &#124; &quot;hasReference&quot; &#124; &quot;defaultSearchOperator&quot;&gt;) =&gt; Promise&lt;SavedObjectsFindResponsePublic&lt;T&gt;&gt;</code> | Search for objects |
| [get](./kibana-plugin-public.savedobjectsclient.get.md) | | <code>&lt;T extends SavedObjectAttributes&gt;(type: string, id: string) =&gt; Promise&lt;SimpleSavedObject&lt;T&gt;&gt;</code> | Fetches a single object |
## Methods

View file

@ -9,14 +9,14 @@ Creates an instance of [IScopedClusterClient](./kibana-plugin-server.iscopedclus
<b>Signature:</b>
```typescript
asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): IScopedClusterClient;
asScoped(request?: ScopeableRequest): IScopedClusterClient;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| request | <code>KibanaRequest &#124; LegacyRequest &#124; FakeRequest</code> | Request the <code>IScopedClusterClient</code> instance will be scoped to. Supports request optionality, Legacy.Request &amp; FakeRequest for BWC with LegacyPlatform |
| request | <code>ScopeableRequest</code> | Request the <code>IScopedClusterClient</code> instance will be scoped to. Supports request optionality, Legacy.Request &amp; FakeRequest for BWC with LegacyPlatform |
<b>Returns:</b>

View file

@ -4,7 +4,7 @@
## ClusterClient class
Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via `asScoped(...)`<!-- -->).
Represents an Elasticsearch cluster API client created by the platform. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via `asScoped(...)`<!-- -->).
See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->.

View file

@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ElasticsearchServiceSetup](./kibana-plugin-server.elasticsearchservicesetup.md) &gt; [adminClient](./kibana-plugin-server.elasticsearchservicesetup.adminclient.md)
## ElasticsearchServiceSetup.adminClient property
A client for the `admin` cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
<b>Signature:</b>
```typescript
readonly adminClient: IClusterClient;
```
## Example
```js
const client = core.elasticsearch.adminClient;
```

View file

@ -1,19 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ElasticsearchServiceSetup](./kibana-plugin-server.elasticsearchservicesetup.md) &gt; [adminClient$](./kibana-plugin-server.elasticsearchservicesetup.adminclient_.md)
## ElasticsearchServiceSetup.adminClient$ property
Observable of clients for the `admin` cluster. Observable emits when Elasticsearch config changes on the Kibana server. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
```js
const client = await elasticsearch.adminClient$.pipe(take(1)).toPromise();
```
<b>Signature:</b>
```typescript
readonly adminClient$: Observable<IClusterClient>;
```

View file

@ -9,7 +9,7 @@ Create application specific Elasticsearch cluster API client with customized con
<b>Signature:</b>
```typescript
readonly createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => IClusterClient;
readonly createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => ICustomClusterClient;
```
## Example

View file

@ -0,0 +1,22 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ElasticsearchServiceSetup](./kibana-plugin-server.elasticsearchservicesetup.md) &gt; [dataClient](./kibana-plugin-server.elasticsearchservicesetup.dataclient.md)
## ElasticsearchServiceSetup.dataClient property
A client for the `data` cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
<b>Signature:</b>
```typescript
readonly dataClient: IClusterClient;
```
## Example
```js
const client = core.elasticsearch.dataClient;
```

View file

@ -1,19 +0,0 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ElasticsearchServiceSetup](./kibana-plugin-server.elasticsearchservicesetup.md) &gt; [dataClient$](./kibana-plugin-server.elasticsearchservicesetup.dataclient_.md)
## ElasticsearchServiceSetup.dataClient$ property
Observable of clients for the `data` cluster. Observable emits when Elasticsearch config changes on the Kibana server. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
```js
const client = await elasticsearch.dataClient$.pipe(take(1)).toPromise();
```
<b>Signature:</b>
```typescript
readonly dataClient$: Observable<IClusterClient>;
```

View file

@ -15,17 +15,7 @@ export interface ElasticsearchServiceSetup
| Property | Type | Description |
| --- | --- | --- |
| [adminClient$](./kibana-plugin-server.elasticsearchservicesetup.adminclient_.md) | <code>Observable&lt;IClusterClient&gt;</code> | Observable of clients for the <code>admin</code> cluster. Observable emits when Elasticsearch config changes on the Kibana server. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
```js
const client = await elasticsearch.adminClient$.pipe(take(1)).toPromise();
```
|
| [createClient](./kibana-plugin-server.elasticsearchservicesetup.createclient.md) | <code>(type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; IClusterClient</code> | Create application specific Elasticsearch cluster API client with customized config. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->. |
| [dataClient$](./kibana-plugin-server.elasticsearchservicesetup.dataclient_.md) | <code>Observable&lt;IClusterClient&gt;</code> | Observable of clients for the <code>data</code> cluster. Observable emits when Elasticsearch config changes on the Kibana server. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->.
```js
const client = await elasticsearch.dataClient$.pipe(take(1)).toPromise();
```
|
| [adminClient](./kibana-plugin-server.elasticsearchservicesetup.adminclient.md) | <code>IClusterClient</code> | A client for the <code>admin</code> cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->. |
| [createClient](./kibana-plugin-server.elasticsearchservicesetup.createclient.md) | <code>(type: string, clientConfig?: Partial&lt;ElasticsearchClientConfig&gt;) =&gt; ICustomClusterClient</code> | Create application specific Elasticsearch cluster API client with customized config. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->. |
| [dataClient](./kibana-plugin-server.elasticsearchservicesetup.dataclient.md) | <code>IClusterClient</code> | A client for the <code>data</code> cluster. All Elasticsearch config value changes are processed under the hood. See [IClusterClient](./kibana-plugin-server.iclusterclient.md)<!-- -->. |

View file

@ -4,12 +4,12 @@
## IClusterClient type
Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via `asScoped(...)`<!-- -->).
Represents an Elasticsearch cluster API client created by the platform. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via `asScoped(...)`<!-- -->).
See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->.
<b>Signature:</b>
```typescript
export declare type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
export declare type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
```

View file

@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ICustomClusterClient](./kibana-plugin-server.icustomclusterclient.md)
## ICustomClusterClient type
Represents an Elasticsearch cluster API client created by a plugin.. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via `asScoped(...)`<!-- -->).
See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->.
<b>Signature:</b>
```typescript
export declare type ICustomClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
```

View file

@ -17,7 +17,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| Class | Description |
| --- | --- |
| [BasePath](./kibana-plugin-server.basepath.md) | Access or manipulate the Kibana base path |
| [ClusterClient](./kibana-plugin-server.clusterclient.md) | Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [ClusterClient](./kibana-plugin-server.clusterclient.md) | Represents an Elasticsearch cluster API client created by the platform. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [CspConfig](./kibana-plugin-server.cspconfig.md) | CSP configuration for use in Kibana. |
| [ElasticsearchErrorHelpers](./kibana-plugin-server.elasticsearcherrorhelpers.md) | Helpers for working with errors returned from the Elasticsearch service.Since the internal data of errors are subject to change, consumers of the Elasticsearch service should always use these helpers to classify errors instead of checking error internals such as <code>body.error.header[WWW-Authenticate]</code> |
| [KibanaRequest](./kibana-plugin-server.kibanarequest.md) | Kibana specific abstraction for an incoming request. |
@ -175,8 +175,9 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [Headers](./kibana-plugin-server.headers.md) | Http request headers to read. |
| [HttpResponsePayload](./kibana-plugin-server.httpresponsepayload.md) | Data send to the client as a response payload. |
| [IBasePath](./kibana-plugin-server.ibasepath.md) | Access or manipulate the Kibana base path[BasePath](./kibana-plugin-server.basepath.md) |
| [IClusterClient](./kibana-plugin-server.iclusterclient.md) | Represents an Elasticsearch cluster API client and allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [IClusterClient](./kibana-plugin-server.iclusterclient.md) | Represents an Elasticsearch cluster API client created by the platform. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [IContextProvider](./kibana-plugin-server.icontextprovider.md) | A function that returns a context value for a specific key of given context type. |
| [ICustomClusterClient](./kibana-plugin-server.icustomclusterclient.md) | Represents an Elasticsearch cluster API client created by a plugin.. It allows to call API on behalf of the internal Kibana user and the actual user that is derived from the request headers (via <code>asScoped(...)</code>).<!-- -->See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->. |
| [IsAuthenticated](./kibana-plugin-server.isauthenticated.md) | Return authentication status for a request. |
| [ISavedObjectsRepository](./kibana-plugin-server.isavedobjectsrepository.md) | See [SavedObjectsRepository](./kibana-plugin-server.savedobjectsrepository.md) |
| [IScopedClusterClient](./kibana-plugin-server.iscopedclusterclient.md) | Serves the same purpose as "normal" <code>ClusterClient</code> but exposes additional <code>callAsCurrentUser</code> method that doesn't use credentials of the Kibana internal user (as <code>callAsInternalUser</code> does) to request Elasticsearch API, but rather passes HTTP headers extracted from the current user request to the API.<!-- -->See [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.md)<!-- -->. |
@ -213,6 +214,7 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [SavedObjectsClientContract](./kibana-plugin-server.savedobjectsclientcontract.md) | Saved Objects is Kibana's data persisentence mechanism allowing plugins to use Elasticsearch for storing plugin state.<!-- -->\#\# SavedObjectsClient errors<!-- -->Since the SavedObjectsClient has its hands in everything we are a little paranoid about the way we present errors back to to application code. Ideally, all errors will be either:<!-- -->1. Caused by bad implementation (ie. undefined is not a function) and as such unpredictable 2. An error that has been classified and decorated appropriately by the decorators in [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md)<!-- -->Type 1 errors are inevitable, but since all expected/handle-able errors should be Type 2 the <code>isXYZError()</code> helpers exposed at <code>SavedObjectsErrorHelpers</code> should be used to understand and manage error responses from the <code>SavedObjectsClient</code>.<!-- -->Type 2 errors are decorated versions of the source error, so if the elasticsearch client threw an error it will be decorated based on its type. That means that rather than looking for <code>error.body.error.type</code> or doing substring checks on <code>error.body.error.reason</code>, just use the helpers to understand the meaning of the error:<!-- -->\`\`\`<!-- -->js if (SavedObjectsErrorHelpers.isNotFoundError(error)) { // handle 404 }<!-- -->if (SavedObjectsErrorHelpers.isNotAuthorizedError(error)) { // 401 handling should be automatic, but in case you wanted to know }<!-- -->// always rethrow the error unless you handle it throw error; \`\`\`<!-- -->\#\#\# 404s from missing index<!-- -->From the perspective of application code and APIs the SavedObjectsClient is a black box that persists objects. One of the internal details that users have no control over is that we use an elasticsearch index for persistance and that index might be missing.<!-- -->At the time of writing we are in the process of transitioning away from the operating assumption that the SavedObjects index is always available. Part of this transition is handling errors resulting from an index missing. These used to trigger a 500 error in most cases, and in others cause 404s with different error messages.<!-- -->From my (Spencer) perspective, a 404 from the SavedObjectsApi is a 404; The object the request/call was targeting could not be found. This is why \#14141 takes special care to ensure that 404 errors are generic and don't distinguish between index missing or document missing.<!-- -->\#\#\# 503s from missing index<!-- -->Unlike all other methods, create requests are supposed to succeed even when the Kibana index does not exist because it will be automatically created by elasticsearch. When that is not the case it is because Elasticsearch's <code>action.auto_create_index</code> setting prevents it from being created automatically so we throw a special 503 with the intention of informing the user that their Elasticsearch settings need to be updated.<!-- -->See [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) See [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) |
| [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md) | Describes the factory used to create instances of the Saved Objects Client. |
| [SavedObjectsClientWrapperFactory](./kibana-plugin-server.savedobjectsclientwrapperfactory.md) | Describes the factory used to create instances of Saved Objects Client Wrappers. |
| [ScopeableRequest](./kibana-plugin-server.scopeablerequest.md) | A user credentials container. It accommodates the necessary auth credentials to impersonate the current user.<!-- -->See [KibanaRequest](./kibana-plugin-server.kibanarequest.md)<!-- -->. |
| [SharedGlobalConfig](./kibana-plugin-server.sharedglobalconfig.md) | |
| [UiSettingsType](./kibana-plugin-server.uisettingstype.md) | UI element type to represent the settings. |

View file

@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [ScopeableRequest](./kibana-plugin-server.scopeablerequest.md)
## ScopeableRequest type
A user credentials container. It accommodates the necessary auth credentials to impersonate the current user.
See [KibanaRequest](./kibana-plugin-server.kibanarequest.md)<!-- -->.
<b>Signature:</b>
```typescript
export declare type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest;
```

View file

@ -1194,6 +1194,7 @@ In server code, `core` can be accessed from either `server.newPlatform` or `kbnS
| `request.getBasePath()` | [`core.http.basePath.get`](/docs/development/core/server/kibana-plugin-server.httpservicesetup.basepath.md) | |
| `server.plugins.elasticsearch.getCluster('data')` | [`context.elasticsearch.dataClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.getCluster('admin')` | [`context.elasticsearch.adminClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.createCluster(...)` | [`core.elasticsearch.createClient`](/docs/development/core/server/kibana-plugin-server.elasticsearchservicesetup.createclient.md) | |
| `server.savedObjects.setScopedSavedObjectsClientFactory` | [`core.savedObjects.setClientFactory`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.setclientfactory.md) | |
| `server.savedObjects.addScopedSavedObjectsClientWrapperFactory` | [`core.savedObjects.addClientWrapper`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.addclientwrapper.md) | |
| `server.savedObjects.getSavedObjectsRepository` | [`core.savedObjects.createInternalRepository`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.createinternalrepository.md) [`core.savedObjects.createScopedRepository`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.createscopedrepository.md) | |

View file

@ -89,15 +89,35 @@ export interface FakeRequest {
}
/**
* Represents an Elasticsearch cluster API client and allows to call API on behalf
* of the internal Kibana user and the actual user that is derived from the request
* headers (via `asScoped(...)`).
* Represents an Elasticsearch cluster API client created by the platform.
* It allows to call API on behalf of the internal Kibana user and
* the actual user that is derived from the request headers (via `asScoped(...)`).
*
* See {@link ClusterClient}.
*
* @public
*/
export type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
export type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
/**
* Represents an Elasticsearch cluster API client created by a plugin.
* It allows to call API on behalf of the internal Kibana user and
* the actual user that is derived from the request headers (via `asScoped(...)`).
*
* See {@link ClusterClient}.
*
* @public
*/
export type ICustomClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
/**
A user credentials container.
* It accommodates the necessary auth credentials to impersonate the current user.
*
* @public
* See {@link KibanaRequest}.
*/
export type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest;
/**
* {@inheritDoc IClusterClient}
@ -174,7 +194,7 @@ export class ClusterClient implements IClusterClient {
* @param request - Request the `IScopedClusterClient` instance will be scoped to.
* Supports request optionality, Legacy.Request & FakeRequest for BWC with LegacyPlatform
*/
public asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): IScopedClusterClient {
public asScoped(request?: ScopeableRequest): IScopedClusterClient {
// It'd have been quite expensive to create and configure client for every incoming
// request since it involves parsing of the config, reading of the SSL certificate and
// key files etc. Moreover scoped client needs two Elasticsearch JS clients at the same

View file

@ -18,33 +18,67 @@
*/
import { BehaviorSubject } from 'rxjs';
import { IClusterClient } from './cluster_client';
import { IClusterClient, ICustomClusterClient } from './cluster_client';
import { IScopedClusterClient } from './scoped_cluster_client';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService } from './elasticsearch_service';
import { InternalElasticsearchServiceSetup } from './types';
import { InternalElasticsearchServiceSetup, ElasticsearchServiceSetup } from './types';
const createScopedClusterClientMock = (): jest.Mocked<IScopedClusterClient> => ({
callAsInternalUser: jest.fn(),
callAsCurrentUser: jest.fn(),
});
const createClusterClientMock = (): jest.Mocked<IClusterClient> => ({
callAsInternalUser: jest.fn(),
asScoped: jest.fn().mockImplementation(createScopedClusterClientMock),
const createCustomClusterClientMock = (): jest.Mocked<ICustomClusterClient> => ({
...createClusterClientMock(),
close: jest.fn(),
});
function createClusterClientMock() {
const client: jest.Mocked<IClusterClient> = {
callAsInternalUser: jest.fn(),
asScoped: jest.fn(),
};
client.asScoped.mockReturnValue(createScopedClusterClientMock());
return client;
}
type MockedElasticSearchServiceSetup = jest.Mocked<
ElasticsearchServiceSetup & {
adminClient: jest.Mocked<IClusterClient>;
dataClient: jest.Mocked<IClusterClient>;
}
>;
const createSetupContractMock = () => {
const setupContract: jest.Mocked<InternalElasticsearchServiceSetup> = {
const setupContract: MockedElasticSearchServiceSetup = {
createClient: jest.fn(),
adminClient: createClusterClientMock(),
dataClient: createClusterClientMock(),
};
setupContract.createClient.mockReturnValue(createCustomClusterClientMock());
setupContract.adminClient.asScoped.mockReturnValue(createScopedClusterClientMock());
setupContract.dataClient.asScoped.mockReturnValue(createScopedClusterClientMock());
return setupContract;
};
type MockedInternalElasticSearchServiceSetup = jest.Mocked<
InternalElasticsearchServiceSetup & {
adminClient: jest.Mocked<IClusterClient>;
dataClient: jest.Mocked<IClusterClient>;
}
>;
const createInternalSetupContractMock = () => {
const setupContract: MockedInternalElasticSearchServiceSetup = {
...createSetupContractMock(),
legacy: {
config$: new BehaviorSubject({} as ElasticsearchConfig),
},
createClient: jest.fn().mockImplementation(createClusterClientMock),
adminClient$: new BehaviorSubject((createClusterClientMock() as unknown) as IClusterClient),
dataClient$: new BehaviorSubject((createClusterClientMock() as unknown) as IClusterClient),
adminClient$: new BehaviorSubject(createClusterClientMock()),
dataClient$: new BehaviorSubject(createClusterClientMock()),
};
setupContract.adminClient.asScoped.mockReturnValue(createScopedClusterClientMock());
setupContract.dataClient.asScoped.mockReturnValue(createScopedClusterClientMock());
return setupContract;
};
@ -55,14 +89,16 @@ const createMock = () => {
start: jest.fn(),
stop: jest.fn(),
};
mocked.setup.mockResolvedValue(createSetupContractMock());
mocked.setup.mockResolvedValue(createInternalSetupContractMock());
mocked.stop.mockResolvedValue();
return mocked;
};
export const elasticsearchServiceMock = {
create: createMock,
createSetupContract: createSetupContractMock,
createInternalSetup: createInternalSetupContractMock,
createSetup: createSetupContractMock,
createClusterClient: createClusterClientMock,
createCustomClusterClient: createCustomClusterClientMock,
createScopedClusterClient: createScopedClusterClientMock,
};

View file

@ -30,6 +30,7 @@ import { loggingServiceMock } from '../logging/logging_service.mock';
import { httpServiceMock } from '../http/http_service.mock';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchService } from './elasticsearch_service';
import { elasticsearchServiceMock } from './elasticsearch_service.mock';
let elasticsearchService: ElasticsearchService;
const configService = configServiceMock.create();
@ -69,6 +70,27 @@ describe('#setup', () => {
);
});
it('returns data and admin client as a part of the contract', async () => {
const mockAdminClusterClientInstance = elasticsearchServiceMock.createClusterClient();
const mockDataClusterClientInstance = elasticsearchServiceMock.createClusterClient();
MockClusterClient.mockImplementationOnce(
() => mockAdminClusterClientInstance
).mockImplementationOnce(() => mockDataClusterClientInstance);
const setupContract = await elasticsearchService.setup(deps);
const adminClient = setupContract.adminClient;
const dataClient = setupContract.dataClient;
expect(mockAdminClusterClientInstance.callAsInternalUser).toHaveBeenCalledTimes(0);
await adminClient.callAsInternalUser('any');
expect(mockAdminClusterClientInstance.callAsInternalUser).toHaveBeenCalledTimes(1);
expect(mockDataClusterClientInstance.callAsInternalUser).toHaveBeenCalledTimes(0);
await dataClient.callAsInternalUser('any');
expect(mockDataClusterClientInstance.callAsInternalUser).toHaveBeenCalledTimes(1);
});
it('returns data and admin client observables as a part of the contract', async () => {
const mockAdminClusterClientInstance = { close: jest.fn() };
const mockDataClusterClientInstance = { close: jest.fn() };

View file

@ -18,17 +18,18 @@
*/
import { ConnectableObservable, Observable, Subscription } from 'rxjs';
import { filter, first, map, publishReplay, switchMap } from 'rxjs/operators';
import { filter, first, map, publishReplay, switchMap, take } from 'rxjs/operators';
import { CoreService } from '../../types';
import { merge } from '../../utils';
import { CoreContext } from '../core_context';
import { Logger } from '../logging';
import { ClusterClient } from './cluster_client';
import { ClusterClient, ScopeableRequest } from './cluster_client';
import { ElasticsearchClientConfig } from './elasticsearch_client_config';
import { ElasticsearchConfig, ElasticsearchConfigType } from './elasticsearch_config';
import { InternalHttpServiceSetup, GetAuthHeaders } from '../http/';
import { InternalElasticsearchServiceSetup } from './types';
import { CallAPIOptions } from './api_types';
/** @internal */
interface CoreClusterClients {
@ -94,11 +95,67 @@ export class ElasticsearchService implements CoreService<InternalElasticsearchSe
const config = await this.config$.pipe(first()).toPromise();
const adminClient$ = clients$.pipe(map(clients => clients.adminClient));
const dataClient$ = clients$.pipe(map(clients => clients.dataClient));
const adminClient = {
async callAsInternalUser(
endpoint: string,
clientParams: Record<string, any> = {},
options?: CallAPIOptions
) {
const client = await adminClient$.pipe(take(1)).toPromise();
return await client.callAsInternalUser(endpoint, clientParams, options);
},
asScoped(request: ScopeableRequest) {
return {
callAsInternalUser: adminClient.callAsInternalUser,
async callAsCurrentUser(
endpoint: string,
clientParams: Record<string, any> = {},
options?: CallAPIOptions
) {
const client = await adminClient$.pipe(take(1)).toPromise();
return await client
.asScoped(request)
.callAsCurrentUser(endpoint, clientParams, options);
},
};
},
};
const dataClient = {
async callAsInternalUser(
endpoint: string,
clientParams: Record<string, any> = {},
options?: CallAPIOptions
) {
const client = await dataClient$.pipe(take(1)).toPromise();
return await client.callAsInternalUser(endpoint, clientParams, options);
},
asScoped(request: ScopeableRequest) {
return {
callAsInternalUser: dataClient.callAsInternalUser,
async callAsCurrentUser(
endpoint: string,
clientParams: Record<string, any> = {},
options?: CallAPIOptions
) {
const client = await dataClient$.pipe(take(1)).toPromise();
return await client
.asScoped(request)
.callAsCurrentUser(endpoint, clientParams, options);
},
};
},
};
return {
legacy: { config$: clients$.pipe(map(clients => clients.config)) },
adminClient$: clients$.pipe(map(clients => clients.adminClient)),
dataClient$: clients$.pipe(map(clients => clients.dataClient)),
adminClient$,
dataClient$,
adminClient,
dataClient,
createClient: (type: string, clientConfig: Partial<ElasticsearchClientConfig> = {}) => {
const finalConfig = merge({}, config, clientConfig);

View file

@ -18,7 +18,13 @@
*/
export { ElasticsearchService } from './elasticsearch_service';
export { IClusterClient, ClusterClient, FakeRequest } from './cluster_client';
export {
ClusterClient,
FakeRequest,
IClusterClient,
ICustomClusterClient,
ScopeableRequest,
} from './cluster_client';
export { IScopedClusterClient, ScopedClusterClient, Headers } from './scoped_cluster_client';
export { ElasticsearchClientConfig } from './elasticsearch_client_config';
export { config } from './elasticsearch_config';

View file

@ -20,7 +20,7 @@
import { Observable } from 'rxjs';
import { ElasticsearchConfig } from './elasticsearch_config';
import { ElasticsearchClientConfig } from './elasticsearch_client_config';
import { IClusterClient } from './cluster_client';
import { IClusterClient, ICustomClusterClient } from './cluster_client';
/**
* @public
@ -46,29 +46,29 @@ export interface ElasticsearchServiceSetup {
readonly createClient: (
type: string,
clientConfig?: Partial<ElasticsearchClientConfig>
) => IClusterClient;
) => ICustomClusterClient;
/**
* Observable of clients for the `admin` cluster. Observable emits when Elasticsearch config changes on the Kibana
* server. See {@link IClusterClient}.
* A client for the `admin` cluster. All Elasticsearch config value changes are processed under the hood.
* See {@link IClusterClient}.
*
* @exmaple
* @example
* ```js
* const client = await elasticsearch.adminClient$.pipe(take(1)).toPromise();
* const client = core.elasticsearch.adminClient;
* ```
*/
readonly adminClient$: Observable<IClusterClient>;
readonly adminClient: IClusterClient;
/**
* Observable of clients for the `data` cluster. Observable emits when Elasticsearch config changes on the Kibana
* server. See {@link IClusterClient}.
* A client for the `data` cluster. All Elasticsearch config value changes are processed under the hood.
* See {@link IClusterClient}.
*
* @exmaple
* @example
* ```js
* const client = await elasticsearch.dataClient$.pipe(take(1)).toPromise();
* const client = core.elasticsearch.dataClient;
* ```
*/
readonly dataClient$: Observable<IClusterClient>;
readonly dataClient: IClusterClient;
}
/** @internal */
@ -77,4 +77,7 @@ export interface InternalElasticsearchServiceSetup extends ElasticsearchServiceS
readonly legacy: {
readonly config$: Observable<ElasticsearchConfig>;
};
readonly adminClient$: Observable<IClusterClient>;
readonly dataClient$: Observable<IClusterClient>;
}

View file

@ -74,6 +74,7 @@ export { CspConfig, ICspConfig } from './csp';
export {
ClusterClient,
IClusterClient,
ICustomClusterClient,
Headers,
ScopedClusterClient,
IScopedClusterClient,
@ -83,6 +84,7 @@ export {
ElasticsearchServiceSetup,
APICaller,
FakeRequest,
ScopeableRequest,
} from './elasticsearch';
export * from './elasticsearch/api_types';
export {

View file

@ -249,8 +249,8 @@ export class LegacyService implements CoreService {
capabilities: setupDeps.core.capabilities,
context: setupDeps.core.context,
elasticsearch: {
adminClient$: setupDeps.core.elasticsearch.adminClient$,
dataClient$: setupDeps.core.elasticsearch.dataClient$,
adminClient: setupDeps.core.elasticsearch.adminClient,
dataClient: setupDeps.core.elasticsearch.dataClient,
createClient: setupDeps.core.elasticsearch.createClient,
},
http: {

View file

@ -107,7 +107,7 @@ function createCoreSetupMock() {
const mock: MockedKeys<CoreSetup> = {
capabilities: capabilitiesServiceMock.createSetupContract(),
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
http: httpMock,
savedObjects: savedObjectsServiceMock.createSetupContract(),
uiSettings: uiSettingsMock,
@ -131,7 +131,7 @@ function createInternalCoreSetupMock() {
const setupDeps: InternalCoreSetup = {
capabilities: capabilitiesServiceMock.createSetupContract(),
context: contextServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createInternalSetup(),
http: httpServiceMock.createSetupContract(),
uiSettings: uiSettingsServiceMock.createSetupContract(),
savedObjects: savedObjectsServiceMock.createSetupContract(),

View file

@ -145,8 +145,8 @@ export function createPluginSetupContext<TPlugin, TPluginDependencies>(
createContextContainer: deps.context.createContextContainer,
},
elasticsearch: {
adminClient$: deps.elasticsearch.adminClient$,
dataClient$: deps.elasticsearch.dataClient$,
adminClient: deps.elasticsearch.adminClient,
dataClient: deps.elasticsearch.dataClient,
createClient: deps.elasticsearch.createClient,
},
http: {

View file

@ -23,7 +23,6 @@ import { SavedObjectsService, SavedObjectsSetupDeps } from './saved_objects_serv
import { mockCoreContext } from '../core_context.mock';
// @ts-ignore Typescript doesn't know about the jest mock
import { KibanaMigrator, mockKibanaMigratorInstance } from './migrations/kibana/kibana_migrator';
import { of } from 'rxjs';
import * as legacyElasticsearch from 'elasticsearch';
import { Env } from '../config';
import { configServiceMock } from '../mocks';
@ -49,7 +48,7 @@ describe('SavedObjectsService', () => {
const soService = new SavedObjectsService(coreContext);
const coreSetup = ({
elasticsearch: { adminClient$: of(clusterClient) },
elasticsearch: { adminClient: clusterClient },
legacyPlugins: { uiExports: { savedObjectMappings: [] }, pluginExtendedConfig: {} },
} as unknown) as SavedObjectsSetupDeps;
@ -68,7 +67,7 @@ describe('SavedObjectsService', () => {
});
const soService = new SavedObjectsService(coreContext);
const coreSetup = ({
elasticsearch: { adminClient$: of({ callAsInternalUser: jest.fn() }) },
elasticsearch: { adminClient: { callAsInternalUser: jest.fn() } },
legacyPlugins: { uiExports: {}, pluginExtendedConfig: {} },
} as unknown) as SavedObjectsSetupDeps;
@ -82,7 +81,7 @@ describe('SavedObjectsService', () => {
const coreContext = mockCoreContext.create({ configService });
const soService = new SavedObjectsService(coreContext);
const coreSetup = ({
elasticsearch: { adminClient$: of({ callAsInternalUser: jest.fn() }) },
elasticsearch: { adminClient: { callAsInternalUser: jest.fn() } },
legacyPlugins: { uiExports: {}, pluginExtendedConfig: {} },
} as unknown) as SavedObjectsSetupDeps;
@ -96,7 +95,7 @@ describe('SavedObjectsService', () => {
const coreContext = mockCoreContext.create({ configService });
const soService = new SavedObjectsService(coreContext);
const coreSetup = ({
elasticsearch: { adminClient$: of({ callAsInternalUser: jest.fn() }) },
elasticsearch: { adminClient: { callAsInternalUser: jest.fn() } },
legacyPlugins: { uiExports: {}, pluginExtendedConfig: {} },
} as unknown) as SavedObjectsSetupDeps;

View file

@ -29,7 +29,7 @@ import {
import { KibanaMigrator, IKibanaMigrator } from './migrations';
import { CoreContext } from '../core_context';
import { LegacyServiceDiscoverPlugins } from '../legacy';
import { ElasticsearchServiceSetup, APICaller } from '../elasticsearch';
import { InternalElasticsearchServiceSetup, APICaller } from '../elasticsearch';
import { KibanaConfigType } from '../kibana_config';
import { migrationsRetryCallCluster } from '../elasticsearch/retry_call_cluster';
import { SavedObjectsConfigType } from './saved_objects_config';
@ -174,7 +174,7 @@ export interface InternalSavedObjectsServiceStart extends SavedObjectsServiceSta
/** @internal */
export interface SavedObjectsSetupDeps {
legacyPlugins: LegacyServiceDiscoverPlugins;
elasticsearch: ElasticsearchServiceSetup;
elasticsearch: InternalElasticsearchServiceSetup;
}
/** @internal */
@ -206,8 +206,6 @@ export class SavedObjectsService
const savedObjectSchemas = new SavedObjectsSchema(savedObjectsSchemasDefinition);
const adminClient = await setupDeps.elasticsearch.adminClient$.pipe(first()).toPromise();
const kibanaConfig = await this.coreContext.configService
.atPath<KibanaConfigType>('kibana')
.pipe(first())
@ -218,6 +216,8 @@ export class SavedObjectsService
.pipe(first())
.toPromise();
const adminClient = setupDeps.elasticsearch.adminClient;
const migrator = (this.migrator = new KibanaMigrator({
savedObjectSchemas,
savedObjectMappings,

View file

@ -66,12 +66,6 @@ export type ISavedObjectsClientProvider<T = unknown> = Pick<
* Provider for the Scoped Saved Objects Client.
*
* @internal
*
* @internalRemarks Because `getClient` is synchronous the Client Provider does
* not support creating factories that react to new ES clients emitted from
* elasticsearch.adminClient$. The Client Provider therefore doesn't support
* configuration changes to the Elasticsearch client. TODO: revisit once we've
* closed https://github.com/elastic/kibana/pull/45796
*/
export class SavedObjectsClientProvider<Request = unknown> {
private readonly _wrapperFactories = new PriorityCollection<{

View file

@ -498,7 +498,7 @@ export type CapabilitiesSwitcher = (request: KibanaRequest, uiCapabilities: Capa
// @public
export class ClusterClient implements IClusterClient {
constructor(config: ElasticsearchClientConfig, log: Logger, getAuthHeaders?: GetAuthHeaders);
asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): IScopedClusterClient;
asScoped(request?: ScopeableRequest): IScopedClusterClient;
callAsInternalUser: APICaller;
close(): void;
}
@ -669,9 +669,9 @@ export class ElasticsearchErrorHelpers {
// @public (undocumented)
export interface ElasticsearchServiceSetup {
readonly adminClient$: Observable<IClusterClient>;
readonly createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => IClusterClient;
readonly dataClient$: Observable<IClusterClient>;
readonly adminClient: IClusterClient;
readonly createClient: (type: string, clientConfig?: Partial<ElasticsearchClientConfig>) => ICustomClusterClient;
readonly dataClient: IClusterClient;
}
// @public (undocumented)
@ -752,7 +752,7 @@ export interface HttpServiceStart {
export type IBasePath = Pick<BasePath, keyof BasePath>;
// @public
export type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
export type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'asScoped'>;
// @public
export interface IContextContainer<THandler extends HandlerFunction<any>> {
@ -771,6 +771,9 @@ export interface ICspConfig {
readonly warnLegacyBrowsers: boolean;
}
// @public
export type ICustomClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
// @public
export interface IKibanaResponse<T extends HttpResponsePayload | ResponseError = any> {
// (undocumented)
@ -1878,6 +1881,9 @@ export interface SavedObjectsUpdateResponse<T extends SavedObjectAttributes = an
references: SavedObjectReference[] | undefined;
}
// @public
export type ScopeableRequest = KibanaRequest | LegacyRequest | FakeRequest;
// @public
export class ScopedClusterClient implements IScopedClusterClient {
constructor(internalAPICaller: APICaller, scopedAPICaller: APICaller, headers?: Headers | undefined);

View file

@ -216,6 +216,7 @@ export class Server {
coreId,
'core',
async (context, req, res): Promise<RequestHandlerContext['core']> => {
// it consumes elasticsearch observables to provide the same client throughout the context lifetime.
const adminClient = await coreSetup.elasticsearch.adminClient$.pipe(take(1)).toPromise();
const dataClient = await coreSetup.elasticsearch.dataClient$.pipe(take(1)).toPromise();
const savedObjectsClient = coreSetup.savedObjects.getScopedClient(req);
@ -226,8 +227,6 @@ export class Server {
render: rendering.render.bind(rendering, req, uiSettingsClient),
},
savedObjects: {
// Note: the client provider doesn't support new ES clients
// emitted from adminClient$
client: savedObjectsClient,
},
elasticsearch: {

View file

@ -16,9 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { combineLatest } from 'rxjs';
import { first, map } from 'rxjs/operators';
import { first } from 'rxjs/operators';
import healthCheck from './lib/health_check';
import { Cluster } from './lib/cluster';
import { createProxy } from './lib/create_proxy';
@ -36,19 +34,12 @@ export default function(kibana) {
// All methods that ES plugin exposes are synchronous so we should get the first
// value from all observables here to be able to synchronously return and create
// cluster clients afterwards.
const [esConfig, adminCluster, dataCluster] = await combineLatest(
server.newPlatform.__internals.elasticsearch.legacy.config$,
server.newPlatform.setup.core.elasticsearch.adminClient$,
server.newPlatform.setup.core.elasticsearch.dataClient$
)
.pipe(
first(),
map(([config, adminClusterClient, dataClusterClient]) => [
config,
new Cluster(adminClusterClient),
new Cluster(dataClusterClient),
])
)
const { adminClient, dataClient } = server.newPlatform.setup.core.elasticsearch;
const adminCluster = new Cluster(adminClient);
const dataCluster = new Cluster(dataClient);
const esConfig = await server.newPlatform.__internals.elasticsearch.legacy.config$
.pipe(first())
.toPromise();
defaultVars = {

View file

@ -106,12 +106,7 @@ describe('Saved Objects Mixin', () => {
newPlatform: {
__internals: {
elasticsearch: {
adminClient$: {
pipe: jest.fn().mockImplementation(() => ({
toPromise: () =>
Promise.resolve({ adminClient: { callAsInternalUser: mockCallCluster } }),
})),
},
adminClient: { callAsInternalUser: mockCallCluster },
},
},
},

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { map, mergeMap } from 'rxjs/operators';
import { map } from 'rxjs/operators';
import { schema, TypeOf } from '@kbn/config-schema';
import {
@ -108,9 +108,7 @@ class Plugin {
return `Some exposed data derived from config: ${configValue.secret}`;
})
),
pingElasticsearch$: core.elasticsearch.adminClient$.pipe(
mergeMap(client => client.callAsInternalUser('ping'))
),
pingElasticsearch: () => core.elasticsearch.adminClient.callAsInternalUser('ping'),
};
}

View file

@ -69,7 +69,7 @@ export class Plugin {
plugins: ActionsPluginsSetup
): Promise<PluginSetupContract> {
const config = await this.config$.pipe(first()).toPromise();
this.adminClient = await core.elasticsearch.adminClient$.pipe(first()).toPromise();
this.adminClient = core.elasticsearch.adminClient;
this.defaultKibanaIndex = (await this.kibana$.pipe(first()).toPromise()).index;
this.licenseState = new LicenseState(plugins.licensing.license$);

View file

@ -5,7 +5,7 @@
*/
import Hapi from 'hapi';
import { first } from 'rxjs/operators';
import { Services } from './types';
import { AlertsClient } from './alerts_client';
import { AlertTypeRegistry } from './alert_type_registry';
@ -62,7 +62,7 @@ export class Plugin {
core: AlertingCoreSetup,
plugins: AlertingPluginsSetup
): Promise<PluginSetupContract> {
this.adminClient = await core.elasticsearch.adminClient$.pipe(first()).toPromise();
this.adminClient = core.elasticsearch.adminClient;
this.licenseState = new LicenseState(plugins.licensing.license$);

View file

@ -3,7 +3,6 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { first } from 'rxjs/operators';
import { Plugin, CoreSetup } from 'src/core/server';
import { i18n } from '@kbn/i18n';
import { PLUGIN } from '../../common/constants';
@ -23,7 +22,7 @@ export class WatcherServerPlugin implements Plugin<void, void, any, any> {
{ http, elasticsearch: elasticsearchService }: CoreSetup,
{ __LEGACY: serverShim }: { __LEGACY: ServerShim }
) {
const elasticsearch = await elasticsearchService.adminClient$.pipe(first()).toPromise();
const elasticsearch = await elasticsearchService.adminClient;
const router = http.createRouter();
const routeDependencies: RouteDependencies = {
elasticsearch,

View file

@ -58,16 +58,14 @@ export class APMPlugin implements Plugin<APMPluginContract> {
});
await new Promise(resolve => {
combineLatest(mergedConfig$, core.elasticsearch.dataClient$).subscribe(
async ([config, dataClient]) => {
this.currentConfig = config;
await createApmAgentConfigurationIndex({
esClient: dataClient,
config,
});
resolve();
}
);
mergedConfig$.subscribe(async config => {
this.currentConfig = config;
await createApmAgentConfigurationIndex({
esClient: core.elasticsearch.dataClient,
config,
});
resolve();
});
});
return {

View file

@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { BehaviorSubject } from 'rxjs';
import { take, toArray } from 'rxjs/operators';
import moment from 'moment';
import { LicenseType } from '../common/types';
@ -53,7 +52,7 @@ describe('licensing plugin', () => {
features: {},
});
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const license = await license$.pipe(take(1)).toPromise();
@ -71,7 +70,7 @@ describe('licensing plugin', () => {
})
);
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const [first, second, third] = await license$.pipe(take(3), toArray()).toPromise();
@ -85,7 +84,7 @@ describe('licensing plugin', () => {
const dataClient = elasticsearchServiceMock.createClusterClient();
dataClient.callAsInternalUser.mockRejectedValue(new Error('test'));
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const license = await license$.pipe(take(1)).toPromise();
@ -99,7 +98,7 @@ describe('licensing plugin', () => {
error.status = 400;
dataClient.callAsInternalUser.mockRejectedValue(error);
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const license = await license$.pipe(take(1)).toPromise();
@ -119,7 +118,7 @@ describe('licensing plugin', () => {
.mockResolvedValue({ license: buildRawLicense(), features: {} });
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const [first, second, third] = await license$.pipe(take(3), toArray()).toPromise();
@ -137,7 +136,7 @@ describe('licensing plugin', () => {
});
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
await plugin.setup(coreSetup);
await flushPromises();
@ -152,7 +151,7 @@ describe('licensing plugin', () => {
});
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
await plugin.setup(coreSetup);
await flushPromises();
@ -180,7 +179,7 @@ describe('licensing plugin', () => {
);
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { license$ } = await plugin.setup(coreSetup);
const [first, second, third] = await license$.pipe(take(3), toArray()).toPromise();
@ -209,7 +208,7 @@ describe('licensing plugin', () => {
features: {},
});
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { refresh } = await plugin.setup(coreSetup);
expect(dataClient.callAsInternalUser).toHaveBeenCalledTimes(0);
@ -242,7 +241,7 @@ describe('licensing plugin', () => {
features: {},
});
const coreSetup = coreMock.createSetup();
coreSetup.elasticsearch.dataClient$ = new BehaviorSubject(dataClient);
coreSetup.elasticsearch.dataClient = dataClient;
const { createLicensePoller, license$ } = await plugin.setup(coreSetup);
const customClient = elasticsearchServiceMock.createClusterClient();

View file

@ -92,7 +92,7 @@ export class LicensingPlugin implements Plugin<LicensingPluginSetup> {
this.logger.debug('Setting up Licensing plugin');
const config = await this.config$.pipe(take(1)).toPromise();
const pollingFrequency = config.api_polling_frequency;
const dataClient = await core.elasticsearch.dataClient$.pipe(take(1)).toPromise();
const dataClient = await core.elasticsearch.dataClient;
const { refresh, license$ } = this.createLicensePoller(
dataClient,

View file

@ -6,7 +6,7 @@
import { of } from 'rxjs';
import { ByteSizeValue } from '@kbn/config-schema';
import { IClusterClient, CoreSetup } from '../../../../src/core/server';
import { ICustomClusterClient, CoreSetup } from '../../../../src/core/server';
import { elasticsearchClientPlugin } from './elasticsearch_client_plugin';
import { Plugin, PluginSetupDependencies } from './plugin';
@ -15,7 +15,7 @@ import { coreMock, elasticsearchServiceMock } from '../../../../src/core/server/
describe('Security Plugin', () => {
let plugin: Plugin;
let mockCoreSetup: MockedKeys<CoreSetup>;
let mockClusterClient: jest.Mocked<IClusterClient>;
let mockClusterClient: jest.Mocked<ICustomClusterClient>;
let mockDependencies: PluginSetupDependencies;
beforeEach(() => {
plugin = new Plugin(
@ -35,9 +35,9 @@ describe('Security Plugin', () => {
mockCoreSetup = coreMock.createSetup();
mockCoreSetup.http.isTlsEnabled = true;
mockClusterClient = elasticsearchServiceMock.createClusterClient();
mockClusterClient = elasticsearchServiceMock.createCustomClusterClient();
mockCoreSetup.elasticsearch.createClient.mockReturnValue(
(mockClusterClient as unknown) as jest.Mocked<IClusterClient>
(mockClusterClient as unknown) as jest.Mocked<ICustomClusterClient>
);
mockDependencies = { licensing: { license$: of({}) } } as PluginSetupDependencies;

View file

@ -7,7 +7,7 @@
import { combineLatest } from 'rxjs';
import { first } from 'rxjs/operators';
import {
IClusterClient,
ICustomClusterClient,
CoreSetup,
KibanaRequest,
Logger,
@ -86,7 +86,7 @@ export interface PluginSetupDependencies {
*/
export class Plugin {
private readonly logger: Logger;
private clusterClient?: IClusterClient;
private clusterClient?: ICustomClusterClient;
private spacesService?: SpacesService | symbol = Symbol('not accessed');
private securityLicenseService?: SecurityLicenseService;

View file

@ -9,7 +9,7 @@ import { SavedObjectsLegacyService, IClusterClient } from 'src/core/server';
import { DEFAULT_SPACE_ID } from '../../common/constants';
interface Deps {
esClient: Pick<IClusterClient, 'callAsInternalUser'>;
esClient: IClusterClient;
savedObjects: SavedObjectsLegacyService;
}

View file

@ -168,7 +168,7 @@ describe('onPostAuthInterceptor', () => {
const spacesService = await service.setup({
http: (http as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -50,7 +50,7 @@ describe('createSpacesTutorialContextFactory', () => {
it('should create context with the current space id for the default space', async () => {
const spacesService = await service.setup({
http: coreMock.createSetup().http,
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -5,7 +5,6 @@
*/
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { HomeServerPluginSetup } from 'src/plugins/home/server';
import {
@ -168,9 +167,8 @@ export class Plugin {
);
},
createDefaultSpace: async () => {
const esClient = await core.elasticsearch.adminClient$.pipe(take(1)).toPromise();
return createDefaultSpace({
esClient,
return await createDefaultSpace({
esClient: core.elasticsearch.adminClient,
savedObjects: this.getLegacyAPI().savedObjects,
});
},

View file

@ -43,7 +43,7 @@ describe('copy to space', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -44,7 +44,7 @@ describe('Spaces Public API', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -42,7 +42,7 @@ describe('GET space', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -42,7 +42,7 @@ describe('GET /spaces/space', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -43,7 +43,7 @@ describe('Spaces Public API', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -44,7 +44,7 @@ describe('PUT /api/spaces/space', () => {
const service = new SpacesService(log, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -22,7 +22,7 @@ describe('GET /internal/spaces/_active_space', () => {
const service = new SpacesService(null as any, () => legacyAPI);
const spacesService = await service.setup({
http: (httpService as unknown) as CoreSetup['http'],
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
authorization: null,
getSpacesAuditLogger: () => ({} as SpacesAuditLogger),
config$: Rx.of(spacesConfig),

View file

@ -74,7 +74,7 @@ const createService = async (serverBasePath: string = '') => {
const spacesServiceSetup = await spacesService.setup({
http: httpSetup,
elasticsearch: elasticsearchServiceMock.createSetupContract(),
elasticsearch: elasticsearchServiceMock.createSetup(),
config$: Rx.of(spacesConfig),
authorization: securityMock.createSetup().authz,
getSpacesAuditLogger: () => new SpacesAuditLogger({}),

View file

@ -5,7 +5,7 @@
*/
import { map, take } from 'rxjs/operators';
import { Observable, Subscription, combineLatest } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import { Legacy } from 'kibana';
import { Logger, KibanaRequest, CoreSetup } from '../../../../../src/core/server';
import { PluginSetupContract as SecurityPluginSetup } from '../../../security/server';
@ -69,15 +69,15 @@ export class SpacesService {
};
const getScopedClient = async (request: KibanaRequest) => {
return combineLatest(elasticsearch.adminClient$, config$)
return config$
.pipe(
map(([clusterClient, config]) => {
map(config => {
const internalRepository = this.getLegacyAPI().savedObjects.getSavedObjectsRepository(
clusterClient.callAsInternalUser,
elasticsearch.adminClient.callAsInternalUser,
['space']
);
const callCluster = clusterClient.asScoped(request).callAsCurrentUser;
const callCluster = elasticsearch.adminClient.asScoped(request).callAsCurrentUser;
const callWithRequestRepository = this.getLegacyAPI().savedObjects.getSavedObjectsRepository(
callCluster,