Skip to content

Commit

Permalink
Merge pull request #216 from adityajoshi12/fix-signupdate
Browse files Browse the repository at this point in the history
(kubectl-hlf): fixed the dependency of identity file while signing update
  • Loading branch information
adityajoshi12 authored Apr 20, 2024
2 parents f0f56f1 + 72e6350 commit 66a688b
Showing 1 changed file with 61 additions and 44 deletions.
105 changes: 61 additions & 44 deletions kubectl-hlf/cmd/channel/signupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package channel
import (
"bytes"
"fmt"
mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"os"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
Expand All @@ -18,7 +20,6 @@ import (
"gopkg.in/yaml.v3"

"io"
"io/ioutil"
)

type signUpdateChannelCmd struct {
Expand Down Expand Up @@ -58,55 +59,71 @@ func (c *signUpdateChannelCmd) run(out io.Writer) error {
if err != nil {
return err
}
updateEnvelopeBytes, err := ioutil.ReadFile(c.file)
updateEnvelopeBytes, err := os.ReadFile(c.file)
if err != nil {
return err
}
configUpdateReader := bytes.NewReader(updateEnvelopeBytes)
sdkConfig, err := sdk.Config()
if err != nil {
return err
}
cryptoConfig := cryptosuite.ConfigFromBackend(sdkConfig)
cryptoSuite, err := sw.GetSuiteByConfig(cryptoConfig)
if err != nil {
return err
}
userStore := mspimpl.NewMemoryUserStore()
endpointConfig, err := fab.ConfigFromBackend(sdkConfig)
if err != nil {
return err
}
identityManager, err := mspimpl.NewIdentityManager(c.mspID, userStore, cryptoSuite, endpointConfig)
if err != nil {
return err
}
identityBytes, err := ioutil.ReadFile(c.identity)
if err != nil {
return err
}
id := &identity{}
err = yaml.Unmarshal(identityBytes, id)
if err != nil {
return err
}
signingIdentity, err := identityManager.CreateSigningIdentity(
msp.WithPrivateKey([]byte(id.Key.Pem)),
msp.WithCert([]byte(id.Cert.Pem)),
)
if err != nil {
return err
}
signature, err := resClient.CreateConfigSignatureFromReader(signingIdentity, configUpdateReader)
if err != nil {
return err
}
signatureBytes, err := proto.Marshal(signature)
if err != nil {
return err

var signatureBytes []byte

// use identity file if provided
if c.identity != "" {
sdkConfig, err := sdk.Config()
if err != nil {
return err
}

cryptoConfig := cryptosuite.ConfigFromBackend(sdkConfig)
cryptoSuite, err := sw.GetSuiteByConfig(cryptoConfig)
if err != nil {
return err
}
userStore := mspimpl.NewMemoryUserStore()
endpointConfig, err := fab.ConfigFromBackend(sdkConfig)
if err != nil {
return err
}
identityManager, err := mspimpl.NewIdentityManager(c.mspID, userStore, cryptoSuite, endpointConfig)
if err != nil {
return err
}
identityBytes, err := os.ReadFile(c.identity)
if err != nil {
return err
}
id := &identity{}
err = yaml.Unmarshal(identityBytes, id)
if err != nil {
return err
}
signingIdentity, err := identityManager.CreateSigningIdentity(
msp.WithPrivateKey([]byte(id.Key.Pem)),
msp.WithCert([]byte(id.Cert.Pem)),
)
if err != nil {
return err
}
signature, err := resClient.CreateConfigSignatureFromReader(signingIdentity, configUpdateReader)
if err != nil {
return err
}
signatureBytes, err = proto.Marshal(signature)
if err != nil {
return err
}
} else {
mspClient, err := mspclient.New(org1AdminClientContext, mspclient.WithOrg(c.mspID))
if err != nil {
return err
}
usr, err := mspClient.GetSigningIdentity(c.userName)
signature, err := resClient.CreateConfigSignatureFromReader(usr, configUpdateReader)
signatureBytes, err = proto.Marshal(signature)
}

if c.output != "" {
err = ioutil.WriteFile(c.output, signatureBytes, 0644)
err = os.WriteFile(c.output, signatureBytes, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit 66a688b

Please sign in to comment.