From 85c65427b965036e71845be4bcdae3883c808210 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Fri, 18 May 2018 16:28:14 -0400 Subject: [PATCH] Ensure we account for cluster level settings when looking for configured actions (#19121) (#19226) --- .../server/models/settings/__tests__/settings.js | 15 +++++++++++++++ .../watcher/server/models/settings/settings.js | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/watcher/server/models/settings/__tests__/settings.js b/x-pack/plugins/watcher/server/models/settings/__tests__/settings.js index 888b79aac8db31..6c34467ef8629b 100644 --- a/x-pack/plugins/watcher/server/models/settings/__tests__/settings.js +++ b/x-pack/plugins/watcher/server/models/settings/__tests__/settings.js @@ -29,6 +29,19 @@ describe('settings module', () => { describe('when upstream JSON contains a configured action type', () => { it('returns the correct Settings instance', () => { const upstreamJson = { + persistent: { + xpack: { + notification: { + email: { + account: { + foo: {}, + bar: {} + }, + default_account: 'bar' + } + } + } + }, defaults: { xpack: { notification: { @@ -49,6 +62,8 @@ describe('settings module', () => { expect(actionTypes.email.enabled).to.be(true); expect(actionTypes.email.accounts.scooby.default).to.be(true); expect(actionTypes.email.accounts.scrappy).to.be.an('object'); + expect(actionTypes.email.accounts.foo).to.be.an('object'); + expect(actionTypes.email.accounts.bar).to.be.an('object'); }); }); }); diff --git a/x-pack/plugins/watcher/server/models/settings/settings.js b/x-pack/plugins/watcher/server/models/settings/settings.js index dd81897a5eaee6..91970573f795fb 100644 --- a/x-pack/plugins/watcher/server/models/settings/settings.js +++ b/x-pack/plugins/watcher/server/models/settings/settings.js @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { get } from 'lodash'; +import { merge } from 'lodash'; import { ACTION_TYPES } from '../../../common/constants'; function isEnabledByDefault(actionType) { @@ -47,9 +47,21 @@ function getAccounts({ account, default_account: defaultAccount }) { }, {}); } +function getNotifications(json) { + if (!json) { + return {}; + } + return Object.values(json).reduce((accum, value) => { + if (value.hasOwnProperty('xpack') && value.xpack.hasOwnProperty('notification')) { + accum = merge(accum, value.xpack.notification); + } + return accum; + }, {}); +} + function getActionTypesSettings(upstreamJson) { - const upstreamActionTypes = get(upstreamJson, 'defaults.xpack.notification', {}); + const upstreamActionTypes = getNotifications(upstreamJson); // Initialize settings for known action types const actionTypes = Object.keys(ACTION_TYPES).reduce((types, typeName) => {