"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.HealthCheck = void 0; var _react = _interopRequireDefault(require("react")); var _Option = require("fp-ts/lib/Option"); var _pipeable = require("fp-ts/lib/pipeable"); var _i18nReact = require("@kbn/i18n-react"); var _eui = require("@elastic/eui"); var _i18n = require("@kbn/i18n"); require("./health_check.scss"); var _health_context = require("../context/health_context"); var _kibana = require("../../common/lib/kibana"); var _center_justified_spinner = require("./center_justified_spinner"); var _health_api = require("../../common/lib/health_api"); var _health = require("../lib/rule_api/health"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ const HealthCheck = ({ children, waitForCheck, inFlyout = false }) => { const { http, docLinks } = (0, _kibana.useKibana)().services; const { setLoadingHealthCheck } = (0, _health_context.useHealthContext)(); const [alertingHealth, setAlertingHealth] = _react.default.useState(_Option.none); _react.default.useEffect(() => { (async function () { setLoadingHealthCheck(true); const triggersActionsUiHealthStatus = await (0, _health_api.triggersActionsUiHealth)({ http }); const healthStatus = { ...triggersActionsUiHealthStatus, isSufficientlySecure: false, hasPermanentEncryptionKey: false }; if (healthStatus.isRulesAvailable) { // Get the framework health, but if not available, do NOT cause the // framework health errors/toasts to appear, since the state is // actually unknown. These also need to be set to clear the busy // indicator. const alertingHealthResult = await getAlertingFrameworkHealth(http); if ((0, _Option.isSome)(alertingHealthResult)) { healthStatus.isSufficientlySecure = alertingHealthResult.value.isSufficientlySecure; healthStatus.hasPermanentEncryptionKey = alertingHealthResult.value.hasPermanentEncryptionKey; } else { healthStatus.isSufficientlySecure = true; healthStatus.hasPermanentEncryptionKey = true; } setAlertingHealth((0, _Option.some)(healthStatus)); } setLoadingHealthCheck(false); })(); }, [http, setLoadingHealthCheck]); const className = inFlyout ? 'alertingFlyoutHealthCheck' : 'alertingHealthCheck'; return (0, _pipeable.pipe)(alertingHealth, (0, _Option.fold)(() => waitForCheck ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_eui.EuiSpacer, { size: "m" }), /*#__PURE__*/_react.default.createElement(_center_justified_spinner.CenterJustifiedSpinner, null)) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children), healthCheck => { return healthCheck !== null && healthCheck !== void 0 && healthCheck.isSufficientlySecure && healthCheck !== null && healthCheck !== void 0 && healthCheck.hasPermanentEncryptionKey ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, children) : !healthCheck.isRulesAvailable ? /*#__PURE__*/_react.default.createElement(AlertsError, { docLinks: docLinks, className: className }) : !healthCheck.isSufficientlySecure && !healthCheck.hasPermanentEncryptionKey ? /*#__PURE__*/_react.default.createElement(ApiKeysAndEncryptionError, { docLinks: docLinks, className: className }) : !healthCheck.hasPermanentEncryptionKey ? /*#__PURE__*/_react.default.createElement(EncryptionError, { docLinks: docLinks, className: className }) : /*#__PURE__*/_react.default.createElement(ApiKeysDisabledError, { docLinks: docLinks, className: className }); })); }; // Return as an Option, returning none if error occurred getting health. // Currently, alerting health returns a 403 if the user is not authorized // for rules. exports.HealthCheck = HealthCheck; async function getAlertingFrameworkHealth(http) { try { return (0, _Option.some)(await (0, _health.alertingFrameworkHealth)({ http })); } catch (err) { return _Option.none; } } const EncryptionError = ({ docLinks, className }) => /*#__PURE__*/_react.default.createElement(_eui.EuiEmptyPrompt, { iconType: "watchesApp", "data-test-subj": "actionNeededEmptyPrompt", className: className, titleSize: "xs", title: /*#__PURE__*/_react.default.createElement("h2", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "xpack.triggersActionsUI.components.healthCheck.encryptionErrorTitle", defaultMessage: "Additional setup required" })), body: /*#__PURE__*/_react.default.createElement("div", { className: `${className}__body` }, /*#__PURE__*/_react.default.createElement("p", { role: "banner" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.encryptionErrorBeforeKey', { defaultMessage: 'You must configure an encryption key to use Alerting. ' }), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, { href: docLinks.links.alerting.generalSettings, external: true, target: "_blank" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.encryptionErrorAction', { defaultMessage: 'Learn more.' })))) }); const ApiKeysDisabledError = ({ docLinks, className }) => /*#__PURE__*/_react.default.createElement(_eui.EuiEmptyPrompt, { iconType: "watchesApp", "data-test-subj": "actionNeededEmptyPrompt", className: className, titleSize: "xs", title: /*#__PURE__*/_react.default.createElement("h2", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "xpack.triggersActionsUI.components.healthCheck.apiKeysDisabledErrorTitle", defaultMessage: "Additional setup required" })), body: /*#__PURE__*/_react.default.createElement("div", { className: `${className}__body` }, /*#__PURE__*/_react.default.createElement("p", { role: "banner" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.apiKeysDisabledError', { defaultMessage: 'You must enable API keys to use Alerting. ' }), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, { href: docLinks.links.security.elasticsearchEnableApiKeys, external: true, target: "_blank" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.apiKeysDisabledErrorAction', { defaultMessage: 'Learn more.' })))) }); const AlertsError = ({ docLinks, className }) => /*#__PURE__*/_react.default.createElement(_eui.EuiEmptyPrompt, { iconType: "watchesApp", "data-test-subj": "alertsNeededEmptyPrompt", className: className, titleSize: "xs", title: /*#__PURE__*/_react.default.createElement("h2", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "xpack.triggersActionsUI.components.healthCheck.alertsErrorTitle", defaultMessage: "You must enable Alerting and Actions" })), body: /*#__PURE__*/_react.default.createElement("div", { className: `${className}__body` }, /*#__PURE__*/_react.default.createElement("p", { role: "banner" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.alertsError', { defaultMessage: 'To create a rule, you must enable the alerting and actions plugins. ' }), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, { href: docLinks.links.alerting.generalSettings, external: true, target: "_blank" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.alertsErrorAction', { defaultMessage: 'Learn how.' })))) }); const ApiKeysAndEncryptionError = ({ docLinks, className }) => /*#__PURE__*/_react.default.createElement(_eui.EuiEmptyPrompt, { iconType: "watchesApp", "data-test-subj": "actionNeededEmptyPrompt", className: className, titleSize: "xs", title: /*#__PURE__*/_react.default.createElement("h2", null, /*#__PURE__*/_react.default.createElement(_i18nReact.FormattedMessage, { id: "xpack.triggersActionsUI.components.healthCheck.apiKeysAndEncryptionErrorTitle", defaultMessage: "Additional setup required" })), body: /*#__PURE__*/_react.default.createElement("div", { className: `${className}__body` }, /*#__PURE__*/_react.default.createElement("p", { role: "banner" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.apiKeysAndEncryptionError', { defaultMessage: 'You must enable API keys and configure an encryption key to use Alerting. ' }), /*#__PURE__*/_react.default.createElement(_eui.EuiLink, { href: docLinks.links.alerting.setupPrerequisites, external: true, target: "_blank" }, _i18n.i18n.translate('xpack.triggersActionsUI.components.healthCheck.apiKeysAndEncryptionErrorAction', { defaultMessage: 'Learn more.' })))) });