Skip to content

Commit 94e8fa4

Browse files
author
Jason Yellick
committed
[FAB-2325] Add reader/writer/admin to orgs
https://jira.hyperledger.org/browse/FAB-2325 This CR adds automatic encoding of a reader/writer/admin policy for each MSP created via the MSP templating tool. In combination with the default reader/writer/admin policies at the group level from [FAB-2324] the reader/writer/admin policies should be ready to be consumed by other parts of the system. Change-Id: I22a70ba33a7aadd99e8c5da7f813e6794c78bede Signed-off-by: Jason Yellick <[email protected]>
1 parent a9ad961 commit 94e8fa4

File tree

3 files changed

+88
-21
lines changed

3 files changed

+88
-21
lines changed

common/cauthdsl/cauthdsl_builder.go

+18
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ func SignedByMspMember(mspId string) *cb.SignaturePolicyEnvelope {
9292
return p
9393
}
9494

95+
// SignedByMspAdmin creates a SignaturePolicyEnvelope
96+
// requiring 1 signature from any admin of the specified MSP
97+
func SignedByMspAdmin(mspId string) *cb.SignaturePolicyEnvelope {
98+
// specify the principal: it's a member of the msp we just found
99+
principal := &cb.MSPPrincipal{
100+
PrincipalClassification: cb.MSPPrincipal_ROLE,
101+
Principal: utils.MarshalOrPanic(&cb.MSPRole{Role: cb.MSPRole_ADMIN, MspIdentifier: mspId})}
102+
103+
// create the policy: it requires exactly 1 signature from the first (and only) principal
104+
p := &cb.SignaturePolicyEnvelope{
105+
Version: 0,
106+
Policy: NOutOf(1, []*cb.SignaturePolicy{SignedBy(0)}),
107+
Identities: []*cb.MSPPrincipal{principal},
108+
}
109+
110+
return p
111+
}
112+
95113
// And is a convenience method which utilizes NOutOf to produce And equivalent behavior
96114
func And(lhs, rhs *cb.SignaturePolicy) *cb.SignaturePolicy {
97115
return NOutOf(2, []*cb.SignaturePolicy{lhs, rhs})

common/configtx/tool/provisional/provisional.go

+10-18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
configtxchannel "github.com/hyperledger/fabric/common/configvalues/channel"
2626
configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
2727
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
28+
configvaluesmsp "github.com/hyperledger/fabric/common/configvalues/msp"
2829
"github.com/hyperledger/fabric/common/genesis"
2930
"github.com/hyperledger/fabric/common/policies"
3031
"github.com/hyperledger/fabric/orderer/common/bootstrap"
@@ -56,15 +57,6 @@ const (
5657

5758
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
5859
AcceptAllPolicyKey = "AcceptAllPolicy"
59-
60-
// ReadersPolicyKey is the key used for the read policy
61-
ReadersPolicyKey = "Readers"
62-
63-
// WritersPolicyKey is the key used for the read policy
64-
WritersPolicyKey = "Writers"
65-
66-
// AdminsPolicyKey is the key used for the read policy
67-
AdminsPolicyKey = "Admins"
6860
)
6961

7062
// DefaultChainCreationPolicyNames is the default value of ChainCreatorsKey.
@@ -99,19 +91,19 @@ func New(conf *genesisconfig.TopLevel) Generator {
9991
cauthdsl.TemplatePolicy(AcceptAllPolicyKey, cauthdsl.AcceptAllPolicy),
10092

10193
// Initialize the default Reader/Writer/Admins channel policies
102-
policies.TemplateImplicitMetaAnyPolicy([]string{}, ReadersPolicyKey),
103-
policies.TemplateImplicitMetaAnyPolicy([]string{}, WritersPolicyKey),
104-
policies.TemplateImplicitMetaMajorityPolicy([]string{}, AdminsPolicyKey),
94+
policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.ReadersPolicyKey),
95+
policies.TemplateImplicitMetaAnyPolicy([]string{}, configvaluesmsp.WritersPolicyKey),
96+
policies.TemplateImplicitMetaMajorityPolicy([]string{}, configvaluesmsp.AdminsPolicyKey),
10597

10698
// Initialize the default Reader/Writer/Admins orderer policies
107-
policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, ReadersPolicyKey),
108-
policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, WritersPolicyKey),
109-
policies.TemplateImplicitMetaMajorityPolicy([]string{configtxorderer.GroupKey}, AdminsPolicyKey),
99+
policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.ReadersPolicyKey),
100+
policies.TemplateImplicitMetaAnyPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.WritersPolicyKey),
101+
policies.TemplateImplicitMetaMajorityPolicy([]string{configtxorderer.GroupKey}, configvaluesmsp.AdminsPolicyKey),
110102

