From be15e3f52b1f27051d1dcd0bc1942b7656dcea0a Mon Sep 17 00:00:00 2001 From: shahzad Date: Wed, 6 May 2020 13:24:37 +0200 Subject: [PATCH 1/4] update --- .../components/settings/certificate_form.tsx | 14 ++++--- .../plugins/uptime/public/pages/settings.tsx | 37 +++++++++++-------- .../uptime/public/pages/translations.ts | 8 ++++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/x-pack/plugins/uptime/public/components/settings/certificate_form.tsx b/x-pack/plugins/uptime/public/components/settings/certificate_form.tsx index 06ce6b3599782..8acbf1b40498e 100644 --- a/x-pack/plugins/uptime/public/components/settings/certificate_form.tsx +++ b/x-pack/plugins/uptime/public/components/settings/certificate_form.tsx @@ -58,7 +58,7 @@ export const CertificateExpirationForm: React.FC = ({ > = ({ }} /> } - isInvalid={!!fieldErrors?.certificatesThresholds?.expirationThresholdError} + isInvalid={!!fieldErrors?.expirationThresholdError} label={ = ({ = ({ = ({ }} /> } - isInvalid={!!fieldErrors?.certificatesThresholds?.ageThresholdError} + isInvalid={!!fieldErrors?.ageThresholdError} label={ = ({ + onChange={({ currentTarget: { value } }) => onChange({ - certAgeThreshold: Number(e.currentTarget.value), + certAgeThreshold: Number(value), }) } /> diff --git a/x-pack/plugins/uptime/public/pages/settings.tsx b/x-pack/plugins/uptime/public/pages/settings.tsx index 7ed9cf4444343..f934c990bec45 100644 --- a/x-pack/plugins/uptime/public/pages/settings.tsx +++ b/x-pack/plugins/uptime/public/pages/settings.tsx @@ -19,6 +19,7 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { useDispatch, useSelector } from 'react-redux'; import { isEqual } from 'lodash'; import { Link } from 'react-router-dom'; +import { i18n } from '@kbn/i18n'; import { selectDynamicSettings } from '../state/selectors'; import { getDynamicSettings, setDynamicSettings } from '../state/actions/dynamic_settings'; import { DynamicSettings } from '../../common/runtime_types'; @@ -35,10 +36,8 @@ import * as Translations from './translations'; interface SettingsPageFieldErrors { heartbeatIndices: 'May not be blank' | ''; - certificatesThresholds: { - expirationThresholdError: string | null; - ageThresholdError: string | null; - } | null; + expirationThresholdError: string | null; + ageThresholdError: string | null; } export interface SettingsFormProps { @@ -49,22 +48,28 @@ export interface SettingsFormProps { isDisabled: boolean; } +const isValidCertVal = (val: string | number) => { + if (val === '') { + return Translations.BLANK_STR; + } + if (val === 0) { + return Translations.VALID_STR; + } +}; + const getFieldErrors = (formFields: DynamicSettings | null): SettingsPageFieldErrors | null => { if (formFields) { - const blankStr = 'May not be blank'; const { certAgeThreshold, certExpirationThreshold, heartbeatIndices } = formFields; - const heartbeatIndErr = heartbeatIndices.match(/^\S+$/) ? '' : blankStr; - const expirationThresholdError = certExpirationThreshold ? null : blankStr; - const ageThresholdError = certAgeThreshold ? null : blankStr; + + const indError = heartbeatIndices.match(/^\S+$/) ? '' : Translations.BLANK_STR; + + const expError = isValidCertVal(certExpirationThreshold); + const ageError = isValidCertVal(certAgeThreshold); + return { - heartbeatIndices: heartbeatIndErr, - certificatesThresholds: - expirationThresholdError || ageThresholdError - ? { - expirationThresholdError, - ageThresholdError, - } - : null, + heartbeatIndices: indError, + expirationThresholdError: expError, + ageThresholdError: ageError, }; } return null; diff --git a/x-pack/plugins/uptime/public/pages/translations.ts b/x-pack/plugins/uptime/public/pages/translations.ts index 74fb2eeb1416b..d07a14b07cefe 100644 --- a/x-pack/plugins/uptime/public/pages/translations.ts +++ b/x-pack/plugins/uptime/public/pages/translations.ts @@ -36,3 +36,11 @@ export const settings = { defaultMessage: 'Return to overview', }), }; + +export const BLANK_STR = i18n.translate('xpack.uptime.settings.blank.error', { + defaultMessage: 'May not be blank.', +}); + +export const VALID_STR = i18n.translate('xpack.uptime.settings.invalid.error', { + defaultMessage: 'Not a valid value.', +}); From 8a4bc0dd8299ae6c78ca31874d160c9e71f84b43 Mon Sep 17 00:00:00 2001 From: shahzad Date: Wed, 6 May 2020 13:28:11 +0200 Subject: [PATCH 2/4] fixed types --- x-pack/plugins/uptime/public/pages/settings.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugins/uptime/public/pages/settings.tsx b/x-pack/plugins/uptime/public/pages/settings.tsx index f934c990bec45..e43fe9e5ea600 100644 --- a/x-pack/plugins/uptime/public/pages/settings.tsx +++ b/x-pack/plugins/uptime/public/pages/settings.tsx @@ -19,7 +19,6 @@ import { FormattedMessage } from '@kbn/i18n/react'; import { useDispatch, useSelector } from 'react-redux'; import { isEqual } from 'lodash'; import { Link } from 'react-router-dom'; -import { i18n } from '@kbn/i18n'; import { selectDynamicSettings } from '../state/selectors'; import { getDynamicSettings, setDynamicSettings } from '../state/actions/dynamic_settings'; import { DynamicSettings } from '../../common/runtime_types'; @@ -35,7 +34,7 @@ import { import * as Translations from './translations'; interface SettingsPageFieldErrors { - heartbeatIndices: 'May not be blank' | ''; + heartbeatIndices: string | ''; expirationThresholdError: string | null; ageThresholdError: string | null; } From 1cd1466287f4cee422662ea3beef909e39d727be Mon Sep 17 00:00:00 2001 From: shahzad Date: Wed, 6 May 2020 13:32:35 +0200 Subject: [PATCH 3/4] tyoe --- x-pack/plugins/uptime/public/pages/settings.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/uptime/public/pages/settings.tsx b/x-pack/plugins/uptime/public/pages/settings.tsx index e43fe9e5ea600..3f3870597d8af 100644 --- a/x-pack/plugins/uptime/public/pages/settings.tsx +++ b/x-pack/plugins/uptime/public/pages/settings.tsx @@ -35,8 +35,8 @@ import * as Translations from './translations'; interface SettingsPageFieldErrors { heartbeatIndices: string | ''; - expirationThresholdError: string | null; - ageThresholdError: string | null; + expirationThresholdError?: string; + ageThresholdError?: string; } export interface SettingsFormProps { From 1d96e26b9c548c95bdcab5e69b24fe83df0dffc7 Mon Sep 17 00:00:00 2001 From: shahzad Date: Thu, 7 May 2020 11:49:34 +0200 Subject: [PATCH 4/4] validte API req --- x-pack/plugins/uptime/common/translations.ts | 14 +++++++++ .../plugins/uptime/public/pages/settings.tsx | 3 +- .../uptime/public/pages/translations.ts | 4 --- .../server/rest_api/dynamic_settings.ts | 29 +++++++++++++++---- 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 x-pack/plugins/uptime/common/translations.ts diff --git a/x-pack/plugins/uptime/common/translations.ts b/x-pack/plugins/uptime/common/translations.ts new file mode 100644 index 0000000000000..678fe7cb1f984 --- /dev/null +++ b/x-pack/plugins/uptime/common/translations.ts @@ -0,0 +1,14 @@ +/* + * 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'; + +export const VALUE_MUST_BE_GREATER_THEN_ZEO = i18n.translate( + 'xpack.uptime.settings.invalid.error', + { + defaultMessage: 'Value must be greater than 0.', + } +); diff --git a/x-pack/plugins/uptime/public/pages/settings.tsx b/x-pack/plugins/uptime/public/pages/settings.tsx index 3f3870597d8af..d018567ae1104 100644 --- a/x-pack/plugins/uptime/public/pages/settings.tsx +++ b/x-pack/plugins/uptime/public/pages/settings.tsx @@ -32,6 +32,7 @@ import { OnFieldChangeType, } from '../components/settings/certificate_form'; import * as Translations from './translations'; +import { VALUE_MUST_BE_GREATER_THEN_ZEO } from '../../common/translations'; interface SettingsPageFieldErrors { heartbeatIndices: string | ''; @@ -52,7 +53,7 @@ const isValidCertVal = (val: string | number) => { return Translations.BLANK_STR; } if (val === 0) { - return Translations.VALID_STR; + return VALUE_MUST_BE_GREATER_THEN_ZEO; } }; diff --git a/x-pack/plugins/uptime/public/pages/translations.ts b/x-pack/plugins/uptime/public/pages/translations.ts index d07a14b07cefe..8ed5503235884 100644 --- a/x-pack/plugins/uptime/public/pages/translations.ts +++ b/x-pack/plugins/uptime/public/pages/translations.ts @@ -40,7 +40,3 @@ export const settings = { export const BLANK_STR = i18n.translate('xpack.uptime.settings.blank.error', { defaultMessage: 'May not be blank.', }); - -export const VALID_STR = i18n.translate('xpack.uptime.settings.invalid.error', { - defaultMessage: 'Not a valid value.', -}); diff --git a/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts b/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts index 31833a25ee8ac..c7d532d932aa6 100644 --- a/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts +++ b/x-pack/plugins/uptime/server/rest_api/dynamic_settings.ts @@ -11,6 +11,7 @@ import { UMServerLibs } from '../lib/lib'; import { DynamicSettings, DynamicSettingsType } from '../../common/runtime_types'; import { UMRestApiRouteFactory } from '.'; import { savedObjectsAdapter } from '../lib/saved_objects'; +import { VALUE_MUST_BE_GREATER_THEN_ZEO } from '../../common/translations'; export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) => ({ method: 'GET', @@ -23,19 +24,35 @@ export const createGetDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSer }, }); +const validateCertsValues = (settings: DynamicSettings) => { + const errors: any = {}; + if (settings.certAgeThreshold <= 0) { + errors.certAgeThreshold = VALUE_MUST_BE_GREATER_THEN_ZEO; + } + if (settings.certExpirationThreshold <= 0) { + errors.certExpirationThreshold = VALUE_MUST_BE_GREATER_THEN_ZEO; + } + if (errors.certAgeThreshold || errors.certExpirationThreshold) { + return errors; + } +}; + export const createPostDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) => ({ method: 'POST', path: '/api/uptime/dynamic_settings', validate: { - body: schema.object({}, { unknowns: 'allow' }), + body: schema.object({ + heartbeatIndices: schema.string(), + certAgeThreshold: schema.number(), + certExpirationThreshold: schema.number(), + }), }, writeAccess: true, - options: { - tags: ['access:uptime-write'], - }, handler: async ({ savedObjectsClient }, _context, request, response): Promise => { const decoded = DynamicSettingsType.decode(request.body); - if (isRight(decoded)) { + const certThresholdErrors = validateCertsValues(request.body as DynamicSettings); + + if (isRight(decoded) && !certThresholdErrors) { const newSettings: DynamicSettings = decoded.right; await savedObjectsAdapter.setUptimeDynamicSettings(savedObjectsClient, newSettings); @@ -47,7 +64,7 @@ export const createPostDynamicSettingsRoute: UMRestApiRouteFactory = (libs: UMSe } else { const error = PathReporter.report(decoded).join(', '); return response.badRequest({ - body: error, + body: JSON.stringify(certThresholdErrors) || error, }); } },