diff options
author | Matthew Holt <[email protected]> | 2019-06-19 16:55:56 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-06-19 16:57:45 -0600 |
commit | ad20323b52b4f30449119b3e7b11b36bf0b78628 (patch) | |
tree | a3a750f518a75f7a0f5e0dfd3fa8a538a8163994 | |
parent | 721c100bb080f162d8b03353422b2bf3883e45ca (diff) | |
download | caddy-ad20323b52b4f30449119b3e7b11b36bf0b78628.tar.gz caddy-ad20323b52b4f30449119b3e7b11b36bf0b78628.zip |
Refactor clustering setup code
-rw-r--r-- | caddytls/config.go | 29 | ||||
-rw-r--r-- | caddytls/setup.go | 45 |
2 files changed, 30 insertions, 44 deletions
diff --git a/caddytls/config.go b/caddytls/config.go index ed2b53d4e..7e8b107ff 100644 --- a/caddytls/config.go +++ b/caddytls/config.go @@ -19,8 +19,6 @@ import ( "crypto/x509" "fmt" "io/ioutil" - "os" - "sync/atomic" "time" "github.com/go-acme/lego/challenge/tlsalpn01" @@ -103,31 +101,14 @@ func NewConfig(inst *caddy.Instance) (*Config, error) { certCache, ok := inst.Storage[CertCacheInstStorageKey].(*certmagic.Cache) inst.StorageMu.RUnlock() if !ok || certCache == nil { - // set up the clustering plugin, if there is one (and there should always - // be one since this tls plugin requires it) -- this should be done exactly - // once, but we can't do it during init while plugins are still registering, - // so do it as soon as we run a setup) - if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) { - clusterPluginName := os.Getenv("CADDY_CLUSTERING") - if clusterPluginName == "" { - clusterPluginName = "file" // name of default storage plugin - } - clusterFn, ok := clusterProviders[clusterPluginName] - if ok { - storage, err := clusterFn() - if err != nil { - return nil, fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err) - } - certmagic.Default.Storage = storage - } else { - return nil, fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName) - } + if err := makeClusteringPlugin(); err != nil { + return nil, err } certCache = certmagic.NewCache(certmagic.CacheOptions{ GetConfigForCert: func(cert certmagic.Certificate) (certmagic.Config, error) { - inst.StorageMu.Lock() + inst.StorageMu.RLock() cfgMap, ok := inst.Storage[configMapKey].(map[string]*Config) - inst.StorageMu.Unlock() + inst.StorageMu.RUnlock() if ok { for hostname, cfg := range cfgMap { if cfg.Manager != nil && hostname == cert.Names[0] { @@ -135,8 +116,6 @@ func NewConfig(inst *caddy.Instance) (*Config, error) { } } } - // returning Default not strictly necessary, since Default is used as template - // anyway; but this makes it clear that that's what we fall back to return certmagic.Default, nil }, }) diff --git a/caddytls/setup.go b/caddytls/setup.go index 3f0546904..785905f6b 100644 --- a/caddytls/setup.go +++ b/caddytls/setup.go @@ -50,25 +50,8 @@ func init() { // are specified by the user in the config file. All the automatic HTTPS // stuff comes later outside of this function. func setupTLS(c *caddy.Controller) error { - // set up the clustering plugin, if there is one (and there should always - // be one since this tls plugin requires it) -- this should be done exactly - // once, but we can't do it during init while plugins are still registering, - // so do it as soon as we run a setup) - if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) { - clusterPluginName := os.Getenv("CADDY_CLUSTERING") - if clusterPluginName == "" { - clusterPluginName = "file" // name of default storage plugin - } - clusterFn, ok := clusterProviders[clusterPluginName] - if ok { - storage, err := clusterFn() - if err != nil { - return fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err) - } - certmagic.Default.Storage = storage - } else { - return fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName) - } + if err := makeClusteringPlugin(); err != nil { + return err } configGetter, ok := configGetters[c.ServerType()] @@ -464,6 +447,30 @@ func loadCertsInDir(cfg *Config, c *caddy.Controller, dir string) error { }) } +func makeClusteringPlugin() error { + // set up the clustering plugin, if there is one (and there should always + // be one since this tls plugin requires it) -- this should be done exactly + // once, but we can't do it during init while plugins are still registering, + // so do it as soon as we run a setup) + if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) { + clusterPluginName := os.Getenv("CADDY_CLUSTERING") + if clusterPluginName == "" { + clusterPluginName = "file" // name of default storage plugin + } + clusterFn, ok := clusterProviders[clusterPluginName] + if ok { + storage, err := clusterFn() + if err != nil { + return fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err) + } + certmagic.Default.Storage = storage + } else { + return fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName) + } + } + return nil +} + func constructDefaultClusterPlugin() (certmagic.Storage, error) { return &certmagic.FileStorage{Path: caddy.AssetsPath()}, nil } |