Skip to content

Commit

Permalink
Allow multiple PEM-encoded certificates in the ca_file.
Browse files Browse the repository at this point in the history
fixes #167
  • Loading branch information
nelhage committed May 26, 2014
1 parent 37f4942 commit fe4ec67
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
50 changes: 20 additions & 30 deletions consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package consul
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/serf"
"io"
"io/ioutil"
"net"
"os"
"time"

"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"github.com/hashicorp/serf/serf"
)

const (
Expand Down Expand Up @@ -131,30 +131,24 @@ func (c *Config) CheckVersion() error {
return nil
}

// CACertificate is used to open and parse a CA file
func (c *Config) CACertificate() (*x509.Certificate, error) {
// AppendCA opens and parses the CA file and adds the certificates to
// the provided CertPool.
func (c *Config) AppendCA(pool *x509.CertPool) error {
if c.CAFile == "" {
return nil, nil
return nil
}

// Read the file
data, err := ioutil.ReadFile(c.CAFile)
if err != nil {
return nil, fmt.Errorf("Failed to read CA file: %v", err)
return fmt.Errorf("Failed to read CA file: %v", err)
}

// Decode from the PEM format
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("Failed to decode CA PEM!")
if !pool.AppendCertsFromPEM(data) {
return fmt.Errorf("Failed to parse any CA certificates")
}

// Parse the certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("Failed to parse CA file: %v", err)
}
return cert, nil
return nil
}

// KeyPair is used to open and parse a certificate and key file
Expand All @@ -177,17 +171,15 @@ func (c *Config) OutgoingTLSConfig() (*tls.Config, error) {
InsecureSkipVerify: !c.VerifyOutgoing,
}

// Ensure we have a CA if VerifyOutgoing is set
if c.VerifyOutgoing && c.CAFile == "" {
return nil, fmt.Errorf("VerifyOutgoing set, and no CA certificate provided!")
}

// Parse the CA cert if any
ca, err := c.CACertificate()
err := c.AppendCA(tlsConfig.RootCAs)
if err != nil {
return nil, err
} else if ca != nil {
tlsConfig.RootCAs.AddCert(ca)
}

// Ensure we have a CA if VerifyOutgoing is set
if c.VerifyOutgoing && ca == nil {
return nil, fmt.Errorf("VerifyOutgoing set, and no CA certificate provided!")
}

// Add cert/key
Expand All @@ -210,11 +202,9 @@ func (c *Config) IncomingTLSConfig() (*tls.Config, error) {
}

// Parse the CA cert if any
ca, err := c.CACertificate()
err := c.AppendCA(tlsConfig.ClientCAs)
if err != nil {
return nil, err
} else if ca != nil {
tlsConfig.ClientCAs.AddCert(ca)
}

// Add cert/key
Expand All @@ -228,7 +218,7 @@ func (c *Config) IncomingTLSConfig() (*tls.Config, error) {
// Check if we require verification
if c.VerifyIncoming {
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
if ca == nil {
if c.CAFile == "" {
return nil, fmt.Errorf("VerifyIncoming set, and no CA certificate provided!")
}
if cert == nil {
Expand Down
15 changes: 9 additions & 6 deletions consul/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ package consul

import (
"crypto/tls"
"crypto/x509"
"testing"
)

func TestConfig_CACertificate_None(t *testing.T) {
func TestConfig_AppendCA_None(t *testing.T) {
conf := &Config{}
cert, err := conf.CACertificate()
pool := x509.NewCertPool()
err := conf.AppendCA(pool)
if err != nil {
t.Fatalf("err: %v", err)
}
if cert != nil {
t.Fatalf("bad: %v", cert)
if len(pool.Subjects()) != 0 {
t.Fatalf("bad: %v", pool.Subjects())
}
}

func TestConfig_CACertificate_Valid(t *testing.T) {
conf := &Config{
CAFile: "../test/ca/root.cer",
}
cert, err := conf.CACertificate()
pool := x509.NewCertPool()
err := conf.AppendCA(pool)
if err != nil {
t.Fatalf("err: %v", err)
}
if cert == nil {
if len(pool.Subjects()) == 0 {
t.Fatalf("expected cert")
}
}
Expand Down

0 comments on commit fe4ec67

Please sign in to comment.