Skip to content

Commit

Permalink
[FABG-814] Derive URLs from hostname if omitted
Browse files Browse the repository at this point in the history
Don't require hardcoded endpoint URLs in `config.yaml` for orderers, peers, and
ca-servers.

Infer URL (and associated port) from parent hostname if URL is omitted.

Note: According to the inline comments in config.yaml, for
`certificateAuthorities`, `caName` is also "optional". This requires further
investigation.

Change-Id: Ic31ed4d61ae4834c9d7128e94cebecdd35477ecb
Signed-off-by: Nye Liu <nye@blockdaemon.com>
  • Loading branch information
nyetwurk committed Jan 26, 2019
1 parent fc535d1 commit ce2814e
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pkg/core/config/lookup/lookup_test.go
Expand Up @@ -305,11 +305,11 @@ func TestUnmarshalWithMultipleBackend(t *testing.T) {
assert.Equal(t, networkConfig.Organizations["org1"].MSPID, "Org1MSP")

//Orderer
assert.Equal(t, len(networkConfig.Orderers), 2)
assert.Equal(t, len(networkConfig.Orderers), 3)
assert.Equal(t, networkConfig.Orderers["local.orderer.example.com"].URL, "orderer.example.com:7050")

//Peer
assert.Equal(t, len(networkConfig.Peers), 3)
assert.Equal(t, len(networkConfig.Peers), 4)
assert.Equal(t, networkConfig.Peers["local.peer0.org1.example.com"].URL, "peer0.org1.example.com:7051")

}
Expand Down
12 changes: 8 additions & 4 deletions pkg/core/config/testdata/config_test.yaml
Expand Up @@ -267,7 +267,8 @@ organizations:
#
orderers:
orderer.example.com:
url: orderer.example.com:7050
# [Optional] Default: Infer from hostname
#url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
# they will be passed in as-is to gRPC client constructor
Expand Down Expand Up @@ -295,7 +296,8 @@ orderers:
peers:
peer0.org1.example.com:
# this URL is used to send endorsement and query requests
url: peer0.org1.example.com:7051
# [Optional] Default: Infer from hostname
#url: peer0.org1.example.com:7051

#TODO to be moved to high level, common for all grpc connections
grpcOptions:
Expand Down Expand Up @@ -339,7 +341,8 @@ peers:
#
certificateAuthorities:
ca.org1.example.com:
url: https://ca.org1.example.com:7054
# [Optional] Default: Infer from hostname
#url: https://ca.org1.example.com:7054
# [Optional] The optional server name for target override
grpcOptions:
ssl-target-name-override: ca.org1.example.com
Expand All @@ -361,6 +364,7 @@ certificateAuthorities:
# [Optional] The optional name of the CA.
caName: ca.org1.example.com
ca.org2.example.com:
# [Optional] Default: Infer from hostname
url: https://ca.org2.example.com:8054
# [Optional] The optional server name for target override
grpcOptions:
Expand Down Expand Up @@ -418,4 +422,4 @@ metrics:
promReporter:

# prometheus http server listen address for pull metrics
listenAddress: peer0.org1.example.com:8080
listenAddress: peer0.org1.example.com:8080
23 changes: 18 additions & 5 deletions pkg/core/config/testdata/config_test_entity_matchers.yaml
Expand Up @@ -80,7 +80,7 @@ channels:
# Default channel is used if channel configuration is missing or if defined channel configuration is missing info
# If channel doesn't define peers then peers from default channel will be used
# If channel doesn't define orderes then orderes from default channel will be used
# If channel doesn't define policies then policies from default channel will be used.
# If channel doesn't define policies then policies from default channel will be used.
# Also, if channel defines policies and some policy info is missing than that missing info will be filled from default channel.
_default:

Expand All @@ -105,14 +105,14 @@ channels:
# produce events but the app typically only needs to connect to one to listen to events.
# Default: true
eventSource: true

