From d2381eb8d29c7ce8a3a8c18271b83f22da58cf6d Mon Sep 17 00:00:00 2001 From: Andrew Cholakian Date: Thu, 14 May 2020 10:17:38 -0500 Subject: [PATCH] [Uptime] Only show ~latest checks on certs page/alert (#66349) * [Uptime] Only show ~latest checks on certs page/alert Fixes https://github.com/elastic/uptime/issues/199 Also fixes some duplicate constants used in the queries here. For default index we just use a literal `0` now since there's no way that'd ever change. * Change window to 5m * Fix dependency issues in FTR suite for certs page. * Ensures tests can run independently, and always navigate to the certs page * Ensures that the not_after field is consistent * Ensures that timespan field is always present in checked documents --- .../plugins/uptime/server/lib/alerts/tls.ts | 6 +- .../lib/requests/__tests__/get_certs.test.ts | 2 +- .../uptime/server/lib/requests/get_certs.ts | 2 +- .../uptime/server/rest_api/certs/certs.ts | 8 +-- .../apis/uptime/rest/helper/make_ping.ts | 7 ++- .../apis/uptime/rest/helper/make_tls.ts | 2 +- .../functional/apps/uptime/certificates.ts | 58 ++++++++++--------- 7 files changed, 45 insertions(+), 40 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/alerts/tls.ts b/x-pack/plugins/uptime/server/lib/alerts/tls.ts index c4464ff575218..6aa9d1aa3c645 100644 --- a/x-pack/plugins/uptime/server/lib/alerts/tls.ts +++ b/x-pack/plugins/uptime/server/lib/alerts/tls.ts @@ -12,12 +12,10 @@ import { updateState } from './common'; import { ACTION_GROUP_DEFINITIONS, DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants'; import { Cert, CertResult } from '../../../common/runtime_types'; import { commonStateTranslations, tlsTranslations } from './translations'; +import { DEFAULT_FROM, DEFAULT_TO } from '../../rest_api/certs/certs'; const { TLS } = ACTION_GROUP_DEFINITIONS; -const DEFAULT_FROM = 'now-1d'; -const DEFAULT_TO = 'now'; -const DEFAULT_INDEX = 0; const DEFAULT_SIZE = 20; interface TlsAlertState { @@ -113,7 +111,7 @@ export const tlsAlertFactory: UptimeAlertTypeFactory = (_server, libs) => ({ dynamicSettings, from: DEFAULT_FROM, to: DEFAULT_TO, - index: DEFAULT_INDEX, + index: 0, size: DEFAULT_SIZE, notValidAfter: `now+${dynamicSettings?.certExpirationThreshold ?? DYNAMIC_SETTINGS_DEFAULTS.certExpirationThreshold}d`, diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_certs.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_certs.test.ts index 689dce98859e1..5fa5c331d398e 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_certs.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_certs.test.ts @@ -180,7 +180,7 @@ describe('getCerts', () => { }, Object { "range": Object { - "@timestamp": Object { + "monitor.timespan": Object { "gte": "now-2d", "lte": "now+1h", }, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_certs.ts b/x-pack/plugins/uptime/server/lib/requests/get_certs.ts index 57a59936ddf7c..4793d420cbfd8 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_certs.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_certs.ts @@ -51,7 +51,7 @@ export const getCerts: UMElasticsearchQueryFn = asyn }, { range: { - '@timestamp': { + 'monitor.timespan': { gte: from, lte: to, }, diff --git a/x-pack/plugins/uptime/server/rest_api/certs/certs.ts b/x-pack/plugins/uptime/server/rest_api/certs/certs.ts index a5ca6e264d299..fb22d603a2d56 100644 --- a/x-pack/plugins/uptime/server/rest_api/certs/certs.ts +++ b/x-pack/plugins/uptime/server/rest_api/certs/certs.ts @@ -9,10 +9,10 @@ import { API_URLS } from '../../../common/constants'; import { UMServerLibs } from '../../lib/lib'; import { UMRestApiRouteFactory } from '../types'; -const DEFAULT_INDEX = 0; +export const DEFAULT_FROM = 'now-5m'; +export const DEFAULT_TO = 'now'; + const DEFAULT_SIZE = 25; -const DEFAULT_FROM = 'now-1d'; -const DEFAULT_TO = 'now'; const DEFAULT_SORT = 'not_after'; const DEFAULT_DIRECTION = 'asc'; @@ -31,7 +31,7 @@ export const createGetCertsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) = }), }, handler: async ({ callES, dynamicSettings }, _context, request, response): Promise => { - const index = request.query?.index ?? DEFAULT_INDEX; + const index = request.query?.index ?? 0; const size = request.query?.size ?? DEFAULT_SIZE; const from = request.query?.from ?? DEFAULT_FROM; const to = request.query?.to ?? DEFAULT_TO; diff --git a/x-pack/test/api_integration/apis/uptime/rest/helper/make_ping.ts b/x-pack/test/api_integration/apis/uptime/rest/helper/make_ping.ts index 908c571e07e06..f9bea050293fc 100644 --- a/x-pack/test/api_integration/apis/uptime/rest/helper/make_ping.ts +++ b/x-pack/test/api_integration/apis/uptime/rest/helper/make_ping.ts @@ -18,6 +18,7 @@ export const makePing = async ( refresh: boolean = true, tls: boolean | TlsProps = false ) => { + const timestamp = new Date(); const baseDoc: any = { tcp: { rtt: { @@ -40,7 +41,7 @@ export const makePing = async ( ephemeral_id: '0d9a8dc6-f604-49e3-86a0-d8f9d6f2cbad', version: '8.0.0', }, - '@timestamp': new Date().toISOString(), + '@timestamp': timestamp.toISOString(), resolve: { rtt: { us: 350, @@ -88,6 +89,10 @@ export const makePing = async ( check_group: uuid.v4(), type: 'http', status: 'up', + timespan: { + gte: timestamp.toISOString(), + lt: new Date(timestamp.getTime() + 5000).toISOString, + }, }, event: { dataset: 'uptime', diff --git a/x-pack/test/api_integration/apis/uptime/rest/helper/make_tls.ts b/x-pack/test/api_integration/apis/uptime/rest/helper/make_tls.ts index 3606462522024..477c9857ca363 100644 --- a/x-pack/test/api_integration/apis/uptime/rest/helper/make_tls.ts +++ b/x-pack/test/api_integration/apis/uptime/rest/helper/make_tls.ts @@ -39,7 +39,7 @@ export const makeTls = ({ valid = true, commonName = '*.elastic.co', expiry, sha server: { x509: { not_before: '2020-03-01T00:00:00.000Z', - not_after: '2020-05-30T12:00:00.000Z', + not_after: expiryDate, issuer: { distinguished_name: 'CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US', diff --git a/x-pack/test/functional/apps/uptime/certificates.ts b/x-pack/test/functional/apps/uptime/certificates.ts index 93a8a852294b2..4a10955637844 100644 --- a/x-pack/test/functional/apps/uptime/certificates.ts +++ b/x-pack/test/functional/apps/uptime/certificates.ts @@ -14,13 +14,9 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { const es = getService('es'); - // FLAKY: https://github.com/elastic/kibana/issues/65010 - describe.skip('certificate page', function() { - before(async () => { - await uptime.goToRoot(true); - }); - + describe('certificates', function() { beforeEach(async () => { + await uptime.goToRoot(true); await makeCheck({ es, tls: true }); await uptimeService.navigation.refreshApp(); }); @@ -30,33 +26,39 @@ export default ({ getPageObjects, getService }: FtrProviderContext) => { await uptimeService.navigation.goToCertificates(); }); - it('displays certificates', async () => { - await uptimeService.cert.hasCertificates(); - }); + describe('page', () => { + beforeEach(async () => { + await uptimeService.navigation.goToCertificates(); + }); - it('displays specific certificates', async () => { - const certId = getSha256(); - const { monitorId } = await makeCheck({ - es, - tls: { - sha256: certId, - }, + it('displays certificates', async () => { + await uptimeService.cert.hasCertificates(); }); - await uptimeService.navigation.refreshApp(); - await uptimeService.cert.certificateExists({ certId, monitorId }); - }); + it('displays specific certificates', async () => { + const certId = getSha256(); + const { monitorId } = await makeCheck({ + es, + tls: { + sha256: certId, + }, + }); - it('performs search against monitor id', async () => { - const certId = getSha256(); - const { monitorId } = await makeCheck({ - es, - tls: { - sha256: certId, - }, + await uptimeService.navigation.refreshApp(); + await uptimeService.cert.certificateExists({ certId, monitorId }); + }); + + it('performs search against monitor id', async () => { + const certId = getSha256(); + const { monitorId } = await makeCheck({ + es, + tls: { + sha256: certId, + }, + }); + await uptimeService.navigation.refreshApp(); + await uptimeService.cert.searchIsWorking(monitorId); }); - await uptimeService.navigation.refreshApp(); - await uptimeService.cert.searchIsWorking(monitorId); }); }); };