Skip to content

GODRIVER-1524 Improve error checking for loading CA certificate #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions mongo/options/clientoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,9 @@ func addCACertFromFile(cfg *tls.Config, file string) error {
return err
}

certBytes, err := loadCert(data)
certBytes, err := loadCACert(data)
if err != nil {
return err
return fmt.Errorf("error loading CA cert: %v", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this error wrapping so it's clear that an error occurred when reading the CA file.

}

cert, err := x509.ParseCertificate(certBytes)
Expand All @@ -799,12 +799,12 @@ func addCACertFromFile(cfg *tls.Config, file string) error {
return nil
}

func loadCert(data []byte) ([]byte, error) {
func loadCACert(data []byte) ([]byte, error) {
var certBlock *pem.Block

for certBlock == nil {
if data == nil || len(data) == 0 {
return nil, errors.New(".pem file must have both a CERTIFICATE and an RSA PRIVATE KEY section")
return nil, errors.New("no CERTIFICATE section found")
}

block, rest := pem.Decode(data)
Expand All @@ -814,10 +814,6 @@ func loadCert(data []byte) ([]byte, error) {

switch block.Type {
case "CERTIFICATE":
if certBlock != nil {
return nil, errors.New("multiple CERTIFICATE sections in .pem file")
}

certBlock = block
}

Expand Down
39 changes: 39 additions & 0 deletions mongo/options/clientoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"reflect"
"strings"
"testing"
"time"

Expand All @@ -17,6 +19,7 @@ import (
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/internal"
"go.mongodb.org/mongo-driver/internal/testutil/assert"
"go.mongodb.org/mongo-driver/mongo/readconcern"
"go.mongodb.org/mongo-driver/mongo/readpref"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
Expand Down Expand Up @@ -459,6 +462,42 @@ func TestClientOptions(t *testing.T) {
})
}
})
t.Run("loadCACert", func(t *testing.T) {
caData := readFile(t, "testdata/ca.pem")
keyData := readFile(t, "testdata/ca-key.pem")
noCertErr := errors.New("no CERTIFICATE section found")
malformedErr := errors.New("invalid .pem file")

testCases := []struct {
name string
data []byte
err error
}{
{"file with certificate succeeds", caData, nil},
{"empty file errors", []byte{}, noCertErr},
{"file with no certificate errors", keyData, noCertErr},
{"file with malformed data errors", []byte{1, 2, 3}, malformedErr},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := loadCACert(tc.data)
if tc.err == nil {
assert.Nil(t, err, "loadCACert error: %v", err)
return
}

assert.NotNil(t, err, "expected error %v, got nil", tc.err)
containsMsg := strings.Contains(err.Error(), tc.err.Error())
assert.True(t, containsMsg, "expected error %v, got %v", tc.err, err)
})
}
})
}

func readFile(t *testing.T, path string) []byte {
data, err := ioutil.ReadFile(path)
assert.Nil(t, err, "ReadFile error for %s: %v", path, err)
return data
}

type testDialer struct {
Expand Down