# [Optional]. Orderer used by this channel
orderers:
- orderer.example.com

# [Optional]. The application will use these option when using services such as discovery etc.
policies:
discovery:
discovery:
#[Optional] Maximum number of random targets
maxTargets: 3
#[Optional] retry options
Expand Down Expand Up @@ -403,6 +403,7 @@ orderers:
allow-insecure: false

local.orderer.example.com:
# [Optional] Default: Infer from hostname
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
Expand All @@ -414,6 +415,11 @@ orderers:
# Certificate location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem

orderer1.example.com:
tlsCACerts:
# Certificae location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/ordererOrganizations/example.com/tlsca/tlsca.example.com-cert.pem

#
# List of peers to send various requests to, including endorsement, query
# and event listener registration.
Expand All @@ -437,7 +443,8 @@ peers:
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.org1.example.com:
# this URL is used to send endorsement and query requests
# [Optional] Default: Infer from hostname
# This URL is used to send endorsement and query requests
url: peer0.org1.example.com:7051

grpcOptions:
Expand All @@ -456,6 +463,12 @@ peers:
tlsCACerts:
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem

peer0.org3.example.com:
tlsCACerts:
# Certificate location absolute path
path: ${GOPATH}/src/github.com/hyperledger/fabric-sdk-go/${CRYPTOCONFIG_FIXTURES_PATH}/peerOrganizations/org2.example.com/tlsca/tlsca.org2.example.com-cert.pem


#
# Fabric-CA is a special kind of Certificate Authority provided by Hyperledger Fabric which allows
# certificate management to be done via REST APIs. Application may choose to use a standard
Expand Down Expand Up @@ -605,4 +618,4 @@ entityMatchers:

channel:
- pattern: ^(sample)(\w*)(channel)$
mappedName: ch1
mappedName: ch1
23 changes: 17 additions & 6 deletions pkg/fab/endpointconfig.go
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/x509"
"reflect"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -30,6 +31,8 @@ import (
)

var logger = logging.NewLogger("fabsdk/fab")
var defaultOrdererListenPort = 7050
var defaultPeerListenPort = 7051

