fix(prom): api crash when tls cert file non existed

This commit is contained in:
JimMoen 2024-02-28 11:33:27 +08:00
parent 8a204f2ac1
commit 042a84c30f
No known key found for this signature in database
2 changed files with 13 additions and 9 deletions

View File

@ -2,7 +2,7 @@
{application, emqx_prometheus, [
{description, "Prometheus for EMQX"},
% strict semver, bump manually!
{vsn, "5.0.19"},
{vsn, "5.0.20"},
{modules, []},
{registered, [emqx_prometheus_sup]},
{applications, [kernel, stdlib, prometheus, emqx, emqx_auth, emqx_resource, emqx_management]},

View File

@ -883,13 +883,17 @@ gen_point(Type, Name, Path) ->
%% TODO: cert manager for more generic utils functions
cert_expiry_at_from_path(Path0) ->
Path = emqx_schema:naive_env_interpolation(Path0),
{ok, PemBin} = file:read_file(Path),
case file:read_file(Path) of
{ok, PemBin} ->
[CertEntry | _] = public_key:pem_decode(PemBin),
Cert = public_key:pem_entry_decode(CertEntry),
%% TODO: Not fully tested for all certs type
{'utcTime', NotAfterUtc} =
Cert#'Certificate'.'tbsCertificate'#'TBSCertificate'.validity#'Validity'.'notAfter',
utc_time_to_epoch(NotAfterUtc).
utc_time_to_epoch(NotAfterUtc);
{error, _} ->
0
end.
utc_time_to_epoch(UtcTime) ->
date_to_expiry_epoch(utc_time_to_datetime(UtcTime)).
@ -898,7 +902,7 @@ utc_time_to_datetime(Str) ->
{ok, [Year, Month, Day, Hour, Minute, Second], _} = io_lib:fread(
"~2d~2d~2d~2d~2d~2dZ", Str
),
%% Alwoys Assuming YY is in 2000
%% Always Assuming YY is in 2000
{{2000 + Year, Month, Day}, {Hour, Minute, Second}}.
%% 62167219200 =:= calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}).