Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type APIClient struct {

certHashMonitoring bool
CertHash string
// Cert stores the first seen certificate. This is stable only if cert hash monitoring is enabled.
Cert *x509.Certificate
}

// Session holds the session ID and auth token needed to identify an
Expand Down Expand Up @@ -200,6 +202,12 @@ func setupClientWithConfig(ctx context.Context, config *ClientConfig) (c *APICli
// CertHash is populated once when the client is initialized.
// After that, it should never change. If monitoring is enabled and it changes, an error is thrown
client.CertHash = tlsCertHash(tlsCert)
if tlsCert != nil {
certClone, err := x509.ParseCertificate(tlsCert.Raw)
if err == nil {
client.Cert = certClone
}
}

// Init default settings
if config.AutoExpand && client.Service != nil {
Expand Down
4 changes: 4 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"math/big"
"net/http"
"net/http/httptest"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -269,6 +270,9 @@ func TestCertHashMonitoring(t *testing.T) {
client.HTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = true

ts.TLS.Certificates = []tls.Certificate{genTLSCert(t)}
if ts.Certificate() == nil || client.Cert == nil || !slices.Equal(ts.Certificate().Raw, client.Cert.Raw) {
t.Error("certs not equal")
}

for i := 0; i < 5; i++ {
_, err = ServiceRoot(client)
Expand Down
Loading