Skip to content

Commit

Permalink
Add CertExpire and extend tlsManager expire if unable to read new cert
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Nov 20, 2020
1 parent 630c746 commit 5e7a9a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
15 changes: 15 additions & 0 deletions test/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,18 @@ func fetchUrls(niterations int) {
func TestFetch(t *testing.T) {
fetchUrls(5)
}

// TestCerts should test certificate manager
func TestCerts(t *testing.T) {
var tlsManager utils.TLSCertsManager
certs, err := tlsManager.GetCerts()
if err != nil {
t.Errorf("Fail TestCerts %v\n", err)
}
notAfter := utils.CertExpire(certs)
ts := time.Now().Add(time.Duration(600 * time.Second))
log.Println("certs expire", notAfter)
if ts.After(notAfter) {
t.Errorf("Fail TestCerts: current certificate expired in 600 seconds\n")
}
}
25 changes: 24 additions & 1 deletion utils/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"bytes"
"container/heap"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -72,12 +73,34 @@ func (t *TLSCertsManager) GetCerts() ([]tls.Certificate, error) {
if err == nil {
t.Certs = certs
} else {
log.Fatal("ERROR ", err.Error())
// to avoid collision between cron obtaining the proxy and
// this code base if we have error we'll increase interval instead of failure
if t.Certs != nil {
ts := time.Now().Add(time.Duration(600 * time.Second))
if CertExpire(t.Certs).After(ts) {
t.Expire = ts
}
} else {
log.Fatal("ERROR ", err.Error())
}
}
}
return t.Certs, nil
}

// CertExpire gets minimum certificate expire from list of certificates
func CertExpire(certs []tls.Certificate) time.Time {
var notAfter time.Time
for _, cert := range certs {
c, e := x509.ParseCertificate(cert.Certificate[0])
if e == nil {
notAfter = c.NotAfter
break
}
}
return notAfter
}

// global TLSCerts manager
var tlsManager TLSCertsManager

Expand Down

0 comments on commit 5e7a9a4

Please sign in to comment.