github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/msp/mgmt/mgmt.go (about) 1 /* 2 Copyright IBM Corp. 2017 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 package mgmt 18 19 import ( 20 "reflect" 21 "sync" 22 23 "errors" 24 25 "github.com/hyperledger/fabric/bccsp/factory" 26 configvaluesmsp "github.com/hyperledger/fabric/common/config/msp" 27 "github.com/hyperledger/fabric/common/flogging" 28 "github.com/hyperledger/fabric/core/config" 29 "github.com/hyperledger/fabric/msp" 30 ) 31 32 // LoadLocalMsp loads the local MSP from the specified directory 33 func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error { 34 if mspID == "" { 35 return errors.New("The local MSP must have an ID") 36 } 37 38 conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID) 39 if err != nil { 40 return err 41 } 42 43 return GetLocalMSP().Setup(conf) 44 } 45 46 // Loads the development local MSP for use in testing. Not valid for production/runtime context 47 func LoadDevMsp() error { 48 mspDir, err := config.GetDevMspDir() 49 if err != nil { 50 return err 51 } 52 53 return LoadLocalMsp(mspDir, nil, "DEFAULT") 54 } 55 56 // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE, 57 // THESE MAPS AND HELPSER FUNCTIONS SHOULD DISAPPEAR BECAUSE 58 // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT; 59 // HOWEVER IN THE INTERIM, THESE HELPER FUNCTIONS ARE REQUIRED 60 61 var m sync.Mutex 62 var localMsp msp.MSP 63 var mspMap map[string]msp.MSPManager = make(map[string]msp.MSPManager) 64 var mspLogger = flogging.MustGetLogger("msp") 65 66 // GetManagerForChain returns the msp manager for the supplied 67 // chain; if no such manager exists, one is created 68 func GetManagerForChain(chainID string) msp.MSPManager { 69 m.Lock() 70 defer m.Unlock() 71 72 mspMgr, ok := mspMap[chainID] 73 if !ok { 74 mspLogger.Debugf("Created new msp manager for chain %s", chainID) 75 mspMgr = msp.NewMSPManager() 76 mspMap[chainID] = mspMgr 77 } else { 78 switch mgr := mspMgr.(type) { 79 case *configvaluesmsp.MSPConfigHandler: 80 // check for nil MSPManager interface as it can exist but not be 81 // instantiated 82 if mgr.MSPManager == nil { 83 mspLogger.Debugf("MSPManager is not instantiated; no MSPs are defined for this channel.") 84 // return nil so the MSPManager methods cannot be accidentally called, 85 // which would result in a panic 86 return nil 87 } 88 default: 89 // check for internal mspManagerImpl type. if a different type is found, 90 // it's because a developer has added a new type that implements the 91 // MSPManager interface and should add a case to the logic above to handle 92 // it. 93 if reflect.TypeOf(mgr).Elem().Name() != "mspManagerImpl" { 94 panic("Found unexpected MSPManager type.") 95 } 96 } 97 mspLogger.Debugf("Returning existing manager for channel '%s'", chainID) 98 } 99 return mspMgr 100 } 101 102 // GetManagers returns all the managers registered 103 func GetDeserializers() map[string]msp.IdentityDeserializer { 104 m.Lock() 105 defer m.Unlock() 106 107 clone := make(map[string]msp.IdentityDeserializer) 108 109 for key, mspManager := range mspMap { 110 clone[key] = mspManager 111 } 112 113 return clone 114 } 115 116 // XXXSetMSPManager is a stopgap solution to transition from the custom MSP config block 117 // parsing to the configtx.Manager interface, while preserving the problematic singleton 118 // nature of the MSP manager 119 func XXXSetMSPManager(chainID string, manager msp.MSPManager) { 120 m.Lock() 121 defer m.Unlock() 122 123 mspMap[chainID] = manager 124 } 125 126 // GetLocalMSP returns the local msp (and creates it if it doesn't exist) 127 func GetLocalMSP() msp.MSP { 128 var lclMsp msp.MSP 129 var created bool = false 130 { 131 m.Lock() 132 defer m.Unlock() 133 134 lclMsp = localMsp 135 if lclMsp == nil { 136 var err error 137 created = true 138 lclMsp, err = msp.NewBccspMsp() 139 if err != nil { 140 mspLogger.Fatalf("Failed to initialize local MSP, received err %s", err) 141 } 142 localMsp = lclMsp 143 } 144 } 145 146 if created { 147 mspLogger.Debugf("Created new local MSP") 148 } else { 149 mspLogger.Debugf("Returning existing local MSP") 150 } 151 152 return lclMsp 153 } 154 155 // GetIdentityDeserializer returns the IdentityDeserializer for the given chain 156 func GetIdentityDeserializer(chainID string) msp.IdentityDeserializer { 157 if chainID == "" { 158 return GetLocalMSP() 159 } 160 161 return GetManagerForChain(chainID) 162 } 163 164 // GetLocalSigningIdentityOrPanic returns the local signing identity or panic in case 165 // or error 166 func GetLocalSigningIdentityOrPanic() msp.SigningIdentity { 167 id, err := GetLocalMSP().GetDefaultSigningIdentity() 168 if err != nil { 169 mspLogger.Panicf("Failed getting local signing identity [%s]", err) 170 } 171 return id 172 }