-
Notifications
You must be signed in to change notification settings - Fork 35
/
encoder.go
205 lines (176 loc) · 8.06 KB
/
encoder.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
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package configtx
import (
cb "github.com/hyperledger/fabric-protos-go/common"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
)
// NewApplicationGroup returns the application component of the channel configuration. It defines the organizations which are involved
// in application logic like chaincodes, and how these members may interact with the orderer. It sets the mod_policy of all elements to "Admins".
func NewApplicationGroup(conf *Application) (*cb.ConfigGroup, error) {
applicationGroup := protoutil.NewConfigGroup()
if err := AddPolicies(applicationGroup, conf.Policies, channelconfig.AdminsPolicyKey); err != nil {
return nil, errors.Wrapf(err, "error adding policies to application group")
}
if len(conf.ACLs) > 0 {
addValue(applicationGroup, channelconfig.ACLValues(conf.ACLs), channelconfig.AdminsPolicyKey)
}
if len(conf.Capabilities) > 0 {
addValue(applicationGroup, channelconfig.CapabilitiesValue(conf.Capabilities), channelconfig.AdminsPolicyKey)
}
for _, org := range conf.Organizations {
var err error
applicationGroup.Groups[org.Name], err = NewApplicationOrgGroup(org)
if err != nil {
return nil, errors.Wrap(err, "failed to create application org")
}
}
applicationGroup.ModPolicy = channelconfig.AdminsPolicyKey
return applicationGroup, nil
}
// NewApplicationOrgGroup returns an application org component of the channel configuration. It defines the crypto material for the organization
// (its MSP) as well as its anchor peers for use by the gossip network. It sets the mod_policy of all elements to "Admins".
func NewApplicationOrgGroup(conf *Organization) (*cb.ConfigGroup, error) {
applicationOrgGroup := protoutil.NewConfigGroup()
applicationOrgGroup.ModPolicy = channelconfig.AdminsPolicyKey
if conf.SkipAsForeign {
return applicationOrgGroup, nil
}
mspConfig, err := msp.GetVerifyingMspConfig(conf.MSPDir, conf.ID, conf.MSPType)
if err != nil {
return nil, errors.Wrapf(err, "1 - Error loading MSP configuration for org %s", conf.Name)
}
if err := AddPolicies(applicationOrgGroup, conf.Policies, channelconfig.AdminsPolicyKey); err != nil {
return nil, errors.Wrapf(err, "error adding policies to application org group %s", conf.Name)
}
addValue(applicationOrgGroup, channelconfig.MSPValue(mspConfig), channelconfig.AdminsPolicyKey)
var anchorProtos []*pb.AnchorPeer
for _, anchorPeer := range conf.AnchorPeers {
anchorProtos = append(anchorProtos, &pb.AnchorPeer{
Host: anchorPeer.Host,
Port: int32(anchorPeer.Port),
})
}
// Avoid adding an unnecessary anchor peers element when one is not required. This helps
// prevent a delta from the orderer system channel when computing more complex channel
// creation transactions
if len(anchorProtos) > 0 {
addValue(applicationOrgGroup, channelconfig.AnchorPeersValue(anchorProtos), channelconfig.AdminsPolicyKey)
}
return applicationOrgGroup, nil
}
// NewConsortiumsGroup returns the consortiums component of the channel configuration. This element is only defined for the ordering system channel.
// It sets the mod_policy for all elements to "/Channel/Orderer/Admins".
func NewConsortiumsGroup(conf map[string]*Consortium) (*cb.ConfigGroup, error) {
consortiumsGroup := protoutil.NewConfigGroup()
// This policy is not referenced anywhere, it is only used as part of the implicit meta policy rule at the channel level, so this setting
// effectively degrades control of the ordering system channel to the ordering admins
addPolicy(consortiumsGroup, policies.SignaturePolicy(channelconfig.AdminsPolicyKey, cauthdsl.AcceptAllPolicy), ordererAdminsPolicyName)
for consortiumName, consortium := range conf {
var err error
consortiumsGroup.Groups[consortiumName], err = NewConsortiumGroup(consortium)
if err != nil {
return nil, errors.Wrapf(err, "failed to create consortium %s", consortiumName)
}
}
consortiumsGroup.ModPolicy = ordererAdminsPolicyName
return consortiumsGroup, nil
}
// NewConsortiums returns a consortiums component of the channel configuration. Each consortium defines the organizations which may be involved in channel
// creation, as well as the channel creation policy the orderer checks at channel creation time to authorize the action. It sets the mod_policy of all
// elements to "/Channel/Orderer/Admins".
func NewConsortiumGroup(conf *Consortium) (*cb.ConfigGroup, error) {
consortiumGroup := protoutil.NewConfigGroup()
for _, org := range conf.Organizations {
var err error
consortiumGroup.Groups[org.Name], err = NewConsortiumOrgGroup(org)
if err != nil {
return nil, errors.Wrap(err, "failed to create consortium org")
}
}
addValue(consortiumGroup, channelconfig.ChannelCreationPolicyValue(policies.ImplicitMetaAnyPolicy(channelconfig.AdminsPolicyKey).Value()), ordererAdminsPolicyName)
consortiumGroup.ModPolicy = ordererAdminsPolicyName
return consortiumGroup, nil
}
// NewConsortiumsGroup returns an org component of the channel configuration. It defines the crypto material for the
// organization (its MSP). It sets the mod_policy of all elements to "Admins".
func NewConsortiumOrgGroup(conf *Organization) (*cb.ConfigGroup, error) {
consortiumsOrgGroup := protoutil.NewConfigGroup()
consortiumsOrgGroup.ModPolicy = channelconfig.AdminsPolicyKey
if conf.SkipAsForeign {
return consortiumsOrgGroup, nil
}
mspConfig, err := msp.GetVerifyingMspConfig(conf.MSPDir, conf.ID, conf.MSPType)
if err != nil {
return nil, errors.Wrapf(err, "error loading MSP configuration for org: %s", conf.Name)
}
if err := AddPolicies(consortiumsOrgGroup, conf.Policies, channelconfig.AdminsPolicyKey); err != nil {
return nil, errors.Wrapf(err, "error adding policies to consortiums org group '%s'", conf.Name)
}
addValue(consortiumsOrgGroup, channelconfig.MSPValue(mspConfig), channelconfig.AdminsPolicyKey)
return consortiumsOrgGroup, nil
}
func AddPolicies(cg *cb.ConfigGroup, policyMap map[string]*Policy, modPolicy string) error {
switch {
case policyMap == nil:
return errors.Errorf("no policies defined")
case policyMap[channelconfig.AdminsPolicyKey] == nil:
return errors.Errorf("no Admins policy defined")
case policyMap[channelconfig.ReadersPolicyKey] == nil:
return errors.Errorf("no Readers policy defined")
case policyMap[channelconfig.WritersPolicyKey] == nil:
return errors.Errorf("no Writers policy defined")
}
for policyName, policy := range policyMap {
switch policy.Type {
case ImplicitMetaPolicyType:
imp, err := policies.ImplicitMetaFromString(policy.Rule)
if err != nil {
return errors.Wrapf(err, "invalid implicit meta policy rule '%s'", policy.Rule)
}
cg.Policies[policyName] = &cb.ConfigPolicy{
ModPolicy: modPolicy,
Policy: &cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: protoutil.MarshalOrPanic(imp),
},
}
case SignaturePolicyType:
sp, err := cauthdsl.FromString(policy.Rule)
if err != nil {
return errors.Wrapf(err, "invalid signature policy rule '%s'", policy.Rule)
}
cg.Policies[policyName] = &cb.ConfigPolicy{
ModPolicy: modPolicy,
Policy: &cb.Policy{
Type: int32(cb.Policy_SIGNATURE),
Value: protoutil.MarshalOrPanic(sp),
},
}
default:
return errors.Errorf("unknown policy type: %s", policy.Type)
}
}
return nil
}