CA Provider Plugins (#4751)
This adds the `agent/connect/ca/plugin` library for consuming/serving Connect CA providers as [go-plugin](https://github.com/hashicorp/go-plugin) plugins. This **does not** wire this up in any way to Consul itself, so this will not enable using these plugins yet.
## Why?
We want to enable CA providers to be pluggable without modifying Consul so that any CA or PKI system can potentially back the Connect certificates. This CA system may also be used in the future for easier bootstrapping and internal cluster security.
### go-plugin
The benefit of `go-plugin` is that for the plugin consumer, the fact that the interface implementation is communicating over multi-process RPC is invisible. Internals of Consul will continue to just use `ca.Provider` interface implementations as if they're local. For plugin _authors_, they simply have to implement the interface. The network/transport/process management issues are handled by go-plugin itself.
The CA provider plugins support both `net/rpc` and gRPC transports. This enables easy authoring in any language. go-plugin handles the actual protocol handshake and connection. This is just a feature of go-plugin.
`go-plugin` is already in production use for years by Packer, Terraform, Nomad, Vault, and Sentinel. We've shown stability for both desktop and server-side software. It is very mature.
## Implementation Details
### `map[string]interface{}`
The `Configure` method passes a `map[string]interface{}`. This map contains only Go primitives and containers of primitives (no funcs, chans, etc.). For `net/rpc` we encode as-is using Gob. For gRPC we marshal to JSON and transmit as a `bytes` type. This is the same approach we take with Vault and other software.
Note that this is just the transport protocol, the end software views it fully decoded.
### `x509.Certificate` and `CertificateRequest`
We transmit the raw ASN.1 bytes and decode on the other side. Unit tests are verifying we get the same cert/csrs across the wire.
### Testing
`go-plugin` exposes test helpers that enable testing the full plugin RPC over real loopback network connections. We test all endpoints for success and error for both `net/rpc` and gRPC.
### Vendoring
This PR doesn't introduce vendoring for two reasons:
1. @banks's `f-envoy` branch introduces a lot of these and I didn't want conflict.
2. The library isn't actually used yet so it doesn't introduce compile-time errors (it does introduce test errors).
## Next Steps
With this in place, we need to figure out the proper way to actually hook these up to Consul, load them, etc. This discussion can happen elsewhere, since regardless of approach this plugin library implementation is the exact same.
2019-01-07 17:48:44 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2019-07-31 02:57:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect/ca"
|
2019-07-31 13:08:10 +00:00
|
|
|
"google.golang.org/grpc"
|
CA Provider Plugins (#4751)
This adds the `agent/connect/ca/plugin` library for consuming/serving Connect CA providers as [go-plugin](https://github.com/hashicorp/go-plugin) plugins. This **does not** wire this up in any way to Consul itself, so this will not enable using these plugins yet.
## Why?
We want to enable CA providers to be pluggable without modifying Consul so that any CA or PKI system can potentially back the Connect certificates. This CA system may also be used in the future for easier bootstrapping and internal cluster security.
### go-plugin
The benefit of `go-plugin` is that for the plugin consumer, the fact that the interface implementation is communicating over multi-process RPC is invisible. Internals of Consul will continue to just use `ca.Provider` interface implementations as if they're local. For plugin _authors_, they simply have to implement the interface. The network/transport/process management issues are handled by go-plugin itself.
The CA provider plugins support both `net/rpc` and gRPC transports. This enables easy authoring in any language. go-plugin handles the actual protocol handshake and connection. This is just a feature of go-plugin.
`go-plugin` is already in production use for years by Packer, Terraform, Nomad, Vault, and Sentinel. We've shown stability for both desktop and server-side software. It is very mature.
## Implementation Details
### `map[string]interface{}`
The `Configure` method passes a `map[string]interface{}`. This map contains only Go primitives and containers of primitives (no funcs, chans, etc.). For `net/rpc` we encode as-is using Gob. For gRPC we marshal to JSON and transmit as a `bytes` type. This is the same approach we take with Vault and other software.
Note that this is just the transport protocol, the end software views it fully decoded.
### `x509.Certificate` and `CertificateRequest`
We transmit the raw ASN.1 bytes and decode on the other side. Unit tests are verifying we get the same cert/csrs across the wire.
### Testing
`go-plugin` exposes test helpers that enable testing the full plugin RPC over real loopback network connections. We test all endpoints for success and error for both `net/rpc` and gRPC.
### Vendoring
This PR doesn't introduce vendoring for two reasons:
1. @banks's `f-envoy` branch introduces a lot of these and I didn't want conflict.
2. The library isn't actually used yet so it doesn't introduce compile-time errors (it does introduce test errors).
## Next Steps
With this in place, we need to figure out the proper way to actually hook these up to Consul, load them, etc. This discussion can happen elsewhere, since regardless of approach this plugin library implementation is the exact same.
2019-01-07 17:48:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// providerPluginGRPCServer implements the CAServer interface for gRPC.
|
|
|
|
type providerPluginGRPCServer struct {
|
|
|
|
impl ca.Provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) Configure(_ context.Context, req *ConfigureRequest) (*Empty, error) {
|
|
|
|
var rawConfig map[string]interface{}
|
|
|
|
if err := json.Unmarshal(req.Config, &rawConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Empty{}, p.impl.Configure(req.ClusterId, req.IsRoot, rawConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) GenerateRoot(context.Context, *Empty) (*Empty, error) {
|
|
|
|
return &Empty{}, p.impl.GenerateRoot()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) ActiveRoot(context.Context, *Empty) (*ActiveRootResponse, error) {
|
|
|
|
pem, err := p.impl.ActiveRoot()
|
|
|
|
return &ActiveRootResponse{CrtPem: pem}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) GenerateIntermediateCSR(context.Context, *Empty) (*GenerateIntermediateCSRResponse, error) {
|
|
|
|
pem, err := p.impl.GenerateIntermediateCSR()
|
|
|
|
return &GenerateIntermediateCSRResponse{CsrPem: pem}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) SetIntermediate(_ context.Context, req *SetIntermediateRequest) (*Empty, error) {
|
|
|
|
return &Empty{}, p.impl.SetIntermediate(req.IntermediatePem, req.RootPem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) ActiveIntermediate(context.Context, *Empty) (*ActiveIntermediateResponse, error) {
|
|
|
|
pem, err := p.impl.ActiveIntermediate()
|
|
|
|
return &ActiveIntermediateResponse{CrtPem: pem}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) GenerateIntermediate(context.Context, *Empty) (*GenerateIntermediateResponse, error) {
|
|
|
|
pem, err := p.impl.GenerateIntermediate()
|
|
|
|
return &GenerateIntermediateResponse{CrtPem: pem}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) Sign(_ context.Context, req *SignRequest) (*SignResponse, error) {
|
|
|
|
csr, err := x509.ParseCertificateRequest(req.Csr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
crtPEM, err := p.impl.Sign(csr)
|
|
|
|
return &SignResponse{CrtPem: crtPEM}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) SignIntermediate(_ context.Context, req *SignIntermediateRequest) (*SignIntermediateResponse, error) {
|
|
|
|
csr, err := x509.ParseCertificateRequest(req.Csr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
crtPEM, err := p.impl.SignIntermediate(csr)
|
|
|
|
return &SignIntermediateResponse{CrtPem: crtPEM}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) CrossSignCA(_ context.Context, req *CrossSignCARequest) (*CrossSignCAResponse, error) {
|
|
|
|
crt, err := x509.ParseCertificate(req.Crt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
crtPEM, err := p.impl.CrossSignCA(crt)
|
|
|
|
return &CrossSignCAResponse{CrtPem: crtPEM}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCServer) Cleanup(context.Context, *Empty) (*Empty, error) {
|
|
|
|
return &Empty{}, p.impl.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
// providerPluginGRPCClient implements ca.Provider for acting as a client
|
|
|
|
// to a remote CA provider plugin over gRPC.
|
|
|
|
type providerPluginGRPCClient struct {
|
|
|
|
client CAClient
|
|
|
|
clientConn *grpc.ClientConn
|
|
|
|
doneCtx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) Configure(
|
|
|
|
clusterId string,
|
|
|
|
isRoot bool,
|
|
|
|
rawConfig map[string]interface{}) error {
|
|
|
|
config, err := json.Marshal(rawConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = p.client.Configure(p.doneCtx, &ConfigureRequest{
|
|
|
|
ClusterId: clusterId,
|
|
|
|
IsRoot: isRoot,
|
|
|
|
Config: config,
|
|
|
|
})
|
|
|
|
return p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) GenerateRoot() error {
|
|
|
|
_, err := p.client.GenerateRoot(p.doneCtx, &Empty{})
|
|
|
|
return p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) ActiveRoot() (string, error) {
|
|
|
|
resp, err := p.client.ActiveRoot(p.doneCtx, &Empty{})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) GenerateIntermediateCSR() (string, error) {
|
|
|
|
resp, err := p.client.GenerateIntermediateCSR(p.doneCtx, &Empty{})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CsrPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) SetIntermediate(intermediatePEM, rootPEM string) error {
|
|
|
|
_, err := p.client.SetIntermediate(p.doneCtx, &SetIntermediateRequest{
|
|
|
|
IntermediatePem: intermediatePEM,
|
|
|
|
RootPem: rootPEM,
|
|
|
|
})
|
|
|
|
return p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) ActiveIntermediate() (string, error) {
|
|
|
|
resp, err := p.client.ActiveIntermediate(p.doneCtx, &Empty{})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) GenerateIntermediate() (string, error) {
|
|
|
|
resp, err := p.client.GenerateIntermediate(p.doneCtx, &Empty{})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) Sign(csr *x509.CertificateRequest) (string, error) {
|
|
|
|
resp, err := p.client.Sign(p.doneCtx, &SignRequest{
|
|
|
|
Csr: csr.Raw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) SignIntermediate(csr *x509.CertificateRequest) (string, error) {
|
|
|
|
resp, err := p.client.SignIntermediate(p.doneCtx, &SignIntermediateRequest{
|
|
|
|
Csr: csr.Raw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) CrossSignCA(crt *x509.Certificate) (string, error) {
|
|
|
|
resp, err := p.client.CrossSignCA(p.doneCtx, &CrossSignCARequest{
|
|
|
|
Crt: crt.Raw,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.CrtPem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) Cleanup() error {
|
|
|
|
_, err := p.client.Cleanup(p.doneCtx, &Empty{})
|
|
|
|
return p.err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *providerPluginGRPCClient) err(err error) error {
|
|
|
|
if err := p.doneCtx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verification
|
|
|
|
var _ CAServer = &providerPluginGRPCServer{}
|
|
|
|
var _ ca.Provider = &providerPluginGRPCClient{}
|