2018-03-17 04:28:27 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-03-19 21:36:17 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-03-17 04:28:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
2018-03-21 19:42:42 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/mitchellh/go-testing-interface"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-03-17 04:28:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectCA manages the Connect CA.
|
|
|
|
type ConnectCA struct {
|
|
|
|
// srv is a pointer back to the server.
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:42:42 +00:00
|
|
|
// ConfigurationSet updates the configuration for the CA.
|
|
|
|
//
|
|
|
|
// NOTE(mitchellh): This whole implementation is temporary until the real
|
|
|
|
// CA plugin work comes in. For now, this is only used to configure a single
|
|
|
|
// static CA root.
|
|
|
|
func (s *ConnectCA) ConfigurationSet(
|
|
|
|
args *structs.CAConfiguration,
|
|
|
|
reply *interface{}) error {
|
|
|
|
// NOTE(mitchellh): This is the temporary hardcoding of a static CA
|
|
|
|
// provider. This will allow us to test agent implementations and so on
|
|
|
|
// with an incomplete CA for now.
|
|
|
|
if args.Provider != "static" {
|
|
|
|
return fmt.Errorf("The CA provider can only be 'static' for now")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the configuration allowed for our static provider
|
|
|
|
var config struct {
|
|
|
|
Name string
|
|
|
|
CertPEM string
|
|
|
|
PrivateKeyPEM string
|
|
|
|
Generate bool
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(args.Config, &config); err != nil {
|
|
|
|
return fmt.Errorf("error decoding config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic validation so demos aren't super jank
|
|
|
|
if config.Name == "" {
|
|
|
|
return fmt.Errorf("Name must be set")
|
|
|
|
}
|
|
|
|
if config.CertPEM == "" || config.PrivateKeyPEM == "" {
|
|
|
|
if !config.Generate {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"CertPEM and PrivateKeyPEM must be set, or Generate must be true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience to auto-generate the cert
|
|
|
|
if config.Generate {
|
|
|
|
ca := connect.TestCA(&testing.RuntimeT{}, nil)
|
|
|
|
config.CertPEM = ca.RootCert
|
|
|
|
config.PrivateKeyPEM = ca.SigningKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mitchellh): verify that the private key is valid for the cert
|
|
|
|
|
|
|
|
// Generate an ID for this
|
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the highest index
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
idx, _, err := state.CARoots(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit
|
|
|
|
resp, err := s.srv.raftApply(structs.ConnectCARequestType, &structs.CARequest{
|
|
|
|
Op: structs.CAOpSet,
|
|
|
|
Index: idx,
|
|
|
|
Roots: []*structs.CARoot{
|
|
|
|
&structs.CARoot{
|
|
|
|
ID: id,
|
|
|
|
Name: config.Name,
|
|
|
|
RootCert: config.CertPEM,
|
|
|
|
SigningKey: config.PrivateKeyPEM,
|
|
|
|
Active: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.srv.logger.Printf("[ERR] consul.test: Apply failed %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
// Roots returns the currently trusted root certificates.
|
|
|
|
func (s *ConnectCA) Roots(
|
|
|
|
args *structs.DCSpecificRequest,
|
|
|
|
reply *structs.IndexedCARoots) error {
|
|
|
|
// Forward if necessary
|
|
|
|
if done, err := s.srv.forward("ConnectCA.Roots", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, roots, err := state.CARoots(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Roots = index, roots
|
|
|
|
if reply.Roots == nil {
|
|
|
|
reply.Roots = make(structs.CARoots, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The API response must NEVER contain the secret information
|
|
|
|
// such as keys and so on. We use a whitelist below to copy the
|
|
|
|
// specific fields we want to expose.
|
|
|
|
for i, r := range reply.Roots {
|
|
|
|
// IMPORTANT: r must NEVER be modified, since it is a pointer
|
|
|
|
// directly to the structure in the memdb store.
|
|
|
|
|
|
|
|
reply.Roots[i] = &structs.CARoot{
|
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
RootCert: r.RootCert,
|
|
|
|
RaftIndex: r.RaftIndex,
|
2018-03-20 17:36:05 +00:00
|
|
|
Active: r.Active,
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Active {
|
|
|
|
reply.ActiveRootID = r.ID
|
2018-03-17 04:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-03-19 21:36:17 +00:00
|
|
|
|
|
|
|
// Sign signs a certificate for a service.
|
|
|
|
//
|
|
|
|
// NOTE(mitchellh): There is a LOT missing from this. I do next to zero
|
|
|
|
// validation of the incoming CSR, the way the cert is signed probably
|
|
|
|
// isn't right, we're not using enough of the CSR fields, etc.
|
|
|
|
func (s *ConnectCA) Sign(
|
|
|
|
args *structs.CASignRequest,
|
2018-03-20 04:00:01 +00:00
|
|
|
reply *structs.IssuedCert) error {
|
2018-03-19 21:36:17 +00:00
|
|
|
// Parse the CSR
|
|
|
|
csr, err := connect.ParseCSR(args.CSR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the SPIFFE ID
|
|
|
|
spiffeId, err := connect.ParseSpiffeID(csr.URIs[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serviceId, ok := spiffeId.(*connect.SpiffeIDService)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR must be a service ID")
|
|
|
|
}
|
|
|
|
|
2018-03-20 03:29:14 +00:00
|
|
|
// Get the currently active root
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
_, root, err := state.CARootActive(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-21 17:10:53 +00:00
|
|
|
if root == nil {
|
|
|
|
return fmt.Errorf("no active CA found")
|
|
|
|
}
|
2018-03-19 21:36:17 +00:00
|
|
|
|
|
|
|
// Determine the signing certificate. It is the set signing cert
|
|
|
|
// unless that is empty, in which case it is identically to the public
|
|
|
|
// cert.
|
|
|
|
certPem := root.SigningCert
|
|
|
|
if certPem == "" {
|
|
|
|
certPem = root.RootCert
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the CA cert and signing key from the root
|
|
|
|
caCert, err := connect.ParseCert(certPem)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing CA cert: %s", err)
|
|
|
|
}
|
|
|
|
signer, err := connect.ParseSigner(root.SigningKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing signing key: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The serial number for the cert. NOTE(mitchellh): in the final
|
|
|
|
// implementation this should be monotonically increasing based on
|
|
|
|
// some raft state.
|
|
|
|
sn, err := rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating serial number: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the keyId for the cert from the signing public key.
|
|
|
|
keyId, err := connect.KeyId(signer.Public())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cert template for generation
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: sn,
|
|
|
|
Subject: pkix.Name{CommonName: serviceId.Service},
|
|
|
|
URIs: csr.URIs,
|
2018-03-20 04:00:01 +00:00
|
|
|
Signature: csr.Signature,
|
|
|
|
SignatureAlgorithm: csr.SignatureAlgorithm,
|
|
|
|
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
|
|
|
|
PublicKey: csr.PublicKey,
|
2018-03-19 21:36:17 +00:00
|
|
|
BasicConstraintsValid: true,
|
|
|
|
KeyUsage: x509.KeyUsageDataEncipherment | x509.KeyUsageKeyAgreement,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
2018-03-20 04:00:01 +00:00
|
|
|
NotAfter: time.Now().Add(3 * 24 * time.Hour),
|
2018-03-19 21:36:17 +00:00
|
|
|
NotBefore: time.Now(),
|
|
|
|
AuthorityKeyId: keyId,
|
|
|
|
SubjectKeyId: keyId,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the certificate, PEM encode it and return that value.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
bs, err := x509.CreateCertificate(
|
|
|
|
rand.Reader, &template, caCert, signer.Public(), signer)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating certificate: %s", err)
|
|
|
|
}
|
|
|
|
err = pem.Encode(&buf, &pem.Block{Type: "CERTIFICATE", Bytes: bs})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error encoding private key: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 04:00:01 +00:00
|
|
|
// Set the response
|
|
|
|
*reply = structs.IssuedCert{
|
2018-03-21 22:54:51 +00:00
|
|
|
SerialNumber: connect.HexString(template.SerialNumber.Bytes()),
|
2018-03-21 17:55:39 +00:00
|
|
|
CertPEM: buf.String(),
|
2018-03-21 18:00:46 +00:00
|
|
|
Service: serviceId.Service,
|
|
|
|
ServiceURI: template.URIs[0].String(),
|
|
|
|
ValidAfter: template.NotBefore,
|
|
|
|
ValidBefore: template.NotAfter,
|
2018-03-20 04:00:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
return nil
|
|
|
|
}
|