Skip to content

Commit 53dbe23

Browse files
author
Raal Goff
committedApr 6, 2022
implemented some requested changes
1 parent a607ab1 commit 53dbe23

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed
 

‎api/api.go

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ type Authority interface {
5050
GetRoots() ([]*x509.Certificate, error)
5151
GetFederation() ([]*x509.Certificate, error)
5252
Version() authority.Version
53-
GenerateCertificateRevocationList() error
5453
GetCertificateRevocationList() ([]byte, error)
5554
}
5655

‎api/crl.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/pem"
55
"fmt"
66
"github.com/pkg/errors"
7+
"github.com/smallstep/certificates/errs"
78
"net/http"
89
)
910

@@ -14,17 +15,16 @@ func (h *caHandler) CRL(w http.ResponseWriter, r *http.Request) {
1415
_, formatAsPEM := r.URL.Query()["pem"]
1516

1617
if err != nil {
17-
w.WriteHeader(500)
18-
_, err = fmt.Fprintf(w, "%v\n", err)
19-
if err != nil {
20-
panic(errors.Wrap(err, "error writing http response"))
18+
19+
caErr, isCaErr := err.(*errs.Error)
20+
21+
if isCaErr {
22+
http.Error(w, caErr.Msg, caErr.Status)
23+
return
2124
}
22-
return
23-
}
2425

25-
if crlBytes == nil {
26-
w.WriteHeader(404)
27-
_, err = fmt.Fprintln(w, "No CRL available")
26+
w.WriteHeader(500)
27+
_, err = fmt.Fprintf(w, "%v\n", err)
2828
if err != nil {
2929
panic(errors.Wrap(err, "error writing http response"))
3030
}

‎authority/authority.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type Authority struct {
6767
sshCAHostFederatedCerts []ssh.PublicKey
6868

6969
// CRL vars
70-
crlChannel chan int
70+
crlTicker *time.Ticker
7171

7272
// Do not re-initialize
7373
initOnce bool
@@ -604,6 +604,10 @@ func (a *Authority) IsAdminAPIEnabled() bool {
604604

605605
// Shutdown safely shuts down any clients, databases, etc. held by the Authority.
606606
func (a *Authority) Shutdown() error {
607+
if a.crlTicker != nil {
608+
a.crlTicker.Stop()
609+
}
610+
607611
if err := a.keyManager.Close(); err != nil {
608612
log.Printf("error closing the key manager: %v", err)
609613
}
@@ -612,6 +616,11 @@ func (a *Authority) Shutdown() error {
612616

613617
// CloseForReload closes internal services, to allow a safe reload.
614618
func (a *Authority) CloseForReload() {
619+
620+
if a.crlTicker != nil {
621+
a.crlTicker.Stop()
622+
}
623+
615624
if err := a.keyManager.Close(); err != nil {
616625
log.Printf("error closing the key manager: %v", err)
617626
}
@@ -686,12 +695,12 @@ func (a *Authority) startCRLGenerator() error {
686695
if tickerDuration <= 0 {
687696
panic(fmt.Sprintf("ERROR: Addition of jitter to CRL generation time %v creates a negative duration (%v). Use a CRL generation time of longer than 1 minute.", a.config.CRL.CacheDuration, tickerDuration))
688697
}
689-
crlTicker := time.NewTicker(tickerDuration)
698+
a.crlTicker = time.NewTicker(tickerDuration)
690699

691700
go func() {
692701
for {
693702
select {
694-
case <-crlTicker.C:
703+
case <-a.crlTicker.C:
695704
log.Println("Regenerating CRL")
696705
err := a.GenerateCertificateRevocationList()
697706
if err != nil {

‎authority/tls.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,15 @@ func (a *Authority) Revoke(ctx context.Context, revokeOpts *RevokeOptions) error
409409
err error
410410
)
411411

412-
// Attempt to get the certificate expiry using the serial number.
413-
cert, err := a.db.GetCertificate(revokeOpts.Serial)
414-
415-
// Revocation of a certificate not in the database may be requested, so fill in the expiry only
416-
// if we can
417-
if err == nil {
418-
rci.ExpiresAt = cert.NotAfter
412+
if revokeOpts.Crt == nil {
413+
// Attempt to get the certificate expiry using the serial number.
414+
cert, err := a.db.GetCertificate(revokeOpts.Serial)
415+
416+
// Revocation of a certificate not in the database may be requested, so fill in the expiry only
417+
// if we can
418+
if err == nil {
419+
rci.ExpiresAt = cert.NotAfter
420+
}
419421
}
420422

421423
// If not mTLS nor ACME, then get the TokenID of the token.

‎db/db.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,15 @@ func (db *DB) GetRevokedCertificates() (*[]RevokedCertificateInfo, error) {
216216
return nil, err
217217
}
218218
var revokedCerts []RevokedCertificateInfo
219+
now := time.Now().UTC()
220+
219221
for _, e := range entries {
220222
var data RevokedCertificateInfo
221223
if err := json.Unmarshal(e.Value, &data); err != nil {
222224
return nil, err
223225
}
224226

225-
if !data.ExpiresAt.IsZero() && data.ExpiresAt.After(time.Now().UTC()) {
227+
if !data.ExpiresAt.IsZero() && data.ExpiresAt.After(now) {
226228
revokedCerts = append(revokedCerts, data)
227229
} else if data.ExpiresAt.IsZero() {
228230
cert, err := db.GetCertificate(data.Serial)
@@ -233,7 +235,7 @@ func (db *DB) GetRevokedCertificates() (*[]RevokedCertificateInfo, error) {
233235
continue
234236
}
235237

236-
if cert.NotAfter.After(time.Now().UTC()) {
238+
if cert.NotAfter.After(now) {
237239
revokedCerts = append(revokedCerts, data)
238240
}
239241
}

0 commit comments

Comments
 (0)