-
Notifications
You must be signed in to change notification settings - Fork 0
/
configtxmsp.go
83 lines (71 loc) · 1.94 KB
/
configtxmsp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package resource
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
mspcfg "github.com/huijinchain/fabric-sdk-go/internal/github.com/hyperledger/fabric/msp"
)
const (
cacerts = "cacerts"
admincerts = "admincerts"
intermediatecerts = "intermediatecerts"
crlsfolder = "crls"
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 {
errGen := generateCertDir(filepath.Join(mspDir, d.dir), d.certs)
if errGen != nil {
return errGen
}
}
return err
}
func generateCertDir(certDir string, certs [][]byte) error {
err := os.MkdirAll(certDir, 0750)
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, 0640)
if err != nil {
return err
}
}
return nil
}