2015-09-04 14:27:44 +00:00
|
|
|
package main // import "github.com/Luzifer/promcertcheck"
|
2015-09-04 13:36:49 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-05 16:00:16 +00:00
|
|
|
"crypto/tls"
|
2015-09-04 13:36:49 +00:00
|
|
|
"crypto/x509"
|
2017-11-05 16:00:16 +00:00
|
|
|
"errors"
|
2015-09-04 13:36:49 +00:00
|
|
|
"fmt"
|
2017-11-05 16:31:06 +00:00
|
|
|
"io/ioutil"
|
2015-09-04 13:36:49 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-11-05 16:31:06 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-04 13:36:49 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Luzifer/rconfig"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/robfig/cron"
|
2017-11-05 15:03:26 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-09-04 13:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-11-05 16:38:33 +00:00
|
|
|
cfg struct {
|
|
|
|
ExpireWarning time.Duration `flag:"expire-warning" default:"744h" description:"When to warn about a soon expiring certificate"`
|
|
|
|
RootsDir string `flag:"roots-dir" default:"" description:"Directory to load custom RootCA certs from to be trusted (*.pem)"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Verbosity of logs to use (debug, info, warning, error, ...)"`
|
|
|
|
Probes []string `flag:"probe" default:"" description:"URLs to check for certificate issues"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Print program version and exit"`
|
|
|
|
}
|
2017-11-05 16:31:06 +00:00
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
|
2015-09-04 13:36:49 +00:00
|
|
|
probeMonitors = map[string]*probeMonitor{}
|
2017-11-05 16:31:06 +00:00
|
|
|
rootPool *x509.CertPool
|
2017-11-05 16:00:16 +00:00
|
|
|
|
|
|
|
redirectFoundError = errors.New("Found a redirect")
|
2015-09-04 13:36:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type probeMonitor struct {
|
2015-09-04 13:54:08 +00:00
|
|
|
IsValid prometheus.Gauge `json:"-"`
|
|
|
|
Expires prometheus.Gauge `json:"-"`
|
2015-09-04 13:36:49 +00:00
|
|
|
Status probeResult
|
|
|
|
Certificate *x509.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-11-05 16:38:33 +00:00
|
|
|
if err := rconfig.Parse(&cfg); err != nil {
|
2017-06-26 13:04:28 +00:00
|
|
|
log.Fatalf("Unable to parse CLI parameters: %s", err)
|
2015-09-04 13:36:49 +00:00
|
|
|
}
|
2017-11-05 15:03:26 +00:00
|
|
|
|
2017-11-05 16:38:33 +00:00
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("promcertcheck %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel, err := log.ParseLevel(cfg.LogLevel); err == nil {
|
2017-11-05 15:03:26 +00:00
|
|
|
log.SetLevel(logLevel)
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Unable to parse log level: %s", err)
|
|
|
|
}
|
2015-09-04 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.DefaultClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
2017-11-05 16:00:16 +00:00
|
|
|
return redirectFoundError
|
|
|
|
}
|
|
|
|
http.DefaultClient.Transport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
2015-09-04 13:36:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-05 16:31:06 +00:00
|
|
|
var err error
|
|
|
|
if rootPool, err = x509.SystemCertPool(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to load system RootCA pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = loadAdditionalRootCAPool(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Could not load intermediate certificates")
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:36:49 +00:00
|
|
|
registerProbes()
|
|
|
|
refreshCertificateStatus()
|
|
|
|
|
2015-09-04 13:46:43 +00:00
|
|
|
fmt.Printf("PromCertcheck %s...\nStarting to listen on 0.0.0.0:3000\n", version)
|
|
|
|
|
2015-09-04 13:36:49 +00:00
|
|
|
c := cron.New()
|
|
|
|
c.AddFunc("0 0 * * * *", refreshCertificateStatus)
|
|
|
|
c.Start()
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.Handle("/metrics", prometheus.Handler())
|
2015-09-04 13:54:08 +00:00
|
|
|
r.HandleFunc("/", htmlHandler)
|
2017-06-26 13:08:54 +00:00
|
|
|
r.HandleFunc("/httpStatus", httpStatusHandler)
|
2015-09-04 13:54:08 +00:00
|
|
|
r.HandleFunc("/results.json", jsonHandler)
|
2015-09-04 13:36:49 +00:00
|
|
|
http.ListenAndServe(":3000", r)
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:31:06 +00:00
|
|
|
func loadAdditionalRootCAPool() error {
|
2017-11-05 16:38:33 +00:00
|
|
|
if cfg.RootsDir == "" {
|
2017-11-05 16:31:06 +00:00
|
|
|
// Nothing specified, not loading anything but sys certs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-05 16:38:33 +00:00
|
|
|
return filepath.Walk(cfg.RootsDir, func(path string, info os.FileInfo, err error) error {
|
2017-11-05 16:31:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, ".pem") || info.IsDir() {
|
|
|
|
// Likely not a certificate, ignore
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pem, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := rootPool.AppendCertsFromPEM(pem); !ok {
|
|
|
|
return fmt.Errorf("Failed to load certificate %q", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{"path": path}).Debug("Loaded RootCA certificate")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:36:49 +00:00
|
|
|
func registerProbes() {
|
2017-11-05 16:38:33 +00:00
|
|
|
for _, probe := range cfg.Probes {
|
2015-09-04 13:36:49 +00:00
|
|
|
probeURL, _ := url.Parse(probe)
|
|
|
|
|
|
|
|
monitors := &probeMonitor{}
|
|
|
|
monitors.Expires = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "certcheck_expires",
|
|
|
|
Help: "Expiration date in unix timestamp (UTC)",
|
|
|
|
ConstLabels: prometheus.Labels{
|
|
|
|
"host": probeURL.Host,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
monitors.IsValid = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "certcheck_valid",
|
|
|
|
Help: "Validity of the certificate (0/1)",
|
|
|
|
ConstLabels: prometheus.Labels{
|
|
|
|
"host": probeURL.Host,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
prometheus.MustRegister(monitors.Expires)
|
|
|
|
prometheus.MustRegister(monitors.IsValid)
|
|
|
|
|
|
|
|
probeMonitors[probeURL.Host] = monitors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func refreshCertificateStatus() {
|
2017-11-05 16:38:33 +00:00
|
|
|
for _, probe := range cfg.Probes {
|
2015-09-04 13:36:49 +00:00
|
|
|
probeURL, _ := url.Parse(probe)
|
|
|
|
verificationResult, verifyCert := checkCertificate(probeURL)
|
|
|
|
|
2017-11-05 16:38:33 +00:00
|
|
|
probeLog := log.WithFields(log.Fields{
|
|
|
|
"host": probeURL.Host,
|
|
|
|
"result": verificationResult,
|
|
|
|
})
|
|
|
|
if verifyCert != nil {
|
|
|
|
probeLog = probeLog.WithFields(log.Fields{
|
|
|
|
"version": verifyCert.Version,
|
|
|
|
"serial": verifyCert.SerialNumber,
|
|
|
|
"subject": verifyCert.Subject.CommonName,
|
|
|
|
"expires": verifyCert.NotAfter,
|
|
|
|
"issuer": verifyCert.Issuer.CommonName,
|
|
|
|
"alt_names": strings.Join(verifyCert.DNSNames, ", "),
|
|
|
|
})
|
2015-09-04 13:36:49 +00:00
|
|
|
}
|
2017-11-05 16:38:33 +00:00
|
|
|
probeLog.Debug("Probe finished")
|
2015-09-04 13:36:49 +00:00
|
|
|
|
2016-01-25 23:36:08 +00:00
|
|
|
if verifyCert != nil {
|
|
|
|
probeMonitors[probeURL.Host].Expires.Set(float64(verifyCert.NotAfter.UTC().Unix()))
|
|
|
|
}
|
|
|
|
|
2015-09-04 13:36:49 +00:00
|
|
|
switch verificationResult {
|
|
|
|
case certificateExpiresSoon, certificateOK:
|
|
|
|
probeMonitors[probeURL.Host].IsValid.Set(1)
|
|
|
|
case certificateInvalid, certificateNotFound:
|
|
|
|
probeMonitors[probeURL.Host].IsValid.Set(0)
|
|
|
|
default:
|
|
|
|
probeMonitors[probeURL.Host].IsValid.Set(0)
|
|
|
|
}
|
|
|
|
probeMonitors[probeURL.Host].Status = verificationResult
|
|
|
|
probeMonitors[probeURL.Host].Certificate = verifyCert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func inSlice(slice []string, needle string) bool {
|
|
|
|
for _, i := range slice {
|
|
|
|
if i == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|