forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
471 lines (407 loc) · 16.5 KB
/
config.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package localconfig
import (
"fmt"
"path/filepath"
"strings"
"time"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/viperutil"
cf "github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/orderer/etcdraft"
"github.com/spf13/viper"
)
const (
// Prefix identifies the prefix for the configtxgen-related ENV vars.
Prefix string = "CONFIGTX"
)
var logger = flogging.MustGetLogger("common.tools.configtxgen.localconfig")
var configName = strings.ToLower(Prefix)
const (
// TestChainID is the channel name used for testing purposes when one is
// not given
TestChainID = "testchainid"
// SampleInsecureSoloProfile references the sample profile which does not
// include any MSPs and uses solo for ordering.
SampleInsecureSoloProfile = "SampleInsecureSolo"
// SampleDevModeSoloProfile references the sample profile which requires
// only basic membership for admin privileges and uses solo for ordering.
SampleDevModeSoloProfile = "SampleDevModeSolo"
// SampleSingleMSPSoloProfile references the sample profile which includes
// only the sample MSP and uses solo for ordering.
SampleSingleMSPSoloProfile = "SampleSingleMSPSolo"
// SampleInsecureKafkaProfile references the sample profile which does not
// include any MSPs and uses Kafka for ordering.
SampleInsecureKafkaProfile = "SampleInsecureKafka"
// SampleDevModeKafkaProfile references the sample profile which requires only
// basic membership for admin privileges and uses Kafka for ordering.
SampleDevModeKafkaProfile = "SampleDevModeKafka"
// SampleSingleMSPKafkaProfile references the sample profile which includes
// only the sample MSP and uses Kafka for ordering.
SampleSingleMSPKafkaProfile = "SampleSingleMSPKafka"
// SampleDevModeEtcdRaftProfile references the sample profile used for testing
// the etcd/raft-based ordering service.
SampleDevModeEtcdRaftProfile = "SampleDevModeEtcdRaft"
// SampleSingleMSPChannelProfile references the sample profile which
// includes only the sample MSP and is used to create a channel
SampleSingleMSPChannelProfile = "SampleSingleMSPChannel"
// SampleConsortiumName is the sample consortium from the
// sample configtx.yaml
SampleConsortiumName = "SampleConsortium"
// SampleOrgName is the name of the sample org in the sample profiles
SampleOrgName = "SampleOrg"
// AdminRoleAdminPrincipal is set as AdminRole to cause the MSP role of
// type Admin to be used as the admin principal default
AdminRoleAdminPrincipal = "Role.ADMIN"
// MemberRoleAdminPrincipal is set as AdminRole to cause the MSP role of
// type Member to be used as the admin principal default
MemberRoleAdminPrincipal = "Role.MEMBER"
)
// TopLevel consists of the structs used by the configtxgen tool.
type TopLevel struct {
Profiles map[string]*Profile `yaml:"Profiles"`
Organizations []*Organization `yaml:"Organizations"`
Channel *Profile `yaml:"Channel"`
Application *Application `yaml:"Application"`
Orderer *Orderer `yaml:"Orderer"`
Capabilities map[string]map[string]bool `yaml:"Capabilities"`
Resources *Resources `yaml:"Resources"`
}
// Profile encodes orderer/application configuration combinations for the
// configtxgen tool.
type Profile struct {
Consortium string `yaml:"Consortium"`
Application *Application `yaml:"Application"`
Orderer *Orderer `yaml:"Orderer"`
Consortiums map[string]*Consortium `yaml:"Consortiums"`
Capabilities map[string]bool `yaml:"Capabilities"`
Policies map[string]*Policy `yaml:"Policies"`
}
// Policy encodes a channel config policy
type Policy struct {
Type string `yaml:"Type"`
Rule string `yaml:"Rule"`
}
// Consortium represents a group of organizations which may create channels
// with each other
type Consortium struct {
Organizations []*Organization `yaml:"Organizations"`
}
// Application encodes the application-level configuration needed in config
// transactions.
type Application struct {
Organizations []*Organization `yaml:"Organizations"`
Capabilities map[string]bool `yaml:"Capabilities"`
Resources *Resources `yaml:"Resources"`
Policies map[string]*Policy `yaml:"Policies"`
ACLs map[string]string `yaml:"ACLs"`
}
// Resources encodes the application-level resources configuration needed to
// seed the resource tree
type Resources struct {
DefaultModPolicy string
}
// Organization encodes the organization-level configuration needed in
// config transactions.
type Organization struct {
Name string `yaml:"Name"`
ID string `yaml:"ID"`
MSPDir string `yaml:"MSPDir"`
MSPType string `yaml:"MSPType"`
Policies map[string]*Policy `yaml:"Policies"`
// Note: Viper deserialization does not seem to care for
// embedding of types, so we use one organization struct
// for both orderers and applications.
AnchorPeers []*AnchorPeer `yaml:"AnchorPeers"`
OrdererEndpoints []string `yaml:"OrdererEndpoints"`
// AdminPrincipal is deprecated and may be removed in a future release
// it was used for modifying the default policy generation, but policies
// may now be specified explicitly so it is redundant and unnecessary
AdminPrincipal string `yaml:"AdminPrincipal"`
}
// AnchorPeer encodes the necessary fields to identify an anchor peer.
type AnchorPeer struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
}
// Orderer contains configuration which is used for the
// bootstrapping of an orderer by the provisional bootstrapper.
type Orderer struct {
OrdererType string `yaml:"OrdererType"`
Addresses []string `yaml:"Addresses"`
BatchTimeout time.Duration `yaml:"BatchTimeout"`
BatchSize BatchSize `yaml:"BatchSize"`
Kafka Kafka `yaml:"Kafka"`
EtcdRaft *etcdraft.ConfigMetadata `yaml:"EtcdRaft"`
Organizations []*Organization `yaml:"Organizations"`
MaxChannels uint64 `yaml:"MaxChannels"`
Capabilities map[string]bool `yaml:"Capabilities"`
Policies map[string]*Policy `yaml:"Policies"`
}
// BatchSize contains configuration affecting the size of batches.
type BatchSize struct {
MaxMessageCount uint32 `yaml:"MaxMessageCount"`
AbsoluteMaxBytes uint32 `yaml:"AbsoluteMaxBytes"`
PreferredMaxBytes uint32 `yaml:"PreferredMaxBytes"`
}
// Kafka contains configuration for the Kafka-based orderer.
type Kafka struct {
Brokers []string `yaml:"Brokers"`
}
var genesisDefaults = TopLevel{
Orderer: &Orderer{
OrdererType: "solo",
Addresses: []string{"127.0.0.1:7050"},
BatchTimeout: 2 * time.Second,
BatchSize: BatchSize{
MaxMessageCount: 500,
AbsoluteMaxBytes: 10 * 1024 * 1024,
PreferredMaxBytes: 2 * 1024 * 1024,
},
Kafka: Kafka{
Brokers: []string{"127.0.0.1:9092"},
},
EtcdRaft: &etcdraft.ConfigMetadata{
Options: &etcdraft.Options{
TickInterval: "500ms",
ElectionTick: 10,
HeartbeatTick: 1,
MaxInflightBlocks: 5,
SnapshotIntervalSize: 20 * 1024 * 1024, // 20 MB
},
},
},
}
// LoadTopLevel simply loads the configtx.yaml file into the structs above and
// completes their initialization. Config paths may optionally be provided and
// will be used in place of the FABRIC_CFG_PATH env variable.
//
// Note, for environment overrides to work properly within a profile, Load
// should be used instead.
func LoadTopLevel(configPaths ...string) *TopLevel {
config := viper.New()
if len(configPaths) > 0 {
for _, p := range configPaths {
config.AddConfigPath(p)
}
config.SetConfigName(configName)
} else {
cf.InitViper(config, configName)
}
// For environment variables
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
config.SetEnvKeyReplacer(replacer)
err := config.ReadInConfig()
if err != nil {
logger.Panic("Error reading configuration: ", err)
}
logger.Debugf("Using config file: %s", config.ConfigFileUsed())
var uconf TopLevel
err = viperutil.EnhancedExactUnmarshal(config, &uconf)
if err != nil {
logger.Panic("Error unmarshaling config into struct: ", err)
}
(&uconf).completeInitialization(filepath.Dir(config.ConfigFileUsed()))
logger.Infof("Loaded configuration: %s", config.ConfigFileUsed())
return &uconf
}
// Load returns the orderer/application config combination that corresponds to
// a given profile. Config paths may optionally be provided and will be used
// in place of the FABRIC_CFG_PATH env variable.
func Load(profile string, configPaths ...string) *Profile {
config := viper.New()
if len(configPaths) > 0 {
for _, p := range configPaths {
config.AddConfigPath(p)
}
config.SetConfigName(configName)
} else {
cf.InitViper(config, configName)
}
// For environment variables
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
// This replacer allows substitution within the particular profile without
// having to fully qualify the name
replacer := strings.NewReplacer(strings.ToUpper(fmt.Sprintf("profiles.%s.", profile)), "", ".", "_")
config.SetEnvKeyReplacer(replacer)
err := config.ReadInConfig()
if err != nil {
logger.Panic("Error reading configuration: ", err)
}
logger.Debugf("Using config file: %s", config.ConfigFileUsed())
var uconf TopLevel
err = viperutil.EnhancedExactUnmarshal(config, &uconf)
if err != nil {
logger.Panic("Error unmarshaling config into struct: ", err)
}
result, ok := uconf.Profiles[profile]
if !ok {
logger.Panic("Could not find profile: ", profile)
}
result.completeInitialization(filepath.Dir(config.ConfigFileUsed()))
logger.Infof("Loaded configuration: %s", config.ConfigFileUsed())
return result
}
func (t *TopLevel) completeInitialization(configDir string) {
for _, org := range t.Organizations {
org.completeInitialization(configDir)
}
if t.Orderer != nil {
t.Orderer.completeInitialization(configDir)
}
}
func (p *Profile) completeInitialization(configDir string) {
if p.Application != nil {
for _, org := range p.Application.Organizations {
org.completeInitialization(configDir)
}
if p.Application.Resources != nil {
p.Application.Resources.completeInitialization()
}
}
if p.Consortiums != nil {
for _, consortium := range p.Consortiums {
for _, org := range consortium.Organizations {
org.completeInitialization(configDir)
}
}
}
if p.Orderer != nil {
for _, org := range p.Orderer.Organizations {
org.completeInitialization(configDir)
}
// Some profiles will not define orderer parameters
p.Orderer.completeInitialization(configDir)
}
}
func (r *Resources) completeInitialization() {
for {
switch {
case r.DefaultModPolicy == "":
r.DefaultModPolicy = policies.ChannelApplicationAdmins
default:
return
}
}
}
func (org *Organization) completeInitialization(configDir string) {
// set the MSP type; if none is specified we assume BCCSP
if org.MSPType == "" {
org.MSPType = msp.ProviderTypeToString(msp.FABRIC)
}
if org.AdminPrincipal == "" {
org.AdminPrincipal = AdminRoleAdminPrincipal
}
translatePaths(configDir, org)
}
func (ord *Orderer) completeInitialization(configDir string) {
loop:
for {
switch {
case ord.OrdererType == "":
logger.Infof("Orderer.OrdererType unset, setting to %v", genesisDefaults.Orderer.OrdererType)
ord.OrdererType = genesisDefaults.Orderer.OrdererType
case ord.Addresses == nil:
logger.Infof("Orderer.Addresses unset, setting to %s", genesisDefaults.Orderer.Addresses)
ord.Addresses = genesisDefaults.Orderer.Addresses
case ord.BatchTimeout == 0:
logger.Infof("Orderer.BatchTimeout unset, setting to %s", genesisDefaults.Orderer.BatchTimeout)
ord.BatchTimeout = genesisDefaults.Orderer.BatchTimeout
case ord.BatchSize.MaxMessageCount == 0:
logger.Infof("Orderer.BatchSize.MaxMessageCount unset, setting to %v", genesisDefaults.Orderer.BatchSize.MaxMessageCount)
ord.BatchSize.MaxMessageCount = genesisDefaults.Orderer.BatchSize.MaxMessageCount
case ord.BatchSize.AbsoluteMaxBytes == 0:
logger.Infof("Orderer.BatchSize.AbsoluteMaxBytes unset, setting to %v", genesisDefaults.Orderer.BatchSize.AbsoluteMaxBytes)
ord.BatchSize.AbsoluteMaxBytes = genesisDefaults.Orderer.BatchSize.AbsoluteMaxBytes
case ord.BatchSize.PreferredMaxBytes == 0:
logger.Infof("Orderer.BatchSize.PreferredMaxBytes unset, setting to %v", genesisDefaults.Orderer.BatchSize.PreferredMaxBytes)
ord.BatchSize.PreferredMaxBytes = genesisDefaults.Orderer.BatchSize.PreferredMaxBytes
default:
break loop
}
}
logger.Infof("orderer type: %s", ord.OrdererType)
// Additional, consensus type-dependent initialization goes here
// Also using this to panic on unknown orderer type.
switch ord.OrdererType {
case "solo":
// nothing to be done here
case "kafka":
if ord.Kafka.Brokers == nil {
logger.Infof("Orderer.Kafka unset, setting to %v", genesisDefaults.Orderer.Kafka.Brokers)
ord.Kafka.Brokers = genesisDefaults.Orderer.Kafka.Brokers
}
case etcdraft.TypeKey:
if ord.EtcdRaft == nil {
logger.Panicf("%s raft configuration missing", etcdraft.TypeKey)
}
if ord.EtcdRaft.Options == nil {
logger.Infof("Orderer.EtcdRaft.Options unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options)
ord.EtcdRaft.Options = genesisDefaults.Orderer.EtcdRaft.Options
}
second_loop:
for {
switch {
case ord.EtcdRaft.Options.TickInterval == "":
logger.Infof("Orderer.EtcdRaft.Options.TickInterval unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options.TickInterval)
ord.EtcdRaft.Options.TickInterval = genesisDefaults.Orderer.EtcdRaft.Options.TickInterval
case ord.EtcdRaft.Options.ElectionTick == 0:
logger.Infof("Orderer.EtcdRaft.Options.ElectionTick unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options.ElectionTick)
ord.EtcdRaft.Options.ElectionTick = genesisDefaults.Orderer.EtcdRaft.Options.ElectionTick
case ord.EtcdRaft.Options.HeartbeatTick == 0:
logger.Infof("Orderer.EtcdRaft.Options.HeartbeatTick unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options.HeartbeatTick)
ord.EtcdRaft.Options.HeartbeatTick = genesisDefaults.Orderer.EtcdRaft.Options.HeartbeatTick
case ord.EtcdRaft.Options.MaxInflightBlocks == 0:
logger.Infof("Orderer.EtcdRaft.Options.MaxInflightBlocks unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options.MaxInflightBlocks)
ord.EtcdRaft.Options.MaxInflightBlocks = genesisDefaults.Orderer.EtcdRaft.Options.MaxInflightBlocks
case ord.EtcdRaft.Options.SnapshotIntervalSize == 0:
logger.Infof("Orderer.EtcdRaft.Options.SnapshotIntervalSize unset, setting to %v", genesisDefaults.Orderer.EtcdRaft.Options.SnapshotIntervalSize)
ord.EtcdRaft.Options.SnapshotIntervalSize = genesisDefaults.Orderer.EtcdRaft.Options.SnapshotIntervalSize
case len(ord.EtcdRaft.Consenters) == 0:
logger.Panicf("%s configuration did not specify any consenter", etcdraft.TypeKey)
default:
break second_loop
}
}
if _, err := time.ParseDuration(ord.EtcdRaft.Options.TickInterval); err != nil {
logger.Panicf("Etcdraft TickInterval (%s) must be in time duration format", ord.EtcdRaft.Options.TickInterval)
}
// validate the specified members for Options
if ord.EtcdRaft.Options.ElectionTick <= ord.EtcdRaft.Options.HeartbeatTick {
logger.Panicf("election tick must be greater than heartbeat tick")
}
for _, c := range ord.EtcdRaft.GetConsenters() {
if c.Host == "" {
logger.Panicf("consenter info in %s configuration did not specify host", etcdraft.TypeKey)
}
if c.Port == 0 {
logger.Panicf("consenter info in %s configuration did not specify port", etcdraft.TypeKey)
}
if c.ClientTlsCert == nil {
logger.Panicf("consenter info in %s configuration did not specify client TLS cert", etcdraft.TypeKey)
}
if c.ServerTlsCert == nil {
logger.Panicf("consenter info in %s configuration did not specify server TLS cert", etcdraft.TypeKey)
}
clientCertPath := string(c.GetClientTlsCert())
cf.TranslatePathInPlace(configDir, &clientCertPath)
c.ClientTlsCert = []byte(clientCertPath)
serverCertPath := string(c.GetServerTlsCert())
cf.TranslatePathInPlace(configDir, &serverCertPath)
c.ServerTlsCert = []byte(serverCertPath)
}
default:
logger.Panicf("unknown orderer type: %s", ord.OrdererType)
}
}
func translatePaths(configDir string, org *Organization) {
cf.TranslatePathInPlace(configDir, &org.MSPDir)
}