From 73df8f6683e6828a39899c79b38310ab8c3ceab3 Mon Sep 17 00:00:00 2001 From: Baha Shaaban Date: Tue, 4 Jul 2017 12:20:35 -0400 Subject: [PATCH] [FAB-5170] Refactor User and improve test coverage Change-Id: Ia919c343562cb397fd718c0bfee9d7a4af107109 Signed-off-by: Baha Shaaban --- api/user.go | 10 ++-- pkg/fabric-ca-client/fabricca.go | 10 ++-- pkg/fabric-ca-client/mocks/mockuser.go | 26 ++++----- pkg/fabric-client/channel/channel.go | 8 +-- pkg/fabric-client/client.go | 27 ++++----- pkg/fabric-client/client_test.go | 4 +- pkg/fabric-client/events/consumer/consumer.go | 2 +- pkg/fabric-client/mocks/mockuser.go | 26 ++++----- pkg/fabric-client/user/user.go | 26 ++++----- pkg/fabric-client/user/user_test.go | 56 ++++++++++++++++++- 10 files changed, 121 insertions(+), 74 deletions(-) diff --git a/api/user.go b/api/user.go index 95cfae8468..6779bff9e5 100644 --- a/api/user.go +++ b/api/user.go @@ -25,17 +25,17 @@ import ( // An application cannot use the Peer identity to sign things because the application doesn’t // have access to the Peer identity’s private key. type User interface { - GetName() string - GetRoles() []string + Name() string + Roles() []string SetRoles([]string) SetMspID(mspID string) - GetMspID() string + MspID() string // ECerts - GetEnrollmentCertificate() []byte + EnrollmentCertificate() []byte SetEnrollmentCertificate(cert []byte) SetPrivateKey(privateKey bccsp.Key) - GetPrivateKey() bccsp.Key + PrivateKey() bccsp.Key // TCerts GenerateTcerts(count int, attributes []string) diff --git a/pkg/fabric-ca-client/fabricca.go b/pkg/fabric-ca-client/fabricca.go index 7632297b3e..5d756362cc 100644 --- a/pkg/fabric-ca-client/fabricca.go +++ b/pkg/fabric-ca-client/fabricca.go @@ -122,7 +122,7 @@ func (fabricCAServices *fabricCA) Reenroll(user sdkApi.User) (bccsp.Key, []byte, if user == nil { return nil, nil, fmt.Errorf("User does not exist") } - if user.GetName() == "" { + if user.Name() == "" { logger.Infof("Invalid re-enroll request, missing argument user") return nil, nil, fmt.Errorf("User is empty") } @@ -132,12 +132,12 @@ func (fabricCAServices *fabricCA) Reenroll(user sdkApi.User) (bccsp.Key, []byte, // Create signing identity identity, err := fabricCAServices.createSigningIdentity(user) if err != nil { - logger.Infof("Invalid re-enroll request, %s is not a valid user %s\n", user.GetName(), err) + logger.Infof("Invalid re-enroll request, %s is not a valid user %s\n", user.Name(), err) return nil, nil, fmt.Errorf("Reenroll has failed; Cannot create user identity: %s", err) } if identity.GetECert() == nil { - logger.Infof("Invalid re-enroll request for user '%s'. Enrollment cert does not exist %s\n", user.GetName(), err) + logger.Infof("Invalid re-enroll request for user '%s'. Enrollment cert does not exist %s\n", user.Name(), err) return nil, nil, fmt.Errorf("Reenroll has failed; enrollment cert does not exist: %s", err) } @@ -220,8 +220,8 @@ func (fabricCAServices *fabricCA) createSigningIdentity(user sdkApi. return nil, fmt.Errorf("Valid user required to create signing identity") } // Validate enrolment information - cert := user.GetEnrollmentCertificate() - key := user.GetPrivateKey() + cert := user.EnrollmentCertificate() + key := user.PrivateKey() if key == nil || cert == nil { return nil, fmt.Errorf( "Unable to read user enrolment information to create signing identity") diff --git a/pkg/fabric-ca-client/mocks/mockuser.go b/pkg/fabric-ca-client/mocks/mockuser.go index b759ca109b..770e3fe6ce 100644 --- a/pkg/fabric-ca-client/mocks/mockuser.go +++ b/pkg/fabric-ca-client/mocks/mockuser.go @@ -16,7 +16,7 @@ type MockUser struct { name string mspID string roles []string - PrivateKey bccsp.Key // ****This key is temporary We use it to sign transaction until we have tcerts + privateKey bccsp.Key // ****This key is temporary We use it to sign transaction until we have tcerts enrollmentCertificate []byte } @@ -30,21 +30,21 @@ func NewMockUser(name string) api.User { return &MockUser{name: name} } -// GetName ... +// Name ... /** * Get the user name. * @returns {string} The user name. */ -func (u *MockUser) GetName() string { +func (u *MockUser) Name() string { return u.name } -// GetRoles ... +// Roles ... /** * Get the roles. * @returns {[]string} The roles. */ -func (u *MockUser) GetRoles() []string { +func (u *MockUser) Roles() []string { return u.roles } @@ -57,11 +57,11 @@ func (u *MockUser) SetRoles(roles []string) { u.roles = roles } -// GetEnrollmentCertificate ... +// EnrollmentCertificate ... /** * Returns the underlying ECert representing this user’s identity. */ -func (u *MockUser) GetEnrollmentCertificate() []byte { +func (u *MockUser) EnrollmentCertificate() []byte { return u.enrollmentCertificate } @@ -78,15 +78,15 @@ func (u *MockUser) SetEnrollmentCertificate(cert []byte) { * deprecated. */ func (u *MockUser) SetPrivateKey(privateKey bccsp.Key) { - u.PrivateKey = privateKey + u.privateKey = privateKey } -// GetPrivateKey ... +// PrivateKey ... /** * deprecated. */ -func (u *MockUser) GetPrivateKey() bccsp.Key { - return u.PrivateKey +func (u *MockUser) PrivateKey() bccsp.Key { + return u.privateKey } // SetMspID sets the MSP for this user @@ -94,8 +94,8 @@ func (u *MockUser) SetMspID(mspID string) { u.mspID = mspID } -// GetMspID returns the MSP for this user -func (u *MockUser) GetMspID() string { +// MspID returns the MSP for this user +func (u *MockUser) MspID() string { return u.mspID } diff --git a/pkg/fabric-client/channel/channel.go b/pkg/fabric-client/channel/channel.go index a383b066ae..e345dcbe06 100644 --- a/pkg/fabric-client/channel/channel.go +++ b/pkg/fabric-client/channel/channel.go @@ -982,7 +982,7 @@ func CreateTransactionProposal(chaincodeName string, channelID string, return nil, fmt.Errorf("Error loading user from store: %s", err) } - signature, err := fc.SignObjectWithKey(proposalBytes, user.GetPrivateKey(), + signature, err := fc.SignObjectWithKey(proposalBytes, user.PrivateKey(), &bccsp.SHAOpts{}, nil, clientContext.GetCryptoSuite()) if err != nil { return nil, err @@ -1236,7 +1236,7 @@ func (c *channel) SendInstantiateProposal(chaincodeName string, channelID string if err != nil { return nil, "", fmt.Errorf("Error getting creator: %v", err) } - chaincodePolicy, err := buildChaincodePolicy(c.clientContext.GetUserContext().GetMspID()) + chaincodePolicy, err := buildChaincodePolicy(c.clientContext.GetUserContext().MspID()) if err != nil { return nil, "", err } @@ -1271,7 +1271,7 @@ func (c *channel) SignPayload(payload []byte) (*api.SignedEnvelope, error) { return nil, fmt.Errorf("LoadUserFromStateStore returned error: %s", err) } - signature, err := fc.SignObjectWithKey(payload, user.GetPrivateKey(), + signature, err := fc.SignObjectWithKey(payload, user.PrivateKey(), &bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite()) if err != nil { return nil, err @@ -1393,7 +1393,7 @@ func (c *channel) signProposal(proposal *pb.Proposal) (*pb.SignedProposal, error return nil, fmt.Errorf("Error mashalling proposal: %s", err) } - signature, err := fc.SignObjectWithKey(proposalBytes, user.GetPrivateKey(), &bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite()) + signature, err := fc.SignObjectWithKey(proposalBytes, user.PrivateKey(), &bccsp.SHAOpts{}, nil, c.clientContext.GetCryptoSuite()) if err != nil { return nil, fmt.Errorf("Error signing proposal: %s", err) } diff --git a/pkg/fabric-client/client.go b/pkg/fabric-client/client.go index 6e1b06a038..27b3d2b609 100644 --- a/pkg/fabric-client/client.go +++ b/pkg/fabric-client/client.go @@ -151,7 +151,7 @@ func (c *client) SaveUserToStateStore(user api.User, skipPersistence bool) error return fmt.Errorf("user is nil") } - if user.GetName() == "" { + if user.Name() == "" { return fmt.Errorf("user name is empty") } c.userContext = user @@ -160,16 +160,16 @@ func (c *client) SaveUserToStateStore(user api.User, skipPersistence bool) error return fmt.Errorf("stateStore is nil") } userJSON := &fcUser.JSON{ - MspID: user.GetMspID(), - Roles: user.GetRoles(), - PrivateKeySKI: user.GetPrivateKey().SKI(), - EnrollmentCertificate: user.GetEnrollmentCertificate(), + MspID: user.MspID(), + Roles: user.Roles(), + PrivateKeySKI: user.PrivateKey().SKI(), + EnrollmentCertificate: user.EnrollmentCertificate(), } data, err := json.Marshal(userJSON) if err != nil { return fmt.Errorf("Marshal json return error: %v", err) } - err = c.stateStore.SetValue(user.GetName(), data) + err = c.stateStore.SetValue(user.Name(), data) if err != nil { return fmt.Errorf("stateStore SaveUserToStateStore return error: %v", err) } @@ -291,7 +291,7 @@ func (c *client) SignChannelConfig(config []byte) (*common.ConfigSignature, erro // get all the bytes to be signed together, then sign signingBytes := fcutils.ConcatenateBytes(signatureHeaderBytes, config) - signature, err := fc.SignObjectWithKey(signingBytes, user.GetPrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) + signature, err := fc.SignObjectWithKey(signingBytes, user.PrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) if err != nil { return nil, fmt.Errorf("error singing config: %v", err) } @@ -410,7 +410,7 @@ func (c *client) CreateOrUpdateChannel(request *api.CreateChannelRequest, haveEn return fmt.Errorf("error marshaling payload: %v", err) } - signature, err = fc.SignObjectWithKey(payloadBytes, c.userContext.GetPrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) + signature, err = fc.SignObjectWithKey(payloadBytes, c.userContext.PrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) if err != nil { return fmt.Errorf("error singing payload: %v", err) } @@ -533,15 +533,12 @@ func (c *client) InstallChaincode(chaincodeName string, chaincodePath string, ch if err != nil { return nil, "", fmt.Errorf("Error loading user from store: %s", err) } - signature, err := fc.SignObjectWithKey(proposalBytes, user.GetPrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) + signature, err := fc.SignObjectWithKey(proposalBytes, user.PrivateKey(), &bccsp.SHAOpts{}, nil, c.GetCryptoSuite()) if err != nil { return nil, "", err } - signedProposal, err := &pb.SignedProposal{ProposalBytes: proposalBytes, Signature: signature}, nil - if err != nil { - return nil, "", err - } + signedProposal := &pb.SignedProposal{ProposalBytes: proposalBytes, Signature: signature} transactionProposalResponse, err := channel.SendTransactionProposal(&apitxn.TransactionProposal{ SignedProposal: signedProposal, @@ -558,8 +555,8 @@ func (c *client) GetIdentity() ([]byte, error) { if c.userContext == nil { return nil, fmt.Errorf("User is nil") } - serializedIdentity := &msp.SerializedIdentity{Mspid: c.userContext.GetMspID(), - IdBytes: c.userContext.GetEnrollmentCertificate()} + serializedIdentity := &msp.SerializedIdentity{Mspid: c.userContext.MspID(), + IdBytes: c.userContext.EnrollmentCertificate()} identity, err := proto.Marshal(serializedIdentity) if err != nil { return nil, fmt.Errorf("Could not Marshal serializedIdentity, err %s", err) diff --git a/pkg/fabric-client/client_test.go b/pkg/fabric-client/client_test.go index 92ae1b1619..2e6c866808 100644 --- a/pkg/fabric-client/client_test.go +++ b/pkg/fabric-client/client_test.go @@ -78,11 +78,11 @@ func TestClientMethods(t *testing.T) { if user == nil { t.Fatalf("client.LoadUserFromStateStore return nil user") } - if user.GetName() != "someUser" { + if user.Name() != "someUser" { t.Fatalf("client.LoadUserFromStateStore didn't return the right user") } - if user.GetMspID() != testMsp { + if user.MspID() != testMsp { t.Fatalf("client.LoadUserFromStateStore didn't return the right msp") } diff --git a/pkg/fabric-client/events/consumer/consumer.go b/pkg/fabric-client/events/consumer/consumer.go index 8fa573592f..87ebae3edf 100644 --- a/pkg/fabric-client/events/consumer/consumer.go +++ b/pkg/fabric-client/events/consumer/consumer.go @@ -88,7 +88,7 @@ func (ec *eventsClient) send(emsg *ehpb.Event) error { if err != nil { return fmt.Errorf("Error marshaling message: %s", err) } - signature, err := fc.SignObjectWithKey(payload, user.GetPrivateKey(), + signature, err := fc.SignObjectWithKey(payload, user.PrivateKey(), &bccsp.SHAOpts{}, nil, ec.client.GetCryptoSuite()) if err != nil { return fmt.Errorf("Error signing message: %s", err) diff --git a/pkg/fabric-client/mocks/mockuser.go b/pkg/fabric-client/mocks/mockuser.go index c794368811..df8c0dfc2b 100644 --- a/pkg/fabric-client/mocks/mockuser.go +++ b/pkg/fabric-client/mocks/mockuser.go @@ -16,7 +16,7 @@ type MockUser struct { name string mspID string roles []string - PrivateKey bccsp.Key // ****This key is temporary We use it to sign transaction until we have tcerts + privateKey bccsp.Key // ****This key is temporary We use it to sign transaction until we have tcerts enrollmentCertificate []byte } @@ -35,21 +35,21 @@ func NewMockUserWithMSPID(name string, mspid string) api.User { return &MockUser{name: name, mspID: mspid} } -// GetName ... +// Name ... /** * Get the user name. * @returns {string} The user name. */ -func (u *MockUser) GetName() string { +func (u *MockUser) Name() string { return u.name } -// GetRoles ... +// Roles ... /** * Get the roles. * @returns {[]string} The roles. */ -func (u *MockUser) GetRoles() []string { +func (u *MockUser) Roles() []string { return u.roles } @@ -62,11 +62,11 @@ func (u *MockUser) SetRoles(roles []string) { u.roles = roles } -// GetEnrollmentCertificate ... +// EnrollmentCertificate ... /** * Returns the underlying ECert representing this user’s identity. */ -func (u *MockUser) GetEnrollmentCertificate() []byte { +func (u *MockUser) EnrollmentCertificate() []byte { return u.enrollmentCertificate } @@ -83,15 +83,15 @@ func (u *MockUser) SetEnrollmentCertificate(cert []byte) { * deprecated. */ func (u *MockUser) SetPrivateKey(privateKey bccsp.Key) { - u.PrivateKey = privateKey + u.privateKey = privateKey } -// GetPrivateKey ... +// PrivateKey ... /** * deprecated. */ -func (u *MockUser) GetPrivateKey() bccsp.Key { - return u.PrivateKey +func (u *MockUser) PrivateKey() bccsp.Key { + return u.privateKey } // SetMspID sets the MSP for this user @@ -99,8 +99,8 @@ func (u *MockUser) SetMspID(mspID string) { u.mspID = mspID } -// GetMspID returns the MSP for this user -func (u *MockUser) GetMspID() string { +// MspID returns the MSP for this user +func (u *MockUser) MspID() string { return u.mspID } diff --git a/pkg/fabric-client/user/user.go b/pkg/fabric-client/user/user.go index d00ab27b44..9915b973b7 100644 --- a/pkg/fabric-client/user/user.go +++ b/pkg/fabric-client/user/user.go @@ -15,7 +15,7 @@ type user struct { name string mspID string roles []string - PrivateKey bccsp.Key + privateKey bccsp.Key enrollmentCertificate []byte } @@ -35,15 +35,15 @@ func NewUser(name string, mspID string) api.User { return &user{name: name, mspID: mspID} } -// GetName Get the user name. +// Name Get the user name. // @returns {string} The user name. -func (u *user) GetName() string { +func (u *user) Name() string { return u.name } -// GetRoles Get the roles. +// Roles Get the roles. // @returns {[]string} The roles. -func (u *user) GetRoles() []string { +func (u *user) Roles() []string { return u.roles } @@ -53,8 +53,8 @@ func (u *user) SetRoles(roles []string) { u.roles = roles } -// GetEnrollmentCertificate Returns the underlying ECert representing this user’s identity. -func (u *user) GetEnrollmentCertificate() []byte { +// EnrollmentCertificate Returns the underlying ECert representing this user’s identity. +func (u *user) EnrollmentCertificate() []byte { return u.enrollmentCertificate } @@ -66,12 +66,12 @@ func (u *user) SetEnrollmentCertificate(cert []byte) { // SetPrivateKey sets the crypto suite representation of the private key // for this user func (u *user) SetPrivateKey(privateKey bccsp.Key) { - u.PrivateKey = privateKey + u.privateKey = privateKey } -// GetPrivateKey returns the crypto suite representation of the private key -func (u *user) GetPrivateKey() bccsp.Key { - return u.PrivateKey +// PrivateKey returns the crypto suite representation of the private key +func (u *user) PrivateKey() bccsp.Key { + return u.privateKey } // SetMspID sets the MSP for this user @@ -79,8 +79,8 @@ func (u *user) SetMspID(mspID string) { u.mspID = mspID } -// GetMspID returns the MSP for this user -func (u *user) GetMspID() string { +// MspID returns the MSP for this user +func (u *user) MspID() string { return u.mspID } diff --git a/pkg/fabric-client/user/user_test.go b/pkg/fabric-client/user/user_test.go index 4c9d39b3c5..fd42261c61 100644 --- a/pkg/fabric-client/user/user_test.go +++ b/pkg/fabric-client/user/user_test.go @@ -8,23 +8,73 @@ package user import ( "testing" + + "io/ioutil" + + "github.com/hyperledger/fabric-sdk-go/pkg/fabric-ca-client/mocks" ) func TestUserMethods(t *testing.T) { user := NewUser("testUser", "testMSP") - if user.GetName() != "testUser" { + + //test Name + if user.Name() != "testUser" { t.Fatalf("NewUser create wrong user") } + + // test Roles var roles []string roles = append(roles, "admin") roles = append(roles, "user") user.SetRoles(roles) - if user.GetRoles()[0] != "admin" { + if user.Roles()[0] != "admin" { t.Fatalf("user.GetRoles() return wrong user") } - if user.GetRoles()[1] != "user" { + if user.Roles()[1] != "user" { t.Fatalf("user.GetRoles() return wrong user") } + // test PrivateKey + privateKey := &mocks.MockKey{} + + user.SetPrivateKey(privateKey) + + returnKey := user.PrivateKey() + if returnKey == nil { + t.Fatalf("GetKey() after SetKey() returned nil.") + } + + if returnKey != privateKey { + t.Fatalf("user.SetKey() and GetKey() don't return matching keys.") + } + + // test TCerts + var attributes []string + user.GenerateTcerts(1, attributes) // TODO implement test when function is implemented + + // test EnrolmentCert + cert := readCert(t) + user.SetEnrollmentCertificate(cert) + setCert := user.EnrollmentCertificate() + if len(cert) != len(setCert) { + t.Fatal("user.SetEnrollmentCertificate did not set the same cert.") + } + + // test MSP + user.SetMspID("test") + mspID := user.MspID() + if mspID != "test" { + t.Fatal("user.SetMspID Failed to MSP.") + } + +} + +// Reads a random cert for testing +func readCert(t *testing.T) []byte { + cert, err := ioutil.ReadFile("../../../test/fixtures/root.pem") + if err != nil { + t.Fatalf("Error reading cert: %s", err.Error()) + } + return cert }