2019-04-25 22:54:48 +03:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2019-06-04 22:52:37 +03:00
|
|
|
"github.com/caddyserver/caddy2"
|
2019-04-25 22:54:48 +03:00
|
|
|
"github.com/go-acme/lego/challenge"
|
|
|
|
"github.com/mholt/certmagic"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy2.RegisterModule(caddy2.Module{
|
|
|
|
Name: "tls",
|
2019-05-21 23:22:21 +03:00
|
|
|
New: func() interface{} { return new(TLS) },
|
2019-04-25 22:54:48 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS represents a process-wide TLS configuration.
|
|
|
|
type TLS struct {
|
2019-05-30 08:11:46 +03:00
|
|
|
Certificates map[string]json.RawMessage `json:"certificates,omitempty"`
|
|
|
|
Automation AutomationConfig `json:"automation,omitempty"`
|
|
|
|
SessionTickets SessionTicketService `json:"session_tickets,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
|
|
|
certificateLoaders []CertificateLoader
|
|
|
|
certCache *certmagic.Cache
|
2019-05-17 01:05:38 +03:00
|
|
|
ctx caddy2.Context
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 21:35:39 +03:00
|
|
|
// Provision sets up the configuration for the TLS app.
|
2019-05-17 01:05:38 +03:00
|
|
|
func (t *TLS) Provision(ctx caddy2.Context) error {
|
|
|
|
t.ctx = ctx
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
// set up the certificate cache
|
|
|
|
// TODO: this makes a new cache every time; better to only make a new
|
|
|
|
// cache (or even better, add/remove only what is necessary) if the
|
|
|
|
// certificates config has been updated
|
|
|
|
t.certCache = certmagic.NewCache(certmagic.CacheOptions{
|
|
|
|
GetConfigForCert: func(cert certmagic.Certificate) (certmagic.Config, error) {
|
|
|
|
return t.getConfigForName(cert.Names[0])
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-05-30 08:11:46 +03:00
|
|
|
// automation/management policies
|
2019-04-25 22:54:48 +03:00
|
|
|
for i, ap := range t.Automation.Policies {
|
2019-06-05 07:43:21 +03:00
|
|
|
val, err := ctx.LoadModuleInline("module", "tls.management", ap.ManagementRaw)
|
2019-04-25 22:54:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading TLS automation management module: %s", err)
|
|
|
|
}
|
2019-06-05 07:43:21 +03:00
|
|
|
t.Automation.Policies[i].Management = val.(managerMaker)
|
|
|
|
t.Automation.Policies[i].ManagementRaw = nil // allow GC to deallocate - TODO: Does this help?
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// certificate loaders
|
|
|
|
for modName, rawMsg := range t.Certificates {
|
|
|
|
if modName == automateKey {
|
|
|
|
continue // special case; these will be loaded in later
|
|
|
|
}
|
2019-05-17 01:05:38 +03:00
|
|
|
val, err := ctx.LoadModule("tls.certificates."+modName, rawMsg)
|
2019-04-25 22:54:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading certificate module '%s': %s", modName, err)
|
|
|
|
}
|
|
|
|
t.certificateLoaders = append(t.certificateLoaders, val.(CertificateLoader))
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:11:46 +03:00
|
|
|
// session ticket ephemeral keys (STEK) service and provider
|
|
|
|
err := t.SessionTickets.provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("provisioning session tickets configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start activates the TLS module.
|
2019-05-17 01:05:38 +03:00
|
|
|
func (t *TLS) Start() error {
|
2019-04-25 22:54:48 +03:00
|
|
|
// load manual/static (unmanaged) certificates
|
|
|
|
for _, loader := range t.certificateLoaders {
|
|
|
|
certs, err := loader.LoadCertificates()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading certificates: %v", err)
|
|
|
|
}
|
|
|
|
magic := certmagic.New(t.certCache, certmagic.Config{
|
2019-05-17 01:05:38 +03:00
|
|
|
Storage: t.ctx.Storage(),
|
2019-04-25 22:54:48 +03:00
|
|
|
})
|
|
|
|
for _, cert := range certs {
|
|
|
|
err := magic.CacheUnmanagedTLSCertificate(cert)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("caching unmanaged certificate: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// load automated (managed) certificates
|
|
|
|
if automatedRawMsg, ok := t.Certificates[automateKey]; ok {
|
|
|
|
var names []string
|
|
|
|
err := json.Unmarshal(automatedRawMsg, &names)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("automate: decoding names: %v", err)
|
|
|
|
}
|
|
|
|
err = t.Manage(names)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("automate: managing %v: %v", names, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Certificates = nil // allow GC to deallocate - TODO: Does this help?
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the TLS module and cleans up any allocations.
|
|
|
|
func (t *TLS) Stop() error {
|
|
|
|
if t.certCache != nil {
|
|
|
|
// TODO: ensure locks are cleaned up too... maybe in certmagic though
|
|
|
|
t.certCache.Stop()
|
|
|
|
}
|
2019-05-30 08:11:46 +03:00
|
|
|
t.SessionTickets.stop()
|
2019-04-25 22:54:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manage immediately begins managing names according to the
|
|
|
|
// matching automation policy.
|
|
|
|
func (t *TLS) Manage(names []string) error {
|
|
|
|
for _, name := range names {
|
|
|
|
ap := t.getAutomationPolicyForName(name)
|
2019-05-17 01:05:38 +03:00
|
|
|
magic := certmagic.New(t.certCache, ap.makeCertMagicConfig(t.ctx))
|
2019-04-25 22:54:48 +03:00
|
|
|
err := magic.Manage([]string{name})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("automate: manage %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleHTTPChallenge ensures that the HTTP challenge is handled for the
|
|
|
|
// certificate named by r.Host, if it is an HTTP challenge request.
|
|
|
|
func (t *TLS) HandleHTTPChallenge(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
if !certmagic.LooksLikeHTTPChallenge(r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ap := t.getAutomationPolicyForName(r.Host)
|
2019-05-17 01:05:38 +03:00
|
|
|
magic := certmagic.New(t.certCache, ap.makeCertMagicConfig(t.ctx))
|
2019-04-25 22:54:48 +03:00
|
|
|
return magic.HandleHTTPChallenge(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLS) getConfigForName(name string) (certmagic.Config, error) {
|
|
|
|
ap := t.getAutomationPolicyForName(name)
|
2019-05-17 01:05:38 +03:00
|
|
|
return ap.makeCertMagicConfig(t.ctx), nil
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLS) getAutomationPolicyForName(name string) AutomationPolicy {
|
|
|
|
for _, ap := range t.Automation.Policies {
|
|
|
|
if len(ap.Hosts) == 0 {
|
|
|
|
// no host filter is an automatic match
|
|
|
|
return ap
|
|
|
|
}
|
|
|
|
for _, h := range ap.Hosts {
|
|
|
|
if h == name {
|
|
|
|
return ap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default automation policy
|
2019-06-05 07:43:21 +03:00
|
|
|
mgmt := new(ACMEManagerMaker)
|
|
|
|
mgmt.SetDefaults()
|
|
|
|
return AutomationPolicy{Management: mgmt}
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CertificateLoader is a type that can load certificates.
|
|
|
|
type CertificateLoader interface {
|
|
|
|
LoadCertificates() ([]tls.Certificate, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutomationConfig designates configuration for the
|
|
|
|
// construction and use of ACME clients.
|
|
|
|
type AutomationConfig struct {
|
|
|
|
Policies []AutomationPolicy `json:"policies,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutomationPolicy designates the policy for automating the
|
|
|
|
// management of managed TLS certificates.
|
|
|
|
type AutomationPolicy struct {
|
2019-06-05 07:43:21 +03:00
|
|
|
Hosts []string `json:"hosts,omitempty"`
|
|
|
|
ManagementRaw json.RawMessage `json:"management"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-06-05 07:43:21 +03:00
|
|
|
Management managerMaker `json:"-"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-05-17 01:05:38 +03:00
|
|
|
func (ap AutomationPolicy) makeCertMagicConfig(ctx caddy2.Context) certmagic.Config {
|
2019-04-26 21:35:39 +03:00
|
|
|
// default manager (ACME) is a special case because of how CertMagic is designed
|
|
|
|
// TODO: refactor certmagic so that ACME manager is not a special case by extracting
|
|
|
|
// its config fields out of the certmagic.Config struct, or something...
|
2019-06-05 07:43:21 +03:00
|
|
|
if acmeMgmt, ok := ap.Management.(*ACMEManagerMaker); ok {
|
2019-05-17 01:05:38 +03:00
|
|
|
return acmeMgmt.makeCertMagicConfig(ctx)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return certmagic.Config{
|
2019-06-05 07:43:21 +03:00
|
|
|
NewManager: ap.Management.newManager,
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChallengesConfig configures the ACME challenges.
|
|
|
|
type ChallengesConfig struct {
|
|
|
|
HTTP HTTPChallengeConfig `json:"http"`
|
|
|
|
TLSALPN TLSALPNChallengeConfig `json:"tls-alpn"`
|
2019-06-05 07:43:21 +03:00
|
|
|
DNSRaw json.RawMessage `json:"dns,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-06-05 07:43:21 +03:00
|
|
|
DNS challenge.Provider `json:"-"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPChallengeConfig configures the ACME HTTP challenge.
|
|
|
|
type HTTPChallengeConfig struct {
|
|
|
|
Disabled bool `json:"disabled,omitempty"`
|
|
|
|
AlternatePort int `json:"alternate_port,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLSALPNChallengeConfig configures the ACME TLS-ALPN challenge.
|
|
|
|
type TLSALPNChallengeConfig struct {
|
|
|
|
Disabled bool `json:"disabled,omitempty"`
|
|
|
|
AlternatePort int `json:"alternate_port,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnDemandConfig configures on-demand TLS, for obtaining
|
|
|
|
// needed certificates at handshake-time.
|
|
|
|
type OnDemandConfig struct {
|
|
|
|
// TODO: MaxCertificates state might not endure reloads...
|
|
|
|
// MaxCertificates int `json:"max_certificates,omitempty"`
|
|
|
|
AskURL string `json:"ask_url,omitempty"`
|
|
|
|
AskStarlark string `json:"ask_starlark,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-06-05 07:43:21 +03:00
|
|
|
// managerMaker makes a certificate manager.
|
|
|
|
type managerMaker interface {
|
2019-04-26 21:35:39 +03:00
|
|
|
newManager(interactive bool) (certmagic.Manager, error)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const automateKey = "automate"
|