2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-10-25 17:13:41 +00:00
|
|
|
"fmt"
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrAutoEncryptAllowTLSNotEnabled = errors.New("AutoEncrypt.AllowTLS must be enabled in order to use this endpoint")
|
|
|
|
)
|
|
|
|
|
|
|
|
type AutoEncrypt struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs a certificate for an agent.
|
|
|
|
func (a *AutoEncrypt) Sign(
|
|
|
|
args *structs.CASignRequest,
|
|
|
|
reply *structs.SignedResponse) error {
|
|
|
|
if !a.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
if !a.srv.config.AutoEncryptAllowTLS {
|
|
|
|
return ErrAutoEncryptAllowTLSNotEnabled
|
|
|
|
}
|
2022-10-25 17:13:41 +00:00
|
|
|
// There's no reason to forward the AutoEncrypt.Sign RPC to a remote datacenter because its certificates
|
|
|
|
// won't be valid in this datacenter. If the client is requesting a different datacenter, then this is a
|
|
|
|
// misconfiguration, and we can give them a useful error.
|
|
|
|
if args.Datacenter != a.srv.config.Datacenter {
|
|
|
|
return fmt.Errorf("mismatched datacenter (client_dc='%s' server_dc='%s');"+
|
|
|
|
" check client has same datacenter set as servers", args.Datacenter, a.srv.config.Datacenter)
|
|
|
|
}
|
2021-04-20 18:55:24 +00:00
|
|
|
if done, err := a.srv.ForwardRPC("AutoEncrypt.Sign", args, reply); done {
|
2019-06-27 20:22:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the ConnectCA endpoint which is reused here because it is
|
|
|
|
// exactly what is needed.
|
|
|
|
c := ConnectCA{srv: a.srv}
|
|
|
|
|
|
|
|
rootsArgs := structs.DCSpecificRequest{Datacenter: args.Datacenter}
|
|
|
|
roots := structs.IndexedCARoots{}
|
|
|
|
err := c.Roots(&rootsArgs, &roots)
|
2020-02-03 09:35:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
cert := structs.IssuedCert{}
|
|
|
|
err = c.Sign(args, &cert)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.IssuedCert = cert
|
|
|
|
reply.ConnectCARoots = roots
|
|
|
|
reply.ManualCARoots = a.srv.tlsConfigurator.ManualCAPems()
|
|
|
|
reply.VerifyServerHostname = a.srv.tlsConfigurator.VerifyServerHostname()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|