Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Fix cert validation #2020

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions pkg/monitors/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package http
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -131,7 +130,7 @@ func (m *Monitor) Configure(conf *Config) (err error) {
}
}
} else {
logger.WithError(err).Error("Failed gathering HTTP stats, ignore other stats")
logger.WithError(err).Error("Failed gathering all HTTP stats, ignore TLS stats and push what we've successfully collected")
}

for i := range dps {
Expand Down Expand Up @@ -214,15 +213,20 @@ func (m *Monitor) getTLSStats(site *url.URL, logger *logrus.Entry) (dps []*datap
serverName = host
}

dimensions := map[string]string{
"server_name": host,
"sni_server_name": serverName,
}

ipConn, err := net.Dial("tcp", host+":"+port)
if err != nil {
logger.WithError(err).Error("connection failed to host during TLS stat collection")
return
}
defer ipConn.Close()

tlsCfg := &tls.Config{
InsecureSkipVerify: m.conf.SkipVerify,
keitwb marked this conversation as resolved.
Show resolved Hide resolved
ServerName: serverName,
ServerName: serverName,
}

if _, err := auth.TLSConfig(tlsCfg, m.conf.CACertPath, m.conf.ClientCertPath, m.conf.ClientKeyPath); err != nil {
Expand All @@ -237,34 +241,11 @@ func (m *Monitor) getTLSStats(site *url.URL, logger *logrus.Entry) (dps []*datap

err = conn.Handshake()
if err != nil {
logger.WithError(err).Error("failed during handshake")
logger.WithError(err).Debug("cert verification failed during handshake")
valid = 0
}

certs := conn.ConnectionState().PeerCertificates
for i, cert := range certs {
opts := x509.VerifyOptions{
Intermediates: x509.NewCertPool(),
}
if i == 0 {
opts.DNSName = serverName
for j, cert := range certs {
if j != 0 {
opts.Intermediates.AddCert(cert)
}
}
secondsLeft = time.Until(cert.NotAfter).Seconds()
}
_, err := cert.Verify(opts)
if err != nil {
logger.WithError(err).Debug("failed verify certificate")
valid = 0
}
}

dimensions := map[string]string{
"server_name": host,
"sni_server_name": serverName,
} else {
cert := conn.ConnectionState().PeerCertificates[0]
secondsLeft = time.Until(cert.NotAfter).Seconds()
}

dps = append(dps,
Expand Down