2018-05-23 21:43:40 +00:00
|
|
|
package ca
|
2018-04-21 03:39:51 +00:00
|
|
|
|
|
|
|
import (
|
2018-06-19 22:10:24 +00:00
|
|
|
"crypto/x509"
|
2018-05-03 19:50:45 +00:00
|
|
|
"fmt"
|
2018-04-21 03:39:51 +00:00
|
|
|
"testing"
|
2018-04-27 06:02:18 +00:00
|
|
|
"time"
|
2018-04-21 03:39:51 +00:00
|
|
|
|
2018-04-27 06:02:18 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-05-03 19:50:45 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-04-21 03:39:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-16 00:59:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-21 03:39:51 +00:00
|
|
|
)
|
|
|
|
|
2018-05-03 19:50:45 +00:00
|
|
|
type consulCAMockDelegate struct {
|
|
|
|
state *state.Store
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulCAMockDelegate) State() *state.Store {
|
|
|
|
return c.state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulCAMockDelegate) ApplyCARequest(req *structs.CARequest) error {
|
|
|
|
idx, _, err := c.state.CAConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch req.Op {
|
|
|
|
case structs.CAOpSetProviderState:
|
|
|
|
_, err := c.state.CASetProviderState(idx+1, req.ProviderState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
case structs.CAOpDeleteProviderState:
|
|
|
|
if err := c.state.CADeleteProviderState(req.ProviderState.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid CA operation '%s'", req.Op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockDelegate(t *testing.T, conf *structs.CAConfiguration) *consulCAMockDelegate {
|
|
|
|
s, err := state.NewStateStore(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("missing state store")
|
|
|
|
}
|
2018-06-16 00:59:08 +00:00
|
|
|
if err := s.CASetConfig(conf.RaftIndex.CreateIndex, conf); err != nil {
|
2018-05-03 19:50:45 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &consulCAMockDelegate{s}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConsulCAConfig() *structs.CAConfiguration {
|
|
|
|
return &structs.CAConfiguration{
|
|
|
|
ClusterID: "asdf",
|
|
|
|
Provider: "consul",
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
func TestConsulCAProvider_Bootstrap(t *testing.T) {
|
2018-04-21 03:39:51 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-05-03 19:50:45 +00:00
|
|
|
conf := testConsulCAConfig()
|
|
|
|
delegate := newMockDelegate(t, conf)
|
2018-04-21 03:39:51 +00:00
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
provider, err := NewConsulProvider(conf.Config, delegate)
|
2018-05-03 19:50:45 +00:00
|
|
|
assert.NoError(err)
|
2018-04-21 03:39:51 +00:00
|
|
|
|
|
|
|
root, err := provider.ActiveRoot()
|
|
|
|
assert.NoError(err)
|
|
|
|
|
2018-04-27 06:02:18 +00:00
|
|
|
// Intermediate should be the same cert.
|
|
|
|
inter, err := provider.ActiveIntermediate()
|
|
|
|
assert.NoError(err)
|
2018-05-03 19:50:45 +00:00
|
|
|
assert.Equal(root, inter)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-05-03 19:50:45 +00:00
|
|
|
// Should be a valid cert
|
|
|
|
parsed, err := connect.ParseCert(root)
|
2018-04-21 03:39:51 +00:00
|
|
|
assert.NoError(err)
|
2018-05-03 19:50:45 +00:00
|
|
|
assert.Equal(parsed.URIs[0].String(), fmt.Sprintf("spiffe://%s.consul", conf.ClusterID))
|
2018-04-27 06:02:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
func TestConsulCAProvider_Bootstrap_WithCert(t *testing.T) {
|
2018-04-27 06:02:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Make sure setting a custom private key/root cert works.
|
|
|
|
assert := assert.New(t)
|
|
|
|
rootCA := connect.TestCA(t, nil)
|
2018-05-03 19:50:45 +00:00
|
|
|
conf := testConsulCAConfig()
|
|
|
|
conf.Config = map[string]interface{}{
|
|
|
|
"PrivateKey": rootCA.SigningKey,
|
|
|
|
"RootCert": rootCA.RootCert,
|
|
|
|
}
|
|
|
|
delegate := newMockDelegate(t, conf)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
provider, err := NewConsulProvider(conf.Config, delegate)
|
2018-04-27 06:02:18 +00:00
|
|
|
assert.NoError(err)
|
|
|
|
|
2018-05-03 19:50:45 +00:00
|
|
|
root, err := provider.ActiveRoot()
|
2018-04-27 06:02:18 +00:00
|
|
|
assert.NoError(err)
|
2018-05-03 19:50:45 +00:00
|
|
|
assert.Equal(root, rootCA.RootCert)
|
2018-04-27 06:02:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
func TestConsulCAProvider_SignLeaf(t *testing.T) {
|
2018-04-27 06:02:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-05-03 19:50:45 +00:00
|
|
|
conf := testConsulCAConfig()
|
|
|
|
delegate := newMockDelegate(t, conf)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
provider, err := NewConsulProvider(conf.Config, delegate)
|
2018-05-03 19:50:45 +00:00
|
|
|
assert.NoError(err)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
|
|
|
spiffeService := &connect.SpiffeIDService{
|
2018-05-03 19:50:45 +00:00
|
|
|
Host: "node1",
|
2018-04-27 06:02:18 +00:00
|
|
|
Namespace: "default",
|
2018-05-03 19:50:45 +00:00
|
|
|
Datacenter: "dc1",
|
2018-04-27 06:02:18 +00:00
|
|
|
Service: "foo",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a leaf cert for the service.
|
|
|
|
{
|
|
|
|
raw, _ := connect.TestCSR(t, spiffeService)
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(raw)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
cert, err := provider.Sign(csr)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
parsed, err := connect.ParseCert(cert)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(parsed.URIs[0], spiffeService.URI())
|
|
|
|
assert.Equal(parsed.Subject.CommonName, "foo")
|
2018-05-09 22:12:31 +00:00
|
|
|
assert.Equal(uint64(2), parsed.SerialNumber.Uint64())
|
2018-04-27 06:02:18 +00:00
|
|
|
|
|
|
|
// Ensure the cert is valid now and expires within the correct limit.
|
|
|
|
assert.True(parsed.NotAfter.Sub(time.Now()) < 3*24*time.Hour)
|
|
|
|
assert.True(parsed.NotBefore.Before(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a new cert for another service and make sure
|
|
|
|
// the serial number is incremented.
|
|
|
|
spiffeService.Service = "bar"
|
|
|
|
{
|
|
|
|
raw, _ := connect.TestCSR(t, spiffeService)
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(raw)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
cert, err := provider.Sign(csr)
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
parsed, err := connect.ParseCert(cert)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(parsed.URIs[0], spiffeService.URI())
|
|
|
|
assert.Equal(parsed.Subject.CommonName, "bar")
|
|
|
|
assert.Equal(parsed.SerialNumber.Uint64(), uint64(2))
|
|
|
|
|
|
|
|
// Ensure the cert is valid now and expires within the correct limit.
|
|
|
|
assert.True(parsed.NotAfter.Sub(time.Now()) < 3*24*time.Hour)
|
|
|
|
assert.True(parsed.NotBefore.Before(time.Now()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:40:03 +00:00
|
|
|
func TestConsulCAProvider_CrossSignCA(t *testing.T) {
|
2018-04-27 06:02:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-06-16 00:59:08 +00:00
|
|
|
conf1 := testConsulCAConfig()
|
|
|
|
delegate1 := newMockDelegate(t, conf1)
|
|
|
|
provider1, err := NewConsulProvider(conf1.Config, delegate1)
|
2018-06-19 23:46:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-06-16 00:59:08 +00:00
|
|
|
conf2 := testConsulCAConfig()
|
|
|
|
conf2.CreateIndex = 10
|
|
|
|
delegate2 := newMockDelegate(t, conf2)
|
|
|
|
provider2, err := NewConsulProvider(conf2.Config, delegate2)
|
2018-06-19 23:46:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-06-16 00:59:08 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
testCrossSignProviders(t, provider1, provider2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCrossSignProviders(t *testing.T, provider1, provider2 Provider) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Get the root from the new provider to be cross-signed.
|
|
|
|
newRootPEM, err := provider2.ActiveRoot()
|
|
|
|
require.NoError(err)
|
|
|
|
newRoot, err := connect.ParseCert(newRootPEM)
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
2018-06-19 23:46:18 +00:00
|
|
|
oldSubject := newRoot.Subject.CommonName
|
2018-06-16 00:59:08 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
newInterPEM, err := provider2.ActiveIntermediate()
|
|
|
|
require.NoError(err)
|
|
|
|
newIntermediate, err := connect.ParseCert(newInterPEM)
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// Have provider1 cross sign our new root cert.
|
|
|
|
xcPEM, err := provider1.CrossSignCA(newRoot)
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
2018-04-27 06:02:18 +00:00
|
|
|
xc, err := connect.ParseCert(xcPEM)
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
oldRootPEM, err := provider1.ActiveRoot()
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
2018-06-19 23:46:18 +00:00
|
|
|
oldRoot, err := connect.ParseCert(oldRootPEM)
|
2018-06-16 00:59:08 +00:00
|
|
|
require.NoError(err)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// AuthorityKeyID should now be the signing root's, SubjectKeyId should be kept.
|
|
|
|
require.Equal(oldRoot.AuthorityKeyId, xc.AuthorityKeyId)
|
|
|
|
require.Equal(newRoot.SubjectKeyId, xc.SubjectKeyId)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
|
|
|
// Subject name should not have changed.
|
2018-06-16 00:59:08 +00:00
|
|
|
require.Equal(oldSubject, xc.Subject.CommonName)
|
2018-04-27 06:02:18 +00:00
|
|
|
|
|
|
|
// Issuer should be the signing root.
|
2018-06-19 23:46:18 +00:00
|
|
|
require.Equal(oldRoot.Issuer.CommonName, xc.Issuer.CommonName)
|
2018-06-19 22:10:24 +00:00
|
|
|
|
|
|
|
// Get a leaf cert so we can verify against the cross-signed cert.
|
|
|
|
spiffeService := &connect.SpiffeIDService{
|
|
|
|
Host: "node1",
|
|
|
|
Namespace: "default",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Service: "foo",
|
|
|
|
}
|
|
|
|
raw, _ := connect.TestCSR(t, spiffeService)
|
|
|
|
|
|
|
|
leafCsr, err := connect.ParseCSR(raw)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
leafPEM, err := provider2.Sign(leafCsr)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
cert, err := connect.ParseCert(leafPEM)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// Check that the leaf signed by the new cert can be verified by either root
|
|
|
|
// certificate by using the new intermediate + cross-signed cert.
|
2018-06-19 22:10:24 +00:00
|
|
|
intermediatePool := x509.NewCertPool()
|
2018-06-19 23:46:18 +00:00
|
|
|
intermediatePool.AddCert(newIntermediate)
|
2018-06-19 22:10:24 +00:00
|
|
|
intermediatePool.AddCert(xc)
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
for _, root := range []*x509.Certificate{oldRoot, newRoot} {
|
|
|
|
rootPool := x509.NewCertPool()
|
|
|
|
rootPool.AddCert(root)
|
2018-06-19 22:10:24 +00:00
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
_, err = cert.Verify(x509.VerifyOptions{
|
|
|
|
Intermediates: intermediatePool,
|
|
|
|
Roots: rootPool,
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
2018-04-21 03:39:51 +00:00
|
|
|
}
|