2019-09-06 08:43:21 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-01-28 19:44:25 +01:00
|
|
|
import { ActionsConfig } from './config';
|
2019-12-17 15:13:44 +01:00
|
|
|
import {
|
|
|
|
getActionsConfigurationUtilities,
|
2020-08-25 01:25:05 +02:00
|
|
|
AllowedHosts,
|
2019-12-17 15:13:44 +01:00
|
|
|
EnabledActionTypes,
|
|
|
|
} from './actions_config';
|
|
|
|
|
2021-01-28 19:44:25 +01:00
|
|
|
const defaultActionsConfig: ActionsConfig = {
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
2021-01-28 19:44:25 +01:00
|
|
|
preconfigured: {},
|
|
|
|
proxyRejectUnauthorizedCertificates: true,
|
|
|
|
rejectUnauthorized: true,
|
2019-12-17 15:13:44 +01:00
|
|
|
};
|
2019-09-06 08:43:21 +02:00
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
describe('ensureUriAllowed', () => {
|
2019-09-06 08:43:21 +02:00
|
|
|
test('returns true when "any" hostnames are allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-09-06 08:43:21 +02:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [AllowedHosts.Any],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
2019-09-06 08:43:21 +02:00
|
|
|
};
|
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('throws when the hostname in the requested uri is not in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(() =>
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toThrowErrorMatchingInlineSnapshot(
|
2020-08-25 01:25:05 +02:00
|
|
|
`"target url \\"https://github.com/elastic/kibana\\" is not added to the Kibana config xpack.actions.allowedHosts"`
|
2019-09-06 08:43:21 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws when the uri cannot be parsed as a valid URI', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(() =>
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureUriAllowed('github.com/elastic')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toThrowErrorMatchingInlineSnapshot(
|
2020-08-25 01:25:05 +02:00
|
|
|
`"target url \\"github.com/elastic\\" is not added to the Kibana config xpack.actions.allowedHosts"`
|
2019-09-06 08:43:21 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('returns true when the hostname in the requested uri is in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: ['github.com'],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
|
|
|
};
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
describe('ensureHostnameAllowed', () => {
|
2019-09-06 08:43:21 +02:00
|
|
|
test('returns true when "any" hostnames are allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-09-06 08:43:21 +02:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [AllowedHosts.Any],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
2019-09-06 08:43:21 +02:00
|
|
|
};
|
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureHostnameAllowed('github.com')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('throws when the hostname in the requested uri is not in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(() =>
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureHostnameAllowed('github.com')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toThrowErrorMatchingInlineSnapshot(
|
2020-08-25 01:25:05 +02:00
|
|
|
`"target hostname \\"github.com\\" is not added to the Kibana config xpack.actions.allowedHosts"`
|
2019-09-06 08:43:21 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('returns true when the hostname in the requested uri is in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: ['github.com'],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
|
|
|
};
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).ensureHostnameAllowed('github.com')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
describe('isUriAllowed', () => {
|
2019-09-06 08:43:21 +02:00
|
|
|
test('returns true when "any" hostnames are allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-09-06 08:43:21 +02:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [AllowedHosts.Any],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
2019-09-06 08:43:21 +02:00
|
|
|
};
|
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).isUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('throws when the hostname in the requested uri is not in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).isUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws when the uri cannot be parsed as a valid URI', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2020-08-25 01:25:05 +02:00
|
|
|
expect(getActionsConfigurationUtilities(config).isUriAllowed('github.com/elastic')).toEqual(
|
2019-09-06 08:43:21 +02:00
|
|
|
false
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('returns true when the hostname in the requested uri is in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: ['github.com'],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
|
|
|
};
|
2019-09-06 08:43:21 +02:00
|
|
|
expect(
|
2020-08-25 01:25:05 +02:00
|
|
|
getActionsConfigurationUtilities(config).isUriAllowed('https://github.com/elastic/kibana')
|
2019-09-06 08:43:21 +02:00
|
|
|
).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
describe('isHostnameAllowed', () => {
|
2019-09-06 08:43:21 +02:00
|
|
|
test('returns true when "any" hostnames are allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-09-06 08:43:21 +02:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [AllowedHosts.Any],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
2019-09-06 08:43:21 +02:00
|
|
|
};
|
2020-08-25 01:25:05 +02:00
|
|
|
expect(getActionsConfigurationUtilities(config).isHostnameAllowed('github.com')).toEqual(true);
|
2019-09-06 08:43:21 +02:00
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('throws when the hostname in the requested uri is not in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2020-08-25 01:25:05 +02:00
|
|
|
expect(getActionsConfigurationUtilities(config).isHostnameAllowed('github.com')).toEqual(false);
|
2019-09-06 08:43:21 +02:00
|
|
|
});
|
|
|
|
|
2020-08-25 01:25:05 +02:00
|
|
|
test('returns true when the hostname in the requested uri is in the allowedHosts', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: ['github.com'],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
|
|
|
};
|
2020-08-25 01:25:05 +02:00
|
|
|
expect(getActionsConfigurationUtilities(config).isHostnameAllowed('github.com')).toEqual(true);
|
2019-09-06 08:43:21 +02:00
|
|
|
});
|
|
|
|
});
|
2019-12-17 15:13:44 +01:00
|
|
|
|
|
|
|
describe('isActionTypeEnabled', () => {
|
|
|
|
test('returns true when "any" actionTypes are allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['ignore', EnabledActionTypes.Any],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).isActionTypeEnabled('foo')).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns false when no actionType is allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: [],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).isActionTypeEnabled('foo')).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns false when the actionType is not in the enabled list', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['foo'],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).isActionTypeEnabled('bar')).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns true when the actionType is in the enabled list', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['ignore', 'foo'],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).isActionTypeEnabled('foo')).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('ensureActionTypeEnabled', () => {
|
|
|
|
test('does not throw when any actionType is allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['ignore', EnabledActionTypes.Any],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).ensureActionTypeEnabled('foo')).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws when no actionType is not allowed', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = defaultActionsConfig;
|
2019-12-17 15:13:44 +01:00
|
|
|
expect(() =>
|
|
|
|
getActionsConfigurationUtilities(config).ensureActionTypeEnabled('foo')
|
|
|
|
).toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"action type \\"foo\\" is not enabled in the Kibana config xpack.actions.enabledActionTypes"`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('throws when actionType is not enabled', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['ignore'],
|
|
|
|
};
|
|
|
|
expect(() =>
|
|
|
|
getActionsConfigurationUtilities(config).ensureActionTypeEnabled('foo')
|
|
|
|
).toThrowErrorMatchingInlineSnapshot(
|
|
|
|
`"action type \\"foo\\" is not enabled in the Kibana config xpack.actions.enabledActionTypes"`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('does not throw when actionType is enabled', () => {
|
2021-01-28 19:44:25 +01:00
|
|
|
const config: ActionsConfig = {
|
|
|
|
...defaultActionsConfig,
|
2019-12-17 15:13:44 +01:00
|
|
|
enabled: false,
|
2020-08-25 01:25:05 +02:00
|
|
|
allowedHosts: [],
|
2019-12-17 15:13:44 +01:00
|
|
|
enabledActionTypes: ['ignore', 'foo'],
|
|
|
|
};
|
|
|
|
expect(getActionsConfigurationUtilities(config).ensureActionTypeEnabled('foo')).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|