Skip to content

Commit

Permalink
[FAB-9954] sdk.New() updating identityconfig
Browse files Browse the repository at this point in the history
-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>
  • Loading branch information
sudeshrshetty committed May 8, 2018
1 parent f06da85 commit 7ecb50d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 44 deletions.
33 changes: 0 additions & 33 deletions pkg/fab/endpointconfig.go
Expand Up @@ -64,7 +64,6 @@ func ConfigFromBackend(coreBackend ...core.ConfigBackend) (fab.EndpointConfig, e
backend: lookup.New(coreBackend...),
peerMatchers: make(map[int]*regexp.Regexp),
ordererMatchers: make(map[int]*regexp.Regexp),
caMatchers: make(map[int]*regexp.Regexp),
channelMatchers: make(map[int]*regexp.Regexp),
}

Expand Down Expand Up @@ -101,7 +100,6 @@ type EndpointConfig struct {
networkConfigCached bool
peerMatchers map[int]*regexp.Regexp
ordererMatchers map[int]*regexp.Regexp
caMatchers map[int]*regexp.Regexp
channelMatchers map[int]*regexp.Regexp
}

Expand Down Expand Up @@ -985,11 +983,6 @@ func (c *EndpointConfig) compileMatchers() error {
return err
}

err = c.compileCertificateAuthorityMatcher(networkConfig)
if err != nil {
return err
}

err = c.compileChannelMatcher(networkConfig)
return err
}
Expand All @@ -1010,22 +1003,6 @@ func (c *EndpointConfig) compileChannelMatcher(networkConfig *fab.NetworkConfig)
return nil
}

func (c *EndpointConfig) compileCertificateAuthorityMatcher(networkConfig *fab.NetworkConfig) error {
var err error
if networkConfig.EntityMatchers["certificateauthority"] != nil {
certMatchersConfig := networkConfig.EntityMatchers["certificateauthority"]
for i := 0; i < len(certMatchersConfig); i++ {
if certMatchersConfig[i].Pattern != "" {
c.caMatchers[i], err = regexp.Compile(certMatchersConfig[i].Pattern)
if err != nil {
return err
}
}
}
}
return nil
}

