forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Uptime] Use async search api for certificates (elastic#111731)
- Loading branch information
Showing
28 changed files
with
433 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
x-pack/plugins/uptime/common/requests/get_certs_request_body.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* 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. | ||
*/ | ||
import { estypes } from '@elastic/elasticsearch'; | ||
import { CertResult, GetCertsParams, Ping } from '../runtime_types'; | ||
import { createEsQuery } from '../utils/es_search'; | ||
|
||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { CertificatesResults } from '../../server/lib/requests/get_certs'; | ||
import { asMutableArray } from '../utils/as_mutable_array'; | ||
|
||
enum SortFields { | ||
'issuer' = 'tls.server.x509.issuer.common_name', | ||
'not_after' = 'tls.server.x509.not_after', | ||
'not_before' = 'tls.server.x509.not_before', | ||
'common_name' = 'tls.server.x509.subject.common_name', | ||
} | ||
|
||
export const DEFAULT_SORT = 'not_after'; | ||
export const DEFAULT_DIRECTION = 'asc'; | ||
export const DEFAULT_SIZE = 20; | ||
export const DEFAULT_FROM = 'now-5m'; | ||
export const DEFAULT_TO = 'now'; | ||
|
||
export const getCertsRequestBody = ({ | ||
pageIndex, | ||
search, | ||
notValidBefore, | ||
notValidAfter, | ||
size = DEFAULT_SIZE, | ||
to = DEFAULT_TO, | ||
from = DEFAULT_FROM, | ||
sortBy = DEFAULT_SORT, | ||
direction = DEFAULT_DIRECTION, | ||
}: GetCertsParams) => { | ||
const sort = SortFields[sortBy as keyof typeof SortFields]; | ||
|
||
const searchRequest = createEsQuery({ | ||
body: { | ||
from: pageIndex * size, | ||
size, | ||
sort: asMutableArray([ | ||
{ | ||
[sort]: { | ||
order: direction, | ||
}, | ||
}, | ||
]), | ||
query: { | ||
bool: { | ||
...(search | ||
? { | ||
minimum_should_match: 1, | ||
should: [ | ||
{ | ||
multi_match: { | ||
query: escape(search), | ||
type: 'phrase_prefix' as const, | ||
fields: [ | ||
'monitor.id.text', | ||
'monitor.name.text', | ||
'url.full.text', | ||
'tls.server.x509.subject.common_name.text', | ||
'tls.server.x509.issuer.common_name.text', | ||
], | ||
}, | ||
}, | ||
], | ||
} | ||
: {}), | ||
filter: [ | ||
{ | ||
exists: { | ||
field: 'tls.server.hash.sha256', | ||
}, | ||
}, | ||
{ | ||
range: { | ||
'monitor.timespan': { | ||
gte: from, | ||
lte: to, | ||
}, | ||
}, | ||
}, | ||
...(notValidBefore | ||
? [ | ||
{ | ||
range: { | ||
'tls.certificate_not_valid_before': { | ||
lte: notValidBefore, | ||
}, | ||
}, | ||
}, | ||
] | ||
: []), | ||
...(notValidAfter | ||
? [ | ||
{ | ||
range: { | ||
'tls.certificate_not_valid_after': { | ||
lte: notValidAfter, | ||
}, | ||
}, | ||
}, | ||
] | ||
: []), | ||
] as estypes.QueryDslQueryContainer, | ||
}, | ||
}, | ||
_source: [ | ||
'monitor.id', | ||
'monitor.name', | ||
'tls.server.x509.issuer.common_name', | ||
'tls.server.x509.subject.common_name', | ||
'tls.server.hash.sha1', | ||
'tls.server.hash.sha256', | ||
'tls.server.x509.not_after', | ||
'tls.server.x509.not_before', | ||
], | ||
collapse: { | ||
field: 'tls.server.hash.sha256', | ||
inner_hits: { | ||
_source: { | ||
includes: ['monitor.id', 'monitor.name', 'url.full'], | ||
}, | ||
collapse: { | ||
field: 'monitor.id', | ||
}, | ||
name: 'monitors', | ||
sort: [{ 'monitor.id': 'asc' as const }], | ||
}, | ||
}, | ||
aggs: { | ||
total: { | ||
cardinality: { | ||
field: 'tls.server.hash.sha256', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return searchRequest.body; | ||
}; | ||
|
||
export const processCertsResult = (result: CertificatesResults): CertResult => { | ||
const certs = result.hits?.hits?.map((hit) => { | ||
const ping = hit._source; | ||
const server = ping.tls?.server!; | ||
|
||
const notAfter = server?.x509?.not_after; | ||
const notBefore = server?.x509?.not_before; | ||
const issuer = server?.x509?.issuer?.common_name; | ||
const commonName = server?.x509?.subject?.common_name; | ||
const sha1 = server?.hash?.sha1; | ||
const sha256 = server?.hash?.sha256; | ||
|
||
const monitors = hit.inner_hits!.monitors.hits.hits.map((monitor) => { | ||
const monitorPing = monitor._source as Ping; | ||
|
||
return { | ||
name: monitorPing?.monitor.name, | ||
id: monitorPing?.monitor.id, | ||
url: monitorPing?.url?.full, | ||
}; | ||
}); | ||
|
||
return { | ||
monitors, | ||
issuer, | ||
sha1, | ||
sha256: sha256 as string, | ||
not_after: notAfter, | ||
not_before: notBefore, | ||
common_name: commonName, | ||
}; | ||
}); | ||
const total = result.aggregations?.total?.value ?? 0; | ||
return { certs, total }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { estypes } from '@elastic/elasticsearch'; | ||
|
||
export function createEsQuery<T extends estypes.SearchRequest>(params: T): T { | ||
return params; | ||
} |
105 changes: 0 additions & 105 deletions
105
...ugins/uptime/public/components/certificates/__snapshots__/certificates_list.test.tsx.snap
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.