Skip to content

Commit

Permalink
Merge branch 'master' into perc-in-title
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Oct 27, 2020
2 parents a3f83f2 + 1066602 commit 8f1e012
Show file tree
Hide file tree
Showing 31 changed files with 679 additions and 186 deletions.
58 changes: 58 additions & 0 deletions src/legacy/server/i18n/get_kibana_translation_paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { I18N_RC } from './constants';
import { fromRoot } from '../../../core/server/utils';

jest.mock('./get_translation_paths', () => ({ getTranslationPaths: jest.fn() }));
import { getKibanaTranslationPaths } from './get_kibana_translation_paths';
import { getTranslationPaths as mockGetTranslationPaths } from './get_translation_paths';

describe('getKibanaTranslationPaths', () => {
const mockConfig = { get: jest.fn() };

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

it('calls getTranslationPaths against kibana root and kibana-extra', async () => {
mockConfig.get.mockReturnValue([]);
await getKibanaTranslationPaths(mockConfig);
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(1, {
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
});

expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(2, {
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
});
});

it('calls getTranslationPaths for each config returned in plugin.paths and plugins.scanDirs', async () => {
mockConfig.get.mockReturnValueOnce(['a', 'b']).mockReturnValueOnce(['c']);
await getKibanaTranslationPaths(mockConfig);
expect(mockConfig.get).toHaveBeenNthCalledWith(1, 'plugins.paths');
expect(mockConfig.get).toHaveBeenNthCalledWith(2, 'plugins.scanDirs');

expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(2, { cwd: 'a', glob: I18N_RC });
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(3, { cwd: 'b', glob: I18N_RC });
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(4, { cwd: 'c', glob: `*/${I18N_RC}` });
});
});
42 changes: 42 additions & 0 deletions src/legacy/server/i18n/get_kibana_translation_paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { KibanaConfig } from '../kbn_server';
import { fromRoot } from '../../../core/server/utils';
import { I18N_RC } from './constants';
import { getTranslationPaths } from './get_translation_paths';

export async function getKibanaTranslationPaths(config: Pick<KibanaConfig, 'get'>) {
return await Promise.all([
getTranslationPaths({
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
}),
...(config.get('plugins.paths') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: I18N_RC })
),
...(config.get('plugins.scanDirs') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: `*/${I18N_RC}` })
),
getTranslationPaths({
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
}),
]);
}
63 changes: 63 additions & 0 deletions src/legacy/server/i18n/i18n_mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n, i18nLoader } from '@kbn/i18n';
import { basename } from 'path';
import { Server } from 'hapi';
import type { UsageCollectionSetup } from '../../../plugins/usage_collection/server';
import { getKibanaTranslationPaths } from './get_kibana_translation_paths';
import KbnServer, { KibanaConfig } from '../kbn_server';
import { registerLocalizationUsageCollector } from './localization';

export async function i18nMixin(
kbnServer: KbnServer,
server: Server,
config: Pick<KibanaConfig, 'get'>
) {
const locale = config.get('i18n.locale') as string;

const translationPaths = await getKibanaTranslationPaths(config);

const currentTranslationPaths = ([] as string[])
.concat(...translationPaths)
.filter((translationPath) => basename(translationPath, '.json') === locale);
i18nLoader.registerTranslationFiles(currentTranslationPaths);

const translations = await i18nLoader.getTranslationsByLocale(locale);
i18n.init(
Object.freeze({
locale,
...translations,
})
);

const getTranslationsFilePaths = () => currentTranslationPaths;

server.decorate('server', 'getTranslationsFilePaths', getTranslationsFilePaths);

if (kbnServer.newPlatform.setup.plugins.usageCollection) {
const { usageCollection } = kbnServer.newPlatform.setup.plugins as {
usageCollection: UsageCollectionSetup;
};
registerLocalizationUsageCollector(usageCollection, {
getLocale: () => config.get('i18n.locale') as string,
getTranslationsFilePaths,
});
}
}
58 changes: 1 addition & 57 deletions src/legacy/server/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,4 @@
* under the License.
*/

import { i18n, i18nLoader } from '@kbn/i18n';
import { basename } from 'path';
import { Server } from 'hapi';
import { fromRoot } from '../../../core/server/utils';
import type { UsageCollectionSetup } from '../../../plugins/usage_collection/server';
import { getTranslationPaths } from './get_translations_path';
import { I18N_RC } from './constants';
import KbnServer, { KibanaConfig } from '../kbn_server';
import { registerLocalizationUsageCollector } from './localization';

export async function i18nMixin(kbnServer: KbnServer, server: Server, config: KibanaConfig) {
const locale = config.get('i18n.locale') as string;

const translationPaths = await Promise.all([
getTranslationPaths({
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
}),
...(config.get('plugins.paths') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: I18N_RC })
),
...(config.get('plugins.scanDirs') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: `*/${I18N_RC}` })
),
getTranslationPaths({
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
}),
]);

