Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 6607947

Browse files
committed
[FAB-8681] SaveChannel: Use multiple signing identities
Change-Id: Iffa5f39538e3666077d2dd156ff6cbfac8b743d6 Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
1 parent 9606a4c commit 6607947

File tree

7 files changed

+81
-42
lines changed

7 files changed

+81
-42
lines changed

pkg/client/resmgmt/resmgmt.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ type SaveChannelRequest struct {
8686
ChannelID string
8787
// Path to channel configuration file
8888
ChannelConfig string
89-
// User that signs channel configuration
90-
SigningIdentity context.Identity
89+
// Users that sign channel configuration
90+
SigningIdentities []context.Identity
9191
}
9292

9393
//RequestOption func for each Opts argument
@@ -611,13 +611,17 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
611611

612612
// Signing user has to belong to one of configured channel organisations
613613
// In case that order org is one of channel orgs we can use context user
614-
var signer context.Identity = rc.context
615-
if req.SigningIdentity != nil {
616-
// Retrieve custom signing identity here
617-
signer = req.SigningIdentity
618-
}
614+
var signers []context.Identity
619615

620-
if signer == nil {
616+
if len(req.SigningIdentities) > 0 {
617+
for _, id := range req.SigningIdentities {
618+
if id != nil {
619+
signers = append(signers, id)
620+
}
621+
}
622+
} else if rc.context != nil {
623+
signers = append(signers, rc.context)
624+
} else {
621625
return errors.New("must provide signing user")
622626
}
623627

@@ -631,18 +635,20 @@ func (rc *Client) SaveChannel(req SaveChannelRequest, options ...RequestOption)
631635
return errors.WithMessage(err, "extracting channel config failed")
632636
}
633637

634-
sigCtx := contextImpl.Client{
635-
Identity: signer,
636-
Providers: rc.context,
637-
}
638+
var configSignatures []*common.ConfigSignature
639+
for _, signer := range signers {
638640

639-
configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
640-
if err != nil {
641-
return errors.WithMessage(err, "signing configuration failed")
642-
}
641+
sigCtx := contextImpl.Client{
642+
Identity: signer,
643+
Providers: rc.context,
644+
}
643645

644-
var configSignatures []*common.ConfigSignature
645-
configSignatures = append(configSignatures, configSignature)
646+
configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
647+
if err != nil {
648+
return errors.WithMessage(err, "signing configuration failed")
649+
}
650+
configSignatures = append(configSignatures, configSignature)
651+
}
646652

647653
// Figure out orderer configuration
648654
var ordererCfg *core.OrdererConfig

pkg/client/resmgmt/resmgmt_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,3 +1295,22 @@ func TestSaveChannelWithOpts(t *testing.T) {
12951295
t.Fatal("Should have failed for invalid orderer ID")
12961296
}
12971297
}
1298+
1299+
func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
1300+
cc := setupDefaultResMgmtClient(t)
1301+
1302+
// empty list of signing identities (defaults to context user)
1303+
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []context.Identity{}}
1304+
err := cc.SaveChannel(req, WithOrdererID(""))
1305+
if err != nil {
1306+
t.Fatalf("Failed to save channel with default signing identity: %s", err)
1307+
}
1308+
1309+
// multiple signing identities
1310+
secondCtx := fcmocks.NewMockContext(fcmocks.NewMockUser("second"))
1311+
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []context.Identity{cc.context, secondCtx}}
1312+
err = cc.SaveChannel(req, WithOrdererID(""))
1313+
if err != nil {
1314+
t.Fatalf("Failed to save channel with multiple signing identities: %s", err)
1315+
}
1316+
}

test/integration/base_test_setup.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,17 @@ func (setup *BaseSetupImpl) Initialize() error {
107107
}
108108
setup.Targets = targets
109109

110+
// Get signing identity that is used to sign create channel request
111+
si, err := GetSigningIdentity(sdk, setup.OrgID, "Admin")
112+
if err != nil {
113+
return errors.Wrapf(err, "failed to load signing identity")
114+
}
115+
110116
// Create channel for tests
111-
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentity: session}
112-
InitializeChannel(sdk, setup.OrgID, req, targets)
117+
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentities: []context.Identity{si}}
118+
if err = InitializeChannel(sdk, setup.OrgID, req, targets); err != nil {
119+
return errors.Wrapf(err, "failed to initalize channel")
120+
}
113121

114122
// Create the channel transactor
115123
chService, err := client.ChannelService(setup.ChannelID)
@@ -278,3 +286,8 @@ func RegisterTxEvent(t *testing.T, txID fab.TransactionID, eventHub fab.EventHub
278286

279287
return done, fail
280288
}
289+
290+
// GetSigningIdentity returns signing identity
291+
func GetSigningIdentity(sdk *fabsdk.FabricSDK, orgID string, user string) (context.Identity, error) {
292+
return sdk.Context(fabsdk.WithUser(user), fabsdk.WithOrgName(orgID)), nil
293+
}

