Skip to content

Commit

Permalink
[FAB-4865] NPE occurs on LoadIdentity
Browse files Browse the repository at this point in the history
Making a call to public function LoadIdentity
results in an NPE if client object has not been initialized.
The following error is seen:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x44950a7]

Need to make sure that the initializing is performed as the
first step inside the LoadIdentity function.

Also, added some debug lines and reformatted an error

Change-Id: I244452fa2871d8a0263df437cf872ba340da45f7
Signed-off-by: Saad Karim <[email protected]>
  • Loading branch information
Saad Karim committed Jun 19, 2017
1 parent 82fad13 commit 4f4264d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Client struct {
func (c *Client) Init() error {
if !c.initialized {
cfg := c.Config
log.Debugf("Initializing client with config: %+v", cfg)
if cfg.MSPDir == "" {
cfg.MSPDir = "msp"
}
Expand Down Expand Up @@ -163,8 +164,7 @@ func (c *Client) Enroll(req *api.EnrollmentRequest) (*EnrollmentResponse, error)
// Generate the CSR
csrPEM, key, err := c.GenCSR(req.CSR, req.Name)
if err != nil {
log.Debugf("Enroll failure generating CSR: %s", err)
return nil, err
return nil, fmt.Errorf("Failure generating CSR: %s", err)
}

reqNet := &api.EnrollmentRequestNet{
Expand Down Expand Up @@ -302,6 +302,11 @@ func (c *Client) StoreMyIdentity(cert []byte) error {

// LoadIdentity loads an identity from disk
func (c *Client) LoadIdentity(keyFile, certFile string) (*Identity, error) {
log.Debug("Loading identity: keyFile=%s, certFile=%s", keyFile, certFile)
err := c.Init()
if err != nil {
return nil, err
}
cert, err := util.ReadFile(certFile)
if err != nil {
log.Debugf("No cert found at %s", certFile)
Expand Down
17 changes: 17 additions & 0 deletions lib/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestClient(t *testing.T) {

c := getTestClient(ctport1)

testLoadIdentity(c, t)
testGetCAInfo(c, t)
testRegister(c, t)
testEnrollIncorrectPassword(c, t)
Expand Down Expand Up @@ -272,6 +273,21 @@ func testLoadBadCSRInfo(c *Client, t *testing.T) {
}
}

func testLoadIdentity(c *Client, t *testing.T) {
_, err := c.LoadIdentity("foo", "bar")
if err == nil {
t.Error("testLoadIdentity foo/bar passed but should have failed")
}
_, err = c.LoadIdentity("foo", "../testdata/ec.pem")
if err == nil {
t.Error("testLoadIdentity foo passed but should have failed")
}
_, err = c.LoadIdentity("../testdata/ec-key.pem", "../testdata/ec.pem")
if err != nil {
t.Errorf("testLoadIdentity failed: %s", err)
}
}

func TestCustomizableMaxEnroll(t *testing.T) {
os.Remove("../testdata/fabric-ca-server.db")

Expand Down Expand Up @@ -384,4 +400,5 @@ func TestLast(t *testing.T) {
os.RemoveAll(serversDir)
os.RemoveAll("multica")
os.RemoveAll("rootDir")
os.RemoveAll("msp")
}

0 comments on commit 4f4264d

Please sign in to comment.