Skip to content

Commit

Permalink
[FAB-11063] regex replace for entitymatcher mappedhost
Browse files Browse the repository at this point in the history
- mapped hosts in peer, orderer and ca entity matchers
to support regex replace


Change-Id: I8b1a2d8c7a888ad7acd49e9213befb20a61178f9
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Jul 11, 2018
1 parent 02ba89a commit 333c1b9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
17 changes: 14 additions & 3 deletions pkg/core/config/testdata/matcher-samples/matchers_sample3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,24 @@ entityMatchers:
sslTargetOverrideUrlSubstitutionExp: peer0.org1.override.com
mappedHost: peer0.org1.example.com

- pattern: (\w+).exampleY.(\w+):(\d+)
urlSubstitutionExp: $1.org1.example.$2:8888
eventUrlSubstitutionExp: $1.org1.example.$2:9999
sslTargetOverrideUrlSubstitutionExp: $1.org1.override.$2
mappedHost: $1.org1.example.$2

orderer:
- pattern: (\w+).exampleX.(\w+):(\d+)
urlSubstitutionExp: orderer.example.com:8888
sslTargetOverrideUrlSubstitutionExp: orderer.override.com
mappedHost: orderer.example.com

- pattern: (\w+).exampleY.(\w+):(\d+)
urlSubstitutionExp: $1.example.$2:8888
sslTargetOverrideUrlSubstitutionExp: $1.override.$2
mappedHost: $1.example.$2

certificateAuthority:
- pattern: (\w+).org1.exampleX.(\w+)
urlSubstitutionExp: https://ca.org1.example.com:8888
mappedHost: ca.org1.example.com
- pattern: (\w+).org1.example.(\w+)
urlSubstitutionExp: https://$1.org1.example.$2:8888
mappedHost: $1.org1.example.$2
14 changes: 12 additions & 2 deletions pkg/fab/endpointconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,12 @@ func (c *EndpointConfig) tryMatchingPeerConfig(peerName string, searchByURL bool

func (c *EndpointConfig) matchPeer(peerName string, matcher matcherEntry) *fab.PeerConfig {

matchedPeer := c.getMappedPeer(matcher.matchConfig.MappedHost)
mappedHost := matcher.matchConfig.MappedHost
if strings.Contains(mappedHost, "$") {
mappedHost = matcher.regex.ReplaceAllString(peerName, mappedHost)
}

matchedPeer := c.getMappedPeer(mappedHost)
if matchedPeer == nil {
logger.Debugf("Could not find mapped host [%s] for peer [%s]", matcher.matchConfig.MappedHost, peerName)
return nil
Expand Down Expand Up @@ -986,8 +991,13 @@ func (c *EndpointConfig) tryMatchingOrdererConfig(ordererName string, searchByUR

func (c *EndpointConfig) matchOrderer(ordererName string, matcher matcherEntry) *fab.OrdererConfig {

mappedHost := matcher.matchConfig.MappedHost
if strings.Contains(mappedHost, "$") {
mappedHost = matcher.regex.ReplaceAllString(ordererName, mappedHost)
}

//Get the ordererConfig from mapped host
matchedOrderer := c.getMappedOrderer(matcher.matchConfig.MappedHost)
matchedOrderer := c.getMappedOrderer(mappedHost)
if matchedOrderer == nil {
logger.Debugf("Could not find mapped host [%s] for orderer [%s]", matcher.matchConfig.MappedHost, ordererName)
return nil
Expand Down
15 changes: 15 additions & 0 deletions pkg/fab/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ func TestURLMapping(t *testing.T) {
assert.Equal(t, overridedOrdererHostNameOverride, ordererConfig.GRPCOptions["ssl-target-name-override"])
assert.Equal(t, 6, len(ordererConfig.GRPCOptions))

//PeerConfig Search Based on URL configured in config (using $ in entity matchers)
peerConfig, ok = config.PeerConfig("peer0.exampleY.com:1234")
assert.True(t, ok, "supposed to find peer config")
assert.Equal(t, overridedPeerURL, peerConfig.URL)
assert.Equal(t, overridedPeerEventURL, peerConfig.EventURL)
assert.Equal(t, overridedPeerHostNameOverride, peerConfig.GRPCOptions["ssl-target-name-override"])
assert.Equal(t, 6, len(peerConfig.GRPCOptions))

//OrdererConfig Search Based on URL configured in config (using $ in entity matchers)
ordererConfig, ok = config.OrdererConfig("orderer.exampleY.com:1234")
assert.True(t, ok, "supposed to find orderer config")
assert.Equal(t, overridedOrdererURL, ordererConfig.URL)
assert.Equal(t, overridedOrdererHostNameOverride, ordererConfig.GRPCOptions["ssl-target-name-override"])
assert.Equal(t, 6, len(ordererConfig.GRPCOptions))

}

func getBackendsFromFiles(files ...string) ([]core.ConfigBackend, error) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/msp/identityconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,14 @@ func (c *IdentityConfig) tryMatchingCAConfig(configEntity *identityConfigEntity,
}

func (c *IdentityConfig) findMatchingCert(configEntity *identityConfigEntity, caName string, matcher matcherEntry) *CAConfig {

mappedHost := matcher.matchConfig.MappedHost
if strings.Contains(mappedHost, "$") {
mappedHost = matcher.regex.ReplaceAllString(caName, mappedHost)
}

//Get the certAuthorityMatchConfig from mapped host
caConfig, ok := configEntity.CertificateAuthorities[strings.ToLower(matcher.matchConfig.MappedHost)]
caConfig, ok := configEntity.CertificateAuthorities[strings.ToLower(mappedHost)]
if !ok {
return nil
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/msp/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const (
sampleMatchersOverrideAll = "../core/config/testdata/matcher-samples/matchers_sample1.yaml"
sampleMatchersOverrideAll = "../core/config/testdata/matcher-samples/matchers_sample1.yaml"
sampleMatchersRegexReplace = "../core/config/testdata/matcher-samples/matchers_sample3.yaml"

actualCAURL = "https://ca.org1.example.com:7054"
overridedCAURL = "https://ca.org1.example.com:8888"
Expand All @@ -25,6 +26,14 @@ const (
//Scenario: Using entity mather to override CA URL
func TestCAURLOverride(t *testing.T) {

//Test basic entity matcher
testCAEntityMatcher(t, sampleMatchersOverrideAll)

//Test entity matcher with regex replace feature '$'
testCAEntityMatcher(t, sampleMatchersRegexReplace)
}

func testCAEntityMatcher(t *testing.T, configPath string) {
//Without entity matcher
backends, err := getBackendsFromFiles(configTestFilePath)
assert.Nil(t, err, "not supposed to get error")
Expand All @@ -40,7 +49,7 @@ func TestCAURLOverride(t *testing.T) {
assert.Equal(t, actualCAURL, caConfig.URL)

//Using entity matcher to override CA URL
backends, err = getBackendsFromFiles(sampleMatchersOverrideAll, configTestFilePath)
backends, err = getBackendsFromFiles(configPath, configTestFilePath)
assert.Nil(t, err, "not supposed to get error")
assert.Equal(t, 2, len(backends))

Expand All @@ -52,7 +61,6 @@ func TestCAURLOverride(t *testing.T) {
assert.True(t, ok, "supposed to find caconfig")
assert.NotNil(t, caConfig)
assert.Equal(t, overridedCAURL, caConfig.URL)

}

func getBackendsFromFiles(files ...string) ([]core.ConfigBackend, error) {
Expand Down

0 comments on commit 333c1b9

Please sign in to comment.