Skip to content

Commit

Permalink
unit test for createConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Mar 20, 2020
1 parent 89118de commit 6bcd58d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
122 changes: 122 additions & 0 deletions x-pack/plugins/reporting/server/config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import * as Rx from 'rxjs';
import { CoreSetup, Logger, PluginInitializerContext } from '../../../../../src/core/server';
import { createConfig$ } from './';

interface KibanaServer {
host?: string;
port?: number;
protocol?: string;
}
interface ReportingKibanaServer {
hostname?: string;
port?: number;
protocol?: string;
}

const makeMockInitContext = (config: {
encryptionKey?: string;
kibanaServer: ReportingKibanaServer;
}): PluginInitializerContext =>
({
config: { create: () => Rx.of(config) },
} as PluginInitializerContext);

const makeMockCoreSetup = (serverInfo: KibanaServer): CoreSetup =>
({ http: { getServerInfo: () => serverInfo } } as any);

describe('Reporting server createConfig$', () => {
let mockCoreSetup: CoreSetup;
let mockInitContext: PluginInitializerContext;
let mockLogger: Logger;

beforeEach(() => {
mockCoreSetup = makeMockCoreSetup({ host: 'kibanaHost', port: 5601, protocol: 'http' });
mockInitContext = makeMockInitContext({
kibanaServer: {},
});
mockLogger = ({ warn: jest.fn() } as unknown) as Logger;
});

afterEach(() => {
jest.resetAllMocks();
});

it('creates random encryption key and default config using host, protocol, and port from server info', async () => {
const result = await createConfig$(mockCoreSetup, mockInitContext, mockLogger).toPromise();

expect(result.encryptionKey).toMatch(/\S{32,}/);
expect(result.kibanaServer).toMatchInlineSnapshot(`
Object {
"hostname": "kibanaHost",
"port": 5601,
"protocol": "http",
}
`);
expect((mockLogger.warn as any).mock.calls.length).toBe(1);
expect((mockLogger.warn as any).mock.calls[0]).toMatchObject([
'Generating a random key for xpack.reporting.encryptionKey. To prevent sessions from being invalidated on restart, please set xpack.reporting.encryptionKey in kibana.yml',
]);
});

it('uses the encryption key', async () => {
mockInitContext = makeMockInitContext({
encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii',
kibanaServer: {},
});
const result = await createConfig$(mockCoreSetup, mockInitContext, mockLogger).toPromise();

expect(result.encryptionKey).toMatch('iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii');
expect((mockLogger.warn as any).mock.calls.length).toBe(0);
});

it('uses the encryption key, reporting kibanaServer settings to override server info', async () => {
mockInitContext = makeMockInitContext({
encryptionKey: 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii',
kibanaServer: {
hostname: 'reportingHost',
port: 5677,
protocol: 'httpsa',
},
});
const result = await createConfig$(mockCoreSetup, mockInitContext, mockLogger).toPromise();

expect(result).toMatchInlineSnapshot(`
Object {
"encryptionKey": "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii",
"kibanaServer": Object {
"hostname": "reportingHost",
"port": 5677,
"protocol": "httpsa",
},
}
`);
expect((mockLogger.warn as any).mock.calls.length).toBe(0);
});

it('show warning when kibanaServer.hostName === "0"', async () => {
mockInitContext = makeMockInitContext({
encryptionKey: 'aaaaaaaaaaaaabbbbbbbbbbbbaaaaaaaaa',
kibanaServer: { hostname: '0' },
});
const result = await createConfig$(mockCoreSetup, mockInitContext, mockLogger).toPromise();

expect(result.kibanaServer).toMatchInlineSnapshot(`
Object {
"hostname": "0.0.0.0",
"port": 5601,
"protocol": "http",
}
`);
expect((mockLogger.warn as any).mock.calls.length).toBe(1);
expect((mockLogger.warn as any).mock.calls[0]).toMatchObject([
`Found 'server.host: \"0\" in Kibana configuration. This is incompatible with Reporting. To enable Reporting to work, 'xpack.reporting.kibanaServer.hostname: 0.0.0.0' is being automatically ` +
`to the configuration. You can change the setting to 'server.host: 0.0.0.0' or add 'xpack.reporting.kibanaServer.hostname: 0.0.0.0' in kibana.yml to prevent this message.`,
]);
});
});
9 changes: 4 additions & 5 deletions x-pack/plugins/reporting/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ConfigSchema, ConfigType } from './schema';
export function createConfig$(core: CoreSetup, context: PluginInitializerContext, logger: Logger) {
return context.config.create<TypeOf<typeof ConfigSchema>>().pipe(
map(config => {
// encryption key
let encryptionKey = config.encryptionKey;
if (encryptionKey === undefined) {
logger.warn(
Expand All @@ -30,23 +31,21 @@ export function createConfig$(core: CoreSetup, context: PluginInitializerContext
const { kibanaServer: reportingServer } = config;
const serverInfo = core.http.getServerInfo();

// kibanaServer.hostname, default to server.host
// kibanaServer.hostname, default to server.host, don't allow "0"
let kibanaServerHostname = reportingServer.hostname
? reportingServer.hostname
: serverInfo.host;

// don't allow "0"
if (kibanaServerHostname === '0') {
logger.warn(
i18n.translate('xpack.reporting.serverConfig.invalidServerHostname', {
defaultMessage:
`Found 'server.host: "0"' in Kibana configuration. This is incompatible with Reporting. ` +
`Found 'server.host: "0" in Kibana configuration. This is incompatible with Reporting. ` +
`To enable Reporting to work, '{configKey}: 0.0.0.0' is being automatically to the configuration. ` +
`You can change the setting to 'server.host: 0.0.0.0' or add '{configKey}: 0.0.0.0' in kibana.yml to prevent this message.`,
values: { configKey: 'xpack.reporting.kibanaServer.hostname' },
})
);
kibanaServerHostname = crypto.randomBytes(16).toString('hex');
kibanaServerHostname = '0.0.0.0';
}

// kibanaServer.port, default to server.port
Expand Down

0 comments on commit 6bcd58d

Please sign in to comment.