mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
3c90e370a4
This commit goes a long way toward making automated documentation of Caddy config and Caddy modules possible. It's a broad, sweeping change, but mostly internal. It allows us to automatically generate docs for all Caddy modules (including future third-party ones) and make them viewable on a web page; it also doubles as godoc comments. As such, this commit makes significant progress in migrating the docs from our temporary wiki page toward our new website which is still under construction. With this change, all host modules will use ctx.LoadModule() and pass in both the struct pointer and the field name as a string. This allows the reflect package to read the struct tag from that field so that it can get the necessary information like the module namespace and the inline key. This has the nice side-effect of unifying the code and documentation. It also simplifies module loading, and handles several variations on field types for raw module fields (i.e. variations on json.RawMessage, such as arrays and maps). I also renamed ModuleInfo.Name -> ModuleInfo.ID, to make it clear that the ID is the "full name" which includes both the module namespace and the name. This clarity is helpful when describing module hierarchy. As of this change, Caddy modules are no longer an experimental design. I think the architecture is good enough to go forward.
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package caddytls
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"math/big"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/mholt/certmagic"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(Policy{})
|
|
}
|
|
|
|
// Policy represents a policy for selecting the certificate used to
|
|
// complete a handshake when there may be multiple options. All fields
|
|
// specified must match the candidate certificate for it to be chosen.
|
|
// This was needed to solve https://github.com/caddyserver/caddy/issues/2588.
|
|
type Policy struct {
|
|
SerialNumber *big.Int `json:"serial_number,omitempty"`
|
|
SubjectOrganization string `json:"subject_organization,omitempty"`
|
|
PublicKeyAlgorithm PublicKeyAlgorithm `json:"public_key_algorithm,omitempty"`
|
|
Tag string `json:"tag,omitempty"`
|
|
}
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
func (Policy) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "tls.certificate_selection.custom",
|
|
New: func() caddy.Module { return new(Policy) },
|
|
}
|
|
}
|
|
|
|
// SelectCertificate implements certmagic.CertificateSelector.
|
|
func (p Policy) SelectCertificate(_ *tls.ClientHelloInfo, choices []certmagic.Certificate) (certmagic.Certificate, error) {
|
|
for _, cert := range choices {
|
|
if p.SerialNumber != nil && cert.SerialNumber.Cmp(p.SerialNumber) != 0 {
|
|
continue
|
|
}
|
|
|
|
if p.PublicKeyAlgorithm != PublicKeyAlgorithm(x509.UnknownPublicKeyAlgorithm) &&
|
|
PublicKeyAlgorithm(cert.PublicKeyAlgorithm) != p.PublicKeyAlgorithm {
|
|
continue
|
|
}
|
|
|
|
if p.SubjectOrganization != "" {
|
|
var matchOrg bool
|
|
for _, org := range cert.Subject.Organization {
|
|
if p.SubjectOrganization == org {
|
|
matchOrg = true
|
|
break
|
|
}
|
|
}
|
|
if !matchOrg {
|
|
continue
|
|
}
|
|
}
|
|
|
|
if p.Tag != "" && !cert.HasTag(p.Tag) {
|
|
continue
|
|
}
|
|
|
|
return cert, nil
|
|
}
|
|
return certmagic.Certificate{}, fmt.Errorf("no certificates matched custom selection policy")
|
|
}
|
|
|
|
// Interface guard
|
|
var _ certmagic.CertificateSelector = (*Policy)(nil)
|