111103
// Initialize the default Reader/Writer/Admins application policies
112-
policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, ReadersPolicyKey),
113-
policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, WritersPolicyKey),
114-
policies.TemplateImplicitMetaMajorityPolicy([]string{configtxapplication.GroupKey}, AdminsPolicyKey),
104+
policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.ReadersPolicyKey),
105+
policies.TemplateImplicitMetaAnyPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.WritersPolicyKey),
106+
policies.TemplateImplicitMetaMajorityPolicy([]string{configtxapplication.GroupKey}, configvaluesmsp.AdminsPolicyKey),
115107
},
116108

117109
systemChainGroups: []*cb.ConfigGroup{

common/configvalues/msp/config_util.go

+60-3
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,82 @@ limitations under the License.
1717
package msp
1818

1919
import (
20+
"github.com/hyperledger/fabric/common/cauthdsl"
21+
"github.com/hyperledger/fabric/msp"
2022
cb "github.com/hyperledger/fabric/protos/common"
21-
"github.com/hyperledger/fabric/protos/msp"
23+
mspprotos "github.com/hyperledger/fabric/protos/msp"
2224
"github.com/hyperledger/fabric/protos/utils"
25+
26+
logging "github.com/op/go-logging"
2327
)
2428

29+
var logger = logging.MustGetLogger("configvalues/msp")
30+
2531
const (
32+
// ReadersPolicyKey is the key used for the read policy
33+
ReadersPolicyKey = "Readers"
34+
35+
// WritersPolicyKey is the key used for the read policy
36+
WritersPolicyKey = "Writers"
37+
38+
// AdminsPolicyKey is the key used for the read policy
39+
AdminsPolicyKey = "Admins"
40+
41+
// MSPKey is the org key used for MSP configuration
2642
MSPKey = "MSP"
2743
)
2844

2945
// TemplateGroupMSP creates an MSP ConfigValue at the given configPath
30-
func TemplateGroupMSP(configPath []string, mspConf *msp.MSPConfig) *cb.ConfigGroup {
46+
func TemplateGroupMSP(configPath []string, mspConfig *mspprotos.MSPConfig) *cb.ConfigGroup {
47+
// check that the type for that MSP is supported
48+
if mspConfig.Type != int32(msp.FABRIC) {
49+
logger.Panicf("Setup error: unsupported msp type %d", mspConfig.Type)
50+
}
51+
52+
// create the msp instance
53+
mspInst, err := msp.NewBccspMsp()
54+
if err != nil {
55+
logger.Panicf("Creating the MSP manager failed, err %s", err)
56+
}
57+
58+
// set it up
59+
err = mspInst.Setup(mspConfig)
60+
if err != nil {
61+
logger.Panicf("Setting up the MSP manager failed, err %s", err)
62+
}
63+
64+
// add the MSP to the map of pending MSPs
65+
mspID, err := mspInst.GetIdentifier()
66+
if err != nil {
67+
logger.Panicf("Could not extract msp identifier, err %s", err)
68+
}
69+
70+
memberPolicy := &cb.ConfigPolicy{
71+
Policy: &cb.Policy{
72+
Type: int32(cb.Policy_SIGNATURE),
73+
Policy: utils.MarshalOrPanic(cauthdsl.SignedByMspMember(mspID)),
74+
},
75+
}
76+
77+
adminPolicy := &cb.ConfigPolicy{
78+
Policy: &cb.Policy{
79+
Type: int32(cb.Policy_SIGNATURE),
80+
Policy: utils.MarshalOrPanic(cauthdsl.SignedByMspAdmin(mspID)),
81+
},
82+
}
83+
3184
result := cb.NewConfigGroup()
85+
3286
intermediate := result
3387
for _, group := range configPath {
3488
intermediate.Groups[group] = cb.NewConfigGroup()
3589
intermediate = intermediate.Groups[group]
3690
}
3791
intermediate.Values[MSPKey] = &cb.ConfigValue{
38-
Value: utils.MarshalOrPanic(mspConf),
92+
Value: utils.MarshalOrPanic(mspConfig),
3993
}
94+
intermediate.Policies[AdminsPolicyKey] = adminPolicy
95+
intermediate.Policies[ReadersPolicyKey] = memberPolicy
96+
intermediate.Policies[WritersPolicyKey] = memberPolicy
4097
return result
4198
}

0 commit comments

Comments
 (0)