mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-01 13:21:50 +08:00
ab80ff4fd2
This commits dds 3 separate, but very related features: 1. Automated server identity management How do you know you're connecting to the server you think you are? How do you know the server connecting to you is the server instance you think it is? Mutually-authenticated TLS (mTLS) answers both of these questions. Using TLS to authenticate requires a public/private key pair (and the peer must trust the certificate you present to it). Fortunately, Caddy is really good at managing certificates by now. We tap into that power to make it possible for Caddy to obtain and renew its own identity credentials, or in other words, a certificate that can be used for both server verification when clients connect to it, and client verification when it connects to other servers. Its associated private key is essentially its identity, and TLS takes care of possession proofs. This configuration is simply a list of identifiers and an optional list of custom certificate issuers. Identifiers are things like IP addresses or DNS names that can be used to access the Caddy instance. The default issuers are ZeroSSL and Let's Encrypt, but these are public CAs, so they won't issue certs for private identifiers. Caddy will simply manage credentials for these, which other parts of Caddy can use, for example: remote administration or dynamic config loading (described below). 2. Remote administration over secure connection This feature adds generic remote admin functionality that is safe to expose on a public interface. - The "remote" (or "secure") endpoint is optional. It does not affect the standard/local/plaintext endpoint. - It's the same as the [API endpoint on localhost:2019](https://caddyserver.com/docs/api), but over TLS. - TLS cannot be disabled on this endpoint. - TLS mutual auth is required, and cannot be disabled. - The server's certificate _must_ be obtained and renewed via automated means, such as ACME. It cannot be manually loaded. - The TLS server takes care of verifying the client. - The admin handler takes care of application-layer permissions (methods and paths that each client is allowed to use).\ - Sensible defaults are still WIP. - Config fields subject to change/renaming. 3. Dyanmic config loading at startup Since this feature was planned in tandem with remote admin, and depends on its changes, I am combining them into one PR. Dynamic config loading is where you tell Caddy how to load its config, and then it loads and runs that. First, it will load the config you give it (and persist that so it can be optionally resumed later). Then, it will try pulling its _actual_ config using the module you've specified (dynamically loaded configs are _not_ persisted to storage, since resuming them doesn't make sense). This PR comes with a standard config loader module called `caddy.config_loaders.http`. Caddyfile config for all of this can probably be added later. COMMITS: * admin: Secure socket for remote management Functional, but still WIP. Optional secure socket for the admin endpoint is designed for remote management, i.e. to be exposed on a public port. It enforces TLS mutual authentication which cannot be disabled. The default port for this is :2021. The server certificate cannot be specified manually, it MUST be obtained from a certificate issuer (i.e. ACME). More polish and sensible defaults are still in development. Also cleaned up and consolidated the code related to quitting the process. * Happy lint * Implement dynamic config loading; HTTP config loader module This allows Caddy to load a dynamic config when it starts. Dynamically-loaded configs are intentionally not persisted to storage. Includes an implementation of the standard config loader, HTTPLoader. Can be used to download configs over HTTP(S). * Refactor and cleanup; prevent recursive config pulls Identity management is now separated from remote administration. There is no need to enable remote administration if all you want is identity management, but you will need to configure identity management if you want remote administration. * Fix lint warnings * Rename identities->identifiers for consistency
152 lines
3.8 KiB
Go
152 lines
3.8 KiB
Go
package caddyconfig
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
)
|
|
|
|
func init() {
|
|
caddy.RegisterModule(HTTPLoader{})
|
|
}
|
|
|
|
// HTTPLoader can load Caddy configs over HTTP(S). It can adapt the config
|
|
// based on the Content-Type header of the HTTP response.
|
|
type HTTPLoader struct {
|
|
// The method for the request. Default: GET
|
|
Method string `json:"method,omitempty"`
|
|
|
|
// The URL of the request.
|
|
URL string `json:"url,omitempty"`
|
|
|
|
// HTTP headers to add to the request.
|
|
Headers http.Header `json:"header,omitempty"`
|
|
|
|
// Maximum time allowed for a complete connection and request.
|
|
Timeout caddy.Duration `json:"timeout,omitempty"`
|
|
|
|
TLS *struct {
|
|
// Present this instance's managed remote identity credentials to the server.
|
|
UseServerIdentity bool `json:"use_server_identity,omitempty"`
|
|
|
|
// PEM-encoded client certificate filename to present to the server.
|
|
ClientCertificateFile string `json:"client_certificate_file,omitempty"`
|
|
|
|
// PEM-encoded key to use with the client certificate.
|
|
ClientCertificateKeyFile string `json:"client_certificate_key_file,omitempty"`
|
|
|
|
// List of PEM-encoded CA certificate files to add to the same trust
|
|
// store as RootCAPool (or root_ca_pool in the JSON).
|
|
RootCAPEMFiles []string `json:"root_ca_pem_files,omitempty"`
|
|
} `json:"tls,omitempty"`
|
|
}
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
func (HTTPLoader) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: "caddy.config_loaders.http",
|
|
New: func() caddy.Module { return new(HTTPLoader) },
|
|
}
|
|
}
|
|
|
|
// LoadConfig loads a Caddy config.
|
|
func (hl HTTPLoader) LoadConfig(ctx caddy.Context) ([]byte, error) {
|
|
client, err := hl.makeClient(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
method := hl.Method
|
|
if method == "" {
|
|
method = http.MethodGet
|
|
}
|
|
|
|
req, err := http.NewRequest(method, hl.URL, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header = hl.Headers
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
return nil, fmt.Errorf("server responded with HTTP %d", resp.StatusCode)
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result, warnings, err := adaptByContentType(resp.Header.Get("Content-Type"), body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, warn := range warnings {
|
|
ctx.Logger(hl).Warn(warn.String())
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (hl HTTPLoader) makeClient(ctx caddy.Context) (*http.Client, error) {
|
|
client := &http.Client{
|
|
Timeout: time.Duration(hl.Timeout),
|
|
}
|
|
|
|
if hl.TLS != nil {
|
|
var tlsConfig *tls.Config
|
|
|
|
// client authentication
|
|
if hl.TLS.UseServerIdentity {
|
|
certs, err := ctx.IdentityCredentials(ctx.Logger(hl))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting server identity credentials: %v", err)
|
|
}
|
|
if tlsConfig == nil {
|
|
tlsConfig = new(tls.Config)
|
|
}
|
|
tlsConfig.Certificates = certs
|
|
} else if hl.TLS.ClientCertificateFile != "" && hl.TLS.ClientCertificateKeyFile != "" {
|
|
cert, err := tls.LoadX509KeyPair(hl.TLS.ClientCertificateFile, hl.TLS.ClientCertificateKeyFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if tlsConfig == nil {
|
|
tlsConfig = new(tls.Config)
|
|
}
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
|
}
|
|
|
|
// trusted server certs
|
|
if len(hl.TLS.RootCAPEMFiles) > 0 {
|
|
rootPool := x509.NewCertPool()
|
|
for _, pemFile := range hl.TLS.RootCAPEMFiles {
|
|
pemData, err := ioutil.ReadFile(pemFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed reading ca cert: %v", err)
|
|
}
|
|
rootPool.AppendCertsFromPEM(pemData)
|
|
}
|
|
if tlsConfig == nil {
|
|
tlsConfig = new(tls.Config)
|
|
}
|
|
tlsConfig.RootCAs = rootPool
|
|
}
|
|
|
|
client.Transport = &http.Transport{TLSClientConfig: tlsConfig}
|
|
}
|
|
|
|
return client, nil
|
|
}
|
|
|
|
var _ caddy.ConfigLoader = (*HTTPLoader)(nil)
|