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

Commit 7ecb50d

Browse files
committed
[FAB-9954] sdk.New() updating identityconfig
-Since identity config uses endpoint config in the background when someone overrides endpoint config, corresponding updates have to be made in identity config too. Change-Id: Id819dee588e5154863b2ececeffa2aaff1a8c280 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent f06da85 commit 7ecb50d

File tree

4 files changed

+122
-44
lines changed

4 files changed

+122
-44
lines changed

pkg/fab/endpointconfig.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ func ConfigFromBackend(coreBackend ...core.ConfigBackend) (fab.EndpointConfig, e
6464
backend: lookup.New(coreBackend...),
6565
peerMatchers: make(map[int]*regexp.Regexp),
6666
ordererMatchers: make(map[int]*regexp.Regexp),
67-
caMatchers: make(map[int]*regexp.Regexp),
6867
channelMatchers: make(map[int]*regexp.Regexp),
6968
}
7069

@@ -101,7 +100,6 @@ type EndpointConfig struct {
101100
networkConfigCached bool
102101
peerMatchers map[int]*regexp.Regexp
103102
ordererMatchers map[int]*regexp.Regexp
104-
caMatchers map[int]*regexp.Regexp
105103
channelMatchers map[int]*regexp.Regexp
106104
}
107105

@@ -985,11 +983,6 @@ func (c *EndpointConfig) compileMatchers() error {
985983
return err
986984
}
987985

988-
err = c.compileCertificateAuthorityMatcher(networkConfig)
989-
if err != nil {
990-
return err
991-
}
992-
993986
err = c.compileChannelMatcher(networkConfig)
994987
return err
995988
}
@@ -1010,22 +1003,6 @@ func (c *EndpointConfig) compileChannelMatcher(networkConfig *fab.NetworkConfig)
10101003
return nil
10111004
}
10121005

