summaryrefslogtreecommitdiff
path: root/modules/caddytls/connpolicy.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-04-01 20:49:35 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-04-01 20:49:35 -0600
commit6ca5828221b25cb781932836d9c6959af857196c (patch)
tree5c47bfd57d1dfc283a5d2f9d59a666ad272e5975 /modules/caddytls/connpolicy.go
parent6fe04a30b102cc9aaa9d0df717c9fbb73f276139 (diff)
caddytls: Refactor certificate selection policies (close #1575)
Certificate selection used to be a module, but this seems unnecessary, especially since the built-in CustomSelectionPolicy allows quite complex selection logic on a number of fields in certs. If we need to extend that logic, we can, but I don't think there are SO many possibilities that we need modules. This update also allows certificate selection to choose between multiple matching certs based on client compatibility and makes a number of other improvements in the default cert selection logic, both here and in the latest CertMagic. The hardest part of this was the conn policy consolidation logic (Caddyfile only, of course). We have to merge connection policies that we can easily combine, because if two certs are manually loaded in a Caddyfile site block, that produces two connection policies, and each cert is tagged with a different tag, meaning only the first would ever be selected. So given the same matchers, we can merge the two, but this required improving the Tag selection logic to support multiple tags to choose from, hence "tags" changed to "any_tag" or "all_tags" (but we use any_tag in our Caddyfile logic). Combining conn policies with conflicting settings is impossible, so that should return an error if two policies with the exact same matchers have non-empty settings that are not the same (the one exception being any_tag which we can merge because the logic for them is to OR them). It was a bit complicated. It seems to work in numerous tests I've conducted, but we'll see how it pans out in the release candidates.
Diffstat (limited to 'modules/caddytls/connpolicy.go')
-rw-r--r--modules/caddytls/connpolicy.go19
1 files changed, 2 insertions, 17 deletions
diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go
index 52ccdd9..4fd8112 100644
--- a/modules/caddytls/connpolicy.go
+++ b/modules/caddytls/connpolicy.go
@@ -18,12 +18,10 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
- "encoding/json"
"fmt"
"strings"
"github.com/caddyserver/caddy/v2"
- "github.com/caddyserver/certmagic"
"github.com/go-acme/lego/v3/challenge/tlsalpn01"
)
@@ -46,15 +44,6 @@ func (cp ConnectionPolicies) Provision(ctx caddy.Context) error {
cp[i].matchers = append(cp[i].matchers, modIface.(ConnectionMatcher))
}
- // certificate selector
- if pol.CertSelection != nil {
- val, err := ctx.LoadModule(pol, "CertSelection")
- if err != nil {
- return fmt.Errorf("loading certificate selection module: %s", err)
- }
- cp[i].certSelector = val.(certmagic.CertificateSelector)
- }
-
// enable HTTP/2 by default
if len(pol.ALPN) == 0 {
pol.ALPN = append(pol.ALPN, defaultALPN...)
@@ -123,7 +112,7 @@ type ConnectionPolicy struct {
// How to choose a certificate if more than one matched
// the given ServerName (SNI) value.
- CertSelection json.RawMessage `json:"certificate_selection,omitempty" caddy:"namespace=tls.certificate_selection inline_key=policy"`
+ CertSelection *CustomCertSelectionPolicy `json:"certificate_selection,omitempty"`
// The list of cipher suites to support. Caddy's
// defaults are modern and secure.
@@ -151,8 +140,6 @@ type ConnectionPolicy struct {
DefaultSNI string `json:"default_sni,omitempty"`
matchers []ConnectionMatcher
- certSelector certmagic.CertificateSelector
-
stdTLSConfig *tls.Config
}
@@ -184,9 +171,7 @@ func (p *ConnectionPolicy) buildStandardTLSConfig(ctx caddy.Context) error {
// more at handshake-time, but I don't know how to practically pre-build
// a certmagic config for each combination of conn policy + automation policy...
cfg := *tlsApp.getConfigForName(hello.ServerName)
- if p.certSelector != nil {
- cfg.CertSelection = p.certSelector
- }
+ cfg.CertSelection = p.CertSelection
cfg.DefaultServerName = p.DefaultSNI
return cfg.GetCertificate(hello)
},