2020-03-18 11:00:45 +08: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.
package httpcaddyfile
import (
"bytes"
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
"encoding/json"
2020-03-18 11:00:45 +08:00
"fmt"
"reflect"
"sort"
2020-04-25 08:58:28 +08:00
"strconv"
caddytls: Add support for ZeroSSL; add Caddyfile support for issuers (#3633)
* caddytls: Add support for ZeroSSL; add Caddyfile support for issuers
Configuring issuers explicitly in a Caddyfile is not easily compatible
with existing ACME-specific parameters such as email or acme_ca which
infer the kind of issuer it creates (this is complicated now because
the ZeroSSL issuer wraps the ACME issuer)... oh well, we can revisit
that later if we need to.
New Caddyfile global option:
{
cert_issuer <name> ...
}
Or, alternatively, as a tls subdirective:
tls {
issuer <name> ...
}
For example, to use ZeroSSL with an API key:
{
cert_issuser zerossl API_KEY
}
For now, that still uses ZeroSSL's ACME endpoint; it fetches EAB
credentials for you. You can also provide the EAB credentials directly
just like any other ACME endpoint:
{
cert_issuer acme {
eab KEY_ID MAC_KEY
}
}
All these examples use the new global option (or tls subdirective). You
can still use traditional/existing options with ZeroSSL, since it's
just another ACME endpoint:
{
acme_ca https://acme.zerossl.com/v2/DV90
acme_eab KEY_ID MAC_KEY
}
That's all there is to it. You just can't mix-and-match acme_* options
with cert_issuer, because it becomes confusing/ambiguous/complicated to
merge the settings.
* Fix broken test
This test was asserting buggy behavior, oops - glad this branch both
discovers and fixes the bug at the same time!
* Fix broken test (post-merge)
* Update modules/caddytls/acmeissuer.go
Fix godoc comment
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
* Add support for ZeroSSL's EAB-by-email endpoint
Also transform the ACMEIssuer into ZeroSSLIssuer implicitly if set to
the ZeroSSL endpoint without EAB (the ZeroSSLIssuer is needed to
generate EAB if not already provided); this is now possible with either
an API key or an email address.
* go.mod: Use latest certmagic, acmez, and x/net
* Wrap underlying logic rather than repeating it
Oops, duh
* Form-encode email info into request body for EAB endpoint
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
2020-08-11 22:58:06 +08:00
"strings"
2020-03-18 11:00:45 +08:00
2023-08-14 23:41:15 +08:00
"github.com/caddyserver/certmagic"
"github.com/mholt/acmez/acme"
2020-03-18 11:00:45 +08:00
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
2020-04-25 08:58:28 +08:00
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
2020-03-18 11:00:45 +08:00
"github.com/caddyserver/caddy/v2/modules/caddytls"
)
func ( st ServerType ) buildTLSApp (
pairings [ ] sbAddrAssociation ,
2022-08-03 04:39:09 +08:00
options map [ string ] any ,
2020-03-18 11:00:45 +08:00
warnings [ ] caddyconfig . Warning ,
) ( * caddytls . TLS , [ ] caddyconfig . Warning , error ) {
tlsApp := & caddytls . TLS { CertificatesRaw : make ( caddy . ModuleMap ) }
var certLoaders [ ] caddytls . CertificateLoader
2021-01-20 05:16:06 +08:00
httpPort := strconv . Itoa ( caddyhttp . DefaultHTTPPort )
if hp , ok := options [ "http_port" ] . ( int ) ; ok {
httpPort = strconv . Itoa ( hp )
}
2022-10-05 10:58:19 +08:00
autoHTTPS := "on"
if ah , ok := options [ "auto_https" ] . ( string ) ; ok {
autoHTTPS = ah
}
2020-04-25 08:58:28 +08:00
2022-10-14 01:30:57 +08:00
// find all hosts that share a server block with a hostless
// key, so that they don't get forgotten/omitted by auto-HTTPS
// (since they won't appear in route matchers)
2021-04-17 01:17:15 +08:00
httpsHostsSharedWithHostlessKey := make ( map [ string ] struct { } )
2022-10-14 01:30:57 +08:00
if autoHTTPS != "off" {
for _ , pair := range pairings {
for _ , sb := range pair . serverBlocks {
for _ , addr := range sb . keys {
if addr . Host == "" {
// this server block has a hostless key, now
// go through and add all the hosts to the set
for _ , otherAddr := range sb . keys {
if otherAddr . Original == addr . Original {
continue
}
if otherAddr . Host != "" && otherAddr . Scheme != "http" && otherAddr . Port != httpPort {
httpsHostsSharedWithHostlessKey [ otherAddr . Host ] = struct { } { }
}
2020-03-18 11:00:45 +08:00
}
2022-10-14 01:30:57 +08:00
break
2020-03-18 11:00:45 +08:00
}
}
}
}
}
2020-10-29 10:36:00 +08:00
// a catch-all automation policy is used as a "default" for all subjects that
// don't have custom configuration explicitly associated with them; this
// is only to add if the global settings or defaults are non-empty
2020-03-18 11:00:45 +08:00
catchAllAP , err := newBaseAutomationPolicy ( options , warnings , false )
if err != nil {
return nil , warnings , err
}
2020-10-29 10:36:00 +08:00
if catchAllAP != nil {
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . Policies = append ( tlsApp . Automation . Policies , catchAllAP )
}
2020-03-18 11:00:45 +08:00
for _ , p := range pairings {
2021-01-20 05:16:06 +08:00
// avoid setting up TLS automation policies for a server that is HTTP-only
if ! listenersUseAnyPortOtherThan ( p . addresses , httpPort ) {
continue
}
2020-03-18 11:00:45 +08:00
for _ , sblock := range p . serverBlocks {
2022-03-25 12:54:03 +08:00
// check the scheme of all the site addresses,
// skip building AP if they all had http://
if sblock . isAllHTTP ( ) {
continue
}
2020-03-18 11:00:45 +08:00
// get values that populate an automation policy for this block
2020-10-29 10:36:00 +08:00
ap , err := newBaseAutomationPolicy ( options , warnings , true )
if err != nil {
return nil , warnings , err
}
2020-03-18 11:00:45 +08:00
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
sblockHosts := sblock . hostsFromKeys ( false )
2020-10-29 10:36:00 +08:00
if len ( sblockHosts ) == 0 && catchAllAP != nil {
2020-03-18 11:00:45 +08:00
ap = catchAllAP
}
// on-demand tls
if _ , ok := sblock . pile [ "tls.on_demand" ] ; ok {
ap . OnDemand = true
}
2021-01-07 03:02:58 +08:00
if keyTypeVals , ok := sblock . pile [ "tls.key_type" ] ; ok {
ap . KeyType = keyTypeVals [ 0 ] . Value . ( string )
}
2020-03-18 11:00:45 +08:00
// certificate issuers
if issuerVals , ok := sblock . pile [ "tls.cert_issuer" ] ; ok {
2020-11-17 02:05:55 +08:00
var issuers [ ] certmagic . Issuer
2020-03-18 11:00:45 +08:00
for _ , issuerVal := range issuerVals {
2021-02-09 02:06:19 +08:00
issuers = append ( issuers , issuerVal . Value . ( certmagic . Issuer ) )
2020-11-17 02:05:55 +08:00
}
if ap == catchAllAP && ! reflect . DeepEqual ( ap . Issuers , issuers ) {
2022-10-14 01:30:57 +08:00
// this more correctly implements an error check that was removed
// below; try it with this config:
//
// :443 {
// bind 127.0.0.1
// }
//
// :443 {
// bind ::1
// tls {
// issuer acme
// }
// }
2020-11-17 02:05:55 +08:00
return nil , warnings , fmt . Errorf ( "automation policy from site block is also default/catch-all policy because of key without hostname, and the two are in conflict: %#v != %#v" , ap . Issuers , issuers )
2020-03-18 11:00:45 +08:00
}
2021-02-09 02:06:19 +08:00
ap . Issuers = issuers
2020-03-18 11:00:45 +08:00
}
2022-02-18 06:40:34 +08:00
// certificate managers
if certManagerVals , ok := sblock . pile [ "tls.cert_manager" ] ; ok {
for _ , certManager := range certManagerVals {
certGetterName := certManager . Value . ( caddy . Module ) . CaddyModule ( ) . ID . Name ( )
ap . ManagersRaw = append ( ap . ManagersRaw , caddyconfig . JSONModuleObject ( certManager . Value , "via" , certGetterName , & warnings ) )
}
}
2020-04-07 02:24:35 +08:00
// custom bind host
for _ , cfgVal := range sblock . pile [ "bind" ] {
2020-11-17 02:05:55 +08:00
for _ , iss := range ap . Issuers {
// if an issuer was already configured and it is NOT an ACME issuer,
// skip, since we intend to adjust only ACME issuers; ensure we
// include any issuer that embeds/wraps an underlying ACME issuer
var acmeIssuer * caddytls . ACMEIssuer
if acmeWrapper , ok := iss . ( acmeCapable ) ; ok {
caddytls: Add support for ZeroSSL; add Caddyfile support for issuers (#3633)
* caddytls: Add support for ZeroSSL; add Caddyfile support for issuers
Configuring issuers explicitly in a Caddyfile is not easily compatible
with existing ACME-specific parameters such as email or acme_ca which
infer the kind of issuer it creates (this is complicated now because
the ZeroSSL issuer wraps the ACME issuer)... oh well, we can revisit
that later if we need to.
New Caddyfile global option:
{
cert_issuer <name> ...
}
Or, alternatively, as a tls subdirective:
tls {
issuer <name> ...
}
For example, to use ZeroSSL with an API key:
{
cert_issuser zerossl API_KEY
}
For now, that still uses ZeroSSL's ACME endpoint; it fetches EAB
credentials for you. You can also provide the EAB credentials directly
just like any other ACME endpoint:
{
cert_issuer acme {
eab KEY_ID MAC_KEY
}
}
All these examples use the new global option (or tls subdirective). You
can still use traditional/existing options with ZeroSSL, since it's
just another ACME endpoint:
{
acme_ca https://acme.zerossl.com/v2/DV90
acme_eab KEY_ID MAC_KEY
}
That's all there is to it. You just can't mix-and-match acme_* options
with cert_issuer, because it becomes confusing/ambiguous/complicated to
merge the settings.
* Fix broken test
This test was asserting buggy behavior, oops - glad this branch both
discovers and fixes the bug at the same time!
* Fix broken test (post-merge)
* Update modules/caddytls/acmeissuer.go
Fix godoc comment
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
* Add support for ZeroSSL's EAB-by-email endpoint
Also transform the ACMEIssuer into ZeroSSLIssuer implicitly if set to
the ZeroSSL endpoint without EAB (the ZeroSSLIssuer is needed to
generate EAB if not already provided); this is now possible with either
an API key or an email address.
* go.mod: Use latest certmagic, acmez, and x/net
* Wrap underlying logic rather than repeating it
Oops, duh
* Form-encode email info into request body for EAB endpoint
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
2020-08-11 22:58:06 +08:00
acmeIssuer = acmeWrapper . GetACMEIssuer ( )
2020-04-07 02:24:35 +08:00
}
2020-11-17 02:05:55 +08:00
if acmeIssuer == nil {
continue
}
2020-04-07 02:24:35 +08:00
2020-11-17 02:05:55 +08:00
// proceed to configure the ACME issuer's bind host, without
// overwriting any existing settings
if acmeIssuer . Challenges == nil {
acmeIssuer . Challenges = new ( caddytls . ChallengesConfig )
}
if acmeIssuer . Challenges . BindHost == "" {
// only binding to one host is supported
var bindHost string
if bindHosts , ok := cfgVal . Value . ( [ ] string ) ; ok && len ( bindHosts ) > 0 {
bindHost = bindHosts [ 0 ]
}
acmeIssuer . Challenges . BindHost = bindHost
2020-04-07 02:24:35 +08:00
}
}
}
2022-10-14 01:30:57 +08:00
// we used to ensure this block is allowed to create an automation policy;
// doing so was forbidden if it has a key with no host (i.e. ":443")
2020-10-29 10:36:00 +08:00
// and if there is a different server block that also has a key with no
// host -- since a key with no host matches any host, we need its
// associated automation policy to have an empty Subjects list, i.e. no
// host filter, which is indistinguishable between the two server blocks
// because automation is not done in the context of a particular server...
// this is an example of a poor mapping from Caddyfile to JSON but that's
2022-10-14 01:30:57 +08:00
// the least-leaky abstraction I could figure out -- however, this check
// was preventing certain listeners, like those provided by plugins, from
// being used as desired (see the Tailscale listener plugin), so I removed
// the check: and I think since I originally wrote the check I added a new
// check above which *properly* detects this ambiguity without breaking the
// listener plugin; see the check above with a commented example config
if len ( sblockHosts ) == 0 && catchAllAP == nil {
// this server block has a key with no hosts, but there is not yet
// a catch-all automation policy (probably because no global options
// were set), so this one becomes it
catchAllAP = ap
2020-10-29 10:36:00 +08:00
}
2020-03-18 11:00:45 +08:00
2020-10-29 10:36:00 +08:00
// associate our new automation policy with this server block's hosts
2023-03-28 05:16:22 +08:00
ap . SubjectsRaw = sblock . hostsFromKeysNotHTTP ( httpPort )
sort . Strings ( ap . SubjectsRaw ) // solely for deterministic test results
2020-10-29 10:36:00 +08:00
// if a combination of public and internal names were given
// for this same server block and no issuer was specified, we
// need to separate them out in the automation policies so
// that the internal names can use the internal issuer and
// the other names can use the default/public/ACME issuer
var ap2 * caddytls . AutomationPolicy
2020-11-17 02:05:55 +08:00
if len ( ap . Issuers ) == 0 {
2020-10-29 10:36:00 +08:00
var internal , external [ ] string
2023-03-28 05:16:22 +08:00
for _ , s := range ap . SubjectsRaw {
2023-05-16 00:47:30 +08:00
// do not create Issuers for Tailscale domains; they will be given a Manager instead
if strings . HasSuffix ( strings . ToLower ( s ) , ".ts.net" ) {
continue
}
2020-10-29 10:36:00 +08:00
if ! certmagic . SubjectQualifiesForCert ( s ) {
return nil , warnings , fmt . Errorf ( "subject does not qualify for certificate: '%s'" , s )
2020-03-18 11:00:45 +08:00
}
2020-10-29 10:36:00 +08:00
// we don't use certmagic.SubjectQualifiesForPublicCert() because of one nuance:
// names like *.*.tld that may not qualify for a public certificate are actually
// fine when used with OnDemand, since OnDemand (currently) does not obtain
// wildcards (if it ever does, there will be a separate config option to enable
// it that we would need to check here) since the hostname is known at handshake;
// and it is unexpected to switch to internal issuer when the user wants to get
// regular certificates on-demand for a class of certs like *.*.tld.
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
if subjectQualifiesForPublicCert ( ap , s ) {
2020-10-29 10:36:00 +08:00
external = append ( external , s )
} else {
internal = append ( internal , s )
2020-03-18 11:00:45 +08:00
}
}
2020-10-29 10:36:00 +08:00
if len ( external ) > 0 && len ( internal ) > 0 {
2023-03-28 05:16:22 +08:00
ap . SubjectsRaw = external
2020-10-29 10:36:00 +08:00
apCopy := * ap
ap2 = & apCopy
2023-03-28 05:16:22 +08:00
ap2 . SubjectsRaw = internal
2020-11-17 02:05:55 +08:00
ap2 . IssuersRaw = [ ] json . RawMessage { caddyconfig . JSONModuleObject ( caddytls . InternalIssuer { } , "module" , "internal" , & warnings ) }
2020-10-29 10:36:00 +08:00
}
}
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . Policies = append ( tlsApp . Automation . Policies , ap )
if ap2 != nil {
tlsApp . Automation . Policies = append ( tlsApp . Automation . Policies , ap2 )
2020-03-18 11:00:45 +08:00
}
// certificate loaders
2020-05-30 01:49:21 +08:00
if clVals , ok := sblock . pile [ "tls.cert_loader" ] ; ok {
2020-03-18 11:00:45 +08:00
for _ , clVal := range clVals {
certLoaders = append ( certLoaders , clVal . Value . ( caddytls . CertificateLoader ) )
}
}
}
}
// group certificate loaders by module name, then add to config
if len ( certLoaders ) > 0 {
loadersByName := make ( map [ string ] caddytls . CertificateLoader )
for _ , cl := range certLoaders {
name := caddy . GetModuleName ( cl )
// ugh... technically, we may have multiple FileLoader and FolderLoader
// modules (because the tls directive returns one per occurrence), but
// the config structure expects only one instance of each kind of loader
// module, so we have to combine them... instead of enumerating each
// possible cert loader module in a type switch, we can use reflection,
// which works on any cert loaders that are slice types
if reflect . TypeOf ( cl ) . Kind ( ) == reflect . Slice {
combined := reflect . ValueOf ( loadersByName [ name ] )
if ! combined . IsValid ( ) {
combined = reflect . New ( reflect . TypeOf ( cl ) ) . Elem ( )
}
clVal := reflect . ValueOf ( cl )
for i := 0 ; i < clVal . Len ( ) ; i ++ {
2020-04-09 05:31:51 +08:00
combined = reflect . Append ( combined , clVal . Index ( i ) )
2020-03-18 11:00:45 +08:00
}
loadersByName [ name ] = combined . Interface ( ) . ( caddytls . CertificateLoader )
}
}
for certLoaderName , loaders := range loadersByName {
tlsApp . CertificatesRaw [ certLoaderName ] = caddyconfig . JSON ( loaders , & warnings )
}
}
// set any of the on-demand options, for if/when on-demand TLS is enabled
if onDemand , ok := options [ "on_demand_tls" ] . ( * caddytls . OnDemandConfig ) ; ok {
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . OnDemand = onDemand
}
2021-05-03 01:57:28 +08:00
// set the storage clean interval if configured
if storageCleanInterval , ok := options [ "storage_clean_interval" ] . ( caddy . Duration ) ; ok {
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . StorageCleanInterval = storageCleanInterval
}
2021-11-29 06:22:26 +08:00
// set the expired certificates renew interval if configured
if renewCheckInterval , ok := options [ "renew_interval" ] . ( caddy . Duration ) ; ok {
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . RenewCheckInterval = renewCheckInterval
}
2022-08-25 01:22:56 +08:00
// set the OCSP check interval if configured
if ocspCheckInterval , ok := options [ "ocsp_interval" ] . ( caddy . Duration ) ; ok {
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . OCSPCheckInterval = ocspCheckInterval
}
2022-02-20 05:20:38 +08:00
// set whether OCSP stapling should be disabled for manually-managed certificates
if ocspConfig , ok := options [ "ocsp_stapling" ] . ( certmagic . OCSPConfig ) ; ok {
tlsApp . DisableOCSPStapling = ocspConfig . DisableStapling
}
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
// if any hostnames appear on the same server block as a key with
// no host, they will not be used with route matchers because the
// hostless key matches all hosts, therefore, it wouldn't be
// considered for auto-HTTPS, so we need to make sure those hosts
// are manually considered for managed certificates; we also need
// to make sure that any of these names which are internal-only
// get internal certificates by default rather than ACME
var al caddytls . AutomateLoader
internalAP := & caddytls . AutomationPolicy {
2020-11-17 02:05:55 +08:00
IssuersRaw : [ ] json . RawMessage { json . RawMessage ( ` { "module":"internal"} ` ) } ,
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
}
2022-10-05 10:58:19 +08:00
if autoHTTPS != "off" {
for h := range httpsHostsSharedWithHostlessKey {
al = append ( al , h )
if ! certmagic . SubjectQualifiesForPublicCert ( h ) {
2023-03-28 05:16:22 +08:00
internalAP . SubjectsRaw = append ( internalAP . SubjectsRaw , h )
2022-10-05 10:58:19 +08:00
}
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
}
}
if len ( al ) > 0 {
tlsApp . CertificatesRaw [ "automate" ] = caddyconfig . JSON ( al , & warnings )
}
2023-03-28 05:16:22 +08:00
if len ( internalAP . SubjectsRaw ) > 0 {
httpcaddyfile, caddytls: Multiple edge case fixes; add tests
- Create two default automation policies; if the TLS app is used in
isolation with the 'automate' certificate loader, it will now use
an internal issuer for internal-only names, and an ACME issuer for
all other names by default.
- If the HTTP Caddyfile adds an 'automate' loader, it now also adds an
automation policy for any names in that loader that do not qualify
for public certificates so that they will be issued internally. (It
might be nice if this wasn't necessary, but the alternative is to
either make auto-HTTPS logic way more complex by scanning the names in
the 'automate' loader, or to have an automation policy without an
issuer switch between default issuer based on the name being issued
a certificate - I think I like the latter option better, right now we
do something kind of like that but at a level above each individual
automation policies, we do that switch only when no automation
policies match, rather than when a policy without an issuer does
match.)
- Set the default LoggerName rather than a LoggerNames with an empty
host value, which is now taken literally rather than as a catch-all.
- hostsFromKeys, the function that gets a list of hosts from server
block keys, no longer returns an empty string in its resulting slice,
ever.
2020-04-09 04:46:44 +08:00
if tlsApp . Automation == nil {
tlsApp . Automation = new ( caddytls . AutomationConfig )
}
tlsApp . Automation . Policies = append ( tlsApp . Automation . Policies , internalAP )
}
2020-11-17 02:05:55 +08:00
// if there are any global options set for issuers (ACME ones in particular), make sure they
// take effect in every automation policy that does not have any issuers
if tlsApp . Automation != nil {
globalEmail := options [ "email" ]
globalACMECA := options [ "acme_ca" ]
globalACMECARoot := options [ "acme_ca_root" ]
globalACMEDNS := options [ "acme_dns" ]
globalACMEEAB := options [ "acme_eab" ]
2021-06-09 04:10:37 +08:00
globalPreferredChains := options [ "preferred_chains" ]
hasGlobalACMEDefaults := globalEmail != nil || globalACMECA != nil || globalACMECARoot != nil || globalACMEDNS != nil || globalACMEEAB != nil || globalPreferredChains != nil
2020-11-17 02:05:55 +08:00
if hasGlobalACMEDefaults {
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
for i := 0 ; i < len ( tlsApp . Automation . Policies ) ; i ++ {
ap := tlsApp . Automation . Policies [ i ]
if len ( ap . Issuers ) == 0 && automationPolicyHasAllPublicNames ( ap ) {
// for public names, create default issuers which will later be filled in with configured global defaults
// (internal names will implicitly use the internal issuer at auto-https time)
2021-02-03 07:17:26 +08:00
ap . Issuers = caddytls . DefaultIssuers ( )
// if a specific endpoint is configured, can't use multiple default issuers
if globalACMECA != nil {
if strings . Contains ( globalACMECA . ( string ) , "zerossl" ) {
ap . Issuers = [ ] certmagic . Issuer { & caddytls . ZeroSSLIssuer { ACMEIssuer : new ( caddytls . ACMEIssuer ) } }
} else {
ap . Issuers = [ ] certmagic . Issuer { new ( caddytls . ACMEIssuer ) }
}
2020-11-17 02:05:55 +08:00
}
}
}
}
}
2020-10-29 10:36:00 +08:00
// finalize and verify policies; do cleanup
if tlsApp . Automation != nil {
2020-11-17 02:05:55 +08:00
for i , ap := range tlsApp . Automation . Policies {
// ensure all issuers have global defaults filled in
for j , issuer := range ap . Issuers {
err := fillInGlobalACMEDefaults ( issuer , options )
if err != nil {
return nil , warnings , fmt . Errorf ( "filling in global issuer defaults for AP %d, issuer %d: %v" , i , j , err )
}
}
// encode all issuer values we created, so they will be rendered in the output
if len ( ap . Issuers ) > 0 && ap . IssuersRaw == nil {
for _ , iss := range ap . Issuers {
issuerName := iss . ( caddy . Module ) . CaddyModule ( ) . ID . Name ( )
ap . IssuersRaw = append ( ap . IssuersRaw , caddyconfig . JSONModuleObject ( iss , "module" , issuerName , & warnings ) )
}
2020-10-29 10:36:00 +08:00
}
2020-03-18 11:00:45 +08:00
}
2020-07-07 22:10:37 +08:00
// consolidate automation policies that are the exact same
tlsApp . Automation . Policies = consolidateAutomationPolicies ( tlsApp . Automation . Policies )
2020-03-18 11:00:45 +08:00
// ensure automation policies don't overlap subjects (this should be
// an error at provision-time as well, but catch it in the adapt phase
// for convenience)
automationHostSet := make ( map [ string ] struct { } )
for _ , ap := range tlsApp . Automation . Policies {
2023-03-28 05:16:22 +08:00
for _ , s := range ap . SubjectsRaw {
2020-03-18 11:00:45 +08:00
if _ , ok := automationHostSet [ s ] ; ok {
return nil , warnings , fmt . Errorf ( "hostname appears in more than one automation policy, making certificate management ambiguous: %s" , s )
}
automationHostSet [ s ] = struct { } { }
}
}
2020-10-29 10:36:00 +08:00
// if nothing remains, remove any excess values to clean up the resulting config
if len ( tlsApp . Automation . Policies ) == 0 {
tlsApp . Automation . Policies = nil
}
if reflect . DeepEqual ( tlsApp . Automation , new ( caddytls . AutomationConfig ) ) {
tlsApp . Automation = nil
}
2020-03-18 11:00:45 +08:00
}
return tlsApp , warnings , nil
}
2020-11-17 02:05:55 +08:00
type acmeCapable interface { GetACMEIssuer ( ) * caddytls . ACMEIssuer }
2022-08-03 04:39:09 +08:00
func fillInGlobalACMEDefaults ( issuer certmagic . Issuer , options map [ string ] any ) error {
2020-11-17 02:05:55 +08:00
acmeWrapper , ok := issuer . ( acmeCapable )
if ! ok {
return nil
}
acmeIssuer := acmeWrapper . GetACMEIssuer ( )
if acmeIssuer == nil {
return nil
}
globalEmail := options [ "email" ]
globalACMECA := options [ "acme_ca" ]
globalACMECARoot := options [ "acme_ca_root" ]
globalACMEDNS := options [ "acme_dns" ]
globalACMEEAB := options [ "acme_eab" ]
2021-06-09 04:10:37 +08:00
globalPreferredChains := options [ "preferred_chains" ]
2020-11-17 02:05:55 +08:00
if globalEmail != nil && acmeIssuer . Email == "" {
acmeIssuer . Email = globalEmail . ( string )
}
if globalACMECA != nil && acmeIssuer . CA == "" {
acmeIssuer . CA = globalACMECA . ( string )
}
if globalACMECARoot != nil && ! sliceContains ( acmeIssuer . TrustedRootsPEMFiles , globalACMECARoot . ( string ) ) {
acmeIssuer . TrustedRootsPEMFiles = append ( acmeIssuer . TrustedRootsPEMFiles , globalACMECARoot . ( string ) )
}
if globalACMEDNS != nil && ( acmeIssuer . Challenges == nil || acmeIssuer . Challenges . DNS == nil ) {
acmeIssuer . Challenges = & caddytls . ChallengesConfig {
DNS : & caddytls . DNSChallengeConfig {
2021-01-06 05:39:30 +08:00
ProviderRaw : caddyconfig . JSONModuleObject ( globalACMEDNS , "name" , globalACMEDNS . ( caddy . Module ) . CaddyModule ( ) . ID . Name ( ) , nil ) ,
2020-11-17 02:05:55 +08:00
} ,
}
}
if globalACMEEAB != nil && acmeIssuer . ExternalAccount == nil {
acmeIssuer . ExternalAccount = globalACMEEAB . ( * acme . EAB )
}
2021-06-09 04:10:37 +08:00
if globalPreferredChains != nil && acmeIssuer . PreferredChains == nil {
acmeIssuer . PreferredChains = globalPreferredChains . ( * caddytls . ChainPreference )
}
2020-11-17 02:05:55 +08:00
return nil
}
2020-03-18 11:00:45 +08:00
// newBaseAutomationPolicy returns a new TLS automation policy that gets
// its values from the global options map. It should be used as the base
// for any other automation policies. A nil policy (and no error) will be
// returned if there are no default/global options. However, if always is
// true, a non-nil value will always be returned (unless there is an error).
2022-08-03 04:39:09 +08:00
func newBaseAutomationPolicy ( options map [ string ] any , warnings [ ] caddyconfig . Warning , always bool ) ( * caddytls . AutomationPolicy , error ) {
2021-01-08 02:01:58 +08:00
issuers , hasIssuers := options [ "cert_issuer" ]
2020-11-17 02:05:55 +08:00
_ , hasLocalCerts := options [ "local_certs" ]
2020-04-09 01:09:38 +08:00
keyType , hasKeyType := options [ "key_type" ]
2021-01-08 06:52:58 +08:00
ocspStapling , hasOCSPStapling := options [ "ocsp_stapling" ]
2020-03-18 11:00:45 +08:00
2021-01-08 06:52:58 +08:00
hasGlobalAutomationOpts := hasIssuers || hasLocalCerts || hasKeyType || hasOCSPStapling
2020-03-18 11:00:45 +08:00
// if there are no global options related to automation policies
// set, then we can just return right away
if ! hasGlobalAutomationOpts {
if always {
return new ( caddytls . AutomationPolicy ) , nil
}
return nil , nil
}
ap := new ( caddytls . AutomationPolicy )
2020-11-17 02:05:55 +08:00
if hasKeyType {
caddytls: Add support for ZeroSSL; add Caddyfile support for issuers (#3633)
* caddytls: Add support for ZeroSSL; add Caddyfile support for issuers
Configuring issuers explicitly in a Caddyfile is not easily compatible
with existing ACME-specific parameters such as email or acme_ca which
infer the kind of issuer it creates (this is complicated now because
the ZeroSSL issuer wraps the ACME issuer)... oh well, we can revisit
that later if we need to.
New Caddyfile global option:
{
cert_issuer <name> ...
}
Or, alternatively, as a tls subdirective:
tls {
issuer <name> ...
}
For example, to use ZeroSSL with an API key:
{
cert_issuser zerossl API_KEY
}
For now, that still uses ZeroSSL's ACME endpoint; it fetches EAB
credentials for you. You can also provide the EAB credentials directly
just like any other ACME endpoint:
{
cert_issuer acme {
eab KEY_ID MAC_KEY
}
}
All these examples use the new global option (or tls subdirective). You
can still use traditional/existing options with ZeroSSL, since it's
just another ACME endpoint:
{
acme_ca https://acme.zerossl.com/v2/DV90
acme_eab KEY_ID MAC_KEY
}
That's all there is to it. You just can't mix-and-match acme_* options
with cert_issuer, because it becomes confusing/ambiguous/complicated to
merge the settings.
* Fix broken test
This test was asserting buggy behavior, oops - glad this branch both
discovers and fixes the bug at the same time!
* Fix broken test (post-merge)
* Update modules/caddytls/acmeissuer.go
Fix godoc comment
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
* Add support for ZeroSSL's EAB-by-email endpoint
Also transform the ACMEIssuer into ZeroSSLIssuer implicitly if set to
the ZeroSSL endpoint without EAB (the ZeroSSLIssuer is needed to
generate EAB if not already provided); this is now possible with either
an API key or an email address.
* go.mod: Use latest certmagic, acmez, and x/net
* Wrap underlying logic rather than repeating it
Oops, duh
* Form-encode email info into request body for EAB endpoint
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
2020-08-11 22:58:06 +08:00
ap . KeyType = keyType . ( string )
}
2020-03-18 11:00:45 +08:00
2021-01-08 02:01:58 +08:00
if hasIssuers && hasLocalCerts {
2020-11-17 02:05:55 +08:00
return nil , fmt . Errorf ( "global options are ambiguous: local_certs is confusing when combined with cert_issuer, because local_certs is also a specific kind of issuer" )
}
2021-01-08 02:01:58 +08:00
if hasIssuers {
ap . Issuers = issuers . ( [ ] certmagic . Issuer )
2020-11-17 02:05:55 +08:00
} else if hasLocalCerts {
ap . Issuers = [ ] certmagic . Issuer { new ( caddytls . InternalIssuer ) }
2020-03-18 11:00:45 +08:00
}
2021-01-08 06:52:58 +08:00
if hasOCSPStapling {
ocspConfig := ocspStapling . ( certmagic . OCSPConfig )
ap . DisableOCSPStapling = ocspConfig . DisableStapling
ap . OCSPOverrides = ocspConfig . ResponderOverrides
}
2020-03-18 11:00:45 +08:00
return ap , nil
}
// consolidateAutomationPolicies combines automation policies that are the same,
// for a cleaner overall output.
func consolidateAutomationPolicies ( aps [ ] * caddytls . AutomationPolicy ) [ ] * caddytls . AutomationPolicy {
2020-10-29 10:36:00 +08:00
// sort from most specific to least specific; we depend on this ordering
sort . SliceStable ( aps , func ( i , j int ) bool {
if automationPolicyIsSubset ( aps [ i ] , aps [ j ] ) {
return true
}
if automationPolicyIsSubset ( aps [ j ] , aps [ i ] ) {
return false
}
2023-03-28 05:16:22 +08:00
return len ( aps [ i ] . SubjectsRaw ) > len ( aps [ j ] . SubjectsRaw )
2020-10-29 10:36:00 +08:00
} )
2021-04-30 00:56:01 +08:00
emptyAPCount := 0
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
origLenAPs := len ( aps )
2021-04-30 00:56:01 +08:00
// compute the number of empty policies (disregarding subjects) - see #4128
2020-10-29 10:36:00 +08:00
emptyAP := new ( caddytls . AutomationPolicy )
2020-03-18 11:00:45 +08:00
for i := 0 ; i < len ( aps ) ; i ++ {
2023-03-28 05:16:22 +08:00
emptyAP . SubjectsRaw = aps [ i ] . SubjectsRaw
2020-10-29 10:36:00 +08:00
if reflect . DeepEqual ( aps [ i ] , emptyAP ) {
2021-04-30 00:56:01 +08:00
emptyAPCount ++
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
if ! automationPolicyHasAllPublicNames ( aps [ i ] ) {
// if this automation policy has internal names, we might as well remove it
// so auto-https can implicitly use the internal issuer
aps = append ( aps [ : i ] , aps [ i + 1 : ] ... )
i --
}
2020-10-29 10:36:00 +08:00
}
}
2021-04-30 00:56:01 +08:00
// If all policies are empty, we can return nil, as there is no need to set any policy
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
if emptyAPCount == origLenAPs {
2021-04-30 00:56:01 +08:00
return nil
}
2020-03-18 11:00:45 +08:00
2020-10-29 10:36:00 +08:00
// remove or combine duplicate policies
2021-04-03 06:47:04 +08:00
outer :
2020-10-29 10:36:00 +08:00
for i := 0 ; i < len ( aps ) ; i ++ {
2020-12-08 05:22:47 +08:00
// compare only with next policies; we sorted by specificity so we must not delete earlier policies
2020-10-29 10:36:00 +08:00
for j := i + 1 ; j < len ( aps ) ; j ++ {
2020-03-18 11:00:45 +08:00
// if they're exactly equal in every way, just keep one of them
if reflect . DeepEqual ( aps [ i ] , aps [ j ] ) {
aps = append ( aps [ : j ] , aps [ j + 1 : ] ... )
2021-05-12 05:26:07 +08:00
// must re-evaluate current i against next j; can't skip it!
// even if i decrements to -1, will be incremented to 0 immediately
i --
continue outer
2020-03-18 11:00:45 +08:00
}
// if the policy is the same, we can keep just one, but we have
// to be careful which one we keep; if only one has any hostnames
// defined, then we need to keep the one without any hostnames,
// otherwise the one without any subjects (a catch-all) would be
// eaten up by the one with subjects; and if both have subjects, we
// need to combine their lists
2020-11-17 02:05:55 +08:00
if reflect . DeepEqual ( aps [ i ] . IssuersRaw , aps [ j ] . IssuersRaw ) &&
2023-10-15 04:23:50 +08:00
reflect . DeepEqual ( aps [ i ] . ManagersRaw , aps [ j ] . ManagersRaw ) &&
2020-03-18 11:00:45 +08:00
bytes . Equal ( aps [ i ] . StorageRaw , aps [ j ] . StorageRaw ) &&
aps [ i ] . MustStaple == aps [ j ] . MustStaple &&
aps [ i ] . KeyType == aps [ j ] . KeyType &&
aps [ i ] . OnDemand == aps [ j ] . OnDemand &&
2020-03-27 04:02:29 +08:00
aps [ i ] . RenewalWindowRatio == aps [ j ] . RenewalWindowRatio {
2023-03-28 05:16:22 +08:00
if len ( aps [ i ] . SubjectsRaw ) > 0 && len ( aps [ j ] . SubjectsRaw ) == 0 {
2020-10-29 10:36:00 +08:00
// later policy (at j) has no subjects ("catch-all"), so we can
// remove the identical-but-more-specific policy that comes first
// AS LONG AS it is not shadowed by another policy before it; e.g.
// if policy i is for example.com, policy i+1 is '*.com', and policy
// j is catch-all, we cannot remove policy i because that would
// cause example.com to be served by the less specific policy for
// '*.com', which might be different (yes we've seen this happen)
if automationPolicyShadows ( i , aps ) >= j {
aps = append ( aps [ : i ] , aps [ i + 1 : ] ... )
2021-01-05 02:25:29 +08:00
i --
2021-04-03 06:47:04 +08:00
continue outer
2020-10-29 10:36:00 +08:00
}
2020-03-18 11:00:45 +08:00
} else {
2020-08-07 03:56:23 +08:00
// avoid repeated subjects
2023-03-28 05:16:22 +08:00
for _ , subj := range aps [ j ] . SubjectsRaw {
if ! sliceContains ( aps [ i ] . SubjectsRaw , subj ) {
aps [ i ] . SubjectsRaw = append ( aps [ i ] . SubjectsRaw , subj )
2020-08-07 03:56:23 +08:00
}
}
2020-03-18 11:00:45 +08:00
aps = append ( aps [ : j ] , aps [ j + 1 : ] ... )
2020-12-08 05:22:47 +08:00
j --
2020-03-18 11:00:45 +08:00
}
}
}
}
return aps
}
2020-10-29 10:36:00 +08:00
// automationPolicyIsSubset returns true if a's subjects are a subset
// of b's subjects.
func automationPolicyIsSubset ( a , b * caddytls . AutomationPolicy ) bool {
2023-03-28 05:16:22 +08:00
if len ( b . SubjectsRaw ) == 0 {
2020-10-29 10:36:00 +08:00
return true
}
2023-03-28 05:16:22 +08:00
if len ( a . SubjectsRaw ) == 0 {
2020-10-29 10:36:00 +08:00
return false
}
2023-03-28 05:16:22 +08:00
for _ , aSubj := range a . SubjectsRaw {
2020-10-29 10:36:00 +08:00
var inSuperset bool
2023-03-28 05:16:22 +08:00
for _ , bSubj := range b . SubjectsRaw {
2020-10-29 10:36:00 +08:00
if certmagic . MatchWildcard ( aSubj , bSubj ) {
inSuperset = true
break
}
}
if ! inSuperset {
return false
}
}
return true
}
// automationPolicyShadows returns the index of a policy that aps[i] shadows;
// in other words, for all policies after position i, if that policy covers
// the same subjects but is less specific, that policy's position is returned,
// or -1 if no shadowing is found. For example, if policy i is for
// "foo.example.com" and policy i+2 is for "*.example.com", then i+2 will be
// returned, since that policy is shadowed by i, which is in front.
func automationPolicyShadows ( i int , aps [ ] * caddytls . AutomationPolicy ) int {
for j := i + 1 ; j < len ( aps ) ; j ++ {
if automationPolicyIsSubset ( aps [ i ] , aps [ j ] ) {
return j
}
}
return - 1
}
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
// subjectQualifiesForPublicCert is like certmagic.SubjectQualifiesForPublicCert() except
// that this allows domains with multiple wildcard levels like '*.*.example.com' to qualify
// if the automation policy has OnDemand enabled (i.e. this function is more lenient).
func subjectQualifiesForPublicCert ( ap * caddytls . AutomationPolicy , subj string ) bool {
return ! certmagic . SubjectIsIP ( subj ) &&
! certmagic . SubjectIsInternal ( subj ) &&
( strings . Count ( subj , "*." ) < 2 || ap . OnDemand )
}
func automationPolicyHasAllPublicNames ( ap * caddytls . AutomationPolicy ) bool {
2023-03-28 05:16:22 +08:00
for _ , subj := range ap . SubjectsRaw {
httpcaddyfile: Don't put localhost in public APs (fix #4220)
If an email is specified in global options, a site called 'localhost' shouldn't be bunched together with public DNS names in the automation policies, which get the default, public-CA issuers. Fix old test that did this.
I also noticed that these two:
localhost {
}
example.com {
}
and
localhost, example.com {
}
produce slightly different TLS automation policies. The former is what the new test case covers, and we have logic that removes the empty automation policy for localhost so that auto-HTTPS can implicitly create one. (We prefer that whenever possible.) But the latter case produces two automation policies, with the second one being for localhost, with an explicit internal issuer. It's not wrong, just more explicit than it needs to be.
I'd really like to completely rewrite the code from scratch that generates automation policies, hopefully there is a simpler, more correct algorithm.
2021-06-26 01:28:32 +08:00
if ! subjectQualifiesForPublicCert ( ap , subj ) {
return false
}
}
return true
}