Skip to content

Commit 8eb04e7

Browse files
MayRosenbaumtock-ibm
authored andcommitted
build policy manager
Signed-off-by: May.Buzaglo <May.Buzaglo@ibm.com>
1 parent 28749f7 commit 8eb04e7

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

common/policy/policy.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package policy
8+
9+
import (
10+
"github.com/hyperledger/fabric-lib-go/bccsp"
11+
cb "github.com/hyperledger/fabric-protos-go-apiv2/common"
12+
"github.com/hyperledger/fabric-x-common/common/channelconfig"
13+
"github.com/hyperledger/fabric-x-common/common/configtx"
14+
"github.com/hyperledger/fabric-x-common/common/policies"
15+
"github.com/hyperledger/fabric-x-common/protoutil"
16+
"github.com/pkg/errors"
17+
)
18+
19+
// BuildPolicyManagerFromBlock builds a new policy manager from block.
20+
func BuildPolicyManagerFromBlock(configTX *cb.Envelope, bccsp bccsp.BCCSP) (policies.Manager, error) {
21+
payload, err := protoutil.UnmarshalPayload(configTX.Payload)
22+
if err != nil {
23+
return nil, errors.WithMessage(err, "error unmarshaling envelope to payload")
24+
}
25+
26+
if payload.Header == nil {
27+
return nil, errors.New("missing channel header")
28+
}
29+
30+
chdr, err := protoutil.UnmarshalChannelHeader(payload.Header.ChannelHeader)
31+
if err != nil {
32+
return nil, errors.WithMessage(err, "error unmarshalling channel header")
33+
}
34+
35+
configEnvelope, err := configtx.UnmarshalConfigEnvelope(payload.Data)
36+
if err != nil {
37+
return nil, errors.WithMessage(err, "error umarshaling config envelope from payload data")
38+
}
39+
40+
bundle, err := channelconfig.NewBundle(chdr.ChannelId, configEnvelope.Config, bccsp)
41+
if err != nil {
42+
return nil, errors.WithMessage(err, "error creating channelconfig bundle")
43+
}
44+
45+
err = checkResources(bundle)
46+
if err != nil {
47+
return nil, errors.WithMessagef(err, "error checking bundle for channel: %s", chdr.ChannelId)
48+
}
49+
50+
policyManager := bundle.PolicyManager()
51+
return policyManager, nil
52+
}
53+
54+
// checkResources makes sure that the channel config is compatible with this binary and logs sanity checks
55+
func checkResources(res channelconfig.Resources) error {
56+
channelconfig.LogSanityChecks(res)
57+
oc, ok := res.OrdererConfig()
58+
if !ok {
59+
return errors.New("config does not contain orderer config")
60+
}
61+
if err := oc.Capabilities().Supported(); err != nil {
62+
return errors.WithMessagef(err, "config requires unsupported orderer capabilities: %s", err)
63+
}
64+
if err := res.ChannelConfig().Capabilities().Supported(); err != nil {
65+
return errors.WithMessagef(err, "config requires unsupported channel capabilities: %s", err)
66+
}
67+
return nil
68+
}

common/policy/policy_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package policy
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger/fabric-lib-go/bccsp/factory"
13+
"github.com/hyperledger/fabric-x-common/common/policies"
14+
"github.com/hyperledger/fabric-x-common/protoutil"
15+
"github.com/hyperledger/fabric-x-orderer/config/generate"
16+
"github.com/hyperledger/fabric-x-orderer/testutil"
17+
"github.com/hyperledger/fabric-x-orderer/testutil/fabric"
18+
"github.com/stretchr/testify/require"
19+
)
20+
21+
func TestBuildPolicyManagerFromBlock(t *testing.T) {
22+
dir := t.TempDir()
23+
24+
sharedConfigYaml, sharedConfigPath := testutil.PrepareSharedConfigBinary(t, dir)
25+
26+
block, err := generate.CreateGenesisBlock(dir, sharedConfigYaml, sharedConfigPath, fabric.GetDevConfigDir())
27+
require.NoError(t, err)
28+
require.NotNil(t, block)
29+
30+
require.True(t, block.Data != nil)
31+
require.NotEqual(t, len(block.Data.Data), 0)
32+
33+
env, err := protoutil.GetEnvelopeFromBlock(block.Data.Data[0])
34+
require.NoError(t, err)
35+
require.NotNil(t, env)
36+
37+
policyManager, err := BuildPolicyManagerFromBlock(env, factory.GetDefault())
38+
require.NoError(t, err)
39+
require.NotNil(t, policyManager)
40+
41+
policy, exists := policyManager.GetPolicy(policies.ChannelWriters)
42+
require.True(t, exists)
43+
require.NotNil(t, policy)
44+
45+
policy, exists = policyManager.GetPolicy(policies.ChannelReaders)
46+
require.True(t, exists)
47+
require.NotNil(t, policy)
48+
49+
policy, exists = policyManager.GetPolicy(policies.ChannelOrdererAdmins)
50+
require.True(t, exists)
51+
require.NotNil(t, policy)
52+
}

0 commit comments

Comments
 (0)