Skip to content

Commit bf4e455

Browse files
adecaromuralisrini
authored andcommitted
[FAB-5721] ACLProvider at QSCC
This change-set does following: - It modifed the QSCC to use ACLProvider for access control. Tests have been updated to validate the changes. removed sync.Once so we can use RegisterACLProvider for UT . Uses mocks framework for all tests (removes MockACLProvider2). . Put back sync.Once and remove SetACLProvider now we use the mock for everything . refactor query_test.go to use the mock Change-Id: I5f5346c131d6cf715f23a0717fed82c76f58dd43 Signed-off-by: Angelo De Caro <adc@zurich.ibm.com> Signed-off-by: Srinivasan Muralidharan <srinivasan.muralidharan99@gmail.com>
1 parent 0e495ee commit bf4e455

File tree

4 files changed

+171
-63
lines changed

4 files changed

+171
-63
lines changed

core/aclmgmt/aclmgmt_test.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
23
Copyright IBM Corp. All Rights Reserved.
34
45
SPDX-License-Identifier: Apache-2.0
@@ -7,34 +8,31 @@ SPDX-License-Identifier: Apache-2.0
78
package aclmgmt
89

910
import (
10-
"fmt"
1111
"sync"
1212
"testing"
1313

14-
"github.com/hyperledger/fabric/core/ledger"
15-
"github.com/hyperledger/fabric/protos/common"
14+
"github.com/hyperledger/fabric/core/aclmgmt/mocks"
1615
pb "github.com/hyperledger/fabric/protos/peer"
1716
"github.com/stretchr/testify/assert"
18-
)
19-
20-
type mockACLProvider struct {
21-
retErr error
22-
}
2317

24-
func (m *mockACLProvider) CheckACL(resName string, channelID string, idinfo interface{}) error {
25-
return m.retErr
26-
}
27-
28-
func (e *mockACLProvider) GenerateSimulationResults(txEnvelop *common.Envelope, simulator ledger.TxSimulator) error {
29-
return nil
30-
}
18+
"github.com/pkg/errors"
19+
)
3120

3221
//treat each test as an independent isolated one
3322
func reinit() {
3423
aclProvider = nil
3524
once = sync.Once{}
3625
}
3726

