Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Oct 3, 2019
1 parent 70a1e00 commit 78d2194
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Creates an instance of `ScopedClusterClient` based on the configuration the curr
<b>Signature:</b>

```typescript
asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): ScopedClusterClient;
asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): IScopedClusterClient;
```

## Parameters
Expand All @@ -20,5 +20,5 @@ asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): ScopedClusterCl

<b>Returns:</b>

`ScopedClusterClient`
`IScopedClusterClient`

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

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(...)`<!-- -->).

See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->.

<b>Signature:</b>

```typescript
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,14 @@

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [IClusterClient](./kibana-plugin-server.iclusterclient.md)

## IClusterClient interface
## 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(...)`<!-- -->).

See [ClusterClient](./kibana-plugin-server.clusterclient.md)<!-- -->.

<b>Signature:</b>

```typescript
export interface IClusterClient
export declare type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [callAsInternalUser](./kibana-plugin-server.iclusterclient.callasinternaluser.md) | <code>APICaller</code> | Calls specified endpoint with provided clientParams on behalf of the Kibana internal user. |

## Methods

| Method | Description |
| --- | --- |
| [asScoped(request)](./kibana-plugin-server.iclusterclient.asscoped.md) | Creates an instance of <code>ScopedClusterClient</code> based on the configuration the current cluster client that exposes additional <code>callAsCurrentUser</code> method scoped to the provided req. Consumers shouldn't worry about closing scoped client instances, these will be automatically closed as soon as the original cluster client isn't needed anymore and closed. |
| [close()](./kibana-plugin-server.iclusterclient.close.md) | Closes the cluster client. After that client cannot be used and one should create a new client instance to be able to interact with Elasticsearch API. |

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

[Home](./index.md) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [IScopedClusterClient](./kibana-plugin-server.iscopedclusterclient.md)

## IScopedClusterClient interface
## IScopedClusterClient type

Serves the same purpose as "normal" `ClusterClient` but exposes additional `callAsCurrentUser` method that doesn't use credentials of the Kibana internal user (as `callAsInternalUser` does) to request Elasticsearch API, but rather passes HTTP headers extracted from the current user request to the API
Serves the same purpose as "normal" `ClusterClient` but exposes additional `callAsCurrentUser` method that doesn't use credentials of the Kibana internal user (as `callAsInternalUser` 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)<!-- -->.

<b>Signature:</b>

```typescript
export interface IScopedClusterClient
export declare type IScopedClusterClient = Pick<ScopedClusterClient, 'callAsCurrentUser' | 'callAsInternalUser'>;
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [callAsCurrentUser](./kibana-plugin-server.iscopedclusterclient.callascurrentuser.md) | <code>APICaller</code> | Calls specified <code>endpoint</code> with provided <code>clientParams</code> on behalf of the user initiated request to the Kibana server (via HTTP request headers). |
| [callAsInternalUser](./kibana-plugin-server.iscopedclusterclient.callasinternaluser.md) | <code>APICaller</code> | Calls specified <code>endpoint</code> with provided <code>clientParams</code> on behalf of the Kibana internal user. |

