Skip to content

Commit 2c54694

Browse files
author
Divjot Arora
authored
GODRIVER-1524 Improve error checking for loading CA certificate (#329)
1 parent 4898271 commit 2c54694

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

mongo/options/clientoptions.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,9 @@ func addCACertFromFile(cfg *tls.Config, file string) error {
780780
return err
781781
}
782782

783-
certBytes, err := loadCert(data)
783+
certBytes, err := loadCACert(data)
784784
if err != nil {
785-
return err
785+
return fmt.Errorf("error loading CA cert: %v", err)
786786
}
787787

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

802-
func loadCert(data []byte) ([]byte, error) {
802+
func loadCACert(data []byte) ([]byte, error) {
803803
var certBlock *pem.Block
804804

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

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

815815
switch block.Type {
816816
case "CERTIFICATE":
817-
if certBlock != nil {
818-
return nil, errors.New("multiple CERTIFICATE sections in .pem file")
819-
}
820-
821817
certBlock = block
822818
}
823819

mongo/options/clientoptions_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import (
66
"crypto/x509"
77
"errors"
88
"fmt"
9+
"io/ioutil"
910
"net"
1011
"os"
1112
"reflect"
13+
"strings"
1214
"testing"
1315
"time"
1416

@@ -17,6 +19,7 @@ import (
1719
"go.mongodb.org/mongo-driver/bson/bsoncodec"
1820
"go.mongodb.org/mongo-driver/event"
1921
"go.mongodb.org/mongo-driver/internal"
22+
"go.mongodb.org/mongo-driver/internal/testutil/assert"
2023
"go.mongodb.org/mongo-driver/mongo/readconcern"
2124
"go.mongodb.org/mongo-driver/mongo/readpref"
2225
"go.mongodb.org/mongo-driver/mongo/writeconcern"
@@ -459,6 +462,42 @@ func TestClientOptions(t *testing.T) {
459462
})
460463
}
461464
})
465+
t.Run("loadCACert", func(t *testing.T) {
466+
caData := readFile(t, "testdata/ca.pem")
467+
keyData := readFile(t, "testdata/ca-key.pem")
468+
noCertErr := errors.New("no CERTIFICATE section found")
469+
malformedErr := errors.New("invalid .pem file")
470+
471+
testCases := []struct {
472+
name string
473+
data []byte
474+
err error
475+
}{
476+
{"file with certificate succeeds", caData, nil},
477+
{"empty file errors", []byte{}, noCertErr},
478+
{"file with no certificate errors", keyData, noCertErr},
479+
{"file with malformed data errors", []byte{1, 2, 3}, malformedErr},
480+
}
481+
for _, tc := range testCases {
482+
t.Run(tc.name, func(t *testing.T) {
483+
_, err := loadCACert(tc.data)
484+
if tc.err == nil {
485+
assert.Nil(t, err, "loadCACert error: %v", err)
486+
return
487+
}
488+
489+
assert.NotNil(t, err, "expected error %v, got nil", tc.err)
490+
containsMsg := strings.Contains(err.Error(), tc.err.Error())
491+
assert.True(t, containsMsg, "expected error %v, got %v", tc.err, err)
492+
})
493+
}
494+
})
495+
}
496+
497+
func readFile(t *testing.T, path string) []byte {
498+
data, err := ioutil.ReadFile(path)
499+
assert.Nil(t, err, "ReadFile error for %s: %v", path, err)
500+
return data
462501
}
463502

464503
type testDialer struct {

0 commit comments

Comments
 (0)