20
"fmt"
21
"reflect"
22
"sort"
23
+ "strconv"
24
25
"github.com/caddyserver/caddy/v2"
26
"github.com/caddyserver/caddy/v2/caddyconfig"
27
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp"
28
"github.com/caddyserver/caddy/v2/modules/caddytls"
29
"github.com/caddyserver/certmagic"
30
)
38
tlsApp := &caddytls.TLS{CertificatesRaw: make(caddy.ModuleMap)}
39
var certLoaders []caddytls.CertificateLoader
40
39
- // count how many server blocks have a key with no host,
40
- // and find all hosts that share a server block with a
41
- // hostless key, so that they don't get forgotten/omitted
41
+ httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort)
42
+ if hsp, ok := options["https_port"].(int); ok {
43
+ httpsPort = strconv.Itoa(hsp)
44
+ }
45
+
46
+ // count how many server blocks have a TLS-enabled key with
47
+ // no host, and find all hosts that share a server block with
48
+ // a hostless key, so that they don't get forgotten/omitted
49
// by auto-HTTPS (since they won't appear in route matchers)
43
- var serverBlocksWithHostlessKey int
50
+ var serverBlocksWithTLSHostlessKey int
51
hostsSharedWithHostlessKey := make(map[string]struct{})
52
for _, pair := range pairings {
53
for _, sb := range pair.serverBlocks {
54
for _, addr := range sb.keys {
55
if addr.Host == "" {
49
- serverBlocksWithHostlessKey++
56
+ // this address has no hostname, but if it's explicitly set
57
+ // to HTTPS, then we need to count it as being TLS-enabled
58
+ if addr.Scheme == "https" || addr.Port == httpsPort {
59
+ serverBlocksWithTLSHostlessKey++
60
+ }
61
// this server block has a hostless key, now
62
// go through and add all the hosts to the set
63
for _, otherAddr := range sb.keys {
160
}
161
162
if ap != nil {
152
- // encode issuer now that it's all set up
153
- issuerName := ap.Issuer.(caddy.Module).CaddyModule().ID.Name()
154
- ap.IssuerRaw = caddyconfig.JSONModuleObject(ap.Issuer, "module", issuerName, &warnings)
163
+ if ap.Issuer != nil {
164
+ // encode issuer now that it's all set up
165
+ issuerName := ap.Issuer.(caddy.Module).CaddyModule().ID.Name()
166
+ ap.IssuerRaw = caddyconfig.JSONModuleObject(ap.Issuer, "module", issuerName, &warnings)
167
+ }
168
169
// first make sure this block is allowed to create an automation policy;
170
// doing so is forbidden if it has a key with no host (i.e. ":443")
176
// this is an example of a poor mapping from Caddyfile to JSON but that's
177
// the least-leaky abstraction I could figure out
178
if len(sblockHosts) == 0 {
166
- if serverBlocksWithHostlessKey > 1 {
179
+ if serverBlocksWithTLSHostlessKey > 1 {
180
// this server block and at least one other has a key with no host,
181
// making the two indistinguishable; it is misleading to define such
182
// a policy within one server block since it actually will apply to
183
// others as well
171
- return nil, warnings, fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other server block addresses lacking a host")
184
+ return nil, warnings, fmt.Errorf("cannot make a TLS automation policy from a server block that has a host-less address when there are other TLS-enabled server block addresses lacking a host")
185
}
186
if catchAllAP == nil {
187
// this server block has a key with no hosts, but there is not yet
306
307
// if there is a global/catch-all automation policy, ensure it goes last
308
if catchAllAP != nil {
296
- // first, encode its issuer
297
- issuerName := catchAllAP.Issuer.(caddy.Module).CaddyModule().ID.Name()
298
- catchAllAP.IssuerRaw = caddyconfig.JSONModuleObject(catchAllAP.Issuer, "module", issuerName, &warnings)
309
+ // first, encode its issuer, if there is one
310
+ if catchAllAP.Issuer != nil {
311
+ issuerName := catchAllAP.Issuer.(caddy.Module).CaddyModule().ID.Name()
312
+ catchAllAP.IssuerRaw = caddyconfig.JSONModuleObject(catchAllAP.Issuer, "module", issuerName, &warnings)
313
+ }
314
315
// then append it to the end of the policies list
316
if tlsApp.Automation == nil {