Skip to content

Commit

Permalink
Fix busted types.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed May 4, 2020
1 parent 6fb9d3b commit 5902515
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
16 changes: 8 additions & 8 deletions x-pack/plugins/uptime/server/lib/alerts/__tests__/tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ describe('tls alert', () => {
diffSpy = jest.spyOn(moment.prototype, 'diff');
mockCerts = [
{
certificate_not_valid_after: '2020-07-16T03:15:39.000Z',
certificate_not_valid_before: '2019-07-24T03:15:39.000Z',
not_after: '2020-07-16T03:15:39.000Z',
not_before: '2019-07-24T03:15:39.000Z',
common_name: 'Common-One',
monitors: [{ name: 'monitor-one', id: 'monitor1' }],
sha256: 'abc',
},
{
certificate_not_valid_after: '2020-07-18T03:15:39.000Z',
certificate_not_valid_before: '2019-07-20T03:15:39.000Z',
not_after: '2020-07-18T03:15:39.000Z',
not_before: '2019-07-20T03:15:39.000Z',
common_name: 'Common-Two',
monitors: [{ name: 'monitor-two', id: 'monitor2' }],
sha256: 'bcd',
},
{
certificate_not_valid_after: '2020-07-19T03:15:39.000Z',
certificate_not_valid_before: '2019-07-22T03:15:39.000Z',
not_after: '2020-07-19T03:15:39.000Z',
not_before: '2019-07-22T03:15:39.000Z',
common_name: 'Common-Three',
monitors: [{ name: 'monitor-three', id: 'monitor3' }],
sha256: 'cde',
},
{
certificate_not_valid_after: '2020-07-25T03:15:39.000Z',
certificate_not_valid_before: '2019-07-25T03:15:39.000Z',
not_after: '2020-07-25T03:15:39.000Z',
not_before: '2019-07-25T03:15:39.000Z',
common_name: 'Common-Four',
monitors: [{ name: 'monitor-four', id: 'monitor4' }],
sha256: 'def',
Expand Down
32 changes: 15 additions & 17 deletions x-pack/plugins/uptime/server/lib/alerts/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UptimeAlertTypeFactory } from './types';
import { savedObjectsAdapter } from '../saved_objects';
import { updateState } from './common';
import { ACTION_GROUP_DEFINITIONS, DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { Cert } from '../../../common/runtime_types';
import { Cert, CertResult } from '../../../common/runtime_types';
import { commonStateTranslations, tlsTranslations } from './translations';

const { TLS } = ACTION_GROUP_DEFINITIONS;
Expand Down Expand Up @@ -42,15 +42,15 @@ const mapCertsToSummaryString = (
.map(cert => `${cert.common_name}, ${certLimitMessage(cert)}`)
.reduce((prev, cur) => (prev === '' ? cur : prev.concat(`; ${cur}`)), '');

const getValidAfter = ({ certificate_not_valid_after: date }: Cert) => {
const getValidAfter = ({ not_after: date }: Cert) => {
if (!date) return 'Error, missing `certificate_not_valid_after` date.';
const relativeDate = moment().diff(date, 'days');
return relativeDate >= 0
? tlsTranslations.validAfterExpiredString(date, relativeDate)
: tlsTranslations.validAfterExpiringString(date, Math.abs(relativeDate));
};

const getValidBefore = ({ certificate_not_valid_before: date }: Cert): string => {
const getValidBefore = ({ not_before: date }: Cert): string => {
if (!date) return 'Error, missing `certificate_not_valid_before` date.';
const relativeDate = moment().diff(date, 'days');
return relativeDate >= 0
Expand All @@ -64,19 +64,13 @@ export const getCertSummary = (
ageThreshold: number,
maxSummaryItems: number = 3
): TlsAlertState => {
certs.sort((a, b) =>
sortCerts(a.certificate_not_valid_after ?? '', b.certificate_not_valid_after ?? '')
);
certs.sort((a, b) => sortCerts(a.not_after ?? '', b.not_after ?? ''));
const expiring = certs.filter(
cert => new Date(cert.certificate_not_valid_after ?? '').valueOf() < expirationThreshold
cert => new Date(cert.not_after ?? '').valueOf() < expirationThreshold
);

certs.sort((a, b) =>
sortCerts(a.certificate_not_valid_before ?? '', b.certificate_not_valid_before ?? '')
);
const aging = certs.filter(
cert => new Date(cert.certificate_not_valid_before ?? '').valueOf() < ageThreshold
);
certs.sort((a, b) => sortCerts(a.not_before ?? '', b.not_before ?? ''));
const aging = certs.filter(cert => new Date(cert.not_before ?? '').valueOf() < ageThreshold);

return {
count: certs.length,
Expand Down Expand Up @@ -113,7 +107,7 @@ export const tlsAlertFactory: UptimeAlertTypeFactory = (_server, libs) => ({
} = options;
const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);

const certs = await libs.requests.getCerts({
const { certs, total }: CertResult = await libs.requests.getCerts({
callES: callCluster,
dynamicSettings,
from: DEFAULT_FROM,
Expand All @@ -124,9 +118,13 @@ export const tlsAlertFactory: UptimeAlertTypeFactory = (_server, libs) => ({
DYNAMIC_SETTINGS_DEFAULTS.certThresholds?.expiration}d`,
notValidBefore: `now-${dynamicSettings.certThresholds?.age ??
DYNAMIC_SETTINGS_DEFAULTS.certThresholds?.age}d`,
sortBy: '@timestamp',
direction: 'desc',
});

if (certs.length) {
const foundCerts = total > 0;

if (foundCerts) {
const absoluteExpirationThreshold = moment()
.add(
dynamicSettings.certThresholds?.expiration ??
Expand All @@ -143,12 +141,12 @@ export const tlsAlertFactory: UptimeAlertTypeFactory = (_server, libs) => ({
const alertInstance = alertInstanceFactory(TLS.id);
const summary = getCertSummary(certs, absoluteExpirationThreshold, absoluteAgeThreshold);
alertInstance.replaceState({
...updateState(state, certs.length > 0),
...updateState(state, foundCerts),
...summary,
});
alertInstance.scheduleActions(TLS.id);
}

return updateState(state, certs.length > 0);
return updateState(state, foundCerts);
},
});

0 comments on commit 5902515

Please sign in to comment.