const currentTranslationPaths = ([] as string[])
.concat(...translationPaths)
.filter((translationPath) => basename(translationPath, '.json') === locale);
i18nLoader.registerTranslationFiles(currentTranslationPaths);

const translations = await i18nLoader.getTranslationsByLocale(locale);
i18n.init(
Object.freeze({
locale,
...translations,
})
);

const getTranslationsFilePaths = () => currentTranslationPaths;

server.decorate('server', 'getTranslationsFilePaths', getTranslationsFilePaths);

if (kbnServer.newPlatform.setup.plugins.usageCollection) {
const { usageCollection } = kbnServer.newPlatform.setup.plugins as {
usageCollection: UsageCollectionSetup;
};
registerLocalizationUsageCollector(usageCollection, {
getLocale: () => config.get('i18n.locale') as string,
getTranslationsFilePaths,
});
}
}
export { i18nMixin } from './i18n_mixin';
13 changes: 10 additions & 3 deletions x-pack/plugins/actions/server/builtin_action_types/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ describe('config validation', () => {
};

test('config validation passes when only required fields are provided', () => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -101,9 +102,10 @@ describe('config validation', () => {

test('config validation passes when valid methods are provided', () => {
['post', 'put'].forEach((method) => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
method,
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -127,8 +129,9 @@ describe('config validation', () => {
});

test('config validation passes when a url is specified', () => {
const config: Record<string, string> = {
const config: Record<string, string | boolean> = {
url: 'http://mylisteningserver:9200/endpoint',
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand All @@ -155,6 +158,7 @@ describe('config validation', () => {
headers: {
'Content-Type': 'application/json',
},
hasAuth: true,
};
expect(validateConfig(actionType, config)).toEqual({
...defaultValues,
Expand Down Expand Up @@ -184,6 +188,7 @@ describe('config validation', () => {
headers: {
'Content-Type': 'application/json',
},
hasAuth: true,
};

expect(validateConfig(actionType, config)).toEqual({
Expand Down Expand Up @@ -263,6 +268,7 @@ describe('execute()', () => {
headers: {
aheader: 'a value',
},
hasAuth: true,
};
await actionType.executor({
actionId: 'some-id',
Expand Down Expand Up @@ -320,6 +326,7 @@ describe('execute()', () => {
headers: {
aheader: 'a value',
},
hasAuth: false,
};
const secrets: ActionTypeSecretsType = { user: null, password: null };
await actionType.executor({
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/actions/server/builtin_action_types/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const configSchemaProps = {
defaultValue: WebhookMethods.POST,
}),
headers: nullableType(HeadersSchema),
hasAuth: schema.boolean({ defaultValue: true }),
};
const ConfigSchema = schema.object(configSchemaProps);
export type ActionTypeConfigType = TypeOf<typeof ConfigSchema>;
Expand Down Expand Up @@ -128,12 +129,12 @@ export async function executor(
execOptions: WebhookActionTypeExecutorOptions
): Promise<ActionTypeExecutorResult<unknown>> {
const actionId = execOptions.actionId;
const { method, url, headers = {} } = execOptions.config;
const { method, url, headers = {}, hasAuth } = execOptions.config;
const { body: data } = execOptions.params;

const secrets: ActionTypeSecretsType = execOptions.secrets;
const basicAuth =
isString(secrets.user) && isString(secrets.password)
hasAuth && isString(secrets.user) && isString(secrets.password)
? { auth: { username: secrets.user, password: secrets.password } }
: {};

Expand Down
57 changes: 57 additions & 0 deletions x-pack/plugins/actions/server/saved_objects/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ describe('7.10.0', () => {
});
});

describe('7.11.0', () => {
beforeEach(() => {
jest.resetAllMocks();
encryptedSavedObjectsSetup.createMigration.mockImplementation(
(shouldMigrateWhenPredicate, migration) => migration
);
});

test('add hasAuth = true for .webhook actions with user and password', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const action = getMockDataForWebhook({}, true);
expect(migration711(action, context)).toMatchObject({
...action,
attributes: {
...action.attributes,
config: {
hasAuth: true,
},
},
});
});

test('add hasAuth = false for .webhook actions without user and password', () => {
const migration711 = getMigrations(encryptedSavedObjectsSetup)['7.11.0'];
const action = getMockDataForWebhook({}, false);
expect(migration711(action, context)).toMatchObject({
...action,
attributes: {
...action.attributes,
config: {
hasAuth: false,
},
},
});
});
});

function getMockDataForWebhook(
overwrites: Record<string, unknown> = {},
hasUserAndPassword: boolean
): SavedObjectUnsanitizedDoc<RawAction> {
const secrets = hasUserAndPassword
? { user: 'test', password: '123' }
: { user: '', password: '' };
return {
attributes: {
name: 'abc',
actionTypeId: '.webhook',
config: {},
secrets,
...overwrites,
},
id: uuid.v4(),
type: 'action',
};
}

function getMockDataForEmail(
overwrites: Record<string, unknown> = {}
): SavedObjectUnsanitizedDoc<RawAction> {
Expand Down
Loading

0 comments on commit 8f1e012

Please sign in to comment.