Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Fix mutex protection on TLS pool (#63)
Browse files Browse the repository at this point in the history
And avoid confusing log if TLS is not required
  • Loading branch information
sergicastro authored Feb 28, 2024
1 parent 090a9bd commit 28ae8dc
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,22 @@ func NewTLSConfigPool(ctx context.Context) TLSConfigPool {

// LoadTLSConfig loads a TLS configuration from the given TLSConfig.
func (p *tlsConfigPool) LoadTLSConfig(config TLSConfig) (*tls.Config, error) {
if config.GetTrustedCertificateAuthority() == "" &&
config.GetTrustedCertificateAuthorityFile() == "" &&
config.GetSkipVerifyPeerCert() == nil {
// no given TLS config, nothing to load
return nil, nil
}

encConfig := encodeConfig(config)
id := encConfig.hash()

p.mu.Lock()
if tlsConfig, ok := p.configs[id]; ok {
p.mu.Unlock()
return tlsConfig, nil
}
p.mu.Unlock()

log := p.log.With("id", id)
log.Info("loading new TLS config", "config", encConfig.JSON())
Expand All @@ -102,10 +113,6 @@ func (p *tlsConfigPool) LoadTLSConfig(config TLSConfig) (*tls.Config, error) {

case config.GetSkipVerifyPeerCert() != nil:
tlsConfig.InsecureSkipVerify = BoolStrValue(config.GetSkipVerifyPeerCert())

default:
// No CA or skip verification, return nil TLS config
return nil, nil
}

// Add the loaded CA to the TLS config
Expand Down

0 comments on commit 28ae8dc

Please sign in to comment.