func (c *EndpointConfig) compileOrdererMatcher(networkConfig *fab.NetworkConfig) error {
var err error
if networkConfig.EntityMatchers["orderer"] != nil {
Expand Down Expand Up @@ -1115,16 +1092,6 @@ func (c *EndpointConfig) client() (*msp.ClientConfig, error) {
return &client, nil
}

//Backend returns config lookup of endpoint config
func (c *EndpointConfig) Backend() *lookup.ConfigLookup {
return c.backend
}

//CAMatchers returns CA matchers of endpoint config
func (c *EndpointConfig) CAMatchers() map[int]*regexp.Regexp {
return c.caMatchers
}

//ResetNetworkConfig clears network config cache
func (c *EndpointConfig) ResetNetworkConfig() {
c.networkConfig = nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/fabsdk/fabsdk.go
Expand Up @@ -384,7 +384,7 @@ func (sdk *FabricSDK) loadConfigs(configProvider core.ConfigProvider) (*configs,
}

if c.identityConfig == nil {
c.identityConfig, err = mspImpl.ConfigFromBackend(configBackend...)
c.identityConfig, err = mspImpl.ConfigFromEndpointConfig(c.endpointConfig, configBackend...)
if err != nil {
return nil, errors.WithMessage(err, "failed to initialize identity config from config backend")
}
Expand Down
58 changes: 49 additions & 9 deletions pkg/msp/identityconfig.go
Expand Up @@ -21,22 +21,44 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
)

//ConfigFromBackend returns identity config implementation of give backend
//ConfigFromBackend returns identity config implementation of given backend
func ConfigFromBackend(coreBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {
//prepare underlying endpoint config
endpointConfig, err := fabImpl.ConfigFromBackend(coreBackend...)
if err != nil {
return nil, errors.New("failed load identity configuration")
}
return &IdentityConfig{endpointConfig.(*fabImpl.EndpointConfig)}, nil

return ConfigFromEndpointConfig(endpointConfig, coreBackend...)
}

//ConfigFromEndpointConfig returns identity config implementation of given endpoint config and backend
func ConfigFromEndpointConfig(endpointConfig fab.EndpointConfig, coreBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {

//create identity config
config := &IdentityConfig{endpointConfig: endpointConfig,
backend: lookup.New(coreBackend...),
caMatchers: make(map[int]*regexp.Regexp)}

//compile CA matchers
err := config.compileMatchers()
if err != nil {
return nil, errors.WithMessage(err, "failed to compile certificate authority matchers")
}

return config, nil
}

// IdentityConfig represents the identity configuration for the client
type IdentityConfig struct {
endpointConfig *fabImpl.EndpointConfig
endpointConfig fab.EndpointConfig
backend *lookup.ConfigLookup
caMatchers map[int]*regexp.Regexp
}

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

// CredentialStorePath returns the user store path
func (c *IdentityConfig) CredentialStorePath() string {
return pathvar.Subst(c.endpointConfig.Backend().GetString("client.credentialStore.path"))
return pathvar.Subst(c.backend.GetString("client.credentialStore.path"))
}

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

func (c *IdentityConfig) tryMatchingCAConfig(networkConfig *fab.NetworkConfig, caName string) (*msp.CAConfig, string) {
//Return if no caMatchers are configured
caMatchers := c.endpointConfig.CAMatchers()
if len(caMatchers) == 0 {
if len(c.caMatchers) == 0 {
return nil, ""
}

//sort the keys
var keys []int
for k := range caMatchers {
for k := range c.caMatchers {
keys = append(keys, k)
}
sort.Ints(keys)

//loop over certAuthorityEntityMatchers to find the matching Cert
for _, k := range keys {
v := caMatchers[k]
v := c.caMatchers[k]
if v.MatchString(caName) {
return c.findMatchingCert(networkConfig, caName, v, k)
}
Expand Down Expand Up @@ -252,3 +273,22 @@ func (c *IdentityConfig) getPortIfPresent(url string) (int, bool) {
}
return 0, false
}

func (c *IdentityConfig) compileMatchers() error {
networkConfig, err := c.endpointConfig.NetworkConfig()
if err != nil {
return err
}
if networkConfig.EntityMatchers["certificateauthority"] != nil {
certMatchersConfig := networkConfig.EntityMatchers["certificateauthority"]
for i := 0; i < len(certMatchersConfig); i++ {
if certMatchersConfig[i].Pattern != "" {
c.caMatchers[i], err = regexp.Compile(certMatchersConfig[i].Pattern)
if err != nil {
return err
}
}
}
}
return nil
}
73 changes: 72 additions & 1 deletion pkg/msp/identityconfig_test.go
Expand Up @@ -13,9 +13,11 @@ import (
"strings"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
fabImpl "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"github.com/hyperledger/fabric-sdk-go/pkg/core/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,7 +61,7 @@ func TestCAConfigFailsByNetworkConfig(t *testing.T) {
}

sampleIdentityConfig := identityCfg.(*IdentityConfig)
sampleIdentityConfig.endpointConfig.ResetNetworkConfig()
sampleIdentityConfig.endpointConfig.(*fab.EndpointConfig).ResetNetworkConfig()

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

}

func TestCAConfigWithCustomEndpointConfig(t *testing.T) {
//Test config
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

endpointConfig, err := fab.ConfigFromBackend(backend...)
if err != nil {
t.Fatal("Failed to get endpoint config")
}

config, err := ConfigFromEndpointConfig(&customEndpointConfig{endpointConfig}, backend...)
if err != nil {
t.Fatal("Failed to get identity config")
}
identityConfig := config.(*IdentityConfig)
//Test Crypto config path

val, ok := backend[0].Lookup("client.cryptoconfig.path")
if !ok || val == nil {
t.Fatal("expected valid value")
}

assert.True(t, pathvar.Subst(val.(string)) == identityConfig.endpointConfig.CryptoConfigPath(), "Incorrect crypto config path", t)

//Testing MSPID
mspID, err := identityConfig.endpointConfig.MSPID(org1)
assert.Nil(t, err, "Get MSP ID failed")
assert.True(t, mspID == "Org1MSP", "Get MSP ID failed")

// testing empty OrgMSP
_, err = identityConfig.endpointConfig.MSPID("dummyorg1")
assert.NotNil(t, err, "Get MSP ID did not fail for dummyorg1")
assert.True(t, err.Error() == "MSP ID is empty for org: dummyorg1", "Get MSP ID did not fail for dummyorg1")

//Testing CAConfig
caConfig, err := identityConfig.CAConfig(org1)
assert.Nil(t, err, "Get CA Config failed")
assert.NotNil(t, caConfig, "Get CA Config failed")

// Test CA KeyStore Path
testCAKeyStorePath(backend[0], t, identityConfig)

// test Client
c, err := identityConfig.Client()
assert.Nil(t, err, "Received error when fetching Client info")
assert.NotNil(t, c, "Received error when fetching Client info")

client, err := identityConfig.Client()
assert.Nil(t, err)
assert.Equal(t, "custom-org1", client.Organization, "supposed to get custom org name from custom endpointconfig")

}

func testCAKeyStorePath(backend core.ConfigBackend, t *testing.T, identityConfig *IdentityConfig) {
// Test User Store Path
val, ok := backend.Lookup("client.credentialStore.path")
Expand Down Expand Up @@ -502,3 +559,17 @@ func newViper(path string) *viper.Viper {
}
return myViper
}

//customEndpointConfig to demonstrate custom endpoint config in identity config
type customEndpointConfig struct {
fabImpl.EndpointConfig
}

func (c *customEndpointConfig) NetworkConfig() (*fabImpl.NetworkConfig, error) {
nConfig, err := c.EndpointConfig.NetworkConfig()
if err != nil {
return nil, err
}
nConfig.Client.Organization = "CUSTOM-ORG1"
return nConfig, nil
}

0 comments on commit 7ecb50d

Please sign in to comment.