diff --git a/src/core/server/elasticsearch/client/client_config.test.ts b/src/core/server/elasticsearch/client/client_config.test.ts index ba6fba0051c05..e8083836d3c1e 100644 --- a/src/core/server/elasticsearch/client/client_config.test.ts +++ b/src/core/server/elasticsearch/client/client_config.test.ts @@ -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 = {} @@ -43,7 +43,7 @@ describe('parseClientOptions', () => { expect(parseClientOptions(config, false)).toEqual( expect.objectContaining({ headers: { - ...KIBANA_HEADERS, + ...DEFAULT_HEADERS, }, }) ); @@ -61,7 +61,7 @@ describe('parseClientOptions', () => { expect(parseClientOptions(config, false)).toEqual( expect.objectContaining({ headers: { - ...KIBANA_HEADERS, + ...DEFAULT_HEADERS, foo: 'bar', hello: 'dolly', }, @@ -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: { diff --git a/src/core/server/elasticsearch/client/client_config.ts b/src/core/server/elasticsearch/client/client_config.ts index bdc9ee011829e..f24c0d86550b8 100644 --- a/src/core/server/elasticsearch/client/client_config.ts +++ b/src/core/server/elasticsearch/client/client_config.ts @@ -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 @@ -63,7 +63,7 @@ export function parseClientOptions( sniffOnStart: config.sniffOnStart, sniffOnConnectionFault: config.sniffOnConnectionFault, headers: { - ...KIBANA_HEADERS, + ...DEFAULT_HEADERS, ...config.customHeaders, }, }; diff --git a/src/core/server/elasticsearch/client/cluster_client.test.ts b/src/core/server/elasticsearch/client/cluster_client.test.ts index 6a3d828deb580..429fea65704d8 100644 --- a/src/core/server/elasticsearch/client/cluster_client.test.ts +++ b/src/core/server/elasticsearch/client/cluster_client.test.ts @@ -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 = {} @@ -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) }, }); }); @@ -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) }, }); }); @@ -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) }, }); }); @@ -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), @@ -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', }, }); @@ -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), @@ -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), @@ -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', @@ -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], }); @@ -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', }, }); @@ -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' }, }); }); @@ -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' }, }); }); }); diff --git a/src/core/server/elasticsearch/client/cluster_client.ts b/src/core/server/elasticsearch/client/cluster_client.ts index 3dcf82b02b29c..2294df89d4274 100644 --- a/src/core/server/elasticsearch/client/cluster_client.ts +++ b/src/core/server/elasticsearch/client/cluster_client.ts @@ -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; @@ -109,7 +109,7 @@ export class ClusterClient implements ICustomClusterClient { } return { - ...KIBANA_HEADERS, + ...DEFAULT_HEADERS, ...this.config.customHeaders, ...scopedHeaders, }; diff --git a/src/core/server/elasticsearch/kibana_headers.ts b/src/core/server/elasticsearch/default_headers.ts similarity index 90% rename from src/core/server/elasticsearch/kibana_headers.ts rename to src/core/server/elasticsearch/default_headers.ts index 6caac8a23bd84..61be0cacbb805 100644 --- a/src/core/server/elasticsearch/kibana_headers.ts +++ b/src/core/server/elasticsearch/default_headers.ts @@ -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', -}; +}); diff --git a/src/core/server/elasticsearch/legacy/elasticsearch_client_config.test.ts b/src/core/server/elasticsearch/legacy/elasticsearch_client_config.test.ts index 3176f3b8a56e9..1e85932caa8ae 100644 --- a/src/core/server/elasticsearch/legacy/elasticsearch_client_config.test.ts +++ b/src/core/server/elasticsearch/legacy/elasticsearch_client_config.test.ts @@ -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()); @@ -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', diff --git a/src/core/server/elasticsearch/legacy/elasticsearch_client_config.ts b/src/core/server/elasticsearch/legacy/elasticsearch_client_config.ts index 6404070e0556b..35681ac7a247d 100644 --- a/src/core/server/elasticsearch/legacy/elasticsearch_client_config.ts +++ b/src/core/server/elasticsearch/legacy/elasticsearch_client_config.ts @@ -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 @@ -132,7 +132,7 @@ export function parseElasticsearchClientConfig( path: uri.pathname, query: uri.query, headers: { - ...KIBANA_HEADERS, + ...DEFAULT_HEADERS, ...config.customHeaders, }, };