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

Commit 9cbdc1b

Browse files
committed
[FAB-10568] Identity Config refactoring
- decoupled endpoint config from identity config - identity config should unmarshal its own entities, no need to rely on endpoint config dictionary based search - preloading CA configs - return (entity, bool) for search functions - return entity only for preloaded entitities TODO: Client & CertificateAuthorities to be removed from NetworkConfig Change-Id: Ia53ea227a2bfcdbbf9dec241e7038e3b31f14fca Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent ec9053c commit 9cbdc1b

26 files changed

+448
-479
lines changed

pkg/client/msp/client.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ func New(clientProvider context.ClientProvider, opts ...ClientOption) (*Client,
8080
}
8181

8282
if msp.orgName == "" {
83-
clientConfig, err := ctx.IdentityConfig().Client()
84-
if err != nil {
85-
return nil, errors.WithMessage(err, "failed to create Client")
86-
}
87-
msp.orgName = clientConfig.Organization
83+
msp.orgName = ctx.IdentityConfig().Client().Organization
8884
}
8985
if msp.orgName == "" {
9086
return nil, errors.New("organization is not provided")

pkg/common/providers/fab/network.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ import (
1414

1515
// NetworkConfig provides a static definition of a Hyperledger Fabric network
1616
type NetworkConfig struct {
17-
Name string
18-
Description string
19-
Version string
20-
Client msp.ClientConfig
21-
Channels map[string]ChannelNetworkConfig
22-
Organizations map[string]OrganizationConfig
23-
Orderers map[string]OrdererConfig
24-
Peers map[string]PeerConfig
17+
Name string
18+
Description string
19+
Version string
20+
//TODO to be removed, no apparent reason to expose it in network config
21+
Client msp.ClientConfig
22+
Channels map[string]ChannelNetworkConfig
23+
Organizations map[string]OrganizationConfig
24+
Orderers map[string]OrdererConfig
25+
Peers map[string]PeerConfig
26+
//TODO to be removed, no apparent reason to expose it in network config
2527
CertificateAuthorities map[string]msp.CAConfig
2628
}
2729

pkg/common/providers/msp/provider.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ type IdentityManagerProvider interface {
2525

2626
//IdentityConfig contains identity configurations
2727
type IdentityConfig interface {
28-
Client() (*ClientConfig, error)
29-
CAConfig(org string) (*CAConfig, error)
30-
CAServerCerts(org string) ([][]byte, error)
31-
CAClientKey(org string) ([]byte, error)
32-
CAClientCert(org string) ([]byte, error)
28+
Client() *ClientConfig
29+
CAConfig(org string) (*CAConfig, bool)
30+
CAServerCerts(org string) ([][]byte, bool)
31+
CAClientKey(org string) ([]byte, bool)
32+
CAClientCert(org string) ([]byte, bool)
3333
CAKeyStorePath() string
3434
CredentialStorePath() string
3535
}

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

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

pkg/fab/endpointconfig.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ func (c *EndpointConfig) loadNetworkConfiguration() error {
435435
networkConfig.Description = c.backend.GetString("description")
436436
networkConfig.Version = c.backend.GetString("version")
437437

438+
//TODO: to be removed from NetworkConfig, to be used only in identity Config
438439
err := c.backend.UnmarshalKey("client", &networkConfig.Client)
439440
logger.Debugf("Client is: %+v", networkConfig.Client)
440441
if err != nil {
@@ -465,6 +466,7 @@ func (c *EndpointConfig) loadNetworkConfiguration() error {
465466
return errors.WithMessage(err, "failed to parse 'peers' config item to networkConfig.Peers type")
466467
}
467468

469+
//TODO: to be removed from NetworkConfig, to be used only in identity Config
468470
err = c.backend.UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)
469471
logger.Debugf("certificateAuthorities are: %+v", networkConfig.CertificateAuthorities)
470472
if err != nil {

pkg/fab/endpointconfig_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,38 @@ func TestEndpointConfigWithMultipleBackends(t *testing.T) {
12491249

12501250
}
12511251

1252+
func TestCAConfig(t *testing.T) {
1253+
//Test config
1254+
backend, err := config.FromFile(configTestFilePath)()
1255+
if err != nil {
1256+
t.Fatal("Failed to get config backend")
1257+
}
1258+
1259+
endpointConfig, err := ConfigFromBackend(backend...)
1260+
if err != nil {
1261+
t.Fatal("Failed to get identity config")
1262+
}
1263+
1264+
//Test Crypto config path
1265+
1266+
val, ok := backend[0].Lookup("client.cryptoconfig.path")
1267+
if !ok || val == nil {
1268+
t.Fatal("expected valid value")
1269+
}
1270+
1271+
assert.True(t, pathvar.Subst(val.(string)) == endpointConfig.CryptoConfigPath(), "Incorrect crypto config path", t)
1272+
1273+
//Testing MSPID
1274+
mspID, ok := comm.MSPID(endpointConfig, org1)
1275+
assert.True(t, ok, "Get MSP ID failed")
1276+
assert.True(t, mspID == "Org1MSP", "Get MSP ID failed")
1277+
1278+
// testing empty OrgMSP
1279+
_, ok = comm.MSPID(endpointConfig, "dummyorg1")
1280+
assert.False(t, ok, "Get MSP ID did not fail for dummyorg1")
1281+
1282+
}
1283+
12521284
func TestNetworkPeers(t *testing.T) {
12531285

12541286
endpointConfig, err := ConfigFromBackend(configBackend)

pkg/fab/mocks/mockconfig.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewMockIdentityConfigCustomized(tlsEnabled, mutualTLSEnabled, errorCase boo
6262
}
6363

6464
// Client ...
65-
func (c *MockConfig) Client() (*msp.ClientConfig, error) {
65+
func (c *MockConfig) Client() *msp.ClientConfig {
6666
clientConfig := msp.ClientConfig{}
6767

6868
clientConfig.CredentialStore = msp.CredentialStoreType{
@@ -86,31 +86,31 @@ func (c *MockConfig) Client() (*msp.ClientConfig, error) {
8686
clientConfig.TLSCerts = mutualTLSCerts
8787
}
8888

89-
return &clientConfig, nil
89+
return &clientConfig
9090
}
9191

9292
// CAConfig not implemented
93-
func (c *MockConfig) CAConfig(org string) (*msp.CAConfig, error) {
93+
func (c *MockConfig) CAConfig(org string) (*msp.CAConfig, bool) {
9494
caConfig := msp.CAConfig{
9595
CAName: "org1",
9696
}
9797

98-
return &caConfig, nil
98+
return &caConfig, true
9999
}
100100

101101
//CAServerCerts Read configuration option for the server certificates for given org
102-
func (c *MockConfig) CAServerCerts(org string) ([][]byte, error) {
103-
return nil, nil
102+
func (c *MockConfig) CAServerCerts(org string) ([][]byte, bool) {
103+
return nil, false
104104
}
105105

106106
//CAClientKey Read configuration option for the fabric CA client key for given org
107-
func (c *MockConfig) CAClientKey(org string) ([]byte, error) {
108-
return nil, nil
107+
func (c *MockConfig) CAClientKey(org string) ([]byte, bool) {
108+
return nil, false
109109
}
110110

111111
//CAClientCert Read configuration option for the fabric CA client cert for given org
112-
func (c *MockConfig) CAClientCert(org string) ([]byte, error) {
113-
return nil, nil
112+
func (c *MockConfig) CAClientCert(org string) ([]byte, bool) {
113+
return nil, false
114114
}
115115

116116
//Timeout not implemented

pkg/fabsdk/context.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,9 @@ func WithOrg(org string) ContextOption {
4848
// don't include neither username nor identity
4949
var ErrAnonymousIdentity = errors.New("missing credentials")
5050

51-
func (sdk *FabricSDK) newIdentity(options ...ContextOption) (msp.SigningIdentity, error) { //nolint
52-
clientConfig, err := sdk.provider.IdentityConfig().Client()
53-
if err != nil {
54-
return nil, errors.WithMessage(err, "retrieving client configuration failed")
55-
}
56-
51+
func (sdk *FabricSDK) newIdentity(options ...ContextOption) (msp.SigningIdentity, error) {
5752
opts := identityOptions{
58-
orgName: clientConfig.Organization,
53+
orgName: sdk.provider.IdentityConfig().Client().Organization,
5954
}
6055

6156
for _, option := range options {

pkg/fabsdk/fabsdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func (sdk *FabricSDK) loadConfigs(configProvider core.ConfigProvider) (*configs,
392392
}
393393

394394
// load identity config
395-
c.identityConfig, err = sdk.loadIdentityConfig(c.endpointConfig, configBackend...)
395+
c.identityConfig, err = sdk.loadIdentityConfig(configBackend...)
396396
if err != nil {
397397
return nil, errors.WithMessage(err, "unalbe to load identity config")
398398
}
@@ -448,11 +448,11 @@ func (sdk *FabricSDK) loadCryptoConfig(configBackend ...core.ConfigBackend) (cor
448448
return cryptoConfigOpt, nil
449449
}
450450

451-
func (sdk *FabricSDK) loadIdentityConfig(endpointConfig fab.EndpointConfig, configBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {
451+
func (sdk *FabricSDK) loadIdentityConfig(configBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {
452452
identityConfigOpt, ok := sdk.opts.IdentityConfig.(*mspImpl.IdentityConfigOptions)
453453

454454
if sdk.opts.IdentityConfig == nil || (ok && !mspImpl.IsIdentityConfigFullyOverridden(identityConfigOpt)) {
455-
defIdentityConfig, err := mspImpl.ConfigFromEndpointConfig(endpointConfig, configBackend...)
455+
defIdentityConfig, err := mspImpl.ConfigFromBackend(configBackend...)
456456
if err != nil {
457457
return nil, errors.WithMessage(err, "failed to initialize identity config from config backend")
458458
}

pkg/fabsdk/fabsdk_chconfig_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,7 @@ func checkClientOrg(configBackend core.ConfigBackend, t *testing.T, orgName stri
138138
if err != nil {
139139
t.Fatalf("Error getting identity config : %s", err)
140140
}
141-
client, err := identityConfig.Client()
142-
if err != nil {
143-
t.Fatalf("Error getting client from config: %s", err)
144-
}
141+
client := identityConfig.Client()
145142
if client.Organization != orgName {
146143
t.Fatalf("Unexpected org in config: %s", client.Organization)
147144
}

0 commit comments

Comments
 (0)