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

Commit a062492

Browse files
committed
[FAB-7387] Ability to load system cert pool
Change-Id: I8d0d531cdca272efd49f9a1b64403e472ef7849c Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
1 parent 83c4a87 commit a062492

File tree

5 files changed

+79
-24
lines changed

5 files changed

+79
-24
lines changed

pkg/config/config.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ func InitConfigFromBytes(configBytes []byte, configType string) (*Config, error)
6767

6868
setLogLevel(myViper)
6969

70-
return &Config{tlsCertPool: x509.NewCertPool(), configViper: myViper}, nil
70+
tlsCertPool, err := getCertPool(myViper)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return &Config{tlsCertPool: tlsCertPool, configViper: myViper}, nil
7176
}
7277

7378
// getNewViper returns a new instance of viper
@@ -111,9 +116,25 @@ func initConfigWithCmdRoot(configFile string, cmdRootPrefix string) (*Config, er
111116
}
112117

113118
setLogLevel(myViper)
119+
tlsCertPool, err := getCertPool(myViper)
120+
if err != nil {
121+
return nil, err
122+
}
114123

115124
logger.Infof("%s logging level is set to: %s", logModule, lu.LogLevelString(logging.GetLevel(logModule)))
116-
return &Config{tlsCertPool: x509.NewCertPool(), configViper: myViper}, nil
125+
return &Config{tlsCertPool: tlsCertPool, configViper: myViper}, nil
126+
}
127+
128+
func getCertPool(myViper *viper.Viper) (*x509.CertPool, error) {
129+
tlsCertPool := x509.NewCertPool()
130+
if myViper.GetBool("client.systemcertpool") == true {
131+
var err error
132+
if tlsCertPool, err = x509.SystemCertPool(); err != nil {
133+
return nil, err
134+
}
135+
logger.Debugf("Loaded system cert pool of size: %d", len(tlsCertPool.Subjects()))
136+
}
137+
return tlsCertPool, nil
117138
}
118139

119140
// setLogLevel will set the log level of the client
@@ -416,9 +437,10 @@ func (c *Config) OrderersConfig() ([]apiconfig.OrdererConfig, error) {
416437
}
417438

