github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/core/peer/config.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // The 'viper' package for configuration handling is very flexible, but has 18 // been found to have extremely poor performance when configuration values are 19 // accessed repeatedly. The function CacheConfiguration() defined here caches 20 // all configuration values that are accessed frequently. These parameters 21 // are now presented as function calls that access local configuration 22 // variables. This seems to be the most robust way to represent these 23 // parameters in the face of the numerous ways that configuration files are 24 // loaded and used (e.g, normal usage vs. test cases). 25 26 // The CacheConfiguration() function is allowed to be called globally to 27 // ensure that the correct values are always cached; See for example how 28 // certain parameters are forced in 'ChaincodeDevMode' in main.go. 29 30 package peer 31 32 import ( 33 "fmt" 34 "io/ioutil" 35 "net" 36 37 "github.com/spf13/viper" 38 39 "github.com/hyperledger/fabric/core/comm" 40 "github.com/hyperledger/fabric/core/config" 41 pb "github.com/hyperledger/fabric/protos/peer" 42 ) 43 44 // Is the configuration cached? 45 var configurationCached = false 46 47 // Cached values and error values of the computed constants getLocalAddress(), 48 // getValidatorStreamAddress(), and getPeerEndpoint() 49 var localAddress string 50 var localAddressError error 51 var peerEndpoint *pb.PeerEndpoint 52 var peerEndpointError error 53 54 // Cached values of commonly used configuration constants. 55 56 // CacheConfiguration computes and caches commonly-used constants and 57 // computed constants as package variables. Routines which were previously 58 // global have been embedded here to preserve the original abstraction. 59 func CacheConfiguration() (err error) { 60 61 // getLocalAddress returns the address:port the local peer is operating on. Affected by env:peer.addressAutoDetect 62 getLocalAddress := func() (peerAddress string, err error) { 63 if viper.GetBool("peer.addressAutoDetect") { 64 // Need to get the port from the peer.address setting, and append to the determined host IP 65 _, port, err := net.SplitHostPort(viper.GetString("peer.address")) 66 if err != nil { 67 err = fmt.Errorf("Error auto detecting Peer's address: %s", err) 68 return "", err 69 } 70 peerAddress = net.JoinHostPort(GetLocalIP(), port) 71 peerLogger.Infof("Auto detected peer address: %s", peerAddress) 72 } else { 73 peerAddress = viper.GetString("peer.address") 74 } 75 return 76 } 77 78 // getPeerEndpoint returns the PeerEndpoint for this Peer instance. Affected by env:peer.addressAutoDetect 79 getPeerEndpoint := func() (*pb.PeerEndpoint, error) { 80 var peerAddress string 81 peerAddress, err := getLocalAddress() 82 if err != nil { 83 return nil, err 84 } 85 return &pb.PeerEndpoint{Id: &pb.PeerID{Name: viper.GetString("peer.id")}, Address: peerAddress}, nil 86 } 87 88 localAddress, localAddressError = getLocalAddress() 89 peerEndpoint, _ = getPeerEndpoint() 90 91 configurationCached = true 92 93 if localAddressError != nil { 94 return localAddressError 95 } 96 return 97 } 98 99 // cacheConfiguration logs an error if error checks have failed. 100 func cacheConfiguration() { 101 if err := CacheConfiguration(); err != nil { 102 peerLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err) 103 } 104 } 105 106 //Functional forms 107 108 // GetLocalAddress returns the peer.address property 109 func GetLocalAddress() (string, error) { 110 if !configurationCached { 111 cacheConfiguration() 112 } 113 return localAddress, localAddressError 114 } 115 116 // GetPeerEndpoint returns peerEndpoint from cached configuration 117 func GetPeerEndpoint() (*pb.PeerEndpoint, error) { 118 if !configurationCached { 119 cacheConfiguration() 120 } 121 return peerEndpoint, peerEndpointError 122 } 123 124 // GetSecureConfig returns the secure server configuration for the peer 125 func GetSecureConfig() (comm.SecureServerConfig, error) { 126 secureConfig := comm.SecureServerConfig{ 127 UseTLS: viper.GetBool("peer.tls.enabled"), 128 } 129 if secureConfig.UseTLS { 130 // get the certs from the file system 131 serverKey, err := ioutil.ReadFile(config.GetPath("peer.tls.key.file")) 132 serverCert, err := ioutil.ReadFile(config.GetPath("peer.tls.cert.file")) 133 // must have both key and cert file 134 if err != nil { 135 return secureConfig, fmt.Errorf("Error loading TLS key and/or certificate (%s)", err) 136 } 137 secureConfig.ServerCertificate = serverCert 138 secureConfig.ServerKey = serverKey 139 // check for root cert 140 if config.GetPath("peer.tls.rootcert.file") != "" { 141 rootCert, err := ioutil.ReadFile(config.GetPath("peer.tls.rootcert.file")) 142 if err != nil { 143 return secureConfig, fmt.Errorf("Error loading TLS root certificate (%s)", err) 144 } 145 secureConfig.ServerRootCAs = [][]byte{rootCert} 146 } 147 return secureConfig, nil 148 } 149 return secureConfig, nil 150 }