test/integration/e2e/end_to_end.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
1516
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
1617

1718
"github.com/hyperledger/fabric-sdk-go/test/integration"
@@ -53,15 +54,14 @@ func Run(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option)
5354
t.Fatalf("Failed to create channel management client: %s", err)
5455
}
5556

56-
// Org admin user is signing user for creating channel
57-
session, err := sdk.NewClient(fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(orgName)).Session()
57+
// Get signing identity that is used to sign create channel request
58+
si, err := integration.GetSigningIdentity(sdk, orgName, orgAdmin)
5859
if err != nil {
59-
t.Fatalf("Failed to get session for %s, %s: %s", orgName, orgAdmin, err)
60+
t.Fatalf("failed to load signing identity: %s", err)
6061
}
61-
orgAdminUser := session
6262

6363
// Create channel
64-
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"), SigningIdentity: orgAdminUser}
64+
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"), SigningIdentities: []context.Identity{si}}
6565
if err = chMgmtClient.SaveChannel(req); err != nil {
6666
t.Fatal(err)
6767
}

test/integration/fab/channel_ledger_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"testing"
1313

14+
"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
1415
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
1516
"github.com/hyperledger/fabric-sdk-go/test/integration"
1617
"github.com/hyperledger/fabric-sdk-go/test/metadata"
@@ -34,10 +35,10 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []fab.ProposalProce
3435
if err != nil {
3536
t.Fatalf("SDK init failed: %v", err)
3637
}
37-
38-
session, err := sdk.NewClient(fabsdk.WithUser("Admin"), fabsdk.WithOrg(orgName)).Session()
38+
// Get signing identity that is used to sign create channel request
39+
si, err := integration.GetSigningIdentity(sdk, orgName, "Admin")
3940
if err != nil {
40-
t.Fatalf("failed getting admin user session for org: %s", err)
41+
t.Fatalf("failed to load signing identity: %s", err)
4142
}
4243

4344
targets, err := integration.CreateProposalProcessors(sdk.Config(), []string{orgName})
@@ -46,7 +47,7 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []fab.ProposalProce
4647
}
4748

4849
channelConfig := path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile)
49-
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentity: session}
50+
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentities: []context.Identity{si}}
5051
err = integration.InitializeChannel(sdk, orgName, req, targets)
5152
if err != nil {
5253
t.Fatalf("failed to ensure channel has been initialized: %s", err)

test/integration/orgs/multiple_orgs_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
2020
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
2121
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
22-
"github.com/pkg/errors"
2322

2423
"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
2524
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
@@ -70,9 +69,19 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
7069
t.Fatal(err)
7170
}
7271

72+
// Get signing identity that is used to sign create channel request
73+
siOrg1, err := integration.GetSigningIdentity(sdk, org1, "Admin")
74+
if err != nil {
75+
t.Fatalf("failed to load signing identity: %s", err)
76+
}
77+
78+
siOrg2, err := integration.GetSigningIdentity(sdk, org2, "Admin")
79+
if err != nil {
80+
t.Fatalf("failed to load signing identity: %s", err)
81+
}
82+
7383
// Create channel (or update if it already exists)
74-
org1AdminUser := loadOrgUser(t, sdk, org1, "Admin")
75-
req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel", ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"), SigningIdentity: org1AdminUser}
84+
req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel", ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"), SigningIdentities: []context.Identity{siOrg1, siOrg2}}
7685
if err = chMgmtClient.SaveChannel(req); err != nil {
7786
t.Fatal(err)
7887
}
@@ -351,15 +360,6 @@ func verifyValue(t *testing.T, chClient *channel.Client, expected int) {
351360

352361
}
353362

354-
func loadOrgUser(t *testing.T, sdk *fabsdk.FabricSDK, orgName string, userName string) context.Identity {
355-
356-
session, err := sdk.NewClient(fabsdk.WithUser(userName), fabsdk.WithOrg(orgName)).Session()
357-
if err != nil {
358-
t.Fatal(errors.Wrapf(err, "Session failed, %s, %s", orgName, userName))
359-
}
360-
return session
361-
}
362-
363363
func loadOrgPeers(t *testing.T, sdk *fabsdk.FabricSDK) {
364364

365365
org1Peers, err := sdk.Config().PeersConfig(org1)

test/integration/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func CreateChannel(sdk *fabsdk.FabricSDK, req resmgmt.SaveChannelRequest) (bool,
8686

8787
// Create channel (or update if it already exists)
8888
if err = resMgmtClient.SaveChannel(req); err != nil {
89-
return false, nil
89+
return false, err
9090
}
9191

9292
time.Sleep(time.Second * 5)

0 commit comments

Comments
 (0)