Skip to content

Commit 6789db6

Browse files
yacovmdenyeart
authored andcommitted
[FAB-10031] Add option to disable discovery auth cache
This change set adds an option to disable the authentication cache of the discovery service. Change-Id: Ic580952ce370dd702e5132e601a529b5fe57f9e6 Signed-off-by: yacovm <yacovm@il.ibm.com>
1 parent 0ec10cb commit 6789db6

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

discovery/authcache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type acSupport interface {
3636
}
3737

3838
type authCacheConfig struct {
39+
enabled bool
3940
// maxCacheSize is the maximum size of the cache, after which
4041
// a purge takes place
4142
maxCacheSize int
@@ -64,6 +65,9 @@ func newAuthCache(s acSupport, conf authCacheConfig) *authCache {
6465
// Eligible returns whether the given peer is eligible for receiving
6566
// service from the discovery service for a given channel
6667
func (ac *authCache) EligibleForService(channel string, data common.SignedData) error {
68+
if !ac.conf.enabled {
69+
return ac.acSupport.EligibleForService(channel, data)
70+
}
6771
// Check whether we already have a cache for this channel
6872
ac.RLock()
6973
cache := ac.credentialCache[channel]

discovery/authcache_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ func (as *mockAcSupport) ConfigSequence(channel string) uint64 {
4545
return as.Called(channel).Get(0).(uint64)
4646
}
4747

48+
func TestCacheDisabled(t *testing.T) {
49+
sd := common.SignedData{
50+
Data: []byte{1, 2, 3},
51+
Identity: []byte("authorizedIdentity"),
52+
Signature: []byte{1, 2, 3},
53+
}
54+
55+
as := &mockAcSupport{}
56+
as.On("ConfigSequence", "foo").Return(uint64(0))
57+
as.On("EligibleForService", "foo", sd).Return(nil)
58+
cache := newAuthCache(as, authCacheConfig{maxCacheSize: 100, purgeRetentionRatio: 0.5})
59+
60+
// Call the cache twice with the same argument and ensure the call isn't cached
61+
cache.EligibleForService("foo", sd)
62+
cache.EligibleForService("foo", sd)
63+
as.AssertNumberOfCalls(t, "EligibleForService", 2)
64+
}
65+
4866
func TestCacheUsage(t *testing.T) {
4967
as := &mockAcSupport{}
5068
as.On("ConfigSequence", "foo").Return(uint64(0))
@@ -166,7 +184,7 @@ func TestCacheConfigChange(t *testing.T) {
166184

167185
func TestCachePurgeCache(t *testing.T) {
168186
as := &mockAcSupport{}
169-
cache := newAuthCache(as, authCacheConfig{maxCacheSize: 4, purgeRetentionRatio: 0.75})
187+
cache := newAuthCache(as, authCacheConfig{maxCacheSize: 4, purgeRetentionRatio: 0.75, enabled: true})
170188
as.On("ConfigSequence", "mychannel").Return(uint64(0))
171189

172190
// Warm up the cache - attempt to place 4 identities to fill it up
@@ -258,5 +276,5 @@ func TestCacheConcurrentConfigUpdate(t *testing.T) {
258276
}
259277

260278
func defaultConfig() authCacheConfig {
261-
return authCacheConfig{maxCacheSize: defaultMaxCacheSize, purgeRetentionRatio: defaultRetentionRatio}
279+
return authCacheConfig{maxCacheSize: defaultMaxCacheSize, purgeRetentionRatio: defaultRetentionRatio, enabled: true}
262280
}

discovery/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package discovery
99
import (
1010
"bytes"
1111
"encoding/hex"
12+
"fmt"
1213

1314
"github.com/hyperledger/fabric/common/flogging"
1415
"github.com/hyperledger/fabric/common/util"
@@ -45,17 +46,27 @@ type service struct {
4546
// Config defines the configuration of the discovery service
4647
type Config struct {
4748
TLS bool
49+
AuthCacheEnabled bool
4850
AuthCacheMaxSize int
4951
AuthCachePurgeRetentionRatio float64
5052
}
5153

54+
// String returns a string representation of this Config
55+
func (c Config) String() string {
56+
if c.AuthCacheEnabled {
57+
return fmt.Sprintf("TLS: %t, authCacheMaxSize: %d, authCachePurgeRatio: %f", c.TLS, c.AuthCacheMaxSize, c.AuthCachePurgeRetentionRatio)
58+
}
59+
return fmt.Sprintf("TLS: %t, auth cache disabled", c.TLS)
60+
}
61+
5262
// peerMapping maps PKI-IDs to Peers
5363
type peerMapping map[string]*discovery.Peer
5464

5565
// NewService creates a new discovery service instance
5666
func NewService(config Config, sup Support) *service {
5767
s := &service{
5868
auth: newAuthCache(sup, authCacheConfig{
69+
enabled: config.AuthCacheEnabled,
5970
maxCacheSize: config.AuthCacheMaxSize,
6071
purgeRetentionRatio: config.AuthCachePurgeRetentionRatio,
6172
}),
@@ -69,6 +80,7 @@ func NewService(config Config, sup Support) *service {
6980
s.localDispatchers = map[discovery.QueryType]dispatcher{
7081
discovery.LocalMembershipQueryType: s.localMembershipResponse,
7182
}
83+
logger.Info("Created with config", config)
7284
return s
7385
}
7486

discovery/service_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,24 @@ import (
2424
"golang.org/x/net/context"
2525
)
2626

27+
func TestConfig(t *testing.T) {
28+
for _, trueOfFalse := range []bool{true, false} {
29+
conf := Config{
30+
AuthCacheEnabled: trueOfFalse,
31+
AuthCachePurgeRetentionRatio: 0.5,
32+
AuthCacheMaxSize: 42,
33+
}
34+
service := NewService(conf, &mockSupport{})
35+
assert.Equal(t, trueOfFalse, service.auth.conf.enabled)
36+
assert.Equal(t, 42, service.auth.conf.maxCacheSize)
37+
assert.Equal(t, 0.5, service.auth.conf.purgeRetentionRatio)
38+
}
39+
}
40+
2741
func TestService(t *testing.T) {
28-
conf := Config{}
42+
conf := Config{
43+
AuthCacheEnabled: true,
44+
}
2945
ctx := context.Background()
3046
req := &discovery.Request{
3147
Authentication: &discovery.AuthInfo{

peer/node/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ func registerDiscoveryService(peerServer *comm.GRPCServer, mcs api.MessageCrypto
433433
support := discsupport.NewDiscoverySupport(acl, gSup, ea, confSup, acl)
434434
svc := discovery.NewService(discovery.Config{
435435
TLS: peerServer.TLSEnabled(),
436+
AuthCacheEnabled: viper.GetBool("peer.discovery.authCacheEnabled"),
436437
AuthCacheMaxSize: viper.GetInt("peer.discovery.authCacheMaxSize"),
437438
AuthCachePurgeRetentionRatio: viper.GetFloat64("peer.discovery.authCachePurgeRetentionRatio"),
438439
}, support)

sampleconfig/core.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ peer:
422422
# what possible sets of peers satisfy the endorsement policy.
423423
discovery:
424424
enabled: true
425+
# Whether the authentication cache is enabled or not.
426+
authCacheEnabled: true
425427
# The maximum size of the cache, after which a purge takes place
426428
authCacheMaxSize: 1000
427429
# The proportion (0 to 1) of entries that remain in the cache after the cache is purged due to overpopulation

0 commit comments

Comments
 (0)