|
| 1 | +// Copyright (c) Kurrent, Inc and/or licensed to Kurrent, Inc under one or more agreements. |
| 2 | +// Kurrent, Inc licenses this file to you under the Kurrent License v1 (see LICENSE.md). |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.Metrics; |
| 7 | +using System.Security.Cryptography.X509Certificates; |
| 8 | +using KurrentDB.Core.Certificates; |
| 9 | +using KurrentDB.Core.Time; |
| 10 | + |
| 11 | +namespace KurrentDB.Core.Metrics; |
| 12 | + |
| 13 | +public class CertificatesMetric { |
| 14 | + private readonly Meter _meter; |
| 15 | + private readonly string _name; |
| 16 | + private readonly CertificateProvider _certificateProvider; |
| 17 | + private readonly List<CertificateExpiration> _subMetric= []; |
| 18 | + private readonly IClock _clock; |
| 19 | + |
| 20 | + public static class ChainLevels { |
| 21 | + public const string Node="node"; |
| 22 | + public const string Intermediate = "intermediate"; |
| 23 | + public const string Root = "root"; |
| 24 | + public static readonly string[] All = [Node, Intermediate, Root]; |
| 25 | + } |
| 26 | + public static class Tags { |
| 27 | + public const string ChainLevel = "chain_level"; |
| 28 | + public const string SerialNumber = "serial_number"; |
| 29 | + public const string Thumbprint = "thumbprint"; |
| 30 | + public const string ValidityPeriod = "validity_period"; |
| 31 | + public static readonly string[] All = [ChainLevel, SerialNumber, Thumbprint, ValidityPeriod]; |
| 32 | + } |
| 33 | + public static class ValidityPeriods { |
| 34 | + public const string InPeriod="0"; |
| 35 | + public const string NotValidYet = "1"; |
| 36 | + public const string Expired = "-1"; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + public CertificatesMetric(Meter meter, string name, |
| 42 | + CertificateProvider certificateProvider, |
| 43 | + IClock clock = null) { |
| 44 | + _meter = meter; |
| 45 | + _name = name; |
| 46 | + _certificateProvider = certificateProvider; |
| 47 | + _clock = clock ?? Clock.Instance; |
| 48 | + |
| 49 | + MeasureAllCerts(meter, name, certificateProvider); |
| 50 | + } |
| 51 | + |
| 52 | + public void Measure() { |
| 53 | + foreach (var subMetric in _subMetric) |
| 54 | + subMetric.Measure(); |
| 55 | + } |
| 56 | + |
| 57 | + public void Renewed() => MeasureAllCerts(_meter, _name, _certificateProvider); |
| 58 | + |
| 59 | + private void MeasureAllCerts(Meter meter, string name, CertificateProvider certificateProvider) |
| 60 | + { |
| 61 | + _subMetric.Clear(); |
| 62 | + _subMetric.Add(new CertificateExpiration(certificateProvider.Certificate, ChainLevels.Node, meter, name,_clock)); |
| 63 | + foreach (var intermediate in certificateProvider.IntermediateCerts) |
| 64 | + _subMetric.Add(new CertificateExpiration(intermediate, ChainLevels.Intermediate, meter, name,_clock)); |
| 65 | + |
| 66 | + foreach (var root in certificateProvider.TrustedRootCerts) |
| 67 | + _subMetric.Add(new CertificateExpiration(root, ChainLevels.Root, meter, name,_clock)); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public class CertificateExpiration(X509Certificate2 cert, string type, Meter meter, string name, IClock clock ) { |
| 72 | + |
| 73 | + private readonly Gauge<double> _gauge = meter.CreateGauge<double>(name, "day", "Days before the certificate expires"); |
| 74 | + |
| 75 | + private readonly KeyValuePair<string, object>[] _tags = [ |
| 76 | + new(Tags.ChainLevel, type), |
| 77 | + new(Tags.SerialNumber, cert.GetSerialNumberString()), |
| 78 | + new(Tags.Thumbprint, cert.Thumbprint), |
| 79 | + new(Tags.ValidityPeriod, ValidityPeriod(DateTimeOffset.FromUnixTimeSeconds(clock.SecondsSinceEpoch).DateTime, cert.NotBefore, cert.NotAfter).ToString()) |
| 80 | + |
| 81 | + |
| 82 | + ]; |
| 83 | + |
| 84 | + private static string ValidityPeriod(DateTime now, DateTime notBefore, DateTime notAfter) => |
| 85 | + notBefore <= now && now <= notAfter |
| 86 | + ? ValidityPeriods.InPeriod |
| 87 | + : now <= notBefore |
| 88 | + ? ValidityPeriods.NotValidYet |
| 89 | + : ValidityPeriods.Expired; |
| 90 | + |
| 91 | + public void Measure() { |
| 92 | + var certApocalypseIn = (cert.NotAfter - DateTime.Now).TotalDays; |
| 93 | + _gauge.Record(certApocalypseIn, _tags); |
| 94 | + |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments