forked from hyperledger/fabric-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.go
277 lines (227 loc) · 6.86 KB
/
policies.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package configtx
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/SmartBFT-Go/fabric-config/configtx/internal/policydsl"
cb "github.com/SmartBFT-Go/fabric-protos-go/v2/common"
mb "github.com/SmartBFT-Go/fabric-protos-go/v2/msp"
"github.com/golang/protobuf/proto"
)
// getPolicies returns a map of Policy from given map of ConfigPolicy in organization config group.
func getPolicies(policies map[string]*cb.ConfigPolicy) (map[string]Policy, error) {
p := map[string]Policy{}
for name, policy := range policies {
switch cb.Policy_PolicyType(policy.Policy.Type) {
case cb.Policy_IMPLICIT_META:
imp := &cb.ImplicitMetaPolicy{}
err := proto.Unmarshal(policy.Policy.Value, imp)
if err != nil {
return nil, err
}
rule, err := implicitMetaToString(imp)
if err != nil {
return nil, err
}
p[name] = Policy{
Type: ImplicitMetaPolicyType,
Rule: rule,
ModPolicy: policy.GetModPolicy(),
}
case cb.Policy_SIGNATURE:
sp := &cb.SignaturePolicyEnvelope{}
err := proto.Unmarshal(policy.Policy.Value, sp)
if err != nil {
return nil, err
}
rule, err := signatureMetaToString(sp)
if err != nil {
return nil, err
}
p[name] = Policy{
Type: SignaturePolicyType,
Rule: rule,
ModPolicy: policy.GetModPolicy(),
}
default:
return nil, fmt.Errorf("unknown policy type: %v", policy.Policy.Type)
}
}
return p, nil
}
// implicitMetaToString converts a *cb.ImplicitMetaPolicy to a string representation.
func implicitMetaToString(imp *cb.ImplicitMetaPolicy) (string, error) {
var args string
switch imp.Rule {
case cb.ImplicitMetaPolicy_ANY:
args += cb.ImplicitMetaPolicy_ANY.String()
case cb.ImplicitMetaPolicy_ALL:
args += cb.ImplicitMetaPolicy_ALL.String()
case cb.ImplicitMetaPolicy_MAJORITY:
args += cb.ImplicitMetaPolicy_MAJORITY.String()
default:
return "", fmt.Errorf("unknown implicit meta policy rule type %v", imp.Rule)
}
args = args + " " + imp.SubPolicy
return args, nil
}
// signatureMetaToString converts a *cb.SignaturePolicyEnvelope to a string representation.
func signatureMetaToString(sig *cb.SignaturePolicyEnvelope) (string, error) {
var roles []string
for _, id := range sig.Identities {
role, err := mspPrincipalToString(id)
if err != nil {
return "", err
}
roles = append(roles, role)
}
return signaturePolicyToString(sig.Rule, roles)
}
// mspPrincipalToString converts a *mb.MSPPrincipal to a string representation.
func mspPrincipalToString(principal *mb.MSPPrincipal) (string, error) {
switch principal.PrincipalClassification {
case mb.MSPPrincipal_ROLE:
var res strings.Builder
role := &mb.MSPRole{}
err := proto.Unmarshal(principal.Principal, role)
if err != nil {
return "", err
}
res.WriteString("'")
res.WriteString(role.MspIdentifier)
res.WriteString(".")
res.WriteString(strings.ToLower(role.Role.String()))
res.WriteString("'")
return res.String(), nil
// TODO: currently fabric only support string to principle convertion for
// type ROLE. Implement MSPPrinciple to String for types ORGANIZATION_UNIT,
// IDENTITY, ANONYMITY, and GOMBINED once we have support from fabric.
case mb.MSPPrincipal_ORGANIZATION_UNIT:
return "", nil
case mb.MSPPrincipal_IDENTITY:
return "", nil
case mb.MSPPrincipal_ANONYMITY:
return "", nil
case mb.MSPPrincipal_COMBINED:
return "", nil
default:
return "", fmt.Errorf("unknown MSP principal classiciation %v", principal.PrincipalClassification)
}
}
// signaturePolicyToString recursively converts a *cb.SignaturePolicy to a
// string representation.
func signaturePolicyToString(sig *cb.SignaturePolicy, IDs []string) (string, error) {
switch sig.Type.(type) {
case *cb.SignaturePolicy_NOutOf_:
nOutOf := sig.GetNOutOf()
var policies []string
var res strings.Builder
// get gate values
gate := policydsl.GateOutOf
if nOutOf.N == 1 {
gate = policydsl.GateOr
}
if nOutOf.N == int32(len(nOutOf.Rules)) {
gate = policydsl.GateAnd
}
if gate == policydsl.GateOutOf {
policies = append(policies, strconv.Itoa(int(nOutOf.N)))
}
// get subpolicies recursively
for _, rule := range nOutOf.Rules {
subPolicy, err := signaturePolicyToString(rule, IDs)
if err != nil {
return "", err
}
policies = append(policies, subPolicy)
}
res.WriteString(strings.ToUpper(gate))
res.WriteString("(")
res.WriteString(strings.Join(policies, ", "))
res.WriteString(")")
return res.String(), nil
case *cb.SignaturePolicy_SignedBy:
return IDs[sig.GetSignedBy()], nil
default:
return "", fmt.Errorf("unknown signature policy type %v", sig.Type)
}
}
func setPolicies(cg *cb.ConfigGroup, policyMap map[string]Policy) error {
if policyMap == nil {
return errors.New("no policies defined")
}
if _, ok := policyMap[AdminsPolicyKey]; !ok {
return errors.New("no Admins policy defined")
}
if _, ok := policyMap[ReadersPolicyKey]; !ok {
return errors.New("no Readers policy defined")
}
if _, ok := policyMap[WritersPolicyKey]; !ok {
return errors.New("no Writers policy defined")
}
cg.Policies = make(map[string]*cb.ConfigPolicy)
for policyName, policy := range policyMap {
err := setPolicy(cg, policyName, policy)
if err != nil {
return err
}
}
return nil
}
func setPolicy(cg *cb.ConfigGroup, policyName string, policy Policy) error {
if cg.Policies == nil {
cg.Policies = make(map[string]*cb.ConfigPolicy)
}
switch policy.Type {
case ImplicitMetaPolicyType:
imp, err := implicitMetaFromString(policy.Rule)
if err != nil {
return fmt.Errorf("invalid implicit meta policy rule: '%s': %v", policy.Rule, err)
}
implicitMetaPolicy, err := proto.Marshal(imp)
if err != nil {
return fmt.Errorf("marshaling implicit meta policy: %v", err)
}
if policy.ModPolicy == "" {
policy.ModPolicy = AdminsPolicyKey
}
cg.Policies[policyName] = &cb.ConfigPolicy{
ModPolicy: policy.ModPolicy,
Policy: &cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: implicitMetaPolicy,
},
}
case SignaturePolicyType:
sp, err := policydsl.FromString(policy.Rule)
if err != nil {
return fmt.Errorf("invalid signature policy rule: '%s': %v", policy.Rule, err)
}
signaturePolicy, err := proto.Marshal(sp)
if err != nil {
return fmt.Errorf("marshaling signature policy: %v", err)
}
if policy.ModPolicy == "" {
policy.ModPolicy = AdminsPolicyKey
}
cg.Policies[policyName] = &cb.ConfigPolicy{
ModPolicy: policy.ModPolicy,
Policy: &cb.Policy{
Type: int32(cb.Policy_SIGNATURE),
Value: signaturePolicy,
},
}
default:
return fmt.Errorf("unknown policy type: %s", policy.Type)
}
return nil
}
// removePolicy removes an existing policy from an group key organization.
func removePolicy(configGroup *cb.ConfigGroup, policyName string, policies map[string]Policy) {
delete(configGroup.Policies, policyName)
}