package tlsconf import ( "crypto/x509" "encoding/pem" "os" "github.com/prometheus/client_golang/prometheus" ) // ExpiryCollector publishes when each certificate in a set of files expires. // // Certificates here last a year and nothing renews them; the first sign of one // running out was going to be every agent refusing to connect on the same day. // Read at every scrape rather than once, so a certificate replaced on disk is // seen without a restart. A file that cannot be read publishes nothing, which // is its own thing to notice. func ExpiryCollector(files map[string]string) prometheus.Collector { return &expiry{files: files, desc: prometheus.NewDesc("llmgw_certificate_expiry_timestamp_seconds", "When a certificate this control plane uses expires, as a Unix time.", []string{"cert", "subject"}, nil)} } type expiry struct { files map[string]string desc *prometheus.Desc } func (e *expiry) Describe(ch chan<- *prometheus.Desc) { ch <- e.desc } func (e *expiry) Collect(ch chan<- prometheus.Metric) { for name, path := range e.files { if path == "" { continue } b, err := os.ReadFile(path) //nolint:gosec // configured certificate paths if err != nil { continue } // Every certificate in the file: a CA bundle may hold several. for { var blk *pem.Block blk, b = pem.Decode(b) if blk == nil { break } if blk.Type != "CERTIFICATE" { continue } c, err := x509.ParseCertificate(blk.Bytes) if err != nil { continue } ch <- prometheus.MustNewConstMetric(e.desc, prometheus.GaugeValue, float64(c.NotAfter.Unix()), name, c.Subject.CommonName) } } }