Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 7dca4ca

Browse files
committed
[FAB-10568] refactoring endpointConfig.TLSCACertPool
- updated TLSCACertPool() signature to return fab CertPool implementation instead of returning x509 certpool and error Change-Id: I409f416c96bbe9c61b54459031db0e3bc0ba255b Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent c235146 commit 7dca4ca

File tree

18 files changed

+92
-81
lines changed

18 files changed

+92
-81
lines changed

pkg/common/providers/fab/provider.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type EndpointConfig interface {
8787
ChannelConfig(name string) (*ChannelEndpointConfig, bool)
8888
ChannelPeers(name string) ([]ChannelPeer, bool)
8989
ChannelOrderers(name string) ([]OrdererConfig, bool)
90-
TLSCACertPool(certConfig ...*x509.Certificate) (*x509.CertPool, error)
90+
TLSCACertPool() CertPool
9191
EventServiceType() EventServiceType
9292
TLSClientCerts() []tls.Certificate
9393
CryptoConfigPath() string
@@ -156,3 +156,10 @@ type Providers interface {
156156
InfraProvider() InfraProvider
157157
EndpointConfig() EndpointConfig
158158
}
159+
160+
// CertPool is a thread safe wrapper around the x509 standard library
161+
// cert pool implementation.
162+
type CertPool interface {
163+
// Get returns the cert pool, optionally adding the provided certs
164+
Get(certs ...*x509.Certificate) (*x509.CertPool, error)
165+
}

pkg/common/providers/test/mockfab/mockconfig.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ const ErrorMessage = "default error message"
3535
func DefaultMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
3636
config := NewMockEndpointConfig(mockCtrl)
3737

38-
config.EXPECT().TLSCACertPool(GoodCert).Return(CertPool, nil).AnyTimes()
39-
config.EXPECT().TLSCACertPool(BadCert).Return(CertPool, errors.New(ErrorMessage)).AnyTimes()
40-
config.EXPECT().TLSCACertPool().Return(CertPool, nil).AnyTimes()
38+
config.EXPECT().TLSCACertPool().Return(&MockCertPool{CertPool: CertPool}).AnyTimes()
39+
4140
config.EXPECT().Timeout(fab.EndorserConnection).Return(time.Second * 5).AnyTimes()
4241
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{TLSCert}).AnyTimes()
4342

@@ -48,11 +47,20 @@ func DefaultMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
4847
func BadTLSClientMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
4948
config := NewMockEndpointConfig(mockCtrl)
5049

51-
config.EXPECT().TLSCACertPool(GoodCert).Return(CertPool, nil).AnyTimes()
52-
config.EXPECT().TLSCACertPool(BadCert).Return(CertPool, errors.New(ErrorMessage)).AnyTimes()
53-
config.EXPECT().TLSCACertPool().Return(CertPool, nil).AnyTimes()
50+
config.EXPECT().TLSCACertPool().Return(&MockCertPool{Err: errors.New(ErrorMessage)}).AnyTimes()
5451
config.EXPECT().Timeout(fab.EndorserConnection).Return(time.Second * 5).AnyTimes()
5552
config.EXPECT().TLSClientCerts().Return(nil).AnyTimes()
5653

5754
return config
5855
}
56+
57+
//MockCertPool for unit tests to mock CertPool
58+
type MockCertPool struct {
59+
CertPool *x509.CertPool
60+
Err error
61+
}
62+
63+
//Get mock implementation of fab CertPool.Get()
64+
func (c *MockCertPool) Get(certs ...*x509.Certificate) (*x509.CertPool, error) {
65+
return c.CertPool, c.Err
66+
}

pkg/common/providers/test/mockfab/mockfab.gen.go

