@@ -42,6 +42,11 @@ type P2PForgeCertMgr struct {
42
42
certCheckMx sync.RWMutex
43
43
}
44
44
45
+ var (
46
+ defaultCertCache * certmagic.Cache
47
+ defaultCertCacheMu sync.Mutex
48
+ )
49
+
45
50
func isRelayAddr (a multiaddr.Multiaddr ) bool {
46
51
found := false
47
52
multiaddr .ForEach (a , func (c multiaddr.Component ) bool {
@@ -182,6 +187,35 @@ func WithLogger(log *zap.SugaredLogger) P2PForgeCertMgrOptions {
182
187
}
183
188
}
184
189
190
+ // newCertmagicConfig is p2p-forge/client-specific version of
191
+ // certmagic.NewDefault() that ensures we have our own cert cache. This is
192
+ // necessary to ensure cert maintenance spawned by NewCache does not share
193
+ // global certmagic.Default.Storage, and certmagic.Default.Logger and uses
194
+ // storage path specific to p2p-forge, and no other instance of certmagic in
195
+ // golang application.
196
+ func newCertmagicConfig (mgrCfg * P2PForgeCertMgrConfig ) * certmagic.Config {
197
+ clog := mgrCfg .log .Desugar ()
198
+
199
+ defaultCertCacheMu .Lock ()
200
+ if defaultCertCache == nil {
201
+ defaultCertCache = certmagic .NewCache (certmagic.CacheOptions {
202
+ GetConfigForCert : func (certmagic.Certificate ) (* certmagic.Config , error ) {
203
+ // default getter that does not depend on certmagic defaults
204
+ // and respects Config.Storage path
205
+ return newCertmagicConfig (mgrCfg ), nil
206
+ },
207
+ Logger : clog ,
208
+ })
209
+ }
210
+ certCache := defaultCertCache
211
+ defaultCertCacheMu .Unlock ()
212
+
213
+ return certmagic .New (certCache , certmagic.Config {
214
+ Storage : mgrCfg .storage ,
215
+ Logger : clog ,
216
+ })
217
+ }
218
+
185
219
// NewP2PForgeCertMgr handles the creation and management of certificates that are automatically granted by a forge
186
220
// to a libp2p host.
187
221
//
@@ -217,9 +251,8 @@ func NewP2PForgeCertMgr(opts ...P2PForgeCertMgrOptions) (*P2PForgeCertMgr, error
217
251
mgrCfg .storage = & certmagic.FileStorage {Path : defaultStorageLocation }
218
252
}
219
253
220
- certCfg := certmagic .NewDefault ()
221
- certCfg .Storage = mgrCfg .storage
222
- certCfg .Logger = mgrCfg .log .Desugar ()
254
+ certCfg := newCertmagicConfig (mgrCfg )
255
+
223
256
hostChan := make (chan host.Host , 1 )
224
257
provideHost := func (host host.Host ) { hostChan <- host }
225
258
hasHostChan := make (chan struct {})
@@ -249,7 +282,9 @@ func NewP2PForgeCertMgr(opts ...P2PForgeCertMgrOptions) (*P2PForgeCertMgr, error
249
282
allowPrivateForgeAddresses : mgrCfg .allowPrivateForgeAddresses ,
250
283
},
251
284
TrustedRoots : mgrCfg .trustedRoots ,
285
+ Logger : certCfg .Logger ,
252
286
})
287
+
253
288
certCfg .Issuers = []certmagic.Issuer {myACME }
254
289
255
290
mgr := & P2PForgeCertMgr {
0 commit comments