2019-07-01 01:07:58 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-09-18 01:00:15 +03:00
|
|
|
"sync"
|
2019-06-21 05:36:29 +03:00
|
|
|
"time"
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-07-02 21:37:06 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-09-13 02:31:10 +03:00
|
|
|
"github.com/go-acme/lego/v3/challenge"
|
2019-04-25 22:54:48 +03:00
|
|
|
"github.com/mholt/certmagic"
|
2019-10-28 23:39:37 +03:00
|
|
|
"go.uber.org/zap"
|
2019-04-25 22:54:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 19:46:35 +03:00
|
|
|
caddy.RegisterModule(TLS{})
|
2019-12-10 23:36:46 +03:00
|
|
|
caddy.RegisterModule(AutomateLoader{})
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-12-10 23:36:46 +03:00
|
|
|
// TLS provides TLS facilities including certificate
|
|
|
|
// loading and management, client auth, and more.
|
2019-04-25 22:54:48 +03:00
|
|
|
type TLS struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// Caches certificates in memory for quick use during
|
|
|
|
// TLS handshakes. Each key is the name of a certificate
|
|
|
|
// loader module. All loaded certificates get pooled
|
|
|
|
// into the same cache and may be used to complete TLS
|
|
|
|
// handshakes for the relevant server names (SNI).
|
|
|
|
// Certificates loaded manually (anything other than
|
|
|
|
// "automate") are not automatically managed and will
|
|
|
|
// have to be refreshed manually before they expire.
|
|
|
|
CertificatesRaw caddy.ModuleMap `json:"certificates,omitempty" caddy:"namespace=tls.certificates"`
|
|
|
|
|
|
|
|
// Configures the automation of certificate management.
|
|
|
|
Automation *AutomationConfig `json:"automation,omitempty"`
|
|
|
|
|
|
|
|
// Configures session ticket ephemeral keys (STEKs).
|
|
|
|
SessionTickets *SessionTicketService `json:"session_tickets,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
|
|
|
certificateLoaders []CertificateLoader
|
2019-12-10 23:36:46 +03:00
|
|
|
automateNames []string
|
2019-04-25 22:54:48 +03:00
|
|
|
certCache *certmagic.Cache
|
2019-06-14 20:58:28 +03:00
|
|
|
ctx caddy.Context
|
2019-09-18 01:00:15 +03:00
|
|
|
storageCleanTicker *time.Ticker
|
|
|
|
storageCleanStop chan struct{}
|
2019-10-28 23:39:37 +03:00
|
|
|
logger *zap.Logger
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (TLS) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 23:36:46 +03:00
|
|
|
ID: "tls",
|
|
|
|
New: func() caddy.Module { return new(TLS) },
|
2019-08-21 19:46:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 21:35:39 +03:00
|
|
|
// Provision sets up the configuration for the TLS app.
|
2019-06-14 20:58:28 +03:00
|
|
|
func (t *TLS) Provision(ctx caddy.Context) error {
|
2019-05-17 01:05:38 +03:00
|
|
|
t.ctx = ctx
|
2019-10-28 23:39:37 +03:00
|
|
|
t.logger = ctx.Logger(t)
|
2019-05-17 01:05:38 +03:00
|
|
|
|
2019-09-30 18:07:43 +03:00
|
|
|
// set up a new certificate cache; this (re)loads all certificates
|
|
|
|
cacheOpts := certmagic.CacheOptions{
|
2019-04-25 22:54:48 +03:00
|
|
|
GetConfigForCert: func(cert certmagic.Certificate) (certmagic.Config, error) {
|
|
|
|
return t.getConfigForName(cert.Names[0])
|
|
|
|
},
|
2019-09-30 18:07:43 +03:00
|
|
|
}
|
|
|
|
if t.Automation != nil {
|
|
|
|
cacheOpts.OCSPCheckInterval = time.Duration(t.Automation.OCSPCheckInterval)
|
|
|
|
cacheOpts.RenewCheckInterval = time.Duration(t.Automation.RenewCheckInterval)
|
|
|
|
}
|
|
|
|
t.certCache = certmagic.NewCache(cacheOpts)
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-05-30 08:11:46 +03:00
|
|
|
// automation/management policies
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.Automation != nil {
|
|
|
|
for i, ap := range t.Automation.Policies {
|
2019-12-10 23:36:46 +03:00
|
|
|
val, err := ctx.LoadModule(&ap, "ManagementRaw")
|
2019-09-30 18:07:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading TLS automation management module: %s", err)
|
|
|
|
}
|
|
|
|
t.Automation.Policies[i].Management = val.(ManagerMaker)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// certificate loaders
|
2019-12-10 23:36:46 +03:00
|
|
|
val, err := ctx.LoadModule(t, "CertificatesRaw")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading TLS automation management module: %s", err)
|
|
|
|
}
|
|
|
|
for modName, modIface := range val.(map[string]interface{}) {
|
|
|
|
if modName == "automate" {
|
|
|
|
// special case; these will be loaded in later
|
|
|
|
// using our automation facilities, which we
|
|
|
|
// want to avoid during provisioning
|
|
|
|
var ok bool
|
|
|
|
t.automateNames, ok = modIface.([]string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("loading certificates with 'automate' requires []string, got: %#v", modIface)
|
|
|
|
}
|
|
|
|
continue
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
2019-12-10 23:36:46 +03:00
|
|
|
t.certificateLoaders = append(t.certificateLoaders, modIface.(CertificateLoader))
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 08:11:46 +03:00
|
|
|
// session ticket ephemeral keys (STEK) service and provider
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.SessionTickets != nil {
|
|
|
|
err := t.SessionTickets.provision(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("provisioning session tickets configuration: %v", err)
|
|
|
|
}
|
2019-05-30 08:11:46 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:36:29 +03:00
|
|
|
// on-demand rate limiting
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.Automation != nil && t.Automation.OnDemand != nil && t.Automation.OnDemand.RateLimit != nil {
|
2019-10-21 21:03:51 +03:00
|
|
|
onDemandRateLimiter.SetMaxEvents(t.Automation.OnDemand.RateLimit.Burst)
|
|
|
|
onDemandRateLimiter.SetWindow(time.Duration(t.Automation.OnDemand.RateLimit.Interval))
|
2019-06-21 05:36:29 +03:00
|
|
|
} else {
|
2019-10-21 21:03:51 +03:00
|
|
|
// remove any existing rate limiter
|
|
|
|
onDemandRateLimiter.SetMaxEvents(0)
|
|
|
|
onDemandRateLimiter.SetWindow(0)
|
2019-06-21 05:36:29 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 21:05:47 +03:00
|
|
|
// load manual/static (unmanaged) certificates - we do this in
|
|
|
|
// provision so that other apps (such as http) can know which
|
2019-12-10 23:36:46 +03:00
|
|
|
// certificates have been manually loaded, and also so that
|
|
|
|
// commands like validate can be a better test
|
2019-06-27 01:03:29 +03:00
|
|
|
magic := certmagic.New(t.certCache, certmagic.Config{
|
2019-08-09 21:05:47 +03:00
|
|
|
Storage: ctx.Storage(),
|
2019-06-27 01:03:29 +03:00
|
|
|
})
|
2019-04-25 22:54:48 +03:00
|
|
|
for _, loader := range t.certificateLoaders {
|
|
|
|
certs, err := loader.LoadCertificates()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading certificates: %v", err)
|
|
|
|
}
|
|
|
|
for _, cert := range certs {
|
2019-06-24 21:16:10 +03:00
|
|
|
err := magic.CacheUnmanagedTLSCertificate(cert.Certificate, cert.Tags)
|
2019-04-25 22:54:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("caching unmanaged certificate: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:05:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start activates the TLS module.
|
|
|
|
func (t *TLS) Start() error {
|
2019-12-10 23:36:46 +03:00
|
|
|
// now that we are running, and all manual certificates have
|
|
|
|
// been loaded, time to load the automated/managed certificates
|
|
|
|
err := t.Manage(t.automateNames)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("automate: managing %v: %v", t.automateNames, err)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:00:15 +03:00
|
|
|
t.keepStorageClean()
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the TLS module and cleans up any allocations.
|
|
|
|
func (t *TLS) Stop() error {
|
2019-09-30 18:07:43 +03:00
|
|
|
// stop the storage cleaner goroutine and ticker
|
2019-10-03 08:39:32 +03:00
|
|
|
if t.storageCleanStop != nil {
|
|
|
|
close(t.storageCleanStop)
|
|
|
|
}
|
|
|
|
if t.storageCleanTicker != nil {
|
|
|
|
t.storageCleanTicker.Stop()
|
|
|
|
}
|
2019-09-30 18:07:43 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup frees up resources allocated during Provision.
|
|
|
|
func (t *TLS) Cleanup() error {
|
2019-09-18 01:00:15 +03:00
|
|
|
// stop the certificate cache
|
2019-04-25 22:54:48 +03:00
|
|
|
if t.certCache != nil {
|
|
|
|
t.certCache.Stop()
|
|
|
|
}
|
2019-09-18 01:00:15 +03:00
|
|
|
|
|
|
|
// stop the session ticket rotation goroutine
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.SessionTickets != nil {
|
|
|
|
t.SessionTickets.stop()
|
|
|
|
}
|
2019-09-18 01:00:15 +03:00
|
|
|
|
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-10-17 00:20:27 +03:00
|
|
|
var err error
|
|
|
|
if ap.ManageSync {
|
|
|
|
err = magic.ManageSync([]string{name})
|
|
|
|
} else {
|
|
|
|
err = magic.ManageAsync(t.ctx.Context, []string{name})
|
|
|
|
}
|
2019-04-25 22:54:48 +03:00
|
|
|
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 {
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.Automation != nil {
|
|
|
|
for _, ap := range t.Automation.Policies {
|
|
|
|
if len(ap.Hosts) == 0 {
|
|
|
|
// no host filter is an automatic match
|
2019-04-25 22:54:48 +03:00
|
|
|
return ap
|
|
|
|
}
|
2019-09-30 18:07:43 +03:00
|
|
|
for _, h := range ap.Hosts {
|
|
|
|
if h == name {
|
|
|
|
return ap
|
|
|
|
}
|
|
|
|
}
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// default automation policy
|
2019-09-13 02:31:54 +03:00
|
|
|
return AutomationPolicy{Management: new(ACMEManagerMaker)}
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:00:15 +03:00
|
|
|
// AllMatchingCertificates returns the list of all certificates in
|
2019-09-13 20:46:58 +03:00
|
|
|
// the cache which could be used to satisfy the given SAN.
|
|
|
|
func (t *TLS) AllMatchingCertificates(san string) []certmagic.Certificate {
|
|
|
|
return t.certCache.AllMatchingCertificates(san)
|
2019-08-09 21:05:47 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:00:15 +03:00
|
|
|
// keepStorageClean immediately cleans up all known storage units
|
|
|
|
// if it was not recently done, and starts a goroutine that runs
|
|
|
|
// the operation at every tick from t.storageCleanTicker.
|
|
|
|
func (t *TLS) keepStorageClean() {
|
2019-09-30 18:07:43 +03:00
|
|
|
t.storageCleanTicker = time.NewTicker(storageCleanInterval)
|
|
|
|
t.storageCleanStop = make(chan struct{})
|
2019-09-18 01:00:15 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.storageCleanStop:
|
|
|
|
return
|
|
|
|
case <-t.storageCleanTicker.C:
|
|
|
|
t.cleanStorageUnits()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
t.cleanStorageUnits()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TLS) cleanStorageUnits() {
|
|
|
|
storageCleanMu.Lock()
|
|
|
|
defer storageCleanMu.Unlock()
|
|
|
|
|
|
|
|
if !storageClean.IsZero() && time.Since(storageClean) < storageCleanInterval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
options := certmagic.CleanStorageOptions{
|
|
|
|
OCSPStaples: true,
|
|
|
|
ExpiredCerts: true,
|
|
|
|
ExpiredCertGracePeriod: 24 * time.Hour * 14,
|
|
|
|
}
|
|
|
|
|
|
|
|
// start with the default storage
|
|
|
|
certmagic.CleanStorage(t.ctx.Storage(), options)
|
|
|
|
|
|
|
|
// then clean each storage defined in ACME automation policies
|
2019-09-30 18:07:43 +03:00
|
|
|
if t.Automation != nil {
|
|
|
|
for _, ap := range t.Automation.Policies {
|
|
|
|
if acmeMgmt, ok := ap.Management.(ACMEManagerMaker); ok {
|
|
|
|
if acmeMgmt.storage != nil {
|
|
|
|
certmagic.CleanStorage(acmeMgmt.storage, options)
|
|
|
|
}
|
2019-09-18 01:00:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
storageClean = time.Now()
|
|
|
|
|
2019-10-28 23:39:37 +03:00
|
|
|
t.logger.Info("cleaned up storage units")
|
2019-09-18 01:00:15 +03:00
|
|
|
}
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
// CertificateLoader is a type that can load certificates.
|
2019-06-24 21:16:10 +03:00
|
|
|
// Certificates can optionally be associated with tags.
|
2019-04-25 22:54:48 +03:00
|
|
|
type CertificateLoader interface {
|
2019-06-24 21:16:10 +03:00
|
|
|
LoadCertificates() ([]Certificate, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Certificate is a TLS certificate, optionally
|
|
|
|
// associated with arbitrary tags.
|
|
|
|
type Certificate struct {
|
|
|
|
tls.Certificate
|
|
|
|
Tags []string
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutomationConfig designates configuration for the
|
|
|
|
// construction and use of ACME clients.
|
|
|
|
type AutomationConfig struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// The list of automation policies. The first matching
|
|
|
|
// policy will be applied for a given certificate/name.
|
|
|
|
Policies []AutomationPolicy `json:"policies,omitempty"`
|
|
|
|
|
|
|
|
// On-Demand TLS defers certificate operations to the
|
|
|
|
// moment they are needed, e.g. during a TLS handshake.
|
|
|
|
// Useful when you don't know all the hostnames up front.
|
|
|
|
// Caddy was the first web server to deploy this technology.
|
|
|
|
OnDemand *OnDemandConfig `json:"on_demand,omitempty"`
|
|
|
|
|
|
|
|
// Caddy staples OCSP (and caches the response) for all
|
|
|
|
// qualifying certificates by default. This setting
|
|
|
|
// changes how often it scans responses for freshness,
|
|
|
|
// and updates them if they are getting stale.
|
|
|
|
OCSPCheckInterval caddy.Duration `json:"ocsp_interval,omitempty"`
|
|
|
|
|
|
|
|
// Every so often, Caddy will scan all loaded, managed
|
|
|
|
// certificates for expiration. Certificates which are
|
|
|
|
// about 2/3 into their valid lifetime are due for
|
|
|
|
// renewal. This setting changes how frequently the scan
|
|
|
|
// is performed. If your certificate lifetimes are very
|
|
|
|
// short (less than ~1 week), you should customize this.
|
|
|
|
RenewCheckInterval caddy.Duration `json:"renew_interval,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutomationPolicy designates the policy for automating the
|
2019-12-10 23:36:46 +03:00
|
|
|
// management (obtaining, renewal, and revocation) of managed
|
|
|
|
// TLS certificates.
|
2019-04-25 22:54:48 +03:00
|
|
|
type AutomationPolicy struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// Which hostnames this policy applies to.
|
|
|
|
Hosts []string `json:"hosts,omitempty"`
|
|
|
|
|
|
|
|
// How to manage certificates.
|
|
|
|
ManagementRaw json.RawMessage `json:"management,omitempty" caddy:"namespace=tls.management inline_key=module"`
|
|
|
|
|
|
|
|
// If true, certificate management will be conducted
|
|
|
|
// in the foreground; this will block config reloads
|
|
|
|
// and return errors if there were problems with
|
|
|
|
// obtaining or renewing certificates. This is often
|
|
|
|
// not desirable, especially when serving sites out
|
|
|
|
// of your control. Default: false
|
|
|
|
ManageSync bool `json:"manage_sync,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
Management ManagerMaker `json:"-"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:36:29 +03:00
|
|
|
// makeCertMagicConfig converts ap into a CertMagic config. Passing onDemand
|
|
|
|
// is necessary because the automation policy does not have convenient access
|
|
|
|
// to the TLS app's global on-demand policies;
|
2019-06-14 20:58:28 +03:00
|
|
|
func (ap AutomationPolicy) makeCertMagicConfig(ctx caddy.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-08-21 19:46:35 +03:00
|
|
|
NewManager: ap.Management.NewManager,
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChallengesConfig configures the ACME challenges.
|
|
|
|
type ChallengesConfig struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// HTTP configures the ACME HTTP challenge. This
|
|
|
|
// challenge is enabled and used automatically
|
|
|
|
// and by default.
|
|
|
|
HTTP *HTTPChallengeConfig `json:"http,omitempty"`
|
|
|
|
|
|
|
|
// TLSALPN configures the ACME TLS-ALPN challenge.
|
|
|
|
// This challenge is enabled and used automatically
|
|
|
|
// and by default.
|
2019-09-30 18:07:43 +03:00
|
|
|
TLSALPN *TLSALPNChallengeConfig `json:"tls-alpn,omitempty"`
|
2019-12-10 23:36:46 +03:00
|
|
|
|
|
|
|
// Configures the ACME DNS challenge. Because this
|
|
|
|
// challenge typically requires credentials for
|
|
|
|
// interfacing with a DNS provider, this challenge is
|
|
|
|
// not enabled by default. This is the only challenge
|
|
|
|
// type which does not require a direct connection
|
|
|
|
// to Caddy from an external server.
|
|
|
|
DNSRaw json.RawMessage `json:"dns,omitempty" caddy:"namespace=tls.dns inline_key=provider"`
|
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 {
|
2019-12-10 23:36:46 +03:00
|
|
|
// If true, the HTTP challenge will be disabled.
|
|
|
|
Disabled bool `json:"disabled,omitempty"`
|
|
|
|
|
|
|
|
// An alternate port on which to service this
|
|
|
|
// challenge. Note that the HTTP challenge port is
|
|
|
|
// hard-coded into the spec and cannot be changed,
|
|
|
|
// so you would have to forward packets from the
|
|
|
|
// standard HTTP challenge port to this one.
|
|
|
|
AlternatePort int `json:"alternate_port,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TLSALPNChallengeConfig configures the ACME TLS-ALPN challenge.
|
|
|
|
type TLSALPNChallengeConfig struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// If true, the TLS-ALPN challenge will be disabled.
|
|
|
|
Disabled bool `json:"disabled,omitempty"`
|
|
|
|
|
|
|
|
// An alternate port on which to service this
|
|
|
|
// challenge. Note that the TLS-ALPN challenge port
|
|
|
|
// is hard-coded into the spec and cannot be changed,
|
|
|
|
// so you would have to forward packets from the
|
|
|
|
// standard TLS-ALPN challenge port to this one.
|
|
|
|
AlternatePort int `json:"alternate_port,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnDemandConfig configures on-demand TLS, for obtaining
|
2019-12-10 23:36:46 +03:00
|
|
|
// needed certificates at handshake-time. Because this
|
|
|
|
// feature can easily be abused, you should set up rate
|
|
|
|
// limits and/or an internal endpoint that Caddy can
|
|
|
|
// "ask" if it should be allowed to manage certificates
|
|
|
|
// for a given hostname.
|
2019-04-25 22:54:48 +03:00
|
|
|
type OnDemandConfig struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// An optional rate limit to throttle the
|
|
|
|
// issuance of certificates from handshakes.
|
2019-06-21 05:36:29 +03:00
|
|
|
RateLimit *RateLimit `json:"rate_limit,omitempty"`
|
2019-12-10 23:36:46 +03:00
|
|
|
|
|
|
|
// If Caddy needs to obtain or renew a certificate
|
|
|
|
// during a TLS handshake, it will perform a quick
|
|
|
|
// HTTP request to this URL to check if it should be
|
|
|
|
// allowed to try to get a certificate for the name
|
|
|
|
// in the "domain" query string parameter, like so:
|
|
|
|
// `?domain=example.com`. The endpoint must return a
|
|
|
|
// 200 OK status if a certificate is allowed;
|
|
|
|
// anything else will cause it to be denied.
|
|
|
|
// Redirects are not followed.
|
|
|
|
Ask string `json:"ask,omitempty"`
|
2019-06-21 05:36:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RateLimit specifies an interval with optional burst size.
|
|
|
|
type RateLimit struct {
|
2019-12-10 23:36:46 +03:00
|
|
|
// A duration value. A certificate may be obtained 'burst'
|
|
|
|
// times during this interval.
|
2019-06-21 05:36:29 +03:00
|
|
|
Interval caddy.Duration `json:"interval,omitempty"`
|
2019-12-10 23:36:46 +03:00
|
|
|
|
|
|
|
// How many times during an interval a certificate can be obtained.
|
|
|
|
Burst int `json:"burst,omitempty"`
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 19:46:35 +03:00
|
|
|
// ManagerMaker makes a certificate manager.
|
|
|
|
type ManagerMaker interface {
|
|
|
|
NewManager(interactive bool) (certmagic.Manager, error)
|
2019-04-25 22:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-12-10 23:36:46 +03:00
|
|
|
// AutomateLoader is a no-op certificate loader module
|
|
|
|
// that is treated as a special case: it uses this app's
|
|
|
|
// automation features to load certificates for the
|
|
|
|
// list of hostnames, rather than loading certificates
|
|
|
|
// manually.
|
|
|
|
type AutomateLoader []string
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (AutomateLoader) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
ID: "tls.certificates.automate",
|
|
|
|
New: func() caddy.Module { return new(AutomateLoader) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 05:36:29 +03:00
|
|
|
// These perpetual values are used for on-demand TLS.
|
|
|
|
var (
|
2019-10-21 21:03:51 +03:00
|
|
|
onDemandRateLimiter = certmagic.NewRateLimiter(0, 0)
|
2019-06-21 05:36:29 +03:00
|
|
|
onDemandAskClient = &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return fmt.Errorf("following http redirects is not allowed")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-18 01:00:15 +03:00
|
|
|
// Variables related to storage cleaning.
|
|
|
|
var (
|
|
|
|
storageCleanInterval = 12 * time.Hour
|
|
|
|
|
|
|
|
storageClean time.Time
|
|
|
|
storageCleanMu sync.Mutex
|
|
|
|
)
|
|
|
|
|
2019-09-30 18:07:43 +03:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.App = (*TLS)(nil)
|
|
|
|
_ caddy.Provisioner = (*TLS)(nil)
|
|
|
|
_ caddy.CleanerUpper = (*TLS)(nil)
|
|
|
|
)
|
|
|
|
|
2019-04-25 22:54:48 +03:00
|
|
|
const automateKey = "automate"
|