Lines changed: 6 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/core/config/comm/comm.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// TLSConfig returns the appropriate config for TLS including the root CAs,
1919
// certs for mutual TLS, and server host override. Works with certs loaded either from a path or embedded pem.
2020
func TLSConfig(cert *x509.Certificate, serverName string, config fab.EndpointConfig) (*tls.Config, error) {
21-
certPool, err := config.TLSCACertPool()
21+
certPool, err := config.TLSCACertPool().Get()
2222
if err != nil {
2323
return nil, err
2424
}
@@ -28,8 +28,7 @@ func TLSConfig(cert *x509.Certificate, serverName string, config fab.EndpointCon
2828
return &tls.Config{}, nil
2929
}
3030

31-
tlsCaCertPool, err := config.TLSCACertPool(cert)
32-
31+
tlsCaCertPool, err := config.TLSCACertPool().Get(cert)
3332
if err != nil {
3433
return nil, err
3534
}

pkg/core/config/comm/comm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestTLSConfigErrorAddingCertificate(t *testing.T) {
2525
mockCtrl := gomock.NewController(t)
2626
defer mockCtrl.Finish()
2727

28-
config := mockfab.DefaultMockConfig(mockCtrl)
28+
config := mockfab.BadTLSClientMockConfig(mockCtrl)
2929

3030
_, err := TLSConfig(mockfab.BadCert, "", config)
3131
if err == nil {

pkg/core/config/comm/tls/certpool.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ import (
1111
"sync"
1212

1313
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
14+
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
1415
)
1516

1617
var logger = logging.NewLogger("fabsdk/core")
1718

18-
// CertPool is a thread safe wrapper around the x509 standard library
19-
// cert pool implementation.
20-
type CertPool interface {
21-
// Get returns the cert pool, optionally adding the provided certs
22-
Get(certs ...*x509.Certificate) (*x509.CertPool, error)
23-
}
24-
2519
// certPool is a thread safe wrapper around the x509 standard library
2620
// cert pool implementation.
2721
// It optionally allows loading the system trust store.
@@ -34,7 +28,7 @@ type certPool struct {
3428
}
3529

3630
// NewCertPool new CertPool implementation
37-
func NewCertPool(useSystemCertPool bool) CertPool {
31+
func NewCertPool(useSystemCertPool bool) fab.CertPool {
3832
return &certPool{
3933
useSystemCertPool: useSystemCertPool,
4034
certsByName: make(map[string][]int),

pkg/fab/api.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@ import (
1111
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
1212
)
1313

14-
//endpointConfigEntity contains endpoint config elements needed by endpointconfig
15-
type endpointConfigEntity struct {
16-
Client ClientConfig
17-
Channels map[string]ChannelEndpointConfig
18-
Organizations map[string]OrganizationConfig
19-
Orderers map[string]OrdererConfig
20-
Peers map[string]PeerConfig
21-
}
22-
23-
//entityMatchers for endpoint configuration
24-
type entityMatchers struct {
25-
matchers map[string][]MatchConfig
26-
}
27-
2814
// ClientConfig provides the definition of the client configuration
2915
type ClientConfig struct {
3016
Organization string

pkg/fab/channel/membership/membership.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func addCertsToConfig(config fab.EndpointConfig, pemCertsList [][]byte) {
208208
certs = append(certs, cert)
209209
}
210210
}
211-
_, err := config.TLSCACertPool(certs...)
211+
_, err := config.TLSCACertPool().Get(certs...)
212212
if err != nil {
213213
logger.Warnf("TLSCACertPool failed %s", err)
214214
}

pkg/fab/endpointconfig.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ func ConfigFromBackend(coreBackend ...core.ConfigBackend) (fab.EndpointConfig, e
6868
return nil, errors.WithMessage(err, "network configuration load failed")
6969
}
7070

71-
config.tlsCertPool = commtls.NewCertPool(config.backend.GetBool("client.tlsCerts.systemCertPool"))
72-
73-
// preemptively add all TLS certs to cert pool as adding them at request time
74-
// is expensive
75-
certs, err := config.loadTLSCerts()
76-
if err != nil {
77-
logger.Infof("could not cache TLS certs: %s", err)
78-
}
79-
if _, err := config.TLSCACertPool(certs...); err != nil {
80-
return nil, errors.WithMessage(err, "cert pool load failed")
81-
}
82-
8371
//print deprecated warning
8472
detectDeprecatedNetworkConfig(config)
8573

@@ -90,7 +78,7 @@ func ConfigFromBackend(coreBackend ...core.ConfigBackend) (fab.EndpointConfig, e
9078
type EndpointConfig struct {
9179
backend *lookup.ConfigLookup
9280
networkConfig *fab.NetworkConfig
93-
tlsCertPool commtls.CertPool
81+
tlsCertPool fab.CertPool
9482
entityMatchers *entityMatchers
9583
peerConfigsByOrg map[string][]fab.PeerConfig
9684
networkPeers []fab.NetworkPeer
@@ -103,6 +91,20 @@ type EndpointConfig struct {
10391
channelMatchers map[int]*regexp.Regexp
10492
}
10593

94+
//endpointConfigEntity contains endpoint config elements needed by endpointconfig
95+
type endpointConfigEntity struct {
96+
Client ClientConfig
97+
Channels map[string]ChannelEndpointConfig
98+
Organizations map[string]OrganizationConfig
99+
Orderers map[string]OrdererConfig
100+
Peers map[string]PeerConfig
101+
}
102+
103+
//entityMatchers for endpoint configuration
104+
type entityMatchers struct {
105+
matchers map[string][]MatchConfig
106+
}
107+
106108
// Timeout reads timeouts for the given timeout type, if type is not found in the config
107109
// then default is set as per the const value above for the corresponding type
108110
func (c *EndpointConfig) Timeout(tType fab.TimeoutType) time.Duration {
@@ -276,8 +278,8 @@ func (c *EndpointConfig) ChannelOrderers(name string) ([]fab.OrdererConfig, bool
276278

277279
// TLSCACertPool returns the configured cert pool. If a certConfig
278280
// is provided, the certificate is added to the pool
279-
func (c *EndpointConfig) TLSCACertPool(certs ...*x509.Certificate) (*x509.CertPool, error) {
280-
return c.tlsCertPool.Get(certs...)
281+
func (c *EndpointConfig) TLSCACertPool() fab.CertPool {
282+
return c.tlsCertPool
281283
}
282284

283285
// EventServiceType returns the type of event service client to use
@@ -503,6 +505,12 @@ func (c *EndpointConfig) loadEndpointConfigEntities(configEntity *endpointConfig
503505
return errors.WithMessage(err, "failed to load channel orderers")
504506
}
505507

508+
//load tls cert pool
509+
err = c.loadTLSCertPool()
510+
if err != nil {
511+
return errors.WithMessage(err, "failed to load TLS cert pool")
512+
}
513+
506514
return nil
507515
}
508516

@@ -845,6 +853,23 @@ func (c *EndpointConfig) loadChannelOrderers() error {
845853
return nil
846854
}
847855

856+
func (c *EndpointConfig) loadTLSCertPool() error {
857+
858+
c.tlsCertPool = commtls.NewCertPool(c.backend.GetBool("client.tlsCerts.systemCertPool"))
859+
860+
// preemptively add all TLS certs to cert pool as adding them at request time
861+
// is expensive
862+
certs, err := c.loadTLSCerts()
863+
if err != nil {
864+
logger.Infof("could not cache TLS certs: %s", err)
865+
}
866+
867+
if _, err := c.tlsCertPool.Get(certs...); err != nil {
868+
return errors.WithMessage(err, "cert pool load failed")
869+
}
870+
return nil
871+
}
872+
848873
// loadTLSClientCerts loads the client's certs for mutual TLS
849874
// It checks the config for embedded pem files before looking for cert files
850875
func (c *EndpointConfig) loadTLSClientCerts(configEntity *endpointConfigEntity) error {

pkg/fab/endpointconfig_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ func TestSystemCertPoolDisabled(t *testing.T) {
662662
t.Fatal("Failed to get endpoint config from backend")
663663
}
664664

665-
_, err = endpointConfig.TLSCACertPool()
665+
_, err = endpointConfig.TLSCACertPool().Get()
666666
if err != nil {
667667
t.Fatal("not supposed to get error")
668668
}

0 commit comments

Comments
 (0)