Skip to content

Commit d37847a

Browse files
authored
Fix advanced cache initialization in README (#198)
* Fix advanced cache initialization in README As per the documentation of GetConfigForCert: > The returned Config MUST be associated with the same Cache as the caller. A valid Config cannot be constructed with &certmagic.Config{} as the certCache field is unexported. The only way to construct a Config with a non-nil Cache is to use either NewDefault or New. * Make it an error for GetConfigForCert to return Config w/ nil cache This prevents an invalid Config from slipping through and causing a hard to debug nil pointer dereference at some later point.
1 parent 8728b18 commit d37847a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,17 @@ if err != nil {
238238
For more control (particularly, if you need a different way of managing each certificate), you'll make and use a `Cache` and a `Config` like so:
239239

240240
```go
241-
cache := certmagic.NewCache(certmagic.CacheOptions{
241+
// First make a pointer to a Cache as we need to reference the same Cache in
242+
// GetConfigForCert below.
243+
var cache *certmagic.Cache
244+
cache = certmagic.NewCache(certmagic.CacheOptions{
242245
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
243-
// do whatever you need to do to get the right
244-
// configuration for this certificate; keep in
245-
// mind that this config value is used as a
246-
// template, and will be completed with any
247-
// defaults that are set in the Default config
248-
return &certmagic.Config{
246+
// Here we use New to get a valid Config associated with the same cache.
247+
// The provided Config is used as a template and will be completed with
248+
// any defaults that are set in the Default config.
249+
return certmagic.New(cache, &certmagic.config{
249250
// ...
250-
}, nil
251+
}), nil
251252
},
252253
...
253254
})

cache.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ type CacheOptions struct {
145145
// used for managing a certificate, or for accessing
146146
// that certificate's asset storage (e.g. for
147147
// OCSP staples, etc). The returned Config MUST
148-
// be associated with the same Cache as the caller.
148+
// be associated with the same Cache as the caller,
149+
// use New to obtain a valid Config.
149150
//
150151
// The reason this is a callback function, dynamically
151152
// returning a Config (instead of attaching a static
@@ -342,7 +343,11 @@ func (certCache *Cache) getConfig(cert Certificate) (*Config, error) {
342343
if err != nil {
343344
return nil, err
344345
}
345-
if cfg.certCache != nil && cfg.certCache != certCache {
346+
if cfg.certCache == nil {
347+
return nil, fmt.Errorf("config returned for certificate %v has nil cache; expected %p (this one)",
348+
cert.Names, certCache)
349+
}
350+
if cfg.certCache != certCache {
346351
return nil, fmt.Errorf("config returned for certificate %v is not nil and points to different cache; got %p, expected %p (this one)",
347352
cert.Names, cfg.certCache, certCache)
348353
}

0 commit comments

Comments
 (0)