Skip to content

Commit

Permalink
[FAB-8681] SaveChannel: Use multiple signing identities
Browse files Browse the repository at this point in the history
Change-Id: Iffa5f39538e3666077d2dd156ff6cbfac8b743d6
Signed-off-by: Sandra Vrtikapa <sandra.vrtikapa@securekey.com>
  • Loading branch information
sandrask committed Mar 6, 2018
1 parent 9606a4c commit 6607947
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 42 deletions.
42 changes: 24 additions & 18 deletions pkg/client/resmgmt/resmgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ type SaveChannelRequest struct {
ChannelID string
// Path to channel configuration file
ChannelConfig string
// User that signs channel configuration
SigningIdentity context.Identity
// Users that sign channel configuration
SigningIdentities []context.Identity
}

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

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

if signer == nil {
if len(req.SigningIdentities) > 0 {
for _, id := range req.SigningIdentities {
if id != nil {
signers = append(signers, id)
}
}
} else if rc.context != nil {
signers = append(signers, rc.context)
} else {
return errors.New("must provide signing user")
}

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

sigCtx := contextImpl.Client{
Identity: signer,
Providers: rc.context,
}
var configSignatures []*common.ConfigSignature
for _, signer := range signers {

configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
if err != nil {
return errors.WithMessage(err, "signing configuration failed")
}
sigCtx := contextImpl.Client{
Identity: signer,
Providers: rc.context,
}

var configSignatures []*common.ConfigSignature
configSignatures = append(configSignatures, configSignature)
configSignature, err := resource.CreateConfigSignature(&sigCtx, chConfig)
if err != nil {
return errors.WithMessage(err, "signing configuration failed")
}
configSignatures = append(configSignatures, configSignature)
}

// Figure out orderer configuration
var ordererCfg *core.OrdererConfig
Expand Down
19 changes: 19 additions & 0 deletions pkg/client/resmgmt/resmgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,3 +1295,22 @@ func TestSaveChannelWithOpts(t *testing.T) {
t.Fatal("Should have failed for invalid orderer ID")
}
}

func TestSaveChannelWithMultipleSigningIdenities(t *testing.T) {
cc := setupDefaultResMgmtClient(t)

// empty list of signing identities (defaults to context user)
req := SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []context.Identity{}}
err := cc.SaveChannel(req, WithOrdererID(""))
if err != nil {
t.Fatalf("Failed to save channel with default signing identity: %s", err)
}

// multiple signing identities
secondCtx := fcmocks.NewMockContext(fcmocks.NewMockUser("second"))
req = SaveChannelRequest{ChannelID: "mychannel", ChannelConfig: channelConfig, SigningIdentities: []context.Identity{cc.context, secondCtx}}
err = cc.SaveChannel(req, WithOrdererID(""))
if err != nil {
t.Fatalf("Failed to save channel with multiple signing identities: %s", err)
}
}
17 changes: 15 additions & 2 deletions test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ func (setup *BaseSetupImpl) Initialize() error {
}
setup.Targets = targets

// Get signing identity that is used to sign create channel request
si, err := GetSigningIdentity(sdk, setup.OrgID, "Admin")
if err != nil {
return errors.Wrapf(err, "failed to load signing identity")
}

// Create channel for tests
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentity: session}
InitializeChannel(sdk, setup.OrgID, req, targets)
req := resmgmt.SaveChannelRequest{ChannelID: setup.ChannelID, ChannelConfig: setup.ChannelConfig, SigningIdentities: []context.Identity{si}}
if err = InitializeChannel(sdk, setup.OrgID, req, targets); err != nil {
return errors.Wrapf(err, "failed to initalize channel")
}

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

return done, fail
}

// GetSigningIdentity returns signing identity
func GetSigningIdentity(sdk *fabsdk.FabricSDK, orgID string, user string) (context.Identity, error) {
return sdk.Context(fabsdk.WithUser(user), fabsdk.WithOrgName(orgID)), nil
}
10 changes: 5 additions & 5 deletions test/integration/e2e/end_to_end.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"

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

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

// Create channel
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"), SigningIdentity: orgAdminUser}
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "mychannel.tx"), SigningIdentities: []context.Identity{si}}
if err = chMgmtClient.SaveChannel(req); err != nil {
t.Fatal(err)
}
Expand Down
9 changes: 5 additions & 4 deletions test/integration/fab/channel_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/common/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/test/integration"
"github.com/hyperledger/fabric-sdk-go/test/metadata"
Expand All @@ -34,10 +35,10 @@ func initializeLedgerTests(t *testing.T) (*fabsdk.FabricSDK, []fab.ProposalProce
if err != nil {
t.Fatalf("SDK init failed: %v", err)
}

session, err := sdk.NewClient(fabsdk.WithUser("Admin"), fabsdk.WithOrg(orgName)).Session()
// Get signing identity that is used to sign create channel request
si, err := integration.GetSigningIdentity(sdk, orgName, "Admin")
if err != nil {
t.Fatalf("failed getting admin user session for org: %s", err)
t.Fatalf("failed to load signing identity: %s", err)
}

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

channelConfig := path.Join("../../../", metadata.ChannelConfigPath, channelConfigFile)
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentity: session}
req := resmgmt.SaveChannelRequest{ChannelID: channelID, ChannelConfig: channelConfig, SigningIdentities: []context.Identity{si}}
err = integration.InitializeChannel(sdk, orgName, req, targets)
if err != nil {
t.Fatalf("failed to ensure channel has been initialized: %s", err)
Expand Down
24 changes: 12 additions & 12 deletions test/integration/orgs/multiple_orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/pkg/errors"

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

// Get signing identity that is used to sign create channel request
siOrg1, err := integration.GetSigningIdentity(sdk, org1, "Admin")
if err != nil {
t.Fatalf("failed to load signing identity: %s", err)
}

siOrg2, err := integration.GetSigningIdentity(sdk, org2, "Admin")
if err != nil {
t.Fatalf("failed to load signing identity: %s", err)
}

// Create channel (or update if it already exists)
org1AdminUser := loadOrgUser(t, sdk, org1, "Admin")
req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel", ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"), SigningIdentity: org1AdminUser}
req := resmgmt.SaveChannelRequest{ChannelID: "orgchannel", ChannelConfig: path.Join("../../../", metadata.ChannelConfigPath, "orgchannel.tx"), SigningIdentities: []context.Identity{siOrg1, siOrg2}}
if err = chMgmtClient.SaveChannel(req); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -351,15 +360,6 @@ func verifyValue(t *testing.T, chClient *channel.Client, expected int) {

}

func loadOrgUser(t *testing.T, sdk *fabsdk.FabricSDK, orgName string, userName string) context.Identity {

session, err := sdk.NewClient(fabsdk.WithUser(userName), fabsdk.WithOrg(orgName)).Session()
if err != nil {
t.Fatal(errors.Wrapf(err, "Session failed, %s, %s", orgName, userName))
}
return session
}

func loadOrgPeers(t *testing.T, sdk *fabsdk.FabricSDK) {

org1Peers, err := sdk.Config().PeersConfig(org1)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func CreateChannel(sdk *fabsdk.FabricSDK, req resmgmt.SaveChannelRequest) (bool,

// Create channel (or update if it already exists)
if err = resMgmtClient.SaveChannel(req); err != nil {
return false, nil
return false, err
}

time.Sleep(time.Second * 5)
Expand Down

0 comments on commit 6607947

Please sign in to comment.