Skip to content

Commit

Permalink
[FABG-871] Generate the genesis block MSP directory
Browse files Browse the repository at this point in the history
The genesis block profile contains only a reference to the MSP
directory, which has to be generated before the genesis block
can be created.

Change-Id: Ie2b620a419724d8d6caeae8223718a5ef3f3b027
Signed-off-by: Aleksandar Likic <[email protected]>
  • Loading branch information
Aleksandar Likic committed Jun 4, 2019
1 parent d42d52c commit 33876cd
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
86 changes: 86 additions & 0 deletions pkg/fab/resource/configtxmsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package resource

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/gogo/protobuf/proto"

mspcfg "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
)

const (
cacerts = "cacerts"
admincerts = "admincerts"
signcerts = "signcerts"
keystore = "keystore"
intermediatecerts = "intermediatecerts"
crlsfolder = "crls"
configfilename = "config.yaml"
tlscacerts = "tlscacerts"
tlsintermediatecerts = "tlsintermediatecerts"
)

// GenerateMspDir generates a MSP directory, using values from the provided MSP config.
// The intended usage is within the scope of creating a genesis block. This means
// private keys are currently not handled.
func GenerateMspDir(mspDir string, config *msp.MSPConfig) error {

if mspcfg.ProviderTypeToString(mspcfg.ProviderType(config.Type)) != "bccsp" {
return fmt.Errorf("Unsupported MSP config type")
}

cfg := &msp.FabricMSPConfig{}
err := proto.Unmarshal(config.Config, cfg)
if err != nil {
return err
}

type certDirDefinition struct {
dir string
certs [][]byte
}
defs := []certDirDefinition{
{cacerts, cfg.RootCerts},
{admincerts, cfg.Admins},
{intermediatecerts, cfg.IntermediateCerts},
{tlscacerts, cfg.TlsRootCerts},
{tlsintermediatecerts, cfg.TlsIntermediateCerts},
{crlsfolder, cfg.RevocationList},
}
for _, d := range defs {
err := generateCertDir(filepath.Join(mspDir, d.dir), d.certs)
if err != nil {
return err
}
}

return err
}

func generateCertDir(certDir string, certs [][]byte) error {
err := os.MkdirAll(certDir, 0755)
if err != nil {
return err
}
if len(certs) == 0 {
return nil
}
for counter, certBytes := range certs {
fileName := filepath.Join(certDir, "cert"+fmt.Sprintf("%d", counter)+".pem")
err = ioutil.WriteFile(fileName, certBytes, 0644)
if err != nil {
return err
}
}
return nil
}
56 changes: 56 additions & 0 deletions pkg/fab/resource/configtxmsp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package resource

import (
"fmt"
"math/rand"
"path/filepath"
"testing"
"time"

"github.com/gogo/protobuf/proto"

"github.com/hyperledger/fabric-sdk-go/test/metadata"

mspcfg "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
"github.com/stretchr/testify/require"
)

func randomMspDir() string {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
return "/tmp/msp/" + fmt.Sprintf("%d", rnd.Uint64())
}

func TestGenerateMspDir(t *testing.T) {

ordererMspDir := filepath.Join(metadata.GetProjectPath(), "test/fixtures/fabric/v1/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp")
cfg, err := mspcfg.GetVerifyingMspConfig(ordererMspDir, "mymspid", "bccsp")
require.NoError(t, err, "Error generating msp config from dir")
mspConfig := &msp.FabricMSPConfig{}
err = proto.Unmarshal(cfg.Config, mspConfig)
require.NoError(t, err, "Error unmarshaling msp config")

dir := randomMspDir()

err = GenerateMspDir(dir, cfg)
require.NoError(t, err, "Error generating msp dir")

cfg1, err := mspcfg.GetVerifyingMspConfig(dir, "mymspid", "bccsp")
require.NoError(t, err, "Error generating msp config from dir1")
mspConfig1 := &msp.FabricMSPConfig{}
err = proto.Unmarshal(cfg1.Config, mspConfig1)
require.NoError(t, err, "Error unmarshaling msp config1")

require.Equal(t, mspConfig.RootCerts, mspConfig1.RootCerts, "RootCerts are different")
require.Equal(t, mspConfig.RevocationList, mspConfig1.RevocationList, "RevocationList are different")
require.Equal(t, mspConfig.TlsIntermediateCerts, mspConfig1.TlsIntermediateCerts, "TlsIntermediateCerts are different")
require.Equal(t, mspConfig.TlsRootCerts, mspConfig1.TlsRootCerts, "TlsRootCerts are different")
require.Equal(t, mspConfig.IntermediateCerts, mspConfig1.IntermediateCerts, "IntermediateCerts are different")
require.Equal(t, mspConfig.Admins, mspConfig1.Admins, "Admins are different")
}

0 comments on commit 33876cd

Please sign in to comment.