@@ -162,6 +162,14 @@ func (c *Client) getToken() (string, error) {
162
162
// Ensure we have the base URL without trailing slashes
163
163
baseEndpoint := strings .TrimRight (baseURL .String (), "/" )
164
164
165
+ var tlsConfig * tls.Config = nil
166
+ if c .config .TlsClientCert != "" {
167
+ tlsConfig , err = loadClientCertificate (c .config .TlsClientCert )
168
+ if err != nil {
169
+ return "" , fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
170
+ }
171
+ }
172
+
165
173
// If we already have a token, try to use it
166
174
if c .config .Token != "" {
167
175
tokenCheckData := map [string ]interface {}{
@@ -190,11 +198,7 @@ func (c *Client) getToken() (string, error) {
190
198
191
199
// Make the request
192
200
client := & http.Client {}
193
- if c .config .TlsClientCert != "" {
194
- tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
195
- if err != nil {
196
- return "" , fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
197
- }
201
+ if tlsConfig != nil {
198
202
client .Transport = & http.Transport {
199
203
TLSClientConfig : tlsConfig ,
200
204
}
@@ -242,11 +246,7 @@ func (c *Client) getToken() (string, error) {
242
246
243
247
// Make the request
244
248
client := & http.Client {}
245
- if c .config .TlsClientCert != "" {
246
- tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
247
- if err != nil {
248
- return "" , fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
249
- }
249
+ if tlsConfig != nil {
250
250
client .Transport = & http.Transport {
251
251
TLSClientConfig : tlsConfig ,
252
252
}
@@ -329,7 +329,7 @@ func (c *Client) establishConnection() error {
329
329
dialer := websocket .DefaultDialer
330
330
if c .config .TlsClientCert != "" {
331
331
logger .Info ("Adding tls to req" )
332
- tlsConfig , err := LoadClientCertificate (c .config .TlsClientCert )
332
+ tlsConfig , err := loadClientCertificate (c .config .TlsClientCert )
333
333
if err != nil {
334
334
return fmt .Errorf ("failed to load certificate %s: %w" , c .config .TlsClientCert , err )
335
335
}
@@ -395,7 +395,7 @@ func (c *Client) setConnected(status bool) {
395
395
}
396
396
397
397
// LoadClientCertificate Helper method to load client certificates
398
- func LoadClientCertificate (p12Path string ) (* tls.Config , error ) {
398
+ func loadClientCertificate (p12Path string ) (* tls.Config , error ) {
399
399
logger .Info ("Loading tls-client-cert %s" , p12Path )
400
400
// Read the PKCS12 file
401
401
p12Data , err := os .ReadFile (p12Path )
@@ -408,7 +408,7 @@ func LoadClientCertificate(p12Path string) (*tls.Config, error) {
408
408
if err != nil {
409
409
return nil , fmt .Errorf ("failed to decode PKCS12: %w" , err )
410
410
}
411
-
411
+
412
412
// Create certificate
413
413
cert := tls.Certificate {
414
414
Certificate : [][]byte {certificate .Raw },
0 commit comments