Skip to content

Commit

Permalink
[FAB-9602] entity matcher refactoring
Browse files Browse the repository at this point in the history
- entity matchers aren't mandatory anymore
- Merged EndpointConfig.PeerConfig and
EndpointConfig.PeerConfigByURL to single function.
- Refactered below config functions,
 * EndpointConfig.PeerConfig - return error if
peerConfig not found, never return nil peerConfig
* EndpointConfig.PeersConfig - will skip the peer
if corresponding peer config not found, instead of
failing as whole
* EndpointConfig.ChannelPeers - will skip the peer
if corresponding peer config not found, instead of
failing as whole
* EndpointConfig.OrdererConfig - return error if
ordererConfig not found, never return nil ordererConfig.
* IdentityConfig CA lookups - will not fail if entity
matchers not found.
- endpoint discovery to skip the peers for which config is
not found, but will fail if in case of error while reading
peer config.
- entity matchers customization removed from usual test config
files.



Change-Id: Ieb64514862ab1f3c03878397602d9fee88e71dfa
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Apr 23, 2018
1 parent 637b655 commit 0728814
Show file tree
Hide file tree
Showing 32 changed files with 680 additions and 705 deletions.
7 changes: 2 additions & 5 deletions pkg/client/common/discovery/dynamicdiscovery/service.go
Expand Up @@ -114,15 +114,12 @@ func asPeers(ctx contextAPI.Client, endpoints []*discclient.Peer) []fab.Peer {

logger.Debugf("Adding endpoint [%s]", url)

peerConfig, err := ctx.EndpointConfig().PeerConfigByURL(url)
peerConfig, err := ctx.EndpointConfig().PeerConfig(url)
if err != nil {
logger.Warnf("Error getting peer config for url [%s]: %s", err)
continue
}
if peerConfig == nil {
logger.Warnf("Unable to resolve peer config for [%s]", url)
continue
}

peer, err := ctx.InfraProvider().CreatePeerFromConfig(&fab.NetworkPeer{PeerConfig: *peerConfig, MSPID: endpoint.MSPID})
if err != nil {
logger.Warnf("Unable to create peer config for [%s]: %s", url, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/common/filter/endpoint.go
Expand Up @@ -48,8 +48,8 @@ type EndpointFilter struct {
// Accept returns false if this peer is to be excluded from the target list
func (f *EndpointFilter) Accept(peer fab.Peer) bool {

peerConfig, err := f.ctx.EndpointConfig().PeerConfigByURL(peer.URL())
if err != nil || peerConfig == nil {
peerConfig, err := f.ctx.EndpointConfig().PeerConfig(peer.URL())
if err != nil {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/ledger/opts_test.go
Expand Up @@ -64,7 +64,7 @@ func TestWithTargetURLsValid(t *testing.T) {

opts := requestOptions{}
err := opt(ctx, &opts)
assert.Nil(t, err, "Should have failed for invalid target peer")
assert.Nil(t, err, "Should not have failed for invalid target peer")

assert.Equal(t, 1, len(opts.Targets), "should have one peer")
assert.Equal(t, pConfig1.URL, opts.Targets[0].URL(), "", "Wrong URL")
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/msp/client_test.go
Expand Up @@ -241,13 +241,13 @@ func getCustomBackend(backend core.ConfigBackend) *mocks.MockConfigBackend {
configLookup := lookup.New(backend)
configLookup.UnmarshalKey("certificateAuthorities", &networkConfig.CertificateAuthorities)

ca1Config := networkConfig.CertificateAuthorities["local.ca.org1.example.com"]
ca1Config := networkConfig.CertificateAuthorities["ca.org1.example.com"]
ca1Config.URL = caServerURL
ca2Config := networkConfig.CertificateAuthorities["local.ca.org2.example.com"]
ca2Config := networkConfig.CertificateAuthorities["ca.org2.example.com"]
ca2Config.URL = caServerURL

networkConfig.CertificateAuthorities["local.ca.org1.example.com"] = ca1Config
networkConfig.CertificateAuthorities["local.ca.org2.example.com"] = ca2Config
networkConfig.CertificateAuthorities["ca.org1.example.com"] = ca1Config
networkConfig.CertificateAuthorities["ca.org2.example.com"] = ca2Config
backendMap["certificateAuthorities"] = networkConfig.CertificateAuthorities

return &mocks.MockConfigBackend{KeyValueMap: backendMap, CustomBackend: backend}
Expand Down
5 changes: 1 addition & 4 deletions pkg/client/resmgmt/opts.go
Expand Up @@ -88,10 +88,7 @@ func WithOrdererURL(url string) RequestOption {

ordererCfg, err := ctx.EndpointConfig().OrdererConfig(url)
if err != nil {
return errors.WithMessage(err, "orderer not found")
}
if ordererCfg == nil {
return errors.New("orderer not found")
return errors.Wrapf(err, "orderer not found for url : %s", url)
}

orderer, err := ctx.InfraProvider().CreateOrdererFromConfig(ordererCfg)
Expand Down
5 changes: 2 additions & 3 deletions pkg/client/resmgmt/resmgmt_test.go
Expand Up @@ -357,7 +357,6 @@ func TestJoinChannelNoOrdererConfig(t *testing.T) {
if err == nil {
t.Fatalf("Should have failed to join channel since global orderer certs are not configured properly")
}
fmt.Println(err)
}

func TestIsChaincodeInstalled(t *testing.T) {
Expand Down Expand Up @@ -1642,10 +1641,10 @@ func getInvalidOrdererBackend(backend core.ConfigBackend) *mocks.MockConfigBacke
if err != nil {
panic(err)
}
exampleOrderer := networkConfig.Orderers["local.orderer.example.com"]
exampleOrderer := networkConfig.Orderers["orderer.example.com"]
exampleOrderer.TLSCACerts.Path = "/some/invalid/path"
exampleOrderer.TLSCACerts.Pem = ""
networkConfig.Orderers["local.orderer.example.com"] = exampleOrderer
networkConfig.Orderers["orderer.example.com"] = exampleOrderer

mockConfigBackend := getCustomBackend(backend)
mockConfigBackend.KeyValueMap["orderers"] = networkConfig.Orderers
Expand Down
3 changes: 1 addition & 2 deletions pkg/common/providers/fab/provider.go
Expand Up @@ -90,8 +90,7 @@ type EndpointConfig interface {
OrderersConfig() ([]OrdererConfig, error)
OrdererConfig(name string) (*OrdererConfig, error)
PeersConfig(org string) ([]PeerConfig, error)
PeerConfig(org string, name string) (*PeerConfig, error)
PeerConfigByURL(url string) (*PeerConfig, error)
PeerConfig(nameOrURL string) (*PeerConfig, error)
NetworkConfig() (*NetworkConfig, error)
NetworkPeers() ([]NetworkPeer, error)
ChannelConfig(name string) (*ChannelNetworkConfig, error)
Expand Down
21 changes: 4 additions & 17 deletions pkg/common/providers/test/mockfab/mockfab.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/core/config/lookup/lookup_test.go
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/stretchr/testify/assert"
)

var sampleConfigFile = "../testdata/config_test.yaml"
var sampleConfigFile = "../testdata/config_test_entity_matchers.yaml"

const orgChannelID = "orgchannel"

Expand Down
105 changes: 6 additions & 99 deletions pkg/core/config/testdata/config_test.yaml
Expand Up @@ -217,7 +217,7 @@ organizations:
# SDK is implementation specific. Consult each SDK's documentation for its handling of orderers.
#
orderers:
local.orderer.example.com:
orderer.example.com:
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
Expand All @@ -244,7 +244,7 @@ orderers:
# and event listener registration.
#
peers:
local.peer0.org1.example.com:
peer0.org1.example.com:
# this URL is used to send endorsement and query requests
url: peer0.org1.example.com:7051

Expand All @@ -268,7 +268,7 @@ peers:
# Certificate location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem

local.peer0.org2.example.com:
peer0.org2.example.com:
url: peer0.org2.example.com:8051
eventUrl: peer0.org2.example.com:8053
#TODO to be moved to high level, common for all grpc connections
Expand All @@ -293,7 +293,7 @@ peers:
# Certificate Authority instead of Fabric-CA, in which case this section would not be specified.
#
certificateAuthorities:
local.ca.org1.example.com:
ca.org1.example.com:
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
Expand All @@ -312,7 +312,7 @@ certificateAuthorities:
enrollSecret: adminpw
# [Optional] The optional name of the CA.
caName: ca.org1.example.com
local.ca.org2.example.com:
ca.org2.example.com:
url: https://ca.org2.example.com:8054
tlsCACerts:
# Comma-Separated list of paths
Expand All @@ -330,97 +330,4 @@ certificateAuthorities:
enrollId: admin
enrollSecret: adminpw
# [Optional] The optional name of the CA.
caName: ca.org2.example.com

# EntityMatchers enable substitution of network hostnames with static configurations
# so that properties can be mapped. Regex can be used for this purpose
# UrlSubstitutionExp can be empty which means the same network hostname will be used
# UrlSubstitutionExp can be given same as mapped peer url, so that mapped peer url can be used
# UrlSubstitutionExp can have golang regex matchers like $1.local.example.$2:$3 for pattern
# like peer0.org1.example.com:1234 which converts peer0.org1.example.com to peer0.org1.local.example.com:1234
# EventUrlSubstitutionExp and sslTargetOverrideUrlSubstitutionExp follow in the same lines as
# SubstitutionExp for the fields eventUrl and gprcOptions.ssl-target-name-override respectively
# In any case mappedHost's config will be used, so mapped host cannot be empty, if entityMatchers are used
entityMatchers:
peer:
- pattern: (\w+).org1.example.(\w+)
urlSubstitutionExp: peer0.org1.example.com:7051
eventUrlSubstitutionExp: peer0.org1.example.com:7053
sslTargetOverrideUrlSubstitutionExp: peer0.org1.example.com
mappedHost: local.peer0.org1.example.com

- pattern: (\w+).org2.example.(\w+)
urlSubstitutionExp: peer0.org2.example.com:8051
eventUrlSubstitutionExp: peer0.org2.example.com:8053
sslTargetOverrideUrlSubstitutionExp: peer0.org2.example.com
mappedHost: local.peer0.org2.example.com

- pattern: (\w+).example5.(\w+)
urlSubstitutionExp: localhost:7051
eventUrlSubstitutionExp: localhost:7053
sslTargetOverrideUrlSubstitutionExp: localhost
mappedHost: local.peer0.org1.example.com

- pattern: (\w+).example2.(\w+):(\d+)
urlSubstitutionExp: localhost:7051
eventUrlSubstitutionExp: localhost:7053
sslTargetOverrideUrlSubstitutionExp: localhost
mappedHost: local.peer0.org2.example.com

- pattern: (\w+).example3.(\w+)
urlSubstitutionExp:
eventUrlSubstitutionExp:
sslTargetOverrideUrlSubstitutionExp:
mappedHost: local.peer0.org1.example.com

- pattern: (\w+).example4.(\w+):(\d+)
urlSubstitutionExp: $1.org1.example.$2:$3
eventUrlSubstitutionExp: $1.org1.example.$2:7053
sslTargetOverrideUrlSubstitutionExp: $1.org1.example.$2
mappedHost: local.peer0.org1.example.com

- pattern: (\w+).example2.com:(\d+)
urlSubstitutionExp: peer0.org2.example.com:7051
eventUrlSubstitutionExp:
sslTargetOverrideUrlSubstitutionExp:
mappedHost: local.peer0.org2.example.com

- pattern: (\w+).org1.example.(\w+):(\d+)
urlSubstitutionExp: peer0.org1.example.com:7051
eventUrlSubstitutionExp: peer0.org1.example.com:7053
sslTargetOverrideUrlSubstitutionExp: peer0.org1.example.com
mappedHost: local.peer0.org1.example.com

orderer:
- pattern: (\w+).example2.(\w+)
urlSubstitutionExp: localhost:7050
sslTargetOverrideUrlSubstitutionExp: localhost
mappedHost: local.orderer.example.com

- pattern: (\w+).example.(\w+)
urlSubstitutionExp: orderer.example.com:7050
sslTargetOverrideUrlSubstitutionExp: orderer.example.com
mappedHost: local.orderer.example.com

- pattern: (\w+).example3.(\w+)
urlSubstitutionExp:
sslTargetOverrideUrlSubstitutionExp:
mappedHost: local.orderer.example.com

- pattern: (\w+).example4.(\w+):(\d+)
urlSubstitutionExp: $1.example.$2:$3
sslTargetOverrideUrlSubstitutionExp: $1.example.$2
mappedHost: local.orderer.example.com

certificateAuthority:
- pattern: (\w+).org1.example.(\w+)
urlSubstitutionExp:
mappedHost: local.ca.org1.example.com

- pattern: (\w+).org2.example.(\w+)
urlSubstitutionExp:
mappedHost: local.ca.org2.example.com

channel:
- pattern: ^(sample)(\w*)(channel)$
mappedName: ch1
caName: ca.org2.example.com

0 comments on commit 0728814

Please sign in to comment.