mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
fc2ff9155c
- Expose the list of Caddy instances through caddy.Instances() - Added arbitrary storage to caddy.Instance - The cache of loaded certificates is no longer global; now scoped per-instance, meaning upon reload (like SIGUSR1) the old cert cache will be discarded entirely, whereas before, aggressively reloading config that added and removed lots of sites would cause unnecessary build-up in the cache over time. - Key certificates in the cache by their SHA-256 hash instead of by their names. This means certificates will not be duplicated in memory (within each instance), making Caddy much more memory-efficient for large-scale deployments with thousands of sites sharing certs. - Perform name-to-certificate lookups scoped per caddytls.Config instead of a single global lookup. This prevents certificates from stepping on each other when they overlap in their names. - Do not allow TLS configurations keyed by the same hostname to be different; this now throws an error. - Updated relevant tests, with a stark awareness that more tests are needed. - Change the NewContext function signature to include an *Instance. - Strongly recommend (basically require) use of caddytls.NewConfig() to create a new *caddytls.Config, to ensure pointers to the instance certificate cache are initialized properly. - Update the TLS-SNI challenge solver (even though TLS-SNI is disabled currently on the CA side). Store temporary challenge cert in instance cache, but do so directly by the ACME challenge name, not the hash. Modified the getCertificate function to check the cache directly for a name match if one isn't found otherwise. This will allow any caddytls.Config to be able to help solve a TLS-SNI challenge, with one extra side-effect that might actually be kind of interesting (and useless): clients could send a certificate's hash as the SNI and Caddy would be able to serve that certificate for the handshake. - Do not attempt to match a "default" (random) certificate when SNI is present but unrecognized; return no certificate so a TLS alert happens instead. - Store an Instance in the list of instances even while the instance is still starting up (this allows access to the cert cache for performing renewals at startup, etc). Will be removed from list again if instance startup fails. - Laid groundwork for ACMEv2 and Let's Encrypt wildcard support. Server type plugins will need to be updated slightly to accommodate minor adjustments to their API (like passing in an Instance). This commit includes the changes for the HTTP server. Certain Caddyfile configurations might error out with this change, if they configured different TLS settings for the same hostname. This change trades some complexity for other complexity, but ultimately this new complexity is more correct and robust than earlier logic. Fixes #1991 Fixes #1994 Fixes #1303
76 lines
3.1 KiB
Go
76 lines
3.1 KiB
Go
// Copyright 2015 Light Code Labs, LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package caddytls
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetCertificate(t *testing.T) {
|
|
certCache := &certificateCache{cache: make(map[string]Certificate)}
|
|
cfg := &Config{Certificates: make(map[string]string), certCache: certCache}
|
|
|
|
hello := &tls.ClientHelloInfo{ServerName: "example.com"}
|
|
helloSub := &tls.ClientHelloInfo{ServerName: "sub.example.com"}
|
|
helloNoSNI := &tls.ClientHelloInfo{}
|
|
helloNoMatch := &tls.ClientHelloInfo{ServerName: "nomatch"}
|
|
|
|
// When cache is empty
|
|
if cert, err := cfg.GetCertificate(hello); err == nil {
|
|
t.Errorf("GetCertificate should return error when cache is empty, got: %v", cert)
|
|
}
|
|
if cert, err := cfg.GetCertificate(helloNoSNI); err == nil {
|
|
t.Errorf("GetCertificate should return error when cache is empty even if server name is blank, got: %v", cert)
|
|
}
|
|
|
|
// When cache has one certificate in it
|
|
firstCert := Certificate{Names: []string{"example.com"}, Certificate: tls.Certificate{Leaf: &x509.Certificate{DNSNames: []string{"example.com"}}}}
|
|
cfg.cacheCertificate(firstCert)
|
|
if cert, err := cfg.GetCertificate(hello); err != nil {
|
|
t.Errorf("Got an error but shouldn't have, when cert exists in cache: %v", err)
|
|
} else if cert.Leaf.DNSNames[0] != "example.com" {
|
|
t.Errorf("Got wrong certificate with exact match; expected 'example.com', got: %v", cert)
|
|
}
|
|
if _, err := cfg.GetCertificate(helloNoSNI); err != nil {
|
|
t.Errorf("Got an error with no SNI but shouldn't have, when cert exists in cache: %v", err)
|
|
}
|
|
|
|
// When retrieving wildcard certificate
|
|
wildcardCert := Certificate{
|
|
Names: []string{"*.example.com"},
|
|
Certificate: tls.Certificate{Leaf: &x509.Certificate{DNSNames: []string{"*.example.com"}}},
|
|
Hash: "(don't overwrite the first one)",
|
|
}
|
|
cfg.cacheCertificate(wildcardCert)
|
|
if cert, err := cfg.GetCertificate(helloSub); err != nil {
|
|
t.Errorf("Didn't get wildcard cert, got: cert=%v, err=%v ", cert, err)
|
|
} else if cert.Leaf.DNSNames[0] != "*.example.com" {
|
|
t.Errorf("Got wrong certificate, expected wildcard: %v", cert)
|
|
}
|
|
|
|
// When cache is NOT empty but there's no SNI
|
|
if cert, err := cfg.GetCertificate(helloNoSNI); err != nil {
|
|
t.Errorf("Expected random certificate with no error when no SNI, got err: %v", err)
|
|
} else if cert == nil || len(cert.Leaf.DNSNames) == 0 {
|
|
t.Errorf("Expected random cert with no matches, got: %v", cert)
|
|
}
|
|
|
|
// When no certificate matches, raise an alert
|
|
if _, err := cfg.GetCertificate(helloNoMatch); err == nil {
|
|
t.Errorf("Expected an error when no certificate matched the SNI, got: %v", err)
|
|
}
|
|
}
|