Skip to content

Commit d91598b

Browse files
committed
Refactor identity code and check for MSP ID
This change set refactors the code inside of the MSP that is called whenever a serialized identity has to be turned into an identity instance. The code was previously repeating the deserialization into a SerializedIdentity struct twice - once by the MSPManager and once by the MSP. Now the MSP implementation exposes a function that turns SerializedIdentity.IdBytes into an identity. This method is called by the public method that takes the full serialized identity, deserializes it into a SerializedIdentity struct and calls the private method passing in SerializedIdentity.IdBytes. Also, the code now checks that SerializedIdentity.Mspid mathces the ID of the MSP that performs the deserialization. Change-Id: Ie76c500e93b9acf1ea9c69ccc8db46f1e830d204 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 8762744 commit d91598b

File tree

8 files changed

+121
-51
lines changed

8 files changed

+121
-51
lines changed

common/cauthdsl/cauthdsl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
var cauthdslLogger = logging.MustGetLogger("cauthdsl")
3131

3232
// compile recursively builds a go evaluatable function corresponding to the policy specified
33-
func compile(policy *cb.SignaturePolicy, identities []*cb.MSPPrincipal, deserializer msp.Common) (func([]*cb.SignedData, []bool) bool, error) {
33+
func compile(policy *cb.SignaturePolicy, identities []*cb.MSPPrincipal, deserializer msp.IdentityDeserializer) (func([]*cb.SignedData, []bool) bool, error) {
3434
switch t := policy.Type.(type) {
3535
case *cb.SignaturePolicy_From:
3636
policies := make([]func([]*cb.SignedData, []bool) bool, len(t.From.Policies))
@@ -159,7 +159,7 @@ func toSignedData(data [][]byte, identities [][]byte, signatures [][]byte) ([]*c
159159
type mockDeserializer struct {
160160
}
161161

162-
func NewMockDeserializer() msp.Common {
162+
func NewMockDeserializer() msp.IdentityDeserializer {
163163
return &mockDeserializer{}
164164
}
165165

common/cauthdsl/policy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import (
2828
)
2929

3030
type provider struct {
31-
deserializer msp.Common
31+
deserializer msp.IdentityDeserializer
3232
}
3333

3434
// NewProviderImpl provides a policy generator for cauthdsl type policies
35-
func NewPolicyProvider(deserializer msp.Common) policies.Provider {
35+
func NewPolicyProvider(deserializer msp.IdentityDeserializer) policies.Provider {
3636
return &provider{
3737
deserializer: deserializer,
3838
}

core/common/validation/msgvalidation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func checkSignatureFromCreator(creatorBytes []byte, sig []byte, msg []byte, Chai
120120
return fmt.Errorf("Nil arguments")
121121
}
122122

123-
mspObj := mspmgmt.GetMSPCommon(ChainID)
123+
mspObj := mspmgmt.GetIdentityDeserializer(ChainID)
124124
if mspObj == nil {
125125
return fmt.Errorf("could not get msp for chain [%s]", ChainID)
126126
}

msp/mgmt/mgmt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func GetLocalMSP() msp.MSP {
111111
return lclMsp
112112
}
113113

114-
//GetMSPCommon returns the common interface
115-
func GetMSPCommon(chainID string) msp.Common {
114+
// GetIdentityDeserializer returns the IdentityDeserializer for the given chain
115+
func GetIdentityDeserializer(chainID string) msp.IdentityDeserializer {
116116
if chainID == "" {
117117
return GetLocalMSP()
118118
}

msp/msp.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
// FIXME: we need better comments on the interfaces!!
2626
// FIXME: we need better comments on the interfaces!!
2727

28-
//Common is implemented by both MSPManger and MSP
29-
type Common interface {
28+
// IdentityDeserializer is implemented by both MSPManger and MSP
29+
type IdentityDeserializer interface {
3030
// DeserializeIdentity deserializes an identity.
3131
// Deserialization will fail if the identity is associated to
3232
// an msp that is different from this one that is performing
@@ -55,8 +55,8 @@ type Common interface {
5555
// This object is immutable, it is initialized once and never changed.
5656
type MSPManager interface {
5757

58-
// Common interface needs to be implemented by MSPManager
59-
Common
58+
// IdentityDeserializer interface needs to be implemented by MSPManager
59+
IdentityDeserializer
6060

6161
// Setup the MSP manager instance according to configuration information
6262
Setup(msps []*msp.MSPConfig) error
@@ -69,8 +69,8 @@ type MSPManager interface {
6969
// to accommodate peer functionality
7070
type MSP interface {
7171

72-
// Common interface needs to be implemented by MSP
73-
Common
72+
// IdentityDeserializer interface needs to be implemented by MSP
73+
IdentityDeserializer
7474

7575
// Setup the MSP instance according to configuration information
7676
Setup(config *msp.MSPConfig) error

msp/msp_test.go

+91-22
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import (
44
"os"
55
"reflect"
66
"testing"
7-
)
87

9-
var localMsp MSP
8+
"fmt"
9+
10+
"github.com/golang/protobuf/proto"
11+
"github.com/hyperledger/fabric/protos/msp"
12+
"github.com/stretchr/testify/assert"
13+
)
1014

1115
func TestNoopMSP(t *testing.T) {
1216
noopmsp := NewNoopMsp()
@@ -57,26 +61,6 @@ func TestMSPSetupBad(t *testing.T) {
5761
}
5862
}
5963

60-
func TestMSPSetupGood(t *testing.T) {
61-
conf, err := GetLocalMspConfig("./sampleconfig/")
62-
if err != nil {
63-
t.Fatalf("Setup should have succeeded, got err %s instead", err)
64-
return
65-
}
66-
67-
localMsp, err = NewBccspMsp()
68-
if err != nil {
69-
t.Fatalf("Constructor for msp should have succeeded, got err %s instead", err)
70-
return
71-
}
72-
73-
err = localMsp.Setup(conf)
74-
if err != nil {
75-
t.Fatalf("Setup for msp should have succeeded, got err %s instead", err)
76-
return
77-
}
78-
}
79-
8064
func TestGetIdentities(t *testing.T) {
8165
_, err := localMsp.GetDefaultSigningIdentity()
8266
if err != nil {
@@ -116,6 +100,61 @@ func TestSerializeIdentities(t *testing.T) {
116100
}
117101
}
118102

103+
func TestSerializeIdentitiesWithWrongMSP(t *testing.T) {
104+
id, err := localMsp.GetDefaultSigningIdentity()
105+
if err != nil {
106+
t.Fatalf("GetSigningIdentity should have succeeded, got err %s", err)
107+
return
108+
}
109+
110+
serializedID, err := id.Serialize()
111+
if err != nil {
112+
t.Fatalf("Serialize should have succeeded, got err %s", err)
113+
return
114+
}
115+
116+
sid := &SerializedIdentity{}
117+
err = proto.Unmarshal(serializedID, sid)
118+
assert.NoError(t, err)
119+
120+
sid.Mspid += "BARF"
121+
122+
serializedID, err = proto.Marshal(sid)
123+
assert.NoError(t, err)
124+
125+
_, err = localMsp.DeserializeIdentity(serializedID)
126+
assert.Error(t, err)
127+
}
128+
129+
func TestSerializeIdentitiesWithMSPManager(t *testing.T) {
130+
id, err := localMsp.GetDefaultSigningIdentity()
131+
if err != nil {
132+
t.Fatalf("GetSigningIdentity should have succeeded, got err %s", err)
133+
return
134+
}
135+
136+
serializedID, err := id.Serialize()
137+
if err != nil {
138+
t.Fatalf("Serialize should have succeeded, got err %s", err)
139+
return
140+
}
141+
142+
_, err = mspMgr.DeserializeIdentity(serializedID)
143+
assert.NoError(t, err)
144+
145+
sid := &SerializedIdentity{}
146+
err = proto.Unmarshal(serializedID, sid)
147+
assert.NoError(t, err)
148+
149+
sid.Mspid += "BARF"
150+
151+
serializedID, err = proto.Marshal(sid)
152+
assert.NoError(t, err)
153+
154+
_, err = mspMgr.DeserializeIdentity(serializedID)
155+
assert.Error(t, err)
156+
}
157+
119158
func TestSignAndVerify(t *testing.T) {
120159
id, err := localMsp.GetDefaultSigningIdentity()
121160
if err != nil {
@@ -194,7 +233,37 @@ func TestSignAndVerify_longMessage(t *testing.T) {
194233
}
195234
}
196235

236+
var conf *msp.MSPConfig
237+
var localMsp MSP
238+
var mspMgr MSPManager
239+
197240
func TestMain(m *testing.M) {
241+
var err error
242+
conf, err = GetLocalMspConfig("./sampleconfig/")
243+
if err != nil {
244+
fmt.Printf("Setup should have succeeded, got err %s instead", err)
245+
os.Exit(-1)
246+
}
247+
248+
localMsp, err = NewBccspMsp()
249+
if err != nil {
250+
fmt.Printf("Constructor for msp should have succeeded, got err %s instead", err)
251+
os.Exit(-1)
252+
}
253+
254+
err = localMsp.Setup(conf)
255+
if err != nil {
256+
fmt.Printf("Setup for msp should have succeeded, got err %s instead", err)
257+
os.Exit(-1)
258+
}
259+
260+
mspMgr = NewMSPManager()
261+
err = mspMgr.Setup([]*msp.MSPConfig{conf})
262+
if err != nil {
263+
fmt.Printf("Setup for msp manager should have succeeded, got err %s instead", err)
264+
os.Exit(-1)
265+
}
266+
198267
retVal := m.Run()
199268
os.Exit(retVal)
200269
}

msp/mspimpl.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -250,29 +250,29 @@ func (msp *bccspmsp) Validate(id Identity) error {
250250
}
251251
}
252252

253-
// DeserializeIdentity returns an Identity
254-
// instance that was marshalled to the supplied byte array
253+
// DeserializeIdentity returns an Identity given the byte-level
254+
// representation of a SerializedIdentity struct
255255
func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error) {
256256
mspLogger.Infof("Obtaining identity")
257257

258-
// FIXME: this is not ideal, because the manager already does this
259-
// unmarshalling if we go through it; however the local MSP does
260-
// not have a manager and in case it has to deserialize an identity,
261-
// it will have to do the whole thing by itself; for now I've left
262-
// it this way but we can introduce a local MSP manager and fix it
263-
// more nicely
264-
265258
// We first deserialize to a SerializedIdentity to get the MSP ID
266259
sId := &SerializedIdentity{}
267260
err := proto.Unmarshal(serializedID, sId)
268261
if err != nil {
269262
return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
270263
}
271264

272-
// TODO: check that sId.Mspid is equal to this msp'id as per contract of the interface.
265+
if sId.Mspid != msp.name {
266+
return nil, fmt.Errorf("Expected MSP ID %s, received %s", msp.name, sId.Mspid)
267+
}
268+
269+
return msp.deserializeIdentityInternal(sId.IdBytes)
270+
}
273271

272+
// deserializeIdentityInternal returns an identity given its byte-level representation
273+
func (msp *bccspmsp) deserializeIdentityInternal(serializedIdentity []byte) (Identity, error) {
274274
// This MSP will always deserialize certs this way
275-
bl, _ := pem.Decode(sId.IdBytes)
275+
bl, _ := pem.Decode(serializedIdentity)
276276
if bl == nil {
277277
return nil, fmt.Errorf("Could not decode the PEM structure")
278278
}
@@ -285,9 +285,6 @@ func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error)
285285
// (e.g. the Issuer.OU or the Subject.OU) match with the
286286
// MSP id that this MSP has; otherwise it might be an attack
287287
// TODO!
288-
// TODO!
289-
// TODO!
290-
// TODO!
291288
// We can't do it yet because there is no standardized way
292289
// (yet) to encode the MSP ID into the x.509 body of a cert
293290

msp/mspmgrimpl.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ func (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, e
116116
return nil, fmt.Errorf("MSP %s is unknown", sId.Mspid)
117117
}
118118

119-
// if we have this MSP, we ask it to deserialize
120-
return msp.DeserializeIdentity(serializedID)
119+
switch t := msp.(type) {
120+
case *bccspmsp:
121+
return t.deserializeIdentityInternal(sId.IdBytes)
122+
default:
123+
return t.DeserializeIdentity(serializedID)
124+
}
121125
}

0 commit comments

Comments
 (0)