forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpointconfig.go
1333 lines (1130 loc) · 41.9 KB
/
endpointconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fab
import (
"crypto/tls"
"crypto/x509"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/multi"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
commtls "github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm/tls"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/cryptoutil"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/lookup"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
var logger = logging.NewLogger("fabsdk/fab")
const (
defaultEndorserConnectionTimeout = time.Second * 10
defaultPeerResponseTimeout = time.Minute * 3
defaultDiscoveryGreylistExpiryTimeout = time.Second * 10
defaultEventHubConnectionTimeout = time.Second * 15
defaultEventRegTimeout = time.Second * 15
defaultOrdererConnectionTimeout = time.Second * 15
defaultOrdererResponseTimeout = time.Minute * 2
defaultQueryTimeout = time.Minute * 3
defaultExecuteTimeout = time.Minute * 3
defaultResMgmtTimeout = time.Minute * 3
defaultDiscoveryConnectionTimeout = time.Second * 15
defaultDiscoveryResponseTimeout = time.Second * 15
defaultConnIdleInterval = time.Second * 30
defaultEventServiceIdleInterval = time.Minute * 2
defaultChannelConfigRefreshInterval = time.Second * 90
defaultChannelMemshpRefreshInterval = time.Second * 60
defaultDiscoveryRefreshInterval = time.Second * 5
defaultSelectionRefreshInterval = time.Minute * 10
defaultCacheSweepInterval = time.Second * 15
)
//ConfigFromBackend returns endpoint config implementation for given backend
func ConfigFromBackend(coreBackend ...core.ConfigBackend) (fab.EndpointConfig, error) {
config := &EndpointConfig{
backend: lookup.New(coreBackend...),
peerMatchers: make(map[int]*regexp.Regexp),
ordererMatchers: make(map[int]*regexp.Regexp),
channelMatchers: make(map[int]*regexp.Regexp),
}
if err := config.loadEndpointConfiguration(); err != nil {
return nil, errors.WithMessage(err, "network configuration load failed")
}
//print deprecated warning
detectDeprecatedNetworkConfig(config)
return config, nil
}
// EndpointConfig represents the endpoint configuration for the client
type EndpointConfig struct {
backend *lookup.ConfigLookup
networkConfig *fab.NetworkConfig
tlsCertPool fab.CertPool
entityMatchers *entityMatchers
peerConfigsByOrg map[string][]fab.PeerConfig
networkPeers []fab.NetworkPeer
ordererConfigs []fab.OrdererConfig
channelPeersByChannel map[string][]fab.ChannelPeer
channelOrderersByChannel map[string][]fab.OrdererConfig
tlsClientCerts []tls.Certificate
peerMatchers map[int]*regexp.Regexp
ordererMatchers map[int]*regexp.Regexp
channelMatchers map[int]*regexp.Regexp
}
//endpointConfigEntity contains endpoint config elements needed by endpointconfig
type endpointConfigEntity struct {
Client ClientConfig
Channels map[string]ChannelEndpointConfig
Organizations map[string]OrganizationConfig
Orderers map[string]OrdererConfig
Peers map[string]PeerConfig
}
//entityMatchers for endpoint configuration
type entityMatchers struct {
matchers map[string][]MatchConfig
}
// Timeout reads timeouts for the given timeout type, if type is not found in the config
// then default is set as per the const value above for the corresponding type
func (c *EndpointConfig) Timeout(tType fab.TimeoutType) time.Duration {
return c.getTimeout(tType)
}
// OrderersConfig returns a list of defined orderers
func (c *EndpointConfig) OrderersConfig() []fab.OrdererConfig {
return c.ordererConfigs
}
// OrdererConfig returns the requested orderer
func (c *EndpointConfig) OrdererConfig(nameOrURL string) (*fab.OrdererConfig, bool) {
orderer, ok := c.networkConfig.Orderers[strings.ToLower(nameOrURL)]
if !ok {
for _, ordererCfg := range c.OrderersConfig() {
if strings.EqualFold(ordererCfg.URL, nameOrURL) {
orderer = ordererCfg
ok = true
break
}
}
}
if !ok {
logger.Debugf("Could not find Orderer for [%s], trying with Entity Matchers", nameOrURL)
matchingOrdererConfig := c.tryMatchingOrdererConfig(strings.ToLower(nameOrURL))
if matchingOrdererConfig == nil {
return nil, false
}
logger.Debugf("Found matching Orderer Config for [%s]", nameOrURL)
orderer = *matchingOrdererConfig
}
return &orderer, true
}
// PeersConfig Retrieves the fabric peers for the specified org from the
// config file provided
func (c *EndpointConfig) PeersConfig(org string) ([]fab.PeerConfig, bool) {
peerConfigs, ok := c.peerConfigsByOrg[strings.ToLower(org)]
return peerConfigs, ok
}
// PeerConfig Retrieves a specific peer from the configuration by name or url
func (c *EndpointConfig) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {
//lookup by name in config
peerConfig, ok := c.networkConfig.Peers[strings.ToLower(nameOrURL)]
var matchPeerConfig *fab.PeerConfig
if ok {
matchPeerConfig = &peerConfig
} else {
for _, staticPeerConfig := range c.networkConfig.Peers {
if strings.EqualFold(staticPeerConfig.URL, nameOrURL) {
matchPeerConfig = c.tryMatchingPeerConfig(nameOrURL)
if matchPeerConfig == nil {
matchPeerConfig = &staticPeerConfig
}
break
}
}
}
//Not found through config lookup by name or URL, try matcher now
if matchPeerConfig == nil {
logger.Debugf("Could not find Peer for name/url [%s], trying with Entity Matchers", nameOrURL)
//try to match nameOrURL with peer entity matchers
matchPeerConfig = c.tryMatchingPeerConfig(nameOrURL)
}
if matchPeerConfig == nil {
return nil, false
}
logger.Debugf("Found MatchingPeerConfig for name/url [%s]", nameOrURL)
return matchPeerConfig, true
}
// NetworkConfig returns the network configuration defined in the config file
func (c *EndpointConfig) NetworkConfig() *fab.NetworkConfig {
return c.networkConfig
}
// NetworkPeers returns the network peers configuration, all the peers from all the orgs in config.
func (c *EndpointConfig) NetworkPeers() []fab.NetworkPeer {
return c.networkPeers
}
// MappedChannelName will return channelName if it is an original channel name in the config
// if it is not, then it will try to find a channelMatcher and return its MappedName.
// If more than one matcher is found, then the first matcher in the list will be used.
func (c *EndpointConfig) mappedChannelName(networkConfig *fab.NetworkConfig, channelName string) string {
// if channelName is the original key found in the Channels map config, then return it as is
_, ok := networkConfig.Channels[strings.ToLower(channelName)]
if ok {
return channelName
}
// if !ok, then find a channelMatcher for channelName
//Return if no channelMatchers are configured
if len(c.channelMatchers) == 0 {
return ""
}
//sort the keys
var keys []int
for k := range c.channelMatchers {
keys = append(keys, k)
}
sort.Ints(keys)
//loop over channelMatchers to find the matching channel name
for _, k := range keys {
v := c.channelMatchers[k]
if v.MatchString(channelName) {
// get the matching matchConfig from the index number
channelMatchConfig := c.entityMatchers.matchers["channel"][k]
return channelMatchConfig.MappedName
}
}
// not matchers found, return empty
return ""
}
// ChannelConfig returns the channel configuration
func (c *EndpointConfig) ChannelConfig(name string) (*fab.ChannelEndpointConfig, bool) {
// get the mapped channel Name
mappedChannelName := c.mappedChannelName(c.networkConfig, name)
if mappedChannelName == "" {
return nil, false
}
//look up in network config by channelName
ch, ok := c.networkConfig.Channels[strings.ToLower(mappedChannelName)]
return &ch, ok
}
// ChannelPeers returns the channel peers configuration
func (c *EndpointConfig) ChannelPeers(name string) ([]fab.ChannelPeer, bool) {
//get mapped channel name
mappedChannelName := c.mappedChannelName(c.networkConfig, name)
if mappedChannelName == "" {
return nil, false
}
//look up in dictionary
peers, ok := c.channelPeersByChannel[strings.ToLower(mappedChannelName)]
return peers, ok
}
// ChannelOrderers returns a list of channel orderers
func (c *EndpointConfig) ChannelOrderers(name string) ([]fab.OrdererConfig, bool) {
//get mapped channel name
mappedChannelName := c.mappedChannelName(c.networkConfig, name)
if mappedChannelName == "" {
return nil, false
}
//look up in dictionary
orderers, ok := c.channelOrderersByChannel[strings.ToLower(mappedChannelName)]
return orderers, ok
}
// TLSCACertPool returns the configured cert pool. If a certConfig
// is provided, the certificate is added to the pool
func (c *EndpointConfig) TLSCACertPool() fab.CertPool {
return c.tlsCertPool
}
// EventServiceType returns the type of event service client to use
func (c *EndpointConfig) EventServiceType() fab.EventServiceType {
etype := c.backend.GetString("client.eventService.type")
switch etype {
case "eventhub":
return fab.EventHubEventServiceType
case "deliver":
return fab.DeliverEventServiceType
default:
return fab.AutoDetectEventServiceType
}
}
// TLSClientCerts loads the client's certs for mutual TLS
func (c *EndpointConfig) TLSClientCerts() []tls.Certificate {
return c.tlsClientCerts
}
func (c *EndpointConfig) loadPrivateKeyFromConfig(clientConfig *ClientConfig, clientCerts tls.Certificate, cb []byte) ([]tls.Certificate, error) {
kb := clientConfig.TLSCerts.Client.Key.Bytes()
// load the key/cert pair from []byte
clientCerts, err := tls.X509KeyPair(cb, kb)
if err != nil {
return nil, errors.Errorf("Error loading cert/key pair as TLS client credentials: %s", err)
}
logger.Debug("pk read from config successfully")
return []tls.Certificate{clientCerts}, nil
}
// CryptoConfigPath ...
func (c *EndpointConfig) CryptoConfigPath() string {
return pathvar.Subst(c.backend.GetString("client.cryptoconfig.path"))
}
func (c *EndpointConfig) getTimeout(tType fab.TimeoutType) time.Duration { //nolint
var timeout time.Duration
switch tType {
case fab.EndorserConnection:
timeout = c.backend.GetDuration("client.peer.timeout.connection")
if timeout == 0 {
timeout = defaultEndorserConnectionTimeout
}
case fab.PeerResponse:
timeout = c.backend.GetDuration("client.peer.timeout.response")
if timeout == 0 {
timeout = defaultPeerResponseTimeout
}
case fab.DiscoveryGreylistExpiry:
timeout = c.backend.GetDuration("client.peer.timeout.discovery.greylistExpiry")
if timeout == 0 {
timeout = defaultDiscoveryGreylistExpiryTimeout
}
case fab.EventHubConnection:
timeout = c.backend.GetDuration("client.eventService.timeout.connection")
if timeout == 0 {
timeout = defaultEventHubConnectionTimeout
}
case fab.EventReg:
timeout = c.backend.GetDuration("client.eventService.timeout.registrationResponse")
if timeout == 0 {
timeout = defaultEventRegTimeout
}
case fab.OrdererConnection:
timeout = c.backend.GetDuration("client.orderer.timeout.connection")
if timeout == 0 {
timeout = defaultOrdererConnectionTimeout
}
case fab.OrdererResponse:
timeout = c.backend.GetDuration("client.orderer.timeout.response")
if timeout == 0 {
timeout = defaultOrdererResponseTimeout
}
case fab.DiscoveryConnection:
timeout = c.backend.GetDuration("client.discovery.timeout.connection")
if timeout == 0 {
timeout = defaultDiscoveryConnectionTimeout
}
case fab.DiscoveryResponse:
timeout = c.backend.GetDuration("client.discovery.timeout.response")
if timeout == 0 {
timeout = defaultDiscoveryResponseTimeout
}
case fab.Query:
timeout = c.backend.GetDuration("client.global.timeout.query")
if timeout == 0 {
timeout = defaultQueryTimeout
}
case fab.Execute:
timeout = c.backend.GetDuration("client.global.timeout.execute")
if timeout == 0 {
timeout = defaultExecuteTimeout
}
case fab.ResMgmt:
timeout = c.backend.GetDuration("client.global.timeout.resmgmt")
if timeout == 0 {
timeout = defaultResMgmtTimeout
}
case fab.ConnectionIdle:
timeout = c.backend.GetDuration("client.global.cache.connectionIdle")
if timeout == 0 {
timeout = defaultConnIdleInterval
}
case fab.EventServiceIdle:
timeout = c.backend.GetDuration("client.global.cache.eventServiceIdle")
if timeout == 0 {
timeout = defaultEventServiceIdleInterval
}
case fab.ChannelConfigRefresh:
timeout = c.backend.GetDuration("client.global.cache.channelConfig")
if timeout == 0 {
timeout = defaultChannelConfigRefreshInterval
}
case fab.ChannelMembershipRefresh:
timeout = c.backend.GetDuration("client.global.cache.channelMembership")
if timeout == 0 {
timeout = defaultChannelMemshpRefreshInterval
}
case fab.DiscoveryServiceRefresh:
timeout = c.backend.GetDuration("client.global.cache.discovery")
if timeout == 0 {
timeout = defaultDiscoveryRefreshInterval
}
case fab.SelectionServiceRefresh:
timeout = c.backend.GetDuration("client.global.cache.selection")
if timeout == 0 {
timeout = defaultSelectionRefreshInterval
}
case fab.CacheSweepInterval: // EXPERIMENTAL - do we need this to be configurable?
timeout = c.backend.GetDuration("client.cache.interval.sweep")
if timeout == 0 {
timeout = defaultCacheSweepInterval
}
}
return timeout
}
func (c *EndpointConfig) loadEndpointConfiguration() error {
endpointConfigEntity := endpointConfigEntity{}
err := c.backend.UnmarshalKey("client", &endpointConfigEntity.Client)
logger.Debugf("Client is: %+v", endpointConfigEntity.Client)
if err != nil {
return errors.WithMessage(err, "failed to parse 'client' config item to endpointConfigEntity.Client type")
}
err = c.backend.UnmarshalKey("channels", &endpointConfigEntity.Channels, lookup.WithUnmarshalHookFunction(peerChannelConfigHookFunc()))
logger.Debugf("channels are: %+v", endpointConfigEntity.Channels)
if err != nil {
return errors.WithMessage(err, "failed to parse 'channels' config item to endpointConfigEntity.Channels type")
}
err = c.backend.UnmarshalKey("organizations", &endpointConfigEntity.Organizations)
logger.Debugf("organizations are: %+v", endpointConfigEntity.Organizations)
if err != nil {
return errors.WithMessage(err, "failed to parse 'organizations' config item to endpointConfigEntity.Organizations type")
}
err = c.backend.UnmarshalKey("orderers", &endpointConfigEntity.Orderers)
logger.Debugf("orderers are: %+v", endpointConfigEntity.Orderers)
if err != nil {
return errors.WithMessage(err, "failed to parse 'orderers' config item to endpointConfigEntity.Orderers type")
}
err = c.backend.UnmarshalKey("peers", &endpointConfigEntity.Peers)
logger.Debugf("peers are: %+v", endpointConfigEntity.Peers)
if err != nil {
return errors.WithMessage(err, "failed to parse 'peers' config item to endpointConfigEntity.Peers type")
}
//load all endpointconfig entities
err = c.loadEndpointConfigEntities(&endpointConfigEntity)
if err != nil {
return errors.WithMessage(err, "failed to load channel configs")
}
return nil
}
func (c *EndpointConfig) loadEndpointConfigEntities(configEntity *endpointConfigEntity) error {
//Compile the entityMatchers
matchError := c.compileMatchers()
if matchError != nil {
return matchError
}
//load network config
err := c.loadNetworkConfig(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to network config")
}
//load peer configs by org dictionary
c.loadPeerConfigsByOrg()
//load network peers
c.loadNetworkPeers()
//load orderer configs
err = c.loadOrdererConfigs()
if err != nil {
return errors.WithMessage(err, "failed to load orderer configs")
}
//load channel peers
err = c.loadChannelPeers()
if err != nil {
return errors.WithMessage(err, "failed to load channel peers")
}
//load channel orderers
err = c.loadChannelOrderers()
if err != nil {
return errors.WithMessage(err, "failed to load channel orderers")
}
//load tls cert pool
err = c.loadTLSCertPool()
if err != nil {
return errors.WithMessage(err, "failed to load TLS cert pool")
}
return nil
}
func (c *EndpointConfig) loadNetworkConfig(configEntity *endpointConfigEntity) error {
//load all TLS configs, before building network config
err := c.loadAllTLSConfig(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to load network TLSConfig")
}
networkConfig := fab.NetworkConfig{}
//Channels
networkConfig.Channels = make(map[string]fab.ChannelEndpointConfig)
for chID, chNwCfg := range configEntity.Channels {
chPeers := make(map[string]fab.PeerChannelConfig)
for chPeer, chPeerCfg := range chNwCfg.Peers {
chPeers[chPeer] = fab.PeerChannelConfig{
EndorsingPeer: chPeerCfg.EndorsingPeer,
ChaincodeQuery: chPeerCfg.ChaincodeQuery,
LedgerQuery: chPeerCfg.LedgerQuery,
EventSource: chPeerCfg.EventSource,
}
}
networkConfig.Channels[chID] = fab.ChannelEndpointConfig{
Peers: chPeers,
Orderers: chNwCfg.Orderers,
Policies: fab.ChannelPolicies{
QueryChannelConfig: fab.QueryChannelConfigPolicy{
RetryOpts: chNwCfg.Policies.QueryChannelConfig.RetryOpts,
MaxTargets: chNwCfg.Policies.QueryChannelConfig.MaxTargets,
MinResponses: chNwCfg.Policies.QueryChannelConfig.MinResponses,
},
},
}
}
//Organizations
networkConfig.Organizations = make(map[string]fab.OrganizationConfig)
for orgName, orgConfig := range configEntity.Organizations {
tlsKeyCertPairs := make(map[string]fab.CertKeyPair)
for user, tlsKeyPair := range orgConfig.Users {
tlsKeyCertPairs[user] = fab.CertKeyPair{
Cert: tlsKeyPair.Cert.Bytes(),
Key: tlsKeyPair.Key.Bytes(),
}
}
networkConfig.Organizations[orgName] = fab.OrganizationConfig{
MSPID: orgConfig.MSPID,
CryptoPath: orgConfig.CryptoPath,
Peers: orgConfig.Peers,
CertificateAuthorities: orgConfig.CertificateAuthorities,
Users: tlsKeyCertPairs,
}
}
//Orderers
networkConfig.Orderers = make(map[string]fab.OrdererConfig)
for name, ordererConfig := range configEntity.Orderers {
tlsCert, _, err := ordererConfig.TLSCACerts.TLSCert()
if err != nil {
return errors.WithMessage(err, "failed to load orderer network config")
}
networkConfig.Orderers[name] = fab.OrdererConfig{
URL: ordererConfig.URL,
GRPCOptions: ordererConfig.GRPCOptions,
TLSCACert: tlsCert,
}
}
//Peers
networkConfig.Peers = make(map[string]fab.PeerConfig)
for name, peerConfig := range configEntity.Peers {
tlsCert, _, err := peerConfig.TLSCACerts.TLSCert()
if err != nil {
return errors.WithMessage(err, "failed to load network config")
}
networkConfig.Peers[name] = fab.PeerConfig{
URL: peerConfig.URL,
EventURL: peerConfig.EventURL,
GRPCOptions: peerConfig.GRPCOptions,
TLSCACert: tlsCert,
}
}
c.networkConfig = &networkConfig
return nil
}
//loadAllTLSConfig pre-loads all network TLS Configs
func (c *EndpointConfig) loadAllTLSConfig(configEntity *endpointConfigEntity) error {
//resolve path and load bytes
err := c.loadClientTLSConfig(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to load client TLSConfig ")
}
//resolve path and load bytes
err = c.loadOrgTLSConfig(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to load org TLSConfig ")
}
//resolve path and load bytes
err = c.loadOrdererPeerTLSConfig(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to load orderer/peer TLSConfig ")
}
//preload TLS client certs
err = c.loadTLSClientCerts(configEntity)
if err != nil {
return errors.WithMessage(err, "failed to load TLS client certs ")
}
return nil
}
//loadClientTLSConfig pre-loads all TLSConfig bytes in client config
func (c *EndpointConfig) loadClientTLSConfig(configEntity *endpointConfigEntity) error {
//Clients Config
//resolve paths and org name
configEntity.Client.Organization = strings.ToLower(configEntity.Client.Organization)
configEntity.Client.TLSCerts.Client.Key.Path = pathvar.Subst(configEntity.Client.TLSCerts.Client.Key.Path)
configEntity.Client.TLSCerts.Client.Cert.Path = pathvar.Subst(configEntity.Client.TLSCerts.Client.Cert.Path)
//pre load client key and cert bytes
err := configEntity.Client.TLSCerts.Client.Key.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load client key")
}
err = configEntity.Client.TLSCerts.Client.Cert.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load client cert")
}
return nil
}
//loadOrgTLSConfig pre-loads all TLSConfig bytes in organizations
func (c *EndpointConfig) loadOrgTLSConfig(configEntity *endpointConfigEntity) error {
//Organizations Config
for org, orgConfig := range configEntity.Organizations {
for user, userConfig := range orgConfig.Users {
//resolve paths
userConfig.Key.Path = pathvar.Subst(userConfig.Key.Path)
userConfig.Cert.Path = pathvar.Subst(userConfig.Cert.Path)
//pre load key and cert bytes
err := userConfig.Key.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load org key")
}
err = userConfig.Cert.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load org cert")
}
orgConfig.Users[user] = userConfig
}
configEntity.Organizations[org] = orgConfig
}
return nil
}
//loadTLSConfig pre-loads all TLSConfig bytes in Orderer and Peer configs
func (c *EndpointConfig) loadOrdererPeerTLSConfig(configEntity *endpointConfigEntity) error {
//Orderers Config
for orderer, ordererConfig := range configEntity.Orderers {
//resolve paths
ordererConfig.TLSCACerts.Path = pathvar.Subst(ordererConfig.TLSCACerts.Path)
//pre load key and cert bytes
err := ordererConfig.TLSCACerts.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load orderer cert")
}
configEntity.Orderers[orderer] = ordererConfig
}
//Peer Config
for peer, peerConfig := range configEntity.Peers {
//resolve paths
peerConfig.TLSCACerts.Path = pathvar.Subst(peerConfig.TLSCACerts.Path)
//pre load key and cert bytes
err := peerConfig.TLSCACerts.LoadBytes()
if err != nil {
return errors.WithMessage(err, "failed to load peer cert")
}
configEntity.Peers[peer] = peerConfig
}
return nil
}
func (c *EndpointConfig) loadPeerConfigsByOrg() {
c.peerConfigsByOrg = make(map[string][]fab.PeerConfig)
for orgName, orgConfig := range c.networkConfig.Organizations {
orgPeers := orgConfig.Peers
peers := []fab.PeerConfig{}
for _, peerName := range orgPeers {
p := c.networkConfig.Peers[strings.ToLower(peerName)]
if err := c.verifyPeerConfig(p, peerName, endpoint.IsTLSEnabled(p.URL)); err != nil {
logger.Debugf("Could not verify Peer for [%s], trying with Entity Matchers", peerName)
matchingPeerConfig := c.tryMatchingPeerConfig(peerName)
if matchingPeerConfig == nil {
continue
}
logger.Debugf("Found a matchingPeerConfig for [%s]", peerName)
p = *matchingPeerConfig
}
peers = append(peers, p)
}
c.peerConfigsByOrg[strings.ToLower(orgName)] = peers
}
}
func (c *EndpointConfig) loadNetworkPeers() {
var netPeers []fab.NetworkPeer
for org, peerConfigs := range c.peerConfigsByOrg {
orgConfig, ok := c.networkConfig.Organizations[org]
if !ok {
continue
}
for _, peerConfig := range peerConfigs {
netPeers = append(netPeers, fab.NetworkPeer{PeerConfig: peerConfig, MSPID: orgConfig.MSPID})
}
}
c.networkPeers = netPeers
}
func (c *EndpointConfig) loadOrdererConfigs() error {
ordererConfigs := []fab.OrdererConfig{}
for name, ordererConfig := range c.networkConfig.Orderers {
matchedOrderer := c.tryMatchingOrdererConfig(name)
if matchedOrderer != nil {
//if found in entity matcher then use the matched one
ordererConfig = *matchedOrderer
}
if ordererConfig.TLSCACert == nil && !c.backend.GetBool("client.tlsCerts.systemCertPool") {
//check for TLS config only if secured connection is enabled
allowInSecure := ordererConfig.GRPCOptions["allow-insecure"] == true
if endpoint.AttemptSecured(ordererConfig.URL, allowInSecure) {
return errors.Errorf("Orderer has no certs configured. Make sure TLSCACerts.Pem or TLSCACerts.Path is set for %s", ordererConfig.URL)
}
}
ordererConfigs = append(ordererConfigs, ordererConfig)
}
c.ordererConfigs = ordererConfigs
return nil
}
func (c *EndpointConfig) loadChannelPeers() error {
channelPeersByChannel := make(map[string][]fab.ChannelPeer)
for channelID, channelConfig := range c.networkConfig.Channels {
peers := []fab.ChannelPeer{}
for peerName, chPeerConfig := range channelConfig.Peers {
// Get generic peer configuration
p, ok := c.networkConfig.Peers[strings.ToLower(peerName)]
if !ok {
logger.Debugf("Could not find Peer for [%s], trying with Entity Matchers", peerName)
matchingPeerConfig := c.tryMatchingPeerConfig(strings.ToLower(peerName))
if matchingPeerConfig == nil {
continue
}
logger.Debugf("Found matchingPeerConfig for [%s]", peerName)
p = *matchingPeerConfig
}
if err := c.verifyPeerConfig(p, peerName, endpoint.IsTLSEnabled(p.URL)); err != nil {
logger.Debugf("Verify PeerConfig failed for peer [%s], cause : [%s]", peerName, err)
return err
}
mspID, ok := c.peerMSPID(peerName)
if !ok {
return errors.Errorf("unable to find MSP ID for peer : %s", peerName)
}
networkPeer := fab.NetworkPeer{PeerConfig: p, MSPID: mspID}
peer := fab.ChannelPeer{PeerChannelConfig: chPeerConfig, NetworkPeer: networkPeer}
peers = append(peers, peer)
}
channelPeersByChannel[strings.ToLower(channelID)] = peers
}
c.channelPeersByChannel = channelPeersByChannel
return nil
}
func (c *EndpointConfig) loadChannelOrderers() error {
channelOrderersByChannel := make(map[string][]fab.OrdererConfig)
for channelID, channelConfig := range c.networkConfig.Channels {
orderers := []fab.OrdererConfig{}
for _, ordererName := range channelConfig.Orderers {
orderer, ok := c.networkConfig.Orderers[strings.ToLower(ordererName)]
if !ok {
//try entityMatcher
logger.Debugf("Could not find Orderer for [%s], trying with Entity Matchers", ordererName)
matchingOrdererConfig := c.tryMatchingOrdererConfig(strings.ToLower(ordererName))
if matchingOrdererConfig == nil {
return errors.Errorf("Could not find Orderer Config for channel orderer [%s]", ordererName)
}
logger.Debugf("Found matching Orderer Config for [%s]", ordererName)
orderer = *matchingOrdererConfig
}
orderers = append(orderers, orderer)
}
channelOrderersByChannel[strings.ToLower(channelID)] = orderers
}
c.channelOrderersByChannel = channelOrderersByChannel
return nil
}
func (c *EndpointConfig) loadTLSCertPool() error {
c.tlsCertPool = commtls.NewCertPool(c.backend.GetBool("client.tlsCerts.systemCertPool"))
// preemptively add all TLS certs to cert pool as adding them at request time
// is expensive
certs, err := c.loadTLSCerts()
if err != nil {
logger.Infof("could not cache TLS certs: %s", err)
}
if _, err := c.tlsCertPool.Get(certs...); err != nil {
return errors.WithMessage(err, "cert pool load failed")
}
return nil
}
// loadTLSClientCerts loads the client's certs for mutual TLS
// It checks the config for embedded pem files before looking for cert files
func (c *EndpointConfig) loadTLSClientCerts(configEntity *endpointConfigEntity) error {
var clientCerts tls.Certificate
cb := configEntity.Client.TLSCerts.Client.Cert.Bytes()
if len(cb) == 0 {
// if no cert found in the config, empty cert chain should be used
c.tlsClientCerts = []tls.Certificate{clientCerts}
return nil
}
// Load private key from cert using default crypto suite
cs := cryptosuite.GetDefault()
pk, err := cryptoutil.GetPrivateKeyFromCert(cb, cs)
// If CryptoSuite fails to load private key from cert then load private key from config
if err != nil || pk == nil {
logger.Debugf("Reading pk from config, unable to retrieve from cert: %s", err)
tlsClientCerts, err := c.loadPrivateKeyFromConfig(&configEntity.Client, clientCerts, cb)
if err != nil {
return errors.WithMessage(err, "failed to load TLS client certs")
}
c.tlsClientCerts = tlsClientCerts
return nil
}
// private key was retrieved from cert
clientCerts, err = cryptoutil.X509KeyPair(cb, pk, cs)
if err != nil {
return errors.WithMessage(err, "failed to load TLS client certs, failed to get X509KeyPair")
}
c.tlsClientCerts = []tls.Certificate{clientCerts}
return nil
}
func (c *EndpointConfig) getPortIfPresent(url string) (int, bool) {
s := strings.Split(url, ":")
if len(s) > 1 {
if port, err := strconv.Atoi(s[len(s)-1]); err == nil {
return port, true
}
}
return 0, false
}
func (c *EndpointConfig) tryMatchingPeerConfig(peerName string) *fab.PeerConfig {
//Return if no peerMatchers are configured
if len(c.peerMatchers) == 0 {
return nil
}
//sort the keys
var keys []int
for k := range c.peerMatchers {
keys = append(keys, k)
}
sort.Ints(keys)
//loop over peerentityMatchers to find the matching peer
for _, k := range keys {
v := c.peerMatchers[k]
logger.Debugf("Trying to match peer [%s] with matcher [%s]", peerName, v.String())
if v.MatchString(peerName) {
logger.Debugf("Peer [%s] matched using matcher [%s]", peerName, v.String())
return c.matchPeer(peerName, k, v)
}
logger.Debugf("Peer [%s] did not match using matcher [%s]", peerName, v.String())
}
return nil
}
func (c *EndpointConfig) matchPeer(peerName string, k int, v *regexp.Regexp) *fab.PeerConfig {
// get the matching matchConfig from the index number
peerMatchConfig := c.entityMatchers.matchers["peer"][k]
//Get the peerConfig from mapped host
peerConfig, ok := c.networkConfig.Peers[strings.ToLower(peerMatchConfig.MappedHost)]
if !ok {
return nil
}
// Make a copy of GRPC options (as it is manipulated below)
peerConfig.GRPCOptions = copyPropertiesMap(peerConfig.GRPCOptions)
_, isPortPresentInPeerName := c.getPortIfPresent(peerName)
//if substitution url is empty, use the same network peer url
if peerMatchConfig.URLSubstitutionExp == "" {
peerConfig.URL = getPeerConfigURL(c, peerName, peerConfig.URL, isPortPresentInPeerName)
} else {
//else, replace url with urlSubstitutionExp if it doesnt have any variable declarations like $
if !strings.Contains(peerMatchConfig.URLSubstitutionExp, "$") {
peerConfig.URL = peerMatchConfig.URLSubstitutionExp
} else {
//if the urlSubstitutionExp has $ variable declarations, use regex replaceallstring to replace networkhostname with substituionexp pattern
peerConfig.URL = v.ReplaceAllString(peerName, peerMatchConfig.URLSubstitutionExp)
}
}
//if eventSubstitution url is empty, use the same network peer url
if peerMatchConfig.EventURLSubstitutionExp == "" {
peerConfig.EventURL = getPeerConfigURL(c, peerName, peerConfig.EventURL, isPortPresentInPeerName)
} else {
//else, replace url with eventUrlSubstitutionExp if it doesnt have any variable declarations like $
if !strings.Contains(peerMatchConfig.EventURLSubstitutionExp, "$") {
peerConfig.EventURL = peerMatchConfig.EventURLSubstitutionExp
} else {
//if the eventUrlSubstitutionExp has $ variable declarations, use regex replaceallstring to replace networkhostname with eventsubstituionexp pattern
peerConfig.EventURL = v.ReplaceAllString(peerName, peerMatchConfig.EventURLSubstitutionExp)
}
}
//if sslTargetOverrideUrlSubstitutionExp is empty, use the same network peer host
if peerMatchConfig.SSLTargetOverrideURLSubstitutionExp == "" {
if !strings.Contains(peerName, ":") {
peerConfig.GRPCOptions["ssl-target-name-override"] = peerName
} else {
//Remove port and protocol of the peerName
s := strings.Split(peerName, ":")
if isPortPresentInPeerName {
peerConfig.GRPCOptions["ssl-target-name-override"] = s[len(s)-2]
} else {
peerConfig.GRPCOptions["ssl-target-name-override"] = s[len(s)-1]
}