Data race fix

This commit is contained in:
Andrey Ermolov 2023-10-04 02:41:59 +00:00
parent f844c8cd88
commit a4da85eed6
1 changed files with 8 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import (
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"os" "os"
"sync"
"time" "time"
) )
@ -78,8 +79,7 @@ func (p *StaticCRLProvider) addCRL(crl *CRL) {
// CRL returns CRL struct if it was previously loaded by calling AddCRL. // CRL returns CRL struct if it was previously loaded by calling AddCRL.
func (p *StaticCRLProvider) CRL(cert *x509.Certificate) (*CRL, error) { func (p *StaticCRLProvider) CRL(cert *x509.Certificate) (*CRL, error) {
key := cert.Issuer.ToRDNSequence().String() return p.crls[cert.Issuer.ToRDNSequence().String()], nil
return p.crls[key], nil
} }
// Options represents a data structure holding a // Options represents a data structure holding a
@ -95,6 +95,7 @@ type Options struct {
type FileWatcherCRLProvider struct { type FileWatcherCRLProvider struct {
crls map[string]*CRL crls map[string]*CRL
opts Options opts Options
mu sync.Mutex
cancel context.CancelFunc cancel context.CancelFunc
} }
@ -225,6 +226,8 @@ func (p *FileWatcherCRLProvider) addCRL(filePath string) error {
} }
certList.RawIssuer = rawCRLIssuer certList.RawIssuer = rawCRLIssuer
key := certList.CertList.Issuer.ToRDNSequence().String() key := certList.CertList.Issuer.ToRDNSequence().String()
p.mu.Lock()
defer p.mu.Unlock()
p.crls[key] = certList p.crls[key] = certList
grpclogLogger.Infof("In-memory CRL storage of FileWatcherCRLProvider for key %v updated", key) grpclogLogger.Infof("In-memory CRL storage of FileWatcherCRLProvider for key %v updated", key)
return nil return nil
@ -233,6 +236,7 @@ func (p *FileWatcherCRLProvider) addCRL(filePath string) error {
// CRL retrieves the CRL associated with the given certificate's issuer DN from // CRL retrieves the CRL associated with the given certificate's issuer DN from
// in-memory if it was previously loaded during CRLDirectory scan. // in-memory if it was previously loaded during CRLDirectory scan.
func (p *FileWatcherCRLProvider) CRL(cert *x509.Certificate) (*CRL, error) { func (p *FileWatcherCRLProvider) CRL(cert *x509.Certificate) (*CRL, error) {
key := cert.Issuer.ToRDNSequence().String() p.mu.Lock()
return p.crls[key], nil defer p.mu.Unlock()
return p.crls[cert.Issuer.ToRDNSequence().String()], nil
} }