-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[alerting] Adds Action Type configuration support and whitelisting (#…
…44483) A wide refactor of Action Types and their configuration: - Action Types are now initialised with a their associated configuration based on `kibana.yml` - You may now whitelist certain hostnames to allow built-in actions, such as _Webhooks_, to communicate with the outside world - The _Webhooks_ Built-in action is now available, if you have `alerting` and `actions` enabled
- Loading branch information
Showing
21 changed files
with
496 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
x-pack/legacy/plugins/actions/server/actions_config.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* 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 { | ||
ActionsKibanaConfig, | ||
getActionsConfigurationUtilities, | ||
WhitelistedHosts, | ||
} from './actions_config'; | ||
|
||
describe('ensureWhitelistedUri', () => { | ||
test('returns true when "any" hostnames are allowed', () => { | ||
const config: ActionsKibanaConfig = { | ||
enabled: false, | ||
whitelistedHosts: [WhitelistedHosts.Any], | ||
}; | ||
expect( | ||
getActionsConfigurationUtilities(config).ensureWhitelistedUri( | ||
'https://github.com/elastic/kibana' | ||
) | ||
).toBeUndefined(); | ||
}); | ||
|
||
test('throws when the hostname in the requested uri is not in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect(() => | ||
getActionsConfigurationUtilities(config).ensureWhitelistedUri( | ||
'https://github.com/elastic/kibana' | ||
) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"target url \\"https://github.com/elastic/kibana\\" is not in the Kibana whitelist"` | ||
); | ||
}); | ||
|
||
test('throws when the uri cannot be parsed as a valid URI', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect(() => | ||
getActionsConfigurationUtilities(config).ensureWhitelistedUri('github.com/elastic') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"target url \\"github.com/elastic\\" is not in the Kibana whitelist"` | ||
); | ||
}); | ||
|
||
test('returns true when the hostname in the requested uri is in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: ['github.com'] }; | ||
expect( | ||
getActionsConfigurationUtilities(config).ensureWhitelistedUri( | ||
'https://github.com/elastic/kibana' | ||
) | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('ensureWhitelistedHostname', () => { | ||
test('returns true when "any" hostnames are allowed', () => { | ||
const config: ActionsKibanaConfig = { | ||
enabled: false, | ||
whitelistedHosts: [WhitelistedHosts.Any], | ||
}; | ||
expect( | ||
getActionsConfigurationUtilities(config).ensureWhitelistedHostname('github.com') | ||
).toBeUndefined(); | ||
}); | ||
|
||
test('throws when the hostname in the requested uri is not in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect(() => | ||
getActionsConfigurationUtilities(config).ensureWhitelistedHostname('github.com') | ||
).toThrowErrorMatchingInlineSnapshot( | ||
`"target hostname \\"github.com\\" is not in the Kibana whitelist"` | ||
); | ||
}); | ||
|
||
test('returns true when the hostname in the requested uri is in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: ['github.com'] }; | ||
expect( | ||
getActionsConfigurationUtilities(config).ensureWhitelistedHostname('github.com') | ||
).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('isWhitelistedUri', () => { | ||
test('returns true when "any" hostnames are allowed', () => { | ||
const config: ActionsKibanaConfig = { | ||
enabled: false, | ||
whitelistedHosts: [WhitelistedHosts.Any], | ||
}; | ||
expect( | ||
getActionsConfigurationUtilities(config).isWhitelistedUri('https://github.com/elastic/kibana') | ||
).toEqual(true); | ||
}); | ||
|
||
test('throws when the hostname in the requested uri is not in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect( | ||
getActionsConfigurationUtilities(config).isWhitelistedUri('https://github.com/elastic/kibana') | ||
).toEqual(false); | ||
}); | ||
|
||
test('throws when the uri cannot be parsed as a valid URI', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect(getActionsConfigurationUtilities(config).isWhitelistedUri('github.com/elastic')).toEqual( | ||
false | ||
); | ||
}); | ||
|
||
test('returns true when the hostname in the requested uri is in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: ['github.com'] }; | ||
expect( | ||
getActionsConfigurationUtilities(config).isWhitelistedUri('https://github.com/elastic/kibana') | ||
).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('isWhitelistedHostname', () => { | ||
test('returns true when "any" hostnames are allowed', () => { | ||
const config: ActionsKibanaConfig = { | ||
enabled: false, | ||
whitelistedHosts: [WhitelistedHosts.Any], | ||
}; | ||
expect(getActionsConfigurationUtilities(config).isWhitelistedHostname('github.com')).toEqual( | ||
true | ||
); | ||
}); | ||
|
||
test('throws when the hostname in the requested uri is not in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: [] }; | ||
expect(getActionsConfigurationUtilities(config).isWhitelistedHostname('github.com')).toEqual( | ||
false | ||
); | ||
}); | ||
|
||
test('returns true when the hostname in the requested uri is in the whitelist', () => { | ||
const config: ActionsKibanaConfig = { enabled: false, whitelistedHosts: ['github.com'] }; | ||
expect(getActionsConfigurationUtilities(config).isWhitelistedHostname('github.com')).toEqual( | ||
true | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { tryCatch, fromNullable } from 'fp-ts/lib/Option'; | ||
import { URL } from 'url'; | ||
import { curry } from 'lodash'; | ||
|
||
export enum WhitelistedHosts { | ||
Any = '*', | ||
} | ||
|
||
enum WhitelistingField { | ||
url = 'url', | ||
hostname = 'hostname', | ||
} | ||
|
||
export interface ActionsKibanaConfig { | ||
enabled: boolean; | ||
whitelistedHosts: string[]; | ||
} | ||
|
||
export interface ActionsConfigurationUtilities { | ||
isWhitelistedHostname: (hostname: string) => boolean; | ||
isWhitelistedUri: (uri: string) => boolean; | ||
ensureWhitelistedHostname: (hostname: string) => void; | ||
ensureWhitelistedUri: (uri: string) => void; | ||
} | ||
|
||
function whitelistingErrorMessage(field: WhitelistingField, value: string) { | ||
return i18n.translate('xpack.actions.urlWhitelistConfigurationError', { | ||
defaultMessage: 'target {field} "{value}" is not in the Kibana whitelist', | ||
values: { | ||
value, | ||
field, | ||
}, | ||
}); | ||
} | ||
|
||
function doesValueWhitelistAnyHostname(whitelistedHostname: string): boolean { | ||
return whitelistedHostname === WhitelistedHosts.Any; | ||
} | ||
|
||
function isWhitelisted({ whitelistedHosts }: ActionsKibanaConfig, hostname: string): boolean { | ||
return ( | ||
Array.isArray(whitelistedHosts) && | ||
fromNullable( | ||
whitelistedHosts.find( | ||
whitelistedHostname => | ||
doesValueWhitelistAnyHostname(whitelistedHostname) || whitelistedHostname === hostname | ||
) | ||
).isSome() | ||
); | ||
} | ||
|
||
function isWhitelistedHostnameInUri(config: ActionsKibanaConfig, uri: string): boolean { | ||
return tryCatch(() => new URL(uri)) | ||
.map(url => url.hostname) | ||
.mapNullable(hostname => isWhitelisted(config, hostname)) | ||
.getOrElse(false); | ||
} | ||
|
||
export function getActionsConfigurationUtilities( | ||
config: ActionsKibanaConfig | ||
): ActionsConfigurationUtilities { | ||
const isWhitelistedHostname = curry(isWhitelisted)(config); | ||
const isWhitelistedUri = curry(isWhitelistedHostnameInUri)(config); | ||
return { | ||
isWhitelistedHostname, | ||
isWhitelistedUri, | ||
ensureWhitelistedUri(uri: string) { | ||
if (!isWhitelistedUri(uri)) { | ||
throw new Error(whitelistingErrorMessage(WhitelistingField.url, uri)); | ||
} | ||
}, | ||
ensureWhitelistedHostname(hostname: string) { | ||
if (!isWhitelistedHostname(hostname)) { | ||
throw new Error(whitelistingErrorMessage(WhitelistingField.hostname, hostname)); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.