8 changes: 4 additions & 4 deletions docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ 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>). |
| [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)<!-- -->. |
| [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. |
| [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | |
| [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.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 |
| [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.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)<!-- -->. |

## Enumerations

Expand Down Expand Up @@ -54,12 +54,10 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [HttpResponseOptions](./kibana-plugin-server.httpresponseoptions.md) | HTTP response parameters |
| [HttpServerSetup](./kibana-plugin-server.httpserversetup.md) | Kibana HTTP Service provides own abstraction for work with HTTP stack. Plugins don't have direct access to <code>hapi</code> server and its primitives anymore. Moreover, plugins shouldn't rely on the fact that HTTP Service uses one or another library under the hood. This gives the platform flexibility to upgrade or changing our internal HTTP stack without breaking plugins. If the HTTP Service lacks functionality you need, we are happy to discuss and support your needs. |
| [HttpServiceStart](./kibana-plugin-server.httpservicestart.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>). |
| [IContextContainer](./kibana-plugin-server.icontextcontainer.md) | An object that handles registration of context providers and configuring handlers with context. |
| [IKibanaSocket](./kibana-plugin-server.ikibanasocket.md) | A tiny abstraction for TCP socket. |
| [IndexSettingsDeprecationInfo](./kibana-plugin-server.indexsettingsdeprecationinfo.md) | |
| [IRouter](./kibana-plugin-server.irouter.md) | Registers route handlers for specified resource path and method. |
| [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 |
| [KibanaRequestRoute](./kibana-plugin-server.kibanarequestroute.md) | Request specific route information exposed to a handler. |
| [LegacyRequest](./kibana-plugin-server.legacyrequest.md) | |
| [LegacyServiceSetupDeps](./kibana-plugin-server.legacyservicesetupdeps.md) | |
Expand Down Expand Up @@ -131,8 +129,10 @@ The plugin integrates with the core system via lifecycle events: `setup`<!-- -->
| [HttpResponsePayload](./kibana-plugin-server.httpresponsepayload.md) | Data send to the client as a response payload. |
| [HttpServiceSetup](./kibana-plugin-server.httpservicesetup.md) | |
| [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)<!-- -->. |
| [IContextProvider](./kibana-plugin-server.icontextprovider.md) | A function that returns a context value for a specific key of given context type. |
| [IsAuthenticated](./kibana-plugin-server.isauthenticated.md) | Return authentication status for a request. |
| [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)<!-- -->. |
| [KibanaResponseFactory](./kibana-plugin-server.kibanaresponsefactory.md) | Creates an object containing request response payload, HTTP headers, error details, and other data transmitted to the client. |
| [KnownHeaders](./kibana-plugin-server.knownheaders.md) | Set of well-known HTTP headers. |
| [LifecycleResponseFactory](./kibana-plugin-server.lifecycleresponsefactory.md) | Creates an object containing redirection or error response with error details, HTTP headers, and other data transmitted to the client. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

## ScopedClusterClient class

Serves the same purpose as "normal" `ClusterClient` but exposes additional `callAsCurrentUser` method that doesn't use credentials of the Kibana internal user (as `callAsInternalUser` does) to request Elasticsearch API, but rather passes HTTP headers extracted from the current user request to the API
Serves the same purpose as "normal" `ClusterClient` but exposes additional `callAsCurrentUser` method that doesn't use credentials of the Kibana internal user (as `callAsInternalUser` 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)<!-- -->.

<b>Signature:</b>

Expand Down
33 changes: 16 additions & 17 deletions src/core/server/elasticsearch/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,19 @@ export interface FakeRequest {
headers: Headers;
}

/**
* {@inheritdoc ClusterClient}
* @public
*/
export interface IClusterClient {
/** {@inheritdoc ClusterClient.callAsInternalUser} */
callAsInternalUser: APICaller;
/** {@inheritdoc ClusterClient.close} */
close(): void;
/** {@inheritdoc ClusterClient.asScoped} */
asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): IScopedClusterClient;
}

/**
* 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(...)`).
*
* See {@link ClusterClient}.
*
* @public
*/
export type IClusterClient = Pick<ClusterClient, 'callAsInternalUser' | 'close' | 'asScoped'>;

/**
* {@inheritDoc IClusterClient}
* @public
*/
export class ClusterClient implements IClusterClient {
Expand Down Expand Up @@ -176,7 +171,7 @@ export class ClusterClient implements IClusterClient {
* @param request - Request the `ScopedClusterClient` instance will be scoped to.
* Supports request optionality, Legacy.Request & FakeRequest for BWC with LegacyPlatform
*/
public asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest) {
public asScoped(request?: KibanaRequest | LegacyRequest | FakeRequest): 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
Expand All @@ -194,7 +189,7 @@ export class ClusterClient implements IClusterClient {

return new ScopedClusterClient(
this.callAsInternalUser,
this.callAsCurrentUser as APICaller,
this.callAsCurrentUser,
filterHeaders(this.getHeaders(request), this.config.requestHeadersWhitelist)
);
}
Expand All @@ -206,14 +201,18 @@ export class ClusterClient implements IClusterClient {
* @param clientParams - A dictionary of parameters that will be passed directly to the Elasticsearch JS client.
* @param options - Options that affect the way we call the API and process the result.
*/
private callAsCurrentUser = async (
private callAsCurrentUser: APICaller = async (
endpoint: string,
clientParams: Record<string, any> = {},
options?: CallAPIOptions
) => {
this.assertIsNotClosed();

return await callAPI(this.scopedClient!, endpoint, clientParams, options);
return await (callAPI.bind(null, this.scopedClient!) as APICaller)(
endpoint,
clientParams,
options
);
};

private assertIsNotClosed() {
Expand Down
28 changes: 12 additions & 16 deletions src/core/server/elasticsearch/scoped_cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@ import { APICaller, CallAPIOptions } from './api_types';
/** @public */
export { Headers };

/**
* {@inheritDoc ScopedClusterClient}
* @public
*/
export interface IScopedClusterClient {
/**
* {@inheritdoc ScopedClusterClient.callAsInternalUser}
*/
callAsInternalUser: APICaller;
/**
* {@inheritdoc ScopedClusterClient.callAsCurrentUser}
*/
callAsCurrentUser: APICaller;
}

/**
* Serves the same purpose as "normal" `ClusterClient` but exposes additional
* `callAsCurrentUser` method that doesn't use credentials of the Kibana internal
* user (as `callAsInternalUser` does) to request Elasticsearch API, but rather
* passes HTTP headers extracted from the current user request to the API
* passes HTTP headers extracted from the current user request to the API.
*
* See {@link ScopedClusterClient}.
*
* @public
*/
export type IScopedClusterClient = Pick<
ScopedClusterClient,
'callAsCurrentUser' | 'callAsInternalUser'
>;

/**
* {@inheritDoc IScopedClusterClient}
* @public
*/
export class ScopedClusterClient implements IScopedClusterClient {
Expand Down
Loading

0 comments on commit 78d2194

Please sign in to comment.