1013-
func (c *EndpointConfig) compileCertificateAuthorityMatcher(networkConfig *fab.NetworkConfig) error {
1014-
var err error
1015-
if networkConfig.EntityMatchers["certificateauthority"] != nil {
1016-
certMatchersConfig := networkConfig.EntityMatchers["certificateauthority"]
1017-
for i := 0; i < len(certMatchersConfig); i++ {
1018-
if certMatchersConfig[i].Pattern != "" {
1019-
c.caMatchers[i], err = regexp.Compile(certMatchersConfig[i].Pattern)
1020-
if err != nil {
1021-
return err
1022-
}
1023-
}
1024-
}
1025-
}
1026-
return nil
1027-
}
1028-
10291006
func (c *EndpointConfig) compileOrdererMatcher(networkConfig *fab.NetworkConfig) error {
10301007
var err error
10311008
if networkConfig.EntityMatchers["orderer"] != nil {
@@ -1115,16 +1092,6 @@ func (c *EndpointConfig) client() (*msp.ClientConfig, error) {
11151092
return &client, nil
11161093
}
11171094

1118-
//Backend returns config lookup of endpoint config
1119-
func (c *EndpointConfig) Backend() *lookup.ConfigLookup {
1120-
return c.backend
1121-
}
1122-
1123-
//CAMatchers returns CA matchers of endpoint config
1124-
func (c *EndpointConfig) CAMatchers() map[int]*regexp.Regexp {
1125-
return c.caMatchers
1126-
}
1127-
11281095
//ResetNetworkConfig clears network config cache
11291096
func (c *EndpointConfig) ResetNetworkConfig() {
11301097
c.networkConfig = nil

pkg/fabsdk/fabsdk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (sdk *FabricSDK) loadConfigs(configProvider core.ConfigProvider) (*configs,
384384
}
385385

386386
if c.identityConfig == nil {
387-
c.identityConfig, err = mspImpl.ConfigFromBackend(configBackend...)
387+
c.identityConfig, err = mspImpl.ConfigFromEndpointConfig(c.endpointConfig, configBackend...)
388388
if err != nil {
389389
return nil, errors.WithMessage(err, "failed to initialize identity config from config backend")
390390
}

pkg/msp/identityconfig.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,44 @@ import (
2121
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
2222
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
2323
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
24+
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
2425
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab"
2526
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
2627
)
2728

28-
//ConfigFromBackend returns identity config implementation of give backend
29+
//ConfigFromBackend returns identity config implementation of given backend
2930
func ConfigFromBackend(coreBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {
31+
//prepare underlying endpoint config
3032
endpointConfig, err := fabImpl.ConfigFromBackend(coreBackend...)
3133
if err != nil {
3234
return nil, errors.New("failed load identity configuration")
3335
}
34-
return &IdentityConfig{endpointConfig.(*fabImpl.EndpointConfig)}, nil
36+
37+
return ConfigFromEndpointConfig(endpointConfig, coreBackend...)
38+
}
39+
40+
//ConfigFromEndpointConfig returns identity config implementation of given endpoint config and backend
41+
func ConfigFromEndpointConfig(endpointConfig fab.EndpointConfig, coreBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {
42+
43+
//create identity config
44+
config := &IdentityConfig{endpointConfig: endpointConfig,
45+
backend: lookup.New(coreBackend...),
46+
caMatchers: make(map[int]*regexp.Regexp)}
47+
48+
//compile CA matchers
49+
err := config.compileMatchers()
50+
if err != nil {
51+
return nil, errors.WithMessage(err, "failed to compile certificate authority matchers")
52+
}
53+
54+
return config, nil
3555
}
3656

3757
// IdentityConfig represents the identity configuration for the client
3858
type IdentityConfig struct {
39-
endpointConfig *fabImpl.EndpointConfig
59+
endpointConfig fab.EndpointConfig
60+
backend *lookup.ConfigLookup
61+
caMatchers map[int]*regexp.Regexp
4062
}
4163

4264
// Client returns the Client config
@@ -171,12 +193,12 @@ func (c *IdentityConfig) CAServerCerts(org string) ([][]byte, error) {
171193
// 'keystore' directory added. This is done because the fabric-ca-client
172194
// adds this to the path
173195
func (c *IdentityConfig) CAKeyStorePath() string {
174-
return pathvar.Subst(c.endpointConfig.Backend().GetString("client.credentialStore.cryptoStore.path"))
196+
return pathvar.Subst(c.backend.GetString("client.credentialStore.cryptoStore.path"))
175197
}
176198

177199
// CredentialStorePath returns the user store path
178200
func (c *IdentityConfig) CredentialStorePath() string {
179-
return pathvar.Subst(c.endpointConfig.Backend().GetString("client.credentialStore.path"))
201+
return pathvar.Subst(c.backend.GetString("client.credentialStore.path"))
180202
}
181203

182204
// NetworkConfig returns the network configuration defined in the config file
@@ -189,21 +211,20 @@ func (c *IdentityConfig) networkConfig() (*fab.NetworkConfig, error) {
189211

190212
func (c *IdentityConfig) tryMatchingCAConfig(networkConfig *fab.NetworkConfig, caName string) (*msp.CAConfig, string) {
191213
//Return if no caMatchers are configured
192-
caMatchers := c.endpointConfig.CAMatchers()
193-
if len(caMatchers) == 0 {
214+
if len(c.caMatchers) == 0 {
194215
return nil, ""
195216
}
196217

197218
//sort the keys
198219
var keys []int
199-
for k := range caMatchers {
220+
for k := range c.caMatchers {
200221
keys = append(keys, k)
201222
}
202223
sort.Ints(keys)
203224

204225
//loop over certAuthorityEntityMatchers to find the matching Cert
205226
for _, k := range keys {
206-
v := caMatchers[k]
227+
v := c.caMatchers[k]
207228
if v.MatchString(caName) {
208229
return c.findMatchingCert(networkConfig, caName, v, k)
209230
}
@@ -252,3 +273,22 @@ func (c *IdentityConfig) getPortIfPresent(url string) (int, bool) {
252273
}
253274
return 0, false
254275
}
276+
277+
func (c *IdentityConfig) compileMatchers() error {
278+
networkConfig, err := c.endpointConfig.NetworkConfig()
279+
if err != nil {
280+
return err
281+
}
282+
if networkConfig.EntityMatchers["certificateauthority"] != nil {
283+
certMatchersConfig := networkConfig.EntityMatchers["certificateauthority"]
284+
for i := 0; i < len(certMatchersConfig); i++ {
285+
if certMatchersConfig[i].Pattern != "" {
286+
c.caMatchers[i], err = regexp.Compile(certMatchersConfig[i].Pattern)
287+
if err != nil {
288+
return err
289+
}
290+
}
291+
}
292+
}
293+
return nil
294+
}

pkg/msp/identityconfig_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
"strings"
1414

1515
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
16+
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
1617
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
1718
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
1819
"github.com/hyperledger/fabric-sdk-go/pkg/core/mocks"
20+
"github.com/hyperledger/fabric-sdk-go/pkg/fab"
1921
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
2022
"github.com/spf13/viper"
2123
"github.com/stretchr/testify/assert"
@@ -59,7 +61,7 @@ func TestCAConfigFailsByNetworkConfig(t *testing.T) {
5961
}
6062

6163
sampleIdentityConfig := identityCfg.(*IdentityConfig)
62-
sampleIdentityConfig.endpointConfig.ResetNetworkConfig()
64+
sampleIdentityConfig.endpointConfig.(*fab.EndpointConfig).ResetNetworkConfig()
6365

6466
customBackend.KeyValueMap["channels"] = "INVALID"
6567
_, err = sampleIdentityConfig.networkConfig()
@@ -389,6 +391,61 @@ func TestCAConfig(t *testing.T) {
389391

390392
}
391393

394+
func TestCAConfigWithCustomEndpointConfig(t *testing.T) {
395+
//Test config
396+
backend, err := config.FromFile(configTestFilePath)()
397+
if err != nil {
398+
t.Fatal("Failed to get config backend")
399+
}
400+
401+
endpointConfig, err := fab.ConfigFromBackend(backend...)
402+
if err != nil {
403+
t.Fatal("Failed to get endpoint config")
404+
}
405+
406+
config, err := ConfigFromEndpointConfig(&customEndpointConfig{endpointConfig}, backend...)
407+
if err != nil {
408+
t.Fatal("Failed to get identity config")
409+
}
410+
identityConfig := config.(*IdentityConfig)
411+
//Test Crypto config path
412+
413+
val, ok := backend[0].Lookup("client.cryptoconfig.path")
414+
if !ok || val == nil {
415+
t.Fatal("expected valid value")
416+
}
417+
418+
assert.True(t, pathvar.Subst(val.(string)) == identityConfig.endpointConfig.CryptoConfigPath(), "Incorrect crypto config path", t)
419+
420+
//Testing MSPID
421+
mspID, err := identityConfig.endpointConfig.MSPID(org1)
422+
assert.Nil(t, err, "Get MSP ID failed")
423+
assert.True(t, mspID == "Org1MSP", "Get MSP ID failed")
424+
425+
// testing empty OrgMSP
426+
_, err = identityConfig.endpointConfig.MSPID("dummyorg1")
427+
assert.NotNil(t, err, "Get MSP ID did not fail for dummyorg1")
428+
assert.True(t, err.Error() == "MSP ID is empty for org: dummyorg1", "Get MSP ID did not fail for dummyorg1")
429+
430+
//Testing CAConfig
431+
caConfig, err := identityConfig.CAConfig(org1)
432+
assert.Nil(t, err, "Get CA Config failed")
433+
assert.NotNil(t, caConfig, "Get CA Config failed")
434+
435+
// Test CA KeyStore Path
436+
testCAKeyStorePath(backend[0], t, identityConfig)
437+
438+
// test Client
439+
c, err := identityConfig.Client()
440+
assert.Nil(t, err, "Received error when fetching Client info")
441+
assert.NotNil(t, c, "Received error when fetching Client info")
442+
443+
client, err := identityConfig.Client()
444+
assert.Nil(t, err)
445+
assert.Equal(t, "custom-org1", client.Organization, "supposed to get custom org name from custom endpointconfig")
446+
447+
}
448+
392449
func testCAKeyStorePath(backend core.ConfigBackend, t *testing.T, identityConfig *IdentityConfig) {
393450
// Test User Store Path
394451
val, ok := backend.Lookup("client.credentialStore.path")
@@ -502,3 +559,17 @@ func newViper(path string) *viper.Viper {
502559
}
503560
return myViper
504561
}
562+
563+
//customEndpointConfig to demonstrate custom endpoint config in identity config
564+
type customEndpointConfig struct {
565+
fabImpl.EndpointConfig
566+
}
567+
568+
func (c *customEndpointConfig) NetworkConfig() (*fabImpl.NetworkConfig, error) {
569+
nConfig, err := c.EndpointConfig.NetworkConfig()
570+
if err != nil {
571+
return nil, err
572+
}
573+
nConfig.Client.Organization = "CUSTOM-ORG1"
574+
return nConfig, nil
575+
}

0 commit comments

Comments
 (0)