418439
for _, orderer := range config.Orderers {
440+
419441
if orderer.TLSCACerts.Path != "" {
420442
orderer.TLSCACerts.Path = substGoPath(orderer.TLSCACerts.Path)
421-
} else if len(orderer.TLSCACerts.Pem) == 0 {
443+
} else if len(orderer.TLSCACerts.Pem) == 0 && c.configViper.GetBool("client.systemcertpool") == false {
422444
errors.Errorf("Orderer has no certs configured. Make sure TLSCACerts.Pem or TLSCACerts.Path is set for %s", orderer.URL)
423445
}
424446

@@ -479,7 +501,7 @@ func (c *Config) PeersConfig(org string) ([]apiconfig.PeerConfig, error) {
479501

480502
for _, peerName := range peersConfig {
481503
p := config.Peers[strings.ToLower(peerName)]
482-
if err = verifyPeerConfig(p, peerName, urlutil.IsTLSEnabled(p.URL)); err != nil {
504+
if err = c.verifyPeerConfig(p, peerName, urlutil.IsTLSEnabled(p.URL)); err != nil {
483505
return nil, err
484506
}
485507
if p.TLSCACerts.Path != "" {
@@ -591,7 +613,7 @@ func (c *Config) ChannelPeers(name string) ([]apiconfig.ChannelPeer, error) {
591613
return nil, errors.Errorf("peer config not found for %s", peerName)
592614
}
593615

594-
if err = verifyPeerConfig(p, peerName, urlutil.IsTLSEnabled(p.URL)); err != nil {
616+
if err = c.verifyPeerConfig(p, peerName, urlutil.IsTLSEnabled(p.URL)); err != nil {
595617
return nil, err
596618
}
597619

@@ -626,7 +648,7 @@ func (c *Config) NetworkPeers() ([]apiconfig.NetworkPeer, error) {
626648

627649
for name, p := range netConfig.Peers {
628650

629-
if err = verifyPeerConfig(p, name, urlutil.IsTLSEnabled(p.URL)); err != nil {
651+
if err = c.verifyPeerConfig(p, name, urlutil.IsTLSEnabled(p.URL)); err != nil {
630652
return nil, err
631653
}
632654

@@ -670,14 +692,14 @@ func (c *Config) PeerMspID(name string) (string, error) {
670692

671693
}
672694

673-
func verifyPeerConfig(p apiconfig.PeerConfig, peerName string, tlsEnabled bool) error {
695+
func (c *Config) verifyPeerConfig(p apiconfig.PeerConfig, peerName string, tlsEnabled bool) error {
674696
if p.URL == "" {
675697
return errors.Errorf("URL does not exist or empty for peer %s", peerName)
676698
}
677699
if p.EventURL == "" {
678700
return errors.Errorf("event URL does not exist or empty for peer %s", peerName)
679701
}
680-
if tlsEnabled && len(p.TLSCACerts.Pem) == 0 && p.TLSCACerts.Path == "" {
702+
if tlsEnabled && len(p.TLSCACerts.Pem) == 0 && p.TLSCACerts.Path == "" && c.configViper.GetBool("client.systemcertpool") == false {
681703
return errors.Errorf("tls.certificate does not exist or empty for peer %s", peerName)
682704
}
683705
return nil

pkg/config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ client:
9696
#library: "/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/softhsm/libsofthsm2.so ,/usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so, /usr/lib/powerpc64le-linux-gnu/softhsm/libsofthsm2.so, /usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so"
9797
library: "add BCCSP library here"
9898

99+
# [Optional]. Use system certificate pool when connecting to peers, orderers (for negotiating TLS) Default: false
100+
# systemcertpool: true
99101

100102
#
101103
# [Optional]. But most apps would have this section so that channel objects can be constructed

pkg/config/config_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,40 @@ func TestInterfaces(t *testing.T) {
675675
}
676676
}
677677

678+
func TestSystemCertPoolDisabled(t *testing.T) {
679+
680+
// get a config file with pool disabled
681+
c, err := InitConfig(configTestFilePath)
682+
if err != nil {
683+
t.Fatal(err)
684+
}
685+
686+
// cert pool should be empty
687+
if len(c.tlsCertPool.Subjects()) > 0 {
688+
t.Fatal("Expecting empty tls cert pool due to disabled system cert pool")
689+
}
690+
}
691+
692+
func TestSystemCertPoolEnabled(t *testing.T) {
693+
694+
// get a config file with pool enabled
695+
c, err := InitConfig(configPemTestFilePath)
696+
if err != nil {
697+
t.Fatal(err)
698+
}
699+
700+
if len(c.tlsCertPool.Subjects()) == 0 {
701+
t.Fatal("System Cert Pool not loaded even though it is enabled")
702+
}
703+
704+
// Org2 'mychannel' peer is missing cert + pem (it should not fail when systemcertpool enabled)
705+
_, err = c.ChannelPeers("mychannel")
706+
if err != nil {
707+
t.Fatalf("Should have skipped verifying ca cert + pem: %s", err)
708+
}
709+
710+
}
711+
678712
func TestSetTLSCACertPool(t *testing.T) {
679713
configImpl.SetTLSCACertPool(nil)
680714
t.Log("TLSCACertRoot must be created. Nothing additional to verify..")

pkg/config/testdata/config_test_pem.yaml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ client:
9090
ephemeral: false
9191
level: 256
9292

93+
# [Optional]. Use system certificate pool when connecting to peers, orderers (for negotiating TLS) Default: false
94+
systemcertpool: true
95+
9396
#
9497
# [Optional]. But most apps would have this section so that channel objects can be constructed
9598
# based on the content below. If an app is creating channels, then it likely will not need this
@@ -127,6 +130,12 @@ channels:
127130
# Default: true
128131
eventSource: true
129132

133+
peer0.org2.example.com:
134+
endorsingPeer: true
135+
chaincodeQuery: true
136+
ledgerQuery: true
137+
eventSource: true
138+
130139
# [Optional]. what chaincodes are expected to exist on this channel? The application can use
131140
# this information to validate that the target peers are in the expected state by comparing
132141
# this list with the query results of getInstalledChaincodes() and getInstantiatedChaincodes()
@@ -292,22 +301,7 @@ peers:
292301
grpcOptions:
293302
ssl-target-name-override: peer0.org2.example.com
294303
tlsCACerts:
295-
pem: |
296-
-----BEGIN CERTIFICATE-----
297-
MIICSDCCAe+gAwIBAgIQK5RpVCPowqtM5Sn0g5udaTAKBggqhkjOPQQDAjB2MQsw
298-
CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy
299-
YW5jaXNjbzEZMBcGA1UEChMQb3JnMi5leGFtcGxlLmNvbTEfMB0GA1UEAxMWdGxz
300-
Y2Eub3JnMi5leGFtcGxlLmNvbTAeFw0xNzA3MjgxNDI3MjBaFw0yNzA3MjYxNDI3
301-
MjBaMHYxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH
302-
Ew1TYW4gRnJhbmNpc2NvMRkwFwYDVQQKExBvcmcyLmV4YW1wbGUuY29tMR8wHQYD
303-
VQQDExZ0bHNjYS5vcmcyLmV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0D
304-
AQcDQgAE/YuiAkpf8ZldX8+4tERG8EjT7sx+bAWG1WGYzNS7MEHOE8aUnoe2gBat
305-
6d5I6OBRd7qlLB2U6bNPv/1vQOSfjKNfMF0wDgYDVR0PAQH/BAQDAgGmMA8GA1Ud
306-
JQQIMAYGBFUdJQAwDwYDVR0TAQH/BAUwAwEB/zApBgNVHQ4EIgQgQBzZ6fWlqa1m
307-
tTVjm5tyQdcIrIqzpRnICRCRkFDIBuwwCgYIKoZIzj0EAwIDRwAwRAIgLB3onZnH
308-
4hp6WZbmIxPfkesVuNjMY9hsveA4n2xvA6ICIE2h9rspRxy08NLPIJXD2L6zPQum
309-
isLpe/VZtWneH2xM
310-
-----END CERTIFICATE-----
304+
pem:
311305
path:
312306
#path: $GOPATH/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/channel/crypto-config/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem
313307

test/fixtures/config/config_test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ client:
9090
ephemeral: false
9191
level: 256
9292

93+
# [Optional]. Use system certificate pool when connecting to peers, orderers (for negotiating TLS) Default: false
94+
systemcertpool: false
95+
9396
#
9497
# [Optional]. But most apps would have this section so that channel objects can be constructed
9598
# based on the content below. If an app is creating channels, then it likely will not need this

0 commit comments

Comments
 (0)