mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 22:23:48 +03: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
138 lines
4.3 KiB
Go
138 lines
4.3 KiB
Go
// 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 caddyconfig
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
)
|
|
|
|
// Adapter is a type which can adapt a configuration to Caddy JSON.
|
|
// It returns the results and any warnings, or an error.
|
|
type Adapter interface {
|
|
Adapt(body []byte, options map[string]interface{}) ([]byte, []Warning, error)
|
|
}
|
|
|
|
// Warning represents a warning or notice related to conversion.
|
|
type Warning struct {
|
|
File string `json:"file,omitempty"`
|
|
Line int `json:"line,omitempty"`
|
|
Directive string `json:"directive,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
func (w Warning) String() string {
|
|
var directive string
|
|
if w.Directive != "" {
|
|
directive = fmt.Sprintf(" (%s)", w.Directive)
|
|
}
|
|
return fmt.Sprintf("%s:%d%s: %s", w.File, w.Line, directive, w.Message)
|
|
}
|
|
|
|
// JSON encodes val as JSON, returning it as a json.RawMessage. Any
|
|
// marshaling errors (which are highly unlikely with correct code)
|
|
// are converted to warnings. This is convenient when filling config
|
|
// structs that require a json.RawMessage, without having to worry
|
|
// about errors.
|
|
func JSON(val interface{}, warnings *[]Warning) json.RawMessage {
|
|
b, err := json.Marshal(val)
|
|
if err != nil {
|
|
if warnings != nil {
|
|
*warnings = append(*warnings, Warning{Message: err.Error()})
|
|
}
|
|
return nil
|
|
}
|
|
return b
|
|
}
|
|
|
|
// JSONModuleObject is like JSON(), except it marshals val into a JSON object
|
|
// with an added key named fieldName with the value fieldVal. This is useful
|
|
// for encoding module values where the module name has to be described within
|
|
// the object by a certain key; for example, `"handler": "file_server"` for a
|
|
// file server HTTP handler (fieldName="handler" and fieldVal="file_server").
|
|
// The val parameter must encode into a map[string]interface{} (i.e. it must be
|
|
// a struct or map). Any errors are converted into warnings.
|
|
func JSONModuleObject(val interface{}, fieldName, fieldVal string, warnings *[]Warning) json.RawMessage {
|
|
// encode to a JSON object first
|
|
enc, err := json.Marshal(val)
|
|
if err != nil {
|
|
if warnings != nil {
|
|
*warnings = append(*warnings, Warning{Message: err.Error()})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// then decode the object
|
|
var tmp map[string]interface{}
|
|
err = json.Unmarshal(enc, &tmp)
|
|
if err != nil {
|
|
if warnings != nil {
|
|
*warnings = append(*warnings, Warning{Message: err.Error()})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// so we can easily add the module's field with its appointed value
|
|
tmp[fieldName] = fieldVal
|
|
|
|
// then re-marshal as JSON
|
|
result, err := json.Marshal(tmp)
|
|
if err != nil {
|
|
if warnings != nil {
|
|
*warnings = append(*warnings, Warning{Message: err.Error()})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// RegisterAdapter registers a config adapter with the given name.
|
|
// This should usually be done at init-time. It panics if the
|
|
// adapter cannot be registered successfully.
|
|
func RegisterAdapter(name string, adapter Adapter) {
|
|
if _, ok := configAdapters[name]; ok {
|
|
panic(fmt.Errorf("%s: already registered", name))
|
|
}
|
|
configAdapters[name] = adapter
|
|
caddy.RegisterModule(adapterModule{name, adapter})
|
|
}
|
|
|
|
// GetAdapter returns the adapter with the given name,
|
|
// or nil if one with that name is not registered.
|
|
func GetAdapter(name string) Adapter {
|
|
return configAdapters[name]
|
|
}
|
|
|
|
// adapterModule is a wrapper type that can turn any config
|
|
// adapter into a Caddy module, which has the benefit of being
|
|
// counted with other modules, even though they do not
|
|
// technically extend the Caddy configuration structure.
|
|
// See caddyserver/caddy#3132.
|
|
type adapterModule struct {
|
|
name string
|
|
Adapter
|
|
}
|
|
|
|
func (am adapterModule) CaddyModule() caddy.ModuleInfo {
|
|
return caddy.ModuleInfo{
|
|
ID: caddy.ModuleID("caddy.adapters." + am.name),
|
|
New: func() caddy.Module { return am },
|
|
}
|
|
}
|
|
|
|
var configAdapters = make(map[string]Adapter)
|