Skip to content

Commit 59bb60e

Browse files
author
Ananth Bhaskararaman
committed
fix: Log cert refresh
1 parent 6f38eff commit 59bb60e

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

bifrost.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package bifrost
33

44
import (
5-
"context"
65
"log/slog"
76
"sync/atomic"
87
)
@@ -26,13 +25,5 @@ func SetLogger(l *slog.Logger) {
2625
}
2726

2827
func init() {
29-
SetLogger(slog.New(discardHandler{}))
28+
SetLogger(slog.New(slog.DiscardHandler))
3029
}
31-
32-
// discardHandler is an [slog.Handler] which is always disabled and therefore logs nothing.
33-
type discardHandler struct{}
34-
35-
func (discardHandler) Enabled(context.Context, slog.Level) bool { return false }
36-
func (discardHandler) Handle(context.Context, slog.Record) error { return nil }
37-
func (d discardHandler) WithAttrs([]slog.Attr) slog.Handler { return d }
38-
func (d discardHandler) WithGroup(string) slog.Handler { return d }

client.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,19 @@ func HTTPClient(
2424
url: caUrl,
2525
privkey: privkey,
2626
}
27-
if _, err := cr.GetClientCertificate(nil); err != nil {
27+
if _, err := cr.getClientCertificate(nil); err != nil {
2828
return nil, err
2929
}
30+
3031
tlsConfig := &tls.Config{
31-
GetClientCertificate: cr.GetClientCertificate,
32+
GetClientCertificate: cr.getClientCertificate,
3233
RootCAs: roots,
3334
KeyLogWriter: ssllog,
3435
}
36+
3537
tlsTransport := http.DefaultTransport.(*http.Transport).Clone()
3638
tlsTransport.TLSClientConfig = tlsConfig
39+
3740
return &http.Client{
3841
Transport: tlsTransport,
3942
}, nil
@@ -45,17 +48,16 @@ type certRefresher struct {
4548
cert atomic.Pointer[Certificate]
4649
}
4750

48-
func (cr *certRefresher) GetClientCertificate(
51+
func (cr *certRefresher) getClientCertificate(
4952
info *tls.CertificateRequestInfo,
5053
) (*tls.Certificate, error) {
5154
ctx := context.Background()
5255
if info != nil {
5356
ctx = info.Context()
5457
}
5558

56-
// If the certificate is nil or is going to expire soon, request a new one.
57-
if cert := cr.cert.Load(); cert == nil ||
58-
cert.NotAfter.Before(time.Now().Add(-time.Minute*10)) {
59+
// If we don't have a certificate or it's about to expire, request a new one.
60+
if cert := cr.cert.Load(); cert == nil || time.Until(cert.NotAfter) < 10*time.Minute {
5961
Logger().DebugContext(ctx, "refreshing client certificate")
6062

6163
cert, err := RequestCertificate(ctx, cr.url, cr.privkey)
@@ -69,7 +71,8 @@ func (cr *certRefresher) GetClientCertificate(
6971
break
7072
}
7173
}
72-
Logger().InfoContext(ctx, "got new client certificate")
74+
Logger().InfoContext(ctx, "got new client certificate",
75+
"namespace", cert.Namespace, "uuid", cert.ID)
7376
}
7477

7578
tlsCert := X509ToTLSCertificate(cr.cert.Load().Certificate, cr.privkey.PrivateKey)

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/RealImage/bifrost
22

3-
go 1.23
4-
5-
toolchain go1.23.0
3+
go 1.24
64

75
require (
86
github.com/VictoriaMetrics/metrics v1.35.1

0 commit comments

Comments
 (0)