27+
func registerACLProvider() *mocks.MockACLProvider {
28+
aclProv := &mocks.MockACLProvider{}
29+
aclProv.Reset()
30+
31+
RegisterACLProvider(aclProv)
32+
33+
return aclProv
34+
}
35+
3836
func TestACLProcessor(t *testing.T) {
3937
reinit()
4038
assert.NotNil(t, GetConfigTxProcessor().GenerateSimulationResults(nil, nil), "Expected non-nil error")
@@ -82,14 +80,18 @@ func TestOverride(t *testing.T) {
8280

8381
func TestWithProvider(t *testing.T) {
8482
reinit()
85-
RegisterACLProvider(&mockACLProvider{})
86-
err := GetACLProvider().CheckACL(PROPOSE, "somechain", &pb.SignedProposal{})
83+
aclprov := registerACLProvider()
84+
prop := &pb.SignedProposal{}
85+
aclprov.On("CheckACL", PROPOSE, "somechain", prop).Return(nil)
86+
err := GetACLProvider().CheckACL(PROPOSE, "somechain", prop)
8787
assert.NoError(t, err)
8888
}
8989

9090
func TestBadACL(t *testing.T) {
9191
reinit()
92-
RegisterACLProvider(&mockACLProvider{retErr: fmt.Errorf("badacl")})
93-
err := GetACLProvider().CheckACL(PROPOSE, "somechain", &pb.SignedProposal{})
92+
aclprov := registerACLProvider()
93+
prop := &pb.SignedProposal{}
94+
aclprov.On("CheckACL", PROPOSE, "somechain", prop).Return(errors.New("badacl"))
95+
err := GetACLProvider().CheckACL(PROPOSE, "somechain", prop)
9496
assert.Error(t, err, "Expected error")
9597
}

core/aclmgmt/mocks/mocks.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package mocks
8+
9+
import (
10+
"testing"
11+
12+
"github.com/hyperledger/fabric/core/ledger"
13+
"github.com/hyperledger/fabric/protos/common"
14+
"github.com/stretchr/testify/mock"
15+
)
16+
17+
type MockACLProvider struct {
18+
//create a mock object that can be reset after
19+
//registering a MockACLProvider with aclmgmt
20+
mock *mock.Mock
21+
}
22+
23+
//clear the mock so we can start afresh
24+
func (m *MockACLProvider) Reset() {
25+
m.mock = &mock.Mock{}
26+
}
27+
28+
func (m *MockACLProvider) CheckACL(resName string, channelID string, idinfo interface{}) error {
29+
args := m.mock.Called(resName, channelID, idinfo)
30+
return args.Error(0)
31+
}
32+
33+
func (m *MockACLProvider) GenerateSimulationResults(txEnvelop *common.Envelope, simulator ledger.TxSimulator) error {
34+
return nil
35+
}
36+
37+
//On overrider the mock method for convenience
38+
func (m *MockACLProvider) On(methodName string, arguments ...interface{}) *mock.Call {
39+
return m.mock.On(methodName, arguments...)
40+
}
41+
42+
//AssertExpectations overrider the mock method for convenience
43+
func (m *MockACLProvider) AssertExpectations(t *testing.T) {
44+
m.mock.AssertExpectations(t)
45+
}

core/scc/qscc/query.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ import (
2222

2323
"github.com/hyperledger/fabric/common/flogging"
2424

25-
"github.com/hyperledger/fabric/common/policies"
25+
"github.com/hyperledger/fabric/core/aclmgmt"
2626
"github.com/hyperledger/fabric/core/chaincode/shim"
2727
"github.com/hyperledger/fabric/core/ledger"
2828
"github.com/hyperledger/fabric/core/peer"
29-
"github.com/hyperledger/fabric/core/policy"
30-
"github.com/hyperledger/fabric/msp/mgmt"
3129
pb "github.com/hyperledger/fabric/protos/peer"
3230
"github.com/hyperledger/fabric/protos/utils"
3331
)
@@ -38,7 +36,6 @@ import (
3836
// - GetBlockByHash returns a block
3937
// - GetTransactionByID returns a transaction
4038
type LedgerQuerier struct {
41-
policyChecker policy.PolicyChecker
4239
}
4340

4441
var qscclogger = flogging.MustGetLogger("qscc")
@@ -58,13 +55,6 @@ const (
5855
func (e *LedgerQuerier) Init(stub shim.ChaincodeStubInterface) pb.Response {
5956
qscclogger.Info("Init QSCC")
6057

61-
// Init policy checker for access control
62-
e.policyChecker = policy.NewPolicyChecker(
63-
peer.NewChannelPolicyManagerGetter(),
64-
mgmt.GetLocalMSP(),
65-
mgmt.NewLocalMSPPrincipalGetter(),
66-
)
67-
6858
return shim.Success(nil)
6959
}
7060

@@ -103,8 +93,9 @@ func (e *LedgerQuerier) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
10393
}
10494

10595
// 2. check the channel reader policy
106-
if err = e.policyChecker.CheckPolicy(cid, policies.ChannelApplicationReaders, sp); err != nil {
107-
return shim.Error(fmt.Sprintf("Authorization request failed %s: %s", cid, err))
96+
res := getACLResource(fname)
97+
if err = aclmgmt.GetACLProvider().CheckACL(res, cid, sp); err != nil {
98+
return shim.Error(fmt.Sprintf("Authorization request for [%s][%cid] failed: [%s]", fname, cid, err))
10899
}
109100

110101
switch fname {
@@ -216,3 +207,7 @@ func getBlockByTxID(vledger ledger.PeerLedger, rawTxID []byte) pb.Response {
216207

217208
return shim.Success(bytes)
218209
}
210+
211+
func getACLResource(fname string) string {
212+
return "QSCC." + fname
213+
}

0 commit comments

Comments
 (0)