@@ -18,6 +18,9 @@ package sharedconfig
18
18
19
19
import (
20
20
"fmt"
21
+ "regexp"
22
+ "strconv"
23
+ "strings"
21
24
22
25
cb "github.com/hyperledger/fabric/protos/common"
23
26
ab "github.com/hyperledger/fabric/protos/orderer"
@@ -26,14 +29,19 @@ import (
26
29
"github.com/op/go-logging"
27
30
)
28
31
29
- // ConsensusTypeKey is the cb.ConfigurationItem type key name for the ConsensusType message
30
- const ConsensusTypeKey = "ConsensusType"
32
+ const (
33
+ // ConsensusTypeKey is the cb.ConfigurationItem type key name for the ConsensusType message
34
+ ConsensusTypeKey = "ConsensusType"
31
35
32
- // BatchSizeKey is the cb.ConfigurationItem type key name for the BatchSize message
33
- const BatchSizeKey = "BatchSize"
36
+ // BatchSizeKey is the cb.ConfigurationItem type key name for the BatchSize message
37
+ BatchSizeKey = "BatchSize"
34
38
35
- // ChainCreatorsKey is the cb.ConfigurationItem type key name for the ChainCreators message
36
- const ChainCreatorsKey = "ChainCreators"
39
+ // ChainCreatorsKey is the cb.ConfigurationItem type key name for the ChainCreators message
40
+ ChainCreatorsKey = "ChainCreators"
41
+
42
+ // KafkaBrokersKey is the cb.ConfigurationItem type key name for the KafkaBrokers message
43
+ KafkaBrokersKey = "KafkaBrokers"
44
+ )
37
45
38
46
var logger = logging .MustGetLogger ("orderer/common/sharedconfig" )
39
47
@@ -55,12 +63,18 @@ type Manager interface {
55
63
// ChainCreators returns the policy names which are allowed for chain creation
56
64
// This field is only set for the system ordering chain
57
65
ChainCreators () []string
66
+
67
+ // KafkaBrokers returns the addresses (IP:port notation) of a set of "bootstrap"
68
+ // Kafka brokers, i.e. this is not necessarily the entire set of Kafka brokers
69
+ // used for ordering
70
+ KafkaBrokers () []string
58
71
}
59
72
60
73
type ordererConfig struct {
61
74
consensusType string
62
75
batchSize uint32
63
76
chainCreators []string
77
+ kafkaBrokers []string
64
78
}
65
79
66
80
// ManagerImpl is an implementation of Manager and configtx.ConfigHandler
@@ -93,6 +107,13 @@ func (pm *ManagerImpl) ChainCreators() []string {
93
107
return pm .config .chainCreators
94
108
}
95
109
110
+ // KafkaBrokers returns the addresses (IP:port notation) of a set of "bootstrap"
111
+ // Kafka brokers, i.e. this is not necessarily the entire set of Kafka brokers
112
+ // used for ordering
113
+ func (pm * ManagerImpl ) KafkaBrokers () []string {
114
+ return pm .config .kafkaBrokers
115
+ }
116
+
96
117
// BeginConfig is used to start a new configuration proposal
97
118
func (pm * ManagerImpl ) BeginConfig () {
98
119
if pm .pendingConfig != nil {
@@ -135,7 +156,6 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigurationItem) error {
135
156
if consensusType .Type != pm .config .consensusType {
136
157
return fmt .Errorf ("Attempted to change the consensus type from %s to %s after init" , pm .config .consensusType , consensusType .Type )
137
158
}
138
-
139
159
pm .pendingConfig .consensusType = consensusType .Type
140
160
case BatchSizeKey :
141
161
batchSize := & ab.BatchSize {}
@@ -147,17 +167,60 @@ func (pm *ManagerImpl) ProposeConfig(configItem *cb.ConfigurationItem) error {
147
167
if batchSize .Messages <= 0 {
148
168
return fmt .Errorf ("Attempted to set the batch size to %d which is less than or equal to 0" , batchSize .Messages )
149
169
}
150
-
151
170
pm .pendingConfig .batchSize = batchSize .Messages
152
171
case ChainCreatorsKey :
153
172
chainCreators := & ab.ChainCreators {}
154
173
err := proto .Unmarshal (configItem .Value , chainCreators )
155
174
if err != nil {
156
175
return fmt .Errorf ("Unmarshaling error for ChainCreator: %s" , err )
157
176
}
158
-
159
177
pm .pendingConfig .chainCreators = chainCreators .Policies
178
+ case KafkaBrokersKey :
179
+ kafkaBrokers := & ab.KafkaBrokers {}
180
+ err := proto .Unmarshal (configItem .Value , kafkaBrokers )
181
+ if err != nil {
182
+ return fmt .Errorf ("Unmarshaling error for KafkaBrokers: %s" , err )
183
+ }
184
+ if len (kafkaBrokers .Brokers ) == 0 {
185
+ return fmt .Errorf ("Kafka broker set cannot be nil" )
186
+ }
187
+ for _ , broker := range kafkaBrokers .Brokers {
188
+ if ! brokerEntrySeemsValid (broker ) {
189
+ return fmt .Errorf ("Invalid broker entry: %s" , broker )
190
+ }
191
+ }
192
+ pm .pendingConfig .kafkaBrokers = kafkaBrokers .Brokers
160
193
}
161
-
162
194
return nil
163
195
}
196
+
197
+ // This does just a barebones sanitfy check.
198
+ func brokerEntrySeemsValid (broker string ) bool {
199
+ if ! strings .Contains (broker , ":" ) {
200
+ return false
201
+ }
202
+
203
+ parts := strings .Split (broker , ":" )
204
+ if len (parts ) > 2 {
205
+ return false
206
+ }
207
+
208
+ host := parts [0 ]
209
+ port := parts [1 ]
210
+
211
+ if _ , err := strconv .ParseUint (port , 10 , 16 ); err != nil {
212
+ return false
213
+ }
214
+
215
+ // Valid hostnames may contain only the ASCII letters 'a' through 'z' (in a
216
+ // case-insensitive manner), the digits '0' through '9', and the hyphen. IP
217
+ // v4 addresses are represented in dot-decimal notation, which consists of
218
+ // four decimal numbers, each ranging from 0 to 255, separated by dots,
219
+ // e.g., 172.16.254.1
220
+ // The following regular expression:
221
+ // 1. allows just a-z (case-insensitive), 0-9, and the dot and hyphen characters
222
+ // 2. does not allow leading trailing dots or hyphens
223
+ re , _ := regexp .Compile ("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9.-]*[a-zA-Z0-9])$" )
224
+ matched := re .FindString (host )
225
+ return len (matched ) == len (host )
226
+ }
0 commit comments