88 "strings"
99 "sync"
1010
11- "github.com/pion/logging "
11+ "github.com/rs/zerolog "
1212)
1313
1414type CertStore struct {
@@ -17,16 +17,20 @@ type CertStore struct {
1717
1818 storePath string
1919
20- log logging. LeveledLogger
20+ log * zerolog. Logger
2121}
2222
23- func NewCertStore (storePath string ) * CertStore {
23+ func NewCertStore (storePath string , log * zerolog.Logger ) * CertStore {
24+ if log == nil {
25+ log = & defaultLogger
26+ }
27+
2428 return & CertStore {
2529 certificates : make (map [string ]* tls.Certificate ),
2630 certLock : & sync.Mutex {},
2731
2832 storePath : storePath ,
29- log : defaultLogger ,
33+ log : log ,
3034 }
3135}
3236
@@ -42,7 +46,7 @@ func (s *CertStore) ensureStorePath() error {
4246 }
4347
4448 if os .IsNotExist (err ) {
45- s .log .Tracef ("TLS store directory does not exist, creating directory" )
49+ s .log .Trace (). Str ( "path" , s . storePath ). Msg ("TLS store directory does not exist, creating directory" )
4650 err = os .MkdirAll (s .storePath , 0755 )
4751 if err != nil {
4852 return fmt .Errorf ("Failed to create TLS store path: %w" , err )
@@ -56,13 +60,13 @@ func (s *CertStore) ensureStorePath() error {
5660func (s * CertStore ) LoadCertificates () {
5761 err := s .ensureStorePath ()
5862 if err != nil {
59- s .log .Errorf ( err . Error ()) //nolint:errcheck
63+ s .log .Error (). Err ( err ). Msg ( "Failed to ensure store path" )
6064 return
6165 }
6266
6367 files , err := os .ReadDir (s .storePath )
6468 if err != nil {
65- s .log .Errorf ( "Failed to read TLS directory: %v" , err )
69+ s .log .Error (). Err ( err ). Msg ( "Failed to read TLS directory" )
6670 return
6771 }
6872
@@ -86,13 +90,13 @@ func (s *CertStore) loadCertificate(hostname string) {
8690
8791 cert , err := tls .LoadX509KeyPair (crtFile , keyFile )
8892 if err != nil {
89- s .log .Errorf ( " Failed to load certificate for %s: %w" , hostname , err )
93+ s .log .Error (). Err ( err ). Str ( "hostname" , hostname ). Msg ( " Failed to load certificate" )
9094 return
9195 }
9296
9397 s .certificates [hostname ] = & cert
9498
95- s .log .Infof ( "Loaded certificate for %s " , hostname )
99+ s .log .Info (). Str ( "hostname " , hostname ). Msg ( "Loaded certificate" )
96100}
97101
98102// GetCertificate returns the certificate for the given hostname
@@ -119,15 +123,15 @@ func (s *CertStore) ValidateAndSaveCertificate(hostname string, cert string, key
119123 // add recover to avoid panic
120124 defer func () {
121125 if r := recover (); r != nil {
122- s .log .Errorf ( " Failed to verify hostname: %v" , r )
126+ s .log .Error (). Interface ( "recovered" , r ). Msg ( " Failed to verify hostname" )
123127 }
124128 }()
125129
126130 if err = tlsCert .Leaf .VerifyHostname (hostname ); err != nil {
127131 if ! ignoreWarning {
128132 return nil , fmt .Errorf ("Certificate does not match hostname: %w" , err )
129133 }
130- s .log .Warnf ( "Certificate does not match hostname: %v" , err )
134+ s .log .Warn (). Err ( err ). Msg ( "Certificate does not match hostname" )
131135 }
132136 }
133137
@@ -144,28 +148,28 @@ func (s *CertStore) saveCertificate(hostname string) {
144148 // check if certificate already exists
145149 tlsCert := s .certificates [hostname ]
146150 if tlsCert == nil {
147- s .log .Errorf ( " Certificate for %s does not exist, skipping saving certificate", hostname )
151+ s .log .Error (). Str ( "hostname" , hostname ). Msg ( " Certificate for hostname does not exist, skipping saving certificate" )
148152 return
149153 }
150154
151155 err := s .ensureStorePath ()
152156 if err != nil {
153- s .log .Errorf ( err . Error ()) //nolint:errcheck
157+ s .log .Error (). Err ( err ). Msg ( "Failed to ensure store path" )
154158 return
155159 }
156160
157161 keyFile := path .Join (s .storePath , hostname + ".key" )
158162 crtFile := path .Join (s .storePath , hostname + ".crt" )
159163
160164 if err := keyToFile (tlsCert , keyFile ); err != nil {
161- s .log .Errorf ( err . Error () )
165+ s .log .Error (). Err ( err ). Msg ( "Failed to save key file" )
162166 return
163167 }
164168
165169 if err := certToFile (tlsCert , crtFile ); err != nil {
166- s .log .Errorf ( err . Error () )
170+ s .log .Error (). Err ( err ). Msg ( "Failed to save certificate" )
167171 return
168172 }
169173
170- s .log .Infof ( "Saved certificate for %s " , hostname )
174+ s .log .Info (). Str ( "hostname " , hostname ). Msg ( "Saved certificate" )
171175}
0 commit comments