Skip to content

Commit

Permalink
[FAB-5170] Refactor User and improve test coverage
Browse files Browse the repository at this point in the history
Change-Id: Ia919c343562cb397fd718c0bfee9d7a4af107109
Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
  • Loading branch information
Baha Shaaban committed Jul 4, 2017
1 parent 4793719 commit 73df8f6
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 74 deletions.
10 changes: 5 additions & 5 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/fabric-ca-client/fabricca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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")
Expand Down
26 changes: 13 additions & 13 deletions pkg/fabric-ca-client/mocks/mockuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -78,24 +78,24 @@ 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
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
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/fabric-client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
27 changes: 12 additions & 15 deletions pkg/fabric-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/fabric-client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/fabric-client/events/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 13 additions & 13 deletions pkg/fabric-client/mocks/mockuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -83,24 +83,24 @@ 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
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
}

Expand Down
Loading

0 comments on commit 73df8f6

Please sign in to comment.