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

Commit 72bc6c6

Browse files
committed
[FAB-11135] int tests - discovery peer msp id
- added integration tests to e2e/orgs to test if msp id is being used from discovered peer not from endpoint config - added multi org test with SDK config having only one org - cleaned up integration tests local. Removed custom config file overrides for local tests. Change-Id: I3352f38aae438257b6e32b9e48f4ce2e0278c92d Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 39d47d6 commit 72bc6c6

21 files changed

+505
-537
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ func DefaultMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
4343
return config
4444
}
4545

46+
// CustomMockConfig returns a custom mock config with custom certpool for testing
47+
func CustomMockConfig(mockCtrl *gomock.Controller, certPool *x509.CertPool) *MockEndpointConfig {
48+
config := NewMockEndpointConfig(mockCtrl)
49+
50+
config.EXPECT().TLSCACertPool().Return(&MockCertPool{CertPool: certPool}).AnyTimes()
51+
52+
config.EXPECT().Timeout(fab.EndorserConnection).Return(time.Second * 5).AnyTimes()
53+
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{TLSCert}).AnyTimes()
54+
55+
return config
56+
}
57+
4658
// BadTLSClientMockConfig returns a mock config for testing with TLSClientCerts() that always returns an error
4759
func BadTLSClientMockConfig(mockCtrl *gomock.Controller) *MockEndpointConfig {
4860
config := NewMockEndpointConfig(mockCtrl)

pkg/core/config/comm/comm.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ 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().Get()
21+
22+
certPool, err := config.TLSCACertPool().Get(cert)
2223
if err != nil {
2324
return nil, err
2425
}
2526

26-
if cert == nil && (certPool == nil || len(certPool.Subjects()) == 0) {
27-
//Return empty tls config if there is no cert provided or if certpool unavailable
27+
if certPool == nil || len(certPool.Subjects()) == 0 {
28+
//Return empty tls config if certpool is unavailable
2829
return &tls.Config{}, nil
2930
}
3031

31-
tlsCaCertPool, err := config.TLSCACertPool().Get(cert)
32-
if err != nil {
33-
return nil, err
34-
}
35-
36-
return &tls.Config{RootCAs: tlsCaCertPool, Certificates: config.TLSClientCerts(), ServerName: serverName}, nil
32+
return &tls.Config{RootCAs: certPool, Certificates: config.TLSClientCerts(), ServerName: serverName}, nil
3733
}
3834

3935
// TLSCertHash is a utility method to calculate the SHA256 hash of the configured certificate (for usage in channel headers)

pkg/core/config/comm/comm_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package comm
99
import (
1010
"bytes"
1111
"encoding/hex"
12+
"strconv"
1213
"testing"
1314

1415
"strings"
@@ -17,6 +18,8 @@ import (
1718

1819
"reflect"
1920

21+
"crypto/x509"
22+
2023
"github.com/golang/mock/gomock"
2124
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/test/mockfab"
2225
)
@@ -58,7 +61,11 @@ func TestTLSConfigHappyPath(t *testing.T) {
5861
mockCtrl := gomock.NewController(t)
5962
defer mockCtrl.Finish()
6063

61-
config := mockfab.DefaultMockConfig(mockCtrl)
64+
testCertPool := x509.NewCertPool()
65+
certs := createNCerts(1)
66+
testCertPool.AddCert(certs[0])
67+
68+
config := mockfab.CustomMockConfig(mockCtrl, testCertPool)
6269

6370
serverHostOverride := "servernamebeingoverriden"
6471

@@ -71,7 +78,7 @@ func TestTLSConfigHappyPath(t *testing.T) {
7178
t.Fatal("Incorrect server name!")
7279
}
7380

74-
if tlsConfig.RootCAs != mockfab.CertPool {
81+
if tlsConfig.RootCAs != testCertPool {
7582
t.Fatal("Incorrect cert pool")
7683
}
7784

@@ -84,6 +91,18 @@ func TestTLSConfigHappyPath(t *testing.T) {
8491
}
8592
}
8693

94+
func createNCerts(n int) []*x509.Certificate {
95+
var certs []*x509.Certificate
96+
for i := 0; i < n; i++ {
97+
cert := &x509.Certificate{
98+
RawSubject: []byte(strconv.Itoa(i)),
99+
Raw: []byte(strconv.Itoa(i)),
100+
}
101+
certs = append(certs, cert)
102+
}
103+
return certs
104+
}
105+
87106
func TestNoTlsCertHash(t *testing.T) {
88107
mockCtrl := gomock.NewController(t)
89108
defer mockCtrl.Finish()

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

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ var logger = logging.NewLogger("fabsdk/core")
2222
type certPool struct {
2323
useSystemCertPool bool
2424
certs []*x509.Certificate
25-
certPool *x509.CertPool
2625
certsByName map[string][]int
2726
lock sync.RWMutex
2827
}
@@ -32,38 +31,35 @@ func NewCertPool(useSystemCertPool bool) fab.CertPool {
3231
return &certPool{
3332
useSystemCertPool: useSystemCertPool,
3433
certsByName: make(map[string][]int),
35-
certPool: x509.NewCertPool(),
3634
}
3735
}
3836

3937
func (c *certPool) Get(certs ...*x509.Certificate) (*x509.CertPool, error) {
40-
c.lock.RLock()
41-
if len(certs) == 0 || c.containsCerts(certs...) {
42-
defer c.lock.RUnlock()
43-
return c.certPool, nil
38+
39+
if len(certs) > 0 {
40+
c.lock.Lock()
41+
//add certs to SDK cert list
42+
for _, newCert := range certs {
43+
c.addCert(newCert)
44+
}
45+
c.lock.Unlock()
4446
}
45-
c.lock.RUnlock()
4647

47-
// We have a cert we have not encountered before, recreate the cert pool
48+
c.lock.RLock()
49+
defer c.lock.RUnlock()
50+
51+
// create the cert pool
4852
certPool, err := c.loadSystemCertPool()
4953
if err != nil {
5054
return nil, err
5155
}
5256

53-
c.lock.Lock()
54-
defer c.lock.Unlock()
55-
56-
//add certs to SDK cert list
57-
for _, newCert := range certs {
58-
c.addCert(newCert)
59-
}
6057
//add all certs to cert pool
6158
for _, cert := range c.certs {
6259
certPool.AddCert(cert)
6360
}
64-
c.certPool = certPool
6561

66-
return c.certPool, nil
62+
return certPool, nil
6763
}
6864

6965
func (c *certPool) addCert(newCert *x509.Certificate) {
@@ -88,15 +84,6 @@ func (c *certPool) containsCert(newCert *x509.Certificate) bool {
8884
return false
8985
}
9086

91-
func (c *certPool) containsCerts(certs ...*x509.Certificate) bool {
92-
for _, cert := range certs {
93-
if cert != nil && !c.containsCert(cert) {
94-
return false
95-
}
96-
}
97-
return true
98-
}
99-
10087
func (c *certPool) loadSystemCertPool() (*x509.CertPool, error) {
10188
if !c.useSystemCertPool {
10289
return x509.NewCertPool(), nil

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestTLSCAConfig(t *testing.T) {
2626
_, err := tlsCertPool.Get(goodCert)
2727
require.NoError(t, err)
2828
assert.Equal(t, true, tlsCertPool.useSystemCertPool)
29-
assert.NotNil(t, tlsCertPool.certPool)
3029
assert.NotNil(t, tlsCertPool.certsByName)
3130

3231
originalLength := len(tlsCertPool.certs)
@@ -37,10 +36,10 @@ func TestTLSCAConfig(t *testing.T) {
3736

3837
// Test with system cert pool disabled
3938
tlsCertPool = NewCertPool(false).(*certPool)
40-
_, err = tlsCertPool.Get(goodCert)
39+
certPool, err := tlsCertPool.Get(goodCert)
4140
require.NoError(t, err)
4241
assert.Len(t, tlsCertPool.certs, 1)
43-
assert.Len(t, tlsCertPool.certPool.Subjects(), 1)
42+
assert.Len(t, certPool.Subjects(), 1)
4443
}
4544

4645
func TestTLSCAPoolManyCerts(t *testing.T) {
@@ -103,7 +102,9 @@ func TestConcurrent(t *testing.T) {
103102
}
104103

105104
assert.Len(t, tlsCertPool.certs, concurrency)
106-
assert.Len(t, tlsCertPool.certPool.Subjects(), concurrency)
105+
certPool, err := tlsCertPool.Get()
106+
require.NoError(t, err)
107+
assert.Len(t, certPool.Subjects(), concurrency)
107108
}
108109

109110
func createNCerts(n int) []*x509.Certificate {

pkg/fab/comm/network_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
const configTestFilePath = "../../core/config/testdata/config_test.yaml"
19-
const entityMatcherTestFilePath = "../../core/config/testdata/config_test_entity_matchers.yaml"
19+
const entityMatcherTestFilePath = "../../core/config/testdata/config_test.yaml"
2020
const localOverrideEntityMatcher = "../../../test/fixtures/config/overrides/local_entity_matchers.yaml"
2121

2222
func TestNetworkPeerConfigFromURL(t *testing.T) {

0 commit comments

Comments
 (0)