Skip to content

Commit

Permalink
[FAB-17510] Change AddOrgToConsortium() to modify the provided config
Browse files Browse the repository at this point in the history
Signed-off-by: Tiffany Harris <tiffany.harris@ibm.com>
  • Loading branch information
stephyee authored and sykesm committed Feb 28, 2020
1 parent 9cf1196 commit 8c1a290
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 223 deletions.
223 changes: 0 additions & 223 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,229 +581,6 @@ func TestCreateSignedConfigUpdateEnvelopeFailures(t *testing.T) {
}
}

func TestAddOrgToConsortium(t *testing.T) {
gt := NewGomegaWithT(t)

config := &cb.Config{
ChannelGroup: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
"Consortiums": {
Groups: map[string]*cb.ConfigGroup{
"test-consortium": {},
},
},
},
},
}

org := &Organization{
Name: "Org1",
ID: "Org1MSP",
Policies: applicationOrgStandardPolicies(),
}

configUpdate, err := AddOrgToConsortium(org, "test-consortium", "testchannel", config, &mb.MSPConfig{})
gt.Expect(err).NotTo(HaveOccurred())

expectedConfigUpdate := &cb.ConfigUpdate{
ChannelId: "testchannel",
ReadSet: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
"Consortiums": {
Groups: map[string]*cb.ConfigGroup{
"test-consortium": {},
},
},
},
},
WriteSet: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
"Consortiums": {
Groups: map[string]*cb.ConfigGroup{
"test-consortium": {
Version: 1,
Groups: map[string]*cb.ConfigGroup{
"Org1": {
Values: map[string]*cb.ConfigValue{
"MSP": {
ModPolicy: "Admins",
Value: marshalOrPanic(&mb.MSPConfig{}),
},
},
Policies: map[string]*cb.ConfigPolicy{
"Admins": {
ModPolicy: "Admins",
Policy: &cb.Policy{
Type: 3,
Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
Rule: cb.ImplicitMetaPolicy_Rule(cb.ImplicitMetaPolicy_MAJORITY),
SubPolicy: "Admins",
}),
},
},
"Endorsement": {
ModPolicy: "Admins",
Policy: &cb.Policy{
Type: 3,
Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
Rule: cb.ImplicitMetaPolicy_Rule(cb.ImplicitMetaPolicy_MAJORITY),
SubPolicy: "Endorsement",
}),
},
},
"LifecycleEndorsement": {
ModPolicy: "Admins",
Policy: &cb.Policy{
Type: 3,
Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
Rule: cb.ImplicitMetaPolicy_Rule(cb.ImplicitMetaPolicy_MAJORITY),
SubPolicy: "Endorsement",
}),
},
},
"Readers": {
ModPolicy: "Admins",
Policy: &cb.Policy{
Type: 3,
Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
Rule: cb.ImplicitMetaPolicy_Rule(cb.ImplicitMetaPolicy_ANY),
SubPolicy: "Readers",
}),
},
},
"Writers": {
ModPolicy: "Admins",
Policy: &cb.Policy{
Type: 3,
Value: marshalOrPanic(&cb.ImplicitMetaPolicy{
Rule: cb.ImplicitMetaPolicy_Rule(cb.ImplicitMetaPolicy_ANY),
SubPolicy: "Writers",
}),
},
},
},
ModPolicy: "Admins",
},
},
},
},
},
},
},
}
gt.Expect(proto.Equal(configUpdate, expectedConfigUpdate)).To(BeTrue())
}

// marshalOrPanic serializes a protobuf message and panics if this
// operation fails.
func marshalOrPanic(pb proto.Message) []byte {
data, err := proto.Marshal(pb)
if err != nil {
panic(err)
}
return data
}

func TestAddOrgToConsortiumFailures(t *testing.T) {
t.Parallel()

baseConfig := &cb.Config{
ChannelGroup: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
"Consortiums": {
Groups: map[string]*cb.ConfigGroup{
"test-consortium": {},
},
},
},
},
}

org := &Organization{
Name: "test-org",
ID: "test-org-msp-id",
Policies: applicationOrgStandardPolicies(),
}

for _, test := range []struct {
name string
org *Organization
consortium string
channelID string
config *cb.Config
expectedErr string
}{
{
name: "When the organization is nil",
org: nil,
consortium: "test-consortium",
channelID: "test-channel",
config: baseConfig,
expectedErr: "organization is empty",
},
{
name: "When the consortium name is not specified",
org: org,
consortium: "",
channelID: "test-channel",
config: baseConfig,
expectedErr: "consortium is empty",
},
{
name: "When the config doesn't contain a consortiums group",
org: org,
consortium: "test-consortium",
channelID: "test-channel",
config: &cb.Config{
ChannelGroup: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{},
},
},
expectedErr: "consortiums group does not exist",
},
{
name: "When the config doesn't contain the consortium",
org: org,
consortium: "what-the-what",
channelID: "test-channel",
config: baseConfig,
expectedErr: "consortium 'what-the-what' does not exist",
},
{
name: "When the config doesn't contain the consortium",
org: &Organization{
Name: "test-msp",
ID: "test-org-msp-id",
Policies: map[string]*Policy{
"Admins": nil,
},
},
consortium: "test-consortium",
channelID: "test-channel",
config: baseConfig,
expectedErr: "failed to create consortium org: no Admins policy defined",
},
{
name: "When the channel ID is not specified",
org: org,
consortium: "test-consortium",
channelID: "",
config: baseConfig,
expectedErr: "channel ID is required",
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)

configUpdate, err := AddOrgToConsortium(test.org, test.consortium, test.channelID, test.config, &mb.MSPConfig{})
gt.Expect(configUpdate).To(BeNil())
gt.Expect(err).To(MatchError(test.expectedErr))
})
}
}

func baseProfile() *Channel {
return &Channel{
ChannelID: "testchannel",
Expand Down
34 changes: 34 additions & 0 deletions pkg/config/consortiums.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package config

import (
"errors"
"fmt"

"github.com/golang/protobuf/proto"
Expand All @@ -21,6 +22,39 @@ type Consortium struct {
Organizations []*Organization
}

// AddOrgToConsortium adds an org definition to a named consortium in a given
// channel configuration.
func AddOrgToConsortium(config *cb.Config, org *Organization, consortium string, mspConfig *mb.MSPConfig) error {
if org == nil {
return errors.New("organization is required")
}
if consortium == "" {
return errors.New("consortium is required")
}

consortiumsGroup, ok := config.ChannelGroup.Groups[ConsortiumsGroupKey]
if !ok {
return errors.New("consortiums group does not exist")
}

consortiumGroup, ok := consortiumsGroup.Groups[consortium]
if !ok {
return fmt.Errorf("consortium '%s' does not exist", consortium)
}

orgGroup, err := newConsortiumOrgGroup(org, mspConfig)
if err != nil {
return fmt.Errorf("failed to create consortium org: %v", err)
}

if consortiumGroup.Groups == nil {
consortiumGroup.Groups = map[string]*cb.ConfigGroup{}
}
consortiumGroup.Groups[org.Name] = orgGroup

return 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".
Expand Down
Loading

0 comments on commit 8c1a290

Please sign in to comment.