Skip to content

Commit

Permalink
Merge "[FAB-4015] Fix -M option of fabric-ca-client"
Browse files Browse the repository at this point in the history
  • Loading branch information
binhn authored and Gerrit Code Review committed May 23, 2017
2 parents 8206d83 + 2560ffd commit 4aead96
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
64 changes: 64 additions & 0 deletions cmd/fabric-ca-client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
rootCertEnvVar = "FABRIC_CA_CLIENT_TLS_CERTFILES"
clientKeyEnvVar = "FABRIC_CA_CLIENT_TLS_CLIENT_KEYFILE"
clientCertEnvVar = "FABRIC_CA_CLIENT_TLS_CLIENT_CERTFILE"
moptionDir = "moption-test"
)

const jsonConfig = `{
Expand Down Expand Up @@ -303,6 +304,34 @@ func testEnroll(t *testing.T) {
os.Remove(defYaml)
}

// TestMOption tests to make sure that the key is stored in the correct
// directory when the "-M" option is used.
func TestMOption(t *testing.T) {
os.RemoveAll(moptionDir)
port := 7173
s := startServer(path.Join(moptionDir, "server"), port, t)
if s == nil {
return
}
homedir := path.Join(moptionDir, "client")
mspdir := "msp2" // relative to homedir
err := RunMain([]string{
cmdName, "enroll",
"-u", fmt.Sprintf("http://admin:adminpw@localhost:%d", port),
"-c", path.Join(homedir, "config.yaml"),
"-M", mspdir, "-d"})
if err != nil {
t.Fatalf("client enroll -u failed: %s", err)
}
keystore := path.Join(homedir, mspdir, "keystore")
count := getNumFiles(keystore, t)
if count != 1 {
t.Fatalf("client enroll -M failed: expecting 1 file in keystore %s but found %d",
keystore, count)
}
s.Stop()
}

// TestReenroll tests fabric-ca-client reenroll
func testReenroll(t *testing.T) {
t.Log("Testing Reenroll CMD")
Expand Down Expand Up @@ -718,6 +747,7 @@ func TestCleanUp(t *testing.T) {
os.Remove(testYaml)
os.Remove(fabricCADB)
os.RemoveAll(mspDir)
os.RemoveAll(moptionDir)
cleanMultiCADir()
}

Expand Down Expand Up @@ -811,3 +841,37 @@ func extraArgErrorTest(in *TestData, t *testing.T) {
}
}
}

// get the number of files in a directory
func getNumFiles(dir string, t *testing.T) int {
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatalf("Failed to get number of files in directory '%s': %s", dir, err)
}
return len(files)
}

func startServer(home string, port int, t *testing.T) *lib.Server {
affiliations := map[string]interface{}{"org1": nil}
srv := &lib.Server{
HomeDir: home,
Config: &lib.ServerConfig{
Debug: true,
Port: port,
},
CA: lib.CA{
Config: &lib.CAConfig{
Affiliations: affiliations,
},
},
}
err := srv.RegisterBootstrapUser("admin", "adminpw", "")
if err != nil {
t.Fatalf("Failed to register bootstrap user: %s", err)
}
err = srv.Start()
if err != nil {
t.Fatalf("Failed to start server: %s", err)
}
return srv
}
2 changes: 1 addition & 1 deletion lib/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (ca *CA) init(renew bool) (err error) {
return err
}
// Initialize the crypto layer (BCCSP) for this CA
ca.csp, err = util.InitBCCSP(&ca.Config.CSP, ca.HomeDir)
ca.csp, err = util.InitBCCSP(&ca.Config.CSP, "", ca.HomeDir)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *Client) Init() error {
return fmt.Errorf("Failed to create cacerts directory: %s", err)
}
// Initialize BCCSP (the crypto layer)
c.csp, err = util.InitBCCSP(&cfg.CSP, c.HomeDir)
c.csp, err = util.InitBCCSP(&cfg.CSP, mspDir, c.HomeDir)
if err != nil {
return err
}
Expand Down
19 changes: 12 additions & 7 deletions util/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func GetDefaultBCCSP() bccsp.BCCSP {
}

// InitBCCSP initializes BCCSP
func InitBCCSP(optsPtr **factory.FactoryOpts, homeDir string) (bccsp.BCCSP, error) {
err := ConfigureBCCSP(optsPtr)
func InitBCCSP(optsPtr **factory.FactoryOpts, mspDir, homeDir string) (bccsp.BCCSP, error) {
err := ConfigureBCCSP(optsPtr, mspDir)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +61,7 @@ func InitBCCSP(optsPtr **factory.FactoryOpts, homeDir string) (bccsp.BCCSP, erro
}

// ConfigureBCCSP configures BCCSP, using
func ConfigureBCCSP(optsPtr **factory.FactoryOpts) error {
func ConfigureBCCSP(optsPtr **factory.FactoryOpts, mspDir string) error {
var err error
if optsPtr == nil {
return errors.New("nil argument not allowed")
Expand All @@ -83,10 +83,14 @@ func ConfigureBCCSP(optsPtr **factory.FactoryOpts) error {
if opts.SwOpts.SecLevel == 0 {
opts.SwOpts.SecLevel = 256
}
// Only override the KeyStorePath if it was left empty
if opts.SwOpts.FileKeystore == nil ||
opts.SwOpts.FileKeystore.KeyStorePath == "" {
opts.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: path.Join("msp", "keystore")}
if opts.SwOpts.FileKeystore == nil {
opts.SwOpts.FileKeystore = &factory.FileKeystoreOpts{}
}
// The mspDir overrides the KeyStorePath; otherwise, if not set, set default
if mspDir != "" {
opts.SwOpts.FileKeystore.KeyStorePath = path.Join(mspDir, "keystore")
} else if opts.SwOpts.FileKeystore.KeyStorePath == "" {
opts.SwOpts.FileKeystore.KeyStorePath = path.Join("msp", "keystore")
}
}
log.Debugf("Initializing BCCSP: %+v", opts)
Expand All @@ -107,6 +111,7 @@ func ConfigureBCCSP(optsPtr **factory.FactoryOpts) error {

// GetBCCSP returns BCCSP
func GetBCCSP(opts *factory.FactoryOpts, homeDir string) (bccsp.BCCSP, error) {

err := makeFileNamesAbsolute(opts, homeDir)
if err != nil {
return nil, fmt.Errorf("Failed to make BCCSP files absolute: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions util/csp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ func TestGetDefaultBCCSP(t *testing.T) {
func TestInitBCCSP(t *testing.T) {
mspDir := "msp"
var opts *factory.FactoryOpts
_, err := InitBCCSP(&opts, mspDir)
_, err := InitBCCSP(&opts, "", mspDir)
if err != nil {
t.Fatalf("Failed initialization 1 of BCCSP: %s", err)
}
cfg := &factory.FactoryOpts{ProviderName: "SW"}
_, err = InitBCCSP(&cfg, mspDir)
_, err = InitBCCSP(&cfg, "msp2", mspDir)
if err != nil {
t.Fatalf("Failed initialization 2 of BCCSP: %s", err)
}
_, err = InitBCCSP(nil, mspDir)
_, err = InitBCCSP(nil, "", mspDir)
if err == nil {
t.Fatalf("Initialization 3 of BCCSP should have failed but did not")
}
Expand Down

0 comments on commit 4aead96

Please sign in to comment.