211
// it that we would need to check here) since the hostname is known at handshake;
212
// and it is unexpected to switch to internal issuer when the user wants to get
213
// regular certificates on-demand for a class of certs like *.*.tld.
214
- if !certmagic.SubjectIsIP(s) && !certmagic.SubjectIsInternal(s) && (strings.Count(s, "*.") < 2 || ap.OnDemand) {
214
+ if subjectQualifiesForPublicCert(ap, s) {
215
external = append(external, s)
216
} else {
217
internal = append(internal, s)
324
globalPreferredChains := options["preferred_chains"]
325
hasGlobalACMEDefaults := globalEmail != nil || globalACMECA != nil || globalACMECARoot != nil || globalACMEDNS != nil || globalACMEEAB != nil || globalPreferredChains != nil
326
if hasGlobalACMEDefaults {
327
- for _, ap := range tlsApp.Automation.Policies {
328
- if len(ap.Issuers) == 0 {
327
+ // for _, ap := range tlsApp.Automation.Policies {
328
+ for i := 0; i < len(tlsApp.Automation.Policies); i++ {
329
+ ap := tlsApp.Automation.Policies[i]
330
+ if len(ap.Issuers) == 0 && automationPolicyHasAllPublicNames(ap) {
331
+ // for public names, create default issuers which will later be filled in with configured global defaults
332
+ // (internal names will implicitly use the internal issuer at auto-https time)
333
ap.Issuers = caddytls.DefaultIssuers()
334
335
// if a specific endpoint is configured, can't use multiple default issuers
498
})
499
500
emptyAPCount := 0
501
+ origLenAPs := len(aps)
502
// compute the number of empty policies (disregarding subjects) - see #4128
503
emptyAP := new(caddytls.AutomationPolicy)
504
for i := 0; i < len(aps); i++ {
505
emptyAP.Subjects = aps[i].Subjects
506
if reflect.DeepEqual(aps[i], emptyAP) {
507
emptyAPCount++
508
+ if !automationPolicyHasAllPublicNames(aps[i]) {
509
+ // if this automation policy has internal names, we might as well remove it
510
+ // so auto-https can implicitly use the internal issuer
511
+ aps = append(aps[:i], aps[i+1:]...)
512
+ i--
513
+ }
514
}
515
}
516
// If all policies are empty, we can return nil, as there is no need to set any policy
506
- if emptyAPCount == len(aps) {
517
+ if emptyAPCount == origLenAPs {
518
return nil
519
}
520
612
}
613
return -1
614
}
615
+
616
+// subjectQualifiesForPublicCert is like certmagic.SubjectQualifiesForPublicCert() except
617
+// that this allows domains with multiple wildcard levels like '*.*.example.com' to qualify
618
+// if the automation policy has OnDemand enabled (i.e. this function is more lenient).
619
+func subjectQualifiesForPublicCert(ap *caddytls.AutomationPolicy, subj string) bool {
620
+ return !certmagic.SubjectIsIP(subj) &&
621
+ !certmagic.SubjectIsInternal(subj) &&
622
+ (strings.Count(subj, "*.") < 2 || ap.OnDemand)
623
+}
624
+
625
+func automationPolicyHasAllPublicNames(ap *caddytls.AutomationPolicy) bool {
626
+ for _, subj := range ap.Subjects {
627
+ if !subjectQualifiesForPublicCert(ap, subj) {
628
+ return false
629
+ }
630
+ }
631
+ return true
632
+}