summaryrefslogtreecommitdiff
path: root/caddyconfig/httpcaddyfile
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2021-01-05 14:39:30 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2021-01-05 14:39:30 -0700
commitf0216967dca12831b1aac351fc8c4bfcea148697 (patch)
tree7e444eec3fd296dd45d27a3e36897b37d2ae1b6d /caddyconfig/httpcaddyfile
parentb1bec8c8992424b423db3d92101bd5b4989dcf65 (diff)
caddyfile: Refactor unmarshaling of module tokens
Eliminates a fair amount of repeated code
Diffstat (limited to 'caddyconfig/httpcaddyfile')
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go54
-rw-r--r--caddyconfig/httpcaddyfile/options.go45
-rw-r--r--caddyconfig/httpcaddyfile/serveroptions.go13
-rw-r--r--caddyconfig/httpcaddyfile/tlsapp.go7
4 files changed, 42 insertions, 77 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go
index 4a0b335..7d16da1 100644
--- a/caddyconfig/httpcaddyfile/builtins.go
+++ b/caddyconfig/httpcaddyfile/builtins.go
@@ -285,21 +285,14 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
return nil, h.ArgErr()
}
modName := h.Val()
- mod, err := caddy.GetModule("tls.issuance." + modName)
- if err != nil {
- return nil, h.Errf("getting issuer module '%s': %v", modName, err)
- }
- unm, ok := mod.New().(caddyfile.Unmarshaler)
- if !ok {
- return nil, h.Errf("issuer module '%s' is not a Caddyfile unmarshaler", mod.ID)
- }
- err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
+ modID := "tls.issuance." + modName
+ unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
if err != nil {
return nil, err
}
issuer, ok := unm.(certmagic.Issuer)
if !ok {
- return nil, h.Errf("module %s is not a certmagic.Issuer", mod.ID)
+ return nil, h.Errf("module %s (%T) is not a certmagic.Issuer", modID, unm)
}
issuers = append(issuers, issuer)
@@ -315,18 +308,12 @@ func parseTLS(h Helper) ([]ConfigValue, error) {
acmeIssuer.Challenges = new(caddytls.ChallengesConfig)
acmeIssuer.Challenges.DNS = new(caddytls.DNSChallengeConfig)
}
- dnsProvModule, err := caddy.GetModule("dns.providers." + provName)
+ modID := "dns.providers." + provName
+ unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
if err != nil {
- return nil, h.Errf("getting DNS provider module named '%s': %v", provName, err)
- }
- dnsProvModuleInstance := dnsProvModule.New()
- if unm, ok := dnsProvModuleInstance.(caddyfile.Unmarshaler); ok {
- err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
- if err != nil {
- return nil, err
- }
+ return nil, err
}
- acmeIssuer.Challenges.DNS.ProviderRaw = caddyconfig.JSONModuleObject(dnsProvModuleInstance, "name", provName, h.warnings)
+ acmeIssuer.Challenges.DNS.ProviderRaw = caddyconfig.JSONModuleObject(unm, "name", provName, h.warnings)
case "ca_root":
arg := h.RemainingArgs()
@@ -583,21 +570,15 @@ func parseLog(h Helper) ([]ConfigValue, error) {
case "discard":
wo = caddy.DiscardWriter{}
default:
- mod, err := caddy.GetModule("caddy.logging.writers." + moduleName)
- if err != nil {
- return nil, h.Errf("getting log writer module named '%s': %v", moduleName, err)
- }
- unm, ok := mod.New().(caddyfile.Unmarshaler)
- if !ok {
- return nil, h.Errf("log writer module '%s' is not a Caddyfile unmarshaler", mod)
- }
- err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
+ modID := "caddy.logging.writers." + moduleName
+ unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID)
if err != nil {
return nil, err
}
+ var ok bool
wo, ok = unm.(caddy.WriterOpener)
if !ok {
- return nil, h.Errf("module %s is not a WriterOpener", mod)
+ return nil, h.Errf("module %s (%T) is not a WriterOpener", modID, unm)
}
}
cl.WriterRaw = caddyconfig.JSONModuleObject(wo, "output", moduleName, h.warnings)
@@ -607,21 +588,14 @@ func parseLog(h Helper) ([]ConfigValue, error) {
return nil, h.ArgErr()
}
moduleName := h.Val()
- mod, err := caddy.GetModule("caddy.logging.encoders." + moduleName)
- if err != nil {
- return nil, h.Errf("getting log encoder module named '%s': %v", moduleName, err)
- }
- unm, ok := mod.New().(caddyfile.Unmarshaler)
- if !ok {
- return nil, h.Errf("log encoder module '%s' is not a Caddyfile unmarshaler", mod)
- }
- err = unm.UnmarshalCaddyfile(h.NewFromNextSegment())
+ moduleID := "caddy.logging.encoders." + moduleName
+ unm, err := caddyfile.UnmarshalModule(h.Dispenser, moduleID)
if err != nil {
return nil, err
}
enc, ok := unm.(zapcore.Encoder)
if !ok {
- return nil, h.Errf("module %s is not a zapcore.Encoder", mod)
+ return nil, h.Errf("module %s (%T) is not a zapcore.Encoder", moduleID, unm)
}
cl.EncoderRaw = caddyconfig.JSONModuleObject(enc, "format", moduleName, h.warnings)
diff --git a/caddyconfig/httpcaddyfile/options.go b/caddyconfig/httpcaddyfile/options.go
index 5001974..3a3cdf6 100644
--- a/caddyconfig/httpcaddyfile/options.go
+++ b/caddyconfig/httpcaddyfile/options.go
@@ -34,7 +34,7 @@ func init() {
RegisterGlobalOption("storage", parseOptStorage)
RegisterGlobalOption("acme_ca", parseOptSingleString)
RegisterGlobalOption("acme_ca_root", parseOptSingleString)
- RegisterGlobalOption("acme_dns", parseOptSingleString)
+ RegisterGlobalOption("acme_dns", parseOptACMEDNS)
RegisterGlobalOption("acme_eab", parseOptACMEEAB)
RegisterGlobalOption("cert_issuer", parseOptCertIssuer)
RegisterGlobalOption("email", parseOptSingleString)
@@ -165,24 +165,35 @@ func parseOptStorage(d *caddyfile.Dispenser) (interface{}, error) {
if !d.Next() { // get storage module name
return nil, d.ArgErr()
}
- modName := d.Val()
- mod, err := caddy.GetModule("caddy.storage." + modName)
+ modID := "caddy.storage." + d.Val()
+ unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
- return nil, d.Errf("getting storage module '%s': %v", modName, err)
+ return nil, err
}
- unm, ok := mod.New().(caddyfile.Unmarshaler)
+ storage, ok := unm.(caddy.StorageConverter)
if !ok {
- return nil, d.Errf("storage module '%s' is not a Caddyfile unmarshaler", mod.ID)
+ return nil, d.Errf("module %s is not a caddy.StorageConverter", modID)
+ }
+ return storage, nil
+}
+
+func parseOptACMEDNS(d *caddyfile.Dispenser) (interface{}, error) {
+ if !d.Next() { // consume option name
+ return nil, d.ArgErr()
}
- err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
+ if !d.Next() { // get DNS module name
+ return nil, d.ArgErr()
+ }
+ modID := "dns.providers." + d.Val()
+ unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
- storage, ok := unm.(caddy.StorageConverter)
+ prov, ok := unm.(certmagic.ACMEDNSProvider)
if !ok {
- return nil, d.Errf("module %s is not a StorageConverter", mod.ID)
+ return nil, d.Errf("module %s (%T) is not a certmagic.ACMEDNSProvider", modID, unm)
}
- return storage, nil
+ return prov, nil
}
func parseOptACMEEAB(d *caddyfile.Dispenser) (interface{}, error) {
@@ -220,22 +231,14 @@ func parseOptCertIssuer(d *caddyfile.Dispenser) (interface{}, error) {
if !d.Next() { // get issuer module name
return nil, d.ArgErr()
}
- modName := d.Val()
- mod, err := caddy.GetModule("tls.issuance." + modName)
- if err != nil {
- return nil, d.Errf("getting issuer module '%s': %v", modName, err)
- }
- unm, ok := mod.New().(caddyfile.Unmarshaler)
- if !ok {
- return nil, d.Errf("issuer module '%s' is not a Caddyfile unmarshaler", mod.ID)
- }
- err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
+ modID := "tls.issuance." + d.Val()
+ unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
iss, ok := unm.(certmagic.Issuer)
if !ok {
- return nil, d.Errf("module %s is not a certmagic.Issuer", mod.ID)
+ return nil, d.Errf("module %s (%T) is not a certmagic.Issuer", modID, unm)
}
return iss, nil
}
diff --git a/caddyconfig/httpcaddyfile/serveroptions.go b/caddyconfig/httpcaddyfile/serveroptions.go
index 38fa0f1..9e94b86 100644
--- a/caddyconfig/httpcaddyfile/serveroptions.go
+++ b/caddyconfig/httpcaddyfile/serveroptions.go
@@ -57,21 +57,14 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (interface{}, error
switch d.Val() {
case "listener_wrappers":
for nesting := d.Nesting(); d.NextBlock(nesting); {
- mod, err := caddy.GetModule("caddy.listeners." + d.Val())
- if err != nil {
- return nil, fmt.Errorf("finding listener module '%s': %v", d.Val(), err)
- }
- unm, ok := mod.New().(caddyfile.Unmarshaler)
- if !ok {
- return nil, fmt.Errorf("listener module '%s' is not a Caddyfile unmarshaler", mod)
- }
- err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
+ modID := "caddy.listeners." + d.Val()
+ unm, err := caddyfile.UnmarshalModule(d, modID)
if err != nil {
return nil, err
}
listenerWrapper, ok := unm.(caddy.ListenerWrapper)
if !ok {
- return nil, fmt.Errorf("module %s is not a listener wrapper", mod)
+ return nil, fmt.Errorf("module %s (%T) is not a listener wrapper", modID, unm)
}
jsonListenerWrapper := caddyconfig.JSONModuleObject(
listenerWrapper,
diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go
index 1fabc45..440c447 100644
--- a/caddyconfig/httpcaddyfile/tlsapp.go
+++ b/caddyconfig/httpcaddyfile/tlsapp.go
@@ -392,14 +392,9 @@ func fillInGlobalACMEDefaults(issuer certmagic.Issuer, options map[string]interf
acmeIssuer.TrustedRootsPEMFiles = append(acmeIssuer.TrustedRootsPEMFiles, globalACMECARoot.(string))
}
if globalACMEDNS != nil && (acmeIssuer.Challenges == nil || acmeIssuer.Challenges.DNS == nil) {
- provName := globalACMEDNS.(string)
- dnsProvModule, err := caddy.GetModule("dns.providers." + provName)
- if err != nil {
- return fmt.Errorf("getting DNS provider module named '%s': %v", provName, err)
- }
acmeIssuer.Challenges = &caddytls.ChallengesConfig{
DNS: &caddytls.DNSChallengeConfig{
- ProviderRaw: caddyconfig.JSONModuleObject(dnsProvModule.New(), "name", provName, nil),
+ ProviderRaw: caddyconfig.JSONModuleObject(globalACMEDNS, "name", globalACMEDNS.(caddy.Module).CaddyModule().ID.Name(), nil),
},
}
}