Skip to content

Commit

Permalink
/s/KIBANA_HEADERS/DEFAULT_HEADERS and a deepFreeze
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Oct 5, 2020
1 parent 8f10124 commit b11e5ce
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/core/server/elasticsearch/client/client_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { duration } from 'moment';
import { ElasticsearchClientConfig, parseClientOptions } from './client_config';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';

const createConfig = (
parts: Partial<ElasticsearchClientConfig> = {}
Expand All @@ -43,7 +43,7 @@ describe('parseClientOptions', () => {
expect(parseClientOptions(config, false)).toEqual(
expect.objectContaining({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
},
})
);
Expand All @@ -61,7 +61,7 @@ describe('parseClientOptions', () => {
expect(parseClientOptions(config, false)).toEqual(
expect.objectContaining({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
foo: 'bar',
hello: 'dolly',
},
Expand All @@ -71,7 +71,7 @@ describe('parseClientOptions', () => {

it('`customHeaders` take precedence to default kibana headers', () => {
const customHeader = {
[Object.keys(KIBANA_HEADERS)[0]]: 'foo',
[Object.keys(DEFAULT_HEADERS)[0]]: 'foo',
};
const config = createConfig({
customHeaders: {
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/elasticsearch/client/client_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { URL } from 'url';
import { Duration } from 'moment';
import { ClientOptions, NodeOptions } from '@elastic/elasticsearch';
import { ElasticsearchConfig } from '../elasticsearch_config';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';

/**
* Configuration options to be used to create a {@link IClusterClient | cluster client} using the
Expand Down Expand Up @@ -63,7 +63,7 @@ export function parseClientOptions(
sniffOnStart: config.sniffOnStart,
sniffOnConnectionFault: config.sniffOnConnectionFault,
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
...config.customHeaders,
},
};
Expand Down
26 changes: 13 additions & 13 deletions src/core/server/elasticsearch/client/cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { GetAuthHeaders } from '../../http';
import { elasticsearchClientMock } from './mocks';
import { ClusterClient } from './cluster_client';
import { ElasticsearchClientConfig } from './client_config';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';

const createConfig = (
parts: Partial<ElasticsearchClientConfig> = {}
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...KIBANA_HEADERS, foo: 'bar', 'x-opaque-id': expect.any(String) },
headers: { ...DEFAULT_HEADERS, foo: 'bar', 'x-opaque-id': expect.any(String) },
});
});

Expand All @@ -148,7 +148,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
headers: { ...DEFAULT_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
});
});

Expand All @@ -172,7 +172,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
headers: { ...DEFAULT_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
});
});

Expand All @@ -194,7 +194,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
foo: 'bar',
hello: 'dolly',
'x-opaque-id': expect.any(String),
Expand All @@ -216,7 +216,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
'x-opaque-id': 'my-fake-id',
},
});
Expand All @@ -242,7 +242,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
foo: 'auth',
hello: 'dolly',
'x-opaque-id': expect.any(String),
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
foo: 'request',
hello: 'dolly',
'x-opaque-id': expect.any(String),
Expand All @@ -279,7 +279,7 @@ describe('ClusterClient', () => {
});

it('respect the precedence of config headers over default headers', () => {
const headerKey = Object.keys(KIBANA_HEADERS)[0];
const headerKey = Object.keys(DEFAULT_HEADERS)[0];
const config = createConfig({
customHeaders: {
[headerKey]: 'foo',
Expand All @@ -302,7 +302,7 @@ describe('ClusterClient', () => {
});

it('respect the precedence of request headers over default headers', () => {
const headerKey = Object.keys(KIBANA_HEADERS)[0];
const headerKey = Object.keys(DEFAULT_HEADERS)[0];
const config = createConfig({
requestHeadersWhitelist: [headerKey],
});
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
'x-opaque-id': 'from request',
},
});
Expand All @@ -367,7 +367,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...KIBANA_HEADERS, authorization: 'auth' },
headers: { ...DEFAULT_HEADERS, authorization: 'auth' },
});
});

Expand All @@ -391,7 +391,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...KIBANA_HEADERS, foo: 'bar' },
headers: { ...DEFAULT_HEADERS, foo: 'bar' },
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/elasticsearch/client/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ElasticsearchClient } from './types';
import { configureClient } from './configure_client';
import { ElasticsearchClientConfig } from './client_config';
import { ScopedClusterClient, IScopedClusterClient } from './scoped_cluster_client';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';

const noop = () => undefined;

Expand Down Expand Up @@ -109,7 +109,7 @@ export class ClusterClient implements ICustomClusterClient {
}

return {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
...this.config.customHeaders,
...scopedHeaders,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

export const KIBANA_HEADERS = {
import { deepFreeze } from '@kbn/std';

export const DEFAULT_HEADERS = deepFreeze({
'x-elastic-product-origin': 'kibana',
};
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
LegacyElasticsearchClientConfig,
parseElasticsearchClientConfig,
} from './elasticsearch_client_config';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';
const logger = loggingSystemMock.create();
afterEach(() => jest.clearAllMocks());

Expand Down Expand Up @@ -348,7 +348,7 @@ describe('#auth', () => {

describe('#customHeaders', () => {
test('override the default headers', () => {
const headerKey = Object.keys(KIBANA_HEADERS)[0];
const headerKey = Object.keys(DEFAULT_HEADERS)[0];
const parsedConfig = parseElasticsearchClientConfig(
{
apiVersion: 'master',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import url from 'url';
import { pick } from '@kbn/std';
import { Logger } from '../../logging';
import { ElasticsearchConfig } from '../elasticsearch_config';
import { KIBANA_HEADERS } from '../kibana_headers';
import { DEFAULT_HEADERS } from '../default_headers';

/**
* @privateRemarks Config that consumers can pass to the Elasticsearch JS client is complex and includes
Expand Down Expand Up @@ -132,7 +132,7 @@ export function parseElasticsearchClientConfig(
path: uri.pathname,
query: uri.query,
headers: {
...KIBANA_HEADERS,
...DEFAULT_HEADERS,
...config.customHeaders,
},
};
Expand Down

0 comments on commit b11e5ce

Please sign in to comment.