const (
defaultPeerConnectionTimeout = time.Second * 10
Expand Down Expand Up @@ -822,7 +825,7 @@ func (c *EndpointConfig) loadAllPeerConfigs(networkConfig *fab.NetworkConfig, en
if err != nil {
return errors.WithMessage(err, "failed to load peer network config")
}
networkConfig.Peers[name] = c.addMissingPeerConfigItems(fab.PeerConfig{
networkConfig.Peers[name] = c.addMissingPeerConfigItems(name, fab.PeerConfig{
URL: peerConfig.URL,
GRPCOptions: peerConfig.GRPCOptions,
TLSCACert: tlsCert,
Expand All @@ -842,7 +845,7 @@ func (c *EndpointConfig) loadAllOrdererConfigs(networkConfig *fab.NetworkConfig,
if err != nil {
return errors.WithMessage(err, "failed to load orderer network config")
}
networkConfig.Orderers[name] = c.addMissingOrdererConfigItems(fab.OrdererConfig{
networkConfig.Orderers[name] = c.addMissingOrdererConfigItems(name, fab.OrdererConfig{
URL: ordererConfig.URL,
GRPCOptions: ordererConfig.GRPCOptions,
TLSCACert: tlsCert,
Expand All @@ -851,11 +854,15 @@ func (c *EndpointConfig) loadAllOrdererConfigs(networkConfig *fab.NetworkConfig,
return nil
}

func (c *EndpointConfig) addMissingPeerConfigItems(config fab.PeerConfig) fab.PeerConfig {
func (c *EndpointConfig) addMissingPeerConfigItems(name string, config fab.PeerConfig) fab.PeerConfig {

// peer URL
if config.URL == "" {
config.URL = c.defaultPeerConfig.URL
if c.defaultPeerConfig.URL == "" {
config.URL = name + ":" + strconv.Itoa(defaultPeerListenPort)
} else {
config.URL = c.defaultPeerConfig.URL
}
}

//tls ca certs
Expand All @@ -880,10 +887,14 @@ func (c *EndpointConfig) addMissingPeerConfigItems(config fab.PeerConfig) fab.Pe
return config
}

func (c *EndpointConfig) addMissingOrdererConfigItems(config fab.OrdererConfig) fab.OrdererConfig {
func (c *EndpointConfig) addMissingOrdererConfigItems(name string, config fab.OrdererConfig) fab.OrdererConfig {
// orderer URL
if config.URL == "" {
config.URL = c.defaultOrdererConfig.URL
if c.defaultOrdererConfig.URL == "" {
config.URL = name + ":" + strconv.Itoa(defaultOrdererListenPort)
} else {
config.URL = c.defaultOrdererConfig.URL
}
}

//tls ca certs
Expand Down
6 changes: 4 additions & 2 deletions pkg/fab/endpointconfig_test.go
Expand Up @@ -1183,12 +1183,14 @@ func TestEndpointConfigWithMultipleBackends(t *testing.T) {
assert.Equal(t, networkConfig.Organizations["org1"].MSPID, "Org1MSP")

//Orderer
assert.Equal(t, len(networkConfig.Orderers), 1)
assert.Equal(t, len(networkConfig.Orderers), 2)
assert.Equal(t, networkConfig.Orderers["local.orderer.example.com"].URL, "orderer.example.com:7050")
assert.Equal(t, networkConfig.Orderers["orderer1.example.com"].URL, "orderer1.example.com:7050")

//Peer
assert.Equal(t, len(networkConfig.Peers), 2)
assert.Equal(t, len(networkConfig.Peers), 3)
assert.Equal(t, networkConfig.Peers["local.peer0.org1.example.com"].URL, "peer0.org1.example.com:7051")
assert.Equal(t, networkConfig.Peers["peer0.org3.example.com"].URL, "peer0.org3.example.com:7051")

//EntityMatchers
endpointConfigImpl := endpointConfig.(*EndpointConfig)
Expand Down
17 changes: 14 additions & 3 deletions pkg/msp/identityconfig.go
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package msp

import (
"strconv"
"strings"

"github.com/pkg/errors"
Expand All @@ -24,6 +25,9 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
)

var defaultCAServerSchema = "https"
var defaultCAServerListenPort = 7054

//ConfigFromBackend returns identity config implementation of given backend
func ConfigFromBackend(coreBackend ...core.ConfigBackend) (msp.IdentityConfig, error) {

Expand Down Expand Up @@ -289,7 +293,7 @@ func (c *IdentityConfig) loadAllCAConfigs(configEntity *identityConfigEntity) er
}

logger.Debugf("Mapped Certificate Authority for [%s] to [%s]", orgName, caName)
mspCAConfig, err := c.getMSPCAConfig(matchedCaConfig)
mspCAConfig, err := c.getMSPCAConfig(caName, matchedCaConfig)
if err != nil {
return err
}
Expand All @@ -304,15 +308,22 @@ func (c *IdentityConfig) loadAllCAConfigs(configEntity *identityConfigEntity) er
return nil
}

func (c *IdentityConfig) getMSPCAConfig(caConfig *CAConfig) (*msp.CAConfig, error) {
func (c *IdentityConfig) getMSPCAConfig(caName string, caConfig *CAConfig) (*msp.CAConfig, error) {

serverCerts, err := c.getServerCerts(caConfig)
if err != nil {
return nil, err
}

var URL string
if caConfig.URL == "" {
URL = defaultCAServerSchema + "://" + caName + ":" + strconv.Itoa(defaultCAServerListenPort)
} else {
URL = caConfig.URL
}

return &msp.CAConfig{
URL: caConfig.URL,
URL: URL,
GRPCOptions: caConfig.GRPCOptions,
Registrar: caConfig.Registrar,
CAName: caConfig.CAName,
Expand Down
3 changes: 3 additions & 0 deletions pkg/msp/identityconfig_test.go
Expand Up @@ -456,6 +456,9 @@ func TestIdentityConfigWithMultipleBackends(t *testing.T) {
assert.Equal(t, 1, len(caConfig.GRPCOptions))
assert.Equal(t, "ca.org1.example.com", caConfig.GRPCOptions["ssl-target-name-override"])

caConfig, ok = identityConfig.CAConfig("org2")
assert.True(t, ok, "identityConfig.CAConfig(org2) should have been successful for multiple backends")
assert.Equal(t, caConfig.URL, "https://ca.org2.example.com:8054")
}

func newViper(path string) *viper.Viper {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config/config_e2e.yaml 100755 → 100644
Expand Up @@ -305,6 +305,7 @@ organizations:
#
orderers:
orderer.example.com:
# [Optional] Default: Infer from hostname
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
Expand Down Expand Up @@ -332,6 +333,7 @@ orderers:
peers:
peer0.org1.example.com:
# this URL is used to send endorsement and query requests
# [Optional] Default: Infer from hostname
url: peer0.org1.example.com:7051

grpcOptions:
Expand Down Expand Up @@ -394,6 +396,7 @@ peers:
#
certificateAuthorities:
ca.org1.example.com:
# [Optional] Default: Infer from hostname
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/config/config_e2e_multiorg_bootstrap.yaml 100755 → 100644
Expand Up @@ -129,6 +129,7 @@ organizations:
peers:
# defining bootstrap peers only
peer0.org1.example.com:
# [Optional] Default: Infer from hostname
url: peer0.org1.example.com:7051

grpcOptions:
Expand Down Expand Up @@ -164,6 +165,7 @@ peers:
orderers:
# needed to fetch the ordrerer config for create channel
orderer.example.com:
# [Optional] Default: Infer from hostname
url: orderer.example.com:7050

grpcOptions:
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/config/config_e2e_no_orderer.yaml 100755 → 100644
Expand Up @@ -136,6 +136,7 @@ organizations:
peers:
peer0.org1.example.com:
# this URL is used to send endorsement and query requests
# [Optional] Default: Infer from hostname
url: peer0.org1.example.com:7051

#TODO to be moved to high level, common for all grpc connections
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config/config_e2e_pkcs11.yaml 100755 → 100644
Expand Up @@ -187,6 +187,7 @@ organizations:
#
orderers:
orderer.example.com:
# [Optional] Default: Infer from hostname
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
Expand All @@ -210,6 +211,7 @@ orderers:
peers:
peer0.org1.example.com:
# this URL is used to send endorsement and query requests
# [Optional] Default: Infer from hostname
url: peer0.org1.example.com:7051

#TODO to be moved to high level, common for all grpc connections
Expand Down Expand Up @@ -243,6 +245,7 @@ peers:
#
certificateAuthorities:
ca.org1.example.com:
# [Optional] Default: Infer from hostname
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config/config_e2e_single_org.yaml 100755 → 100644
Expand Up @@ -189,6 +189,7 @@ orderers:
allow-insecure: false

orderer.example.com:
# [Optional] Default: Infer from hostname
url: orderer.example.com:7050

# these are standard properties defined by the gRPC library
Expand Down Expand Up @@ -229,6 +230,7 @@ peers:

peer0.org1.example.com:
# this URL is used to send endorsement and query requests
# [Optional] Default: Infer from hostname
url: peer0.org1.example.com:7051

grpcOptions:
Expand Down Expand Up @@ -256,6 +258,7 @@ peers:
#
certificateAuthorities:
ca.org1.example.com:
# [Optional] Default: Infer from hostname
url: https://ca.org1.example.com:7054
tlsCACerts:
# Comma-Separated list of paths
Expand Down

0 comments on commit ce2814e

Please sign in to comment.