-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FABG-871] Generate the genesis block MSP directory
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
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |